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

Introduce Export/Import (to file) Reading List #1163

Merged
merged 5 commits into from
Aug 11, 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
15 changes: 15 additions & 0 deletions resources/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,18 @@ ContentTypeFilter {
font-size: 16px;
margin-bottom: 10px;
}

#readinglistbar QPushButton {
margin-left: 4px;
}

#readinglistbar QPushButton::hover {
border: 1px solid #3366CC;
background-color: #D9E9FF;
border-radius: 3px;
}

#readinglistbar QPushButton::menu-indicator {
width: 0;
height: 0;
}
6 changes: 5 additions & 1 deletion resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,9 @@
"file-not-found-text": "ZIM file doesn't exist or is not readable",
"zim-id": "Zim Id",
"zim-name": "Zim Name",
"zim-path": "Zim File Path"
"zim-path": "Zim File Path",
"export-reading-list": "Export reading list",
"export-reading-list-error": "An error has occured during export of the reading list.",
"import-reading-list": "Import reading list",
"import-reading-list-error": "An error has occured during import of the reading list."
}
6 changes: 5 additions & 1 deletion resources/i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,9 @@
"file-not-found-text": "Error description text for when the desktop application cannot find the Zim file needed to display the web page.",
"zim-id": "The term for the unique identifier of a zim file.",
"zim-name": "The term for the name of a Zim file",
"zim-path": "The term for the path of a Zim file"
"zim-path": "The term for the path of a Zim file",
"export-reading-list": "Represents the action of exporting the reading list to a file.",
"export-reading-list-error": "Error description text for when exporting the reading list to a file failed.",
"import-reading-list": "Represents the action of importing a reading list from a file.",
"import-reading-list-error": "Error description text for when importing a reading list from a file failed."
}
1 change: 1 addition & 0 deletions resources/icons/more-vertical.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions resources/kiwix.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,6 @@
<file>icons/check-solid.svg</file>
<file>icons/xmark-solid.svg</file>
<file>icons/home-button.svg</file>
<file>icons/more-vertical.svg</file>
</qresource>
</RCC>
4 changes: 4 additions & 0 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ void KiwixApp::createActions()

CREATE_ACTION_ONOFF_ICON_SHORTCUT(ToggleReadingListAction, "reading-list-active", "reading-list", gt("reading-list"), QKeySequence(Qt::CTRL | Qt::Key_B));

CREATE_ACTION(ExportReadingListAction, gt("export-reading-list"));

CREATE_ACTION(ImportReadingListAction, gt("import-reading-list"));

CREATE_ACTION_ONOFF_ICON_SHORTCUT(ToggleAddBookmarkAction, "star-active", "star", gt("add-bookmark"), QKeySequence(Qt::CTRL | Qt::Key_D));

CREATE_ACTION_SHORTCUTS(ZoomInAction, gt("zoom-in"), QList<QKeySequence>({QKeySequence::ZoomIn, QKeySequence(Qt::CTRL | Qt::Key_Equal)}));
Expand Down
2 changes: 2 additions & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class KiwixApp : public QtSingleApplication
SettingAction,
DonateAction,
ExitAction,
ExportReadingListAction,
ImportReadingListAction,
MAX_ACTION
};

Expand Down
10 changes: 10 additions & 0 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ Library::QStringSet Library::getLibraryZimsFromDir(QString dir) const
return zimsInDir;
}

bool Library::readBookMarksFile(const std::string &filename)
{
kiwix::Manager manager(mp_library);
if (!manager.readBookmarkFile(filename))
return false;

emit bookmarksChanged();
return true;
}

const kiwix::Book &Library::getBookById(QString id) const
{
return mp_library->getBookById(id.toStdString());
Expand Down
1 change: 1 addition & 0 deletions src/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Library : public QObject
void removeBookFromLibraryById(const QString& id);
void addBookmark(kiwix::Bookmark& bookmark);
void removeBookmark(const QString& zimId, const QString& url);
bool readBookMarksFile(const std::string& filename);
void save();
kiwix::LibraryPtr getKiwixLibrary() { return mp_library; }
public slots:
Expand Down
2 changes: 2 additions & 0 deletions src/mainmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ MainMenu::MainMenu(QWidget *parent) :
m_fileMenu.ADD_ACTION(BrowseLibraryAction);
m_fileMenu.ADD_ACTION(OpenFileAction);
m_fileMenu.ADD_ACTION(OpenRecentAction);
m_fileMenu.ADD_ACTION(ExportReadingListAction);
m_fileMenu.ADD_ACTION(ImportReadingListAction);

/* TODO See https://github.com/kiwix/kiwix-desktop/issues/77
m_fileMenu.ADD_ACTION(SavePageAsAction);
Expand Down
43 changes: 43 additions & 0 deletions src/readinglistbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "zim/item.h"

#include <QListWidgetItem>
#include <QFileDialog>
#include <QStandardPaths>

const QString documentsDir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);

ReadingListBar::ReadingListBar(QWidget *parent) :
QWidget(parent),
Expand All @@ -28,7 +32,17 @@ ReadingListBar::ReadingListBar(QWidget *parent) :

setupList();

auto app = KiwixApp::instance();
auto exportAction = app->getAction(KiwixApp::ExportReadingListAction);
auto importAction = app->getAction(KiwixApp::ImportReadingListAction);
connect(exportAction, &QAction::triggered, this, &ReadingListBar::onExport);
connect(importAction, &QAction::triggered, this, &ReadingListBar::onImport);
ui->label->setText(gt("reading-list-title"));

QMenu *portMenu = new QMenu(this);
portMenu->addAction(exportAction);
portMenu->addAction(importAction);
ui->readingListMenuButton->setMenu(portMenu);
}

ReadingListBar::~ReadingListBar()
Expand Down Expand Up @@ -104,6 +118,35 @@ void ReadingListBar::onItemActivated(QListWidgetItem* item, Qt::MouseButtons but
}
}

void ReadingListBar::onExport()
{
auto app = KiwixApp::instance();
auto kiwixLibrary = app->getLibrary()->getKiwixLibrary();
auto suggestedFilePath = documentsDir + "/kiwix_readinglist.xml";
QString fileName = QFileDialog::getSaveFileName(app->getMainWindow(),
gt("save-file-as-window-title"),
suggestedFilePath, "(*.xml)");
if (fileName.isEmpty())
return;

if (!kiwixLibrary->writeBookmarksToFile(fileName.toStdString()))
app->showMessage(gt("export-reading-list-error"), gt("error-title"), QMessageBox::Information);
}

void ReadingListBar::onImport()
{
auto app = KiwixApp::instance();
auto library = app->getLibrary();
QString fileName = QFileDialog::getOpenFileName(app->getMainWindow(),
gt("open-file"),
documentsDir, "(*.xml)");
if (fileName.isEmpty())
return;

if (!library->readBookMarksFile(fileName.toStdString()))
app->showMessage(gt("import-reading-list-error"), gt("error-title"), QMessageBox::Information);
}

void ReadingListBar::openUrl(QListWidgetItem* item, bool newTab)
{
int index = ui->listWidget->row(item);
Expand Down
2 changes: 2 additions & 0 deletions src/readinglistbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public slots:
void onItemDoubleClicked(QListWidgetItem *item);
void onItemPressed(QListWidgetItem* item, Qt::MouseButtons buttons);
void onItemActivated(QListWidgetItem *item, Qt::MouseButtons buttons);
void onExport();
void onImport();
private:
Ui::readinglistbar *ui;
int clickKind;
Expand Down
61 changes: 50 additions & 11 deletions src/readinglistbar.ui
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,53 @@
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>Reading List</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>16</pointsize>
</font>
</property>
<property name="text">
<string>Reading List</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="readingListMenuButton">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="../resources/kiwix.qrc">
<normaloff>:/icons/more-vertical.svg</normaloff>:/icons/more-vertical.svg</iconset>
</property>
<property name="iconSize">
<size>
<width>30</width>
<height>30</height>
</size>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QListWidget" name="listWidget">
Expand Down Expand Up @@ -68,6 +105,8 @@
</item>
</layout>
</widget>
<resources/>
<resources>
<include location="../resources/kiwix.qrc"/>
</resources>
<connections/>
</ui>
3 changes: 3 additions & 0 deletions src/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ BookmarkButton::BookmarkButton(QWidget *parent) :
connect(this, &QToolButton::triggered, this, &BookmarkButton::on_buttonClicked);
connect(this, &QToolButton::triggered, this, &BookmarkButton::update_display);
setDefaultAction(KiwixApp::instance()->getAction(KiwixApp::Actions::ToggleAddBookmarkAction));

auto library = KiwixApp::instance()->getLibrary();
connect(library, &Library::bookmarksChanged, this, &BookmarkButton::update_display);
}

void BookmarkButton::update_display()
Expand Down
Loading