Merge pull request #39 from norbusan/obok_linux

improvements in the obok device handling
This commit is contained in:
Apprentice Harper 2015-09-21 07:40:54 +01:00
commit b1cccf4b25
2 changed files with 53 additions and 26 deletions

View File

@ -34,6 +34,15 @@ from calibre_plugins.obok_dedrm.utilities import (
from calibre_plugins.obok_dedrm.obok.obok import KoboLibrary from calibre_plugins.obok_dedrm.obok.obok import KoboLibrary
from calibre_plugins.obok_dedrm.obok.legacy_obok import legacy_obok from calibre_plugins.obok_dedrm.obok.legacy_obok import legacy_obok
can_parse_xml = True
try:
from xml.etree import ElementTree as ET
debug_print("using xml.etree for xml parsing")
except ImportError:
can_parse_xml = False
debug_print("Cannot find xml.etree, disabling extraction of serial numbers")
PLUGIN_ICONS = ['images/obok.png'] PLUGIN_ICONS = ['images/obok.png']
try: try:
@ -80,21 +89,37 @@ class InterfacePluginAction(InterfaceAction):
print ('Running {}'.format(PLUGIN_NAME + ' v' + PLUGIN_VERSION)) print ('Running {}'.format(PLUGIN_NAME + ' v' + PLUGIN_VERSION))
# #
# search for connected device in case serials are saved # search for connected device in case serials are saved
tmpserials = cfg['kobo_serials']
device = self.parent().device_manager.connected_device device = self.parent().device_manager.connected_device
device_path = None device_path = None
if (device): if (device):
device_path = self.parent().device_manager.connected_device._main_prefix device_path = device._main_prefix
debug_print("get_device_settings - device_path=", device_path) debug_print("get_device_settings - device_path=", device_path)
# get serial from device_path/.adobe-digital-editions/device.xml
if can_parse_xml:
devicexml = os.path.join(device_path, '.adobe-digital-editions', 'device.xml')
debug_print("trying to load %s" % devicexml)
if (os.path.exists(devicexml)):
debug_print("trying to parse %s" % devicexml)
xmltree = ET.parse(devicexml)
for node in xmltree.iter():
if "deviceSerial" in node.tag:
serial = node.text
debug_print ("found serial %s" % serial)
tmpserials.append(serial)
break
else: else:
debug_print("didn't find device") debug_print("didn't find device")
# Get the Kobo Library object (obok v3.01) # Get the Kobo Library object (obok v3.01)
self.library = KoboLibrary(cfg['kobo_serials'], device_path) self.library = KoboLibrary(tmpserials, device_path)
debug_print ("got kobodir %s" % self.library.kobodir) debug_print ("got kobodir %s" % self.library.kobodir)
if (self.library.kobodir == ''): if (self.library.kobodir == ''):
# linux and no device connected, but could be extended # linux and no device connected, but could be extended
# to the case where on Windows/Mac the prog is not installed # to the case where on Windows/Mac the prog is not installed
msg = _('<p>Could not find Kobo Library\n<p>Windows/Mac: do you have Kobo Desktop installed?\n<p>Windows/Mac/Linux: In case you have an Kobo eInk device, configure the serial number and connect the device.') msg = _('<p>Could not find Kobo Library\n<p>Windows/Mac: do you have Kobo Desktop installed?\n<p>Windows/Mac/Linux: In case you have an Kobo eInk device, connect the device.')
showErrorDlg(msg, None) showErrorDlg(msg, None)
return return

View File

@ -248,29 +248,31 @@ class KoboLibrary(object):
print u"Obok v{0}\nCopyright © 2012-2015 Physisticated et al.".format(__version__) print u"Obok v{0}\nCopyright © 2012-2015 Physisticated et al.".format(__version__)
self.kobodir = '' self.kobodir = ''
kobodb = '' kobodb = ''
if sys.platform.startswith('win'):
if sys.getwindowsversion().major > 5: # - first check whether serials have been found or are provided
self.kobodir = os.environ['LOCALAPPDATA'] # and a device is connected. In this case, use the device
else: # - otherwise fall back to Kobo Desktop Application for Windows and Mac
self.kobodir = os.path.join(os.environ['USERPROFILE'], 'Local Settings', 'Application Data') if (device_path and (len(serials) > 0)):
self.kobodir = os.path.join(self.kobodir, 'Kobo', 'Kobo Desktop Edition') self.kobodir = os.path.join(device_path, '.kobo')
elif sys.platform.startswith('darwin'): # devices use KoboReader.sqlite
self.kobodir = os.path.join(os.environ['HOME'], 'Library', 'Application Support', 'Kobo', 'Kobo Desktop Edition') kobodb = os.path.join(self.kobodir, 'KoboReader.sqlite')
# desktop versions use Kobo.sqlite if (not(os.path.exists(kobodb))):
kobodb = os.path.join(self.kobodir, 'Kobo.sqlite') # give up here, we haven't found anything useful
if (self.kobodir == '' or not(os.path.exists(kobodb))): self.kobodir = ''
# kobodb is either not set or not an existing file, that means that either: kobodb = ''
# . windows or mac: desktop app is not installed
# . linux if (self.kobodir == ''):
# we check for a connected device and try to set up kobodir and kobodb from there # we haven't found a device with serials, so try desktop apps
if (device_path): if sys.platform.startswith('win'):
self.kobodir = os.path.join(device_path, '.kobo') if sys.getwindowsversion().major > 5:
# devices use KoboReader.sqlite self.kobodir = os.environ['LOCALAPPDATA']
kobodb = os.path.join(self.kobodir, 'KoboReader.sqlite') else:
if (not(os.path.exists(kobodb))): self.kobodir = os.path.join(os.environ['USERPROFILE'], 'Local Settings', 'Application Data')
# give up here, we haven't found anything useful self.kobodir = os.path.join(self.kobodir, 'Kobo', 'Kobo Desktop Edition')
self.kobodir = '' elif sys.platform.startswith('darwin'):
kobodb = '' self.kobodir = os.path.join(os.environ['HOME'], 'Library', 'Application Support', 'Kobo', 'Kobo Desktop Edition')
# desktop versions use Kobo.sqlite
kobodb = os.path.join(self.kobodir, 'Kobo.sqlite')
if (self.kobodir != ''): if (self.kobodir != ''):
self.bookdir = os.path.join(self.kobodir, 'kepub') self.bookdir = os.path.join(self.kobodir, 'kepub')