Skip to content

Commit

Permalink
Check MTP device vendor id with MTP_DEVICE.current_vid
Browse files Browse the repository at this point in the history
Also don't try to copy Word Wise db file from MTP device, that
probably never works(allow access to `/system`).
  • Loading branch information
xxyzz committed Nov 29, 2023
1 parent 8de03cd commit 9a2b82e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
9 changes: 8 additions & 1 deletion custom_lemmas.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
)

from .error_dialogs import device_not_found_dialog, ww_db_not_found_dialog
from .send_file import copy_klld_from_android, copy_klld_from_kindle, device_connected
from .send_file import (
copy_klld_from_android,
copy_klld_from_kindle,
device_connected,
is_mtp_device,
)
from .utils import (
custom_lemmas_folder,
get_kindle_klld_path,
Expand Down Expand Up @@ -217,6 +222,8 @@ def check_empty_kindle_gloss(self) -> None:
if not package_name:
device_not_found_dialog(self)
return
if is_mtp_device(gui.device_manager.device):
return
custom_folder = custom_lemmas_folder(plugin_path, "en")
if isinstance(package_name, str):
copy_klld_from_android(package_name, custom_folder)
Expand Down
20 changes: 18 additions & 2 deletions send_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def send_files(self, job: Any) -> None:
)

def move_files_to_kindle(self, device_driver: Any, device_book_path: Path) -> None:
use_mtp = getattr(device_driver, "DEVICE_PLUGBOARD_NAME", "") == "MTP_DEVICE"
use_mtp = is_mtp_device(device_driver)
if not use_mtp:
# _main_prefix: Kindle mount point, /Volumes/Kindle
device_mount_point = Path(device_driver._main_prefix)
Expand Down Expand Up @@ -172,7 +172,16 @@ def push_files_to_android(self, adb_path: str) -> None:

def device_connected(gui: Any, book_fmt: str) -> str | bool:
if gui.device_manager.is_device_present:
is_kindle = getattr(gui.device_manager.device, "VENDOR_ID", None) == [0x1949]
is_kindle = False
device = gui.device_manager.device
if hasattr(device, "VENDOR_NAME"):
# Normal USB mass storage Kindle
is_kindle = device.VENDOR_NAME == "KINDLE"
elif hasattr(device, "current_vid"):
# Kindle Scribe, MTP vendor id is Amazon
# https://github.com/kovidgoyal/calibre/blob/475b0d3d2e6678dc4fd5441619f71a048c3806ea/src/calibre/devices/mtp/driver.py#L145
is_kindle = device.current_vid == 0x1949

if book_fmt == "EPUB":
if is_kindle:
kindle_epub_dialog(gui)
Expand All @@ -188,6 +197,13 @@ def device_connected(gui: Any, book_fmt: str) -> str | bool:
return False


def is_mtp_device(device_driver: Any) -> bool:
# https://github.com/kovidgoyal/calibre/blob/475b0d3d2e6678dc4fd5441619f71a048c3806ea/src/calibre/devices/mtp/driver.py#L49
if hasattr(device_driver, "DEVICE_PLUGBOARD_NAME"):
return device_driver.DEVICE_PLUGBOARD_NAME == "MTP_DEVICE"
return False


def adb_connected(adb_path: str) -> bool:
r = run_subprocess([adb_path, "devices"])
return r.stdout.decode().strip().endswith("device") if r else False
Expand Down

0 comments on commit 9a2b82e

Please sign in to comment.