| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from gettext import gettext as _ |
| 6 |
|
| 7 |
from read_book.cfi import create_cfi_cmp, cfi_sort_key |
| 8 |
from read_book.globals import ui_operations |
| 9 |
|
| 10 |
|
| 11 |
no_cfi = '/99999999' |
| 12 |
|
| 13 |
|
| 14 |
def bookmark_get_cfi(b): |
| 15 |
if b.pos_type is 'epubcfi': |
| 16 |
return b.pos[8:-1] |
| 17 |
return no_cfi |
| 18 |
|
| 19 |
|
| 20 |
def highlight_get_cfi(hl): |
| 21 |
cfi = hl.start_cfi |
| 22 |
if cfi: |
| 23 |
si = f'/{hl.spine_index}' if hl.spine_index? else no_cfi |
| 24 |
return si + cfi |
| 25 |
return no_cfi |
| 26 |
|
| 27 |
|
| 28 |
def sort_annot_list(annots, get_cfi_func): |
| 29 |
key_map = {no_cfi: cfi_sort_key(no_cfi)} |
| 30 |
for annot in annots: |
| 31 |
cfi = get_cfi_func(annot) |
| 32 |
if cfi and not key_map[cfi]: |
| 33 |
key_map[cfi] = cfi_sort_key(cfi) |
| 34 |
cfi_cmp = create_cfi_cmp(key_map) |
| 35 |
annots.sort(def (a, b): |
| 36 |
acfi = get_cfi_func(a) |
| 37 |
bcfi = get_cfi_func(b) |
| 38 |
return cfi_cmp(acfi, bcfi) |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def annots_descending_cmp(a, b): |
| 43 |
return -1 if a.timestamp > b.timestamp else (1 if a.timestamp < b.timestamp else 0) |
| 44 |
|
| 45 |
|
| 46 |
def merge_annots_with_identical_field(annots_a, annots_b, field, cfi_getter_func): |
| 47 |
title_groups = {} |
| 48 |
changed = False |
| 49 |
all_annots = annots_a.concat(annots_b) |
| 50 |
for a in all_annots: |
| 51 |
q = title_groups[a[field]] |
| 52 |
if not q: |
| 53 |
q = title_groups[a[field]] = [] |
| 54 |
q.push(a) |
| 55 |
for tg in Object.values(title_groups): |
| 56 |
tg.sort(annots_descending_cmp) |
| 57 |
seen = {} |
| 58 |
ans = [] |
| 59 |
for a in all_annots: |
| 60 |
title = a[field] |
| 61 |
if not seen[title]: |
| 62 |
seen[title] = True |
| 63 |
candidates = title_groups[title] |
| 64 |
if not changed and candidates.length > 1 and candidates[0].timestamp is not candidates[1].timestamp: |
| 65 |
changed = True |
| 66 |
ans.push(title_groups[title][0]) |
| 67 |
if ans.length is not annots_a.length or ans.length is not annots_b.length: |
| 68 |
changed = True |
| 69 |
if changed: |
| 70 |
sort_annot_list(ans, cfi_getter_func) |
| 71 |
return changed, ans |
| 72 |
|
| 73 |
|
| 74 |
field_map = {'bookmark': 'title', 'highlight': 'uuid'} |
| 75 |
getter_map = {'bookmark': bookmark_get_cfi, 'highlight': highlight_get_cfi} |
| 76 |
|
| 77 |
|
| 78 |
def merge_annot_lists(a, b, field): |
| 79 |
key_field = field_map[field] |
| 80 |
return merge_annots_with_identical_field(a, b, key_field, getter_map[field]) |
| 81 |
|
| 82 |
|
| 83 |
def merge_annotation_maps(a, b): |
| 84 |
# If you make changes to this algorithm also update the |
| 85 |
# implementation in calibre.db.annotations |
| 86 |
updated = False |
| 87 |
ans = {} |
| 88 |
for field in field_map: |
| 89 |
a_items = a[field] or [] |
| 90 |
b_items = b[field] or [] |
| 91 |
if not a_items.length: |
| 92 |
ans[field] = b_items |
| 93 |
updated = True |
| 94 |
continue |
| 95 |
if not b_items.length: |
| 96 |
ans[field] = a_items |
| 97 |
continue |
| 98 |
changed, ans[field] = merge_annot_lists(a_items, b_items, field) |
| 99 |
if changed: |
| 100 |
updated = True |
| 101 |
|
| 102 |
return updated, ans |
| 103 |
|
| 104 |
|
| 105 |
class AnnotationsManager: # {{{ |
| 106 |
def __init__(self, view): |
| 107 |
self.view = view |
| 108 |
self.set_highlights() |
| 109 |
self.set_bookmarks() |
| 110 |
|
| 111 |
def sync_annots_to_server(self, which): |
| 112 |
if ui_operations.annots_changed: |
| 113 |
amap = {} |
| 114 |
if not which or which is 'bookmarks': |
| 115 |
amap.bookmark = self.bookmarks.as_array() |
| 116 |
if not which or which is 'highlights': |
| 117 |
amap.highlight = Object.values(self.highlights) |
| 118 |
ui_operations.annots_changed(amap) |
| 119 |
|
| 120 |
def set_bookmarks(self, bookmarks): |
| 121 |
bookmarks = bookmarks or [] |
| 122 |
self.bookmarks = list(bookmarks) |
| 123 |
|
| 124 |
def all_bookmarks(self): |
| 125 |
return self.bookmarks |
| 126 |
|
| 127 |
def add_bookmark(self, title, cfi): |
| 128 |
if not title or not cfi: |
| 129 |
return |
| 130 |
self.bookmarks = [b for b in self.bookmarks if b.title is not title] |
| 131 |
self.bookmarks.push({ |
| 132 |
'type': 'bookmark', |
| 133 |
'timestamp': Date().toISOString(), |
| 134 |
'pos_type': 'epubcfi', |
| 135 |
'pos': cfi, |
| 136 |
'title': title, |
| 137 |
}) |
| 138 |
sort_annot_list(self.bookmarks, bookmark_get_cfi) |
| 139 |
self.sync_annots_to_server('bookmarks') |
| 140 |
|
| 141 |
def remove_bookmark(self, title): |
| 142 |
changed = False |
| 143 |
for b in self.bookmarks: |
| 144 |
if b.title is title: |
| 145 |
b.removed = True |
| 146 |
b.timestamp = Date().toISOString() |
| 147 |
changed = True |
| 148 |
if changed: |
| 149 |
self.sync_annots_to_server('bookmarks') |
| 150 |
return changed |
| 151 |
|
| 152 |
def edit_bookmark(self, title, new_title): |
| 153 |
changed = False |
| 154 |
for b in self.bookmarks: |
| 155 |
if b.title is title: |
| 156 |
b.title = new_title |
| 157 |
b.timestamp = Date().toISOString() |
| 158 |
changed = True |
| 159 |
if changed: |
| 160 |
self.sync_annots_to_server('bookmarks') |
| 161 |
return changed |
| 162 |
|
| 163 |
def default_bookmark_title(self, base_default_title): |
| 164 |
all_titles = {bm.title: True for bm in self.bookmarks if not bm.removed} |
| 165 |
base_default_title = base_default_title or _('Bookmark') |
| 166 |
c = 0 |
| 167 |
while True: |
| 168 |
c += 1 |
| 169 |
default_title = f'{base_default_title} #{c}' |
| 170 |
if not all_titles[default_title]: |
| 171 |
return default_title |
| 172 |
|
| 173 |
def set_highlights(self, highlights): |
| 174 |
highlights = highlights or [] |
| 175 |
self.highlights = {h.uuid: h for h in highlights} |
| 176 |
|
| 177 |
def all_highlights(self): |
| 178 |
ans = [h for h in Object.values(self.highlights) if not h.removed].as_array() |
| 179 |
sort_annot_list(ans, highlight_get_cfi) |
| 180 |
return ans |
| 181 |
|
| 182 |
def merge_highlights(self, highlights): |
| 183 |
highlights = highlights or [] |
| 184 |
updated = False |
| 185 |
if highlights.length: |
| 186 |
base = {'highlight': Object.values(self.highlights)} |
| 187 |
newvals = {'highlight': highlights} |
| 188 |
updated, ans = merge_annotation_maps(base, newvals) |
| 189 |
if updated: |
| 190 |
self.set_highlights(ans.highlight) |
| 191 |
return updated |
| 192 |
|
| 193 |
def remove_highlight(self, uuid): |
| 194 |
h = self.highlights[uuid] |
| 195 |
if h: |
| 196 |
h.timestamp = Date().toISOString() |
| 197 |
h.removed = True |
| 198 |
v'delete h.style' |
| 199 |
v'delete h.highlighted_text' |
| 200 |
v'delete h.start_cfi' |
| 201 |
v'delete h.end_cfi' |
| 202 |
v'delete h.notes' |
| 203 |
v'delete h.spine_name' |
| 204 |
v'delete h.spine_index' |
| 205 |
v'delete h.toc_family_titles' |
| 206 |
return True |
| 207 |
|
| 208 |
def delete_highlight(self, uuid): |
| 209 |
if self.remove_highlight(uuid): |
| 210 |
self.sync_annots_to_server('highlights') |
| 211 |
|
| 212 |
def notes_for_highlight(self, uuid): |
| 213 |
h = self.highlights[uuid] if uuid else None |
| 214 |
if h: |
| 215 |
return h.notes |
| 216 |
|
| 217 |
def text_for_highlight(self, uuid): |
| 218 |
h = self.highlights[uuid] if uuid else None |
| 219 |
if h: |
| 220 |
return h.highlighted_text |
| 221 |
|
| 222 |
def set_notes_for_highlight(self, uuid, notes): |
| 223 |
h = self.highlights[uuid] |
| 224 |
if h: |
| 225 |
if notes: |
| 226 |
h.notes = notes |
| 227 |
else: |
| 228 |
v'delete h.notes' |
| 229 |
self.sync_annots_to_server('highlights') |
| 230 |
return True |
| 231 |
return False |
| 232 |
|
| 233 |
def set_style_for_highlights(self, updates): |
| 234 |
now = Date().toISOString() |
| 235 |
changed = False |
| 236 |
for item in updates: |
| 237 |
h = self.highlights[item.uuid] |
| 238 |
if h: |
| 239 |
h.style = item.style |
| 240 |
h.timestamp = now |
| 241 |
changed = True |
| 242 |
if changed: |
| 243 |
self.sync_annots_to_server('highlights') |
| 244 |
return changed |
| 245 |
|
| 246 |
def style_for_highlight(self, uuid): |
| 247 |
h = self.highlights[uuid] |
| 248 |
if h: |
| 249 |
return h.style |
| 250 |
|
| 251 |
def data_for_highlight(self, uuid): |
| 252 |
return self.highlights[uuid] |
| 253 |
|
| 254 |
def spine_index_for_highlight(self, uuid, spine): |
| 255 |
h = self.highlights[uuid] |
| 256 |
if not h: |
| 257 |
return -1 |
| 258 |
ans = h.spine_index |
| 259 |
name = h.spine_name |
| 260 |
if name: |
| 261 |
idx = spine.indexOf(name) |
| 262 |
if idx > -1: |
| 263 |
ans = idx |
| 264 |
return ans |
| 265 |
|
| 266 |
def cfi_for_highlight(self, uuid, spine_index): |
| 267 |
h = self.highlights[uuid] |
| 268 |
if h: |
| 269 |
x = 2 * (spine_index + 1) |
| 270 |
return f'epubcfi(/{x}{h.start_cfi})' |
| 271 |
|
| 272 |
def add_highlight(self, msg, style, notes, toc_family): |
| 273 |
now = Date().toISOString() |
| 274 |
for uuid in msg.removed_highlights: |
| 275 |
self.remove_highlight(uuid) |
| 276 |
|
| 277 |
annot = self.highlights[msg.uuid] = { |
| 278 |
'type': 'highlight', |
| 279 |
'timestamp': now, |
| 280 |
'uuid': msg.uuid, |
| 281 |
'highlighted_text': msg.highlighted_text, |
| 282 |
'start_cfi': msg.bounds.start, |
| 283 |
'end_cfi': msg.bounds.end, |
| 284 |
'style': style, # dict with color and background-color |
| 285 |
'spine_name': self.view.currently_showing.name, |
| 286 |
'spine_index': self.view.currently_showing.spine_index, |
| 287 |
} |
| 288 |
if notes: |
| 289 |
annot.notes = notes |
| 290 |
if toc_family?.length: |
| 291 |
toc_family_titles = [] |
| 292 |
for x in toc_family: |
| 293 |
if x.title: |
| 294 |
toc_family_titles.push(x.title) |
| 295 |
annot.toc_family_titles = toc_family_titles |
| 296 |
self.sync_annots_to_server('highlights') |
| 297 |
|
| 298 |
def highlights_for_currently_showing(self): |
| 299 |
name = self.view.currently_showing.name |
| 300 |
ans = [] |
| 301 |
for h in Object.values(self.highlights): |
| 302 |
if h.spine_name is name and not h.removed and h.start_cfi: |
| 303 |
ans.push(h) |
| 304 |
return ans |
| 305 |
# }}} |