-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.py
345 lines (299 loc) · 11.8 KB
/
config.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
"""Config for KOReader Sync plugin for Calibre."""
import math
import os
import json
from functools import partial
from PyQt5.Qt import (
QComboBox,
QCheckBox,
QGridLayout,
QHBoxLayout,
QLabel,
QVBoxLayout,
QWidget,
Qt,
)
from PyQt5.QtGui import QPixmap
from calibre.constants import numeric_version
from calibre.devices.usbms.driver import debug_print as root_debug_print
from calibre.utils.config import JSONConfig
from calibre_plugins.koreader import clean_bookmarks
__license__ = 'GNU GPLv3'
__copyright__ = '2021, harmtemolder <mail at harmtemolder.com>'
__modified_by__ = 'kyxap kyxappp@gmail.com'
__modification_date__ = '2024'
__docformat__ = 'restructuredtext en'
SUPPORTED_DEVICES = [
'FOLDER_DEVICE',
'KINDLE2',
'KOBO',
'KOBOTOUCH',
'KOBOTOUCHEXTENDED',
'POCKETBOOK622',
'POCKETBOOK626',
'SMART_DEVICE_APP',
'TOLINO',
'USER_DEFINED',
'POCKETBOOK632',
]
UNSUPPORTED_DEVICES = [
'MTP_DEVICE',
]
COLUMNS = [{
'name': 'column_percent_read',
'label': 'Percent read column (float):',
'tooltip': 'A "Floating point numbers" column to store the current\n'
'percent read, with "Format for numbers" set to `{:.0%}`.',
'type': 'float',
'sidecar_property': ['percent_finished'],
'transform': (lambda value: float(value))
}, {
'name': 'column_percent_read_int',
'label': 'Percent read column (int):',
'tooltip': 'An "Integers" column to store the current percent read.',
'type': 'int',
'sidecar_property': ['percent_finished'],
'transform': (lambda value: math.floor(float(value) * 100))
}, {
'name': 'column_last_read_location',
'label': 'Last read location column:',
'tooltip': 'A regular "Text" column to store the location you last\n'
'stopped reading at.',
'type': 'text',
'sidecar_property': ['last_xpointer'],
}, {
'name': 'column_rating',
'label': 'Rating column:',
'tooltip': 'A "Rating" column to store your rating of the book,\n'
'as entered on the book’s status page.',
'type': 'rating',
'sidecar_property': ['summary', 'rating'],
}, {
'name': 'column_review',
'label': 'Review column:',
'tooltip': 'A "Long text" column to store your review of the book,\n'
'as entered on the book’s status page.',
'type': 'comments',
'sidecar_property': ['summary', 'note'],
}, {
'name': 'column_status',
'label': 'Reading status column (text):',
'tooltip': 'A regular "Text" column to store the reading status of the\n'
'book, as entered on the book status page ("Finished",\n'
'"Reading", "On hold").',
'type': 'text',
'sidecar_property': ['summary', 'status'],
}, {
'name': 'column_status_bool',
'label': 'Reading status column (yes/no):',
'tooltip': 'A "Yes/No" column to store the reading status of the book,\n'
'as a boolean ("Yes" = "Finished", "No" = everything else).',
'type': 'bool',
'sidecar_property': ['summary', 'status'],
'transform': (lambda val: bool(val == 'complete')),
}, {
'name': 'column_bookmarks',
'label': 'Bookmarks column',
'tooltip': 'A "Long text" column to store your bookmarks and\n'
'highlights.',
'type': 'comments',
'sidecar_property': ['annotations'],
'transform': clean_bookmarks,
}, {
'name': 'column_md5',
'label': 'MD5 hash column:',
'tooltip': 'A regular "Text" column to store the MD5 hash KOReader uses\n'
'to sync progress to a KOReader Sync Server. ("Progress sync"\n'
'in the KOReader app.) This might allow for syncing progress\n'
'to calibre without having to connect your KOReader device,\n'
'in the future.',
'type': 'text',
'sidecar_property': ['partial_md5_checksum'],
}, {
'name': 'column_date_synced',
'label': 'Date Synced column:',
'tooltip': 'A "Date" column to store when the last sync was performed.',
'type': 'datetime',
'sidecar_property': ['calculated', 'date_synced'],
}, {
'name': 'column_sidecar',
'label': 'Raw sidecar column:',
'tooltip': 'A "Long text" column to store the contents of the\n'
'metadata sidecar as JSON, with "Interpret this column as" set to\n'
'"Plain text". This is required to sync metadata back to KOReader sidecars.',
'type': 'comments',
'sidecar_property': [], # `[]` gives the entire sidecar dict
'transform': (lambda d: json.dumps(
{k: d[k] for k in d if k != 'calculated'},
skipkeys=True,
indent=2,
default=str
)),
}]
CHECKBOXES = [{
'name': 'checkbox_sync_if_more_recent',
'label': 'Sync only if changes are more recent:',
'tooltip': 'Sync book only if the metadata is more recent. Requires\n'
'"Date Modified Column" or "Percent read column" to be synced',
}, {
'name': 'checkbox_no_sync_if_finished',
'label': 'No sync if book has already been finished:',
'tooltip': 'Do not sync book if it has already been finished. Requires\n'
'"Percent read column" or "Reading status column" to be synced',
}]
CONFIG = JSONConfig(os.path.join('plugins', 'KOReader Sync.json'))
for this_column in COLUMNS:
CONFIG.defaults[this_column['name']] = ''
for this_checkbox in CHECKBOXES:
CONFIG.defaults[this_checkbox['name']] = False
if numeric_version >= (5, 5, 0):
module_debug_print = partial(root_debug_print, ' koreader:config:', sep='')
else:
module_debug_print = partial(root_debug_print, 'koreader:config:')
class ConfigWidget(QWidget): # https://doc.qt.io/qt-5/qwidget.html
def __init__(self, plugin_action):
QWidget.__init__(self)
debug_print = partial(module_debug_print, 'ConfigWidget:__init__:')
debug_print('start')
self.action = plugin_action
# Set up main layout
layout = QVBoxLayout()
self.setLayout(layout)
# Add icon and title
title_layout = TitleLayout(
self,
'images/icon.png',
f'Configure {self.action.version}',
)
layout.addLayout(title_layout)
# Add custom column dropdowns
custom_columns_layout = CustomColumnsLayout(self)
layout.addLayout(custom_columns_layout)
# Add custom checkboxes
custom_checkbox_layout = CustomCheckboxLayout(self)
layout.addLayout(custom_checkbox_layout)
def save_settings(self):
debug_print = partial(module_debug_print,
'ConfigWidget:save_settings:')
debug_print('old CONFIG = ', CONFIG)
for column in COLUMNS:
CONFIG[column['name']] = column['combo'].get_selected_column()
for checkbox in CHECKBOXES:
CONFIG[checkbox['name']] = checkbox['checkbox'].checkState() == Qt.Checked
debug_print('new CONFIG = ', CONFIG)
class TitleLayout(QHBoxLayout):
"""A sub-layout to the main layout used in ConfigWidget that contains an
icon and title.
"""
def __init__(self, parent, icon, title):
QHBoxLayout.__init__(self)
# Add icon
icon_label = QLabel(parent)
pixmap = QPixmap()
pixmap.loadFromData(get_resources(icon))
icon_label.setPixmap(pixmap)
icon_label.setMaximumSize(64, 64)
icon_label.setScaledContents(True)
self.addWidget(icon_label)
# Add title
title_label = QLabel(f'<h2>{title}</h2>', parent)
self.addWidget(title_label)
# Add empty space
self.addStretch()
# Add Readme hyperlink
readme_label = QLabel('<a href="#">Readme</a>', parent)
readme_label.setTextInteractionFlags(
Qt.LinksAccessibleByMouse | Qt.LinksAccessibleByKeyboard)
readme_label.linkActivated.connect(parent.action.show_readme)
self.addWidget(readme_label)
# Add About hyperlink
about_label = QLabel('<a href="#">About</a>', parent)
about_label.setTextInteractionFlags(
Qt.LinksAccessibleByMouse | Qt.LinksAccessibleByKeyboard)
about_label.linkActivated.connect(parent.action.show_about)
self.addWidget(about_label)
class CustomColumnsLayout(QGridLayout):
"""A sub-layout to the main layout used in ConfigWidget that contains a
grid of dropdowns for the mapping from KOReader metadata properties to
calibre’s custom columns.
"""
def __init__(self, parent):
QGridLayout.__init__(self)
self.action = parent.action
row = 1
# Get available columns per type
available_columns = {
'comments': self.get_custom_columns(['comments']),
'datetime': self.get_custom_columns(['datetime']),
'float': self.get_custom_columns(['float']),
'int': self.get_custom_columns(['int']),
'rating': self.get_rating_columns(), # Includes built-in
'text': self.get_custom_columns(['text']),
'bool': self.get_custom_columns(['bool']),
}
# Add custom column dropdowns
for column in COLUMNS:
label = QLabel(column['label'], parent)
label.setToolTip(column['tooltip'])
column['combo'] = CustomColumnComboBox(
parent,
available_columns[column['type']],
CONFIG[column['name']])
label.setBuddy(column['combo'])
self.addWidget(label, row, 1, Qt.AlignRight)
self.addWidget(column['combo'], row, 2, 1, 2)
row += 1
def get_custom_columns(self, column_types):
custom_columns = self.action.gui.library_view.model().custom_columns
available_columns = {}
for key, column in custom_columns.items():
type_ = column['datatype']
if type_ in column_types and not column['is_multiple']:
available_columns[key] = column
return available_columns
def get_rating_columns(self):
rating_columns = self.get_custom_columns(['rating'])
# Add built-in rating column as well
rating_column_name = self.action.gui.library_view.model().orig_headers[
'rating']
rating_columns['rating'] = {'name': rating_column_name}
return rating_columns
class CustomCheckboxLayout(QGridLayout):
"""A sub-layout to the main layout used in ConfigWidget that contains a
grid of checkboxes for various settings.
"""
def __init__(self, parent):
QGridLayout.__init__(self)
self.action = parent.action
row = 1
# Add custom cehckboxes
for checkbox in CHECKBOXES:
label = QLabel(checkbox['label'], parent)
label.setToolTip(checkbox['tooltip'])
checkbox['checkbox'] = QCheckBox()
checkbox['checkbox'].setCheckState(Qt.Checked if CONFIG[checkbox['name']] else Qt.Unchecked)
label.setBuddy(checkbox['checkbox'])
self.addWidget(label, row, 1, Qt.AlignRight)
self.addWidget(checkbox['checkbox'], row, 2, 1, 2)
row += 1
class CustomColumnComboBox(QComboBox):
def __init__(self, parent, custom_columns=None, selected_column=''):
QComboBox.__init__(self, parent)
if custom_columns is None:
custom_columns = {}
self.populate_combo(custom_columns, selected_column)
def populate_combo(self, custom_columns, selected_column):
self.clear()
self.column_names = ['']
self.addItem('do not sync')
selected_idx = 0
for key in sorted(custom_columns.keys()):
self.column_names.append(key)
display_name = f'{custom_columns[key]["name"]} ({key})'
self.addItem(display_name)
if key == selected_column:
selected_idx = len(self.column_names) - 1
self.setCurrentIndex(selected_idx)
def get_selected_column(self):
return self.column_names[self.currentIndex()]