-
Notifications
You must be signed in to change notification settings - Fork 17
/
readactivity.py
1332 lines (1092 loc) · 49.9 KB
/
readactivity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (C) 2007, Red Hat, Inc.
# Copyright (C) 2007 Collabora Ltd. <http://www.collabora.co.uk/>
# Copyright 2008 One Laptop Per Child
# Copyright 2009 Simon Schampijer
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import logging
import os
import time
from gettext import gettext as _
import re
import hashlib
import io
import cairo
import json
import dbus
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gst', '1.0')
gi.require_version('TelepathyGLib', '0.12')
from gi.repository import GLib
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import Gio
from gi.repository import TelepathyGLib
GObject.threads_init()
from sugar3.activity import activity
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.toolbarbox import ToolbarBox
from sugar3.graphics.toolbarbox import ToolbarButton
from sugar3.graphics.toggletoolbutton import ToggleToolButton
from sugar3.graphics.alert import ConfirmationAlert
from sugar3.graphics.alert import Alert
from sugar3.activity.widgets import ActivityToolbarButton
from sugar3.activity.widgets import StopButton
from sugar3.graphics.tray import HTray
from sugar3.graphics.menuitem import MenuItem
from sugar3 import network
from sugar3 import profile
from sugar3.datastore import datastore
from sugar3.graphics.objectchooser import ObjectChooser
from sugar3.graphics.objectchooser import FILTER_TYPE_MIME_BY_ACTIVITY
from sugar3.graphics import style
import emptypanel
from readtoolbar import EditToolbar
from readtoolbar import ViewToolbar
from bookmarkview import BookmarkView
from readdb import BookmarkManager
from linkbutton import LinkButton
from speechtoolbar import SpeechToolbar
_HARDWARE_MANAGER_INTERFACE = 'org.laptop.HardwareManager'
_HARDWARE_MANAGER_SERVICE = 'org.laptop.HardwareManager'
_HARDWARE_MANAGER_OBJECT_PATH = '/org/laptop/HardwareManager'
_TOOLBAR_READ = 2
_logger = logging.getLogger('read-activity')
def _get_screen_dpi():
xft_dpi = Gtk.Settings.get_default().get_property('gtk-xft-dpi')
_logger.debug('Setting dpi to %f', float(xft_dpi / 1024))
return float(xft_dpi / 1024)
def get_md5(filename):
# FIXME: Should be moved somewhere else
filename = filename.replace('file://', '') # XXX: hack
fh = open(filename, 'rb')
digest = hashlib.md5()
while 1:
buf = fh.read(4096)
if buf == b'':
break
digest.update(buf)
fh.close()
return digest.hexdigest()
class ReadHTTPRequestHandler(network.ChunkedGlibHTTPRequestHandler):
"""HTTP Request Handler for transferring document while collaborating.
RequestHandler class that integrates with Glib mainloop. It writes
the specified file to the client in chunks, returning control to the
mainloop between chunks.
"""
def translate_path(self, path):
"""Return the filepath to the shared document."""
if path.endswith('document'):
return self.server.filepath
if path.endswith('metadata'):
return self.server.get_metadata_path()
class ReadHTTPServer(network.GlibTCPServer):
"""HTTP Server for transferring document while collaborating."""
def __init__(self, server_address, filepath, create_metadata_cb):
"""Set up the GlibTCPServer with the ReadHTTPRequestHandler.
filepath -- path to shared document to be served.
"""
self.filepath = filepath
self._create_metadata_cb = create_metadata_cb
network.GlibTCPServer.__init__(self, server_address,
ReadHTTPRequestHandler)
def get_metadata_path(self):
return self._create_metadata_cb()
class ReadURLDownloader(network.GlibURLDownloader):
"""URLDownloader that provides content-length and content-type."""
def get_content_length(self):
"""Return the content-length of the download."""
if self._info is not None:
return int(self._info.headers.get('Content-Length'))
def get_content_type(self):
"""Return the content-type of the download."""
if self._info is not None:
return self._info.headers.get('Content-type')
return None
READ_STREAM_SERVICE = 'read-activity-http'
class ProgressAlert(Alert):
"""
Progress alert with a progressbar - to show the advance of a task
"""
def __init__(self, timeout=5, **kwargs):
Alert.__init__(self, **kwargs)
self._pb = Gtk.ProgressBar()
self._msg_box.pack_start(self._pb, False, False, 0)
self._pb.set_size_request(int(Gdk.Screen.width() * 9. / 10.), -1)
self._pb.set_fraction(0.0)
self._pb.show()
def set_fraction(self, fraction):
# update only by 10% fractions
if int(fraction * 100) % 10 == 0:
self._pb.set_fraction(fraction)
self._pb.queue_draw()
# force updating the progressbar
while Gtk.events_pending():
Gtk.main_iteration_do(True)
class ReadActivity(activity.Activity):
"""The Read sugar activity."""
def __init__(self, handle):
activity.Activity.__init__(self, handle)
self.max_participants = 1
self._document = None
self._fileserver = None
self._object_id = handle.object_id
self._toc_model = None
self.filehash = None
self.connect('key-press-event', self._key_press_event_cb)
self.connect('key-release-event', self._key_release_event_cb)
_logger.debug('Starting Read...')
self._view = None
self.dpi = _get_screen_dpi()
self._bookmark_view = BookmarkView()
self._bookmark_view.connect('bookmark-changed',
self._update_bookmark_cb)
tray = HTray()
self.set_tray(tray, Gtk.PositionType.BOTTOM)
toolbar_box = ToolbarBox()
self.activity_button = ActivityToolbarButton(self)
toolbar_box.toolbar.insert(self.activity_button, 0)
self.activity_button.show()
self._edit_toolbar = EditToolbar()
self._edit_toolbar.undo.props.visible = False
self._edit_toolbar.redo.props.visible = False
self._edit_toolbar.separator.props.visible = False
self._edit_toolbar.copy.set_sensitive(False)
self._edit_toolbar.copy.connect('clicked', self._edit_toolbar_copy_cb)
self._edit_toolbar.paste.props.visible = False
edit_toolbar_button = ToolbarButton(page=self._edit_toolbar,
icon_name='toolbar-edit')
self._edit_toolbar.show()
toolbar_box.toolbar.insert(edit_toolbar_button, -1)
edit_toolbar_button.show()
self._highlight = self._edit_toolbar.highlight
self._highlight_id = self._highlight.connect('clicked',
self.__highlight_cb)
self._view_toolbar = ViewToolbar()
self._view_toolbar.connect('go-fullscreen',
self.__view_toolbar_go_fullscreen_cb)
self._view_toolbar.connect('toggle-index-show',
self.__toggle_navigator_cb)
self._view_toolbar.connect('toggle-tray-show',
self.__toggle_tray_cb)
self._view_toolbar.connect('toggle-inverted-colors',
self.__toggle_inverted_colors_cb)
view_toolbar_button = ToolbarButton(page=self._view_toolbar,
icon_name='toolbar-view')
self._view_toolbar.show()
toolbar_box.toolbar.insert(view_toolbar_button, -1)
view_toolbar_button.show()
self._back_button = self._create_back_button()
toolbar_box.toolbar.insert(self._back_button, -1)
self._back_button.show()
self._forward_button = self._create_forward_button()
toolbar_box.toolbar.insert(self._forward_button, -1)
self._forward_button.show()
num_page_item = Gtk.ToolItem()
self._num_page_entry = self._create_search()
num_page_item.add(self._num_page_entry)
self._num_page_entry.show()
toolbar_box.toolbar.insert(num_page_item, -1)
num_page_item.show()
total_page_item = Gtk.ToolItem()
self._total_page_label = Gtk.Label()
total_page_item.add(self._total_page_label)
self._total_page_label.show()
self._total_page_label.set_margin_right(5)
toolbar_box.toolbar.insert(total_page_item, -1)
total_page_item.show()
self._bookmarker = ToggleToolButton('emblem-favorite')
self._bookmarker_toggle_handler_id = self._bookmarker.connect(
'toggled', self.__bookmarker_toggled_cb)
self._bookmarker.show()
toolbar_box.toolbar.insert(self._bookmarker, -1)
self.speech_toolbar_button = ToolbarButton(icon_name='speak')
toolbar_box.toolbar.insert(self.speech_toolbar_button, -1)
separator = Gtk.SeparatorToolItem()
separator.props.draw = False
separator.set_size_request(0, -1)
separator.set_expand(True)
toolbar_box.toolbar.insert(separator, -1)
separator.show()
stop_button = StopButton(self)
toolbar_box.toolbar.insert(stop_button, -1)
stop_button.show()
self.set_toolbar_box(toolbar_box)
toolbar_box.show()
# This is needed to prevent the call of read_file on
# canvas map, becuase interact in a bad way with the emptypanel
# the program takes responsability of this task.
self._read_file_called = True
self._vbox = Gtk.VBox()
self._vbox.show()
overlay = Gtk.Overlay()
self._hbox = Gtk.HBox()
self._hbox.show()
overlay.add(self._hbox)
self._bookmark_view.props.halign = Gtk.Align.END
self._bookmark_view.props.valign = Gtk.Align.START
# HACK: This is to calculate the scrollbar width
# defined in sugar-artwork gtk-widgets.css.em
if style.zoom(1):
scrollbar_width = 15
else:
scrollbar_width = 11
self._bookmark_view.props.margin_right = scrollbar_width
overlay.add_overlay(self._bookmark_view)
overlay.show()
self._vbox.pack_start(overlay, True, True, 0)
self.set_canvas(self._vbox)
self._navigator = self._create_navigator()
# Set up for idle suspend
self._idle_timer = 0
self._service = None
# start with sleep off
self._sleep_inhibit = True
self.unused_download_tubes = set()
self._want_document = True
self._download_content_length = 0
self._download_content_type = None
# Status of temp file used for write_file:
self._tempfile = None
self._close_requested = False
fname = os.path.join('/etc', 'inhibit-ebook-sleep')
if not os.path.exists(fname):
try:
bus = dbus.SystemBus()
proxy = bus.get_object(_HARDWARE_MANAGER_SERVICE,
_HARDWARE_MANAGER_OBJECT_PATH)
self._service = dbus.Interface(proxy,
_HARDWARE_MANAGER_INTERFACE)
self._scrolled.props.vadjustment.connect("value-changed",
self._user_action_cb)
self._scrolled.props.hadjustment.connect("value-changed",
self._user_action_cb)
self.connect("focus-in-event", self._focus_in_event_cb)
self.connect("focus-out-event", self._focus_out_event_cb)
self.connect("notify::active", self._now_active_cb)
_logger.debug('Suspend on idle enabled')
except dbus.DBusException:
_logger.info(
'Hardware manager service not found, no idle suspend.')
else:
_logger.debug('Suspend on idle disabled')
self.connect("shared", self._shared_cb)
h = hash(self._activity_id)
self.port = 1024 + (h % 64511)
self._progress_alert = None
if self._jobject.file_path is not None and \
self._jobject.file_path != '':
self.read_file(self._jobject.file_path)
elif handle.uri:
self._load_document(handle.uri)
# TODO: we need trasfer the metadata and uodate
# bookmarks and urls
elif self.shared_activity:
# We're joining
if self.get_shared():
# Already joined for some reason, just get the document
self._joined_cb(self)
else:
self._progress_alert = ProgressAlert()
self._progress_alert.props.title = _('Please wait')
self._progress_alert.props.msg = _('Starting connection...')
self.add_alert(self._progress_alert)
# Wait for a successful join before trying to get the document
self.connect("joined", self._joined_cb)
else:
# Not joining, not resuming or resuming session without file
emptypanel.show(self, 'activity-read',
_('No book'), _('Choose something to read'),
self._show_journal_object_picker_cb)
def _create_back_button(self):
back = ToolButton('go-previous-paired')
back.set_tooltip(_('Back'))
back.props.sensitive = False
palette = back.get_palette()
previous_page = MenuItem(text_label=_("Previous page"))
previous_page.show()
previous_bookmark = MenuItem(text_label=_("Previous bookmark"))
previous_bookmark.show()
palette.menu.append(previous_page)
palette.menu.append(previous_bookmark)
back.connect('clicked', self.__go_back_cb)
previous_page.connect('activate', self.__go_back_page_cb)
previous_bookmark.connect('activate', self.__prev_bookmark_activate_cb)
return back
def _create_forward_button(self):
forward = ToolButton('go-next-paired')
forward.set_tooltip(_('Forward'))
forward.props.sensitive = False
palette = forward.get_palette()
next_page = MenuItem(text_label=_("Next page"))
next_page.show()
next_bookmark = MenuItem(text_label=_("Next bookmark"))
next_bookmark.show()
palette.menu.append(next_page)
palette.menu.append(next_bookmark)
forward.connect('clicked', self.__go_forward_cb)
next_page.connect('activate', self.__go_forward_page_cb)
next_bookmark.connect('activate', self.__next_bookmark_activate_cb)
return forward
def _create_search(self):
num_page_entry = Gtk.Entry()
num_page_entry.set_text('0')
num_page_entry.set_alignment(1)
num_page_entry.connect('insert-text',
self.__num_page_entry_insert_text_cb)
num_page_entry.connect('activate',
self.__num_page_entry_activate_cb)
num_page_entry.set_width_chars(4)
return num_page_entry
def _set_total_page_label(self, value):
self._total_page_label.set_text(' / %s' % value)
def show_navigator_button(self):
self._view_toolbar.show_nav_button()
def _create_navigator(self):
def __cursor_changed_cb(treeview):
selection = treeview.get_selection()
store, index_iter = selection.get_selected()
if index_iter is None:
# Nothing selected. This happens at startup
return
if store.iter_has_child(index_iter):
path = store.get_path(index_iter)
if treeview.row_expanded(path):
treeview.collapse_row(path)
else:
treeview.expand_row(path, False)
self._toc_visible = False
self._update_toc_view = False
toc_navigator = Gtk.TreeView()
toc_navigator.set_enable_search(False)
toc_navigator.connect('cursor-changed', __cursor_changed_cb)
toc_selection = toc_navigator.get_selection()
toc_selection.set_mode(Gtk.SelectionMode.SINGLE)
cell = Gtk.CellRendererText()
self.treecol_toc = Gtk.TreeViewColumn(_('Index'), cell, text=0)
toc_navigator.append_column(self.treecol_toc)
self._toc_scroller = Gtk.ScrolledWindow(hadjustment=None,
vadjustment=None)
self._toc_scroller.set_policy(Gtk.PolicyType.AUTOMATIC,
Gtk.PolicyType.AUTOMATIC)
self._toc_scroller.add(toc_navigator)
self._hbox.pack_start(self._toc_scroller, expand=False, fill=False,
padding=0)
self._toc_separator = Gtk.VSeparator()
self._hbox.pack_start(self._toc_separator, expand=False, fill=False,
padding=1)
return toc_navigator
def set_navigator_model(self, model):
self._toc_model = model
self._navigator.set_model(model)
def __toggle_navigator_cb(self, button, visible):
scrollbar_pos = -1
if hasattr(self._view, 'get_vertical_pos'):
scrollbar_pos = self._view.get_vertical_pos()
if visible:
self._toc_visible = True
self._update_toc_view = True
self._toc_select_active_page()
self._toc_scroller.set_size_request(int(Gdk.Screen.width() / 4),
-1)
self._toc_scroller.show_all()
self._toc_separator.show()
else:
self._toc_visible = False
self._toc_scroller.hide()
self._toc_separator.hide()
if scrollbar_pos > -1:
self._view.set_vertical_pos(scrollbar_pos)
def __toggle_tray_cb(self, button, visible):
if visible:
logging.debug('Show tray')
self.tray.show()
else:
logging.debug('Hide tray')
self.tray.hide()
def __toggle_inverted_colors_cb(self, button, active):
if hasattr(self._view._model, 'set_inverted_colors'):
self._view._model.set_inverted_colors(active)
def __num_page_entry_insert_text_cb(self, entry, text, length, position):
if not re.match('[0-9]', text):
entry.emit_stop_by_name('insert-text')
return True
return False
def __num_page_entry_activate_cb(self, entry):
if entry.props.text:
page = int(entry.props.text) - 1
else:
page = 0
self._view.set_current_page(page)
entry.props.text = str(page + 1)
def __go_back_cb(self, button):
self._view.scroll(Gtk.ScrollType.PAGE_BACKWARD, False)
def __go_forward_cb(self, button):
self._view.scroll(Gtk.ScrollType.PAGE_FORWARD, False)
def __go_back_page_cb(self, button):
self._view.previous_page()
def __go_forward_page_cb(self, button):
self._view.next_page()
def __highlight_cb(self, button):
self._view.toggle_highlight(button.get_active())
def __prev_bookmark_activate_cb(self, menuitem):
page = self._view.get_current_page()
prev_bookmark = self._bookmarkmanager.get_prev_bookmark_for_page(page)
if prev_bookmark is not None:
self._view.set_current_page(prev_bookmark.page_no)
def __next_bookmark_activate_cb(self, menuitem):
page = self._view.get_current_page()
next_bookmark = self._bookmarkmanager.get_next_bookmark_for_page(page)
if next_bookmark is not None:
self._view.set_current_page(next_bookmark.page_no)
def __bookmarker_toggled_cb(self, button):
page = self._view.get_current_page()
if self._bookmarker.props.active:
self._bookmark_view.add_bookmark(page)
else:
alert = ConfirmationAlert()
alert.props.title = _('Delete bookmark')
alert.props.msg = _('All the information related '
'with this bookmark will be lost')
self.add_alert(alert)
alert.connect('response', self.__alert_response_cb, page)
alert.show()
def __alert_response_cb(self, alert, response_id, page):
self.remove_alert(alert)
if response_id is Gtk.ResponseType.OK:
self._bookmark_view.del_bookmark(page)
elif response_id is Gtk.ResponseType.CANCEL:
self._bookmarker.handler_block(self._bookmarker_toggle_handler_id)
self._bookmarker.props.active = True
self._bookmarker.handler_unblock(
self._bookmarker_toggle_handler_id)
def __page_changed_cb(self, model, page_from, page_to):
self._update_nav_buttons(page_to)
if self._toc_model is not None:
self._toc_select_active_page()
self._bookmark_view.update_for_page(page_to)
self.update_bookmark_button()
if self._view.can_highlight():
self._view.show_highlights(page_to)
def _update_bookmark_cb(self, sidebar):
self.update_bookmark_button()
def update_bookmark_button(self):
self._bookmarker.handler_block(self._bookmarker_toggle_handler_id)
self._bookmarker.props.active = \
self._bookmark_view.is_showing_local_bookmark()
self._bookmarker.handler_unblock(self._bookmarker_toggle_handler_id)
def _update_nav_buttons(self, current_page):
self._back_button.props.sensitive = current_page > 0
self._forward_button.props.sensitive = \
current_page < self._view.get_pagecount() - 1
self._num_page_entry.handler_block_by_func(
self.__num_page_entry_insert_text_cb)
self._num_page_entry.props.text = str(current_page + 1)
self._num_page_entry.handler_unblock_by_func(
self.__num_page_entry_insert_text_cb)
self._set_total_page_label(self._view.get_pagecount())
def _update_toc(self):
if self._view.update_toc(self):
self._navigator_changed_handler_id = \
self._navigator.connect('cursor-changed',
self.__navigator_cursor_changed_cb)
def __navigator_cursor_changed_cb(self, toc_treeview):
if toc_treeview.get_selection() is None:
return
treestore, toc_selected = toc_treeview.get_selection().get_selected()
if toc_selected is not None:
link = self._toc_model.get(toc_selected, 1)[0]
logging.debug('View handle link %s', link)
self._update_toc_view = False
self._view.handle_link(link)
self._update_toc_view = True
def _toc_select_active_page(self):
if not self._toc_visible or not self._update_toc_view:
return
_store, toc_selected = self._navigator.get_selection().get_selected()
if toc_selected is not None:
selected_link = self._toc_model.get(toc_selected, 1)[0]
else:
selected_link = ""
current_link = self._view.get_current_link()
if current_link == selected_link:
return
link_iter = self._view.get_link_iter(current_link)
if link_iter is not None:
self._navigator.handler_block(self._navigator_changed_handler_id)
toc_selection = self._navigator.get_selection()
toc_selection.select_iter(link_iter)
self._navigator.handler_unblock(self._navigator_changed_handler_id)
else:
logging.debug('link "%s" not found in the toc model', current_link)
def _show_journal_object_picker_cb(self, button):
"""Show the journal object picker to load a document.
This is for if Read is launched without a document.
"""
if not self._want_document:
return
chooser = ObjectChooser(parent=self,
what_filter=self.get_bundle_id(),
filter_type=FILTER_TYPE_MIME_BY_ACTIVITY)
try:
result = chooser.run()
if result == Gtk.ResponseType.ACCEPT:
logging.debug('ObjectChooser: %r' %
chooser.get_selected_object())
jobject = chooser.get_selected_object()
if jobject and jobject.file_path:
for key in list(jobject.metadata.keys()):
self.metadata[key] = jobject.metadata[key]
self.read_file(jobject.file_path)
jobject.object_id = self._object_id
finally:
chooser.destroy()
del chooser
def _now_active_cb(self, widget, pspec):
if self.props.active:
# Now active, start initial suspend timeout
if self._idle_timer > 0:
GLib.source_remove(self._idle_timer)
self._idle_timer = GLib.timeout_add_seconds(15,
self._suspend_cb)
self._sleep_inhibit = False
else:
# Now inactive
self._sleep_inhibit = True
def _focus_in_event_cb(self, widget, event):
"""Enable ebook mode idle sleep since Read has focus."""
self._sleep_inhibit = False
self._user_action_cb(self)
def _focus_out_event_cb(self, widget, event):
"""Disable ebook mode idle sleep since Read lost focus."""
self._sleep_inhibit = True
def _user_action_cb(self, widget):
"""Set a timer for going back to ebook mode idle sleep."""
if self._idle_timer > 0:
GLib.source_remove(self._idle_timer)
self._idle_timer = GLib.timeout_add_seconds(5, self._suspend_cb)
def _suspend_cb(self):
"""Go into ebook mode idle sleep."""
# If the machine has been idle for 5 seconds, suspend
self._idle_timer = 0
if not self._sleep_inhibit and not self.get_shared():
self._service.set_kernel_suspend()
return False
def read_file(self, file_path):
"""Load a file from the datastore on activity start."""
_logger.debug('ReadActivity.read_file: %s', file_path)
# enable collaboration
self.activity_button.page.share.props.sensitive = True
# we need copy the file to a new place, the file_path disappear
extension = os.path.splitext(file_path)[1]
tempfile = os.path.join(self.get_activity_root(), 'instance',
'tmp%i%s' % (time.time(), extension))
os.link(file_path, tempfile)
self._load_document('file://' + tempfile)
# FIXME: This should obviously be fixed properly
GLib.timeout_add_seconds(
1, self.__view_toolbar_needs_update_size_cb, None)
def write_file(self, file_path):
"""Write into datastore for Keep.
The document is saved by hardlinking from the temporary file we
keep around instead of "saving".
The metadata is updated, including current page, view settings,
search text.
"""
if self._tempfile is None:
# Workaround for closing Read with no document loaded
raise NotImplementedError
try:
self.metadata['Read_current_page'] = \
str(self._view.get_current_page())
self._view.update_metadata(self)
self.metadata['Read_search'] = \
self._edit_toolbar._search_entry.props.text
except Exception as e:
_logger.error('write_file(): %s', e)
self.metadata['Read_search'] = \
self._edit_toolbar._search_entry.props.text
self.metadata['activity'] = self.get_bundle_id()
# the file is only saved if modified
saved = False
if hasattr(self._view, 'save'):
saved = self._view.save(file_path)
if saved:
self.filehash = get_md5(file_path)
self.metadata['filehash'] = self.filehash
else:
os.link(self._tempfile, file_path)
if self.filehash is None:
self.filehash = get_md5(file_path)
self.metadata['filehash'] = self.filehash
self._save_bookmarks_in_metadata()
if self._close_requested:
_logger.debug("Removing temp file %s because we will close",
self._tempfile)
os.unlink(self._tempfile)
# remove file used to transmit the metadata if exits
try:
metadata_file_path = self._tempfile + '.json'
os.unlink(metadata_file_path)
except OSError:
pass
self._tempfile = None
def _save_bookmarks_in_metadata(self):
# save bookmarks in the metadata
bookmarks = []
for bookmark in self._bookmarkmanager.get_bookmarks():
bookmarks.append(bookmark.get_as_dict())
self.metadata['bookmarks'] = json.dumps(bookmarks)
self.metadata['highlights'] = json.dumps(
self._bookmarkmanager.get_all_highlights())
def can_close(self):
"""Prepare to cleanup on closing.
Called from self.close()
"""
self._close_requested = True
return True
def _download_result_cb(self, getter, tempfile, suggested_name, tube_id,
tube_ip, tube_port):
if self._download_content_type == 'text/html':
# got an error page instead
self._download_error_cb(getter, 'HTTP Error', tube_id)
return
del self.unused_download_tubes
# Use the suggested file, the mime is not recognized if the extension
# is wrong in some cases (epub)
temp_dir = os.path.dirname(tempfile)
new_name = os.path.join(temp_dir, suggested_name)
os.rename(tempfile, new_name)
tempfile = new_name
_logger.debug("Saving file %s to datastore...", tempfile)
mimetype = Gio.content_type_guess(tempfile, None)[0]
self._jobject.metadata['mime_type'] = mimetype
self._jobject.file_path = tempfile
datastore.write(self._jobject)
_logger.debug("Got document %s (%s) from tube %u",
tempfile, suggested_name, tube_id)
if self._progress_alert is not None:
self.remove_alert(self._progress_alert)
self._progress_alert = None
# download the metadata
GLib.idle_add(self._download_metadata, tube_id, tube_ip, tube_port)
def _download_metadata_result_cb(self, getter, tempfile, suggested_name,
tube_id):
# load the shared metadata
with open(tempfile) as json_file:
shared_metadata = json.load(json_file)
os.remove(tempfile)
# load the object from the datastore to update the file path
GLib.idle_add(self._open_downloaded_file, shared_metadata)
def _open_downloaded_file(self, shared_metadata):
self._jobject = datastore.get(self._jobject.object_id)
for key in list(shared_metadata.keys()):
self.metadata[key] = shared_metadata[key]
self.read_file(self._jobject.file_path)
def _download_progress_cb(self, getter, bytes_downloaded, tube_id):
# FIXME: Draw a progress bar
if self._download_content_length > 0:
_logger.debug("Downloaded %u of %u bytes from tube %u...",
bytes_downloaded, self._download_content_length,
tube_id)
fraction = float(bytes_downloaded) / \
float(self._download_content_length)
self._progress_alert.set_fraction(fraction)
else:
_logger.debug("Downloaded %u bytes from tube %u...",
bytes_downloaded, tube_id)
def _download_error_cb(self, getter, err, tube_id):
_logger.debug("Error getting document from tube %u: %s",
tube_id, err)
self._want_document = True
self._download_content_length = 0
self._download_content_type = None
if self._progress_alert is not None:
self.remove_alert(self._progress_alert)
self._progress_alert = None
GLib.idle_add(self._get_document)
def _get_connection_params(self, tube_id):
# return ip and port to download a file
chan = self.shared_activity.telepathy_tubes_chan
iface = chan[TelepathyGLib.IFACE_CHANNEL_TYPE_TUBES]
addr = iface.AcceptStreamTube(
tube_id,
TelepathyGLib.SocketAddressType.IPV4,
TelepathyGLib.SocketAccessControl.LOCALHOST, 0,
utf8_strings=True)
_logger.debug('Accepted stream tube: listening address is %r', addr)
# SOCKET_ADDRESS_TYPE_IPV4 is defined to have addresses of type '(sq)'
assert isinstance(addr, dbus.Struct)
assert len(addr) == 2
assert isinstance(addr[0], str)
assert isinstance(addr[1], int)
assert addr[1] > 0 and addr[1] < 65536
ip = addr[0]
port = int(addr[1])
return ip, port
def _download_document(self, tube_id):
ip, port = self._get_connection_params(tube_id)
getter = ReadURLDownloader("http://%s:%d/document" % (ip, port))
getter.connect("finished", self._download_result_cb, tube_id, ip, port)
getter.connect("progress", self._download_progress_cb, tube_id)
getter.connect("error", self._download_error_cb, tube_id)
getter.start()
self._download_content_length = getter.get_content_length()
self._download_content_type = getter.get_content_type()
return False
def _download_metadata(self, tube_id, ip, port):
getter = ReadURLDownloader("http://%s:%d/metadata" % (ip, port))
getter.connect("finished", self._download_metadata_result_cb, tube_id)
# getter.connect("progress", self._download_progress_cb, tube_id)
getter.connect("error", self._download_error_cb, tube_id)
getter.start()
self._download_content_length = getter.get_content_length()
self._download_content_type = getter.get_content_type()
return False
def _get_document(self):
if not self._want_document:
return False
# Pick an arbitrary tube we can try to download the document from
try:
tube_id = self.unused_download_tubes.pop()
except (ValueError, KeyError) as e:
_logger.debug('No tubes to get the document from right now: %s',
e)
return False
# Avoid trying to download the document multiple times at once
self._want_document = False
GLib.idle_add(self._download_document, tube_id)
return False
def _joined_cb(self, also_self):
"""Callback for when a shared activity is joined.
Get the shared document from another participant.
"""
self.watch_for_tubes()
if self._progress_alert is not None:
self._progress_alert.props.msg = _('Receiving book...')
GLib.idle_add(self._get_document)
def _load_document(self, filepath):
"""Load the specified document and set up the UI.
filepath -- string starting with file://
"""
if self._tempfile is not None:
# prevent reopen
return
self.set_canvas(self._vbox)
filename = filepath.replace('file://', '')
self._tempfile = filename
if not os.path.exists(filename) or os.path.getsize(filename) == 0:
return
if 'mime_type' not in self.metadata or not self.metadata['mime_type']:
mimetype = Gio.content_type_guess(filepath, None)[0]
self.metadata['mime_type'] = mimetype
else:
mimetype = self.metadata['mime_type']
if mimetype == 'application/epub+zip':
import epubadapter
self._view = epubadapter.EpubViewer()
elif mimetype == 'text/plain' or mimetype == 'application/zip':
import textadapter
self._view = textadapter.TextViewer()
elif mimetype == 'application/x-cbz':
import comicadapter
self._view = comicadapter.ComicViewer()
else:
import evinceadapter
self._view = evinceadapter.EvinceViewer()
self._view_toolbar.show_inverted_colors_button()
self._view.setup(self)