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' or '%2' in [X-KDE-ExtraNativeMimeTypes]" ).arg( mimetype ).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 
00135     // Workaround for "exist" bug in kdelibs < 3.2 (fixed in kservice.cpp:1.153)
00136     if ( ( *it )->library().isEmpty() )
00137         continue;
00138     // Maybe this could be done as a trader constraint too.
00139     if ( (!_onlyDocEmb) || ((*it)->property("X-KDE-NOTKoDocumentEmbeddable").toString()!="1") )
00140     {
00141       KoDocumentEntry d( *it );
00142       // Append converted offer
00143       lst.append( d );
00144       // Next service
00145     }
00146   }
00147 
00148   if ( lst.count() > 1 && !_constr.isEmpty() )
00149     kdWarning(30003) << "KoDocumentEntry::query " << constr << " got " << max << " offers!" << endl;
00150 
00151   return lst;
00152 }
00153 
00154 
00155 
00156 
00157 /*******************************************************************
00158  *
00159  * KoFilterEntry
00160  *
00161  *******************************************************************/
00162 
00163 KoFilterEntry::KoFilterEntry( KService::Ptr service )
00164   : m_service( service )
00165 {
00166   import = service->property( "X-KDE-Import" ).toStringList();
00167   export_ = service->property( "X-KDE-Export" ).toStringList();
00168   int w = service->property( "X-KDE-Weight" ).toInt();
00169   weight = w < 0 ? UINT_MAX : static_cast<unsigned int>( w );
00170   available = service->property( "X-KDE-Available" ).toString();
00171 }
00172 
00173 QValueList<KoFilterEntry::Ptr> KoFilterEntry::query( const QString & _constr )
00174 {
00175   kdDebug(30500) << "KoFilterEntry::query( " << _constr << " )" << endl;
00176   QValueList<KoFilterEntry::Ptr> lst;
00177 
00178   KTrader::OfferList offers = KTrader::self()->query( "KOfficeFilter", _constr );
00179 
00180   KTrader::OfferList::ConstIterator it = offers.begin();
00181   unsigned int max = offers.count();
00182   //kdDebug(30500) << "Query returned " << max << " offers" << endl;
00183   for( unsigned int i = 0; i < max; i++ )
00184   {
00185     //kdDebug(30500) << "   desktopEntryPath=" << (*it)->desktopEntryPath()
00186     //               << "   library=" << (*it)->library() << endl;
00187     // Append converted offer
00188     lst.append( new KoFilterEntry( *it ) );
00189     // Next service
00190     it++;
00191   }
00192 
00193   return lst;
00194 }
00195 
00196 KoFilter* KoFilterEntry::createFilter( KoFilterChain* chain, QObject* parent, const char* name )
00197 {
00198     KLibFactory* factory = KLibLoader::self()->factory( QFile::encodeName( m_service->library() ) );
00199 
00200     if ( !factory ) {
00201         kdWarning(30003) << KLibLoader::self()->lastErrorMessage() << endl;
00202         return 0;
00203     }
00204 
00205     QObject* obj = factory->create( parent, name, "KoFilter" );
00206     if ( !obj || !obj->inherits( "KoFilter" ) )
00207     {
00208         delete obj;
00209         return 0;
00210     }
00211 
00212     KoFilter* filter = static_cast<KoFilter*>( obj );
00213     filter->m_chain = chain;
00214     return filter;
00215 }
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:04 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003