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 <faure@kde.org> 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., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "kdatatool.h" 00022 00023 #include <kstandarddirs.h> 00024 #include <klibloader.h> 00025 #include <kdebug.h> 00026 #include <kinstance.h> 00027 00028 #include <ktrader.h> 00029 #include <kparts/componentfactory.h> 00030 00031 #include <qpixmap.h> 00032 #include <qfile.h> 00033 00034 /************************************************* 00035 * 00036 * KDataToolInfo 00037 * 00038 *************************************************/ 00039 00040 KDataToolInfo::KDataToolInfo() 00041 { 00042 m_service = 0; 00043 } 00044 00045 KDataToolInfo::KDataToolInfo( const KService::Ptr& service, KInstance* instance ) 00046 { 00047 m_service = service; 00048 m_instance = instance; 00049 00050 if ( !!m_service && !m_service->serviceTypes().contains( "KDataTool" ) ) 00051 { 00052 kdDebug(30003) << "The service " << m_service->name().latin1() 00053 << " does not feature the service type KDataTool" << endl; 00054 m_service = 0; 00055 } 00056 } 00057 00058 KDataToolInfo::KDataToolInfo( const KDataToolInfo& info ) 00059 { 00060 m_service = info.service(); 00061 m_instance = info.instance(); 00062 } 00063 00064 KDataToolInfo& KDataToolInfo::operator= ( const KDataToolInfo& info ) 00065 { 00066 m_service = info.service(); 00067 m_instance = info.instance(); 00068 return *this; 00069 } 00070 00071 QString KDataToolInfo::dataType() const 00072 { 00073 if ( !m_service ) 00074 return QString::null; 00075 00076 return m_service->property( "DataType" ).toString(); 00077 } 00078 00079 QStringList KDataToolInfo::mimeTypes() const 00080 { 00081 if ( !m_service ) 00082 return QStringList(); 00083 00084 return m_service->property( "DataMimeTypes" ).toStringList(); 00085 } 00086 00087 bool KDataToolInfo::isReadOnly() const 00088 { 00089 if ( !m_service ) 00090 return true; 00091 00092 return m_service->property( "ReadOnly" ).toBool(); 00093 } 00094 00095 QPixmap KDataToolInfo::icon() const 00096 { 00097 if ( !m_service ) 00098 return QPixmap(); 00099 00100 QPixmap pix; 00101 QStringList lst = KGlobal::dirs()->resourceDirs("icon"); 00102 QStringList::ConstIterator it = lst.begin(); 00103 while (!pix.load( *it + "/" + m_service->icon() ) && it != lst.end() ) 00104 it++; 00105 00106 return pix; 00107 } 00108 00109 QPixmap KDataToolInfo::miniIcon() const 00110 { 00111 if ( !m_service ) 00112 return QPixmap(); 00113 00114 QPixmap pix; 00115 QStringList lst = KGlobal::dirs()->resourceDirs("mini"); 00116 QStringList::ConstIterator it = lst.begin(); 00117 while (!pix.load( *it + "/" + m_service->icon() ) && it != lst.end() ) 00118 it++; 00119 00120 return pix; 00121 } 00122 00123 QString KDataToolInfo::iconName() const 00124 { 00125 if ( !m_service ) 00126 return QString::null; 00127 return m_service->icon(); 00128 } 00129 00130 QStringList KDataToolInfo::commands() const 00131 { 00132 if ( !m_service ) 00133 return QString::null; 00134 00135 return m_service->property( "Commands" ).toStringList(); 00136 } 00137 00138 QStringList KDataToolInfo::userCommands() const 00139 { 00140 if ( !m_service ) 00141 return QString::null; 00142 00143 return QStringList::split( ',', m_service->comment() ); 00144 } 00145 00146 KDataTool* KDataToolInfo::createTool( QObject* parent, const char* name ) const 00147 { 00148 if ( !m_service ) 00149 return 0; 00150 00151 KDataTool* tool = KParts::ComponentFactory::createInstanceFromService<KDataTool>( m_service, parent, name ); 00152 if ( tool ) 00153 tool->setInstance( m_instance ); 00154 return tool; 00155 } 00156 00157 KService::Ptr KDataToolInfo::service() const 00158 { 00159 return m_service; 00160 } 00161 00162 QValueList<KDataToolInfo> KDataToolInfo::query( const QString& datatype, const QString& mimetype, KInstance* instance ) 00163 { 00164 QValueList<KDataToolInfo> lst; 00165 00166 QString constr; 00167 00168 if ( !datatype.isEmpty() ) 00169 { 00170 constr = QString::fromLatin1( "DataType == '%1'" ).arg( datatype ); 00171 } 00172 if ( !mimetype.isEmpty() ) 00173 { 00174 QString tmp = QString::fromLatin1( "'%1' in DataMimeTypes" ).arg( mimetype ); 00175 if ( constr.isEmpty() ) 00176 constr = tmp; 00177 else 00178 constr = constr + " and " + tmp; 00179 } 00180 /* Bug in KTrader ? Test with HEAD-kdelibs! 00181 if ( instance ) 00182 { 00183 QString tmp = QString::fromLatin1( "not ('%1' in ExcludeFrom)" ).arg( instance->instanceName() ); 00184 if ( constr.isEmpty() ) 00185 constr = tmp; 00186 else 00187 constr = constr + " and " + tmp; 00188 } */ 00189 00190 // Query the trader 00191 //kdDebug() << "KDataToolInfo::query " << constr << endl; 00192 KTrader::OfferList offers = KTrader::self()->query( "KDataTool", constr ); 00193 00194 KTrader::OfferList::ConstIterator it = offers.begin(); 00195 for( ; it != offers.end(); ++it ) 00196 { 00197 // Temporary replacement for the non-working trader query above 00198 if ( !instance || !(*it)->property("ExcludeFrom").toStringList() 00199 .contains( instance->instanceName() ) ) 00200 lst.append( KDataToolInfo( *it, instance ) ); 00201 else 00202 kdDebug() << (*it)->entryPath() << " excluded." << endl; 00203 } 00204 00205 return lst; 00206 } 00207 00208 bool KDataToolInfo::isValid() const 00209 { 00210 return( m_service ); 00211 } 00212 00213 /************************************************* 00214 * 00215 * KDataToolAction 00216 * 00217 *************************************************/ 00218 KDataToolAction::KDataToolAction( const QString & text, const KDataToolInfo & info, const QString & command, 00219 QObject * parent, const char * name ) 00220 : KAction( text, info.iconName(), 0, parent, name ), 00221 m_command( command ), 00222 m_info( info ) 00223 { 00224 } 00225 00226 void KDataToolAction::slotActivated() 00227 { 00228 emit toolActivated( m_info, m_command ); 00229 } 00230 00231 QPtrList<KAction> KDataToolAction::dataToolActionList( const QValueList<KDataToolInfo> & tools, const QObject *receiver, const char* slot ) 00232 { 00233 QPtrList<KAction> actionList; 00234 if ( tools.isEmpty() ) 00235 return actionList; 00236 00237 actionList.append( new KActionSeparator() ); 00238 QValueList<KDataToolInfo>::ConstIterator entry = tools.begin(); 00239 for( ; entry != tools.end(); ++entry ) 00240 { 00241 QStringList userCommands = (*entry).userCommands(); 00242 QStringList commands = (*entry).commands(); 00243 Q_ASSERT(!commands.isEmpty()); 00244 if ( commands.count() != userCommands.count() ) 00245 kdWarning() << "KDataTool desktop file error (" << (*entry).service()->entryPath() 00246 << "). " << commands.count() << " commands and " 00247 << userCommands.count() << " descriptions." << endl; 00248 QStringList::ConstIterator uit = userCommands.begin(); 00249 QStringList::ConstIterator cit = commands.begin(); 00250 for (; uit != userCommands.end() && cit != commands.end(); ++uit, ++cit ) 00251 { 00252 //kdDebug() << "creating action " << *uit << " " << *cit << endl; 00253 KDataToolAction * action = new KDataToolAction( *uit, *entry, *cit ); 00254 connect( action, SIGNAL( toolActivated( const KDataToolInfo &, const QString & ) ), 00255 receiver, slot ); 00256 actionList.append( action ); 00257 } 00258 } 00259 00260 return actionList; 00261 } 00262 00263 /************************************************* 00264 * 00265 * KDataTool 00266 * 00267 *************************************************/ 00268 00269 KDataTool::KDataTool( QObject* parent, const char* name ) 00270 : QObject( parent, name ), m_instance( 0L ) 00271 { 00272 } 00273 00274 KInstance* KDataTool::instance() const 00275 { 00276 return m_instance; 00277 } 00278 00279 void KDataToolAction::virtual_hook( int id, void* data ) 00280 { KAction::virtual_hook( id, data ); } 00281 00282 void KDataTool::virtual_hook( int, void* ) 00283 { /*BASE::virtual_hook( id, data );*/ } 00284 00285 #include "kdatatool.moc"