Debian Codegraph @

GTG/gtk/backends/configurepanel.py

raw symbols SCIP
1 # -----------------------------------------------------------------------------
2 # Getting Things GNOME! - a personal organizer for the GNOME desktop
3 # Copyright (c) 2008-2013 - Lionel Dricot & Bertrand Rousseau
4 #
5 # This program is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # this program. If not, see <http://www.gnu.org/licenses/>.
17 # -----------------------------------------------------------------------------
18
19 from gi.repository import Gtk
20
21 from gettext import gettext as _
22 from GTG.gtk.backends.parameters_ui import ParametersUI
23 from GTG.backends.backend_signals import BackendSignals
24
25
26 class ConfigurePanel(Gtk.Box):
27 """
28 A vertical Box that lets you configure a backend
29 """
30
31 def __init__(self, backends):
32 """
33 Constructor, creating all the gtk widgets
34
35 @param backends: a reference to the dialog in which this is
36 loaded
37 """
38 super().__init__(orientation=Gtk.Orientation.VERTICAL)
39 self.dialog = backends
40 self.should_spinner_be_shown = False
41 self.task_deleted_handle = None
42 self.task_added_handle = None
43 self.req = backends.get_requester()
44 self._create_widgets()
45 self._connect_signals()
46
47 def _connect_signals(self):
48 """ Connects the backends generated signals """
49 _signals = BackendSignals()
50 _signals.connect(_signals.BACKEND_RENAMED, self.refresh_title)
51 _signals.connect(_signals.BACKEND_STATE_TOGGLED, self.refresh_sync_status)
52 _signals.connect(_signals.BACKEND_SYNC_STARTED, self.on_sync_started)
53 _signals.connect(_signals.BACKEND_SYNC_ENDED, self.on_sync_ended)
54
55 def _create_widgets(self):
56 """
57 This function fills this box with widgets
58 """
59 # Division of the available space in three segments:
60 # top, middle and bottom (parameters_ui)
61 top = Gtk.Box()
62 top.set_spacing(6)
63 middle = Gtk.Box()
64 middle.set_spacing(6)
65 self._fill_top_box(top)
66 self._fill_middle_box(middle)
67 self.pack_start(top, False, True, 0)
68 self.pack_start(middle, False, True, 0)
69 align = Gtk.Alignment.new(0, 0, 1, 0)
70 align.set_padding(10, 0, 0, 0)
71 self.parameters_ui = ParametersUI(self.req)
72 align.add(self.parameters_ui)
73 self.pack_start(align, False, True, 0)
74
75 def _fill_top_box(self, box):
76 """ Fill header with service's icon, name, and a spinner
77 for inidcation of work.
78 """
79 box.set_spacing(10)
80 self.image_icon = Gtk.Image()
81 self.image_icon.set_size_request(48, 48)
82
83 self.human_name_label = Gtk.Label()
84 self.human_name_label.set_alignment(xalign=0, yalign=0.5)
85
86 # FIXME in the newer versions of GTK3 there always be Spinner!
87 try:
88 self.spinner = Gtk.Spinner()
89 except AttributeError:
90 # worarkound for archlinux: bug #624204
91 self.spinner = Gtk.Box()
92 self.spinner.connect("show", self.on_spinner_show)
93 self.spinner.set_size_request(32, 32)
94 align_spin = Gtk.Alignment.new(1, 0, 0, 0)
95 align_spin.add(self.spinner)
96
97 box.pack_start(self.image_icon, False, True, 0)
98 box.pack_start(self.human_name_label, True, True, 0)
99 box.pack_start(align_spin, False, True, 0)
100
101 def _fill_middle_box(self, box):
102 """
103 Helper function to fill an box with a label and a button
104
105 @param box: the Gtk.Box to fill
106 """
107 self.sync_status_label = Gtk.Label()
108 self.sync_status_label.set_alignment(xalign=0.8, yalign=0.5)
109 self.sync_button = Gtk.Button()
110 self.sync_button.connect("clicked", self.on_sync_button_clicked)
111 box.pack_start(self.sync_status_label, True, True, 0)
112 box.pack_start(self.sync_button, True, True, 0)
113
114 def set_backend(self, backend_id):
115 """Changes the backend to configure, refreshing this view.
116
117 @param backend_id: the id of the backend to configure
118 """
119 self.backend = self.dialog.get_requester().get_backend(backend_id)
120 self.refresh_title()
121 self.refresh_sync_status()
122 self.parameters_ui.refresh(self.backend)
123 self.image_icon.set_from_pixbuf(self.dialog.get_pixbuf_from_icon_name(
124 self.backend.get_icon(), 48))
125
126 def refresh_title(self, sender=None, data=None):
127 """
128 Callback for the signal that notifies backends name changes. It changes
129 the title of this view
130
131 @param sender: not used, here only for signal callback compatibility
132 @param data: not used, here only for signal callback compatibility
133 """
134 markup = "<big><big><big><b>%s</b></big></big></big>" % \
135 self.backend.get_human_name()
136 self.human_name_label.set_markup(markup)
137
138 def refresh_sync_button(self):
139 """
140 Refreshes the state of the button that enables the backend
141 """
142 self.sync_button.set_sensitive(not self.backend.is_default())
143 if self.backend.is_enabled():
144 label = _("Disable syncing")
145 else:
146 label = _("Enable syncing")
147 self.sync_button.set_label(label)
148
149 def refresh_sync_status_label(self):
150 """
151 Refreshes the Gtk.Label that shows the current state of this backend
152 """
153 if self.backend.is_default():
154 label = _("This is the default synchronization service")
155 else:
156 if self.backend.is_enabled():
157 label = _("Syncing is enabled.")
158 else:
159 label = _('Syncing is <span color="red">disabled</span>.')
160 self.sync_status_label.set_markup(label)
161
162 def refresh_sync_status(self, sender=False, data=False):
163 """Signal callback function, called when a backend state
164 (enabled/disabled) changes. Refreshes this view.
165
166 @param sender: not used, here only for signal callback compatibility
167 @param data: not used, here only for signal callback compatibility
168 """
169 self.refresh_sync_button()
170 self.refresh_sync_status_label()
171
172 def on_sync_button_clicked(self, sender):
173 """
174 Signal callback when a backend is enabled/disabled via the UI button
175
176 @param sender: not used, here only for signal callback compatibility
177 """
178 self.parameters_ui.commit_changes()
179 self.req.set_backend_enabled(self.backend.get_id(),
180 not self.backend.is_enabled())
181
182 def on_sync_started(self, sender, backend_id):
183 """
184 If the backend has started syncing tasks, update the state of the
185 Gtk.Spinner
186
187 @param sender: not used, here only for signal callback compatibility
188 @param backend_id: the id of the backend that emitted this signal
189 """
190 if backend_id == self.backend.get_id():
191 self.spinner_set_active(True)
192
193 def on_sync_ended(self, sender, backend_id):
194 """
195 If the backend has stopped syncing tasks, update the state of the
196 Gtk.Spinner
197
198 @param sender: not used, here only for signal callback compatibility
199 @param backend_id: the id of the backend that emitted this signal
200 """
201
202 if backend_id == self.backend.get_id():
203 self.spinner_set_active(False)
204
205 def on_spinner_show(self, sender):
206 """This signal callback hides the spinner if it's not supposed to be
207 seen. It's a workaround to let us call show_all on the whole window
208 while keeping this hidden (it's the only widget that requires special
209 attention)
210
211 @param sender: not used, here only for signal callback compatibility
212 """
213 if not self.should_spinner_be_shown:
214 self.spinner.hide()
215
216 def spinner_set_active(self, active):
217 """
218 Enables/disables the Gtk.Spinner, while showing/hiding it at the same
219 time
220
221 @param active: True if the spinner should spin
222 """
223 self.should_spinner_be_shown = active
224 if active:
225 if isinstance(self.spinner, Gtk.Spinner):
226 self.spinner.start()
227 self.spinner.show()
228 else:
229 self.spinner.hide()
230 if isinstance(self.spinner, Gtk.Spinner):
231 self.spinner.stop()