kparts Library API Documentation

componentfactory.h

00001 #ifndef __kparts_componentfactory_h__
00002 #define __kparts_componentfactory_h__
00003 
00004 #include <kparts/factory.h>
00005 #include <ktrader.h>
00006 #include <qmetaobject.h>
00007 
00008 namespace KParts
00009 {
00010 
00011     // this is a namespace and not a class because stupid egcs 1.1.2 doesn't grok
00012     // static template methods in classes. !@%@#$!
00013     namespace ComponentFactory
00014     {
00033         enum ComponentLoadingError { ErrNoServiceFound = 1,
00034                                      ErrServiceProvidesNoLibrary,
00035                                      ErrNoLibrary,
00036                                      ErrNoFactory,
00037                                      ErrNoComponent };
00038 
00056         template <class T>
00057         static T *createInstanceFromFactory( KLibFactory *factory, QObject *parent = 0,
00058                                              const char *name = 0,
00059                                              const QStringList &args = QStringList() )
00060         {
00061             QObject *object = factory->create( parent, name, 
00062                                                T::staticMetaObject()->className(),
00063                                                args );
00064 
00065             T *result = dynamic_cast<T *>( object );
00066             if ( !result )
00067                     delete object;
00068             return result;
00069         }
00070 
00090         template <class T>
00091         static T *createPartInstanceFromFactory( KParts::Factory *factory, 
00092                                                  QWidget *parentWidget = 0,
00093                                                  const char *widgetName = 0, 
00094                                                  QObject *parent = 0,
00095                                                  const char *name = 0,
00096                                                  const QStringList &args = QStringList() )
00097         {
00098             KParts::Part *object = factory->createPart( parentWidget, widgetName,
00099                                                         parent, name, 
00100                                                         T::staticMetaObject()->className(),
00101                                                         args );
00102 
00103             T *result = dynamic_cast<T *>( object );
00104             if ( !result )
00105                 delete object;
00106             return result;
00107         }
00108 
00122         template <class T>
00123         static T *createInstanceFromLibrary( const char *libraryName, QObject *parent = 0, 
00124                                              const char *name = 0, 
00125                                              const QStringList &args = QStringList(),
00126                                              int *error = 0 )
00127         {
00128             KLibrary *library = KLibLoader::self()->library( libraryName );
00129             if ( !library )
00130             {
00131                 if ( error )
00132                     *error = ErrNoLibrary;
00133                 return 0;
00134             }
00135             KLibFactory *factory = library->factory();
00136             if ( !factory )
00137             {
00138                 library->unload();
00139                 if ( error )
00140                     *error = ErrNoFactory;
00141                 return 0;
00142             }
00143             T *res = createInstanceFromFactory<T>( factory, parent, name, args );
00144             if ( !res )
00145             {
00146                 library->unload();
00147                 if ( error )
00148                     *error = ErrNoComponent;
00149             }
00150             return res;
00151         }
00152 
00153         template <class T>
00154         static T *createPartInstanceFromLibrary( const char *libraryName, 
00155                                                  QWidget *parentWidget = 0, 
00156                                                  const char *widgetName = 0,
00157                                                  QObject *parent = 0, 
00158                                                  const char *name = 0, 
00159                                                  const QStringList &args = QStringList(),
00160                                                  int *error = 0 )
00161         {
00162             KLibrary *library = KLibLoader::self()->library( libraryName );
00163             if ( !library )
00164             {
00165                 if ( error )
00166                     *error = ErrNoLibrary;
00167                 return 0;
00168             }
00169             KLibFactory *factory = library->factory();
00170             if ( !factory )
00171             {
00172                 library->unload();
00173                 if ( error )
00174                     *error = ErrNoFactory;
00175                 return 0;
00176             }
00177             KParts::Factory *partFactory = dynamic_cast<KParts::Factory *>( factory );
00178             if ( !partFactory )
00179             {
00180                 library->unload();
00181                 if ( error )
00182                     *error = ErrNoFactory;
00183                 return 0;
00184             }
00185             T *res = createPartInstanceFromFactory<T>( partFactory, parentWidget, 
00186                                                        widgetName, parent, name, args );
00187             if ( !res )
00188             {
00189                 library->unload();
00190                 if ( error )
00191                     *error = ErrNoComponent;
00192             }
00193             return res;
00194         }
00195 
00196         template <class T>
00197         static T *createInstanceFromService( const KService::Ptr &service,
00198                                              QObject *parent = 0,
00199                                              const char *name = 0,
00200                                              const QStringList &args = QStringList(),
00201                                              int *error = 0 )
00202         {
00203             QString library = service->library();
00204             if ( library.isEmpty() )
00205             {
00206                 if ( error )
00207                     *error = ErrServiceProvidesNoLibrary;
00208                 return 0;
00209             }
00210 
00211             return createInstanceFromLibrary<T>( library.local8Bit().data(), parent, 
00212                              name, args, error );
00213         }
00214 
00215         template <class T>
00216         static T *createPartInstanceFromService( const KService::Ptr &service,
00217                                                  QWidget *parentWidget = 0,
00218                                                  const char *widgetName = 0,
00219                                                  QObject *parent = 0,
00220                                                  const char *name = 0,
00221                                                  const QStringList &args = QStringList(),
00222                                                  int *error = 0 )
00223         {
00224             QString library = service->library();
00225             if ( library.isEmpty() )
00226             {
00227                 if ( error )
00228                     *error = ErrServiceProvidesNoLibrary;
00229                 return 0;
00230             }
00231 
00232             return createPartInstanceFromLibrary<T>( library.local8Bit().data(), parentWidget,
00233                                                      widgetName, parent, name, args, error );
00234         }
00235 
00236         template <class T, class ServiceIterator>
00237         static T *createInstanceFromServices( ServiceIterator begin, ServiceIterator end,
00238                                               QObject *parent = 0,
00239                                               const char *name = 0,
00240                                               const QStringList &args = QStringList(),
00241                                               int *error = 0 )
00242         {
00243             for (; begin != end; ++begin )
00244             {
00245                 KService::Ptr service = *begin;
00246 
00247                 if ( error )
00248                     *error = 0;
00249 
00250                 T *component = createInstanceFromService<T>( service, parent, name,
00251                                                              args, error );
00252                 if ( component )
00253                     return component;
00254             }
00255 
00256             if ( error )
00257                 *error = ErrNoServiceFound;
00258 
00259             return 0;
00260  
00261         }
00262 
00263         template <class T, class ServiceIterator>
00264         static T *createPartInstanceFromServices( ServiceIterator begin, 
00265                                                   ServiceIterator end,
00266                                                   QWidget *parentWidget = 0,
00267                                                   const char *widgetName = 0,
00268                                                   QObject *parent = 0,
00269                                                   const char *name = 0,
00270                                                   const QStringList &args = QStringList(),
00271                                                   int *error = 0 )
00272          {
00273             for (; begin != end; ++begin )
00274             {
00275                 KService::Ptr service = *begin;
00276 
00277                 if ( error )
00278                     *error = 0;
00279 
00280                 T *component = createPartInstanceFromService<T>( service, parentWidget,
00281                                                                  widgetName, parent, 
00282                                                                  name, args, error );
00283                 if ( component )
00284                     return component;
00285             }
00286 
00287             if ( error )
00288                 *error = ErrNoServiceFound;
00289 
00290             return 0;
00291  
00292         }
00293 
00294         template <class T>
00295         static T *createInstanceFromQuery( const QString &serviceType,
00296                                            const QString &constraint = QString::null,
00297                                            QObject *parent = 0,
00298                                            const char *name = 0,
00299                                            const QStringList &args = QStringList(),
00300                                            int *error = 0 )
00301         {
00302             KTrader::OfferList offers = KTrader::self()->query( serviceType, constraint );
00303             if ( offers.isEmpty() )
00304             {
00305                 if ( error )
00306                     *error = ErrNoServiceFound;
00307                 return 0;
00308             }
00309 
00310             return createInstanceFromServices<T>( offers.begin(),
00311                                                   offers.end(),
00312                                                   parent, name, args, error );
00313         }
00314 
00343         template <class T>
00344         static T *createPartInstanceFromQuery( const QString &serviceType,
00345                                                const QString &constraint,
00346                                                QWidget *parentWidget = 0,
00347                                                const char *widgetName = 0,
00348                                                QObject *parent = 0,
00349                                                const char *name = 0,
00350                                                const QStringList &args = QStringList(),
00351                                                int *error = 0 )
00352         {
00353             KTrader::OfferList offers = KTrader::self()->query( serviceType, "KParts/ReadOnlyPart", constraint, QString::null );
00354             if ( offers.isEmpty() )
00355             {
00356                 if ( error )
00357                     *error = ErrNoServiceFound;
00358                 return 0;
00359             }
00360 
00361             return createPartInstanceFromServices<T>( offers.begin(), offers.end(),
00362                                                       parentWidget, widgetName,
00363                                                       parent, name, args, error );
00364         }
00365  
00366     }
00367 
00368 };
00369 
00370 /*
00371  * vim: et sw=4
00372  */
00373 
00374 #endif
00375 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:45 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001