| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2018, 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 ajax, ajax_send |
| 9 |
from book_list.book_details import report_load_failure |
| 10 |
from book_list.conversion_widgets import create_option_group, entry_points, commit_changes |
| 11 |
from book_list.library_data import download_url, load_status, url_books_query |
| 12 |
from book_list.router import back, open_book, report_a_load_failure |
| 13 |
from book_list.top_bar import create_top_bar, set_title |
| 14 |
from book_list.ui import set_panel_handler |
| 15 |
from dom import add_extra_css, build_rule, clear |
| 16 |
from modals import error_dialog |
| 17 |
from utils import conditional_timeout, human_readable, parse_url_params |
| 18 |
from widgets import create_button, enable_escape_key |
| 19 |
|
| 20 |
CLASS_NAME = 'convert-book-panel' |
| 21 |
current_state = 'initializing' |
| 22 |
overall_container_id = None |
| 23 |
initializers = {} |
| 24 |
|
| 25 |
add_extra_css(def (): |
| 26 |
sel = '.' + CLASS_NAME + ' ' |
| 27 |
style = build_rule(sel, padding='1ex 1rem') |
| 28 |
style += build_rule(sel + 'h3', margin_bottom='1ex') |
| 29 |
style += build_rule(sel + '.group-names', list_style='none', display='flex', flex_wrap='wrap') |
| 30 |
style += build_rule(sel + '.group-names li', padding='1ex 1rem', font_weight='bold', white_space='nowrap') |
| 31 |
return style |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
conversion_data = None |
| 36 |
conversion_data_load_status = {'loading': True, 'ok': False, 'error_html': None, 'current_fetch': None} |
| 37 |
|
| 38 |
|
| 39 |
def container_for_current_state(): |
| 40 |
ans = document.getElementById(overall_container_id) |
| 41 |
if ans: |
| 42 |
return ans.querySelector(f'[data-state="{current_state}"]') |
| 43 |
|
| 44 |
|
| 45 |
def create_converted_markup(): |
| 46 |
def init(container): |
| 47 |
clear(container) |
| 48 |
container.appendChild(E.h3('Conversion successful!')) |
| 49 |
fmt = conversion_data.fmt.toUpperCase() |
| 50 |
book_id = int(conversion_data.book_id) |
| 51 |
|
| 52 |
def read_book(): |
| 53 |
open_book(book_id, fmt) |
| 54 |
|
| 55 |
container.appendChild(E.div( |
| 56 |
style='margin-top: 1rem', |
| 57 |
create_button(_('Read {}').format(fmt), 'book', read_book), |
| 58 |
'\xa0', |
| 59 |
create_button(_('Download {}').format(fmt), 'cloud-download', download_url(book_id, fmt), |
| 60 |
_('File size: {}').format(human_readable(conversion_data.size)), |
| 61 |
download_filename=f'{conversion_data.title}.{fmt.toLowerCase()}') |
| 62 |
)) |
| 63 |
|
| 64 |
return E.div(), init |
| 65 |
|
| 66 |
|
| 67 |
def report_conversion_ajax_failure(xhr): |
| 68 |
nonlocal current_state |
| 69 |
current_state = 'configuring' |
| 70 |
apply_state_to_markup() |
| 71 |
error_dialog(_('Failed to convert'), _( |
| 72 |
'Could not convert {}. Click "Show details" for more' |
| 73 |
' information').format(conversion_data.title), |
| 74 |
xhr.error_html |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
def show_failure(response): |
| 79 |
nonlocal current_state |
| 80 |
current_state = 'failed' |
| 81 |
apply_state_to_markup() |
| 82 |
c = container_for_current_state() |
| 83 |
clear(c) |
| 84 |
c.appendChild(E.h3(_('Conversion failed!'))) |
| 85 |
c.appendChild(E.div(_('Error details below...'))) |
| 86 |
c.appendChild(E.div('\xa0')) |
| 87 |
c.appendChild(E.div()) |
| 88 |
c = c.lastChild |
| 89 |
if response.was_aborted: |
| 90 |
c.textContent = _( |
| 91 |
'Conversion of {} was taking too long, and has been aborted').format(conversion_data.title) |
| 92 |
else: |
| 93 |
log = '' |
| 94 |
if response.traceback: |
| 95 |
log += response.traceback |
| 96 |
if response.log: |
| 97 |
log += '\n\n' + _('Conversion log') + '\n\n' |
| 98 |
log += response.log |
| 99 |
c.appendChild(E.pre(log)) |
| 100 |
|
| 101 |
|
| 102 |
def on_conversion_status(end_type, xhr, ev): |
| 103 |
nonlocal current_state |
| 104 |
if current_state is not 'converting' or not container_for_current_state(): |
| 105 |
return # user clicked the back button similar |
| 106 |
if end_type is 'load': |
| 107 |
response = JSON.parse(xhr.responseText) |
| 108 |
if response.running: |
| 109 |
c = container_for_current_state() |
| 110 |
c.querySelector('progress').value = response.percent |
| 111 |
if response.msg: |
| 112 |
c.querySelector('.progress-msg').textContent = response.msg |
| 113 |
check_for_conversion_status() |
| 114 |
else: |
| 115 |
if response.ok: |
| 116 |
conversion_data.fmt = response.fmt |
| 117 |
conversion_data.size = response.size |
| 118 |
current_state = 'converted' |
| 119 |
apply_state_to_markup() |
| 120 |
else: |
| 121 |
show_failure(response) |
| 122 |
else: |
| 123 |
report_conversion_ajax_failure(xhr) |
| 124 |
|
| 125 |
|
| 126 |
def check_for_conversion_status(abort_job): |
| 127 |
query = url_books_query() |
| 128 |
if abort_job: |
| 129 |
query.abort_job = '1' |
| 130 |
data = {} |
| 131 |
ajax_send(f'conversion/status/{conversion_data.job_id}', data, on_conversion_status, query=query) |
| 132 |
|
| 133 |
|
| 134 |
def on_conversion_started(end_type, xhr, ev): |
| 135 |
nonlocal current_state |
| 136 |
if end_type is 'load': |
| 137 |
conversion_data.job_id = JSON.parse(xhr.responseText) |
| 138 |
check_for_conversion_status() |
| 139 |
else: |
| 140 |
report_conversion_ajax_failure(xhr) |
| 141 |
|
| 142 |
|
| 143 |
def get_conversion_options(container): |
| 144 |
return conversion_data.conversion_options.options |
| 145 |
|
| 146 |
|
| 147 |
def create_converting_markup(): |
| 148 |
ans = E.div( |
| 149 |
E.div( |
| 150 |
style='text-align: center; margin: auto', |
| 151 |
_('Converting, please wait…'), |
| 152 |
E.div(E.progress()), |
| 153 |
E.div('\xa0', class_='progress-msg'), |
| 154 |
E.div('\xa0'), |
| 155 |
E.div(_('Conversion progress can sometimes stall for a few minutes when converting complex books, that is normal.')), |
| 156 |
E.div('\xa0'), |
| 157 |
E.div(_('Click the close button in the top left corner to abort the conversion.')), |
| 158 |
) |
| 159 |
) |
| 160 |
|
| 161 |
def init(container): |
| 162 |
container.querySelector('progress').removeAttribute('value') |
| 163 |
|
| 164 |
return ans, init |
| 165 |
|
| 166 |
|
| 167 |
def current_input_format(): |
| 168 |
return document.getElementById(overall_container_id).querySelector('select[name="input_formats"]').value.toUpperCase() |
| 169 |
|
| 170 |
|
| 171 |
def current_output_format(): |
| 172 |
return document.getElementById(overall_container_id).querySelector('select[name="output_formats"]').value.toUpperCase() |
| 173 |
|
| 174 |
|
| 175 |
def start_conversion(): |
| 176 |
nonlocal current_state |
| 177 |
container = document.getElementById(overall_container_id) |
| 178 |
data = { |
| 179 |
'input_fmt': current_input_format(), |
| 180 |
'output_fmt': current_output_format(), |
| 181 |
'options': get_conversion_options(container), |
| 182 |
'book_id': conversion_data.book_id, |
| 183 |
} |
| 184 |
query = url_books_query() |
| 185 |
ajax_send(f'conversion/start/{conversion_data.book_id}', data, on_conversion_started, query=query) |
| 186 |
current_state = 'converting' |
| 187 |
apply_state_to_markup() |
| 188 |
|
| 189 |
|
| 190 |
def create_configuring_markup(): |
| 191 |
ignore_changes = False |
| 192 |
ans = E.div() |
| 193 |
|
| 194 |
def on_format_change(): |
| 195 |
nonlocal ignore_changes, current_state |
| 196 |
if ignore_changes: |
| 197 |
return |
| 198 |
input_fmt = container_for_current_state().querySelector('select[name="input_formats"]').value |
| 199 |
output_fmt = container_for_current_state().querySelector('select[name="output_formats"]').value |
| 200 |
current_state = 'initializing' |
| 201 |
conditional_timeout(overall_container_id, 5, check_for_data_loaded) |
| 202 |
q = parse_url_params() |
| 203 |
fetch_conversion_data(q.book_id, input_fmt, output_fmt) |
| 204 |
apply_state_to_markup() |
| 205 |
|
| 206 |
def generate_choice(name): |
| 207 |
ans = E.select(name=name) |
| 208 |
ans.addEventListener('change', on_format_change) |
| 209 |
return ans |
| 210 |
|
| 211 |
tcell = 'display: table-cell; padding-top: 1em; padding-left: 1em' |
| 212 |
|
| 213 |
start_conv = E.div( |
| 214 |
style='border-bottom: solid 1px currentColor; margin-bottom: 1em', |
| 215 |
E.div( |
| 216 |
E.label( |
| 217 |
style='display: table-row', |
| 218 |
E.div(style=tcell, _('Input format:')), |
| 219 |
E.div(generate_choice('input_formats'), style=tcell), |
| 220 |
), |
| 221 |
E.label( |
| 222 |
style='display: table-row', |
| 223 |
E.div(style=tcell, _('Output format:')), |
| 224 |
E.div(generate_choice('output_formats'), style=tcell), |
| 225 |
) |
| 226 |
), |
| 227 |
E.div( |
| 228 |
style='margin: 1em', |
| 229 |
create_button(_('Start conversion'), action=start_conversion) |
| 230 |
) |
| 231 |
) |
| 232 |
ans.appendChild(start_conv) |
| 233 |
ans.appendChild(E.div(style='margin-bottom: 1em', |
| 234 |
_('To change the settings for this conversion, click on one of the option groups below:'))) |
| 235 |
|
| 236 |
def show_group(ev): |
| 237 |
nonlocal current_state |
| 238 |
li = ev.currentTarget.closest('li') |
| 239 |
group_name = li.dataset.group |
| 240 |
group_title = li.firstChild.textContent |
| 241 |
conversion_data.configuring_group = group_name |
| 242 |
conversion_data.configuring_group_title = group_title |
| 243 |
current_state = 'configure-group' |
| 244 |
apply_state_to_markup() |
| 245 |
document.getElementById(overall_container_id).focus() |
| 246 |
|
| 247 |
def is_group_configurable(name): |
| 248 |
if entry_points[name]: |
| 249 |
return True |
| 250 |
if name is 'input_fmt': |
| 251 |
name = conversion_data.conversion_options.input_plugin_name |
| 252 |
elif name is 'output_fmt': |
| 253 |
name = conversion_data.conversion_options.output_plugin_name |
| 254 |
return bool(entry_points[name]) |
| 255 |
|
| 256 |
def category(name): |
| 257 |
ans = E.li(E.a(class_='simple-link', href='javascript: void(0)')) |
| 258 |
ans.dataset.group = name |
| 259 |
ans.firstChild.addEventListener('click', show_group) |
| 260 |
return ans |
| 261 |
|
| 262 |
GROUP_TITLES = { |
| 263 |
'heuristics': _('Heuristic processing'), |
| 264 |
'look_and_feel': _('Look & feel'), |
| 265 |
'page_setup': _('Page setup'), |
| 266 |
'search_and_replace': _('Search & replace'), |
| 267 |
'structure_detection': _('Structure detection'), |
| 268 |
'toc': _('Table of Contents'), |
| 269 |
} |
| 270 |
|
| 271 |
ans.appendChild( |
| 272 |
E.ul( |
| 273 |
class_='group-names', |
| 274 |
category('input_fmt'), |
| 275 |
category('look_and_feel'), |
| 276 |
category('page_setup'), |
| 277 |
category('search_and_replace'), |
| 278 |
category('structure_detection'), |
| 279 |
category('toc'), |
| 280 |
category('heuristics'), |
| 281 |
category('output_fmt') |
| 282 |
) |
| 283 |
) |
| 284 |
|
| 285 |
def initialize(container): |
| 286 |
nonlocal ignore_changes |
| 287 |
ignore_changes = True |
| 288 |
for name in 'input_formats', 'output_formats': |
| 289 |
sel = container.querySelector(f'select[name="{name}"]') |
| 290 |
clear(sel) |
| 291 |
formats = conversion_data[name] |
| 292 |
for fmt in formats: |
| 293 |
sel.appendChild(E.option(fmt, value=fmt)) |
| 294 |
ignore_changes = False |
| 295 |
for li in container.querySelectorAll('.group-names > li'): |
| 296 |
group_name = li.dataset.group |
| 297 |
li.style.display = 'block' if is_group_configurable(group_name) else 'none' |
| 298 |
if GROUP_TITLES[group_name]: |
| 299 |
title = GROUP_TITLES[group_name] |
| 300 |
elif group_name is 'input_fmt': |
| 301 |
title = _('{} input').format(current_input_format(), title) |
| 302 |
else: |
| 303 |
title = _('{} output').format(current_output_format(), title) |
| 304 |
li.firstChild.textContent = title |
| 305 |
|
| 306 |
return ans, initialize |
| 307 |
|
| 308 |
|
| 309 |
def get_option_value(name, defval): |
| 310 |
ans = conversion_data.conversion_options.options[name] |
| 311 |
if ans is undefined: |
| 312 |
ans = defval |
| 313 |
return ans |
| 314 |
|
| 315 |
|
| 316 |
def get_option_default_value(name, defval): |
| 317 |
ans = conversion_data.conversion_options.defaults[name] |
| 318 |
if ans is undefined: |
| 319 |
ans = defval |
| 320 |
return ans |
| 321 |
|
| 322 |
|
| 323 |
def set_option_value(name, val): |
| 324 |
conversion_data.conversion_options.options[name] = val |
| 325 |
|
| 326 |
|
| 327 |
def is_option_disabled(name): |
| 328 |
return conversion_data.disabled_map[name] is True |
| 329 |
|
| 330 |
|
| 331 |
def get_option_help(name): |
| 332 |
return conversion_data.conversion_options.help[name] or '' |
| 333 |
|
| 334 |
|
| 335 |
def create_configure_group_markup(): |
| 336 |
ans = E.div() |
| 337 |
|
| 338 |
def init(container): |
| 339 |
clear(container) |
| 340 |
container.appendChild(E.h3( |
| 341 |
style='margin-bottom: 1ex', |
| 342 |
_('Configuring {} settings').format(conversion_data.configuring_group_title))) |
| 343 |
panel = E.div() |
| 344 |
container.appendChild(panel) |
| 345 |
g = conversion_data.configuring_group |
| 346 |
ui_data = None |
| 347 |
if g is 'input_fmt': |
| 348 |
g = conversion_data.conversion_options.input_plugin_name |
| 349 |
ui_data = conversion_data.conversion_options.input_ui_data |
| 350 |
elif g is 'output_fmt': |
| 351 |
g = conversion_data.conversion_options.output_plugin_name |
| 352 |
ui_data = conversion_data.conversion_options.output_ui_data |
| 353 |
create_option_group( |
| 354 |
g, |
| 355 |
ui_data, |
| 356 |
conversion_data.profiles, |
| 357 |
container, |
| 358 |
get_option_value, |
| 359 |
get_option_default_value, |
| 360 |
is_option_disabled, |
| 361 |
get_option_help, |
| 362 |
on_close |
| 363 |
) |
| 364 |
|
| 365 |
return ans, init |
| 366 |
|
| 367 |
|
| 368 |
# Initialization {{{ |
| 369 |
|
| 370 |
|
| 371 |
def on_data_loaded(end_type, xhr, ev): |
| 372 |
nonlocal conversion_data |
| 373 |
conversion_data_load_status.current_fetch = None |
| 374 |
conversion_data_load_status.loading = False |
| 375 |
conversion_data_load_status.ok = True |
| 376 |
conversion_data_load_status.error_html = None |
| 377 |
|
| 378 |
def bad_load(msg): |
| 379 |
conversion_data_load_status.ok = False |
| 380 |
conversion_data_load_status.error_html = msg or xhr.error_html |
| 381 |
|
| 382 |
if end_type is 'load': |
| 383 |
conversion_data = JSON.parse(xhr.responseText) |
| 384 |
conversion_data.disabled_map = {} |
| 385 |
for name in conversion_data.conversion_options.disabled: |
| 386 |
conversion_data.disabled_map[name] = True |
| 387 |
elif end_type is 'abort': |
| 388 |
pass |
| 389 |
else: |
| 390 |
bad_load() |
| 391 |
|
| 392 |
|
| 393 |
def fetch_conversion_data(book_id, input_fmt, output_fmt): |
| 394 |
nonlocal conversion_data |
| 395 |
if conversion_data_load_status.current_fetch: |
| 396 |
conversion_data_load_status.current_fetch.abort() |
| 397 |
conversion_data = None |
| 398 |
query = url_books_query() |
| 399 |
if input_fmt: |
| 400 |
query.input_fmt = input_fmt |
| 401 |
if output_fmt: |
| 402 |
query.output_fmt = output_fmt |
| 403 |
conversion_data_load_status.loading = True |
| 404 |
conversion_data_load_status.ok = False |
| 405 |
conversion_data_load_status.error_html = None |
| 406 |
conversion_data_load_status.current_fetch = ajax(f'conversion/book-data/{book_id}', on_data_loaded, query=query) |
| 407 |
conversion_data_load_status.current_fetch.send() |
| 408 |
|
| 409 |
|
| 410 |
def on_close(container_id): |
| 411 |
nonlocal current_state |
| 412 |
if current_state is 'configure-group': |
| 413 |
commit_changes(set_option_value) |
| 414 |
current_state = 'configuring' |
| 415 |
apply_state_to_markup() |
| 416 |
return |
| 417 |
if current_state is 'converting': |
| 418 |
check_for_conversion_status(True) |
| 419 |
current_state = 'initializing' |
| 420 |
apply_state_to_markup() |
| 421 |
back() |
| 422 |
|
| 423 |
|
| 424 |
def check_for_data_loaded(): |
| 425 |
nonlocal current_state |
| 426 |
container = this |
| 427 |
if load_status.loading or conversion_data_load_status.loading: |
| 428 |
conditional_timeout(container.id, 5, check_for_data_loaded) |
| 429 |
return |
| 430 |
container = container.lastChild |
| 431 |
if not load_status.ok: |
| 432 |
current_state = 'load-failure' |
| 433 |
report_load_failure(container_for_current_state()) |
| 434 |
elif not conversion_data_load_status.ok: |
| 435 |
current_state = 'load-failure' |
| 436 |
report_a_load_failure( |
| 437 |
container_for_current_state(), |
| 438 |
_('Failed to load conversion data from calibre, with error:'), |
| 439 |
conversion_data_load_status.error_html) |
| 440 |
else: |
| 441 |
set_title(container.parentNode, _('Convert: {}').format(conversion_data.title)) |
| 442 |
current_state = 'configuring' |
| 443 |
apply_state_to_markup() |
| 444 |
|
| 445 |
|
| 446 |
def create_markup(container): |
| 447 |
container.appendChild(E.div( |
| 448 |
data_state='initializing', |
| 449 |
E.div(_('Loading conversion data, please wait...')) |
| 450 |
)) |
| 451 |
container.appendChild(E.div(data_state='load-failure')) |
| 452 |
container.appendChild(E.div(data_state='failed')) |
| 453 |
|
| 454 |
def cm(name, func): |
| 455 |
ccm, init = func() |
| 456 |
ccm.dataset.state = name |
| 457 |
container.appendChild(ccm) |
| 458 |
initializers[name] = init |
| 459 |
|
| 460 |
cm('configuring', create_configuring_markup) |
| 461 |
cm('converting', create_converting_markup) |
| 462 |
cm('converted', create_converted_markup) |
| 463 |
cm('configure-group', create_configure_group_markup) |
| 464 |
|
| 465 |
|
| 466 |
def apply_state_to_markup(): |
| 467 |
container = document.getElementById(overall_container_id) |
| 468 |
if container: |
| 469 |
for node in container.lastChild.childNodes: |
| 470 |
if node.dataset.state is current_state: |
| 471 |
node.style.display = 'block' |
| 472 |
if initializers[current_state]: |
| 473 |
initializers[current_state](node) |
| 474 |
else: |
| 475 |
node.style.display = 'none' |
| 476 |
|
| 477 |
|
| 478 |
def init(container_id): |
| 479 |
nonlocal overall_container_id |
| 480 |
container = document.getElementById(container_id) |
| 481 |
overall_container_id = container_id |
| 482 |
create_top_bar(container, title=_('Convert book'), action=on_close.bind(None, container_id), icon='close') |
| 483 |
container.appendChild(E.div(class_=CLASS_NAME)) |
| 484 |
create_markup(container.lastChild) |
| 485 |
apply_state_to_markup() |
| 486 |
conditional_timeout(container_id, 5, check_for_data_loaded) |
| 487 |
q = parse_url_params() |
| 488 |
fetch_conversion_data(q.book_id) |
| 489 |
enable_escape_key(container, on_close.bind(None, container_id)) |
| 490 |
|
| 491 |
|
| 492 |
def set_panel_handlers(): |
| 493 |
set_panel_handler('convert_book', init) |
| 494 |
# }}} |