summaryrefslogtreecommitdiff
path: root/src/frontend/btmoduleindexdialog.cpp
blob: 2dac4988aab970634f06ae6f6ca93e251c5292de (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
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2011 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#include "frontend/btmoduleindexdialog.h"

#include <QApplication>
#include <QMutexLocker>
#include "backend/managers/cswordbackend.h"


QMutex BtModuleIndexDialog::m_singleInstanceMutex;

bool BtModuleIndexDialog::indexAllModules(
        const QList<const CSwordModuleInfo*> &modules)
{
    QMutexLocker lock(&m_singleInstanceMutex);

    if (modules.empty()) return true;

    BtModuleIndexDialog d(modules.size());
    d.show();
    d.raise();
    return d.indexAllModules2(modules);
}

BtModuleIndexDialog::BtModuleIndexDialog(int numModules)
    : QProgressDialog(tr("Preparing to index modules..."), tr("Cancel"), 0,
                      numModules * 100, 0),
      m_currentModuleIndex(0)
{
    setWindowTitle(tr("Creating indices"));
    setModal(true);
}

bool BtModuleIndexDialog::indexAllModules2(
        const QList<const CSwordModuleInfo*> &modules)
{
    bool success = true;

    QList<CSwordModuleInfo *> indexedModules;
    Q_FOREACH(const CSwordModuleInfo *cm, modules) {
        Q_ASSERT(!cm->hasIndex());

        /// \warning const_cast
        CSwordModuleInfo *m = const_cast<CSwordModuleInfo*>(cm);

        /*
          Keep track of created indices to delete them on failure or
          cancellation:
        */
        indexedModules.append(m);

        connect(this, SIGNAL(canceled()),
                m,    SLOT(cancelIndexing()));
        connect(m,    SIGNAL(indexingFinished()),
                this, SLOT(slotFinished()));
        connect(m,    SIGNAL(indexingProgress(int)),
                this, SLOT(slotModuleProgress(int)));

        // Single module indexing blocks until finished:
        setLabelText(tr("Creating index for work: %1").arg(m->name()));

        if (!m->buildIndex()) success = false;

        m_currentModuleIndex++;

        disconnect(this, SIGNAL(canceled()),
                   m,    SLOT(cancelIndexing()));
        disconnect(m,    SIGNAL(indexingFinished()),
                   this, SLOT(slotFinished()));
        disconnect(m,    SIGNAL(indexingProgress(int)),
                   this, SLOT(slotModuleProgress(int)));

        if (wasCanceled()) success = false;

        if (!success) break;
    }

    if (!success) {
        // Delete already created indices:
        Q_FOREACH(CSwordModuleInfo *m, indexedModules) {
            if (m->hasIndex()) {
                m->deleteIndex();
            }
        }
    }
    return success;
}

void BtModuleIndexDialog::slotModuleProgress(int percentage) {
    setValue(m_currentModuleIndex * 100 + percentage);
    qApp->processEvents();
}

void BtModuleIndexDialog::slotFinished() {
    setValue(m_currentModuleIndex * 100 + 100);
    qApp->processEvents();
}