Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

oapplicationfactory.h

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003                              Copyright (C) Holger Freyther <zecke@handhelds.org>
00004               =.
00005             .=l.
00006            .>+-=
00007  _;:,     .>    :=|.         This program is free software; you can
00008 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00009 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00010 .="- .-=="i,     .._         License as published by the Free Software
00011  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00012      ._= =}       :          or (at your option) any later version.
00013     .%`+i>       _;_.
00014     .i_,=:_.      -<s.       This program is distributed in the hope that
00015      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00016     : ..    .:,     . . .    without even the implied warranty of
00017     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00018   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00019 ..}^=.=       =       ;      Library General Public License for more
00020 ++=   -.     .`     .:       details.
00021  :     =  ...= . :.=-
00022  -.   .:....=;==+<;          You should have received a copy of the GNU
00023   -_. . .   )=.  =           Library General Public License along with
00024     --        :-=`           this library; see the file COPYING.LIB.
00025                              If not, write to the Free Software Foundation,
00026                              Inc., 59 Temple Place - Suite 330,
00027                              Boston, MA 02111-1307, USA.
00028 */
00029 
00030 /*
00031  This work is derived from:
00032  ----
00033  The Loki Library
00034  Copyright (c) 2001 by Andrei Alexandrescu
00035  This code accompanies the book:
00036  Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
00037      Patterns Applied". Copyright (c) 2001. Addison-Wesley.
00038  Permission to use, copy, modify, distribute and sell this software for any
00039      purpose is hereby granted without fee, provided that the above copyright
00040      notice appear in all copies and that both that copyright notice and this
00041      permission notice appear in supporting documentation.
00042  The author or Addison-Welsey Longman make no representations about the
00043      suitability of this software for any purpose. It is provided "as is"
00044      without express or implied warranty.
00045  ----
00046 
00047   And KGenericFactor et all from Simon Hausmann <tronical@kde.org>
00048 
00049 */
00050 
00051 #include <qstring.h>
00052 #include <qmetaobject.h>
00053 
00054 #include <qtopia/qcom.h>
00055 #include <qtopia/applicationinterface.h>
00056 
00057 #include <opie2/odebug.h>
00058 
00059 namespace Opie {
00060 namespace Core {
00061     struct NullType;
00062 
00063     template <class T, class U>
00064     struct Typelist
00065     {
00066        typedef T Head;
00067        typedef U Tail;
00068     };
00069     template<
00070         typename T1  = NullType, typename T2  = NullType, typename T3  = NullType,
00071         typename T4  = NullType, typename T5  = NullType, typename T6  = NullType,
00072         typename T7  = NullType, typename T8  = NullType, typename T9  = NullType,
00073         typename T10 = NullType, typename T11 = NullType, typename T12 = NullType,
00074         typename T13 = NullType, typename T14 = NullType, typename T15 = NullType,
00075         typename T16 = NullType, typename T17 = NullType, typename T18 = NullType
00076         >
00077     struct MakeTypelist{
00078     private:
00079     typedef typename MakeTypelist
00080     <
00081         T2 , T3 , T4 ,
00082         T5 , T6 , T7 ,
00083         T8 , T9 , T10,
00084         T11, T12, T13,
00085         T14, T15, T16,
00086         T17, T18
00087         >
00088     ::Result TailResult;
00089 
00090 public:
00091     typedef Typelist<T1, TailResult> Result;
00092 };
00093 
00094 template<>
00095 struct MakeTypelist<>
00096 {
00097     typedef NullType Result;
00098 };
00099 
00100 
00115 template <class Product>
00116 struct OApplicationFactory : public ApplicationInterface {
00117     QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) {
00118         *iface = 0;
00119         if ( uuid == IID_QUnknown ) *iface = this;
00120         else if ( uuid == IID_QtopiaApplication ) *iface = this;
00121         else return QS_FALSE;
00122         (*iface)->addRef();
00123         return QS_OK;
00124     }
00125 
00126     /*
00127      *
00128      */
00129     virtual QWidget *createMainWindow( const QString& appName, QWidget* parent,
00130                                        const char* name, Qt::WFlags f ) {
00131         if (appName == Product::appName() )
00132             return new Product(parent, name, f );
00133         else{
00134             odebug << "Application Name = " << appName.latin1() << oendl;
00135             odebug << "ProductName      = " << Product::appName().latin1() << oendl;
00136             odebug << "The application name is not equal to the product name!" << oendl; 
00137             odebug << "Please compare TARGET entry in the project file (*.pro) and the call of the OApplicationFactory< productName >" << oendl;
00138             return 0l;
00139         }
00140     }
00141 
00142     virtual QStringList applications()const {
00143         QStringList list;
00144         list << Product::appName() ;
00145 
00146         return list;
00147     }
00148     Q_REFCOUNT
00149 
00150 };
00151 
00152 
00153 /* Internal */
00154 
00155 template< class Product >
00156 struct OPrivate {
00157     inline static QWidget *multiFactory( const QString& appName, QWidget* parent,
00158                            const char* name, Qt::WFlags fl ) {
00159         if ( appName == Product::appName() )
00160             return new Product( parent, name, fl );
00161         else
00162             return 0;
00163     }
00164 
00165     inline static QStringList multiString( const QStringList& _list ) {
00166         QStringList list = _list;
00167         list << Product::appName();
00168         return list;
00169     }
00170 };
00171 
00172 template <>
00173 struct OPrivate<Opie::Core::NullType > {
00174     inline static QWidget* multiFactory ( const QString& , QWidget* ,
00175                             const char* , Qt::WFlags ) {
00176         return 0l;
00177     }
00178     inline static QStringList multiString( const QStringList& _list ) {
00179         return _list;
00180     }
00181 };
00182 
00183 /*
00184 template <>
00185 struct OPrivate <Opie::NullType, Opie::NullType > {
00186     inline static QWidget* multiFactory( const QString& , QWidget* ,
00187                            const char* , Qt::WFlags  ) {
00188         return 0l;
00189     }
00190 
00191     inline static QStringList multiString( const QStringList& _list ) {
00192         return _list;
00193     }
00194 };
00195 */
00196 
00197 template <class Product, class ProductListTail>
00198 struct OPrivate< Opie::Core::Typelist<Product, ProductListTail> > {
00199     inline static QWidget* multiFactory( const QString& appName, QWidget* parent,
00200                            const char* name, Qt::WFlags fl) {
00201         QWidget* wid = OPrivate<Product>::multiFactory( appName, parent, name, fl );
00202 
00203         if (!wid )
00204             wid = OPrivate<ProductListTail>::multiFactory( appName, parent, name, fl );
00205 
00206         return wid;
00207     }
00208 
00209     inline static QStringList multiString( const QStringList& _list ) {
00210         QStringList list = _list;
00211 
00212         list = OPrivate<Product>::multiString( list );
00213         list = OPrivate<ProductListTail>::multiString( list );
00214 
00215         return list;
00216     }
00217 };
00218 
00219 
00220 
00221 
00222 
00223 
00224 
00225 
00226 /* Internal END */
00227 
00228 /*
00229  * If you want to export more than one Widget use that function
00230  * Make sure all your Widgets provide the appName() static method
00231  * otherwise you'll get a compiler error
00232  *
00233  * typedef Opie::Core::MakeTypelist<MyWidget, MyDialog, MyMediaPlayer >::Result MyTypes;
00234  * OPIE_EXPORT_APP( OApplicationFactory<MyTypes> )
00235  */
00236 
00237 template<class Product, class ProductListTail>
00238 struct OApplicationFactory< Opie::Core::Typelist<Product, ProductListTail > >
00239     : ApplicationInterface {
00240     QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) {
00241         *iface = 0;
00242         if ( uuid == IID_QUnknown ) *iface = this;
00243         else if ( uuid ==IID_QtopiaApplication ) *iface = this;
00244         else return QS_FALSE;
00245         (*iface)->addRef();
00246         return QS_OK;
00247     }
00248 
00249     QWidget* createMainWindow ( const QString& appName, QWidget* parent,
00250                                 const char* name, Qt::WFlags fl ) {
00251         qWarning("StringList is %s", applications().join(":").latin1() );
00252         return OPrivate< Opie::Core::Typelist<Product, ProductListTail > >::multiFactory( appName, parent, name, fl );
00253     }
00254 
00255     QStringList applications()const {
00256         QStringList _list;
00257         return OPrivate< Opie::Core::Typelist<Product, ProductListTail> >::multiString( _list );
00258     }
00259 
00260     Q_REFCOUNT
00261 };
00262 
00263 }
00264 }
00265 
00266 /* If the library version should be build */
00267 #ifdef OPIE_APP_INTERFACE
00268 #define OPIE_EXPORT_APP( factory ) Q_EXPORT_INTERFACE() { Q_CREATE_INSTANCE( factory ) }
00269 #else
00270 
00271 #include <qpe/qpeapplication.h>
00272 
00273 #define OPIE_EXPORT_APP( Factory )                                      \
00274 int main( int argc,  char **argv ) {                                    \
00275     QPEApplication a(argc, argv );               \
00276     QWidget *mw = 0;\
00277 \
00278     /* method from TT */ \
00279     QString executableName = QString::fromLatin1( argv[0] ); \
00280     executableName =  executableName.right(executableName.length() \
00281             - executableName.findRev('/') - 1); \
00282  \
00283     Factory f; \
00284     QStringList list = f.applications(); \
00285     if (list.contains(executableName) ) \
00286         mw = f.createMainWindow(executableName, 0, 0, 0 ); \
00287     else \
00288         mw = f.createMainWindow( list[0], 0, 0, 0 ); \
00289 \
00290     if( mw ) { \
00291         if ( mw->metaObject()->slotNames().contains("setDocument(const QString&)" ) ) \
00292             a.showMainDocumentWidget( mw ); \
00293         else \
00294             a.showMainWidget( mw ); \
00295 \
00296         int rv = a.exec(); \
00297         delete mw; \
00298         return rv; \
00299     }else \
00300         return -1; \
00301 }
00302 #endif
00303 
00304 #ifdef OPIE_APP_INTERFACE
00305 #define OPIE_EXPORT_APP_V2( factory,name ) Q_EXPORT_INTERFACE() { Q_CREATE_INSTANCE( factory ) }
00306 #else
00307 
00308 #include <opie2/oapplication.h>
00309 
00310 #define OPIE_EXPORT_APP_V2( Factory,name )                                      \
00311 int main( int argc,  char **argv ) {                                    \
00312     Opie::Core::OApplication a(argc, argv, name );               \
00313     QWidget *mw = 0;\
00314 \
00315     /* method from TT */ \
00316     QString executableName = QString::fromLatin1( argv[0] ); \
00317     executableName =  executableName.right(executableName.length() \
00318             - executableName.findRev('/') - 1); \
00319  \
00320     Factory f; \
00321     QStringList list = f.applications(); \
00322     if (list.contains(executableName) ) \
00323         mw = f.createMainWindow(executableName, 0, 0, 0 ); \
00324     else \
00325         mw = f.createMainWindow( list[0], 0, 0, 0 ); \
00326 \
00327     if( mw ) { \
00328         if ( mw->metaObject()->slotNames().contains("setDocument(const QString&)" ) ) \
00329             a.showMainDocumentWidget( mw ); \
00330         else \
00331             a.showMainWidget( mw ); \
00332 \
00333         int rv = a.exec(); \
00334         delete mw; \
00335         return rv; \
00336     }else \
00337         return -1; \
00338 }
00339 #endif
00340 
00341 
00342 #define OPIE_EXPORT_APPNAME  static QString appName() { return QString::fromLatin1( QUICKAPP_NAME ); }

Generated on Sat Nov 5 16:16:07 2005 for OPIE by  doxygen 1.4.2