summaryrefslogtreecommitdiff
path: root/libopie/oapplicationfactory.h
blob: ab88d80437316bb69ea7f7ca7cbe1e6d5f904555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
 This work is derived from:
 ----
 The Loki Library
 Copyright (c) 2001 by Andrei Alexandrescu
 This code accompanies the book:
 Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
     Patterns Applied". Copyright (c) 2001. Addison-Wesley.
 Permission to use, copy, modify, distribute and sell this software for any
     purpose is hereby granted without fee, provided that the above copyright
     notice appear in all copies and that both that copyright notice and this
     permission notice appear in supporting documentation.
 The author or Addison-Welsey Longman make no representations about the
     suitability of this software for any purpose. It is provided "as is"
     without express or implied warranty.
 ----

  And KGenericFactor et all from Simon Hausmann <tronical@kde.org>

*/

#include <qstring.h>
#include <qmetaobject.h>

#include <qtopia/qcom.h>
#include <qtopia/applicationinterface.h>

namespace Opie {
    struct NullType;

    template <class T, class U>
    struct Typelist
    {
       typedef T Head;
       typedef U Tail;
    };
    template<
        typename T1  = NullType, typename T2  = NullType, typename T3  = NullType,
        typename T4  = NullType, typename T5  = NullType, typename T6  = NullType,
        typename T7  = NullType, typename T8  = NullType, typename T9  = NullType,
        typename T10 = NullType, typename T11 = NullType, typename T12 = NullType,
        typename T13 = NullType, typename T14 = NullType, typename T15 = NullType,
        typename T16 = NullType, typename T17 = NullType, typename T18 = NullType
        >
    struct MakeTypelist{
    private:
    typedef typename MakeTypelist
    <
        T2 , T3 , T4 ,
        T5 , T6 , T7 ,
        T8 , T9 , T10,
        T11, T12, T13,
        T14, T15, T16,
        T17, T18
        >
    ::Result TailResult;

public:
    typedef Typelist<T1, TailResult> Result;
};

template<>
struct MakeTypelist<>
{
    typedef NullType Result;
};

}

/**
 * To allow your application to be quick launched some one needs
 * to create the QWidget.
 * This is this factory. Make surce your widget has static QString Widget::appName()
 * as one of its functions.
 *
 * This template takes one QWidget and initialized it in the form of
 *  MyWidget::MyWidget( QWidget* parent, const char* name, WFlags f );
 *
 * To use it on your app do that:
 * typedef OApplicationFactory<MyWidget> MyFactory;
 * OPIE_EXPORT_APP( MyFactory )
 *
 */
template <class Product>
struct OApplicationFactory : public ApplicationInterface {
    QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) {
        *iface = 0;
        if ( uuid == IID_QUnknown ) *iface = this;
        else if ( uuid == IID_QtopiaApplication ) *iface = this;
        else return QS_FALSE;
        (*iface)->addRef();
        return QS_OK;
    }

    /*
     *
     */
    virtual QWidget *createMainWindow( const QString& appName, QWidget* parent,
                                       const char* name, Qt::WFlags f ) {
        if (appName == Product::appName() )
            return new Product(parent, name, f );
        else
            return 0l;
    }

    virtual QStringList applications()const {
        QStringList list;
        list << Product::appName() ;

        return list;
    }
    Q_REFCOUNT

};


/* Internal */

template< class Product >
struct OPrivate {
    inline static QWidget *multiFactory( const QString& appName, QWidget* parent,
                           const char* name, Qt::WFlags fl ) {
        if ( appName == Product::appName() )
            return new Product( parent, name, fl );
        else
            return 0;
    }

    inline static QStringList multiString( const QStringList& _list ) {
        QStringList list = _list;
        list << Product::appName();
        return list;
    }
};

template <>
struct OPrivate<Opie::NullType > {
    inline static QWidget* multiFactory ( const QString& , QWidget* ,
                            const char* , Qt::WFlags ) {
        return 0l;
    }
    inline static QStringList multiString( const QStringList& _list ) {
        return _list;
    }
};

/*
template <>
struct OPrivate <Opie::NullType, Opie::NullType > {
    inline static QWidget* multiFactory( const QString& , QWidget* ,
                           const char* , Qt::WFlags  ) {
        return 0l;
    }

    inline static QStringList multiString( const QStringList& _list ) {
        return _list;
    }
};
*/

template <class Product, class ProductListTail>
struct OPrivate< Opie::Typelist<Product, ProductListTail> > {
    inline static QWidget* multiFactory( const QString& appName, QWidget* parent,
                           const char* name, Qt::WFlags fl) {
        QWidget* wid = OPrivate<Product>::multiFactory( appName, parent, name, fl );

        if (!wid )
            wid = OPrivate<ProductListTail>::multiFactory( appName, parent, name, fl );

        return wid;
    }

    inline static QStringList multiString( const QStringList& _list ) {
        QStringList list = _list;

        list = OPrivate<Product>::multiString( list );
        list = OPrivate<ProductListTail>::multiString( list );

        return list;
    }
};








/* Internal END */

/*
 * If you want to export more than one Widget use that function
 * Make sure all your Widgets provide the appName() static method
 * otherwise you'll get a compiler error
 *
 * typedef Opie::MakeTypeList<MyWidget, MyDialog, MyMediaPlayer >::Result MyTypes;
 * OPIE_EXPORT_APP( OApplicationFactory<MyTypes> )
 */

template<class Product, class ProductListTail>
struct OApplicationFactory< Opie::Typelist<Product, ProductListTail > >
    : ApplicationInterface {
    QRESULT queryInterface( const QUuid &uuid, QUnknownInterface **iface ) {
        *iface = 0;
        if ( uuid == IID_QUnknown ) *iface = this;
        else if ( uuid ==IID_QtopiaApplication ) *iface = this;
        else return QS_FALSE;
        (*iface)->addRef();
        return QS_OK;
    }

    QWidget* createMainWindow ( const QString& appName, QWidget* parent,
                                const char* name, Qt::WFlags fl ) {
        qWarning("StringList is %s", applications().join(":").latin1() );
        return OPrivate< Opie::Typelist<Product, ProductListTail > >::multiFactory( appName, parent, name, fl );
    }

    QStringList applications()const {
        QStringList _list;
        return OPrivate< Opie::Typelist<Product, ProductListTail> >::multiString( _list );
    }

    Q_REFCOUNT
};


/* If the library version should be build */
#ifdef OPIE_APP_INTERFACE
#define OPIE_EXPORT_APP( factory ) Q_EXPORT_INTERFACE() { Q_CREATE_INSTANCE( factory ) }
#else

#include <qpe/qpeapplication.h>

#define OPIE_EXPORT_APP( Factory )                                      \
int main( int argc,  char **argv ) {                                    \
    QPEApplication a(argc, argv );               \
    QWidget *mw = 0;\
\
    /* method from TT */ \
    QString executableName = QString::fromLatin1( argv[0] ); \
    executableName =  executableName.right(executableName.length() \
	    - executableName.findRev('/') - 1); \
 \
    Factory f; \
    QStringList list = f.applications(); \
    if (list.contains(executableName) ) \
        mw = f.createMainWindow(executableName, 0, 0, 0 ); \
    else \
        mw = f.createMainWindow( list[0], 0, 0, 0 ); \
\
    if( mw ) { \
        if ( mw->metaObject()->slotNames().contains("setDocument(const QString&)" ) ) \
            a.showMainDocumentWidget( mw ); \
        else \
            a.showMainWidget( mw ); \
\
        int rv = a.exec(); \
        delete mw; \
        return rv; \
    }else \
        return -1; \
}
#endif