Add a device name to the driveinfo structure, along with the API to set it.

This commit is contained in:
Charles Haley 2011-02-28 16:20:42 +00:00
parent f348e2594c
commit 160e57ed54
3 changed files with 29 additions and 4 deletions

View File

@ -447,6 +447,15 @@ class DevicePlugin(Plugin):
''' '''
pass pass
def set_driveinfo_name(self, location_code, name):
'''
Set the device name in the driveinfo file to 'name'. This setting will
persist until the file is re-created or the name is changed again.
Non-disk devices will ignore this request.
'''
pass
class BookList(list): class BookList(list):
''' '''
A list of books. Each Book object must have the fields A list of books. Each Book object must have the fields

View File

@ -55,29 +55,33 @@ class USBMS(CLI, Device):
METADATA_CACHE = 'metadata.calibre' METADATA_CACHE = 'metadata.calibre'
DRIVEINFO = 'driveinfo.calibre' DRIVEINFO = 'driveinfo.calibre'
def _update_driveinfo_record(self, dinfo, prefix): def _update_driveinfo_record(self, dinfo, prefix, name=None):
if not isinstance(dinfo, dict): if not isinstance(dinfo, dict):
dinfo = {} dinfo = {}
if dinfo.get('device_store_uuid', None) is None: if dinfo.get('device_store_uuid', None) is None:
dinfo['device_store_uuid'] = unicode(uuid.uuid4()) dinfo['device_store_uuid'] = unicode(uuid.uuid4())
if dinfo.get('device_name') is None:
dinfo['device_name'] = self.get_gui_name()
if name is not None:
dinfo['device_name'] = name
dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None) dinfo['last_library_uuid'] = getattr(self, 'current_library_uuid', None)
dinfo['calibre_version'] = '.'.join([unicode(i) for i in numeric_version]) dinfo['calibre_version'] = '.'.join([unicode(i) for i in numeric_version])
dinfo['date_last_connected'] = unicode(now()) dinfo['date_last_connected'] = unicode(now())
dinfo['prefix'] = prefix.replace('\\', '/') dinfo['prefix'] = prefix.replace('\\', '/')
return dinfo return dinfo
def _update_driveinfo_file(self, prefix): def _update_driveinfo_file(self, prefix, name=None):
if os.path.exists(os.path.join(prefix, self.DRIVEINFO)): if os.path.exists(os.path.join(prefix, self.DRIVEINFO)):
with open(os.path.join(prefix, self.DRIVEINFO), 'rb') as f: with open(os.path.join(prefix, self.DRIVEINFO), 'rb') as f:
try: try:
driveinfo = json.loads(f.read(), object_hook=from_json) driveinfo = json.loads(f.read(), object_hook=from_json)
except: except:
driveinfo = None driveinfo = None
driveinfo = self._update_driveinfo_record(driveinfo, prefix) driveinfo = self._update_driveinfo_record(driveinfo, prefix, name)
with open(os.path.join(prefix, self.DRIVEINFO), 'wb') as f: with open(os.path.join(prefix, self.DRIVEINFO), 'wb') as f:
f.write(json.dumps(driveinfo, default=to_json)) f.write(json.dumps(driveinfo, default=to_json))
else: else:
driveinfo = self._update_driveinfo_record({}, prefix) driveinfo = self._update_driveinfo_record({}, prefix, name)
with open(os.path.join(prefix, self.DRIVEINFO), 'wb') as f: with open(os.path.join(prefix, self.DRIVEINFO), 'wb') as f:
f.write(json.dumps(driveinfo, default=to_json)) f.write(json.dumps(driveinfo, default=to_json))
return driveinfo return driveinfo
@ -93,6 +97,14 @@ class USBMS(CLI, Device):
self.driveinfo['B'] = self._update_driveinfo_file(self._card_b_prefix) self.driveinfo['B'] = self._update_driveinfo_file(self._card_b_prefix)
return (self.get_gui_name(), '', '', '', self.driveinfo) return (self.get_gui_name(), '', '', '', self.driveinfo)
def set_driveinfo_name(self, location_code, name):
if location_code == 'main':
self._update_driveinfo_file(self._main_prefix, name)
elif location_code == 'A':
self._update_driveinfo_file(self._card_a_prefix, name)
elif location_code == 'B':
self._update_driveinfo_file(self._card_b_prefix, name)
def books(self, oncard=None, end_session=True): def books(self, oncard=None, end_session=True):
from calibre.ebooks.metadata.meta import path_to_ext from calibre.ebooks.metadata.meta import path_to_ext

View File

@ -430,6 +430,10 @@ class DeviceManager(Thread): # {{{
def set_current_library_uuid(self, uuid): def set_current_library_uuid(self, uuid):
self.current_library_uuid = uuid self.current_library_uuid = uuid
def set_driveinfo_name(self, location_code, name):
if self.connected_device:
self.connected_device.set_driveinfo_name(location_code, name)
# }}} # }}}
class DeviceAction(QAction): # {{{ class DeviceAction(QAction): # {{{