| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from uuid import short_uuid |
| 6 |
|
| 7 |
from ajax import ajax_send |
| 8 |
|
| 9 |
all_settings = { |
| 10 |
'copy_to_library_dupes': {'default': 'add;overwrite', 'category': 'book_list'}, |
| 11 |
'last_sort_order': {'default': {}, 'category': 'book_list'}, |
| 12 |
'show_all_metadata': {'default': False, 'category': 'book_list'}, # show all metadata fields in the book details panel |
| 13 |
'show_nav_list_book_count': {'default': True, 'category': 'book_list', 'is_local': False}, |
| 14 |
'show_nav_list_avg_rating': {'default': True, 'category': 'book_list', 'is_local': False}, |
| 15 |
'show_sidebar_in_book_viewer': {'default': False, 'category': 'book_list', 'is_local': False}, |
| 16 |
# auto = follow OS; dark/light = force content-server chrome (book list, sidebar, modals). |
| 17 |
'book_list_color_scheme': {'default': 'auto', 'category': 'book_list', 'is_local': False}, |
| 18 |
'ui_highlight_colors': {'default': {'light': '#437000', 'dark': '#6cb4ee'}, 'category': 'book_list', 'is_local': False}, |
| 19 |
'series_nav_display_mode': {'default': 'stack', 'category': 'book_list', 'is_local': False}, |
| 20 |
'saved_virtual_libraries': {'default': [], 'category': 'book_list', 'is_local': False}, |
| 21 |
'browse_visible_fields': {'default': {}, 'category': 'book_list', 'is_local': False}, |
| 22 |
'sort': {'default': 'timestamp.desc', 'category': 'book_list'}, # comma separated list of items of the form: field.order |
| 23 |
'view_mode': {'default': 'cover_grid', 'category': 'book_list'}, |
| 24 |
# Cover grid tile width as % of viewport axis (portrait: vw, landscape: vh), integer ~8–45. |
| 25 |
'cover_grid_tile_pct': {'default': 17, 'category': 'book_list'}, |
| 26 |
'fts_related_words': {'default': True, 'category': 'book_list'}, |
| 27 |
# Tag Browser settings |
| 28 |
'and_search_terms': {'default': False, 'category': 'tag_browser'}, # how to add search terms to the search expression from the Tag Browser |
| 29 |
'collapse_at': {'default': 25, 'category': 'tag_browser'}, # number of items at which sub-groups are created, 0 to disable |
| 30 |
'dont_collapse': {'default': '', 'category': 'tag_browser'}, # comma separated list of category names |
| 31 |
'hide_empty_categories': {'default': 'no', 'category': 'tag_browser'}, |
| 32 |
'partition_method': {'default': 'first letter', 'category': 'tag_browser'}, # other choices: 'disable', 'partition' |
| 33 |
'sort_tags_by': {'default': 'name', 'category': 'tag_browser'}, # other choices: popularity, rating |
| 34 |
# Book reader settings |
| 35 |
'background_image_fade': {'default': 0, 'category': 'read_book', 'is_local': True}, |
| 36 |
'background_image_style': {'default': 'scaled', 'category': 'read_book', 'is_local': True}, |
| 37 |
'background_image': {'default': None, 'category': 'read_book', 'is_local': True}, |
| 38 |
'base_font_size': {'default': 16, 'category': 'read_book', 'is_local': True}, |
| 39 |
'book_scrollbar': {'default': False, 'category': 'read_book'}, |
| 40 |
'columns_per_screen': {'default': {'portrait': 0, 'landscape': 0}, 'category': 'read_book', 'is_local': True}, |
| 41 |
'controls_help_shown_count': {'default': 0, 'category': 'read_book', 'is_local': True, 'disallowed_in_profile': True}, |
| 42 |
'controls_help_shown_count_rtl_page_progression': {'default': 0, 'category': 'read_book', 'is_local': True, 'disallowed_in_profile': True}, |
| 43 |
'cover_preserve_aspect_ratio': {'default': True, 'category': 'read_book'}, |
| 44 |
'current_color_scheme': {'default': 'system', 'category': 'read_book'}, |
| 45 |
'footer': {'default': {'left': 'menu-icon-on-hover', 'right': 'progress'}, 'category': 'read_book'}, |
| 46 |
'header': {'default': {'right': 'menu-icon-on-hover'}, 'category': 'read_book'}, |
| 47 |
'controls_footer': {'default': {'right': 'progress'}, 'category': 'read_book'}, |
| 48 |
'left-margin': {'default': {}, 'category': 'read_book'}, |
| 49 |
'right-margin': {'default': {}, 'category': 'read_book'}, |
| 50 |
'hide_tooltips': {'default': False, 'category': 'read_book'}, |
| 51 |
'keyboard_shortcuts': {'default': {}, 'category': 'read_book'}, |
| 52 |
'lines_per_sec_auto': {'default': 1, 'category': 'read_book', 'is_local': True}, |
| 53 |
'lines_per_sec_smooth': {'default': 20, 'category': 'read_book', 'is_local': True}, |
| 54 |
'margin_bottom': {'default': 20, 'category': 'read_book', 'is_local': True}, |
| 55 |
'margin_left': {'default': 20, 'category': 'read_book', 'is_local': True}, |
| 56 |
'margin_right': {'default': 20, 'category': 'read_book', 'is_local': True}, |
| 57 |
'margin_top': {'default': 20, 'category': 'read_book', 'is_local': True}, |
| 58 |
'max_text_height': {'default': 0, 'category': 'read_book', 'is_local': True}, |
| 59 |
'max_text_width': {'default': 0, 'category': 'read_book', 'is_local': True}, |
| 60 |
'override_book_colors': {'default': 'never', 'category': 'read_book'}, |
| 61 |
'paged_margin_clicks_scroll_by_screen': {'default': True, 'category': 'read_book'}, |
| 62 |
'paged_wheel_scrolls_by_screen': {'default': False, 'category': 'read_book'}, |
| 63 |
'paged_wheel_section_jumps': {'default': True, 'category': 'read_book'}, |
| 64 |
'paged_pixel_scroll_threshold': {'default': 60, 'category': 'read_book'}, |
| 65 |
'read_mode': {'default': 'paged', 'category': 'read_book', 'is_local': True}, |
| 66 |
'scroll_auto_boundary_delay': {'default': 5, 'category': 'read_book', 'is_local': True}, |
| 67 |
'scroll_mouse_boundary_delay': {'default': 0.5, 'category': 'read_book', 'is_local': True}, |
| 68 |
'scroll_stop_boundaries': {'default': False, 'category': 'read_book', 'is_local': True}, |
| 69 |
'standalone_font_settings': {'default': {}, 'category': 'read_book', 'is_local': True}, |
| 70 |
'standalone_misc_settings': {'default': {}, 'category': 'read_book', 'is_local': True}, |
| 71 |
'standalone_recently_opened': {'default': [], 'category': 'read_book', 'is_local': True, 'disallowed_in_profile': True}, |
| 72 |
'user_color_schemes': {'default': {}, 'category': 'read_book'}, |
| 73 |
'user_stylesheet': {'default': '', 'category': 'read_book', 'is_local': True}, |
| 74 |
'word_actions': {'default': [], 'category': 'read_book'}, |
| 75 |
'highlight_style': {'default': None, 'category': 'read_book', 'is_local': True}, |
| 76 |
'highlights_export_format': {'default': 'text', 'category': 'read_book', 'is_local': True}, |
| 77 |
'custom_highlight_styles': {'default': [], 'category': 'read_book'}, |
| 78 |
'show_selection_bar': {'default': True, 'category': 'read_book'}, |
| 79 |
'net_search_url': {'default': 'https://google.com/search?q={q}', 'category': 'read_book'}, |
| 80 |
'selection_bar_actions': {'default': ['copy', 'lookup', 'highlight', 'remove_highlight', 'search_net', 'clear'], 'category': 'read_book'}, |
| 81 |
'selection_bar_quick_highlights': {'default': [], 'category': 'read_book'}, |
| 82 |
'skipped_dialogs': {'default': v'{}', 'category': 'read_book', 'is_local': True, 'disallowed_in_profile': True}, |
| 83 |
'tts': {'default': v'{}', 'category': 'read_book', 'is_local': True, 'disallowed_in_profile': True}, |
| 84 |
'tts_backend': {'default': v'{}', 'category': 'read_book', 'is_local': True, 'disallowed_in_profile': True}, |
| 85 |
'fullscreen_when_opening': {'default': 'auto', 'category': 'read_book', 'is_local': True}, |
| 86 |
'book_search_mode': {'default': 'contains', 'category': 'read_book', 'is_local': True}, |
| 87 |
'book_search_case_sensitive': {'default': False, 'category': 'read_book', 'is_local': True}, |
| 88 |
'reverse_page_turn_zones': {'default': False, 'category': 'read_book', 'is_local': True}, |
| 89 |
'gesture_overrides': {'default': {}, 'category': 'read_book'}, |
| 90 |
'cite_text_template': {'default': '[{text}]({url})', 'category': 'read_book'}, |
| 91 |
'cite_hl_template': {'default': '[{text}]({url})', 'category': 'read_book'}, |
| 92 |
'tts_bar_position': {'default': 'float', 'category': 'read_book', 'is_local': False, 'disallowed_in_profile': False}, |
| 93 |
} |
| 94 |
|
| 95 |
defaults = {} |
| 96 |
for x in Object.entries(all_settings): |
| 97 |
defaults[x[0]] = x[1].default |
| 98 |
|
| 99 |
|
| 100 |
def session_defaults(): |
| 101 |
return defaults |
| 102 |
|
| 103 |
|
| 104 |
def storage_available(which): |
| 105 |
which = which or 'localStorage' |
| 106 |
try: |
| 107 |
storage = window[which] |
| 108 |
x = '__storage__test__' |
| 109 |
storage.setItem(x, x) |
| 110 |
storage.removeItem(x) |
| 111 |
return True |
| 112 |
except: |
| 113 |
return False |
| 114 |
|
| 115 |
|
| 116 |
class FakeStorage: |
| 117 |
def __init__(self): |
| 118 |
self.data = {} |
| 119 |
|
| 120 |
def getItem(self, key): |
| 121 |
return self.data[key] |
| 122 |
|
| 123 |
def setItem(self, key, value): |
| 124 |
if jstype(value) is not 'string': |
| 125 |
value = JSON.stringify(value) |
| 126 |
self.data[key] = value |
| 127 |
|
| 128 |
def clear(self): |
| 129 |
self.data = {} |
| 130 |
|
| 131 |
|
| 132 |
def get_session_storage(): |
| 133 |
if not get_session_storage.ans: |
| 134 |
if storage_available('localStorage'): |
| 135 |
get_session_storage.ans = window.localStorage |
| 136 |
elif storage_available('sessionStorage'): |
| 137 |
get_session_storage.ans = window.sessionStorage |
| 138 |
console.error('localStorage not available using sessionStorage instead') |
| 139 |
else: |
| 140 |
get_session_storage.ans = FakeStorage() |
| 141 |
console.error('sessionStorage and localStorage not available using a temp cache instead') |
| 142 |
return get_session_storage.ans |
| 143 |
|
| 144 |
|
| 145 |
class SessionData: |
| 146 |
def __init__(self, global_prefix=None): |
| 147 |
self.global_prefix = global_prefix or 'calibre-session-' |
| 148 |
self.storage = get_session_storage() |
| 149 |
self.overflow_storage = {} |
| 150 |
self.has_overflow = False |
| 151 |
|
| 152 |
def get(self, key, defval): |
| 153 |
key = self.global_prefix + key |
| 154 |
if self.has_overflow: |
| 155 |
ans = self.overflow_storage[key] |
| 156 |
if ans is undefined: |
| 157 |
ans = self.storage.getItem(key) |
| 158 |
else: |
| 159 |
ans = self.storage.getItem(key) |
| 160 |
if ans is undefined or ans is None: |
| 161 |
if defval is undefined: |
| 162 |
defval = None |
| 163 |
return defval |
| 164 |
try: |
| 165 |
return JSON.parse(ans) |
| 166 |
except: |
| 167 |
if defval is undefined: |
| 168 |
defval = None |
| 169 |
return defval |
| 170 |
|
| 171 |
def set(self, key, value): |
| 172 |
key = self.global_prefix + key |
| 173 |
if value is None: |
| 174 |
self.storage.removeItem(key) |
| 175 |
v'delete self.overflow_storage[key]' |
| 176 |
return True |
| 177 |
value = JSON.stringify(value) |
| 178 |
try: |
| 179 |
self.storage.setItem(key, value) |
| 180 |
v'delete self.overflow_storage[key]' |
| 181 |
return True |
| 182 |
except: |
| 183 |
self.overflow_storage[key] = value |
| 184 |
self.has_overflow = True |
| 185 |
console.error('session storage has overflowed, using a temp cache instead') |
| 186 |
return False |
| 187 |
|
| 188 |
def set_bulk(self, changes): |
| 189 |
for key in Object.keys(changes): |
| 190 |
self.set(key, changes[key]) |
| 191 |
|
| 192 |
def clear(self): |
| 193 |
self.storage.clear() |
| 194 |
self.overflow_storage = {} |
| 195 |
self.has_overflow = False |
| 196 |
|
| 197 |
|
| 198 |
def local_storage(): |
| 199 |
if not local_storage.storage: |
| 200 |
local_storage.storage = SessionData('calibre-local-') |
| 201 |
return local_storage.storage |
| 202 |
|
| 203 |
|
| 204 |
def get_device_uuid(): |
| 205 |
if not get_device_uuid.ans: |
| 206 |
s = local_storage() |
| 207 |
ans = s.get('device_uuid') |
| 208 |
if not ans: |
| 209 |
ans = short_uuid() |
| 210 |
s.set('device_uuid', ans) |
| 211 |
get_device_uuid.ans = ans |
| 212 |
return get_device_uuid.ans |
| 213 |
|
| 214 |
|
| 215 |
def deep_eq(a, b): |
| 216 |
if a is b: |
| 217 |
return True |
| 218 |
if Array.isArray(a) and Array.isArray(b): |
| 219 |
if a.length is not b.length: |
| 220 |
return False |
| 221 |
for i in range(a.length): |
| 222 |
if not deep_eq(a[i], b[i]): |
| 223 |
return False |
| 224 |
return True |
| 225 |
if a is None or b is None or a is undefined or b is undefined: |
| 226 |
return False |
| 227 |
if jstype(a) is not 'object' or jstype(b) is not 'object': |
| 228 |
return False |
| 229 |
ka = Object.keys(a) |
| 230 |
if ka.length is not Object.keys(b).length: |
| 231 |
return False |
| 232 |
for key in ka: |
| 233 |
if not deep_eq(a[key], b[key]): |
| 234 |
return False |
| 235 |
return True |
| 236 |
|
| 237 |
|
| 238 |
def setting_val_is_default(setting_name, curval): |
| 239 |
defval = defaults[setting_name] |
| 240 |
return deep_eq(defval, curval) |
| 241 |
|
| 242 |
|
| 243 |
def get_subset_of_settings(sd, filter_func): |
| 244 |
ans = {} |
| 245 |
for setting_name in Object.keys(all_settings): |
| 246 |
curval = sd.get(setting_name, ans) |
| 247 |
metadata = all_settings[setting_name] |
| 248 |
is_set = curval is not ans |
| 249 |
if filter_func(setting_name, metadata, is_set, curval): |
| 250 |
ans[setting_name] = window.structuredClone(curval if is_set else metadata.default) |
| 251 |
return ans |
| 252 |
|
| 253 |
|
| 254 |
def apply_profile(sd, profile, filter_func): |
| 255 |
changes = {} |
| 256 |
for setting_name in Object.keys(all_settings): |
| 257 |
metadata = all_settings[setting_name] |
| 258 |
curval = sd.get(setting_name, changes) |
| 259 |
is_set = curval is not changes |
| 260 |
if filter_func(setting_name, metadata, is_set, curval): |
| 261 |
newval = None |
| 262 |
if Object.prototype.hasOwnProperty.call(profile, setting_name): |
| 263 |
newval = profile[setting_name] |
| 264 |
if deep_eq(newval, metadata.default): |
| 265 |
newval = None |
| 266 |
changes[setting_name] = window.structuredClone(newval) |
| 267 |
sd.set_bulk(changes) |
| 268 |
return changes |
| 269 |
|
| 270 |
|
| 271 |
standalone_reader_defaults = { |
| 272 |
'remember_window_geometry': False, |
| 273 |
'remember_last_read': True, |
| 274 |
'show_actions_toolbar': False, |
| 275 |
'show_actions_toolbar_in_fullscreen': False, |
| •276 |
'save_annotations_in_ebook': False, |
| 277 |
'sync_annots_user': '', |
| 278 |
'singleinstance': False, |
| 279 |
'auto_hide_mouse': True, |
| 280 |
'restore_docks': True, |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
def settings_for_reader_profile(sd): |
| 285 |
margin_text_settings = {'footer': True, 'header': True, 'controls_footer': True, 'left-margin': True, 'right-margin': True} |
| 286 |
|
| 287 |
def filter_func(setting_name, metadata, is_set, curval): |
| 288 |
if margin_text_settings[setting_name]: |
| 289 |
curval = window.structuredClone(curval) |
| 290 |
for x in ['left', 'right', 'middle']: |
| 291 |
if curval[x] is 'empty': |
| 292 |
v'delete curval[x]' |
| 293 |
elif setting_name is 'standalone_misc_settings': |
| 294 |
curval = window.structuredClone(curval) |
| 295 |
for key in Object.keys(curval): |
| 296 |
if curval[key] is standalone_reader_defaults[key]: |
| 297 |
v'delete curval[key]' |
| 298 |
|
| 299 |
if not is_set or metadata.category is not 'read_book' or metadata.disallowed_in_profile or deep_eq(curval, metadata.default): |
| 300 |
return False |
| 301 |
return True |
| 302 |
|
| 303 |
return get_subset_of_settings(sd, filter_func) |
| 304 |
|
| 305 |
|
| 306 |
def apply_reader_profile(sd, profile): |
| 307 |
def filter_func(setting_name, metadata, is_set, curval): |
| 308 |
return metadata.category is 'read_book' and not metadata.disallowed_in_profile |
| 309 |
apply_profile(sd, profile, filter_func) |
| 310 |
|
| 311 |
|
| 312 |
default_interface_data = { |
| 313 |
'username': None, |
| 314 |
'output_format': 'EPUB', |
| 315 |
'input_formats': {'EPUB', 'MOBI', 'AZW3'}, |
| 316 |
'gui_pubdate_display_format': 'MMM yyyy', |
| 317 |
'gui_timestamp_display_format': 'dd MMM yyyy', |
| 318 |
'gui_last_modified_display_format': 'dd MMM yyyy', |
| 319 |
'use_roman_numerals_for_series_number': True, |
| 320 |
'default_library_id': None, |
| 321 |
'default_book_list_mode': session_defaults().view_mode, |
| 322 |
'library_map': None, |
| 323 |
'search_the_net_urls': [], |
| 324 |
'donate_link': 'https://calibre-ebook.com/donate', |
| 325 |
'default_author_link': '', |
| 326 |
'icon_map': {}, |
| 327 |
'icon_path': '', |
| 328 |
'custom_list_template': None, |
| 329 |
'num_per_page': 50, |
| 330 |
'lang_code_for_user_manual': '', |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
def get_interface_data(): |
| 335 |
if not get_interface_data.storage: |
| 336 |
get_interface_data.storage = SessionData('calibre-interface-data-') |
| 337 |
ans = get_interface_data.storage.get('current') |
| 338 |
if ans: |
| 339 |
ans.is_default = False |
| 340 |
else: |
| 341 |
ans = {'is_default': True} |
| 342 |
for k in default_interface_data: |
| 343 |
ans[k] = default_interface_data[k] |
| 344 |
return ans |
| 345 |
|
| 346 |
|
| 347 |
def update_interface_data(new_data): |
| 348 |
data = get_interface_data() |
| 349 |
for k in default_interface_data: |
| 350 |
nval = new_data[k] |
| 351 |
if k is not undefined: |
| 352 |
data[k] = nval |
| 353 |
if not get_interface_data.storage: |
| 354 |
get_interface_data.storage = SessionData('calibre-interface-data-') |
| 355 |
get_interface_data.storage.set('current', data) |
| 356 |
|
| 357 |
|
| 358 |
def get_translations(newval): |
| 359 |
if not get_translations.storage: |
| 360 |
get_translations.storage = SessionData('calibre-translations-') |
| 361 |
if newval?: |
| 362 |
get_translations.storage.set('current', newval) |
| 363 |
else: |
| 364 |
return get_translations.storage.get('current') |
| 365 |
|
| 366 |
|
| 367 |
def is_setting_local(name): |
| 368 |
m = all_settings[name] |
| 369 |
return m.is_local if m else True |
| 370 |
|
| 371 |
|
| 372 |
class UserSessionData(SessionData): |
| 373 |
def __init__(self, username, saved_data): |
| 374 |
self.prefix = (username or '') + '-' |
| 375 |
self.has_user = bool(username) |
| 376 |
self.username = username |
| 377 |
SessionData.__init__(self) |
| 378 |
self.echo_changes = False |
| 379 |
self.changes = {} |
| 380 |
self.has_changes = False |
| 381 |
self.push_timer_id = None |
| 382 |
if saved_data: |
| 383 |
for key in saved_data: |
| 384 |
if not is_setting_local(key): |
| 385 |
self.set(key, saved_data[key]) |
| 386 |
self.echo_changes = True |
| 387 |
|
| 388 |
def defval(self, key): |
| 389 |
ans = session_defaults()[key] |
| 390 |
if ans: |
| 391 |
ans = window.structuredClone(ans) |
| 392 |
return ans |
| 393 |
|
| 394 |
def get(self, key, defval): |
| 395 |
if defval is undefined: |
| 396 |
defval = self.defval(key) |
| 397 |
return SessionData.get(self, (self.prefix + key), defval) |
| 398 |
|
| 399 |
def get_library_option(self, library_id, key, defval): |
| 400 |
if not library_id: |
| 401 |
return self.get(key, defval) |
| 402 |
lkey = key + '-||-' + library_id |
| 403 |
if defval is undefined: |
| 404 |
defval = self.defval(key) |
| 405 |
return self.get(lkey, defval) |
| 406 |
|
| 407 |
def set(self, key, value): |
| 408 |
if self.echo_changes and self.has_user and not is_setting_local(key): |
| 409 |
self.changes[key] = value |
| 410 |
self.has_changes = True |
| 411 |
if self.push_timer_id is not None: |
| 412 |
clearTimeout(self.push_timer_id) |
| 413 |
self.push_timer_id = setTimeout(self.push_to_server.bind(self), 1000) |
| 414 |
return SessionData.set(self, (self.prefix + key), value) |
| 415 |
|
| 416 |
def set_library_option(self, library_id, key, value): |
| 417 |
if library_id: |
| 418 |
key = key + '-||-' + library_id |
| 419 |
return self.set(key, value) |
| 420 |
|
| 421 |
def push_to_server(self): |
| 422 |
if self.has_changes: |
| 423 |
ajax_send('interface-data/set-session-data', self.changes, def (end_type, xhr, ev): |
| 424 |
if end_type is not 'load': |
| 425 |
console.error('Failed to send session data to server: ' + xhr.error_html) |
| 426 |
) |
| 427 |
self.changes = {} |
| 428 |
self.has_changes = False |