koQueryTrader.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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>
00032
00040
00041
00042
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;
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
00086 QString constr = QString::fromLatin1( "'%1' in ServiceTypes" ).arg( mimetype );
00087 vec = query( constr );
00088 if ( vec.isEmpty() )
00089 {
00090
00091
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
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
00133
00134
00135
00136 if ( ( *it )->library().isEmpty() )
00137 continue;
00138
00139 if ( (!_onlyDocEmb) || ((*it)->property("X-KDE-NOTKoDocumentEmbeddable").toString()!="1") )
00140 {
00141 KoDocumentEntry d( *it );
00142
00143 lst.append( d );
00144
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
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
00183 for( unsigned int i = 0; i < max; i++ )
00184 {
00185
00186
00187
00188 lst.append( new KoFilterEntry( *it ) );
00189
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 }
This file is part of the documentation for lib Library Version 1.4.2.