summaryrefslogtreecommitdiffstats
path: root/tools/assistant/lib/qhelpcollectionhandler.cpp
blob: d7c1b0c4a0deba45961f6dc7396d165a14bd5ea7 (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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Assistant of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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, Digia gives you certain additional
** rights.  These rights are described in the Digia 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qhelpcollectionhandler_p.h"
#include "qhelp_global.h"
#include "qhelpdbreader_p.h"

#include <QtCore/QFile>
#include <QtCore/QDir>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>

#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver>

QT_BEGIN_NAMESPACE

QHelpCollectionHandler::QHelpCollectionHandler(const QString &collectionFile, QObject *parent)
    : QObject(parent)
    , m_dbOpened(false)
    , m_collectionFile(collectionFile)
    , m_connectionName(QString())
{
    QFileInfo fi(m_collectionFile);
    if (!fi.isAbsolute())
        m_collectionFile = fi.absoluteFilePath();
    m_query.clear();
}

QHelpCollectionHandler::~QHelpCollectionHandler()
{
    m_query.clear();
    if (m_dbOpened)
        QSqlDatabase::removeDatabase(m_connectionName);
}

bool QHelpCollectionHandler::isDBOpened()
{
    if (m_dbOpened)
        return true;
    emit error(tr("The collection file '%1' is not set up yet!").
               arg(m_collectionFile));
    return false;
}

QString QHelpCollectionHandler::collectionFile() const
{
    return m_collectionFile;
}

bool QHelpCollectionHandler::openCollectionFile()
{
    if (m_dbOpened)
        return m_dbOpened;

    m_connectionName = QHelpGlobal::uniquifyConnectionName(
        QLatin1String("QHelpCollectionHandler"), this);
    bool openingOk = true;
    {
        QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"),
            m_connectionName);
        if (db.driver()
            && db.driver()->lastError().type() == QSqlError::ConnectionError) {
            emit error(tr("Cannot load sqlite database driver!"));
            return false;
        }

        db.setDatabaseName(collectionFile());
        openingOk = db.open();
        if (openingOk)
            m_query = QSqlQuery(db);
    }
    if (!openingOk) {
        QSqlDatabase::removeDatabase(m_connectionName);
        emit error(tr("Cannot open collection file: %1").arg(collectionFile()));
        return false;
    }

    m_query.exec(QLatin1String("PRAGMA synchronous=OFF"));
    m_query.exec(QLatin1String("PRAGMA cache_size=3000"));

    m_query.exec(QLatin1String("SELECT COUNT(*) FROM sqlite_master WHERE TYPE=\'table\'"
                               "AND Name=\'NamespaceTable\'"));
    m_query.next();
    if (m_query.value(0).toInt() < 1) {
        if (!createTables(&m_query)) {
            emit error(tr("Cannot create tables in file %1!").arg(collectionFile()));
            return false;
        }
    }

    m_dbOpened = true;
    return m_dbOpened;
}

bool QHelpCollectionHandler::copyCollectionFile(const QString &fileName)
{
    if (!m_dbOpened)
        return false;

    QFileInfo fi(fileName);
    if (fi.exists()) {
        emit error(tr("The collection file '%1' already exists!").
                   arg(fileName));
        return false;
    }

    if (!fi.absoluteDir().exists() && !QDir().mkpath(fi.absolutePath())) {
        emit error(tr("Cannot create directory: %1").arg(fi.absolutePath()));
        return false;
    }

    QString colFile = fi.absoluteFilePath();
    QString connectionName = QHelpGlobal::uniquifyConnectionName(
        QLatin1String("QHelpCollectionHandlerCopy"), this);
    QSqlQuery *copyQuery = 0;
    bool openingOk = true;
    {
        QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), connectionName);
        db.setDatabaseName(colFile);
        openingOk = db.open();
        if (openingOk)
            copyQuery = new QSqlQuery(db);
    }

    if (!openingOk) {
        emit error(tr("Cannot open collection file: %1").arg(colFile));
        return false;
    }

    copyQuery->exec(QLatin1String("PRAGMA synchronous=OFF"));
    copyQuery->exec(QLatin1String("PRAGMA cache_size=3000"));

    if (!createTables(copyQuery)) {
        emit error(tr("Cannot copy collection file: %1").arg(colFile));
        return false;
    }

    QString oldBaseDir = QFileInfo(collectionFile()).absolutePath();
    QString oldFilePath;
    QFileInfo newColFi(colFile);
    m_query.exec(QLatin1String("SELECT Name, FilePath FROM NamespaceTable"));
    while (m_query.next()) {
        copyQuery->prepare(QLatin1String("INSERT INTO NamespaceTable VALUES(NULL, ?, ?)"));
        copyQuery->bindValue(0, m_query.value(0).toString());
        oldFilePath = m_query.value(1).toString();
        if (!QDir::isAbsolutePath(oldFilePath))
            oldFilePath = oldBaseDir + QDir::separator() + oldFilePath;
        copyQuery->bindValue(1, newColFi.absoluteDir().relativeFilePath(oldFilePath));
        copyQuery->exec();
    }

    m_query.exec(QLatin1String("SELECT NamespaceId, Name FROM FolderTable"));
    while (m_query.next()) {
        copyQuery->prepare(QLatin1String("INSERT INTO FolderTable VALUES(NULL, ?, ?)"));
        copyQuery->bindValue(0, m_query.value(0).toString());
        copyQuery->bindValue(1, m_query.value(1).toString());
        copyQuery->exec();
    }

    m_query.exec(QLatin1String("SELECT Name FROM FilterAttributeTable"));
    while (m_query.next()) {
        copyQuery->prepare(QLatin1String("INSERT INTO FilterAttributeTable VALUES(NULL, ?)"));
        copyQuery->bindValue(0, m_query.value(0).toString());
        copyQuery->exec();
    }

    m_query.exec(QLatin1String("SELECT Name FROM FilterNameTable"));
    while (m_query.next()) {
        copyQuery->prepare(QLatin1String("INSERT INTO FilterNameTable VALUES(NULL, ?)"));
        copyQuery->bindValue(0, m_query.value(0).toString());
        copyQuery->exec();
    }

    m_query.exec(QLatin1String("SELECT NameId, FilterAttributeId FROM FilterTable"));
    while (m_query.next()) {
        copyQuery->prepare(QLatin1String("INSERT INTO FilterTable VALUES(?, ?)"));
        copyQuery->bindValue(0, m_query.value(0).toInt());
        copyQuery->bindValue(1, m_query.value(1).toInt());
        copyQuery->exec();
    }

    m_query.exec(QLatin1String("SELECT Key, Value FROM SettingsTable"));
    while (m_query.next()) {
        if (m_query.value(0).toString() == QLatin1String("CluceneSearchNamespaces"))
            continue;
        copyQuery->prepare(QLatin1String("INSERT INTO SettingsTable VALUES(?, ?)"));
        copyQuery->bindValue(0, m_query.value(0).toString());
        copyQuery->bindValue(1, m_query.value(1));
        copyQuery->exec();
    }

    copyQuery->clear();
    delete copyQuery;
    QSqlDatabase::removeDatabase(connectionName);
    return true;
}

bool QHelpCollectionHandler::createTables(QSqlQuery *query)
{
    QStringList tables;
    tables << QLatin1String("CREATE TABLE NamespaceTable ("
        "Id INTEGER PRIMARY KEY, "
        "Name TEXT, "
        "FilePath TEXT )")
        << QLatin1String("CREATE TABLE FolderTable ("
        "Id INTEGER PRIMARY KEY, "
        "NamespaceId INTEGER, "
        "Name TEXT )")
        << QLatin1String("CREATE TABLE FilterAttributeTable ("
        "Id INTEGER PRIMARY KEY, "
        "Name TEXT )")
        << QLatin1String("CREATE TABLE FilterNameTable ("
        "Id INTEGER PRIMARY KEY, "
        "Name TEXT )")
        << QLatin1String("CREATE TABLE FilterTable ("
        "NameId INTEGER, "
        "FilterAttributeId INTEGER )")
        << QLatin1String("CREATE TABLE SettingsTable ("
        "Key TEXT PRIMARY KEY, "
        "Value BLOB )");

    foreach (const QString &q, tables) {
        if (!query->exec(q))
            return false;
    }
    return true;
}

QStringList QHelpCollectionHandler::customFilters() const
{
    QStringList list;
    if (m_dbOpened) {
        m_query.exec(QLatin1String("SELECT Name FROM FilterNameTable"));
        while (m_query.next())
            list.append(m_query.value(0).toString());
    }
    return list;
}

bool QHelpCollectionHandler::removeCustomFilter(const QString &filterName)
{
    if (!isDBOpened() || filterName.isEmpty())
        return false;

    int filterNameId = -1;
    m_query.prepare(QLatin1String("SELECT Id FROM FilterNameTable WHERE Name=?"));
    m_query.bindValue(0, filterName);
    m_query.exec();
    if (m_query.next())
        filterNameId = m_query.value(0).toInt();

    if (filterNameId < 0) {
        emit error(tr("Unknown filter '%1'!").arg(filterName));
        return false;
    }

    m_query.prepare(QLatin1String("DELETE FROM FilterTable WHERE NameId=?"));
    m_query.bindValue(0, filterNameId);
    m_query.exec();

    m_query.prepare(QLatin1String("DELETE FROM FilterNameTable WHERE Id=?"));
    m_query.bindValue(0, filterNameId);
    m_query.exec();

    return true;
}

bool QHelpCollectionHandler::addCustomFilter(const QString &filterName,
                                             const QStringList &attributes)
{
    if (!isDBOpened() || filterName.isEmpty())
        return false;

    int nameId = -1;
    m_query.prepare(QLatin1String("SELECT Id FROM FilterNameTable WHERE Name=?"));
    m_query.bindValue(0, filterName);
    m_query.exec();
    if (m_query.next())
        nameId = m_query.value(0).toInt();

    m_query.exec(QLatin1String("SELECT Id, Name FROM FilterAttributeTable"));
    QStringList idsToInsert = attributes;
    QMap<QString, int> attributeMap;
    while (m_query.next()) {
        attributeMap.insert(m_query.value(1).toString(),
            m_query.value(0).toInt());
        if (idsToInsert.contains(m_query.value(1).toString()))
            idsToInsert.removeAll(m_query.value(1).toString());
    }

    foreach (const QString &id, idsToInsert) {
        m_query.prepare(QLatin1String("INSERT INTO FilterAttributeTable VALUES(NULL, ?)"));
        m_query.bindValue(0, id);
        m_query.exec();
        attributeMap.insert(id, m_query.lastInsertId().toInt());
    }

    if (nameId < 0) {
        m_query.prepare(QLatin1String("INSERT INTO FilterNameTable VALUES(NULL, ?)"));
        m_query.bindValue(0, filterName);
        if (m_query.exec())
            nameId = m_query.lastInsertId().toInt();
    }

    if (nameId < 0) {
        emit error(tr("Cannot register filter %1!").arg(filterName));
        return false;
    }

    m_query.prepare(QLatin1String("DELETE FROM FilterTable WHERE NameId=?"));
    m_query.bindValue(0, nameId);
    m_query.exec();

    foreach (const QString &att, attributes) {
        m_query.prepare(QLatin1String("INSERT INTO FilterTable VALUES(?, ?)"));
        m_query.bindValue(0, nameId);
        m_query.bindValue(1, attributeMap[att]);
        if (!m_query.exec())
            return false;
    }
    return true;
}

QHelpCollectionHandler::DocInfoList QHelpCollectionHandler::registeredDocumentations() const
{
    DocInfoList list;
    if (m_dbOpened) {
        m_query.exec(QLatin1String("SELECT a.Name, a.FilePath, b.Name "
            "FROM NamespaceTable a, FolderTable b WHERE a.Id=b.NamespaceId"));

        while (m_query.next()) {
            DocInfo info;
            info.fileName = m_query.value(1).toString();
            info.folderName = m_query.value(2).toString();
            info.namespaceName = m_query.value(0).toString();
            list.append(info);
        }
    }
    return list;
}

bool QHelpCollectionHandler::registerDocumentation(const QString &fileName)
{
    if (!isDBOpened())
        return false;

    QHelpDBReader reader(fileName, QHelpGlobal::uniquifyConnectionName(
        QLatin1String("QHelpCollectionHandler"), this), 0);
    if (!reader.init()) {
        emit error(tr("Cannot open documentation file %1!").arg(fileName));
        return false;
    }

    QString ns = reader.namespaceName();
    if (ns.isEmpty()) {
        emit error(tr("Invalid documentation file '%1'!").arg(fileName));
        return false;
    }

    int nsId = registerNamespace(ns, fileName);
    if (nsId < 1)
        return false;

    if (!registerVirtualFolder(reader.virtualFolder(), nsId))
        return false;

    addFilterAttributes(reader.filterAttributes());
    foreach (const QString &filterName, reader.customFilters())
        addCustomFilter(filterName, reader.filterAttributes(filterName));

    optimizeDatabase(fileName);

    return true;
}

bool QHelpCollectionHandler::unregisterDocumentation(const QString &namespaceName)
{
    if (!isDBOpened())
        return false;

    m_query.prepare(QLatin1String("SELECT Id FROM NamespaceTable WHERE Name=?"));
    m_query.bindValue(0, namespaceName);
    m_query.exec();

    int nsId = -1;
    if (m_query.next())
        nsId = m_query.value(0).toInt();

    if (nsId < 0) {
        emit error(tr("The namespace %1 was not registered!").arg(namespaceName));
        return false;
    }

    m_query.prepare(QLatin1String("DELETE FROM NamespaceTable WHERE Id=?"));
    m_query.bindValue(0, nsId);
    m_query.exec();

    m_query.prepare(QLatin1String("DELETE FROM FolderTable WHERE NamespaceId=?"));
    m_query.bindValue(0, nsId);
    return m_query.exec();
}

bool QHelpCollectionHandler::removeCustomValue(const QString &key)
{
    if (!isDBOpened())
        return false;

    m_query.prepare(QLatin1String("DELETE FROM SettingsTable WHERE Key=?"));
    m_query.bindValue(0, key);
    return m_query.exec();
}

QVariant QHelpCollectionHandler::customValue(const QString &key,
                                             const QVariant &defaultValue) const
{
    QVariant value = defaultValue;
    if (m_dbOpened) {
        m_query.prepare(QLatin1String("SELECT COUNT(Key) FROM SettingsTable WHERE Key=?"));
        m_query.bindValue(0, key);
        if (!m_query.exec() || !m_query.next() || !m_query.value(0).toInt()) {
            m_query.clear();
            return defaultValue;
        }

        m_query.clear();
        m_query.prepare(QLatin1String("SELECT Value FROM SettingsTable WHERE Key=?"));
        m_query.bindValue(0, key);
        if (m_query.exec() && m_query.next())
            value = m_query.value(0);
        m_query.clear();
    }
    return value;
}

bool QHelpCollectionHandler::setCustomValue(const QString &key,
                                            const QVariant &value)
{
    if (!isDBOpened())
        return false;

    m_query.prepare(QLatin1String("SELECT Value FROM SettingsTable WHERE Key=?"));
    m_query.bindValue(0, key);
    m_query.exec();
    if (m_query.next()) {
        m_query.prepare(QLatin1String("UPDATE SettingsTable SET Value=? where Key=?"));
        m_query.bindValue(0, value);
        m_query.bindValue(1, key);
    }
    else {
        m_query.prepare(QLatin1String("INSERT INTO SettingsTable VALUES(?, ?)"));
        m_query.bindValue(0, key);
        m_query.bindValue(1, value);
    }
    return m_query.exec();
}

bool QHelpCollectionHandler::addFilterAttributes(const QStringList &attributes)
{
    if (!isDBOpened())
        return false;

    m_query.exec(QLatin1String("SELECT Name FROM FilterAttributeTable"));
    QSet<QString> atts;
    while (m_query.next())
        atts.insert(m_query.value(0).toString());

    foreach (const QString &s, attributes) {
        if (!atts.contains(s)) {
            m_query.prepare(QLatin1String("INSERT INTO FilterAttributeTable VALUES(NULL, ?)"));
            m_query.bindValue(0, s);
            m_query.exec();
        }
    }
    return true;
}

QStringList QHelpCollectionHandler::filterAttributes() const
{
    QStringList list;
    if (m_dbOpened) {
        m_query.exec(QLatin1String("SELECT Name FROM FilterAttributeTable"));
        while (m_query.next())
            list.append(m_query.value(0).toString());
    }
    return list;
}

QStringList QHelpCollectionHandler::filterAttributes(const QString &filterName) const
{
    QStringList list;
    if (m_dbOpened) {
        m_query.prepare(QLatin1String("SELECT a.Name FROM FilterAttributeTable a, "
            "FilterTable b, FilterNameTable c WHERE a.Id=b.FilterAttributeId "
            "AND b.NameId=c.Id AND c.Name=?"));
        m_query.bindValue(0, filterName);
        m_query.exec();
        while (m_query.next())
            list.append(m_query.value(0).toString());
    }
    return list;
}

int QHelpCollectionHandler::registerNamespace(const QString &nspace, const QString &fileName)
{
    m_query.prepare(QLatin1String("SELECT COUNT(Id) FROM NamespaceTable WHERE Name=?"));
    m_query.bindValue(0, nspace);
    m_query.exec();
    while (m_query.next()) {
        if (m_query.value(0).toInt() > 0) {
            emit error(tr("Namespace %1 already exists!").arg(nspace));
            return -1;
        }
    }

    QFileInfo fi(m_collectionFile);
    m_query.prepare(QLatin1String("INSERT INTO NamespaceTable VALUES(NULL, ?, ?)"));
    m_query.bindValue(0, nspace);
    m_query.bindValue(1, fi.absoluteDir().relativeFilePath(fileName));
    int namespaceId = -1;
    if (m_query.exec())
        namespaceId = m_query.lastInsertId().toInt();
    if (namespaceId < 1) {
        emit error(tr("Cannot register namespace '%1'!").arg(nspace));
        return -1;
    }
    return namespaceId;
}

bool QHelpCollectionHandler::registerVirtualFolder(const QString &folderName, int namespaceId)
{
    m_query.prepare(QLatin1String("INSERT INTO FolderTable VALUES(NULL, ?, ?)"));
    m_query.bindValue(0, namespaceId);
    m_query.bindValue(1, folderName);
    return m_query.exec();
}

void QHelpCollectionHandler::optimizeDatabase(const QString &fileName)
{
    if (!QFile::exists(fileName))
        return;

    {   // according to removeDatabase() documentation
        QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), QLatin1String("optimize"));
        db.setDatabaseName(fileName);
        if (!db.open()) {
            QSqlDatabase::removeDatabase(QLatin1String("optimize"));
            emit error(tr("Cannot open database '%1' to optimize!").arg(fileName));
            return;
        }

        QSqlQuery query(db);
        db.exec(QLatin1String("PRAGMA synchronous=OFF"));
        db.exec(QLatin1String("PRAGMA cache_size=3000"));
        db.exec(QLatin1String("CREATE INDEX IF NOT EXISTS NameIndex ON IndexTable(Name)"));
        db.exec(QLatin1String("CREATE INDEX IF NOT EXISTS FileNameIndex ON FileNameTable(Name)"));
        db.exec(QLatin1String("CREATE INDEX IF NOT EXISTS FileIdIndex ON FileNameTable(FileId)"));

        db.close();
    }

    QSqlDatabase::removeDatabase(QLatin1String("optimize"));
}

QT_END_NAMESPACE