summaryrefslogtreecommitdiff
path: root/src/frontend/cexportmanager.cpp
blob: eaa0894a23816f513da03fdfe4790f038065c2b5 (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
/*********
*
* 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/cexportmanager.h"

#include <QSharedPointer>
#include <QApplication>
#include <QClipboard>
#include <QFileDialog>
#include <QList>
#include <QProgressDialog>
#include <QTextStream>
#include "backend/drivers/cswordmoduleinfo.h"
#include "backend/keys/cswordkey.h"
#include "backend/keys/cswordversekey.h"
#include "backend/managers/referencemanager.h"
#include "backend/managers/cdisplaytemplatemgr.h"
#include "backend/rendering/centrydisplay.h"
#include "backend/rendering/chtmlexportrendering.h"
#include "backend/rendering/cplaintextexportrendering.h"
#include "frontend/cprinter.h"
#include "util/tool.h"

// Sword includes:
#include <swkey.h>
#include <listkey.h>


using namespace Rendering;
using namespace Printing;

CExportManager::CExportManager(const QString &caption,
                               const bool showProgress,
                               const QString &progressLabel,
                               const FilterOptions &filterOptions,
                               const DisplayOptions &displayOptions)
{
    m_caption = !caption.isEmpty() ? caption : QString::fromLatin1("BibleTime");
    m_progressLabel = progressLabel;
    m_filterOptions = filterOptions;
    m_displayOptions = displayOptions;
    m_showProgress = showProgress;
    m_progressDialog = 0;
}

bool CExportManager::saveKey(CSwordKey* key, const Format format, const bool addText) {
    if (!key) {
        return false;
    }
    if (!key->module()) {
        return false;
    }
    const QString filename = getSaveFileName(format);
    if (filename.isEmpty()) {
        return false;
    }

    FilterOptions filterOptions = m_filterOptions;
    filterOptions.footnotes = false;
    filterOptions.strongNumbers = false;
    filterOptions.morphTags = false;
    filterOptions.lemmas = false;
    filterOptions.scriptureReferences = false;
    filterOptions.textualVariants = false;

    CHTMLExportRendering::Settings settings(addText);
    QSharedPointer<CTextRendering> render (
        (format == HTML)
        ? new CHTMLExportRendering(settings, m_displayOptions, filterOptions)
        : new CPlainTextExportRendering(settings, m_displayOptions, filterOptions)
    );

    QString text;
    QString startKey;
    QString stopKey;

    QList<const CSwordModuleInfo*> modules;
    modules.append(key->module());

    CSwordVerseKey *vk = dynamic_cast<CSwordVerseKey*>(key);
    if (vk && vk->isBoundSet()) {
        text = render->renderKeyRange( QString::fromUtf8(vk->LowerBound()), QString::fromUtf8(vk->UpperBound()), modules );
    }
    else { //no range supported
        text = render->renderSingleKey(key->key(), modules);
    }

    if (!progressWasCancelled()) {
        util::tool::savePlainFile(filename, text, false, (format == HTML) ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale() );
        closeProgressDialog();
        return true;
    }
    return false;
}

bool CExportManager::saveKeyList(const sword::ListKey &l,
                                 const CSwordModuleInfo *module,
                                 Format format,
                                 bool addText)
{
    /// \warning This is a workaround for Sword constness
    sword::ListKey list = l;
    if (!list.Count())
        return false;

    const QString filename = getSaveFileName(format);
    if (filename.isEmpty()) {
        return false;
    }

    FilterOptions filterOptions = m_filterOptions;
    filterOptions.footnotes = false;
    filterOptions.strongNumbers = false;
    filterOptions.morphTags = false;
    filterOptions.lemmas = false;
    filterOptions.scriptureReferences = false;
    filterOptions.textualVariants = false;

    CHTMLExportRendering::Settings settings(addText);
    QSharedPointer<CTextRendering> render (
        (format == HTML)
        ? new CHTMLExportRendering(settings, m_displayOptions, filterOptions)
        : new CPlainTextExportRendering(settings, m_displayOptions, filterOptions)
    );

    CTextRendering::KeyTree tree;

    setProgressRange(list.Count());
    CTextRendering::KeyTreeItem::Settings itemSettings;
    itemSettings.highlight = false;

    list.setPosition(sword::TOP);
    while (!list.Error() && !progressWasCancelled()) {
        tree.append( new CTextRendering::KeyTreeItem(QString::fromLocal8Bit((const char*)list) , module, itemSettings) );
        incProgress();

        list.increment();
    }

    const QString text = render->renderKeyTree(tree);

    if (!progressWasCancelled()) {
        util::tool::savePlainFile(filename, text, false, (format == HTML) ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale() );
        closeProgressDialog();
        return true;
    }
    return false;
}

bool CExportManager::saveKeyList(const QList<CSwordKey*> &list,
                                 Format format,
                                 bool addText)
{
    if (!list.count())
        return false;

    const QString filename = getSaveFileName(format);
    if (filename.isEmpty()) {
        return false;
    }

    FilterOptions filterOptions = m_filterOptions;
    filterOptions.footnotes = false;
    filterOptions.strongNumbers = false;
    filterOptions.morphTags = false;
    filterOptions.lemmas = false;
    filterOptions.scriptureReferences = false;
    filterOptions.textualVariants = false;

    CHTMLExportRendering::Settings settings(addText);
    QSharedPointer<CTextRendering> render (
        (format == HTML)
        ? new CHTMLExportRendering(settings, m_displayOptions, filterOptions)
        : new CPlainTextExportRendering(settings, m_displayOptions, filterOptions)
    );

    CTextRendering::KeyTree tree;

    setProgressRange(list.count());
    CTextRendering::KeyTreeItem::Settings itemSettings;
    itemSettings.highlight = false;

    QListIterator<CSwordKey*> it(list);
    while (it.hasNext() && !progressWasCancelled()) {
        CSwordKey* k = it.next();
        tree.append( new CTextRendering::KeyTreeItem(k->key(), k->module(), itemSettings) );
        incProgress();
    };

    const QString text = render->renderKeyTree(tree);

    if (!progressWasCancelled()) {
        util::tool::savePlainFile(filename, text, false, (format == HTML) ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale() );
        closeProgressDialog();
        return true;
    }
    return false;
}

bool CExportManager::copyKey(CSwordKey* key, const Format format, const bool addText) {
    if (!key) {
        return false;
    }
    if (!key->module()) {
        return false;
    }

    FilterOptions filterOptions = m_filterOptions;
    filterOptions.footnotes = false;
    filterOptions.strongNumbers = false;
    filterOptions.morphTags = false;
    filterOptions.lemmas = false;
    filterOptions.scriptureReferences = false;
    filterOptions.textualVariants = false;

    CHTMLExportRendering::Settings settings(addText);
    QSharedPointer<CTextRendering> render (
        (format == HTML)
        ? new CHTMLExportRendering(settings, m_displayOptions, filterOptions)
        : new CPlainTextExportRendering(settings, m_displayOptions, filterOptions)
    );

    QString text;
    QString startKey;
    QString stopKey;

    QList<const CSwordModuleInfo*> modules;
    modules.append(key->module());

    CSwordVerseKey *vk = dynamic_cast<CSwordVerseKey*>(key);
    if (vk && vk->isBoundSet()) {
        text = render->renderKeyRange(
                   QString::fromUtf8(vk->LowerBound()),
                   QString::fromUtf8(vk->UpperBound()),
                   modules
               );
    }
    else { //no range supported
        text = render->renderSingleKey(key->key(), modules);
    }

    QApplication::clipboard()->setText(text);
    return true;
}

bool CExportManager::copyKeyList(const sword::ListKey &l,
                                 const CSwordModuleInfo *module,
                                 Format format,
                                 bool addText)
{
    sword::ListKey list = l;
    if (!list.Count())
        return false;

    FilterOptions filterOptions = m_filterOptions;
    filterOptions.footnotes = false;
    filterOptions.strongNumbers = false;
    filterOptions.morphTags = false;
    filterOptions.lemmas = false;
    filterOptions.scriptureReferences = false;
    filterOptions.textualVariants = false;

    CHTMLExportRendering::Settings settings(addText);
    QSharedPointer<CTextRendering> render (
        (format == HTML)
        ? new CHTMLExportRendering(settings, m_displayOptions, filterOptions)
        : new CPlainTextExportRendering(settings, m_displayOptions, filterOptions)
    );

    CTextRendering::KeyTree tree;
    CTextRendering::KeyTreeItem::Settings itemSettings;
    itemSettings.highlight = false;

    list.setPosition(sword::TOP);
    while (!list.Error() && !progressWasCancelled()) {
        tree.append( new CTextRendering::KeyTreeItem(QString::fromLocal8Bit((const char*)list) , module, itemSettings) );

        list.increment();
    }

    const QString text = render->renderKeyTree(tree);
    QApplication::clipboard()->setText(text);
    return true;
}


bool CExportManager::copyKeyList(const QList<CSwordKey*> &list,
                                 Format format,
                                 bool addText)
{
    if (!list.count())
        return false;

    FilterOptions filterOptions = m_filterOptions;
    filterOptions.footnotes = false;
    filterOptions.strongNumbers = false;
    filterOptions.morphTags = false;
    filterOptions.lemmas = false;
    filterOptions.scriptureReferences = false;
    filterOptions.textualVariants = false;

    CHTMLExportRendering::Settings settings(addText);
    QSharedPointer<CTextRendering> render (
        (format == HTML)
        ? new CHTMLExportRendering(settings, m_displayOptions, filterOptions)
        : new CPlainTextExportRendering(settings, m_displayOptions, filterOptions)
    );

    CTextRendering::KeyTree tree;

    CTextRendering::KeyTreeItem::Settings itemSettings;
    itemSettings.highlight = false;

    QListIterator<CSwordKey*> it(list);
    while (it.hasNext() && !progressWasCancelled()) {
        CSwordKey* k = it.next();
        tree.append( new CTextRendering::KeyTreeItem(k->key(), k->module(), itemSettings) );
        incProgress();
    };

    const QString text = render->renderKeyTree(tree);
    QApplication::clipboard()->setText(text);
    if (!progressWasCancelled()) {
        closeProgressDialog();
    }
    return true;
}

bool CExportManager::printKeyList(const sword::ListKey &l,
                                  const CSwordModuleInfo *module,
                                  const DisplayOptions &displayOptions,
                                  const FilterOptions &filterOptions)
{
    /// \warning This is a workaround for Sword constness
    sword::ListKey list = l;
    CPrinter::KeyTreeItem::Settings settings;
    CPrinter::KeyTree tree;

    QString startKey, stopKey;
    setProgressRange(list.Count());

    list.setPosition(sword::TOP);
    while (!list.Error() && !progressWasCancelled()) {
        if (dynamic_cast<const sword::VerseKey&>(l) != 0) {
            const sword::VerseKey &vk = static_cast<const sword::VerseKey&>(l);
            startKey = QString::fromUtf8((const char*) vk.LowerBound());
            stopKey = QString::fromUtf8((const char*) vk.UpperBound());
            tree.append(new CTextRendering::KeyTreeItem(startKey,
                                                        stopKey,
                                                        module,
                                                        settings));
        }
        else {
            startKey = QString::fromUtf8((const char*) * list);
            tree.append(new CTextRendering::KeyTreeItem(startKey,
                                                        module,
                                                        settings));
        }

        list.increment();
        incProgress();
    }

    QSharedPointer<CPrinter> printer(new CPrinter(0, displayOptions, filterOptions));

    if (!progressWasCancelled()) {
        printer->printKeyTree(tree);
        closeProgressDialog();
        return true;
    }

    return false;
}

bool CExportManager::printKey(const CSwordModuleInfo *module,
                              const QString &startKey,
                              const QString &stopKey,
                              const DisplayOptions &displayOptions,
                              const FilterOptions &filterOptions)
{
    CPrinter::KeyTreeItem::Settings settings;
    settings.keyRenderingFace =
        displayOptions.verseNumbers
        ? CPrinter::KeyTreeItem::Settings::SimpleKey
        : CPrinter::KeyTreeItem::Settings::NoKey;

    CPrinter::KeyTree tree;
    if (startKey != stopKey) {
        tree.append( new CPrinter::KeyTreeItem(startKey, stopKey, module, settings) );
    }
    else {
        tree.append( new CPrinter::KeyTreeItem(startKey, module, settings) );
    }

    QSharedPointer<CPrinter> printer(new CPrinter(0, displayOptions, filterOptions));
    printer->printKeyTree(tree);
    return true;
}

bool CExportManager::printKey(const CSwordKey *key,
                              const DisplayOptions &displayOptions,
                              const FilterOptions &filterOptions)
{
    CPrinter::KeyTreeItem::Settings settings;
    settings.keyRenderingFace =
        displayOptions.verseNumbers
        ? CPrinter::KeyTreeItem::Settings::SimpleKey
        : CPrinter::KeyTreeItem::Settings::NoKey;

    CPrinter::KeyTree tree;
    tree.append( new CPrinter::KeyTreeItem(key->key(), key->module(), settings) );

    QSharedPointer<CPrinter> printer(new CPrinter(0, displayOptions, filterOptions));
    printer->printKeyTree(tree);
    return true;
}

bool CExportManager::printByHyperlink(const QString &hyperlink,
                                      const DisplayOptions &displayOptions,
                                      const FilterOptions &filterOptions)
{
    QString moduleName;
    QString keyName;
    ReferenceManager::Type type;

    ReferenceManager::decodeHyperlink(hyperlink, moduleName, keyName, type);
    if (moduleName.isEmpty()) {
        moduleName = ReferenceManager::preferredModule(type);
    }

    CPrinter::KeyTree tree;
    CPrinter::KeyTreeItem::Settings settings;
    settings.keyRenderingFace =
        displayOptions.verseNumbers
        ? CPrinter::KeyTreeItem::Settings::SimpleKey
        : CPrinter::KeyTreeItem::Settings::NoKey;

    CSwordModuleInfo* module = CSwordBackend::instance()->findModuleByName(moduleName);
    Q_ASSERT(module);

    if (module) {
        //check if we have a range of entries or a single one
        if ((module->type() == CSwordModuleInfo::Bible) || (module->type() == CSwordModuleInfo::Commentary)) {
            sword::ListKey verses = sword::VerseKey().ParseVerseList((const char*)keyName.toUtf8(), "Genesis 1:1", true);

            for (int i = 0; i < verses.Count(); ++i) {
                sword::VerseKey* element = dynamic_cast<sword::VerseKey*>(verses.GetElement(i));
                if (element) {
                    const QString startKey = QString::fromUtf8(element->LowerBound().getText());
                    const QString stopKey =  QString::fromUtf8(element->UpperBound().getText());

                    tree.append( new CPrinter::KeyTreeItem(startKey, stopKey, module, settings) );
                }
                else if (verses.GetElement(i)) {
                    const QString key =  QString::fromUtf8(verses.GetElement(i)->getText());

                    tree.append( new CPrinter::KeyTreeItem(key, module, settings) );
                }
            }
        }
        else {
            tree.append( new CPrinter::KeyTreeItem(keyName, module, settings) );
        }
    }

    QSharedPointer<CPrinter> printer(new CPrinter(0, displayOptions, filterOptions));
    printer->printKeyTree(tree);
    return true;
}

bool CExportManager::printKeyList(const QStringList &list,
                                  const CSwordModuleInfo *module,
                                  const DisplayOptions &displayOptions,
                                  const FilterOptions &filterOptions)
{
    CPrinter::KeyTreeItem::Settings settings;
    settings.keyRenderingFace =
        displayOptions.verseNumbers
        ? CPrinter::KeyTreeItem::Settings::SimpleKey
        : CPrinter::KeyTreeItem::Settings::NoKey;

    CPrinter::KeyTree tree;
    setProgressRange(list.count());

    const QStringList::const_iterator end = list.constEnd();
    for (QStringList::const_iterator it = list.constBegin(); (it != end) && !progressWasCancelled(); ++it) {
        tree.append( new CPrinter::KeyTreeItem(*it, module, settings) );
        incProgress();
    }

    QSharedPointer<CPrinter> printer(new CPrinter(0, displayOptions, filterOptions));

    if (!progressWasCancelled()) {
        printer->printKeyTree(tree);
        closeProgressDialog();
        return true;
    }

    return false;
}

/** Returns the string for the filedialogs to show the correct files. */
const QString CExportManager::filterString( const Format format ) {
    switch (format) {
        case HTML:
            return QObject::tr("HTML files") + QString(" (*.html *.htm);;") + QObject::tr("All files") + QString(" (*.*)");
        case Text:
            return QObject::tr("Text files") + QString(" (*.txt);;") + QObject::tr("All files") + QString(" (*.*)");
        default:
            return QObject::tr("All files") + QString(" (*.*)");
    }
}

/** Returns a filename to save a file. */
const QString CExportManager::getSaveFileName(const Format format) {
    return QFileDialog::getSaveFileName(0, QObject::tr("Save file"), "", filterString(format), 0);
}

/** No descriptions */
void CExportManager::setProgressRange( const int items ) {
    if (QProgressDialog* dlg = progressDialog()) {
        dlg->setMaximum(items);
        dlg->setValue(0);
        dlg->setMinimumDuration(0);
        dlg->show();
        //     dlg->repaint();
        qApp->processEvents(); //do not lock the GUI!
    }
}

/** Creates the progress dialog with the correct settings. */
QProgressDialog* CExportManager::progressDialog() {
    if (!m_showProgress) {
        return 0;
    }

    if (!m_progressDialog) {
        m_progressDialog = new QProgressDialog(0, Qt::Dialog );
        m_progressDialog->setLabelText(m_progressLabel);

        m_progressDialog->setWindowTitle("BibleTime");
    }

    return m_progressDialog;
}

/** Increments the progress by one item. */
void CExportManager::incProgress() {
    if (QProgressDialog* dlg = progressDialog()) {
        dlg->setValue( dlg->value() + 1 );
    }
}

/** No descriptions */
bool CExportManager::progressWasCancelled() {
    if (QProgressDialog* dlg = progressDialog()) {
        return dlg->wasCanceled();
    }

    return true;
}

/** Closes the progress dialog immediatly. */
void CExportManager::closeProgressDialog() {
    if (QProgressDialog* dlg = progressDialog()) {
        dlg->close();
        dlg->reset();
    }

    qApp->processEvents(); //do not lock the GUI!
}