mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Merge pull request #1035 from kiwix/confirmbox_utils
Made it easier to display info and/or confirmation boxes
This commit is contained in:
commit
4914bb86eb
@ -23,6 +23,26 @@
|
|||||||
#include "contentmanagerheader.h"
|
#include "contentmanagerheader.h"
|
||||||
#include <QDesktopServices>
|
#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)
|
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
|
||||||
: QObject(parent),
|
: QObject(parent),
|
||||||
mp_library(library),
|
mp_library(library),
|
||||||
@ -122,18 +142,7 @@ void ContentManager::onCustomContextMenu(const QPoint &point)
|
|||||||
contextMenu.addAction(&menuDeleteBook);
|
contextMenu.addAction(&menuDeleteBook);
|
||||||
contextMenu.addAction(&menuOpenFolder);
|
contextMenu.addAction(&menuOpenFolder);
|
||||||
connect(&menuOpenFolder, &QAction::triggered, [=]() {
|
connect(&menuOpenFolder, &QAction::triggered, [=]() {
|
||||||
QFileInfo fileInfo(bookPath);
|
openFileLocation(bookPath, mp_view);
|
||||||
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();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
contextMenu.addAction(&menuDownloadBook);
|
contextMenu.addAction(&menuDownloadBook);
|
||||||
@ -428,11 +437,7 @@ QString ContentManager::downloadBook(const QString &id, QModelIndex index)
|
|||||||
emit managerModel->startDownload(index);
|
emit managerModel->startDownload(index);
|
||||||
return downloadStatus;
|
return downloadStatus;
|
||||||
}
|
}
|
||||||
KiwixConfirmBox *dialog = new KiwixConfirmBox(dialogHeader, dialogText, true, mp_view);
|
showInfoBox(dialogHeader, dialogText, mp_view);
|
||||||
dialog->show();
|
|
||||||
connect(dialog, &KiwixConfirmBox::okClicked, [=]() {
|
|
||||||
dialog->deleteLater();
|
|
||||||
});
|
|
||||||
return downloadStatus;
|
return downloadStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -499,6 +504,26 @@ QString formatText(QString text)
|
|||||||
return finalText;
|
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)
|
void ContentManager::eraseBook(const QString& id)
|
||||||
{
|
{
|
||||||
auto text = gt("delete-book-text");
|
auto text = gt("delete-book-text");
|
||||||
@ -513,29 +538,8 @@ void ContentManager::eraseBook(const QString& id)
|
|||||||
text += formatText(gt("perma-delete-files-text"));
|
text += formatText(gt("perma-delete-files-text"));
|
||||||
}
|
}
|
||||||
text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle()));
|
text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle()));
|
||||||
KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("delete-book"), text, false, mp_view);
|
showConfirmBox(gt("delete-book"), text, mp_view, [=]() {
|
||||||
dialog->show();
|
reallyEraseBook(id, moveToTrash);
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -577,15 +581,9 @@ void ContentManager::cancelBook(const QString& id, QModelIndex index)
|
|||||||
{
|
{
|
||||||
auto text = gt("cancel-download-text");
|
auto text = gt("cancel-download-text");
|
||||||
text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle()));
|
text = text.replace("{{ZIM}}", QString::fromStdString(mp_library->getBookById(id).getTitle()));
|
||||||
KiwixConfirmBox *dialog = new KiwixConfirmBox(gt("cancel-download"), text, false, mp_view);
|
showConfirmBox(gt("cancel-download"), text, mp_view, [=]() {
|
||||||
dialog->show();
|
|
||||||
connect(dialog, &KiwixConfirmBox::yesClicked, [=]() {
|
|
||||||
cancelBook(id);
|
cancelBook(id);
|
||||||
emit managerModel->cancelDownload(index);
|
emit managerModel->cancelDownload(index);
|
||||||
dialog->deleteLater();
|
|
||||||
});
|
|
||||||
connect(dialog, &KiwixConfirmBox::noClicked, [=]() {
|
|
||||||
dialog->deleteLater();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,6 +54,8 @@ private:
|
|||||||
QStringList m_categories;
|
QStringList m_categories;
|
||||||
|
|
||||||
QStringList getBookIds();
|
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);
|
void eraseBookFilesFromComputer(const QString dirPath, const QString filename, const bool moveToTrash);
|
||||||
BookInfoList getBooksList();
|
BookInfoList getBooksList();
|
||||||
ContentManagerModel *managerModel;
|
ContentManagerModel *managerModel;
|
||||||
@ -83,6 +85,7 @@ public slots:
|
|||||||
void updateLibrary();
|
void updateLibrary();
|
||||||
void setSearch(const QString& search);
|
void setSearch(const QString& search);
|
||||||
void setSortBy(const QString& sortBy, const bool sortOrderAsc);
|
void setSortBy(const QString& sortBy, const bool sortOrderAsc);
|
||||||
|
// eraseBook() asks for confirmation (reallyEraseBook() doesn't)
|
||||||
void eraseBook(const QString& id);
|
void eraseBook(const QString& id);
|
||||||
void updateRemoteLibrary(const QString& content);
|
void updateRemoteLibrary(const QString& content);
|
||||||
void updateLanguages(const QString& content);
|
void updateLanguages(const QString& content);
|
||||||
|
@ -36,3 +36,12 @@ KiwixConfirmBox::~KiwixConfirmBox()
|
|||||||
{
|
{
|
||||||
delete ui;
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
@ -12,7 +12,7 @@ class KiwixConfirmBox : public QDialog
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit KiwixConfirmBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr);
|
KiwixConfirmBox(QString confirmTitle, QString confirmText, bool okDialog, QWidget *parent = nullptr);
|
||||||
~KiwixConfirmBox();
|
~KiwixConfirmBox();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
@ -26,4 +26,22 @@ private:
|
|||||||
Ui::kiwixconfirmbox *ui;
|
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
|
#endif // KIWIXCONFIRMBOX_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user