Merge pull request #970 from kiwix/openFolder

This commit is contained in:
Matthieu Gautier 2023-08-04 11:02:32 +02:00 committed by GitHub
commit 8fa06343e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 13 deletions

View File

@ -144,5 +144,12 @@
"download-storage-error": "Storage Error", "download-storage-error": "Storage Error",
"download-storage-error-text": "The system doesn't have enough storage available.", "download-storage-error-text": "The system doesn't have enough storage available.",
"download-unavailable": "Download Unavailable", "download-unavailable": "Download Unavailable",
"download-unavailable-text": "This download is unavailable." "download-unavailable-text": "This download is unavailable.",
"open-book": "Open book",
"download-book": "Download book",
"pause-download": "Pause download",
"resume-download": "Resume download",
"open-folder": "Open folder",
"couldnt-open-location": "Couldn't open location",
"couldnt-open-location-text": "Kiwix is not able to open folder {{FOLDER}}"
} }

View File

@ -21,6 +21,7 @@
#include "kiwixconfirmbox.h" #include "kiwixconfirmbox.h"
#include <QtConcurrent/QtConcurrentRun> #include <QtConcurrent/QtConcurrentRun>
#include "contentmanagerheader.h" #include "contentmanagerheader.h"
#include <QDesktopServices>
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent) ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent), : QObject(parent),
@ -89,16 +90,19 @@ QList<QMap<QString, QVariant>> ContentManager::getBooksList()
void ContentManager::onCustomContextMenu(const QPoint &point) void ContentManager::onCustomContextMenu(const QPoint &point)
{ {
QModelIndex index = mp_view->getView()->indexAt(point); QModelIndex index = mp_view->getView()->indexAt(point);
if (!index.isValid())
return;
QMenu contextMenu("optionsMenu", mp_view->getView()); QMenu contextMenu("optionsMenu", mp_view->getView());
auto bookNode = static_cast<RowNode*>(index.internalPointer()); auto bookNode = static_cast<RowNode*>(index.internalPointer());
const auto id = bookNode->getBookId(); const auto id = bookNode->getBookId();
QAction menuDeleteBook("Delete book", this); QAction menuDeleteBook(gt("delete-book"), this);
QAction menuOpenBook("Open book", this); QAction menuOpenBook(gt("open-book"), this);
QAction menuDownloadBook("Download book", this); QAction menuDownloadBook(gt("download-book"), this);
QAction menuPauseBook("Pause download", this); QAction menuPauseBook(gt("pause-download"), this);
QAction menuResumeBook("Resume download", this); QAction menuResumeBook(gt("resume-download"), this);
QAction menuCancelBook("Cancel download", this); QAction menuCancelBook(gt("cancel-download"), this);
QAction menuOpenFolder(gt("open-folder"), this);
if (bookNode->isDownloading()) { if (bookNode->isDownloading()) {
if (bookNode->getDownloadInfo().paused) { if (bookNode->getDownloadInfo().paused) {
@ -108,12 +112,29 @@ void ContentManager::onCustomContextMenu(const QPoint &point)
} }
contextMenu.addAction(&menuCancelBook); contextMenu.addAction(&menuCancelBook);
} else { } else {
if (m_local) { try {
const auto book = KiwixApp::instance()->getLibrary()->getBookById(id);
auto bookPath = QString::fromStdString(book.getPath());
contextMenu.addAction(&menuOpenBook); contextMenu.addAction(&menuOpenBook);
contextMenu.addAction(&menuDeleteBook); contextMenu.addAction(&menuDeleteBook);
} contextMenu.addAction(&menuOpenFolder);
else 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();
});
}
});
} catch (...) {
contextMenu.addAction(&menuDownloadBook); contextMenu.addAction(&menuDownloadBook);
}
} }
connect(&menuDeleteBook, &QAction::triggered, [=]() { connect(&menuDeleteBook, &QAction::triggered, [=]() {
@ -135,9 +156,7 @@ void ContentManager::onCustomContextMenu(const QPoint &point)
resumeBook(id, index); resumeBook(id, index);
}); });
if (index.isValid()) { contextMenu.exec(mp_view->getView()->viewport()->mapToGlobal(point));
contextMenu.exec(mp_view->getView()->viewport()->mapToGlobal(point));
}
} }
void ContentManager::setLocal(bool local) { void ContentManager::setLocal(bool local) {