1'''library_add_progress_dialog.py - Progress bar for the library.'''
2
3from gi.repository import Gtk
4from gi.repository import Pango
5
6from mcomix import labels
7
8_dialog = None
9# The "All books" collection is not a real collection stored in the library,
10# but is represented by this ID in the library's TreeModels.
11_COLLECTION_ALL = -1
12
13class _AddLibraryProgressDialog(Gtk.Dialog):
14
15    '''Dialog with a ProgressBar that adds books to the library.'''
16
17    def __init__(self, library, window, paths, collection):
18        '''Adds the books at <paths> to the library, and also to the
19        <collection>, unless it is None.
20        '''
21        super(_AddLibraryProgressDialog, self).__init__(title=_('Adding books'),
22                                                        modal=True)
23        self.set_transient_for(library)
24        self.add_buttons(Gtk.STOCK_STOP, Gtk.ResponseType.CLOSE)
25
26        self._window = window
27        self._destroy = False
28        self.set_size_request(400, -1)
29        self.set_resizable(False)
30        self.set_border_width(4)
31        self.connect('response', self._response)
32        self.set_default_response(Gtk.ResponseType.CLOSE)
33
34        main_box = Gtk.VBox(homogeneous=False, spacing=5)
35        main_box.set_border_width(6)
36        self.vbox.pack_start(main_box, False, False, 0)
37        hbox = Gtk.HBox(homogeneous=False, spacing=10)
38        main_box.pack_start(hbox, False, False, 5)
39        left_box = Gtk.VBox(homogeneous=True, spacing=5)
40        right_box = Gtk.VBox(homogeneous=True, spacing=5)
41        hbox.pack_start(left_box, False, False, 0)
42        hbox.pack_start(right_box, False, False, 0)
43
44        label = labels.BoldLabel(_('Added books:'))
45        label.set_alignment(1.0, 1.0)
46        left_box.pack_start(label, True, True, 0)
47        number_label = Gtk.Label(label='0')
48        number_label.set_alignment(0, 1.0)
49        right_box.pack_start(number_label, True, True, 0)
50
51        bar = Gtk.ProgressBar()
52        main_box.pack_start(bar, False, False, 0)
53
54        added_label = labels.ItalicLabel()
55        added_label.set_alignment(0, 0.5)
56        added_label.set_width_chars(64)
57        added_label.set_max_width_chars(64)
58        added_label.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
59        main_box.pack_start(added_label, False, False, 0)
60        self.show_all()
61
62        total_paths_int = len(paths)
63        total_paths_float = float(len(paths))
64        total_added = 0
65
66        for path in paths:
67
68            if library.backend.add_book(path, collection):
69                total_added += 1
70
71                number_label.set_text('%d / %d' % (total_added, total_paths_int))
72
73            added_label.set_text(_('Adding "%s"...') % path)
74            bar.set_fraction(total_added / total_paths_float)
75
76            while Gtk.events_pending():
77                Gtk.main_iteration_do(False)
78
79            if self._destroy:
80                return
81
82        self._response()
83
84    def _response(self, *args):
85        self._destroy = True
86        self.destroy()
87
88# vim: expandtab:sw=4:ts=4
89