-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmarteject_plugin.py
177 lines (144 loc) · 8.82 KB
/
smarteject_plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import (unicode_literals, division, absolute_import,
print_function)
__license__ = 'GPL v3'
__copyright__ = '2019, Jim Miller'
__docformat__ = 'restructuredtext en'
from calibre.gui2 import question_dialog
# The class that all interface action plugins must inherit from
from calibre.gui2.actions import InterfaceAction
from calibre_plugins.smarteject.common_utils import get_icon
from calibre_plugins.smarteject.config import prefs, default_prefs
# pulls in translation files for _() strings
try:
load_translations()
except NameError:
pass # load_translations() added in calibre 1.9
# PLUGIN_ICONS = ['images/icon.png']
class SmartEjectPlugin(InterfaceAction):
name = 'SmartEject'
# Declare the main action associated with this plugin
# The keyboard shortcut can be None if you dont want to use a keyboard
# shortcut. Remember that currently calibre has no central management for
# keyboard shortcuts, so try to use an unusual/unused shortcut.
# (text, icon_path, tooltip, keyboard shortcut)
# icon_path isn't in the zip--icon loaded below.
action_spec = (_('SmartEject'), None,
_('Check for duplicated/deleted/added books before ejecting.'), ())
# None for keyboard shortcut doesn't allow shortcut. () does, there just isn't one yet
action_type = 'global'
# make button menu drop down only
#popup_type = QToolButton.InstantPopup
#: Set of locations to which this action must not be added.
#: See :attr:`all_locations` for a list of possible locations
dont_add_to = frozenset(['toolbar', 'context-menu', 'toolbar-child',
'context-menu-device', 'menubar',
'context-menu-cover-browser'])
def genesis(self):
# This method is called once per plugin, do initial setup here
base = self.interface_action_base_plugin
self.version = base.name+" v%d.%d.%d"%base.version
# Set the icon for this interface action
# The get_icons function is a builtin function defined for all your
# plugin code. It loads icons from the plugin zip file. It returns
# QIcon objects, if you want the actual data, use the analogous
# get_resources builtin function.
# Note that if you are loading more than one icon, for performance, you
# should pass a list of names to get_icons. In this case, get_icons
# will return a dictionary mapping names to QIcons. Names that
# are not found in the zip file will result in null QIcons.
icon = get_icon('eject.png')
self.qaction.setText(_('SmartEject'))
# The qaction is automatically created from the action_spec defined
# above
self.qaction.setIcon(icon)
# Call function when plugin triggered.
self.qaction.triggered.connect(self.plugin_button)
def plugin_button(self):
if not self.gui.device_manager.is_device_present:
# no device connected, silently skip.
return
if 'Reading List' in self.gui.iactions and ( prefs['checkreadinglistsync']
or prefs['checkreadinglistsyncfromdevice']):
rl_plugin = self.gui.iactions['Reading List']
list_names = rl_plugin.get_list_names(exclude_auto=True)
all_list_names = rl_plugin.get_list_names(exclude_auto=False)
auto_list_names = list(set(all_list_names) - set(list_names))
if self.gui.device_manager.is_device_connected:
sync_total = rl_plugin._count_books_for_connected_device()
## why is this setting the enabled for RL?
## Probably RL's rebuild_menus hasn't been called
# print(all_list_names)
# print(auto_list_names)
# print(sync_total)
rl_plugin.sync_now_action.setEnabled(bool(sync_total > 0) or len(auto_list_names) > 0)
if sync_total > 0 and prefs['checkreadinglistsync']:
if question_dialog(self.gui, _("Sync Reading List?"), _("There are books that need syncing according to Reading List.<p>Sync Books?"), show_copy_button=False):
rl_plugin.sync_now(force_sync=True)
return
elif len(auto_list_names) > 0 and prefs['checkreadinglistsyncfromdevice']:
if prefs['silentsyncfromdevice'] or question_dialog(self.gui, _("Sync Now Reading List?"), _("There are lists that could be sync'ed according to Reading List.<p>Sync before ejecting?"), show_copy_button=False):
# print("doreadinglistsync")
rl_plugin.sync_now(force_sync=True)
if prefs['checkdups']:
# As of Calibre 5.42, the duplicate search needs to change.
# Automatically change it if it's the old default search.
if prefs['checkdups_search'] == 'ondevice:"("':
prefs['checkdups_search'] = default_prefs['checkdups_search']
print("checkdups_search changed to new default value.")
prefs.save_to_db()
if self.gui.library_view.model().db.search_getting_ids(prefs['checkdups_search'], None):
if question_dialog(self.gui, _("Duplicates on Device"), _("There are duplicate ebooks on the device.<p>Display duplicates?"), show_copy_button=False):
# print("Duplicates on Device warning:%s"%warning_dialog(self.gui, "Duplicates on Device", "There are duplicate ebooks on the device.", show=True, show_copy_button=False))
self.gui.location_manager._location_selected('library')
self.gui.search.setEditText(prefs['checkdups_search'])
self.gui.search.do_search()
return
if prefs['checknotinlibrary'] and self.checkdevice(self.gui.memory_view.model(),"Main"):
self.gui.location_manager._location_selected('main')
return
if prefs['checknotinlibrary'] and self.checkdevice(self.gui.card_a_view.model(),"Card A"):
self.gui.location_manager._location_selected('carda')
return
if prefs['checknotinlibrary'] and self.checkdevice(self.gui.card_b_view.model(),"Card B"):
self.gui.location_manager._location_selected('cardb')
return
if prefs['checknotondevice'] and self.gui.library_view.model().db.search_getting_ids(prefs['checknotondevice_search'], None):
if question_dialog(self.gui,
_("Books in Library not on Device"),
_("There are books in the Library that are not on the Device.<p>Display books not on Device?"),
show_copy_button=False):
self.gui.location_manager._location_selected('library')
self.gui.search.setEditText(prefs['checknotondevice_search'])
self.gui.search.do_search()
return
self.gui.location_manager._location_selected('library')
from calibre.gui2.device import device_name_for_plugboards
device_name = device_name_for_plugboards(self.gui.device_manager.connected_device.__class__)
# print(device_name)
self.gui.location_manager._eject_requested()
if prefs['stopsmartdevice'] and 'SMART_DEVICE_APP' in device_name:
self.gui.device_manager.stop_plugin('smartdevice')
# if one of the configured searchs, clear it.
#print("self.gui.search.current_text :(%s)"%self.gui.search.current_text )
if self.gui.search.current_text in (prefs['checkdups_search'],prefs['checknotinlibrary_search'],prefs['checknotondevice_search']):
self.gui.search.clear()
def checkdevice(self,model,loc):
savesearch = model.last_search
#print("\nmodel.last_search:%s"%model.last_search)
#print("model.count():%s"%model.count())
model.search(prefs['checknotinlibrary_search'])
#print("model.count():%s"%model.count())
if model.count() > 0:
if question_dialog(self.gui, _("Books on Device not in Library"), _("There are books on the device in %s that are not in the Library.<p>Display books not in Library?")%loc, show_copy_button=False):
# #print("not in Library warning:%s"%warning_dialog(self.gui, "Books on Device not in Library", "There are books on the device not in Library.", show=True, show_copy_button=False))
self.gui.search.setEditText(prefs['checknotinlibrary_search'])
self.gui.search.do_search()
return True
model.search(savesearch)
#print("model.count():%s"%model.count())
return False
def apply_settings(self):
# No need to do anything with prefs here, but we could.
prefs