F:/KPlato/koffice/libs/kofficecore/KoQueryTrader.cpp

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <kparts/factory.h>
00021 
00022 #include <KoQueryTrader.h>
00023 #include <KoDocument.h>
00024 #include <KoFilter.h>
00025 #include <kservicetype.h>
00026 #include <kdebug.h>
00027 #include <kservicetypetrader.h>
00028 #include <QFile>
00029 //Added by qt3to4:
00030 #include <Q3ValueList>
00031 
00032 #include <limits.h> // UINT_MAX
00033 
00041 /*******************************************************************
00042  *
00043  * KoDocumentEntry
00044  *
00045  *******************************************************************/
00046 
00047 KoDocumentEntry::KoDocumentEntry( KService::Ptr service )
00048   : m_service( service )
00049 {
00050 }
00051 
00052 KoDocument* KoDocumentEntry::createDoc( QString* errorMsg, KoDocument* parent ) const
00053 {
00054     // TODO use KParts::ComponentFactory::createInstanceFromService( m_service )
00055     // to get better error handling.
00056     KLibFactory* factory = KLibLoader::self()->factory( QFile::encodeName(m_service->library()) );
00057 
00058     if( !factory ) {
00059         if ( errorMsg )
00060             *errorMsg = KLibLoader::self()->lastErrorMessage();
00061         kWarning(30003) << KLibLoader::self()->lastErrorMessage() << endl;
00062         return 0;
00063     }
00064 
00065     QObject* obj;
00066     if ( factory->inherits( "KParts::Factory" ) )
00067       obj = static_cast<KParts::Factory *>(factory)->createPart( 0L, parent, "KoDocument" );
00068     else {
00069       kWarning(30003) << "factory doesn't inherit KParts::Factory ! It is a " << factory->metaObject()->className() << endl; // This shouldn't happen...
00070       obj = factory->create( parent, "KoDocument" );
00071     }
00072 
00073     if ( !obj || !obj->inherits( "KoDocument" ) )
00074     {
00075         // TODO
00076         //if ( errorMsg )
00077         //    *errorMsg = i18n( "Document could not be created" );
00078         delete obj;
00079         return 0;
00080     }
00081 
00082     return static_cast<KoDocument*>(obj);
00083 }
00084 
00085 KoDocumentEntry KoDocumentEntry::queryByMimeType( const QString & mimetype )
00086 {
00087   QString constr = QString::fromLatin1( "[X-KDE-NativeMimeType] == '%1' or '%2' in [X-KDE-ExtraNativeMimeTypes]" ).arg( mimetype ).arg( mimetype );
00088 
00089   Q3ValueList<KoDocumentEntry> vec = query( false,constr );
00090   if ( vec.isEmpty() )
00091   {
00092     kWarning(30003) << "Got no results with " << constr << endl;
00093     // Fallback to the old way (which was probably wrong, but better be safe)
00094     QString constr = QString::fromLatin1( "'%1' in ServiceTypes" ).arg( mimetype );
00095     vec = query( constr );
00096     if ( vec.isEmpty() )
00097     {
00098       // Still no match. Either the mimetype itself is unknown, or we have no service for it.
00099       // Help the user debugging stuff by providing some more diagnostics
00100       if ( KServiceType::serviceType( mimetype ).isNull() )
00101       {
00102         kError(30003) << "Unknown KOffice MimeType " << mimetype << "." << endl;
00103         kError(30003) << "Check your installation (for instance, run 'kde4-config --path mime' and check the result)." << endl;
00104       } else
00105       {
00106         kError(30003) << "Found no KOffice part able to handle " << mimetype << "!" << endl;
00107         kError(30003) << "Check your installation (does the desktop file have X-KDE-NativeMimeType and KOfficePart, did you install KOffice in a different prefix than KDE, without adding the prefix to /etc/kderc ?)" << endl;
00108       }
00109       return KoDocumentEntry();
00110     }
00111   }
00112 
00113   return KoDocumentEntry( vec[0] );
00114 }
00115 
00116 Q3ValueList<KoDocumentEntry> KoDocumentEntry::query( const QString & _constr )
00117 {
00118   return query(true,_constr);
00119 }
00120 
00121 Q3ValueList<KoDocumentEntry> KoDocumentEntry::query( bool _onlyDocEmb, const QString & _constr )
00122 {
00123 
00124   Q3ValueList<KoDocumentEntry> lst;
00125   QString constr;
00126   if ( !_constr.isEmpty() ) {
00127       constr = "(";
00128       constr += _constr;
00129       constr += ") and ";
00130   }
00131   constr += " exist Library";
00132 
00133   // Query the trader
00134   KService::List offers = KServiceTypeTrader::self()->query( "KOfficePart", constr );
00135 
00136   KService::List::ConstIterator it = offers.begin();
00137   unsigned int max = offers.count();
00138   for( unsigned int i = 0; i < max; i++, ++it )
00139   {
00140     //kDebug(30003) << "   desktopEntryPath=" << (*it)->desktopEntryPath()
00141     //               << "   library=" << (*it)->library() << endl;
00142 
00143     if ( (*it)->noDisplay() )
00144         continue;
00145     // Maybe this could be done as a trader constraint too.
00146     if ( (!_onlyDocEmb) || ((*it)->property("X-KDE-NOTKoDocumentEmbeddable").toString()!="1") )
00147     {
00148       KoDocumentEntry d( *it );
00149       // Append converted offer
00150       lst.append( d );
00151       // Next service
00152     }
00153   }
00154 
00155   if ( lst.count() > 1 && !_constr.isEmpty() )
00156     kWarning(30003) << "KoDocumentEntry::query " << constr << " got " << max << " offers!" << endl;
00157 
00158   return lst;
00159 }
00160 
00161 
00162 
00163 
00164 /*******************************************************************
00165  *
00166  * KoFilterEntry
00167  *
00168  *******************************************************************/
00169 
00170 KoFilterEntry::KoFilterEntry( KService::Ptr service )
00171   : m_service( service )
00172 {
00173   import = service->property( "X-KDE-Import" ).toStringList();
00174   export_ = service->property( "X-KDE-Export" ).toStringList();
00175   int w = service->property( "X-KDE-Weight" ).toInt();
00176   weight = w < 0 ? UINT_MAX : static_cast<unsigned int>( w );
00177   available = service->property( "X-KDE-Available" ).toString();
00178 }
00179 
00180 Q3ValueList<KoFilterEntry::Ptr> KoFilterEntry::query( const QString & _constr )
00181 {
00182   kDebug(30500) << "KoFilterEntry::query( " << _constr << " )" << endl;
00183   Q3ValueList<KoFilterEntry::Ptr> lst;
00184 
00185   KService::List offers = KServiceTypeTrader::self()->query( "KOfficeFilter", _constr );
00186 
00187   KService::List::ConstIterator it = offers.begin();
00188   unsigned int max = offers.count();
00189   //kDebug(30500) << "Query returned " << max << " offers" << endl;
00190   for( unsigned int i = 0; i < max; i++ )
00191   {
00192     //kDebug(30500) << "   desktopEntryPath=" << (*it)->desktopEntryPath()
00193     //               << "   library=" << (*it)->library() << endl;
00194     // Append converted offer
00195     lst.append( KoFilterEntry::Ptr( new KoFilterEntry( *it ) ) );
00196     // Next service
00197     it++;
00198   }
00199 
00200   return lst;
00201 }
00202 
00203 KoFilter* KoFilterEntry::createFilter( KoFilterChain* chain, QObject* parent )
00204 {
00205     KLibFactory* factory = KLibLoader::self()->factory( QFile::encodeName( m_service->library() ) );
00206 
00207     if ( !factory ) {
00208         kWarning(30003) << KLibLoader::self()->lastErrorMessage() << endl;
00209         return 0;
00210     }
00211 
00212     QObject* obj = factory->create( parent, "KoFilter" );
00213     if ( !obj || !obj->inherits( "KoFilter" ) )
00214     {
00215         delete obj;
00216         return 0;
00217     }
00218 
00219     KoFilter* filter = static_cast<KoFilter*>( obj );
00220     filter->m_chain = chain;
00221     return filter;
00222 }

Généré le Wed Nov 22 23:41:04 2006 pour KPlato par  doxygen 1.5.1-p1