BibleTime
btmoduleindexdialog.cpp
Go to the documentation of this file.
1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, https://bibletime.info/
6 *
7 * Copyright 1999-2021 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License
9 * version 2.0.
10 *
11 **********/
12 
13 #include "btmoduleindexdialog.h"
14 
15 #include <array>
16 #include <QApplication>
17 #include <utility>
18 #include "../backend/drivers/cswordmoduleinfo.h"
19 #include "../util/btassert.h"
20 #include "../util/btconnect.h"
21 #include "../util/btdebugonly.h"
22 #include "messagedialog.h"
23 
24 
26 {
27  if (modules.empty()) return true;
28 
29  BtModuleIndexDialog d(modules.size());
30  d.show();
31  d.raise();
32  return d.indexAllModulesPrivate(modules);
33 }
34 
36  : QProgressDialog(tr("Preparing to index modules..."), tr("Cancel"), 0,
37  numModules * 100, nullptr),
38  m_currentModuleIndex(0)
39 {
40  setWindowTitle(tr("Creating indices"));
41  setModal(true);
42 }
43 
45 {
46  bool success = true;
47 
48  QList <CSwordModuleInfo*> indexedModules;
49  for (auto * const m : modules) {
50  BT_ASSERT(!m->hasIndex());
51 
52  /*
53  Keep track of created indices to delete them on failure or
54  cancellation:
55  */
56  indexedModules.append(m);
57 
58  std::array<QMetaObject::Connection, 3u> connections{
59  BT_CONNECT(this, &BtModuleIndexDialog::canceled,
60  m /* needed */, [m]{ m->cancelIndexing(); }),
62  this, // needed
63  [this]{
64  setValue(m_currentModuleIndex * 100 + 100);
65  qApp->processEvents();
66  }),
68  this, // needed
69  [this](int percentage) {
70  setValue(m_currentModuleIndex * 100 + percentage);
71  qApp->processEvents();
72  })};
73 
74  // Single module indexing blocks until finished:
75  setLabelText(tr("Creating index for work: %1").arg(m->name()));
76 
77  try {
78  m->buildIndex();
79  } catch (...) {
80  QString msg;
81  try {
82  throw;
83  } catch (std::exception const & e) {
84  msg = e.what();
85  } catch (...) {
86  msg = tr("<UNKNOWN EXCEPTION>");
87  }
88 
90  tr("Indexing aborted"),
91  tr("An internal error occurred while building "
92  "the index:<br/><br/>%1").arg(msg));
93  success = false;
94  }
95 
97 
98  for (auto & connection : connections) {
99  BT_DEBUG_ONLY(auto const r =) disconnect(std::move(connection));
100  BT_ASSERT(r);
101  }
102 
103  if (wasCanceled()) success = false;
104 
105  if (!success) break;
106  }
107 
108  if (!success) {
109  // Delete already created indices:
110  for (auto * const m : indexedModules)
111  if (m->hasIndex())
112  m->deleteIndex();
113  }
114  return success;
115 }
#define BT_ASSERT(...)
Definition: btassert.h:17
#define BT_CONNECT(...)
Definition: btconnect.h:20
#define BT_DEBUG_ONLY(...)
Definition: btdebugonly.h:19
BtModuleIndexDialog(int numModules)
bool indexAllModulesPrivate(QList< CSwordModuleInfo * > const &modules)
static bool indexAllModules(QList< CSwordModuleInfo * > const &modules)
void indexingFinished()
void indexingProgress(int)
QStringList r(content.left(bodyIndex))
QMessageBox::StandardButton showWarning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)