File: glossary.cpp

package info (click to toggle)
khelpcenter 4%3A25.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,216 kB
  • sloc: cpp: 6,279; xml: 378; python: 51; perl: 25; makefile: 5; sh: 3
file content (287 lines) | stat: -rw-r--r-- 8,744 bytes parent folder | download | duplicates (2)
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
/*
    SPDX-FileCopyrightText: 2002 Frerich Raabe <raabe@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "glossary.h"
#include "khc_debug.h"
#include "view.h"

#include <KLocalizedString>
#include <KProcess>
#include <KXmlGuiWindow>

#include <QApplication>
#include <QDir>
#include <QFileInfo>
#include <QIcon>
#include <QStandardPaths>
#include <QStatusBar>
#include <QTreeWidgetItem>

#include <sys/stat.h>

#include <prefs.h>

using namespace KHC;

class SectionItem : public QTreeWidgetItem
{
public:
    SectionItem(QTreeWidgetItem *parent, const QString &text)
        : QTreeWidgetItem(parent)
    {
        setText(0, text);
        setIcon(0, QIcon::fromTheme(QStringLiteral("help-contents")));
    }
};

class EntryItem : public QTreeWidgetItem
{
public:
    EntryItem(SectionItem *parent, const QString &term, const QString &id)
        : QTreeWidgetItem(parent)
        , m_id(id)
    {
        setText(0, term);
    }

    QString id() const
    {
        return m_id;
    }

private:
    QString m_id;
};

bool Glossary::m_alreadyWarned = false;

Glossary::Glossary(QWidget *parent)
    : QTreeWidget(parent)
{
    m_initialized = false;
    setFrameStyle(QFrame::NoFrame);

    connect(this, &QTreeWidget::itemClicked, this, &Glossary::treeItemSelected);

    setHeaderHidden(true);
    setAllColumnsShowFocus(true);
    setRootIsDecorated(true);

    m_byTopicItem = new QTreeWidgetItem(this);
    m_byTopicItem->setText(0, i18n("By Topic"));
    m_byTopicItem->setIcon(0, QIcon::fromTheme(QStringLiteral("help-contents")));

    m_alphabItem = new QTreeWidgetItem(this);
    m_alphabItem->setText(0, i18n("Alphabetically"));
    m_alphabItem->setIcon(0, QIcon::fromTheme(QStringLiteral("character-set")));

    m_cacheFile = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/') + QStringLiteral("help/glossary.xml");
    QDir().mkpath(QFileInfo(m_cacheFile).absolutePath());

    m_sourceFile = View::langLookup(QStringLiteral("khelpcenter/glossary/index.docbook"));
}

void Glossary::showEvent(QShowEvent *event)
{
    if (!m_initialized) {
        if (cacheStatus() == NeedRebuild) {
            rebuildGlossaryCache();
        } else {
            buildGlossaryTree();
        }
        m_initialized = true;
    }
    QTreeWidget::showEvent(event);
}

Glossary::~Glossary()
{
    qDeleteAll(m_glossEntries);
}

const GlossaryEntry &Glossary::entry(const QString &id) const
{
    return *m_glossEntries[id];
}

Glossary::CacheStatus Glossary::cacheStatus() const
{
    if (!QFile::exists(m_cacheFile) || Prefs::cachedGlossary() != m_sourceFile || Prefs::cachedGlossaryTimestamp() != glossaryCTime())
        return NeedRebuild;

    return CacheOk;
}

int Glossary::glossaryCTime() const
{
    struct stat stat_buf;
    stat(QFile::encodeName(m_sourceFile).data(), &stat_buf);

    return stat_buf.st_ctime;
}

void Glossary::rebuildGlossaryCache()
{
    KXmlGuiWindow *mainWindow = dynamic_cast<KXmlGuiWindow *>(qobject_cast<QApplication *>(qApp)->activeWindow());
    if (mainWindow)
        mainWindow->statusBar()->showMessage(i18n("Rebuilding glossary cache..."));

    KProcess *meinproc = new KProcess;
    connect(meinproc, QOverload<int, QProcess::ExitStatus>::of(&KProcess::finished), this, &Glossary::meinprocFinished);
    *meinproc << QStandardPaths::findExecutable(QStringLiteral("meinproc6"));
    *meinproc << QStringLiteral("--output") << m_cacheFile;
    *meinproc << QStringLiteral("--stylesheet") << QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("khelpcenter/glossary.xslt"));
    *meinproc << m_sourceFile;

    meinproc->setOutputChannelMode(KProcess::OnlyStderrChannel);
    meinproc->start();
    if (!meinproc->waitForStarted()) {
        qCWarning(KHC_LOG) << "could not start process" << meinproc->program();
        if (mainWindow && !m_alreadyWarned) {
            ; // add warning message box with don't display again option
            m_alreadyWarned = true;
        }
        delete meinproc;
    }
}

void Glossary::meinprocFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    KProcess *meinproc = static_cast<KProcess *>(sender());
    KXmlGuiWindow *mainWindow = dynamic_cast<KXmlGuiWindow *>(qobject_cast<QApplication *>(qApp)->activeWindow());

    if (exitStatus != QProcess::NormalExit || exitCode != 0) {
        qCWarning(KHC_LOG) << "running" << meinproc->program() << "failed with exitCode" << exitCode;
        qCWarning(KHC_LOG) << "stderr output:" << meinproc->readAllStandardError();
        if (mainWindow && !m_alreadyWarned) {
            ; // add warning message box with don't display again option
            m_alreadyWarned = true;
        }
        delete meinproc;
        return;
    }
    delete meinproc;

    if (!QFile::exists(m_cacheFile))
        return;

    Prefs::setCachedGlossary(m_sourceFile);
    Prefs::setCachedGlossaryTimestamp(glossaryCTime());
    Prefs::self()->save();

    m_status = CacheOk;

    if (mainWindow)
        mainWindow->statusBar()->showMessage(i18n("Rebuilding cache... done."), 2000);

    buildGlossaryTree();
}

void Glossary::buildGlossaryTree()
{
    QFile cacheFile(m_cacheFile);
    if (!cacheFile.open(QIODevice::ReadOnly))
        return;

    QDomDocument doc;
    if (!doc.setContent(&cacheFile))
        return;

    QHash<QChar, SectionItem *> alphabSections;

    QDomNodeList sectionNodes = doc.documentElement().elementsByTagName(QStringLiteral("section"));
    for (int i = 0; i < sectionNodes.count(); i++) {
        QDomElement sectionElement = sectionNodes.item(i).toElement();
        QString title = sectionElement.attribute(QStringLiteral("title"));
        SectionItem *topicSection = new SectionItem(m_byTopicItem, title);

        QDomNodeList entryNodes = sectionElement.elementsByTagName(QStringLiteral("entry"));
        for (int j = 0; j < entryNodes.count(); j++) {
            QDomElement entryElement = entryNodes.item(j).toElement();

            QString entryId = entryElement.attribute(QStringLiteral("id"));
            if (entryId.isNull())
                continue;

            QDomElement termElement = childElement(entryElement, QStringLiteral("term"));
            QString term = termElement.text().simplified();

            EntryItem *entry = new EntryItem(topicSection, term, entryId);
            m_idDict.insert(entryId, entry);

            const QChar first = term.at(0).toUpper();
            SectionItem *alphabSection = alphabSections.value(first);

            if (!alphabSection) {
                alphabSection = new SectionItem(m_alphabItem, QString(first));
                alphabSections.insert(first, alphabSection);
            }

            new EntryItem(alphabSection, term, entryId);

            QDomElement definitionElement = childElement(entryElement, QStringLiteral("definition"));
            QString definition = definitionElement.text().simplified();

            GlossaryEntryXRef::List seeAlso;

            QDomElement referencesElement = childElement(entryElement, QStringLiteral("references"));
            QDomNodeList referenceNodes = referencesElement.elementsByTagName(QStringLiteral("reference"));
            if (referenceNodes.count() > 0)
                for (int k = 0; k < referenceNodes.count(); k++) {
                    QDomElement referenceElement = referenceNodes.item(k).toElement();

                    QString term = referenceElement.attribute(QStringLiteral("term"));
                    QString id = referenceElement.attribute(QStringLiteral("id"));

                    seeAlso += GlossaryEntryXRef(term, id);
                }

            m_glossEntries.insert(entryId, new GlossaryEntry(entryId, term, definition, seeAlso));
        }
    }
    sortItems(0, Qt::AscendingOrder);
}

void Glossary::treeItemSelected(QTreeWidgetItem *item)
{
    if (!item)
        return;

    if (EntryItem *i = dynamic_cast<EntryItem *>(item))
        Q_EMIT entrySelected(entry(i->id()));

    item->setExpanded(!item->isExpanded());
}

QDomElement Glossary::childElement(const QDomElement &element, const QString &name)
{
    QDomElement e;
    for (e = element.firstChild().toElement(); !e.isNull(); e = e.nextSibling().toElement())
        if (e.tagName() == name)
            break;
    return e;
}

void Glossary::slotSelectGlossEntry(const QString &id)
{
    if (!m_idDict.contains(id))
        return;

    EntryItem *newItem = m_idDict.value(id);
    EntryItem *curItem = dynamic_cast<EntryItem *>(currentItem());
    if (curItem != nullptr) {
        if (curItem->id() == id) {
            return;
        }
        curItem->parent()->setExpanded(false);
    }

    setCurrentItem(newItem);
}

#include "moc_glossary.cpp"

// vim:ts=4:sw=4:et