• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

KParts

componentfactory.h

Go to the documentation of this file.
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. !@%@#$!
00017     namespace ComponentFactory
00018     {
00037         enum ComponentLoadingError { ErrNoServiceFound = 1,
00038                                      ErrServiceProvidesNoLibrary,
00039                                      ErrNoLibrary,
00040                                      ErrNoFactory,
00041                                      ErrNoComponent };
00042 
00060         template <class T>
00061         static T *createInstanceFromFactory( KLibFactory *factory, QObject *parent = 0,
00062                                              const char *name = 0,
00063                                              const QStringList &args = QStringList() )
00064         {
00065             QObject *object = factory->create( parent, name,
00066                                                T::staticMetaObject()->className(),
00067                                                args );
00068 
00069             T *result = dynamic_cast<T *>( object );
00070             if ( !result )
00071                     delete object;
00072             return result;
00073         }
00074 
00094         template <class T>
00095         static T *createPartInstanceFromFactory( KParts::Factory *factory,
00096                                                  QWidget *parentWidget = 0,
00097                                                  const char *widgetName = 0,
00098                                                  QObject *parent = 0,
00099                                                  const char *name = 0,
00100                                                  const QStringList &args = QStringList() )
00101         {
00102             KParts::Part *object = factory->createPart( parentWidget, widgetName,
00103                                                         parent, name,
00104                                                         T::staticMetaObject()->className(),
00105                                                         args );
00106 
00107             T *result = dynamic_cast<T *>( object );
00108             if ( !result )
00109                 delete object;
00110             return result;
00111         }
00112 
00126         template <class T>
00127         static T *createInstanceFromLibrary( const char *libraryName, QObject *parent = 0,
00128                                              const char *name = 0,
00129                                              const QStringList &args = QStringList(),
00130                                              int *error = 0 )
00131         {
00132             KLibrary *library = KLibLoader::self()->library( libraryName );
00133             if ( !library )
00134             {
00135                 if ( error )
00136                     *error = ErrNoLibrary;
00137                 return 0;
00138             }
00139             KLibFactory *factory = library->factory();
00140             if ( !factory )
00141             {
00142                 library->unload();
00143                 if ( error )
00144                     *error = ErrNoFactory;
00145                 return 0;
00146             }
00147             T *res = createInstanceFromFactory<T>( factory, parent, name, args );
00148             if ( !res )
00149             {
00150                 library->unload();
00151                 if ( error )
00152                     *error = ErrNoComponent;
00153             }
00154             return res;
00155         }
00156 
00157         template <class T>
00158         static T *createPartInstanceFromLibrary( const char *libraryName,
00159                                                  QWidget *parentWidget = 0,
00160                                                  const char *widgetName = 0,
00161                                                  QObject *parent = 0,
00162                                                  const char *name = 0,
00163                                                  const QStringList &args = QStringList(),
00164                                                  int *error = 0 )
00165         {
00166             KLibrary *library = KLibLoader::self()->library( libraryName );
00167             if ( !library )
00168             {
00169                 if ( error )
00170                     *error = ErrNoLibrary;
00171                 return 0;
00172             }
00173             KLibFactory *factory = library->factory();
00174             if ( !factory )
00175             {
00176                 library->unload();
00177                 if ( error )
00178                     *error = ErrNoFactory;
00179                 return 0;
00180             }
00181             KParts::Factory *partFactory = dynamic_cast<KParts::Factory *>( factory );
00182             if ( !partFactory )
00183             {
00184                 library->unload();
00185                 if ( error )
00186                     *error = ErrNoFactory;
00187                 return 0;
00188             }
00189             T *res = createPartInstanceFromFactory<T>( partFactory, parentWidget,
00190                                                        widgetName, parent, name, args );
00191             if ( !res )
00192             {
00193                 library->unload();
00194                 if ( error )
00195                     *error = ErrNoComponent;
00196             }
00197             return res;
00198         }
00199 
00200         template <class T>
00201         static T *createInstanceFromService( const KService::Ptr &service,
00202                                              QObject *parent = 0,
00203                                              const char *name = 0,
00204                                              const QStringList &args = QStringList(),
00205                                              int *error = 0 )
00206         {
00207             QString library = service->library();
00208             if ( library.isEmpty() )
00209             {
00210                 if ( error )
00211                     *error = ErrServiceProvidesNoLibrary;
00212                 return 0;
00213             }
00214 
00215             return createInstanceFromLibrary<T>( library.local8Bit().data(), parent,
00216                              name, args, error );
00217         }
00218 
00219         template <class T>
00220         static T *createPartInstanceFromService( const KService::Ptr &service,
00221                                                  QWidget *parentWidget = 0,
00222                                                  const char *widgetName = 0,
00223                                                  QObject *parent = 0,
00224                                                  const char *name = 0,
00225                                                  const QStringList &args = QStringList(),
00226                                                  int *error = 0 )
00227         {
00228             QString library = service->library();
00229             if ( library.isEmpty() )
00230             {
00231                 if ( error )
00232                     *error = ErrServiceProvidesNoLibrary;
00233                 return 0;
00234             }
00235 
00236             return createPartInstanceFromLibrary<T>( library.local8Bit().data(), parentWidget,
00237                                                      widgetName, parent, name, args, error );
00238         }
00239 
00240         template <class T, class ServiceIterator>
00241         static T *createInstanceFromServices( ServiceIterator begin, ServiceIterator end,
00242                                               QObject *parent = 0,
00243                                               const char *name = 0,
00244                                               const QStringList &args = QStringList(),
00245                                               int *error = 0 )
00246         {
00247             for (; begin != end; ++begin )
00248             {
00249                 KService::Ptr service = *begin;
00250 
00251                 if ( error )
00252                     *error = 0;
00253 
00254                 T *component = createInstanceFromService<T>( service, parent, name,
00255                                                              args, error );
00256                 if ( component )
00257                     return component;
00258             }
00259 
00260             if ( error )
00261                 *error = ErrNoServiceFound;
00262 
00263             return 0;
00264 
00265         }
00266 
00267         template <class T, class ServiceIterator>
00268         static T *createPartInstanceFromServices( ServiceIterator begin,
00269                                                   ServiceIterator end,
00270                                                   QWidget *parentWidget = 0,
00271                                                   const char *widgetName = 0,
00272                                                   QObject *parent = 0,
00273                                                   const char *name = 0,
00274                                                   const QStringList &args = QStringList(),
00275                                                   int *error = 0 )
00276          {
00277             for (; begin != end; ++begin )
00278             {
00279                 KService::Ptr service = *begin;
00280 
00281                 if ( error )
00282                     *error = 0;
00283 
00284                 T *component = createPartInstanceFromService<T>( service, parentWidget,
00285                                                                  widgetName, parent,
00286                                                                  name, args, error );
00287                 if ( component )
00288                     return component;
00289             }
00290 
00291             if ( error )
00292                 *error = ErrNoServiceFound;
00293 
00294             return 0;
00295 
00296         }
00297 
00320         template <class T>
00321         static T *createInstanceFromQuery( const QString &serviceType,
00322                                            const QString &constraint = QString::null,
00323                                            QObject *parent = 0,
00324                                            const char *name = 0,
00325                                            const QStringList &args = QStringList(),
00326                                            int *error = 0 )
00327         {
00328             KTrader::OfferList offers = KTrader::self()->query( serviceType, constraint );
00329             if ( offers.isEmpty() )
00330             {
00331                 if ( error )
00332                     *error = ErrNoServiceFound;
00333                 return 0;
00334             }
00335 
00336             return createInstanceFromServices<T>( offers.begin(),
00337                                                   offers.end(),
00338                                                   parent, name, args, error );
00339         }
00340 
00369         template <class T>
00370         static T *createPartInstanceFromQuery( const QString &serviceType,
00371                                                const QString &constraint,
00372                                                QWidget *parentWidget = 0,
00373                                                const char *widgetName = 0,
00374                                                QObject *parent = 0,
00375                                                const char *name = 0,
00376                                                const QStringList &args = QStringList(),
00377                                                int *error = 0 )
00378         {
00379             KTrader::OfferList offers = KTrader::self()->query( serviceType, QString::fromLatin1("KParts/ReadOnlyPart"), constraint, QString::null );
00380             if ( offers.isEmpty() )
00381             {
00382                 if ( error )
00383                     *error = ErrNoServiceFound;
00384                 return 0;
00385             }
00386 
00387             return createPartInstanceFromServices<T>( offers.begin(), offers.end(),
00388                                                       parentWidget, widgetName,
00389                                                       parent, name, args, error );
00390         }
00391 
00392     }
00393 
00394 }
00395 
00396 /*
00397  * vim: et sw=4
00398  */
00399 
00400 #endif
00401 

KParts

Skip menu "KParts"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • dcop
  • DNSSD
  • interfaces
  • Kate
  • kconf_update
  • KDECore
  • KDED
  • kdefx
  • KDEsu
  • kdeui
  • KDocTools
  • KHTML
  • KImgIO
  • KInit
  • kio
  • kioslave
  • KJS
  • KNewStuff
  • KParts
  • KUtils
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal