Skip to content

Commit

Permalink
Add ability to generate a new setup code for another device
Browse files Browse the repository at this point in the history
  • Loading branch information
ping committed Sep 5, 2023
1 parent 87427fd commit 815ccc9
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 336 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Unreleased
- New: Simple filter for titles and names in the Loans/Holds/Magazines/Cards tabs
- New: Copy the Libby share link for a book
- New: Verify a card
- New: Generate a setup code (for another device)
- Improve: Display linked identifiers, subjects in Book Details
- Improve: Infrequently changed data like libraries are now cached to give sync a small speed bump
- Improve: Card image in cards tab is not fuzzy anymore
Expand Down
62 changes: 61 additions & 1 deletion calibre-plugin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# See https://github.com/ping/libby-calibre-plugin for more
# information
#
import time
from typing import Tuple

from calibre import confirm_config_name
Expand Down Expand Up @@ -34,6 +35,7 @@
QVBoxLayout,
QWidget,
Qt,
QTimer,
)

from . import DEMO_MODE, PLUGIN_NAME, PLUGINS_FOLDER_NAME, logger
Expand All @@ -42,7 +44,7 @@

# noinspection PyUnreachableCode
if False:
load_translations = _ = lambda x=None: x
load_translations = _ = ngettext = lambda x=None, y=None, z=None: x

load_translations()

Expand Down Expand Up @@ -224,6 +226,24 @@ def __init__(self, plugin_action):
self.libby_setup_code_txt.setText(PREFS[PreferenceKeys.LIBBY_SETUP_CODE])
libby_layout.addRow(self.libby_setup_code_lbl, self.libby_setup_code_txt)

if is_configured:
generate_code_layout = QHBoxLayout()
self.generate_code_btn = QPushButton(_("Generate Setup Code"), self)
self.generate_code_btn.setToolTip(
_("Generate setup code for another device")
)
self.generate_code_btn.setMinimumWidth(150)
self.generate_code_btn.clicked.connect(self.generate_code_btn_clicked)
generate_code_layout.addWidget(self.generate_code_btn)
self.generated_code_lbl = QLabel(self)
self.generated_code_lbl.setTextInteractionFlags(
Qt.TextSelectableByKeyboard | Qt.TextSelectableByMouse
)
generate_code_layout.addWidget(self.generated_code_lbl)
self.time_remaining_lbl = QLabel(self)
generate_code_layout.addWidget(self.time_remaining_lbl, stretch=1)
libby_layout.addRow(generate_code_layout)

# ------------------------------------ LOANS ------------------------------------
loans_section = QGroupBox(_("Loans"))
loan_layout = QFormLayout()
Expand Down Expand Up @@ -647,6 +667,46 @@ def __init__(self, plugin_action):

self.resize(self.sizeHint())

def generate_code_btn_clicked(self):
from .libby import LibbyClient

client = LibbyClient(
identity_token=PREFS[PreferenceKeys.LIBBY_TOKEN],
max_retries=PREFS[PreferenceKeys.NETWORK_RETRY],
timeout=PREFS[PreferenceKeys.NETWORK_TIMEOUT],
logger=logger,
)
res = client.generate_clone_code()
generated_code = res.get("code", "")
expiry: int = res.get("expiry", 0)
if generated_code and expiry:
self.code_expiry = expiry
self.generated_code = generated_code
self.timer = QTimer(self)
self.timer.timeout.connect(self.timer_update)
self.generate_code_btn.setEnabled(False)
self.generated_code_lbl.setText(f"<b>{self.generated_code}</b>")
self.timer_update()
self.timer.start(1000)

def timer_update(self):
remaining_seconds = self.code_expiry - int(time.time())
if remaining_seconds <= 0:
self.timer.stop()
self.generate_code_btn.setEnabled(True)
self.generated_code_lbl.setText("")
self.time_remaining_lbl.setText("")
self.generated_code = ""
self.code_expiry = 0
else:
self.time_remaining_lbl.setText(
ngettext(
"{remaining} second remaining",
"{remaining} seconds remaining",
remaining_seconds,
).format(remaining=remaining_seconds)
)

def custom_column_name(self, col_type: str):
for c in (" ",):
col_type = col_type.replace(c, "_")
Expand Down
9 changes: 9 additions & 0 deletions calibre-plugin/libby/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,15 @@ def clone_by_code(self, code: str) -> Dict:
res: Dict = self.send_request("chip/clone/code", params={"code": code})
return res

def generate_clone_code(self):
"""
Get a clone code for setting up another device
:return:
"""
res: Dict = self.send_request("chip/clone/code")
return res

def sync(self) -> Dict:
"""
Get the user account state, which includes loans, holds, etc.
Expand Down

0 comments on commit 815ccc9

Please sign in to comment.