From 2848fc1e43e914db732387cad57ccab8f5071e24 Mon Sep 17 00:00:00 2001 From: luddens Date: Fri, 19 Apr 2019 17:48:09 +0200 Subject: [PATCH 1/2] delete books with simple ui add a column at the book's list's table with "delete" button. This ui is temporary. On click, this button sends the id of the selected book at the ContentManager class which gets book by this id, and erases all files in relationship with this book on the computer. --- resources/texts/_contentManager.html | 7 +++++++ src/contentmanager.cpp | 16 ++++++++++++++-- src/contentmanager.h | 1 + src/library.cpp | 4 ++++ src/library.h | 1 + 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/resources/texts/_contentManager.html b/resources/texts/_contentManager.html index 7ae1bea..32df1bc 100644 --- a/resources/texts/_contentManager.html +++ b/resources/texts/_contentManager.html @@ -74,6 +74,9 @@ function init() { downloadUpdaters[book.id] = setInterval(function() { getDownloadInfo(book.id); }, 1000); }); }, + eraseBook : function(book) { + contentManager.eraseBook(book.id); + }, displayedBooks : function(books, nb) { var a = books.slice(0, nb); return a; @@ -215,6 +218,7 @@ details:hover {
+ icone poubelle File name Size @@ -225,6 +229,9 @@ details:hover {
+ + + diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index fc9dc41..dc3da5f 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -25,7 +25,6 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, connect(this, &ContentManager::filterParamsChanged, this, &ContentManager::updateLibrary); } - void ContentManager::setLocal(bool local) { if (local == m_local) { return; @@ -185,10 +184,23 @@ QString ContentManager::downloadBook(const QString &id) mp_library->addBookToLibrary(book); mp_library->save(); emit(mp_library->booksChanged()); - emit(booksChanged()); return QString::fromStdString(download->getDid()); } +void ContentManager::eraseBook(const QString& id) +{ + kiwix::Book book = mp_library->getBookById(id); + QString dirName = QString::fromUtf8(getDataDirectory().c_str()); + QString fileSelection = QString::fromUtf8(getLastPathElement(book.getPath()).c_str()) + "*"; + QDir dir(dirName, fileSelection); + for(const QString& filename: dir.entryList()) { + dir.remove(filename); + } + mp_library->removeBookFromLibraryById(id); + mp_library->save(); + emit(mp_library->booksChanged()); +} + QStringList ContentManager::getDownloadIds() { QStringList list; diff --git a/src/contentmanager.h b/src/contentmanager.h index 4a7324c..64e1f26 100644 --- a/src/contentmanager.h +++ b/src/contentmanager.h @@ -51,6 +51,7 @@ public slots: QString downloadBook(const QString& id); void updateLibrary(); void setSearch(const QString& search); + void eraseBook(const QString& id); }; #endif // CONTENTMANAGER_H diff --git a/src/library.cpp b/src/library.cpp index 3eb47e9..9c0e989 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -113,6 +113,10 @@ void Library::addBookToLibrary(kiwix::Book &book) m_library.addBook(book); } +void Library::removeBookFromLibraryById(const QString& id) { + m_library.removeBookById(id.toStdString()); +} + void Library::addBookmark(kiwix::Bookmark &bookmark) { m_library.addBookmark(bookmark); diff --git a/src/library.h b/src/library.h index 47c33e9..b6705bb 100644 --- a/src/library.h +++ b/src/library.h @@ -32,6 +32,7 @@ public: QStringList listBookIds(const QString& query, const QString &categoryFilter); const std::vector& getBookmarks() { return m_library.getBookmarks(); } void addBookToLibrary(kiwix::Book& book); + void removeBookFromLibraryById(const QString& id); void addBookmark(kiwix::Bookmark& bookmark); void removeBookmark(const QString& zimId, const QString& url); void save(); From a5c4beb61264b7bc3e96f42f4bd1fa09196dc432 Mon Sep 17 00:00:00 2001 From: luddens Date: Fri, 26 Apr 2019 15:15:53 +0200 Subject: [PATCH 2/2] erase book in a right-click context menu Foreach "summary" element that represents a book, Vue.js binds its id with the book's id. When a "contextmenu" event (right-click) is emitted, it saves the mouse's coordinates to set the contextmenu's position and display it if the cursor is on a book. The "displayMenu(book)" function displays the contextmenu if the "book" parameter isn't null. It handles which options to show too (for now there is the "delete" option only). The delete option calls "eraseBook(book)" that uses "getBookFromMousePosition()" to know which book has to be deleted. "getBookFromMousePosition(info)" gets all elements at the mouse's position, selects a "summary" element which has the "book-summary" class if there is one, to get the book requested thanks to the id bind by Vue.js and return it. --- resources/texts/_contentManager.html | 89 ++++++++++++++++++++++++++-- src/contentmanagerview.cpp | 1 + 2 files changed, 85 insertions(+), 5 deletions(-) diff --git a/resources/texts/_contentManager.html b/resources/texts/_contentManager.html index 32df1bc..819e04d 100644 --- a/resources/texts/_contentManager.html +++ b/resources/texts/_contentManager.html @@ -81,6 +81,24 @@ function init() { var a = books.slice(0, nb); return a; }, + getBookFromMousePosition : function() { + var elements = document.elementsFromPoint(mouseX, mouseY); + var bookId = null; + for(var i = 0; i < elements.length; i++) { + if (elements[i].localName == "summary" && elements[i].classList.contains("book-summary")) { + bookId = elements[i].id; + break; + } + } + var book = null; + for(var i = 0; i < app.books.length; i++) { + if (app.books[i]["id"] == bookId) { + book = app.books[i]; + break; + } + } + return book; + }, niceBytes : niceBytes } }); @@ -100,6 +118,39 @@ function scrolled(e) { app.displayedBooksNb = Math.min(app.displayedBooksNb+20, app.books.length); } } + +window.addEventListener("click", e => { + if (menuVisible) + displayMenu(null); +}); + +var mouseX, mouseY = 0; +window.addEventListener("contextmenu", e => { + e.preventDefault(); + mouseX = e.pageX; + mouseY = e.pageY; + setContextMenuPosition(); + var book = app.getBookFromMousePosition(); + displayMenu(book); +}); + +var menuVisible = false; +function displayMenu(book) { + var menu = document.getElementById("menu"); + menu.style.display = (book) ? "block" : "none"; + menuVisible = (book) ? true : false; + if (!book) + return; + var localElement = document.getElementsByClassName("local-option")[0]; + localElement.style.display = (book.path) ? "block" : "none"; +}; + +function setContextMenuPosition() { + var menu = document.getElementById("menu"); + menu.style.left = `${mouseX}px`; + menu.style.top = `${mouseY}px`; +}; + @@ -218,7 +296,6 @@ details:hover {
- icone poubelle File name Size @@ -227,11 +304,13 @@ details:hover {
+
- - - - + diff --git a/src/contentmanagerview.cpp b/src/contentmanagerview.cpp index 5b40249..fbe9119 100644 --- a/src/contentmanagerview.cpp +++ b/src/contentmanagerview.cpp @@ -10,6 +10,7 @@ ContentManagerView::ContentManagerView(QWidget *parent) auto profile = page()->profile(); auto app = KiwixApp::instance(); profile->installUrlSchemeHandler("zim", app->getSchemeHandler()); + setContextMenuPolicy( Qt::NoContextMenu ); }