libopie API Documentation

oapplicationfactory.h

00001 /*
00002  This work is derived from:
00003  ----
00004  The Loki Library
00005  Copyright (c) 2001 by Andrei Alexandrescu
00006  This code accompanies the book:
00007  Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
00008      Patterns Applied". Copyright (c) 2001. Addison-Wesley.
00009  Permission to use, copy, modify, distribute and sell this software for any
00010      purpose is hereby granted without fee, provided that the above copyright
00011      notice appear in all copies and that both that copyright notice and this
00012      permission notice appear in supporting documentation.
00013  The author or Addison-Welsey Longman make no representations about the
00014      suitability of this software for any purpose. It is provided "as is"
00015      without express or implied warranty.
00016  ----
00017 
00018   And KGenericFactor et all from Simon Hausmann <tronical@kde.org>
00019 
00020 */
00021 
00022 #include <qstring.h>
00023 #include <qmetaobject.h>
00024 
00025 #include <qtopia/qcom.h>
00026 #include <qtopia/applicationinterface.h>
00027 
00028 namespace Opie {
00029     struct NullType;
00030 
00031     template <class T, class U>
00032     struct Typelist
00033     {
00034        typedef T Head;
00035        typedef U Tail;
00036     };
00037     template<
00038         typename T1  = NullType, typename T2  = NullType, typename T3  = NullType,
00039         typename T4  = NullType, typename T5  = NullType, typename T6  = NullType,
00040         typename T7  = NullType, typename T8  = NullType, typename T9  = NullType,
00041         typename T10 = NullType, typename T11 = NullType, typename T12 = NullType,
00042         typename T13 = NullType, typename T14 = NullType, typename T15 = NullType,
00043         typename T16 = NullType, typename T17 = NullType, typename T18 = NullType
00044         >
00045     struct MakeTypelist{
00046     private:
00047     typedef typename MakeTypelist
00048     <
00049         T2 , T3 , T4 ,
00050         T5 , T6 , T7 ,
00051         T8 , T9 , T10,
00052         T11, T12, T13,
00053         T14, T15, T16,
00054         T17, T18
00055         >
00056     ::Result TailResult;
00057 
00058 public:
00059     typedef Typelist<T1, TailResult> Result;
00060 };
00061 
00062 template<>
00063 struct MakeTypelist<>
00064 {
00065     typedef NullType Result;
00066 };
00067 
00068 }
00069 
00084 template <class Product>
00085 struct OApplicationFactory : public ApplicationInterface {
00086     QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) {
00087         *iface = 0;
00088         if ( uuid == IID_QUnknown ) *iface = this;
00089         else if ( uuid == IID_QtopiaApplication ) *iface = this;
00090         else return QS_FALSE;
00091         (*iface)->addRef();
00092         return QS_OK;
00093     }
00094 
00095     /*
00096      *
00097      */
00098     virtual QWidget *createMainWindow( const QString& appName, QWidget* parent,
00099                                        const char* name, Qt::WFlags f ) {
00100         if (appName == Product::appName() )
00101             return new Product(parent, name, f );
00102         else
00103             return 0l;
00104     }
00105 
00106     virtual QStringList applications()const {
00107         QStringList list;
00108         list << Product::appName() ;
00109 
00110         return list;
00111     }
00112     Q_REFCOUNT
00113 
00114 };
00115 
00116 
00117 /* Internal */
00118 
00119 template< class Product >
00120 struct OPrivate {
00121     inline static QWidget *multiFactory( const QString& appName, QWidget* parent,
00122                            const char* name, Qt::WFlags fl ) {
00123         if ( appName == Product::appName() )
00124             return new Product( parent, name, fl );
00125         else
00126             return 0;
00127     }
00128 
00129     inline static QStringList multiString( const QStringList& _list ) {
00130         QStringList list = _list;
00131         list << Product::appName();
00132         return list;
00133     }
00134 };
00135 
00136 template <>
00137 struct OPrivate<Opie::NullType > {
00138     inline static QWidget* multiFactory ( const QString& , QWidget* ,
00139                             const char* , Qt::WFlags ) {
00140         return 0l;
00141     }
00142     inline static QStringList multiString( const QStringList& _list ) {
00143         return _list;
00144     }
00145 };
00146 
00147 /*
00148 template <>
00149 struct OPrivate <Opie::NullType, Opie::NullType > {
00150     inline static QWidget* multiFactory( const QString& , QWidget* ,
00151                            const char* , Qt::WFlags  ) {
00152         return 0l;
00153     }
00154 
00155     inline static QStringList multiString( const QStringList& _list ) {
00156         return _list;
00157     }
00158 };
00159 */
00160 
00161 template <class Product, class ProductListTail>
00162 struct OPrivate< Opie::Typelist<Product, ProductListTail> > {
00163     inline static QWidget* multiFactory( const QString& appName, QWidget* parent,
00164                            const char* name, Qt::WFlags fl) {
00165         QWidget* wid = OPrivate<Product>::multiFactory( appName, parent, name, fl );
00166 
00167         if (!wid )
00168             wid = OPrivate<ProductListTail>::multiFactory( appName, parent, name, fl );
00169 
00170         return wid;
00171     }
00172 
00173     inline static QStringList multiString( const QStringList& _list ) {
00174         QStringList list = _list;
00175 
00176         list = OPrivate<Product>::multiString( list );
00177         list = OPrivate<ProductListTail>::multiString( list );
00178 
00179         return list;
00180     }
00181 };
00182 
00183 
00184 
00185 
00186 
00187 
00188 
00189 
00190 /* Internal END */
00191 
00192 /*
00193  * If you want to export more than one Widget use that function
00194  * Make sure all your Widgets provide the appName() static method
00195  * otherwise you'll get a compiler error
00196  *
00197  * typedef Opie::MakeTypeList<MyWidget, MyDialog, MyMediaPlayer >::Result MyTypes;
00198  * OPIE_EXPORT_APP( OApplicationFactory<MyTypes> )
00199  */
00200 
00201 template<class Product, class ProductListTail>
00202 struct OApplicationFactory< Opie::Typelist<Product, ProductListTail > >
00203     : ApplicationInterface {
00204     QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) {
00205         *iface = 0;
00206         if ( uuid == IID_QUnknown ) *iface = this;
00207         else if ( uuid ==IID_QtopiaApplication ) *iface = this;
00208         else return QS_FALSE;
00209         (*iface)->addRef();
00210         return QS_OK;
00211     }
00212 
00213     QWidget* createMainWindow ( const QString& appName, QWidget* parent,
00214                                 const char* name, Qt::WFlags fl ) {
00215         qWarning("StringList is %s", applications().join(":").latin1() );
00216         return OPrivate< Opie::Typelist<Product, ProductListTail > >::multiFactory( appName, parent, name, fl );
00217     }
00218 
00219     QStringList applications()const {
00220         QStringList _list;
00221         return OPrivate< Opie::Typelist<Product, ProductListTail> >::multiString( _list );
00222     }
00223 
00224     Q_REFCOUNT
00225 };
00226 
00227 
00228 /* If the library version should be build */
00229 #ifdef OPIE_APP_INTERFACE
00230 #define OPIE_EXPORT_APP( factory ) Q_EXPORT_INTERFACE() { Q_CREATE_INSTANCE( factory ) }
00231 #else
00232 
00233 #include <qpe/qpeapplication.h>
00234 
00235 #define OPIE_EXPORT_APP( Factory )                                      \
00236 int main( int argc,  char **argv ) {                                    \
00237     QPEApplication a(argc, argv );               \
00238     QWidget *mw = 0;\
00239 \
00240     /* method from TT */ \
00241     QString executableName = QString::fromLatin1( argv[0] ); \
00242     executableName =  executableName.right(executableName.length() \
00243         - executableName.findRev('/') - 1); \
00244  \
00245     Factory f; \
00246     QStringList list = f.applications(); \
00247     if (list.contains(executableName) ) \
00248         mw = f.createMainWindow(executableName, 0, 0, 0 ); \
00249     else \
00250         mw = f.createMainWindow( list[0], 0, 0, 0 ); \
00251 \
00252     if( mw ) { \
00253         if ( mw->metaObject()->slotNames().contains("setDocument(const QString&)" ) ) \
00254             a.showMainDocumentWidget( mw ); \
00255         else \
00256             a.showMainWidget( mw ); \
00257 \
00258         int rv = a.exec(); \
00259         delete mw; \
00260         return rv; \
00261     }else \
00262         return -1; \
00263 }
00264 #endif
KDE Logo
This file is part of the documentation for OPIE Version 1.1.
Documentation copyright © 1997-2003 the KDE developers. 2003 OPIE developers
Generated on Tue Feb 10 20:24:42 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001