lib Library API Documentation

koQueryTrader.cc

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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <kparts/factory.h>
00021 
00022 #include <koQueryTrader.h>
00023 #include <koDocument.h>
00024 #include <koFilter.h>
00025 #include <ktrader.h>
00026 #include <kservicetype.h>
00027 #include <kdebug.h>
00028 
00029 #include <qfile.h>
00030 
00031 #include <limits.h> // UINT_MAX
00032 
00040 /*******************************************************************
00041  *
00042  * KoDocumentEntry
00043  *
00044  *******************************************************************/
00045 
00046 KoDocumentEntry::KoDocumentEntry( KService::Ptr service )
00047   : m_service( service )
00048 {
00049 }
00050 
00051 KoDocument* KoDocumentEntry::createDoc( KoDocument* parent, const char* name ) const
00052 {
00053     KLibFactory* factory = KLibLoader::self()->factory( QFile::encodeName(m_service->library()) );
00054 
00055     if( !factory ) {
00056         kdWarning(30003) << KLibLoader::self()->lastErrorMessage() << endl;
00057         return 0;
00058     }
00059 
00060     QObject* obj;
00061     if ( factory->inherits( "KParts::Factory" ) )
00062       obj = static_cast<KParts::Factory *>(factory)->createPart( 0L, "", parent, name, "KoDocument" );
00063     else {
00064       kdWarning(30003) << "factory doesn't inherit KParts::Factory ! It is a " << factory->className() << endl; // This shouldn't happen...
00065       obj = factory->create( parent, name, "KoDocument" );
00066     }
00067 
00068     if ( !obj || !obj->inherits( "KoDocument" ) )
00069     {
00070         delete obj;
00071         return 0;
00072     }
00073 
00074     return static_cast<KoDocument*>(obj);
00075 }
00076 
00077 KoDocumentEntry KoDocumentEntry::queryByMimeType( const QString & mimetype )
00078 {
00079   QString constr = QString::fromLatin1( "[X-KDE-NativeMimeType] == '%1'" ).arg( mimetype );
00080 
00081   QValueList<KoDocumentEntry> vec = query( false,constr );
00082   if ( vec.isEmpty() )
00083   {
00084     kdWarning(30003) << "Got no results with " << constr << endl;
00085     // Fallback to the old way (which was probably wrong, but better be safe)
00086     QString constr = QString::fromLatin1( "'%1' in ServiceTypes" ).arg( mimetype );
00087     vec = query( constr );
00088     if ( vec.isEmpty() )
00089     {
00090       // Still no match. Either the mimetype itself is unknown, or we have no service for it.
00091       // Help the user debugging stuff by providing some more diagnostics
00092       if ( KServiceType::serviceType( mimetype ) == 0L )
00093       {
00094         kdError(30003) << "Unknown KOffice MimeType " << mimetype << "." << endl;
00095         kdError(30003) << "Check your installation (for instance, run 'kde-config --path mime' and check the result)." << endl;
00096       } else
00097       {
00098         kdError(30003) << "Found no KOffice part able to handle " << mimetype << "!" << endl;
00099         kdError(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;
00100       }
00101       return KoDocumentEntry();
00102     }
00103   }
00104 
00105   return vec[0];
00106 }
00107 
00108 QValueList<KoDocumentEntry> KoDocumentEntry::query( const QString & _constr )
00109 {
00110   return query(true,_constr);
00111 }
00112 
00113 QValueList<KoDocumentEntry> KoDocumentEntry::query( bool _onlyDocEmb, const QString & _constr )
00114 {
00115 
00116   QValueList<KoDocumentEntry> lst;
00117   QString constr;
00118   if ( !_constr.isEmpty() ) {
00119       constr = "(";
00120       constr += _constr;
00121       constr += ") and ";
00122   }
00123   constr += " exist Library";
00124 
00125   // Query the trader
00126   KTrader::OfferList offers = KTrader::self()->query( "KOfficePart", constr );
00127 
00128   KTrader::OfferList::ConstIterator it = offers.begin();
00129   unsigned int max = offers.count();
00130   for( unsigned int i = 0; i < max; i++, ++it )
00131   {
00132     //kdDebug(30003) << "   desktopEntryPath=" << (*it)->desktopEntryPath()
00133     //               << "   library=" << (*it)->library() << endl;
00134     if ( (*it)->library().isEmpty() )
00135         continue;
00136     // Parse the service
00137     if ((!_onlyDocEmb) || ((*it)->property("X-KDE-NOTKoDocumentEmbeddable").toString()!="1"))
00138     {
00139       KoDocumentEntry d( *it );
00140       // Append converted offer
00141       lst.append( d );
00142       // Next service
00143     }
00144   }
00145 
00146   if ( lst.count() > 1 && !_constr.isEmpty() )
00147     kdWarning(30003) << "KoDocumentEntry::query " << _constr << " got " << max << " offers!" << endl;
00148   return lst;
00149 }
00150 
00151 
00152 
00153 
00154 /*******************************************************************
00155  *
00156  * KoFilterEntry
00157  *
00158  *******************************************************************/
00159 
00160 KoFilterEntry::KoFilterEntry( KService::Ptr service )
00161   : m_service( service )
00162 {
00163   import = service->property( "X-KDE-Import" ).toStringList();
00164   export_ = service->property( "X-KDE-Export" ).toStringList();
00165   int w = service->property( "X-KDE-Weight" ).toString().toInt();
00166   weight = w < 0 ? UINT_MAX : static_cast<unsigned int>( w );
00167   available = service->property( "X-KDE-Available" ).toString();
00168 }
00169 
00170 QValueList<KoFilterEntry::Ptr> KoFilterEntry::query( const QString & _constr )
00171 {
00172   kdDebug(30500) << "KoFilterEntry::query( " << _constr << " )" << endl;
00173   QValueList<KoFilterEntry::Ptr> lst;
00174 
00175   KTrader::OfferList offers = KTrader::self()->query( "KOfficeFilter", _constr );
00176 
00177   KTrader::OfferList::ConstIterator it = offers.begin();
00178   unsigned int max = offers.count();
00179   //kdDebug(30500) << "Query returned " << max << " offers" << endl;
00180   for( unsigned int i = 0; i < max; i++ )
00181   {
00182     //kdDebug(30500) << "   desktopEntryPath=" << (*it)->desktopEntryPath()
00183     //               << "   library=" << (*it)->library() << endl;
00184     // Append converted offer
00185     lst.append( new KoFilterEntry( *it ) );
00186     // Next service
00187     it++;
00188   }
00189 
00190   return lst;
00191 }
00192 
00193 KoFilter* KoFilterEntry::createFilter( KoFilterChain* chain, QObject* parent, const char* name )
00194 {
00195     KLibFactory* factory = KLibLoader::self()->factory( QFile::encodeName( m_service->library() ) );
00196 
00197     if ( !factory )
00198         return 0;
00199 
00200     QObject* obj = factory->create( parent, name, "KoFilter" );
00201     if ( !obj || !obj->inherits( "KoFilter" ) )
00202     {
00203         delete obj;
00204         return 0;
00205     }
00206 
00207     KoFilter* filter = static_cast<KoFilter*>( obj );
00208     filter->m_chain = chain;
00209     return filter;
00210 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Mar 11 11:47:42 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003