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/language.h"
45#include "../../backend/managers/cswordbackend.h"
46#include "../../util/btassert.h"
47#include "../../util/btconnect.h"
48#include "../btbookshelfgroupingmenu.h"
49#include "../btbookshelfview.h"
50#include "btbookshelfwizard.h"
51#include "btinstallpagemodel.h"
52
53// Sword includes:
54#pragma GCC diagnostic push
55#pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
56#include <installmgr.h>
57#include <swbuf.h>
58#include <swversion.h>
59#pragma GCC diagnostic pop
60
61
62namespace {
63QString const groupingOrderKey("GUI/BookshelfWizard/InstallPage/grouping");
64QString const installPathKey(
65 "GUI/BookshelfWizard/InstallPage/installPathIndex");
66
67inline bool filter(WizardTaskType const taskType,
68 QStringList const & languages,
69 CSwordModuleInfo const * const mInfo)
70{
71 if (taskType == WizardTaskType::installWorks) {
73 && languages.contains(mInfo->language()->translatedName());
74 } else if (taskType == WizardTaskType::updateWorks) {
75 using CSMI = CSwordModuleInfo;
76 using CSV = sword::SWVersion const;
77 CSMI const * const installedModule =
79 return installedModule
80 && (CSV(installedModule->config(CSMI::ModuleVersion).toLatin1())
81 < CSV(mInfo->config(CSMI::ModuleVersion).toLatin1()));
82 } else {
85 }
86}
87
88} // anonymous namespace
89
91 QWidget * parent)
92 : BtBookshelfWizardPage(parent)
93 , m_taskType(iType)
94{
95 if (!m_groupingOrder.loadFrom(btConfig(), groupingOrderKey))
97
98 // Initialize menus:
101 m_contextMenu = new QMenu(this);
104
105 // Setup UI:
106 QVBoxLayout * const verticalLayout = new QVBoxLayout(this);
107
108 QHBoxLayout * horizontalLayout = new QHBoxLayout;
109 m_nameFilterLabel = new QLabel(this);
110 horizontalLayout->addWidget(m_nameFilterLabel);
111 m_nameFilterEdit = new QLineEdit(this);
113 horizontalLayout->addWidget(m_nameFilterEdit);
114 verticalLayout->addLayout(horizontalLayout);
115
117 m_bookshelfView->setHeaderHidden(false);
118 m_bookshelfView->header()->setSectionResizeMode(
119 QHeaderView::ResizeToContents);
120 verticalLayout->addWidget(m_bookshelfView);
121
122 m_msgLabel = new QLabel(this);
123 m_msgLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
124 m_msgLabel->setWordWrap(true);
125 verticalLayout->addWidget(m_msgLabel);
126
127 QHBoxLayout * const pathLayout = new QHBoxLayout();
129 m_pathCombo = new QComboBox(this);
130 m_pathCombo->setMinimumContentsLength(20);
131 m_pathCombo->setSizeAdjustPolicy(
132 QComboBox::AdjustToMinimumContentsLengthWithIcon);
133 m_pathCombo->setSizePolicy(QSizePolicy::Expanding,
134 QSizePolicy::Minimum);
135 m_pathCombo->view()->setTextElideMode(Qt::ElideMiddle);
136
137 m_pathLabel = new QLabel(this);
138 m_pathLabel->setBuddy(m_pathCombo);
140
141 pathLayout->setContentsMargins(0, 8, 0, 0);
142 pathLayout->addWidget(m_pathLabel);
143 pathLayout->addWidget(m_pathCombo);
144 }
145 pathLayout->addStretch();
146
147 m_groupingLabel = new QLabel(this);
148 pathLayout->addWidget(m_groupingLabel);
149
150 m_groupingButton = new QToolButton(this);
151 m_groupingButton->setPopupMode(QToolButton::InstantPopup);
153 m_groupingButton->setIcon(m_groupingMenu->icon());
154 m_groupingButton->setAutoRaise(true);
155 pathLayout->addWidget(m_groupingButton);
156
157 verticalLayout->addLayout(pathLayout);
158
159 // Setup models:
160 BtBookshelfFilterModel * const filterModel =
161 new BtBookshelfFilterModel(this);
162 m_bookshelfView->setModel(filterModel);
163
167 filterModel->setSourceModel(m_installPageModel);
168
169 /// \todo is this useless if m_taskType == WizardTaskType::removeWorks?
173 } else {
175 }
177 m_bookshelfView->setColumnHidden(1, true);
178
179 // Initialize connections:
182 this,
185 BT_CONNECT(m_pathCombo, qOverload<int>(&QComboBox::currentIndexChanged),
190 }
192 this, &BtBookshelfWorksPage::completeChanged);
195 BT_CONNECT(m_nameFilterEdit, &QLineEdit::textEdited,
197
199}
200
202 m_nameFilterLabel->setText(tr("Fi&lter:"));
203 if (m_taskType == installWorks) {
204 setTitle(QApplication::translate("BookshelfWizard", "Install Works"));
205 setSubTitle(QApplication::translate("BookshelfWizard",
206 "Choose one or more works to "
207 "install."));
208 setButtonText(QWizard::NextButton,tr("Install Works >"));
210 setTitle(QApplication::translate("BookshelfWizard", "Update Works"));
211 setSubTitle(QApplication::translate("BookshelfWizard",
212 "Choose one or more works to "
213 "update."));
214 setButtonText(QWizard::NextButton,tr("Update Works >"));
215 } else {
217 setTitle(QApplication::translate("BookshelfWizard", "Remove Works"));
218 setSubTitle(QApplication::translate("BookshelfWizard",
219 "Choose one or more works to "
220 "remove."));
221 setButtonText(QWizard::NextButton,tr("Remove Works >"));
222 }
223
225 m_pathLabel->setText(tr("Install &folder:"));
226 m_pathCombo->setToolTip(tr("The folder where the new works will be "
227 "installed"));
228 }
229
231 m_msgLabel->setText(tr("There are no works to update."));
233 m_msgLabel->setText(tr("No works are currently installed so they "
234 "cannot be removed."));
235 } else {
237 m_msgLabel->setText(
238 tr("No works can be installed with the current selection of "
239 "remote libraries and languages. Please go back and make a "
240 "different selection."));
241 }
242
243 m_groupingLabel->setText(tr("Grouping:"));
244 m_groupingButton->setText(tr("Grouping"));
245 m_groupingButton->setToolTip(
246 tr("Change the grouping of items in the bookshelf."));
247}
248
254
256 // Update models:
257 QStringList sources;
258 QStringList languages;
259 if (m_taskType == installWorks) {
260 sources = btWizard().selectedSources();
261 languages = btWizard().selectedLanguages();
262 } else {
264 }
265
266 {
267 QSet<QString> addedModuleNames;
268 m_bookshelfModel->clear();
269 m_usedBackends.clear();
270 for (auto const & sourceName : sources) {
271 sword::InstallSource const source =
272 BtInstallBackend::source(sourceName);
273 std::unique_ptr<CSwordBackend const> backend(
275 bool backendUsed = false;
276 for (auto * const module : backend->moduleList()) {
277 if (filter(m_taskType, languages, module)) {
278 QString const & moduleName = module->name();
279 if (addedModuleNames.contains(moduleName))
280 continue;
281 addedModuleNames.insert(moduleName);
282 m_bookshelfModel->addModule(module);
283 module->setProperty("installSourceName",
284 QString(source.caption.c_str()));
285 backendUsed = true;
286 }
287 }
288 if (backendUsed)
289 m_usedBackends.emplace_back(std::move(backend));
290 }
292 m_bookshelfView->expandAll();
293
294 bool const noWorks = (addedModuleNames.count() == 0);
295 m_msgLabel->setVisible(noWorks);
296 m_bookshelfView->setVisible(!noWorks);
297 m_groupingButton->setVisible(!noWorks);
298 m_groupingLabel->setVisible(!noWorks);
299 }
300
301 // Set grouping:
302 auto const & cat = BtBookshelfTreeModel::Grouping::CAT;
303 auto const & catLang = BtBookshelfTreeModel::Grouping::CAT_LANG;
304 if (languages.count() == 1) {
306 } else if (languages.count() > 1 && m_groupingOrder == cat) {
308 }
309}
310
312 BtBookshelfTreeModel::Grouping const & grouping)
313{
315 m_bookshelfView->setRootIsDecorated(!grouping.list().isEmpty());
316}
317
319{ return checkedModules().count() > 0; }
320
327
330
332{ return m_pathCombo->currentText(); }
333
335{ btConfig().setValue(installPathKey, index); }
336
337static bool installPathIsUsable(QString const & path) {
338 if (path.isEmpty())
339 return false;
340 QDir const dir(path);
341 if (!dir.exists() || !dir.isReadable())
342 return false;
343 return QFileInfo(dir.canonicalPath()).isWritable();
344}
345
347 m_pathCombo->clear();
348 bool haveUsableTargets = false;
349 for (auto const & target : CSwordBackend::instance().swordDirList()) {
350 if (installPathIsUsable(target)) {
351 m_pathCombo->addItem(QDir::toNativeSeparators(target));
352 haveUsableTargets = true;
353 }
354 }
355
356 m_pathCombo->setVisible(haveUsableTargets);
357 m_pathLabel->setVisible(haveUsableTargets);
358 if (haveUsableTargets) {
359 /* Choose the current value from config but check whether we have so
360 many items: */
361 int const cfgValue = btConfig().value<int>(installPathKey, 0);
362 int const lastIdx = m_pathCombo->count() - 1;
363 m_pathCombo->setCurrentIndex(cfgValue > lastIdx ? lastIdx : cfgValue);
364 }
365}
#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)