BibleTime
btsourcesthread.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 "btsourcesthread.h"
14 
15 #include <cstddef>
16 #include <QDebug>
17 #include <QString>
18 #include <QStringList>
19 #include <memory>
20 #include <utility>
21 #include "../util/btassert.h"
22 #include "btinstallbackend.h"
23 #include "btinstallmgr.h"
24 
25 // Sword includes:
26 #include <installmgr.h>
27 
28 
30  Q_EMIT percentComplete(0);
31  Q_EMIT showMessage(tr("Getting Library List"));
32  if (BtInstallMgr().refreshRemoteSourceConfiguration())
33  qWarning("InstallMgr: getting remote list returned an error.");
34  Q_EMIT percentComplete(10);
35 
36  if (shouldStop()) {
37  Q_EMIT showMessage(tr("Updating stopped"));
38  return;
39  }
40 
41  QStringList const sourceNames = BtInstallBackend::sourceNameList();
42  auto const sourceCount = sourceNames.count();
43  BT_ASSERT(sourceCount >= 0);
44  auto const failedSources(std::make_unique<int[]>(sourceCount));
45  std::size_t numFailedSources = 0u;
46  BtInstallMgr iMgr;
47  for (int i = 0; i < sourceCount; ++i) {
48  if (shouldStop()) {
49  Q_EMIT showMessage(tr("Updating stopped"));
50  return;
51  }
52  QString const & sourceName = sourceNames[i];
53  Q_EMIT showMessage(
54  tr("Updating remote library \"%1\"").arg(sourceName));
55  {
56  sword::InstallSource source = BtInstallBackend::source(sourceName);
57  if (iMgr.refreshRemoteSource(&source)) {
58  failedSources[numFailedSources] = i;
59  ++numFailedSources;
60  }
61  }
62  Q_EMIT percentComplete(
63  static_cast<int>(10 + 90 * ((i + 1.0) / sourceCount)));
64  }
65  Q_EMIT percentComplete(100);
66  if (numFailedSources <= 0) {
67  Q_EMIT showMessage(tr("Remote libraries have been updated."));
68  m_finishedSuccessfully.store(true, std::memory_order_release);
69  } else {
70  QString msg = tr("The following remote libraries failed to update: ");
71  for (std::size_t i = 0;;) {
72  msg += sourceNames[failedSources[i]];
73  if (++i >= numFailedSources)
74  break;
75  static auto const separator = QStringLiteral(", ");
76  msg.append(separator);
77  }
78  Q_EMIT showMessage(std::move(msg));
79  m_finishedSuccessfully.store(true, std::memory_order_release);
80  }
81 }
#define BT_ASSERT(...)
Definition: btassert.h:17
std::atomic< bool > m_finishedSuccessfully
void percentComplete(int percent)
void run() override
void showMessage(QString const &msg)
bool shouldStop() const noexcept
sword::InstallSource source(const QString &name)
QStringList sourceNameList()