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 
00295         template <class T>
00296         static T *createInstanceFromQuery( const QString &serviceType,
00297                                            const QString &constraint = QString::null,
00298                                            QObject *parent = 0,
00299                                            const char *name = 0,
00300                                            const QStringList &args = QStringList(),
00301                                            int *error = 0 )
00302         {
00303             KTrader::OfferList offers = KTrader::self()->query( serviceType, constraint );
00304             if ( offers.isEmpty() )
00305             {
00306                 if ( error )
00307                     *error = ErrNoServiceFound;
00308                 return 0;
00309             }
00310 
00311             return createInstanceFromServices<T>( offers.begin(),
00312                                                   offers.end(),
00313                                                   parent, name, args, error );
00314         }
00315 
00344         template <class T>
00345         static T *createPartInstanceFromQuery( const QString &serviceType,
00346                                                const QString &constraint,
00347                                                QWidget *parentWidget = 0,
00348                                                const char *widgetName = 0,
00349                                                QObject *parent = 0,
00350                                                const char *name = 0,
00351                                                const QStringList &args = QStringList(),
00352                                                int *error = 0 )
00353         {
00354             KTrader::OfferList offers = KTrader::self()->query( serviceType, QString::fromLatin1( "KParts/ReadOnlyPart" ), constraint, QString::null );
00355             if ( offers.isEmpty() )
00356             {
00357                 if ( error )
00358                     *error = ErrNoServiceFound;
00359                 return 0;
00360             }
00361 
00362             return createPartInstanceFromServices<T>( offers.begin(), offers.end(),
00363                                                       parentWidget, widgetName,
00364                                                       parent, name, args, error );
00365         }
00366  
00367     }
00368 
00369 }
00370 
00371 /*
00372  * vim: et sw=4
00373  */
00374 
00375 #endif
00376 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:15:44 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001