Skip to content

Commit

Permalink
- Use calibre-defined functions instead of QErrorMessage, QMessageBox
Browse files Browse the repository at this point in the history
- Use icons instead of unicode for buttons and context menus
  • Loading branch information
ping committed Jul 4, 2023
1 parent 18e9d5f commit 83b16e2
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 290 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Each selected loan will then be downloaded in its own calibre job. When the job

Only downloadable loans will be listed. If the loan does not have a downloadable format, or has previously been sent to your Kindle, it will not be displayed.

## Credit

- Icons from https://remixicon.com/

## Disclaimer

This is not affliated, endorsed or certified by OverDrive. To use this plugin, you must already have access to OverDrive services via a valid library account. Use at your own risk.
54 changes: 40 additions & 14 deletions calibre-plugin/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
# See https://github.com/ping/libby-calibre-plugin for more
# information
#

from pathlib import Path
from typing import Dict

from calibre import browser
from calibre.gui2 import Dispatcher
from calibre.gui2 import Dispatcher, error_dialog, is_dark_theme
from calibre.gui2.actions import InterfaceAction
from calibre.gui2.dialogs.confirm_delete import confirm
from calibre.gui2.ebook_download import show_download_info
from calibre.gui2.threaded_jobs import ThreadedJob
from polyglot.builtins import as_unicode
Expand All @@ -32,12 +33,11 @@
QThread,
QStatusBar,
QSize,
QErrorMessage,
QMenu,
QIcon,
QCursor,
QUrl,
QDesktopServices,
QMessageBox,
)

from . import logger, PLUGIN_NAME, PLUGIN_ICON, __version__
Expand All @@ -52,6 +52,13 @@

load_translations()

ICON_MAP = {
"return": "arrow-go-back-fill.png",
"download": "download-line.png",
"ext-link": "external-link-line.png",
"refresh": "refresh-line.png",
}


class OverdriveLibbyAction(InterfaceAction):
name = PLUGIN_NAME
Expand Down Expand Up @@ -98,6 +105,15 @@ def __init__(self, gui, icon, do_user_config):
self.__loans_thread = QThread()
self.__curr_width = 0
self.__curr_height = 0
theme_folder = Path("images").joinpath(
"dark-theme" if is_dark_theme() else "light-theme"
)
self.icons = {}
for k, v in ICON_MAP.items():
self.icons[k] = theme_folder.joinpath(v)
icons_resources = get_icons([str(v) for v in self.icons.values()])
for k in ICON_MAP.keys():
self.icons[k] = icons_resources.pop(str(self.icons[k]))

libby_token = PREFS[PreferenceKeys.LIBBY_TOKEN]
if libby_token:
Expand All @@ -115,7 +131,8 @@ def __init__(self, gui, icon, do_user_config):
self.setWindowIcon(icon)
loan_view_span = 8

self.refresh_btn = QPushButton("\u21BB " + _("Refresh"), self)
self.refresh_btn = QPushButton(_("Refresh"), self)
self.refresh_btn.setIcon(self.icons["refresh"])
self.refresh_btn.setAutoDefault(False)
self.refresh_btn.setToolTip(_("Get latest loans"))
self.refresh_btn.clicked.connect(self.do_refresh)
Expand Down Expand Up @@ -153,7 +170,8 @@ def __init__(self, gui, icon, do_user_config):
self.loans_view.customContextMenuRequested.connect(self.loan_view_context_menu)
self.layout.addWidget(self.loans_view, 1, 0, 3, loan_view_span)

self.download_btn = QPushButton("\u2913 " + _("Download"), self)
self.download_btn = QPushButton(_("Download"), self)
self.download_btn.setIcon(self.icons["download"])
self.download_btn.setAutoDefault(False)
self.download_btn.setToolTip(_("Download selected loans"))
self.download_btn.setStyleSheet("padding: 4px 16px")
Expand Down Expand Up @@ -266,8 +284,9 @@ def download_selected_loans(self):
for row in reversed(rows):
self.download_loan(row.data(Qt.UserRole))
else:
d = QErrorMessage(self)
d.showMessage(_("Please select at least 1 loan."), "select_at_least_1_loan")
return error_dialog(
self, _("Download"), _("Please select at least 1 loan."), show=True
)

def download_loan(self, loan):
format_id = LibbyClient.get_loan_format(
Expand Down Expand Up @@ -411,14 +430,15 @@ def loan_view_context_menu(self, pos):
return
indices = selection_model.selectedRows()
menu = QMenu(self)
menu.addSection("Actions")
view_action = menu.addAction(_("View in Libby"))
view_action.setIcon(self.icons["ext-link"])
view_action.triggered.connect(lambda: self.open_loan_in_libby(indices))
return_action = menu.addAction(
ngettext("Return {n} loan", "Return {n} loans", len(indices)).format(
n=len(indices)
)
)
return_action.setIcon(self.icons["return"])
return_action.triggered.connect(lambda: self.return_selection(indices))
menu.exec(QCursor.pos())

Expand All @@ -440,17 +460,23 @@ def open_loan_in_libby(self, indices):
)

def return_selection(self, indices):
if (not PREFS[PreferenceKeys.CONFIRM_RETURNS]) or QMessageBox.question(
self,
_("Return Loans"),
msg = (
ngettext(
"Return this loan?", "Return these {n} loans?", len(indices)
).format(n=len(indices))
+ "\n- "
+ "\n- ".join(
[get_loan_title(index.data(Qt.UserRole)) for index in indices]
),
) == QMessageBox.Yes:
)
)

if confirm(
msg,
name=PreferenceKeys.CONFIRM_RETURNS,
parent=self,
title=_("Return Loans"),
config_set=PREFS,
):
for index in reversed(indices):
loan = index.data(Qt.UserRole)
self.return_loan(loan)
Expand Down
17 changes: 12 additions & 5 deletions calibre-plugin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
# information
#

from calibre import confirm_config_name
from calibre.gui2 import error_dialog
from calibre.utils.config import JSONConfig
from qt.core import Qt, QWidget, QGridLayout, QLabel, QCheckBox, QLineEdit

Expand Down Expand Up @@ -56,7 +58,7 @@ class PreferenceTexts:
PREFS.defaults[PreferenceKeys.MAIN_UI_HEIGHT] = 0
PREFS.defaults[PreferenceKeys.TAG_EBOOKS] = ""
PREFS.defaults[PreferenceKeys.TAG_MAGAZINES] = ""
PREFS.defaults[PreferenceKeys.CONFIRM_RETURNS] = True
PREFS.defaults[confirm_config_name(PreferenceKeys.CONFIRM_RETURNS)] = True


class ConfigWidget(QWidget):
Expand Down Expand Up @@ -180,7 +182,9 @@ def __init__(self, plugin_action):

# Always confirm returns
self.confirm_returns_checkbox = QCheckBox(PreferenceTexts.CONFIRM_RETURNS, self)
self.confirm_returns_checkbox.setChecked(PREFS[PreferenceKeys.CONFIRM_RETURNS])
self.confirm_returns_checkbox.setChecked(
PREFS[confirm_config_name(PreferenceKeys.CONFIRM_RETURNS)]
)
self.layout.addWidget(self.confirm_returns_checkbox, widget_row_pos, 0)
label_column_widths.append(
self.layout.itemAtPosition(widget_row_pos, 0).sizeHint().width()
Expand All @@ -202,7 +206,7 @@ def save_settings(self):
PREFS[PreferenceKeys.TAG_EBOOKS] = self.tag_ebooks_txt.text().strip()
PREFS[PreferenceKeys.TAG_MAGAZINES] = self.tag_magazines_txt.text().strip()
PREFS[
PreferenceKeys.CONFIRM_RETURNS
confirm_config_name(PreferenceKeys.CONFIRM_RETURNS)
] = self.confirm_returns_checkbox.isChecked()

setup_code = self.libby_setup_code_txt.text().strip()
Expand All @@ -212,8 +216,11 @@ def save_settings(self):

if not LibbyClient.is_valid_sync_code(setup_code):
# save a http request for get_chip()
raise Exception(
_("Invalid setup code format: {code}").format(code=setup_code)
return error_dialog(
self,
_("Libby Setup Code"),
_("Invalid setup code format: {code}").format(code=setup_code),
show=True,
)

libby_client = LibbyClient(logger=logger)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibre-plugin/images/dark-theme/download-line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibre-plugin/images/dark-theme/refresh-line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added calibre-plugin/images/light-theme/download-line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 83b16e2

Please sign in to comment.