kparts Library API Documentation

componentfactory.h

00001 #ifndef __kparts_componentfactory_h__
00002 #define __kparts_componentfactory_h__
00003 
00004 #include <kparts/factory.h>
00005 #include <kparts/part.h>
00006 #include <ktrader.h>
00007 #include <qmetaobject.h>
00008 
00009 namespace KParts
00010 {
00011 
00012     // this is a namespace and not a class because stupid egcs 1.1.2 doesn't grok
00013     // static template methods in classes. !@%@#$!
00014     namespace ComponentFactory
00015     {
00034         enum ComponentLoadingError { ErrNoServiceFound = 1,
00035                                      ErrServiceProvidesNoLibrary,
00036                                      ErrNoLibrary,
00037                                      ErrNoFactory,
00038                                      ErrNoComponent };
00039 
00057         template <class T>
00058         static T *createInstanceFromFactory( KLibFactory *factory, QObject *parent = 0,
00059                                              const char *name = 0,
00060                                              const QStringList &args = QStringList() )
00061         {
00062             QObject *object = factory->create( parent, name,
00063                                                T::staticMetaObject()->className(),
00064                                                args );
00065 
00066             T *result = dynamic_cast<T *>( object );
00067             if ( !result )
00068                     delete object;
00069             return result;
00070         }
00071 
00091         template <class T>
00092         static T *createPartInstanceFromFactory( KParts::Factory *factory,
00093                                                  QWidget *parentWidget = 0,
00094                                                  const char *widgetName = 0,
00095                                                  QObject *parent = 0,
00096                                                  const char *name = 0,
00097                                                  const QStringList &args = QStringList() )
00098         {
00099             KParts::Part *object = factory->createPart( parentWidget, widgetName,
00100                                                         parent, name,
00101                                                         T::staticMetaObject()->className(),
00102                                                         args );
00103 
00104             T *result = dynamic_cast<T *>( object );
00105             if ( !result )
00106                 delete object;
00107             return result;
00108         }
00109 
00123         template <class T>
00124         static T *createInstanceFromLibrary( const char *libraryName, QObject *parent = 0,
00125                                              const char *name = 0,
00126                                              const QStringList &args = QStringList(),
00127                                              int *error = 0 )
00128         {
00129             KLibrary *library = KLibLoader::self()->library( libraryName );
00130             if ( !library )
00131             {
00132                 if ( error )
00133                     *error = ErrNoLibrary;
00134                 return 0;
00135             }
00136             KLibFactory *factory = library->factory();
00137             if ( !factory )
00138             {
00139                 library->unload();
00140                 if ( error )
00141                     *error = ErrNoFactory;
00142                 return 0;
00143             }
00144             T *res = createInstanceFromFactory<T>( factory, parent, name, args );
00145             if ( !res )
00146             {
00147                 library->unload();
00148                 if ( error )
00149                     *error = ErrNoComponent;
00150             }
00151             return res;
00152         }
00153 
00154         template <class T>
00155         static T *createPartInstanceFromLibrary( const char *libraryName,
00156                                                  QWidget *parentWidget = 0,
00157                                                  const char *widgetName = 0,
00158                                                  QObject *parent = 0,
00159                                                  const char *name = 0,
00160                                                  const QStringList &args = QStringList(),
00161                                                  int *error = 0 )
00162         {
00163             KLibrary *library = KLibLoader::self()->library( libraryName );
00164             if ( !library )
00165             {
00166                 if ( error )
00167                     *error = ErrNoLibrary;
00168                 return 0;
00169             }
00170             KLibFactory *factory = library->factory();
00171             if ( !factory )
00172             {
00173                 library->unload();
00174                 if ( error )
00175                     *error = ErrNoFactory;
00176                 return 0;
00177             }
00178             KParts::Factory *partFactory = dynamic_cast<KParts::Factory *>( factory );
00179             if ( !partFactory )
00180             {
00181                 library->unload();
00182                 if ( error )
00183                     *error = ErrNoFactory;
00184                 return 0;
00185             }
00186             T *res = createPartInstanceFromFactory<T>( partFactory, parentWidget,
00187                                                        widgetName, parent, name, args );
00188             if ( !res )
00189             {
00190                 library->unload();
00191                 if ( error )
00192                     *error = ErrNoComponent;
00193             }
00194             return res;
00195         }
00196 
00197         template <class T>
00198         static T *createInstanceFromService( const KService::Ptr &service,
00199                                              QObject *parent = 0,
00200                                              const char *name = 0,
00201                                              const QStringList &args = QStringList(),
00202                                              int *error = 0 )
00203         {
00204             QString library = service->library();
00205             if ( library.isEmpty() )
00206             {
00207                 if ( error )
00208                     *error = ErrServiceProvidesNoLibrary;
00209                 return 0;
00210             }
00211 
00212             return createInstanceFromLibrary<T>( library.local8Bit().data(), parent,
00213                              name, args, error );
00214         }
00215 
00216         template <class T>
00217         static T *createPartInstanceFromService( const KService::Ptr &service,
00218                                                  QWidget *parentWidget = 0,
00219                                                  const char *widgetName = 0,
00220                                                  QObject *parent = 0,
00221                                                  const char *name = 0,
00222                                                  const QStringList &args = QStringList(),
00223                                                  int *error = 0 )
00224         {
00225             QString library = service->library();
00226             if ( library.isEmpty() )
00227             {
00228                 if ( error )
00229                     *error = ErrServiceProvidesNoLibrary;
00230                 return 0;
00231             }
00232 
00233             return createPartInstanceFromLibrary<T>( library.local8Bit().data(), parentWidget,
00234                                                      widgetName, parent, name, args, error );
00235         }
00236 
00237         template <class T, class ServiceIterator>
00238         static T *createInstanceFromServices( ServiceIterator begin, ServiceIterator end,
00239                                               QObject *parent = 0,
00240                                               const char *name = 0,
00241                                               const QStringList &args = QStringList(),
00242                                               int *error = 0 )
00243         {
00244             for (; begin != end; ++begin )
00245             {
00246                 KService::Ptr service = *begin;
00247 
00248                 if ( error )
00249                     *error = 0;
00250 
00251                 T *component = createInstanceFromService<T>( service, parent, name,
00252                                                              args, error );
00253                 if ( component )
00254                     return component;
00255             }
00256 
00257             if ( error )
00258                 *error = ErrNoServiceFound;
00259 
00260             return 0;
00261 
00262         }
00263 
00264         template <class T, class ServiceIterator>
00265         static T *createPartInstanceFromServices( ServiceIterator begin,
00266                                                   ServiceIterator end,
00267                                                   QWidget *parentWidget = 0,
00268                                                   const char *widgetName = 0,
00269                                                   QObject *parent = 0,
00270                                                   const char *name = 0,
00271                                                   const QStringList &args = QStringList(),
00272                                                   int *error = 0 )
00273          {
00274             for (; begin != end; ++begin )
00275             {
00276                 KService::Ptr service = *begin;
00277 
00278                 if ( error )
00279                     *error = 0;
00280 
00281                 T *component = createPartInstanceFromService<T>( service, parentWidget,
00282                                                                  widgetName, parent,
00283                                                                  name, args, error );
00284                 if ( component )
00285                     return component;
00286             }
00287 
00288             if ( error )
00289                 *error = ErrNoServiceFound;
00290 
00291             return 0;
00292 
00293         }
00294 
00317         template <class T>
00318         static T *createInstanceFromQuery( const QString &serviceType,
00319                                            const QString &constraint = QString::null,
00320                                            QObject *parent = 0,
00321                                            const char *name = 0,
00322                                            const QStringList &args = QStringList(),
00323                                            int *error = 0 )
00324         {
00325             KTrader::OfferList offers = KTrader::self()->query( serviceType, constraint );
00326             if ( offers.isEmpty() )
00327             {
00328                 if ( error )
00329                     *error = ErrNoServiceFound;
00330                 return 0;
00331             }
00332 
00333             return createInstanceFromServices<T>( offers.begin(),
00334                                                   offers.end(),
00335                                                   parent, name, args, error );
00336         }
00337 
00366         template <class T>
00367         static T *createPartInstanceFromQuery( const QString &serviceType,
00368                                                const QString &constraint,
00369                                                QWidget *parentWidget = 0,
00370                                                const char *widgetName = 0,
00371                                                QObject *parent = 0,
00372                                                const char *name = 0,
00373                                                const QStringList &args = QStringList(),
00374                                                int *error = 0 )
00375         {
00376             KTrader::OfferList offers = KTrader::self()->query( serviceType, QString::fromLatin1("KParts/ReadOnlyPart"), constraint, QString::null );
00377             if ( offers.isEmpty() )
00378             {
00379                 if ( error )
00380                     *error = ErrNoServiceFound;
00381                 return 0;
00382             }
00383 
00384             return createPartInstanceFromServices<T>( offers.begin(), offers.end(),
00385                                                       parentWidget, widgetName,
00386                                                       parent, name, args, error );
00387         }
00388 
00389     }
00390 
00391 }
00392 
00393 /*
00394  * vim: et sw=4
00395  */
00396 
00397 #endif
00398 
KDE Logo
This file is part of the documentation for kparts Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 7 22:10:07 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003