| 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 |
| 9 |
from book_list.add import write_access_error |
| 10 |
from book_list.library_data import book_after, force_refresh_on_next_load, remove_book |
| 11 |
from book_list.router import back |
| 12 |
from book_list.ui import show_panel |
| 13 |
from dom import clear |
| 14 |
from modals import create_custom_dialog |
| 15 |
from read_book.db import get_db |
| 16 |
from utils import safe_set_inner_html |
| 17 |
from widgets import create_button |
| 18 |
|
| 19 |
|
| 20 |
def delete_from_cache(library_id, book_id, title): |
| 21 |
db = get_db() |
| 22 |
db.delete_books_matching(library_id, book_id) |
| 23 |
|
| 24 |
|
| 25 |
def refresh_after_delete(book_id, library_id): |
| 26 |
force_refresh_on_next_load() |
| 27 |
next_book_id = book_after(book_id) |
| 28 |
remove_book(book_id) |
| 29 |
if next_book_id: |
| 30 |
show_panel('book_details', {'book_id': str(next_book_id), 'library_id': library_id}, True) |
| 31 |
else: |
| 32 |
back() |
| 33 |
|
| 34 |
|
| 35 |
def do_delete_from_library(parent, close_modal, library_id, book_id, title): |
| 36 |
parent.appendChild(E.div(_('Deleting {0} from library, please wait...').format(title))) |
| 37 |
|
| 38 |
def oncomplete(end_type, xhr, ev): |
| 39 |
if end_type is 'load' or end_type is 'abort': |
| 40 |
close_modal() |
| 41 |
if end_type is 'load': |
| 42 |
refresh_after_delete(book_id, library_id) |
| 43 |
return |
| 44 |
clear(parent) |
| 45 |
msg = E.div() |
| 46 |
safe_set_inner_html(msg, write_access_error(_('Failed to delete {0}, with error:').format(title), xhr)) |
| 47 |
parent.appendChild(E.div( |
| 48 |
msg, |
| 49 |
E.div(class_='button-box', |
| 50 |
create_button(_('Close'), None, close_modal, True) |
| 51 |
) |
| 52 |
)) |
| 53 |
|
| 54 |
ajax(f'cdb/delete-books/{book_id}/{library_id}', oncomplete, method='POST').send() |
| 55 |
|
| 56 |
|
| 57 |
def confirm_delete_from_library(library_id, book_id, title): |
| 58 |
create_custom_dialog(_('Are you sure?'), def (parent, close_modal): |
| 59 |
def action(doit): |
| 60 |
if doit: |
| 61 |
clear(parent) |
| 62 |
do_delete_from_library(parent, close_modal, library_id, book_id, title) |
| 63 |
else: |
| 64 |
close_modal() |
| 65 |
msg = _('This will <b>permanently delete</b> <i>{0}</i> from your calibre library. Are you sure?').format(title) |
| 66 |
m = E.div() |
| 67 |
safe_set_inner_html(m, msg) |
| 68 |
parent.appendChild(E.div( |
| 69 |
m, |
| 70 |
E.div(class_='button-box', |
| 71 |
create_button(_('Cancel'), None, action.bind(None, False)), |
| 72 |
create_button(_('OK'), None, action.bind(None, True), highlight=True), |
| 73 |
) |
| 74 |
)) |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
def choose_which_delete(library_id, book_id, title): |
| 79 |
create_custom_dialog(_('Delete from?'), def (parent, close_modal): |
| 80 |
def action(from_cache, from_library): |
| 81 |
close_modal() |
| 82 |
if from_cache: |
| 83 |
delete_from_cache(library_id, book_id, title) |
| 84 |
if from_library: |
| 85 |
confirm_delete_from_library(library_id, book_id, title) |
| 86 |
parent.appendChild(E.div( |
| 87 |
E.div(_('{0} is available both in the browser cache for offline reading and in' |
| 88 |
' your calibre library. Where do you want to delete it from?').format(title)), |
| 89 |
E.div(class_='button-box', |
| 90 |
create_button(_('Cache'), None, action.bind(None, True, False)), |
| 91 |
create_button(_('Library and cache'), None, action.bind(None, True, True)), |
| 92 |
) |
| 93 |
)) |
| 94 |
) |
| 95 |
|
| 96 |
|
| 97 |
def delete_book_stage2(library_id, book_id, title, has_offline_copies): |
| 98 |
if has_offline_copies: |
| 99 |
choose_which_delete(library_id, book_id, title) |
| 100 |
else: |
| 101 |
confirm_delete_from_library(library_id, book_id, title) |
| 102 |
|
| 103 |
|
| 104 |
def start_delete_book(library_id, book_id, title): |
| 105 |
book_id = int(book_id) |
| 106 |
db = get_db() |
| 107 |
db.has_book_matching(library_id, book_id, delete_book_stage2.bind(None, library_id, book_id, title)) |