Add progress indicator for deleting items (bug 268)
[gpodder.git] / src / gpodder / gtkui / interface / progress.py
blobcc10cf35d89e7544475eda36c06449856dc4487a
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2009 Thomas Perl and the gPodder Team
6 # gPodder is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # gPodder is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 import gtk
21 import gobject
22 import pango
24 import gpodder
26 _ = gpodder.gettext
28 from gpodder.gtkui.widgets import SpinningProgressIndicator
30 class ProgressIndicator(object):
31 # Delayed time until window is shown (for short operations)
32 DELAY = 500
34 # Time between GUI updates after window creation
35 INTERVAL = 100
37 def __init__(self, title, subtitle=None, cancellable=False, parent=None):
38 self.title = title
39 self.subtitle = subtitle
40 self.cancellable = cancellable
41 self.parent = parent
42 self.dialog = None
43 self.progressbar = None
44 self.indicator = None
45 self._initial_message = None
46 self._initial_progress = None
47 self.source_id = gobject.timeout_add(self.DELAY, self._create_progress)
49 def _on_delete_event(self, window, event):
50 if self.cancellable:
51 self.dialog.response(gtk.RESPONSE_CANCEL)
52 return True
54 def _create_progress(self):
55 self.dialog = gtk.MessageDialog(self.parent, \
56 0, 0, gtk.BUTTONS_CANCEL, self.subtitle or self.title)
57 self.dialog.connect('delete-event', self._on_delete_event)
58 if gpodder.ui.fremantle:
59 import hildon
60 hildon.hildon_gtk_window_set_progress_indicator(self.dialog, True)
61 self.dialog.set_title(self.title)
62 self.dialog.set_deletable(self.cancellable)
63 self.dialog.label.set_selectable(False)
65 self.dialog.set_response_sensitive(gtk.RESPONSE_CANCEL, \
66 self.cancellable)
68 self.progressbar = gtk.ProgressBar()
69 self.progressbar.set_ellipsize(pango.ELLIPSIZE_END)
71 # If the window is shown after the first update, set the progress
72 # info so that when the window appears, data is there already
73 if self._initial_progress is not None:
74 self.progressbar.set_fraction(self._initial_progress)
75 if self._initial_message is not None:
76 self.progressbar.set_text(self._initial_message)
78 self.dialog.vbox.add(self.progressbar)
79 if gpodder.ui.fremantle:
80 self.dialog.set_image(gtk.Image())
81 else:
82 self.indicator = SpinningProgressIndicator()
83 self.dialog.set_image(self.indicator)
84 self.dialog.show_all()
86 gobject.source_remove(self.source_id)
87 self.source_id = gobject.timeout_add(self.INTERVAL, self._update_gui)
88 return False
90 def _update_gui(self):
91 if self.indicator:
92 self.indicator.step_animation()
93 return True
95 def on_message(self, message):
96 if self.progressbar:
97 self.progressbar.set_text(message)
98 else:
99 self._initial_message = message
101 def on_progress(self, progress):
102 if self.progressbar:
103 self.progressbar.set_fraction(progress)
104 else:
105 self._initial_progress = progress
107 def on_finished(self):
108 if self.dialog is not None:
109 self.dialog.destroy()
110 gobject.source_remove(self.source_id)