kio Library API Documentation

kdatatool.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
00003    Copyright (C) 2001 David Faure <david@mandrakesoft.com>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018    Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <qpixmap.h>
00022 #include <qfile.h>
00023 #include "kdatatool.h"
00024 #include <kinstance.h>
00025 #include <ktrader.h>
00026 
00027 #include <kstandarddirs.h>
00028 
00029 #include <klibloader.h>
00030 #include <kdebug.h>
00031 
00032 /*************************************************
00033  *
00034  * KDataToolInfo
00035  *
00036  *************************************************/
00037 
00038 KDataToolInfo::KDataToolInfo()
00039 {
00040     m_service = 0;
00041 }
00042 
00043 KDataToolInfo::KDataToolInfo( const KService::Ptr& service, KInstance* instance )
00044 {
00045     m_service = service;
00046     m_instance = instance;
00047 
00048     if ( !!m_service && !m_service->serviceTypes().contains( "KDataTool" ) )
00049     {
00050         kdDebug(30003) << "The service " << m_service->name().latin1()
00051                        << " does not feature the service type KDataTool" << endl;
00052         m_service = 0;
00053     }
00054 }
00055 
00056 KDataToolInfo::KDataToolInfo( const KDataToolInfo& info )
00057 {
00058     m_service = info.service();
00059     m_instance = info.instance();
00060 }
00061 
00062 KDataToolInfo& KDataToolInfo::operator= ( const KDataToolInfo& info )
00063 {
00064     m_service = info.service();
00065     m_instance = info.instance();
00066     return *this;
00067 }
00068 
00069 QString KDataToolInfo::dataType() const
00070 {
00071     if ( !m_service )
00072         return QString::null;
00073 
00074     return m_service->property( "DataType" ).toString();
00075 }
00076 
00077 QStringList KDataToolInfo::mimeTypes() const
00078 {
00079     if ( !m_service )
00080         return QStringList();
00081 
00082     return m_service->property( "DataMimeTypes" ).toStringList();
00083 }
00084 
00085 bool KDataToolInfo::isReadOnly() const
00086 {
00087     if ( !m_service )
00088         return TRUE;
00089 
00090     return m_service->property( "ReadOnly" ).toBool();
00091 }
00092 
00093 QPixmap KDataToolInfo::icon() const
00094 {
00095     if ( !m_service )
00096         return QPixmap();
00097 
00098     QPixmap pix;
00099     QStringList lst = KGlobal::dirs()->resourceDirs("icon");
00100     QStringList::ConstIterator it = lst.begin();
00101     while (!pix.load( *it + "/" + m_service->icon() ) && it != lst.end() )
00102         it++;
00103 
00104     return pix;
00105 }
00106 
00107 QPixmap KDataToolInfo::miniIcon() const
00108 {
00109     if ( !m_service )
00110         return QPixmap();
00111 
00112     QPixmap pix;
00113     QStringList lst = KGlobal::dirs()->resourceDirs("mini");
00114     QStringList::ConstIterator it = lst.begin();
00115     while (!pix.load( *it + "/" + m_service->icon() ) && it != lst.end() )
00116         it++;
00117 
00118     return pix;
00119 }
00120 
00121 QString KDataToolInfo::iconName() const
00122 {
00123     if ( !m_service )
00124         return QString::null;
00125     return m_service->icon();
00126 }
00127 
00128 QStringList KDataToolInfo::commands() const
00129 {
00130     if ( !m_service )
00131         return QString::null;
00132 
00133     return m_service->property( "Commands" ).toStringList();
00134 }
00135 
00136 QStringList KDataToolInfo::userCommands() const
00137 {
00138     if ( !m_service )
00139         return QString::null;
00140 
00141     return QStringList::split( ',', m_service->comment() );
00142 }
00143 
00144 KDataTool* KDataToolInfo::createTool( QObject* parent, const char* name ) const
00145 {
00146     if ( !m_service )
00147         return 0;
00148 
00149     KLibFactory* factory = KLibLoader::self()->factory( QFile::encodeName(m_service->library()) );
00150 
00151     if( !factory )
00152         return 0;
00153 
00154     QObject* obj = factory->create( parent, name );
00155     if ( !obj || !obj->inherits( "KDataTool" ) )
00156     {
00157         delete obj;
00158         return 0;
00159     }
00160     KDataTool * tool = static_cast<KDataTool *>(obj);
00161     tool->setInstance( m_instance );
00162     return tool;
00163 }
00164 
00165 KService::Ptr KDataToolInfo::service() const
00166 {
00167     return m_service;
00168 }
00169 
00170 QValueList<KDataToolInfo> KDataToolInfo::query( const QString& datatype, const QString& mimetype, KInstance* instance )
00171 {
00172     QValueList<KDataToolInfo> lst;
00173 
00174     QString constr;
00175 
00176     if ( !datatype.isEmpty() )
00177     {
00178         constr = QString::fromLatin1( "DataType == '%1'" ).arg( datatype );
00179     }
00180     if ( !mimetype.isEmpty() )
00181     {
00182         QString tmp = QString::fromLatin1( "'%1' in DataMimeTypes" ).arg( mimetype );
00183         if ( constr.isEmpty() )
00184             constr = tmp;
00185         else
00186             constr = constr + " and " + tmp;
00187     }
00188 /* Bug in KTrader ? Test with HEAD-kdelibs!
00189     if ( instance )
00190     {
00191         QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg( instance->instanceName() );
00192         if ( constr.isEmpty() )
00193             constr = tmp;
00194         else
00195             constr = constr + " and " + tmp;
00196     } */
00197 
00198     // Query the trader
00199     //kdDebug() << "KDataToolInfo::query " << constr << endl;
00200     KTrader::OfferList offers = KTrader::self()->query( "KDataTool", constr );
00201 
00202     KTrader::OfferList::ConstIterator it = offers.begin();
00203     for( ; it != offers.end(); ++it )
00204     {
00205         // Temporary replacement for the non-working trader query above
00206         if ( !instance || !(*it)->property("ExcludeFrom").toStringList()
00207              .contains( instance->instanceName() ) )
00208             lst.append( KDataToolInfo( *it, instance ) );
00209         else
00210             kdDebug() << (*it)->entryPath() << " excluded." << endl;
00211     }
00212 
00213     return lst;
00214 }
00215 
00216 bool KDataToolInfo::isValid() const
00217 {
00218     return( m_service );
00219 }
00220 
00221 /*************************************************
00222  *
00223  * KDataToolAction
00224  *
00225  *************************************************/
00226 KDataToolAction::KDataToolAction( const QString & text, const KDataToolInfo & info, const QString & command,
00227                                     QObject * parent, const char * name )
00228     : KAction( text, info.iconName(), 0, parent, name ),
00229       m_command( command ),
00230       m_info( info )
00231 {
00232 }
00233 
00234 void KDataToolAction::slotActivated()
00235 {
00236     emit toolActivated( m_info, m_command );
00237 }
00238 
00239 QPtrList<KAction> KDataToolAction::dataToolActionList( const QValueList<KDataToolInfo> & tools, const QObject *receiver, const char* slot )
00240 {
00241     QPtrList<KAction> actionList;
00242     if ( tools.isEmpty() )
00243         return actionList;
00244 
00245     actionList.append( new KActionSeparator() );
00246     QValueList<KDataToolInfo>::ConstIterator entry = tools.begin();
00247     for( ; entry != tools.end(); ++entry )
00248     {
00249         QStringList userCommands = (*entry).userCommands();
00250         QStringList commands = (*entry).commands();
00251         Q_ASSERT(!commands.isEmpty());
00252         if ( commands.count() != userCommands.count() )
00253             kdWarning() << "KDataTool desktop file error (" << (*entry).service()->entryPath()
00254                         << "). " << commands.count() << " commands and "
00255                         << userCommands.count() << " descriptions." << endl;
00256         QStringList::ConstIterator uit = userCommands.begin();
00257         QStringList::ConstIterator cit = commands.begin();
00258         for (; uit != userCommands.end() && cit != commands.end(); ++uit, ++cit )
00259         {
00260             //kdDebug() << "creating action " << *uit << " " << *cit << endl;
00261             KDataToolAction * action = new KDataToolAction( *uit, *entry, *cit );
00262             connect( action, SIGNAL( toolActivated( const KDataToolInfo &, const QString & ) ),
00263                      receiver, slot );
00264             actionList.append( action );
00265         }
00266     }
00267 
00268     return actionList;
00269 }
00270 
00271 /*************************************************
00272  *
00273  * KDataTool
00274  *
00275  *************************************************/
00276 
00277 KDataTool::KDataTool( QObject* parent, const char* name )
00278     : QObject( parent, name ), m_instance( 0L )
00279 {
00280 }
00281 
00282 KInstance* KDataTool::instance() const
00283 {
00284    return m_instance;
00285 }
00286 
00287 void KDataToolAction::virtual_hook( int id, void* data )
00288 { KAction::virtual_hook( id, data ); }
00289 
00290 void KDataTool::virtual_hook( int, void* )
00291 { /*BASE::virtual_hook( id, data );*/ }
00292 
00293 #include "kdatatool.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:28 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001