Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made it easier to display info and/or confirmation boxes #1035

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 45 additions & 47 deletions src/contentmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@
#include "contentmanagerheader.h"
#include <QDesktopServices>

namespace
{

// Opens the directory containing the input file path.
// parent is the widget serving as the parent for the error dialog in case of
// failure.
void openFileLocation(QString path, QWidget *parent = nullptr)
{
QFileInfo fileInfo(path);
QDir dir = fileInfo.absoluteDir();
bool dirOpen = dir.exists() && dir.isReadable() && QDesktopServices::openUrl(dir.absolutePath());
if (!dirOpen) {
QString failedText = gt("couldnt-open-location-text");
failedText = failedText.replace("{{FOLDER}}", "<b>" + dir.absolutePath() + "</b>");
showInfoBox(gt("couldnt-open-location"), failedText, parent);
}
}

} // unnamed namespace

ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent),
mp_library(library),
Expand Down Expand Up @@ -122,18 +142,7 @@ void ContentManager::onCustomContextMenu(const QPoint &point)
contextMenu.addAction(&menuDeleteBook);
contextMenu.addAction(&menuOpenFolder);
connect(&menuOpenFolder, &QAction::triggered, [=]() {
QFileInfo fileInfo(bookPath);
QDir bookDir = fileInfo.absoluteDir();
bool dirOpen = bookDir.exists() && bookDir.isReadable() && QDesktopServices::openUrl(bookDir.absolutePath());
if (!dirOpen) {
QString failedText = gt("couldnt-open-location-text");
failedText = failedText.replace("{{FOLDER}}", "<b>" + bookDir.absolutePath() + "</b>");
KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("couldnt-open-location"), failedText, true, mp_view);
dialog->show();
connect(dialog, &KiwixConfirmBox::okClicked, [=]() {
dialog->deleteLater();
});
}
openFileLocation(bookPath, mp_view);
});
} catch (...) {
contextMenu.addAction(&menuDownloadBook);
Expand Down Expand Up @@ -428,11 +437,7 @@ QString ContentManager::downloadBook(const QString &id, QModelIndex index)
emit managerModel->startDownload(index);
return downloadStatus;
}
KiwixConfirmBox *dialog = new KiwixConfirmBox(dialogHeader, dialogText, true, mp_view);
dialog->show();
connect(dialog, &KiwixConfirmBox::okClicked, [=]() {
dialog->deleteLater();
});
showInfoBox(dialogHeader, dialogText, mp_view);
return downloadStatus;
}

Expand Down Expand Up @@ -499,6 +504,26 @@ QString formatText(QString text)
return finalText;
}

void ContentManager::reallyEraseBook(const QString& id, bool moveToTrash)
{
auto tabBar = KiwixApp::instance()->getTabWidget();
tabBar->closeTabsByZimId(id);
kiwix::Book book = mp_library->getBookById(id);
QString dirPath = QString::fromStdString(kiwix::removeLastPathElement(book.getPath()));
QString fileName = QString::fromStdString(kiwix::getLastPathElement(book.getPath())) + "*";
eraseBookFilesFromComputer(dirPath, fileName, moveToTrash);
mp_library->removeBookFromLibraryById(id);
mp_library->save();
emit mp_library->bookmarksChanged();
if (m_local) {
emit(bookRemoved(id));
} else {
emit(oneBookChanged(id));
}
KiwixApp::instance()->getSettingsManager()->deleteSettings(id);
emit booksChanged();
}

void ContentManager::eraseBook(const QString& id)
{
auto text = gt("delete-book-text");
Expand All @@ -513,29 +538,8 @@ void ContentManager::eraseBook(const QString& id)
text += formatText(gt("perma-delete-files-text"));
}
text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle()));
KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("delete-book"), text, false, mp_view);
dialog->show();
connect(dialog, &KiwixConfirmBox::yesClicked, [=]() {
auto tabBar = KiwixApp::instance()->getTabWidget();
tabBar->closeTabsByZimId(id);
kiwix::Book book = mp_library->getBookById(id);
QString dirPath = QString::fromStdString(kiwix::removeLastPathElement(book.getPath()));
QString fileName = QString::fromStdString(kiwix::getLastPathElement(book.getPath())) + "*";
eraseBookFilesFromComputer(dirPath, fileName, moveToTrash);
mp_library->removeBookFromLibraryById(id);
mp_library->save();
emit mp_library->bookmarksChanged();
if (m_local) {
emit(bookRemoved(id));
} else {
emit(oneBookChanged(id));
}
KiwixApp::instance()->getSettingsManager()->deleteSettings(id);
dialog->deleteLater();
emit booksChanged();
});
connect(dialog, &KiwixConfirmBox::noClicked, [=]() {
dialog->deleteLater();
showConfirmBox(gt("delete-book"), text, mp_view, [=]() {
reallyEraseBook(id, moveToTrash);
});
}

Expand Down Expand Up @@ -577,15 +581,9 @@ void ContentManager::cancelBook(const QString& id, QModelIndex index)
{
auto text = gt("cancel-download-text");
text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle()));
KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("cancel-download"), text, false, mp_view);
dialog->show();
connect(dialog, &KiwixConfirmBox::yesClicked, [=]() {
showConfirmBox(gt("cancel-download"), text, mp_view, [=]() {
cancelBook(id);
emit managerModel->cancelDownload(index);
dialog->deleteLater();
});
connect(dialog, &KiwixConfirmBox::noClicked, [=]() {
dialog->deleteLater();
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/contentmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class ContentManager : public QObject
QStringList m_categories;

QStringList getBookIds();
// reallyEraseBook() doesn't ask for confirmation (unlike eraseBook())
void reallyEraseBook(const QString& id, bool moveToTrash);
void eraseBookFilesFromComputer(const QString dirPath, const QString filename, const bool moveToTrash);
BookInfoList getBooksList();
ContentManagerModel *managerModel;
Expand Down Expand Up @@ -83,6 +85,7 @@ public slots:
void updateLibrary();
void setSearch(const QString& search);
void setSortBy(const QString& sortBy, const bool sortOrderAsc);
// eraseBook() asks for confirmation (reallyEraseBook() doesn't)
void eraseBook(const QString& id);
void updateRemoteLibrary(const QString& content);
void updateLanguages(const QString& content);
Expand Down
9 changes: 9 additions & 0 deletions src/kiwixconfirmbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ KiwixConfirmBox::~KiwixConfirmBox()
{
delete ui;
}

void showInfoBox(QString title, QString text, QWidget *parent)
{
KiwixConfirmBox *dialog = new KiwixConfirmBox(title, text, true, parent);
dialog->show();
QObject::connect(dialog, &KiwixConfirmBox::okClicked, [=]() {
dialog->deleteLater();
});
}
20 changes: 19 additions & 1 deletion src/kiwixconfirmbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class KiwixConfirmBox : public QDialog
Q_OBJECT

public:
explicit KiwixConfirmBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr);
KiwixConfirmBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr);
~KiwixConfirmBox();

signals:
Expand All @@ -26,4 +26,22 @@ class KiwixConfirmBox : public QDialog
Ui::kiwixconfirmbox *ui;
};


void showInfoBox(QString title, QString text, QWidget *parent = nullptr);

template<class YesAction>
void showConfirmBox(QString title, QString text, QWidget *parent,
YesAction yesAction)
{
KiwixConfirmBox *dialog = new KiwixConfirmBox(title, text, false, parent);
dialog->show();
QObject::connect(dialog, &KiwixConfirmBox::yesClicked, [=]() {
yesAction();
dialog->deleteLater();
});
QObject::connect(dialog, &KiwixConfirmBox::noClicked, [=]() {
dialog->deleteLater();
});
}

#endif // KIWIXCONFIRMBOX_H
Loading