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 <kservicetype.h>
00026 #include <kdebug.h>
00027 #include <kservicetypetrader.h>
00028 #include <QFile>
00029
00030 #include <Q3ValueList>
00031
00032 #include <limits.h>
00033
00041
00042
00043
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
00055
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;
00070 obj = factory->create( parent, "KoDocument" );
00071 }
00072
00073 if ( !obj || !obj->inherits( "KoDocument" ) )
00074 {
00075
00076
00077
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
00094 QString constr = QString::fromLatin1( "'%1' in ServiceTypes" ).arg( mimetype );
00095 vec = query( constr );
00096 if ( vec.isEmpty() )
00097 {
00098
00099
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
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
00141
00142
00143 if ( (*it)->noDisplay() )
00144 continue;
00145
00146 if ( (!_onlyDocEmb) || ((*it)->property("X-KDE-NOTKoDocumentEmbeddable").toString()!="1") )
00147 {
00148 KoDocumentEntry d( *it );
00149
00150 lst.append( d );
00151
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
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
00190 for( unsigned int i = 0; i < max; i++ )
00191 {
00192
00193
00194
00195 lst.append( KoFilterEntry::Ptr( new KoFilterEntry( *it ) ) );
00196
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 }