| 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 elementmaker import E |
| 6 |
from gettext import gettext as _ |
| 7 |
|
| 8 |
from ajax import absolute_path, ajax_send, ajax |
| 9 |
from book_list.cover_grid import BORDER_RADIUS |
| 10 |
from book_list.globals import get_db |
| 11 |
from book_list.library_data import ( |
| 12 |
all_libraries, last_virtual_library_for, sync_library_books |
| 13 |
) |
| 14 |
from book_list.router import open_book, update_window_title |
| 15 |
from book_list.top_bar import add_button, create_top_bar |
| 16 |
from book_list.ui import set_default_panel_handler, show_panel |
| 17 |
from dom import add_extra_css, build_rule, clear, ensure_id, set_css, unique_id |
| 18 |
from modals import create_custom_dialog |
| 19 |
from session import get_device_uuid, get_interface_data |
| 20 |
from utils import conditional_timeout, safe_set_inner_html, username_key |
| 21 |
from widgets import create_button |
| 22 |
|
| 23 |
CLASS_NAME = 'home-page' |
| 24 |
|
| 25 |
add_extra_css(def (): |
| 26 |
ans = '' |
| 27 |
sel = f'.{CLASS_NAME} ' |
| 28 |
ans += build_rule('.' + CLASS_NAME, |
| 29 |
min_width='0', |
| 30 |
max_width='100%', |
| 31 |
box_sizing='border-box', |
| 32 |
) |
| 33 |
# Page gutter + consistent width like other pages (narrow: never wider than viewport) |
| 34 |
ans += build_rule(sel + '.cs-home-content', |
| 35 |
padding='0.75rem 1rem 1rem 1rem', |
| 36 |
max_width='980px', |
| 37 |
width='100%', |
| 38 |
min_width='0', |
| 39 |
box_sizing='border-box', |
| 40 |
margin_left='auto', |
| 41 |
margin_right='auto' |
| 42 |
) |
| 43 |
ans += r''' |
| 44 |
@media (max-width: 720px) { |
| 45 |
.home-page .cs-home-content { padding-left: 0.75rem; padding-right: 0.75rem; } |
| 46 |
} |
| 47 |
@media (max-width: 380px) { |
| 48 |
.home-page .cs-home-content { padding-left: 0.5rem; padding-right: 0.5rem; } |
| 49 |
} |
| 50 |
''' |
| 51 |
# Section cards (surface hierarchy) |
| 52 |
ans += build_rule(sel + '.cs-home-section', |
| 53 |
margin_top='0.95rem', |
| 54 |
padding='1rem', |
| 55 |
border_radius='18px', |
| 56 |
border='var(--calibre-border-style-subtle)', |
| 57 |
background='var(--calibre-color-surface-1)', |
| 58 |
box_shadow='0 16px 44px rgba(0,0,0,0.10)', |
| 59 |
min_width='0', |
| 60 |
max_width='100%', |
| 61 |
box_sizing='border-box', |
| 62 |
overflow_x='hidden', |
| 63 |
) |
| 64 |
ans += build_rule(sel + '.cs-home-section:first-child', margin_top='0') |
| 65 |
ans += build_rule(sel + '.cs-home-section h2', |
| 66 |
margin='0 0 0.85rem 0', |
| 67 |
padding='0', |
| 68 |
font_size='1.1em', |
| 69 |
font_weight='800', |
| 70 |
opacity='0.92' |
| 71 |
) |
| 72 |
ans += build_rule(sel + '.cs-home-actions', |
| 73 |
display='flex', |
| 74 |
flex_wrap='wrap', |
| 75 |
gap='0.65rem', |
| 76 |
min_width='0', |
| 77 |
max_width='100%', |
| 78 |
) |
| 79 |
ans += build_rule(sel + '.cs-home-download-actions', margin_top='0.55rem') |
| 80 |
ans += build_rule(sel + '.cs-home-actions a.calibre-push-button', margin='0') |
| 81 |
# Covers row — match book-list-cover-grid gap (10px); no per-tile margins (see inline styles removed in JS) |
| 82 |
ans += build_rule(sel + '.cs-home-covers', |
| 83 |
display='flex', |
| 84 |
flex_wrap='wrap', |
| 85 |
align_items='flex-start', |
| 86 |
align_content='flex-start', |
| 87 |
gap='10px', |
| 88 |
row_gap='10px', |
| 89 |
column_gap='10px', |
| 90 |
width='100%', |
| 91 |
min_width='0', |
| 92 |
max_width='100%', |
| 93 |
box_sizing='border-box', |
| 94 |
) |
| 95 |
ans += build_rule(sel + '.cs-home-covers > div', |
| 96 |
margin='0', |
| 97 |
flex_grow='0', |
| 98 |
flex_shrink='1', |
| 99 |
flex_basis='auto', |
| 100 |
min_width='0', |
| 101 |
width='min(118px, max(72px, calc((100% - 20px) / 3.15)))', |
| 102 |
max_width='100%', |
| 103 |
box_sizing='border-box', |
| 104 |
) |
| 105 |
ans += build_rule(sel + '.cs-home-covers > div > a', |
| 106 |
display='block', |
| 107 |
width='100%', |
| 108 |
min_width='0', |
| 109 |
box_sizing='border-box', |
| 110 |
) |
| 111 |
img_sel = sel + '.recently-read img' |
| 112 |
ans += build_rule(img_sel, |
| 113 |
width='100%', |
| 114 |
height='auto', |
| 115 |
aspect_ratio='2 / 3', |
| 116 |
border_radius=f'{BORDER_RADIUS}px', |
| 117 |
display='block', |
| 118 |
object_fit='cover', |
| 119 |
border='var(--calibre-border-style)', |
| 120 |
box_shadow='0 10px 26px rgba(0,0,0,0.14)', |
| 121 |
transition='box-shadow 160ms ease, filter 160ms ease', |
| 122 |
) |
| 123 |
ans += build_rule(f'{img_sel}:hover', |
| 124 |
box_shadow='0 16px 44px rgba(0,0,0,0.18), 0 0 0 4px var(--calibre-color-primary-light)', |
| 125 |
filter='brightness(1.03)' |
| 126 |
) |
| 127 |
ans += build_rule(f'{img_sel}:active', |
| 128 |
box_shadow='0 12px 34px rgba(0,0,0,0.18), 0 0 0 4px var(--calibre-color-primary-light-2)', |
| 129 |
filter='brightness(1.01)' |
| 130 |
) |
| 131 |
return ans |
| 132 |
) |
| 133 |
|
| 134 |
recently_read_by_user = {'updated': False} |
| 135 |
|
| 136 |
|
| 137 |
def update_recently_read_by_user(items): |
| 138 |
if items: |
| 139 |
recently_read_by_user.items = items |
| 140 |
recently_read_by_user.updated = True |
| 141 |
|
| 142 |
|
| 143 |
def update_book_in_recently_read_by_user_on_home_page(library_id, book_id, book_format, cfi): |
| 144 |
if recently_read_by_user.items: |
| 145 |
for item in recently_read_by_user.items: |
| 146 |
if item.library_id is library_id and item.book_id is book_id and item.format is book_format: |
| 147 |
item.cfi = cfi |
| 148 |
|
| 149 |
|
| 150 |
def show_cover(blob, name, mt, book): |
| 151 |
img = document.getElementById(this) |
| 152 |
if not img: |
| 153 |
return |
| 154 |
img.onload = def (): |
| 155 |
window.URL.revokeObjectURL(this.src) |
| 156 |
img.src = window.URL.createObjectURL(blob) |
| 157 |
|
| 158 |
|
| 159 |
def read_book(library_id, book_id, fmt, extra_query): |
| 160 |
open_book(book_id, fmt, library_id, extra_query=extra_query) |
| 161 |
|
| 162 |
|
| 163 |
def view_book_page(library_id, book_id): |
| 164 |
show_panel('book_details', {'library_id': library_id, 'book_id': book_id + ''}, replace=False) |
| 165 |
|
| 166 |
|
| 167 |
def get_last_read_position(last_read_positions, prev_last_read): |
| 168 |
prev_epoch = prev_last_read.getTime() |
| 169 |
dev = get_device_uuid() |
| 170 |
newest_epoch = ans = None |
| 171 |
for data in last_read_positions: |
| 172 |
if data.device is not dev and data.epoch > prev_epoch: |
| 173 |
if ans is None or data.epoch > newest_epoch: |
| 174 |
newest_epoch = data.epoch |
| 175 |
ans = data |
| 176 |
return ans |
| 177 |
|
| 178 |
|
| 179 |
def sync_data_received(library_id, lrmap, load_type, xhr, ev): |
| 180 |
if load_type is not 'load': |
| 181 |
print('Failed to get book sync data') |
| 182 |
return |
| 183 |
data = JSON.parse(xhr.responseText) |
| 184 |
|
| 185 |
db = get_db() |
| 186 |
for key in data: |
| 187 |
new_vals = data[key] |
| 188 |
entry = {'last_read': None, 'last_read_position': None, 'annotations_map': None} |
| 189 |
prev_last_read = lrmap[key] |
| 190 |
if prev_last_read: |
| 191 |
last_read_positions = new_vals.last_read_positions |
| 192 |
new_last_read = get_last_read_position(last_read_positions, prev_last_read) |
| 193 |
if new_last_read: |
| 194 |
last_read = new Date(new_last_read.epoch * 1000) |
| 195 |
cfi = new_last_read.cfi |
| 196 |
if cfi: |
| 197 |
entry.last_read = last_read |
| 198 |
entry.last_read_position = cfi |
| 199 |
new_amap = new_vals.annotations_map or {} |
| 200 |
is_empty = True |
| 201 |
v'for(var ikey in new_amap) { is_empty = false; break; }' |
| 202 |
if ! is_empty: |
| 203 |
entry.annotations_map = new_amap |
| 204 |
if entry.last_read_position or entry.annotations_map: |
| 205 |
book_id, fmt = key.partition(':')[::2] |
| 206 |
db.update_annotations_data_from_key(library_id, int(book_id), fmt, entry) |
| 207 |
|
| 208 |
|
| 209 |
def start_sync(to_sync): |
| 210 |
libraries = {} |
| 211 |
for key, last_read in to_sync: |
| 212 |
library_id = key[0] |
| 213 |
if not libraries[library_id]: |
| 214 |
libraries[library_id] = [] |
| 215 |
libraries[library_id].push([key, last_read]) |
| 216 |
for lid in libraries: |
| 217 |
sync_library_books(lid, libraries[lid], sync_data_received) |
| 218 |
|
| 219 |
|
| 220 |
def prepare_recent_container(container): |
| 221 |
container.style.display = 'block' |
| 222 |
container.appendChild(E.h2(_('Continue reading…'))) |
| 223 |
container.appendChild(E.div(class_='cs-home-covers')) |
| 224 |
cover_container = container.lastChild |
| 225 |
container.appendChild(E.div(class_='cs-home-actions cs-home-download-actions', |
| 226 |
create_button( |
| 227 |
_('Browse all downloaded books…'), |
| 228 |
icon='cloud-download', |
| 229 |
action=def (): |
| 230 |
show_panel('local_books') |
| 231 |
))) |
| 232 |
return cover_container |
| 233 |
|
| 234 |
|
| 235 |
def show_recent_for_user(container_id): |
| 236 |
container = document.getElementById(container_id) |
| 237 |
images = prepare_recent_container(container) |
| 238 |
for item in recently_read_by_user.items[:3]: |
| 239 |
q = {} |
| 240 |
if item.cfi: |
| 241 |
q.bookpos = item.cfi |
| 242 |
rb = read_book.bind(None, item.library_id, item.book_id, item.format, q) |
| 243 |
img = E.img(alt=item.tooltip, src=absolute_path(f'get/cover/{item.book_id}/{item.library_id}')) |
| 244 |
images.appendChild(E.div( |
| 245 |
E.a(title=item.tooltip, href='javascript:void(0)', img, onclick=rb) |
| 246 |
)) |
| 247 |
img.onerror = def (err): |
| 248 |
failed = err.target |
| 249 |
failed.parentNode.parentNode.style.display = 'none' |
| 250 |
|
| 251 |
|
| 252 |
def show_recent_stage2(books): |
| 253 |
container = document.getElementById(this) |
| 254 |
if not container or not books.length: |
| 255 |
return |
| 256 |
images = prepare_recent_container(container) |
| 257 |
db = get_db() |
| 258 |
to_sync = [] |
| 259 |
username = get_interface_data().username |
| 260 |
for book in books: |
| 261 |
if to_sync.length < 10: |
| 262 |
lr = book.last_read[username_key(username)] or new Date(0) |
| 263 |
to_sync.push([book.key, lr]) |
| 264 |
authors = book.metadata.authors.join(' & ') if book.metadata.authors else _('Unknown') |
| 265 |
img = E.img( |
| 266 |
alt=_('{} by {}').format(book.metadata.title, authors) |
| 267 |
) |
| 268 |
img_id = ensure_id(img) |
| 269 |
images.appendChild(E.div( |
| 270 |
E.a(img, href='javascript: void(0)', title=img.alt, |
| 271 |
onclick=read_book.bind(None, book.key[0], book.key[1], book.key[2], None) |
| 272 |
), |
| 273 |
)) |
| 274 |
if book.cover_name: |
| 275 |
db.get_file(book, book.cover_name, show_cover.bind(img_id)) |
| 276 |
start_sync(to_sync) |
| 277 |
|
| 278 |
|
| 279 |
def show_recent_for_user_if_fetched(): |
| 280 |
container = this |
| 281 |
if not recently_read_by_user.updated: |
| 282 |
return conditional_timeout(container.id, 5, show_recent_for_user_if_fetched) |
| 283 |
if recently_read_by_user.items and recently_read_by_user.items.length > 0: |
| 284 |
return show_recent_for_user(container.id) |
| 285 |
return show_recent.call(container) |
| 286 |
|
| 287 |
|
| 288 |
def show_recent(): |
| 289 |
container = this |
| 290 |
db = get_db() |
| 291 |
if not db.initialized: |
| 292 |
conditional_timeout(container.id, 5, show_recent) |
| 293 |
return |
| 294 |
if db.is_ok: |
| 295 |
db.get_recently_read_books(show_recent_stage2.bind(container.id)) |
| 296 |
else: |
| 297 |
print(db.initialize_error_msg) |
| 298 |
|
| 299 |
|
| 300 |
def newly_added_received(newly_container_id, end_type, xhr, ev): |
| 301 |
container = document.getElementById(newly_container_id) |
| 302 |
if not container or end_type is not 'load': |
| 303 |
return |
| 304 |
data = JSON.parse(xhr.responseText) |
| 305 |
if not data.books or not data.books.length: |
| 306 |
return |
| 307 |
container.style.display = 'block' |
| 308 |
container.appendChild(E.div(class_='cs-home-covers')) |
| 309 |
images = container.lastChild |
| 310 |
|
| 311 |
for book_id in data.books: |
| 312 |
authors = data.authors[book_id].join(' & ') |
| 313 |
alt = _('{} by {}').format(data.titles[book_id], authors) |
| 314 |
img = E.img(alt=alt, src=absolute_path(f'get/cover/{book_id}/{data.library_id}')) |
| 315 |
images.appendChild(E.div( |
| 316 |
E.a(img, href='javascript: void(0)', title=img.alt, |
| 317 |
onclick=view_book_page.bind(None, data.library_id, book_id) |
| 318 |
), |
| 319 |
)) |
| 320 |
|
| 321 |
|
| 322 |
# User account {{{ |
| 323 |
|
| 324 |
|
| 325 |
def change_password(): |
| 326 |
interface_data = get_interface_data() |
| 327 |
create_custom_dialog(_('Change password for: {}').format(interface_data.username), def (parent, close_modal): |
| 328 |
ids = unique_id(), unique_id(), unique_id() |
| 329 |
parent.appendChild(E.table( |
| 330 |
E.tr( |
| 331 |
E.td(E.label(_('Current password:') + '\xa0', for_=ids[0])), set_css(E.td(E.input(type='password'), id=ids[0]), padding_bottom='1.5ex') |
| 332 |
), |
| 333 |
E.tr( |
| 334 |
E.td(E.label(_('New password:') + '\xa0', for_=ids[1])), set_css(E.td(E.input(type='password'), id=ids[1]), padding_bottom='1.5ex') |
| 335 |
), |
| 336 |
E.tr( |
| 337 |
E.td(E.label(_('New password again:') + '\xa0', for_=ids[2])), set_css(E.td(E.input(type='password'), id=ids[2]), padding_bottom='1.5ex') |
| 338 |
) |
| 339 |
)) |
| 340 |
parent.appendChild(E.div()) |
| 341 |
def show_msg(html, is_info): |
| 342 |
msg = parent.firstChild.nextSibling |
| 343 |
safe_set_inner_html(msg, html) |
| 344 |
msg.style.color = 'red' if not is_info else 'currentColor' |
| 345 |
def on_complete(end_type, xhr, ev): |
| 346 |
if end_type is 'load': |
| 347 |
clear(parent) |
| 348 |
parent.appendChild(E.div(_( |
| 349 |
'Password successfully changed, you will be asked for the new password' |
| 350 |
' the next time the browser has to contact the calibre server.'))) |
| 351 |
parent.appendChild( |
| 352 |
E.div(class_='button-box', |
| 353 |
create_button(_('OK'), None, close_modal), |
| 354 |
)) |
| 355 |
return |
| 356 |
show_msg(_('Failed to change password, with error: {}').format(xhr.error_html)) |
| 357 |
def ok(): |
| 358 |
pws = parent.firstChild.getElementsByTagName('input') |
| 359 |
oldpw, pw1, pw2 = pws[0].value, pws[1].value, pws[2].value |
| 360 |
if pw1 is not pw2: |
| 361 |
show_msg(_('The two new passwords do not match')) |
| 362 |
return |
| 363 |
if not pw1 or not oldpw: |
| 364 |
show_msg(_('Empty passwords are not allowed')) |
| 365 |
return |
| 366 |
ajax_send('users/change-pw', {'oldpw': oldpw, 'newpw': pw1}, on_complete) |
| 367 |
show_msg(_('Contacting server, please wait...'), True) |
| 368 |
parent.lastChild.display = 'none' |
| 369 |
parent.appendChild( |
| 370 |
E.div(class_='button-box', |
| 371 |
create_button(_('Cancel'), None, close_modal), |
| 372 |
create_button(_('OK'), None, ok, highlight=True), |
| 373 |
) |
| 374 |
) |
| 375 |
) |
| 376 |
|
| 377 |
|
| 378 |
def show_user_details(): |
| 379 |
interface_data = get_interface_data() |
| 380 |
create_custom_dialog(_('Logged in as {}').format(interface_data.username), def (parent, close_modal): |
| 381 |
msg = E.div() |
| 382 |
safe_set_inner_html(msg, _( |
| 383 |
'You are logged in as the user <b>{}</b>. To log in ' |
| 384 |
'as a different user, you will have to restart the browser.').format(interface_data.username)) |
| 385 |
parent.appendChild(msg) |
| 386 |
parent.appendChild( |
| 387 |
E.div(class_='button-box', |
| 388 |
create_button(_('Close'), None, close_modal), |
| 389 |
create_button(_('Change password'), None, def (): |
| 390 |
setTimeout(change_password, 0) |
| 391 |
close_modal() |
| 392 |
, highlight = True), |
| 393 |
) |
| 394 |
) |
| 395 |
) |
| 396 |
# }}} |
| 397 |
|
| 398 |
|
| 399 |
def init(container_id): |
| 400 |
update_window_title() |
| 401 |
container = document.getElementById(container_id) |
| 402 |
container.classList.add(CLASS_NAME) |
| 403 |
create_top_bar(container, run_animation=True) |
| 404 |
interface_data = get_interface_data() |
| 405 |
if interface_data.username: |
| 406 |
add_button(container, 'user', show_user_details, _('Logged in as {}').format(interface_data.username)) |
| 407 |
|
| 408 |
content = E.div(class_='cs-home-content') |
| 409 |
container.appendChild(content) |
| 410 |
|
| 411 |
# Recent books |
| 412 |
recent = E.div(style='display:none', class_='cs-home-section recently-read') |
| 413 |
recent_container_id = ensure_id(recent) |
| 414 |
content.appendChild(recent) |
| 415 |
if interface_data.username: |
| 416 |
conditional_timeout(recent_container_id, 5, show_recent_for_user_if_fetched) |
| 417 |
else: |
| 418 |
conditional_timeout(recent_container_id, 5, show_recent) |
| 419 |
|
| 420 |
# Choose library |
| 421 |
choose = E.div(class_='cs-home-section') |
| 422 |
content.appendChild(choose) |
| 423 |
choose.appendChild(E.h2(_('Choose the calibre library to browse…'))) |
| 424 |
choose.appendChild(E.div(class_='cs-home-actions')) |
| 425 |
cl = choose.lastChild |
| 426 |
for library_id, library_name in all_libraries(): |
| 427 |
library_name = interface_data.library_map[library_id] |
| 428 |
if library_name: |
| 429 |
cl.appendChild( |
| 430 |
create_button(library_name, icon='library', action=def (ev): |
| 431 |
lib_id = ev.currentTarget.dataset.lid |
| 432 |
q = {'library_id': lib_id} |
| 433 |
vlid = last_virtual_library_for(lib_id) |
| 434 |
if vlid: |
| 435 |
q.vl = vlid |
| 436 |
show_panel('book_list', q) |
| 437 |
)) |
| 438 |
cl.lastChild.dataset.lid = library_id |
| 439 |
|
| 440 |
# Newly added |
| 441 |
newly = E.div(style='display: none', class_='cs-home-section recently-read') |
| 442 |
newly.appendChild(E.h2(_('Newly added…'))) |
| 443 |
newly_container_id = ensure_id(newly) |
| 444 |
content.appendChild(newly) |
| 445 |
ajax('interface-data/newly-added', newly_added_received.bind(None, newly_container_id)).send() |
| 446 |
|
| 447 |
# Service worker status |
| 448 |
offline_mode = E.div( |
| 449 |
style='font-size:smaller', class_='cs-home-section recently-read') |
| 450 |
offline_mode.textContent = _('Offline mode is not supported') |
| 451 |
content.appendChild(offline_mode) |
| 452 |
if window.performance?.getEntriesByType: |
| 453 |
nav = window.performance.getEntriesByType('navigation') |
| 454 |
if nav and nav.length > 0: |
| 455 |
nav = nav[0] |
| 456 |
if nav.workerStart > 0: |
| 457 |
if (nav.transferSize is 0 and nav.decodedBodySize > 0): |
| 458 |
offline_mode.textContent = _('Loaded from cache in offline mode') |
| 459 |
else: |
| 460 |
offline_mode.textContent = _('Offline mode is available') |
| 461 |
else: |
| 462 |
if window.isSecureContext: |
| 463 |
offline_mode.textContent = _('Offline mode is possible, reload the page to enable it') |
| 464 |
else: |
| 465 |
offline_mode.textContent = _('Offline mode is not available, you need to setup HTTPS support in the calibre Content server') |
| 466 |
|
| 467 |
|
| 468 |
def set_panel_handlers(): |
| 469 |
set_default_panel_handler(init) |