| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from gettext import gettext as _ |
| 6 |
|
| 7 |
from ajax import absolute_path, ajax |
| 8 |
from book_list.globals import get_session_data |
| 9 |
from lru_cache import LRUCache |
| 10 |
from session import get_interface_data |
| 11 |
from utils import parse_url_params |
| 12 |
|
| 13 |
load_status = {'loading': True, 'ok': False, 'error_html': None, 'current_fetch': None, 'http_error_code': 0} |
| 14 |
library_data = { |
| 15 |
'metadata': {}, |
| 16 |
'previous_book_ids': [], |
| 17 |
'force_refresh': False, |
| 18 |
'bools_are_tristate': True, |
| 19 |
'field_names': {}, |
| 20 |
'available_browse_fields': [], |
| 21 |
'browse_fields': [], |
| 22 |
} |
| 23 |
BROWSE_VISIBLE_FIELDS_SETTING = 'browse_visible_fields' |
| 24 |
|
| 25 |
|
| 26 |
def current_library_id(): |
| 27 |
q = parse_url_params() |
| 28 |
return q.library_id or get_interface_data().default_library_id |
| 29 |
|
| 30 |
|
| 31 |
def all_libraries(): |
| 32 |
interface_data = get_interface_data() |
| 33 |
lids = sorted(interface_data.library_map, key=def (x): return interface_data.library_map[x];) |
| 34 |
return [(lid, interface_data.library_map[lid]) for lid in lids] |
| 35 |
|
| 36 |
|
| 37 |
def current_virtual_library(): |
| 38 |
q = parse_url_params() |
| 39 |
return q.vl or '' |
| 40 |
|
| 41 |
|
| 42 |
def last_virtual_library_for(library_id): |
| 43 |
if last_virtual_library_for.library_id is library_id: |
| 44 |
return last_virtual_library_for.vl or '' |
| 45 |
return '' |
| 46 |
|
| 47 |
|
| 48 |
def url_books_query(sd): |
| 49 |
q = parse_url_params() |
| 50 |
sd = sd or get_session_data() |
| 51 |
lid = current_library_id() |
| 52 |
return { |
| 53 |
'library_id': lid, |
| 54 |
'sort': q.sort or sd.get_library_option(lid, 'sort'), |
| 55 |
'search': q.search, |
| 56 |
'vl': current_virtual_library(), |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
def loaded_books_query(): |
| 61 |
sr = library_data.search_result |
| 62 |
sort = None |
| 63 |
if sr: |
| 64 |
sort = [s + '.' + o for s, o in zip(sr.sort.split(','), sr.sort_order.split(','))].join(',') |
| 65 |
return { |
| 66 |
'library_id': sr.library_id if sr else None, |
| 67 |
'sort': sort, |
| 68 |
'search': sr?.query, |
| 69 |
'vl': sr?.vl |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
def current_sorted_field(): |
| 74 |
if library_data.search_result: |
| 75 |
return library_data.search_result.sort, library_data.search_result.sort_order |
| 76 |
sort = url_books_query().sort.partition(',')[0] |
| 77 |
csf = sort.partition('.')[0] |
| 78 |
csfo = sort.partition('.')[2] or 'asc' |
| 79 |
return csf, csfo |
| 80 |
|
| 81 |
|
| 82 |
def all_virtual_libraries(): |
| 83 |
return library_data.virtual_libraries or {} |
| 84 |
|
| 85 |
|
| 86 |
def update_library_data(data): |
| 87 |
load_status.loading = False |
| 88 |
load_status.ok = True |
| 89 |
load_status.error_html = None |
| 90 |
load_status.http_error_code = 0 |
| 91 |
library_data.previous_book_ids = [] |
| 92 |
if library_data.for_library is not current_library_id(): |
| 93 |
library_data.field_names = {} |
| 94 |
library_data.for_library = current_library_id() |
| 95 |
for key in ( |
| 96 |
'search_result sortable_fields field_metadata metadata virtual_libraries book_display_fields' |
| 97 |
' bools_are_tristate book_details_vertical_categories fts_enabled fields_that_support_notes' |
| 98 |
' categories_using_hierarchy' |
| 99 |
).split(' '): |
| 100 |
library_data[key] = data[key] |
| 101 |
library_data.available_browse_fields = data.browse_fields or [] |
| 102 |
update_visible_browse_fields() |
| 103 |
sr = library_data.search_result |
| 104 |
if sr: |
| 105 |
last_virtual_library_for.library_id = sr.library_id |
| 106 |
last_virtual_library_for.vl = sr.vl |
| 107 |
else: |
| 108 |
last_virtual_library_for.library_id = None |
| 109 |
last_virtual_library_for.vl = None |
| 110 |
refresh_sidebar_navigation() |
| 111 |
refresh_browse_field_settings() |
| 112 |
|
| 113 |
|
| 114 |
def configured_browse_fields(): |
| 115 |
by_library = get_session_data().get(BROWSE_VISIBLE_FIELDS_SETTING) or {} |
| 116 |
return by_library[current_library_id()] |
| 117 |
|
| 118 |
|
| 119 |
def configured_browse_field_map(): |
| 120 |
configured = configured_browse_fields() |
| 121 |
if not Array.isArray(configured): |
| 122 |
return None |
| 123 |
ans = {} |
| 124 |
for key in configured: |
| 125 |
ans[key] = True |
| 126 |
return ans |
| 127 |
|
| 128 |
|
| 129 |
def update_visible_browse_fields(): |
| 130 |
fields = library_data.available_browse_fields or [] |
| 131 |
selected = configured_browse_field_map() |
| 132 |
if selected is None: |
| 133 |
library_data.browse_fields = [field for field in fields if field.default_visible is not False] |
| 134 |
else: |
| 135 |
library_data.browse_fields = [field for field in fields if selected[field.key]] |
| 136 |
|
| 137 |
|
| 138 |
def set_browse_fields(field_keys): |
| 139 |
by_library = get_session_data().get(BROWSE_VISIBLE_FIELDS_SETTING) or {} |
| 140 |
by_library[current_library_id()] = field_keys |
| 141 |
get_session_data().set(BROWSE_VISIBLE_FIELDS_SETTING, by_library) |
| 142 |
update_visible_browse_fields() |
| 143 |
refresh_sidebar_navigation() |
| 144 |
|
| 145 |
|
| 146 |
def refresh_sidebar_navigation(): |
| 147 |
try: |
| 148 |
refresh = window.cs_refresh_sidebar_navigation |
| 149 |
except Exception: |
| 150 |
refresh = None |
| 151 |
if refresh: |
| 152 |
refresh() |
| 153 |
|
| 154 |
|
| 155 |
def refresh_browse_field_settings(): |
| 156 |
try: |
| 157 |
refresh = window.cs_refresh_browse_field_settings |
| 158 |
except Exception: |
| 159 |
refresh = None |
| 160 |
if refresh: |
| 161 |
refresh() |
| 162 |
|
| 163 |
|
| 164 |
def browse_field_settings(): |
| 165 |
ans = [] |
| 166 |
selected = configured_browse_field_map() |
| 167 |
for field in library_data.available_browse_fields or []: |
| 168 |
f = Object.assign({}, field) |
| 169 |
f.enabled = (field.default_visible is not False) if selected is None else bool(selected[f.key]) |
| 170 |
ans.push(f) |
| 171 |
return ans |
| 172 |
|
| 173 |
|
| 174 |
def browse_fields(): |
| 175 |
return library_data.browse_fields or [] |
| 176 |
|
| 177 |
|
| 178 |
def browse_field_for(field): |
| 179 |
for f in browse_fields(): |
| 180 |
if f.key is field: |
| 181 |
return f |
| 182 |
|
| 183 |
|
| 184 |
def add_more_books(data): |
| 185 |
for key in data.metadata: |
| 186 |
library_data.metadata[key] = data.metadata[key] |
| 187 |
sr = library_data.search_result |
| 188 |
if sr and sr.book_ids and sr.book_ids.length > 0: |
| 189 |
library_data.previous_book_ids = library_data.previous_book_ids.concat(sr.book_ids) |
| 190 |
library_data.search_result = data.search_result |
| 191 |
|
| 192 |
|
| 193 |
def current_book_ids(): |
| 194 |
return library_data.previous_book_ids.concat(library_data.search_result.book_ids) |
| 195 |
|
| 196 |
|
| 197 |
def remove_from_array(array, item): |
| 198 |
while True: |
| 199 |
idx = array.indexOf(item) |
| 200 |
if idx < 0: |
| 201 |
break |
| 202 |
array.splice(idx, 1) |
| 203 |
|
| 204 |
|
| 205 |
def remove_book(book_id): |
| 206 |
book_id = int(book_id) |
| 207 |
if library_data.previous_book_ids: |
| 208 |
remove_from_array(library_data.previous_book_ids, book_id) |
| 209 |
if library_data.search_result?.book_ids: |
| 210 |
remove_from_array(library_data.search_result.book_ids, book_id) |
| 211 |
|
| 212 |
|
| 213 |
def book_after(book_id, delta): |
| 214 |
if not delta: |
| 215 |
delta = 1 |
| 216 |
ids = current_book_ids() |
| 217 |
idx = ids.indexOf(int(book_id)) |
| 218 |
if idx > -1: |
| 219 |
new_idx = (idx + ids.length + delta) % ids.length |
| 220 |
return ids[new_idx] |
| 221 |
|
| 222 |
|
| 223 |
def on_data_loaded(end_type, xhr, ev): |
| 224 |
load_status.current_fetch = None |
| 225 |
|
| 226 |
def bad_load(msg): |
| 227 |
load_status.ok = False |
| 228 |
load_status.loading = False |
| 229 |
load_status.error_html = msg or xhr.error_html |
| 230 |
load_status.http_error_code = xhr.status |
| 231 |
|
| 232 |
if end_type is 'load': |
| 233 |
data = JSON.parse(xhr.responseText) |
| 234 |
if data.bad_restriction: |
| 235 |
bad_load(_('The library restriction for the current user is invalid: {}').format(data.bad_restriction)) |
| 236 |
else: |
| 237 |
update_library_data(data) |
| 238 |
sd = get_session_data() |
| 239 |
q = loaded_books_query() |
| 240 |
sd.set_library_option(q.library_id, 'sort', q.sort) |
| 241 |
elif end_type is 'abort': |
| 242 |
pass |
| 243 |
else: |
| 244 |
bad_load() |
| 245 |
|
| 246 |
|
| 247 |
def fetch_init_data(): |
| 248 |
if load_status.current_fetch: |
| 249 |
load_status.current_fetch.abort() |
| 250 |
query = url_books_query() |
| 251 |
load_status.loading = True |
| 252 |
load_status.ok = False |
| 253 |
load_status.error_html = None |
| 254 |
load_status.http_error_code = 0 |
| 255 |
load_status.current_fetch = ajax('interface-data/books-init', on_data_loaded, query=query) |
| 256 |
load_status.current_fetch.send() |
| 257 |
|
| 258 |
|
| 259 |
def field_names_received(library_id, field, proceed, end_type, xhr, event): |
| 260 |
if library_id is not current_library_id(): |
| 261 |
return |
| 262 |
if end_type is not 'load': |
| 263 |
if end_type is not 'abort': |
| 264 |
proceed(False, field, xhr.error_html) |
| 265 |
return |
| 266 |
try: |
| 267 |
names = JSON.parse(xhr.responseText) |
| 268 |
except Exception: |
| 269 |
import traceback |
| 270 |
traceback.print_exc() |
| 271 |
proceed(False, field, 'Invalid JSON from server') |
| 272 |
return |
| 273 |
library_data.field_names[field] = { |
| 274 |
'loading': False, |
| 275 |
'loaded': True, |
| 276 |
'last_updated_at': Date.now(), |
| 277 |
'names': names |
| 278 |
} |
| 279 |
proceed(True, field, names) |
| 280 |
|
| 281 |
|
| 282 |
def field_names_for(field, proceed): |
| 283 |
if not library_data.field_names: |
| 284 |
library_data.field_names = {} |
| 285 |
if library_data.field_names[field] and library_data.field_names[field].loaded: |
| 286 |
proceed(True, field, library_data.field_names[field].names) |
| 287 |
if not library_data.field_names[field] or ( |
| 288 |
not library_data.field_names[field].loading |
| 289 |
and Date.now() - library_data.field_names[field].last_updated_at > 3600 * 1000 |
| 290 |
): |
| 291 |
ajax( |
| 292 |
f'interface-data/field-names/{encodeURIComponent(field)}', |
| 293 |
field_names_received.bind(None, current_library_id(), field, proceed), |
| 294 |
query={'library_id': current_library_id()} |
| 295 |
).send() |
| 296 |
if not library_data.field_names[field]: |
| 297 |
library_data.field_names[field] = {'last_updated_at': 0, 'loaded': False, 'names': []} |
| 298 |
library_data.field_names[field].loading = True |
| 299 |
|
| 300 |
|
| 301 |
def thumbnail_url(book_id, width, height): |
| 302 |
query = f'sz={Math.ceil(width * window.devicePixelRatio)}x{Math.ceil(height * window.devicePixelRatio)}' |
| 303 |
prefix = f'get/thumb/{book_id}' |
| 304 |
lid = loaded_books_query().library_id or current_library_id() |
| 305 |
if lid: |
| 306 |
prefix += f'/{lid}' |
| 307 |
return absolute_path(f'{prefix}?{query}') |
| 308 |
|
| 309 |
|
| 310 |
def cover_url(book_id): |
| 311 |
lid = current_library_id() |
| 312 |
return absolute_path(f'get/cover/{book_id}/{lid}') |
| 313 |
|
| 314 |
|
| 315 |
def download_url(book_id, fmt, content_disposition): |
| 316 |
lid = current_library_id() |
| 317 |
ans = absolute_path(f'get/{fmt}/{book_id}/{lid}') |
| 318 |
if content_disposition: |
| 319 |
ans += f'?content_disposition={content_disposition}' |
| 320 |
return ans |
| 321 |
|
| 322 |
|
| 323 |
def download_data_file_url(book_id, relpath, content_disposition): |
| 324 |
lid = current_library_id() |
| 325 |
rpath = encodeURIComponent(relpath) |
| 326 |
ans = absolute_path(f'data-files/get/{book_id}/{rpath}/{lid}') |
| 327 |
if content_disposition: |
| 328 |
ans += f'?content_disposition={content_disposition}' |
| 329 |
return ans |
| 330 |
|
| 331 |
|
| 332 |
def web_search_url(book_id, field, item_val): |
| 333 |
return absolute_path( |
| 334 |
f'web-search/{book_id}/{encodeURIComponent(field)}/{encodeURIComponent(item_val)}' |
| 335 |
+ f'?library_id={encodeURIComponent(current_library_id())}' |
| 336 |
) |
| 337 |
|
| 338 |
|
| 339 |
def book_metadata(book_id): |
| 340 |
return library_data.metadata[book_id] |
| 341 |
|
| 342 |
|
| 343 |
def set_book_metadata(book_id, value): |
| 344 |
library_data.metadata[book_id] = value |
| 345 |
|
| 346 |
|
| 347 |
def loaded_book_ids(): |
| 348 |
return Object.keys(library_data.metadata) |
| 349 |
|
| 350 |
|
| 351 |
def force_refresh_on_next_load(): |
| 352 |
library_data.force_refresh = True |
| 353 |
|
| 354 |
|
| 355 |
def ensure_current_library_data(): |
| 356 |
fr = library_data.force_refresh |
| 357 |
library_data.force_refresh = False |
| 358 |
|
| 359 |
def is_same(a, b): |
| 360 |
if not a and not b: |
| 361 |
return True |
| 362 |
return a is b |
| 363 |
|
| 364 |
if fr: |
| 365 |
matches = False |
| 366 |
else: |
| 367 |
q = url_books_query() |
| 368 |
loaded = loaded_books_query() |
| 369 |
matches = True |
| 370 |
|
| 371 |
for key in q: |
| 372 |
if not is_same(q[key], loaded[key]): |
| 373 |
matches = False |
| 374 |
break |
| 375 |
if not matches: |
| 376 |
fetch_init_data() |
| 377 |
|
| 378 |
|
| 379 |
class ThumbnailCache: |
| 380 |
# Cache to prevent browser from issuing HTTP requests when thumbnails pages |
| 381 |
# are destroyed/rebuilt. |
| 382 |
|
| 383 |
def __init__(self, size=256): |
| 384 |
self.cache = LRUCache(size) |
| 385 |
|
| 386 |
def get(self, book_id, width, height, callback): |
| 387 |
url = thumbnail_url(book_id, width, height) |
| 388 |
item = self.cache.get(url) |
| 389 |
if not item: |
| 390 |
img = new Image() |
| 391 |
item = {'img': img, 'load_type': None, 'callbacks': [callback]} |
| 392 |
img.onerror = self.load_finished.bind(None, item, 'error') |
| 393 |
img.onload = self.load_finished.bind(None, item, 'load') |
| 394 |
img.onabort = self.load_finished.bind(None, item, 'abort') |
| 395 |
img.dataset.bookId = book_id + '' |
| 396 |
img.src = url |
| 397 |
self.cache.set(url, item) |
| 398 |
return img |
| 399 |
if item.load_type is None: |
| 400 |
if item.callbacks.indexOf(callback) < 0: |
| 401 |
item.callbacks.push(callback) |
| 402 |
else: |
| 403 |
callback(item.img, item.load_type) |
| 404 |
return item.img |
| 405 |
|
| 406 |
def load_finished(self, item, load_type): |
| 407 |
item.load_type = load_type |
| 408 |
img = item.img |
| 409 |
img.onload = img.onerror = img.onabort = None |
| 410 |
for callback in item.callbacks: |
| 411 |
callback(img, load_type) |
| 412 |
|
| 413 |
thumbnail_cache = ThumbnailCache() |
| 414 |
|
| 415 |
|
| 416 |
def sync_library_books(library_id, to_sync, callback): |
| 417 |
url = f'book-get-annotations/{library_id}/' |
| 418 |
which = [] |
| 419 |
lrmap = {} |
| 420 |
for key, last_read in to_sync: |
| 421 |
library_id, book_id, fmt = key |
| 422 |
fmt = fmt.upper() |
| 423 |
which.push(f'{book_id}-{fmt}') |
| 424 |
lrmap[f'{book_id}:{fmt}'] = last_read |
| 425 |
url += which.join('_') |
| 426 |
ajax(url, callback.bind(None, library_id, lrmap)).send() |