summaryrefslogtreecommitdiffstats
path: root/tools/servicedbgen/servicedbgen.cpp
blob: 0e3360a8ffb4131790a306f260a1217bcdd1f9d1 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtPlugin>

#include <stdio.h>
#include <QtCore>
#include <QtSql>
#include <servicedatabase_p.h>
#include <servicemetadata_p.h>
#include <databasemanager_p.h>

QT_USE_NAMESPACE

QTM_USE_NAMESPACE
/*
 * If the emulator supports per process writeable static
 * data then we can use a separate server process and
 * put the db in the same location as on a real
 * device
 */
#define WINSCW_DES_DEPLOY_WSD   "epoc32/winscw/c/private/2002AC7F/des"
#define WINSCW_DBPATH_WSD       "epoc32/winscw/c/private/2002AC7F"

#define WINSCW_DES_DEPLOY       "epoc32/winscw/c/private/2002AC7F/des"
#define WINSCW_DBPATH           "epoc32/winscw/c/data/temp/QtServiceFW"

#define HW_DES_DEPLOY           "epoc32/data/z/private/2002AC7F/des"
#define HW_DBPATH               "epoc32/data/z/private/2002AC7F"

#define SEC_TOKEN               0x101FB657
#define TOOL_VERSION            "0.4"

#define MESSAGE(msg) \
{ \
   *stdoutStream << msg; \
   stdoutStream->flush(); \
}

QString dbName()
{
    QString qtVersion(qVersion());
    qtVersion = qtVersion.left(qtVersion.size() -2); //strip off patch version
    return QString("QtServiceFramework_") + qtVersion + "_system";
};

class GeneratorPaths
{
public:
    GeneratorPaths(){};
    GeneratorPaths(const QString &dbPath, const QString &desPath) : databasePath(dbPath), descriptorsPath(desPath){};
    ~GeneratorPaths(){};
    GeneratorPaths(const GeneratorPaths &copy){databasePath = copy.databasePath; descriptorsPath = copy.descriptorsPath;};
public:
    QString databasePath;
    QString descriptorsPath;
};

class CommandProcessor : public QObject
{
    Q_OBJECT

public:
    CommandProcessor(QObject *parent = 0);
    ~CommandProcessor();

    bool initialize();
    void execute(const QStringList &options, const QString &cmd, const QStringList &args);
    void showUsage();

public slots:
    void add(const QStringList &desList, ServiceDatabase *db);
    void remove(const QStringList &desList, ServiceDatabase *db);

private:
    bool setOptions(const QStringList &options, QMap<QString, GeneratorPaths> &targets);
    ServiceDatabase *initTargetDatabase(const QString &dbPath, bool deleteDbAllowed);
    QString getServiceFromXML(const QString &xmlFile);
    QStringList getServiceDescriptors(const QString &path);
    QStringList targetServiceDescriptors(const QString &desPath, const QStringList &args);

    bool batchMode;
    bool requireInitialization;
    bool deleteDatabase;
    QTextStream *stdoutStream;
    QMap<QString, QString> targetMap; // (target, platform)
    QMap<QString, GeneratorPaths> pathMap; // (platform, (databasePath, descriptorsPath))
    QMap<QString, GeneratorPaths> wsdMap; // (platform, (databasePath, descriptorsPath))
};

CommandProcessor::CommandProcessor(QObject *parent)
    : QObject(parent),
      batchMode(false),
      requireInitialization(false),
      deleteDatabase(false),
      stdoutStream(new QTextStream(stdout))
{
}

CommandProcessor::~CommandProcessor()
{
    delete stdoutStream;
}

bool CommandProcessor::initialize()
{
    QString epocRoot(qgetenv("EPOCROOT").data());
    if (epocRoot.isEmpty()) {
        *stdoutStream << "servicedbgen failed: EPOCROOT not set\n";
        return false;
    }

    targetMap["winscw"] = "emulator";
    targetMap["armv5"] = "hw";
    targetMap["armv6"] = "hw";
    targetMap["gcce"] = "hw";

    if (epocRoot.right(1) != QDir::separator())
        epocRoot += QDir::separator();

    wsdMap["nowsd"] = GeneratorPaths(QDir::toNativeSeparators(epocRoot + WINSCW_DBPATH),
                                     QDir::toNativeSeparators(epocRoot + WINSCW_DES_DEPLOY));

    wsdMap["wsd"] = GeneratorPaths(QDir::toNativeSeparators(epocRoot + WINSCW_DBPATH_WSD),
                                   QDir::toNativeSeparators(epocRoot + WINSCW_DES_DEPLOY_WSD));

#ifdef QT_SYMBIAN_EMULATOR_SUPPORTS_PERPROCESS_WSD
    pathMap["emulator"] = wsdMap["wsd"];
#else
    pathMap["emulator"] = wsdMap["nowsd"];
#endif
    pathMap["hw"] = GeneratorPaths(QDir::toNativeSeparators(epocRoot + HW_DBPATH),
                                   QDir::toNativeSeparators(epocRoot + HW_DES_DEPLOY));
    return true;
}

void CommandProcessor::execute(const QStringList &options, const QString &cmd, const QStringList &args)
{
    if(cmd.isEmpty())
        MESSAGE("servicedbgen failed: no command given\n\n");

    // setup options and collect target(s)
    // parse options, and fails if there's no cmd
    // usage output includes paths and targets, so make
    // sure to parse options that might change them
    QMap<QString, GeneratorPaths> targets;
    if (!setOptions(options, targets) || cmd.isEmpty()) {
        showUsage();
        return;
    }

    int methodIndex = metaObject()->indexOfMethod(cmd.toAscii() + "(QStringList,ServiceDatabase*)");
    if (methodIndex < 0) {
        MESSAGE("Bad command: " << cmd << "\n\n");
        showUsage();
        return;
    }

    QMapIterator<QString, GeneratorPaths> i(targets);
    while (i.hasNext()) {
        i.next();
        const QString &target = i.key();
        const GeneratorPaths &paths = i.value();
        ServiceDatabase *db = initTargetDatabase(paths.databasePath, true);
        if (!db) {
            MESSAGE("servicedbgen failed: database for " << target << " cannot be opened/created.\n");
            continue;
        }
        MESSAGE("Database[" << i.key() << "]: " << db->databasePath() << '\n');
        QStringList desList = (batchMode) ? targetServiceDescriptors(paths.descriptorsPath, args) : args;
        // register services
        metaObject()->method(methodIndex).invoke(this, Q_ARG(QStringList, desList), Q_ARG(ServiceDatabase*, db));
        // remove database so other targets can be handled
        db->close();
        QSqlDatabase::removeDatabase(db->m_connectionName);
        delete db;
    }
}

void CommandProcessor::showUsage()
{
		*stdoutStream << "Service framework database management tool, version " << QString(TOOL_VERSION) << " with QtMobility " << QString(QTM_VERSION_STR) << "\n"
            "Usage: servicedbgen [options] <command> [command parameters]\n\n"
            "Commands:\n"
            "\tadd     Register or update a service\n"
            "\tremove  Unregister a service\n"
            "\nOptions:\n"
            "\t-t<tgt> Specifies the target platform to be used (e.g. armv5, all).\n"
            "\t-b      Batch mode (take service descriptors from a path). If specified, \n"
            "\t        the command parameters are specifying the path to the folder\n"
            "\t        where the service descriptors reside. In case command parameters\n"
            "\t        are not given, the target specific default path will be taken\n"
            "\t        (see paths below).\n"
            "\t-c      Delete the service database, has effect only in batch mode. \n"
            "\t-i      The services require initialization upon first load.\n"
            "\t-w<1|0> Override the default support for WSD in the emulator\n"
            "\n"
            "Supported targets and their database paths:";
    QMapIterator<QString, QString> i(targetMap);
    while (i.hasNext()) {
        i.next();
        GeneratorPaths out(pathMap[i.value()]);
        *stdoutStream << "\nTarget: " << i.key() << "\n\tpath: " << out.databasePath << "\n\tdeployment: " << out.descriptorsPath << '\n';
    }
    *stdoutStream << "\nExamples:\n"
        " - registering service1 and service2 for winscw target:\n"
        "\tservicedbgen -twinscw add service1.xml service2.xml\n\n"
        " - registering service1 for all targets, service requesting initialization:\n"
        "\tservicedbgen -tall -i add service1.xml\n\n"
        " - unregistering service1 from all targets:\n"
        "\tservicedbgen -tall remove service1.xml\n\n"
        " - registering all services for armv5 from default descriptor path:\n"
        "\tservicedbgen -tall -b -c add\n\n"
        " - registering services for all targets from specific descriptor path:\n"
        "\tservicedbgen -tall -b add ./mypath/services\n\n"
        " - removing services for all targets from default descriptor path:\n"
        "\tservicedbgen -tall -b remove\n\n";
    stdoutStream->flush();
}

bool CommandProcessor::setOptions(const QStringList &options, QMap<QString, GeneratorPaths> &targets)
{
    QStringList opts = options;
    QMutableListIterator<QString> i(opts);

    while (i.hasNext()) {
        QString option = i.next();
        if (option == "-b") {
            batchMode = true;
            i.remove();
        }
        else if (option == "-c") {
            deleteDatabase = true;
            i.remove();
        }
        else if (option == "-i") {
            requireInitialization = true;
            i.remove();
        }
        else if (option.startsWith("-t")) {
            QString target = option.right(option.size() - 2).toLower();
            if (target == "all") {
                // copy all target paths
                targets.clear();
                targets = pathMap;
            }
            else if (targetMap[target].isEmpty()) {
                MESSAGE("servicedbgen failed: unknown target " << target << '\n');
                return false;
            }
            else if (!targets.contains(targetMap[target])){
                targets[targetMap[target]] = pathMap[targetMap[target]];
            }
            i.remove();
        }
        else if (option.startsWith("-w")) {
            QString wsd = option.right(option.size() - 2).toLower();
            if (wsd == "0") {
                pathMap["emulator"] = wsdMap["nowsd"];
            }
            else if (wsd == "1"){
                pathMap["emulator"] = wsdMap["wsd"];
            }
            else {
                MESSAGE("servicedbgen failed: unknown parameter " << wsd << '\n');
                return false;
            }
            if(targets.contains("emulator"))
                targets["emulator"] = pathMap["emulator"];
            i.remove();
        }
    }
    if (!opts.isEmpty()) {
        MESSAGE("Bad options: " << opts.join(" ") << "\n\n");
        return false;
    }
    if (targets.isEmpty()) {
        MESSAGE("No options\n");
        targets = pathMap;
    }

    return true;
}

ServiceDatabase *CommandProcessor::initTargetDatabase(const QString &databasePath, bool deleteDbAllowed)
{
    if (!databasePath.isEmpty()) {

        ServiceDatabase *db = new ServiceDatabase;
        db->setDatabasePath(databasePath + QDir::separator() + dbName() + QLatin1String(".db"));

        if (batchMode && deleteDatabase && deleteDbAllowed) {
            // delete file
            QFile dbFile(db->databasePath());
            dbFile.remove();
        }
        if (db->open())
            return db;
        else
            delete db;
    }
    return NULL;
}

QString CommandProcessor::getServiceFromXML(const QString &xmlFile)
{
    QFile file(xmlFile);
    ServiceMetaData data(0);
    if (file.open(QIODevice::ReadOnly)) {
        data.setDevice(&file);
        if (data.extractMetadata()) {
            return data.parseResults().name;
        }
    }

    return QString();
}

QStringList CommandProcessor::getServiceDescriptors(const QString &path)
{
    QDir dir(QDir::toNativeSeparators(path));
    if (!dir.exists()) {
        return QStringList();
    }
    QStringList fileExt;
    fileExt << "*.xml";
    QStringList files = dir.entryList(fileExt);
    QStringList ret;
    foreach (const QString &f, files) {
        ret << (QDir::toNativeSeparators(path) + QDir::separator() + f);
    }
    return ret;
}

QStringList CommandProcessor::targetServiceDescriptors(const QString &desPath, const QStringList &args)
{
    if (batchMode) {
        // arguments are paths or empty!
        if (args.isEmpty()) {
            // default path for target
            QDir::root().mkpath(desPath);
            return getServiceDescriptors(desPath);
        }
        else {
            // get files from arguments
            QStringList files;
            foreach (const QString &folder, args)
                files << getServiceDescriptors(folder);
            return files;
        }
    }

    // individual mode
    return args;
}

void CommandProcessor::add(const QStringList &desList, ServiceDatabase *db)
{
    if (desList.isEmpty()) {
        MESSAGE("servicedbgen failed: No descriptor files/path specified, or the path does not contain service descriptors!\n");
        MESSAGE("Usage:\n\tadd <service-xml-file(s)>[|path-to-service-xmls]\n");
        showUsage();
        return;
    }

    QString securityToken = QString::number(SEC_TOKEN);

    foreach (const QString &serviceXml, desList) {
        QFile f(serviceXml);
        ServiceMetaData parser(&f);
        if (!parser.extractMetadata()) {
            MESSAGE("Parsing in servicedbgen failed: " << serviceXml << '\n');
            continue;
        }

        const ServiceMetaDataResults results = parser.parseResults();
        db->unregisterService(results.name, securityToken);
        if (db->registerService(results, securityToken)) {
            if (!requireInitialization) {
                // remove the previous installation of the service from the database
                db->serviceInitialized(results.name, securityToken);
            }
            MESSAGE("Service " << results.name << " registered\n");
        }
        else
            MESSAGE("servicedbgen failed: Service " << results.name << " registration to " << db->databasePath() << " failed\n");
    }
}

void CommandProcessor::remove(const QStringList &desList, ServiceDatabase *db)
{
    if (desList.isEmpty()) {
        MESSAGE("servicedbgen failed: No descriptor files/path specified, or the path does not contain service descriptors!\n");
        MESSAGE("Usage:\n\tremove <service-xml-file(s)|path-to-service-xmls>\n");
        return;
    }

    QString securityToken = QString::number(SEC_TOKEN);

    foreach (const QString &serviceXml, desList) {
        const QString &service = getServiceFromXML(serviceXml);
        if (service.isEmpty()) {
            MESSAGE("servicedbgen failed: empty service descriptor or wrong parameter given.\n");
            continue;
        }
        if (db->unregisterService(service, securityToken))
            MESSAGE("Service " << service << " unregistered\n")
        else
            MESSAGE("servicedbgen failed: Service " << service << " unregistration from " << db->databasePath() << " failed\n");
    }
}

#ifdef STATIC_BUILD
Q_IMPORT_PLUGIN(qsqlite)
#endif

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QStringList args = QCoreApplication::arguments();
    CommandProcessor processor;

    if (!processor.initialize())
        return 1;

    if (args.count() == 1 || args.value(1) == "--help" || args.value(1) == "-h") {
        processor.showUsage();
        return 0;
    }

    QStringList options;
    for (int i=1; i<args.count(); i++) {
        if (args[i].startsWith("-"))
            options += args[i];
    }

    processor.execute(options, args.value(options.count() + 1), args.mid(options.count() + 2));
    return 0;
}

#include "servicedbgen.moc"