1'''
2Created on 15 May 2010
3
4@author: charles
5'''
6import os
7
8from calibre.devices.usbms.driver import USBMS, BookList
9from calibre.ebooks import BOOK_EXTENSIONS
10
11# This class is added to the standard device plugin chain, so that it can
12# be configured. It has invalid vendor_id etc, so it will never match a
13# device. The 'real' FOLDER_DEVICE will use the config from it.
14
15
16class FOLDER_DEVICE_FOR_CONFIG(USBMS):
17    name           = 'Folder Device Interface'
18    gui_name       = 'Folder Device'
19    description    = _('Use an arbitrary folder as a device.')
20    author         = 'John Schember/Charles Haley'
21    supported_platforms = ['windows', 'osx', 'linux']
22    FORMATS     = list(BOOK_EXTENSIONS) + ['ppt', 'pptx']
23
24    VENDOR_ID   = [0xffff]
25    PRODUCT_ID  = [0xffff]
26    BCD         = [0xffff]
27    DEVICE_PLUGBOARD_NAME = 'FOLDER_DEVICE'
28    SUPPORTS_SUB_DIRS = True
29
30
31class FOLDER_DEVICE(USBMS):
32    type = _('Device interface')
33
34    name           = 'Folder Device Interface'
35    gui_name       = 'Folder Device'
36    description    = _('Use an arbitrary folder as a device.')
37    author         = 'John Schember/Charles Haley'
38    supported_platforms = ['windows', 'osx', 'linux']
39    FORMATS     = FOLDER_DEVICE_FOR_CONFIG.FORMATS
40
41    VENDOR_ID   = [0xffff]
42    PRODUCT_ID  = [0xffff]
43    BCD         = [0xffff]
44    DEVICE_PLUGBOARD_NAME = 'FOLDER_DEVICE'
45
46    THUMBNAIL_HEIGHT = 68  # Height for thumbnails on device
47
48    CAN_SET_METADATA = ['title', 'authors']
49    SUPPORTS_SUB_DIRS = True
50
51    #: Icon for this device
52    icon = I('devices/folder.png')
53    METADATA_CACHE = '.metadata.calibre'
54    DRIVEINFO = '.driveinfo.calibre'
55
56    _main_prefix = ''
57    _card_a_prefix = None
58    _card_b_prefix = None
59
60    is_connected = False
61
62    def __init__(self, path):
63        if not os.path.isdir(path):
64            raise OSError('Path is not a folder')
65        path = USBMS.normalize_path(path)
66        if path.endswith(os.sep):
67            self._main_prefix = path
68        else:
69            self._main_prefix = path + os.sep
70        self.booklist_class = BookList
71        self.is_connected = True
72
73    def reset(self, key='-1', log_packets=False, report_progress=None,
74              detected_device=None):
75        pass
76
77    def unmount_device(self):
78        self._main_prefix = ''
79        self.is_connected = False
80
81    def is_usb_connected(self, devices_on_system, debug=False,
82            only_presence=False):
83        return self.is_connected, self
84
85    def open(self, connected_device, library_uuid):
86        self.current_library_uuid = library_uuid
87        if not self._main_prefix:
88            return False
89        return True
90
91    def set_progress_reporter(self, report_progress):
92        self.report_progress = report_progress
93
94    def card_prefix(self, end_session=True):
95        return (None, None)
96
97    def eject(self):
98        self.is_connected = False
99
100    @classmethod
101    def settings(self):
102        return FOLDER_DEVICE_FOR_CONFIG._config().parse()
103
104    @classmethod
105    def config_widget(cls):
106        return FOLDER_DEVICE_FOR_CONFIG.config_widget()
107
108    @classmethod
109    def save_settings(cls, config_widget):
110        return FOLDER_DEVICE_FOR_CONFIG.save_settings(config_widget)
111