BibleTime
btbookshelfworkspage.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-2025 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
14
15#include <algorithm>
16#include <QAbstractItemView>
17#include <QApplication>
18#include <QByteArray>
19#include <QComboBox>
20#include <QDir>
21#include <QFileInfo>
22#include <QHBoxLayout>
23#include <QHeaderView>
24#include <QLabel>
25#include <QLineEdit>
26#include <QMenu>
27#include <QNonConstOverload>
28#include <QSet>
29#include <QSizePolicy>
30#include <QStringList>
31#include <QStringLiteral>
32#include <Qt>
33#include <QToolButton>
34#include <QVBoxLayout>
35#include <QWizard>
36#include <utility>
37#include "../../backend/btinstallbackend.h"
38#include "../../backend/bookshelfmodel/btbookshelfmodel.h"
39#include "../../backend/bookshelfmodel/btbookshelffiltermodel.h"
40#include "../../backend/bookshelfmodel/btbookshelftreemodel.h"
41#include "../../backend/config/btconfig.h"
42#include "../../backend/drivers/btmoduleset.h"
43#include "../../backend/drivers/cswordmoduleinfo.h"
44#include "../../backend/managers/cswordbackend.h"
45#include "../../util/btassert.h"
46#include "../../util/btconnect.h"
47#include "../btbookshelfgroupingmenu.h"
48#include "../btbookshelfview.h"
49#include "btbookshelfwizard.h"
50#include "btinstallpagemodel.h"
51
52// Sword includes:
53#pragma GCC diagnostic push
54#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
55#include <installmgr.h>
56#include <swbuf.h>
57#include <swversion.h>
58#pragma GCC diagnostic pop
59
60
61namespace {
62QString const groupingOrderKey("GUI/BookshelfWizard/InstallPage/grouping");
63QString const installPathKey(
64 "GUI/BookshelfWizard/InstallPage/installPathIndex");
65
66inline bool filter(WizardTaskType const taskType,
67 QStringList const & languages,
68 CSwordModuleInfo const * const mInfo)
69{
70 if (taskType == WizardTaskType::installWorks) {
72 && languages.contains(mInfo->language()->translatedName());
73 } else if (taskType == WizardTaskType::updateWorks) {
74 using CSMI = CSwordModuleInfo;
75 using CSV = sword::SWVersion const;
76 CSMI const * const installedModule =
78 return installedModule
79 && (CSV(installedModule->config(CSMI::ModuleVersion).toLatin1())
80 < CSV(mInfo->config(CSMI::ModuleVersion).toLatin1()));
81 } else {
84 }
85}
86
87} // anonymous namespace
88
90 QWidget * parent)
91 : BtBookshelfWizardPage(parent)
92 , m_taskType(iType)
93{
94 if (!m_groupingOrder.loadFrom(btConfig(), groupingOrderKey))
96
97 // Initialize menus:
100 m_contextMenu = new QMenu(this);
103
104 // Setup UI:
105 QVBoxLayout * const verticalLayout = new QVBoxLayout(this);
106
107 QHBoxLayout * horizontalLayout = new QHBoxLayout;
108 m_nameFilterLabel = new QLabel(this);
109 horizontalLayout->addWidget(m_nameFilterLabel);
110 m_nameFilterEdit = new QLineEdit(this);
112 horizontalLayout->addWidget(m_nameFilterEdit);
113 verticalLayout->addLayout(horizontalLayout);
114
116 m_bookshelfView->setHeaderHidden(false);
117 m_bookshelfView->header()->setSectionResizeMode(
118 QHeaderView::ResizeToContents);
119 verticalLayout->addWidget(m_bookshelfView);
120
121 m_msgLabel = new QLabel(this);
122 m_msgLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
123 m_msgLabel->setWordWrap(true);
124 verticalLayout->addWidget(m_msgLabel);
125
126 QHBoxLayout * const pathLayout = new QHBoxLayout();
128 m_pathCombo = new QComboBox(this);
129 m_pathCombo->setMinimumContentsLength(20);
130 m_pathCombo->setSizeAdjustPolicy(
131 QComboBox::AdjustToMinimumContentsLengthWithIcon);
132 m_pathCombo->setSizePolicy(QSizePolicy::Expanding,
133 QSizePolicy::Minimum);
134 m_pathCombo->view()->setTextElideMode(Qt::ElideMiddle);
135
136 m_pathLabel = new QLabel(this);
137 m_pathLabel->setBuddy(m_pathCombo);
139
140 pathLayout->setContentsMargins(0, 8, 0, 0);
141 pathLayout->addWidget(m_pathLabel);
142 pathLayout->addWidget(m_pathCombo);
143 }
144 pathLayout->addStretch();
145
146 m_groupingLabel = new QLabel(this);
147 pathLayout->addWidget(m_groupingLabel);
148
149 m_groupingButton = new QToolButton(this);
150 m_groupingButton->setPopupMode(QToolButton::InstantPopup);
152 m_groupingButton->setIcon(m_groupingMenu->icon());
153 m_groupingButton->setAutoRaise(true);
154 pathLayout->addWidget(m_groupingButton);
155
156 verticalLayout->addLayout(pathLayout);
157
158 // Setup models:
159 BtBookshelfFilterModel * const filterModel =
160 new BtBookshelfFilterModel(this);
161 m_bookshelfView->setModel(filterModel);
162
166 filterModel->setSourceModel(m_installPageModel);
167
168 /// \todo is this useless if m_taskType == WizardTaskType::removeWorks?
172 } else {
174 }
176 m_bookshelfView->setColumnHidden(1, true);
177
178 // Initialize connections:
181 this,
184 BT_CONNECT(m_pathCombo, qOverload<int>(&QComboBox::currentIndexChanged),
189 }
191 this, &BtBookshelfWorksPage::completeChanged);
194 BT_CONNECT(m_nameFilterEdit, &QLineEdit::textEdited,
196
198}
199
201 m_nameFilterLabel->setText(tr("Fi&lter:"));
202 if (m_taskType == installWorks) {
203 setTitle(QApplication::translate("BookshelfWizard", "Install Works"));
204 setSubTitle(QApplication::translate("BookshelfWizard",
205 "Choose one or more works to "
206 "install."));
207 setButtonText(QWizard::NextButton,tr("Install Works >"));
209 setTitle(QApplication::translate("BookshelfWizard", "Update Works"));
210 setSubTitle(QApplication::translate("BookshelfWizard",
211 "Choose one or more works to "
212 "update."));
213 setButtonText(QWizard::NextButton,tr("Update Works >"));
214 } else {
216 setTitle(QApplication::translate("BookshelfWizard", "Remove Works"));
217 setSubTitle(QApplication::translate("BookshelfWizard",
218 "Choose one or more works to "
219 "remove."));
220 setButtonText(QWizard::NextButton,tr("Remove Works >"));
221 }
222
224 m_pathLabel->setText(tr("Install &folder:"));
225 m_pathCombo->setToolTip(tr("The folder where the new works will be "
226 "installed"));
227 }
228
230 m_msgLabel->setText(tr("There are no works to update."));
232 m_msgLabel->setText(tr("No works are currently installed so they "
233 "cannot be removed."));
234 } else {
236 m_msgLabel->setText(
237 tr("No works can be installed with the current selection of "
238 "remote libraries and languages. Please go back and make a "
239 "different selection."));
240 }
241
242 m_groupingLabel->setText(tr("Grouping:"));
243 m_groupingButton->setText(tr("Grouping"));
244 m_groupingButton->setToolTip(
245 tr("Change the grouping of items in the bookshelf."));
246}
247
253
255 // Update models:
256 QStringList sources;
257 QStringList languages;
258 if (m_taskType == installWorks) {
259 sources = btWizard().selectedSources();
260 languages = btWizard().selectedLanguages();
261 } else {
263 }
264
265 {
266 QSet<QString> addedModuleNames;
267 m_bookshelfModel->clear();
268 m_usedBackends.clear();
269 for (auto const & sourceName : sources) {
270 sword::InstallSource const source =
271 BtInstallBackend::source(sourceName);
272 std::unique_ptr<CSwordBackend const> backend(
274 bool backendUsed = false;
275 for (auto * const module : backend->moduleList()) {
276 if (filter(m_taskType, languages, module)) {
277 QString const & moduleName = module->name();
278 if (addedModuleNames.contains(moduleName))
279 continue;
280 addedModuleNames.insert(moduleName);
281 m_bookshelfModel->addModule(module);
282 module->setProperty("installSourceName",
283 QString(source.caption.c_str()));
284 backendUsed = true;
285 }
286 }
287 if (backendUsed)
288 m_usedBackends.emplace_back(std::move(backend));
289 }
291 m_bookshelfView->expandAll();
292
293 bool const noWorks = (addedModuleNames.count() == 0);
294 m_msgLabel->setVisible(noWorks);
295 m_bookshelfView->setVisible(!noWorks);
296 m_groupingButton->setVisible(!noWorks);
297 m_groupingLabel->setVisible(!noWorks);
298 }
299
300 // Set grouping:
301 auto const & cat = BtBookshelfTreeModel::Grouping::CAT;
302 auto const & catLang = BtBookshelfTreeModel::Grouping::CAT_LANG;
303 if (languages.count() == 1) {
305 } else if (languages.count() > 1 && m_groupingOrder == cat) {
307 }
308}
309
311 BtBookshelfTreeModel::Grouping const & grouping)
312{
314 m_bookshelfView->setRootIsDecorated(!grouping.list().isEmpty());
315}
316
318{ return checkedModules().count() > 0; }
319
326
329
331{ return m_pathCombo->currentText(); }
332
334{ btConfig().setValue(installPathKey, index); }
335
336static bool installPathIsUsable(QString const & path) {
337 if (path.isEmpty())
338 return false;
339 QDir const dir(path);
340 if (!dir.exists() || !dir.isReadable())
341 return false;
342 return QFileInfo(dir.canonicalPath()).isWritable();
343}
344
346 m_pathCombo->clear();
347 bool haveUsableTargets = false;
348 for (auto const & target : CSwordBackend::instance().swordDirList()) {
349 if (installPathIsUsable(target)) {
350 m_pathCombo->addItem(QDir::toNativeSeparators(target));
351 haveUsableTargets = true;
352 }
353 }
354
355 m_pathCombo->setVisible(haveUsableTargets);
356 m_pathLabel->setVisible(haveUsableTargets);
357 if (haveUsableTargets) {
358 /* Choose the current value from config but check whether we have so
359 many items: */
360 int const cfgValue = btConfig().value<int>(installPathKey, 0);
361 int const lastIdx = m_pathCombo->count() - 1;
362 m_pathCombo->setCurrentIndex(cfgValue > lastIdx ? lastIdx : cfgValue);
363 }
364}
#define BT_ASSERT(...)
Definition btassert.h:17
@ removeFinalPage
@ installFinalPage
static bool installPathIsUsable(QString const &path)
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition btconfig.h:305
#define BT_CONNECT(...)
Definition btconnect.h:20
void setNameFilterFixedString(QString const &nameFilter)
void setGrouping(BtBookshelfTreeModel::Grouping const &grouping)
void signalGroupingOrderChanged(const BtBookshelfTreeModel::Grouping &)
static std::shared_ptr< BtBookshelfModel > newInstance()
auto const & list() const noexcept
void saveTo(BtConfigCore &config, QString const &key) const
bool loadFrom(BtConfigCore const &config, QString const &key)
void groupingOrderChanged(BtBookshelfTreeModel::Grouping newGrouping)
void setDefaultChecked(CheckedBehavior const b)
BtModuleSet const & checkedModules() const
void setGroupingOrder(BtBookshelfTreeModel::Grouping const &groupingOrder, bool emitSignal=true)
void setSourceModel(std::shared_ptr< QAbstractItemModel > sourceModel)
void moduleChecked(CSwordModuleInfo *module, bool checked)
BtBookshelfWizard & btWizard() const noexcept
QStringList selectedSources() const
QStringList selectedLanguages() const
bool isComplete() const final override
void initializePage() final override
std::shared_ptr< BtBookshelfModel > m_bookshelfModel
std::vector< std::unique_ptr< CSwordBackend const > > m_usedBackends
void slotGroupingActionTriggered(BtBookshelfTreeModel::Grouping const &grouping)
BtModuleSet const & checkedModules() const
int nextId() const final override
void slotGroupingOrderChanged(BtBookshelfTreeModel::Grouping const &g)
WizardTaskType const m_taskType
BtBookshelfTreeModel::Grouping m_groupingOrder
void slotPathChanged(int const index)
BtBookshelfGroupingMenu * m_groupingMenu
BtBookshelfWorksPage(WizardTaskType iType, QWidget *parent=nullptr)
BtBookshelfView * m_bookshelfView
BtInstallPageModel * m_installPageModel
T value(QString const &key, T const &defaultValue=T()) const
Returns the settings value for the given global key.
void setValue(QString const &key, T const &value)
Sets a value for a key.
QStringList swordDirList() const
Sword prefix list.
CSwordModuleInfo * findModuleByName(const QString &name) const
Searches for a module with the given name.
void sigSwordSetupChanged()
static CSwordBackend & instance() noexcept
QString config(const CSwordModuleInfo::ConfigEntry entry) const
QString const & name() const
std::shared_ptr< Language const > language() const
sword::InstallSource source(const QString &name)
QStringList sourceNameList()
std::unique_ptr< CSwordBackend > backend(sword::InstallSource const &is)
QString const groupingOrderKey("GUI/BookshelfWizard/InstallPage/grouping")
QString const installPathKey("GUI/BookshelfWizard/InstallPage/installPathIndex")
bool filter(WizardTaskType const taskType, QStringList const &languages, CSwordModuleInfo const *const mInfo)