Improve incomplete file resuming UI
[gpodder.git] / src / gpodder / gtkui / interface / progress.py
blob2a7e6bc917a5e5370d0d3f96aa0f5951d7596997
1 # -*- coding: utf-8 -*-
3 # gPodder - A media aggregator and podcast client
4 # Copyright (c) 2005-2010 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._progress_set = False
48 self.source_id = gobject.timeout_add(self.DELAY, self._create_progress)
50 def _on_delete_event(self, window, event):
51 if self.cancellable:
52 self.dialog.response(gtk.RESPONSE_CANCEL)
53 return True
55 def _create_progress(self):
56 if gpodder.ui.fremantle:
57 self.dialog = gtk.Dialog(self.title, self.parent, 0, \
58 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
59 import hildon
60 hildon.hildon_gtk_window_set_progress_indicator(self.dialog, True)
61 else:
62 self.dialog = gtk.MessageDialog(self.parent, \
63 0, 0, gtk.BUTTONS_CANCEL, self.subtitle or self.title)
64 self.dialog.label.set_selectable(False)
65 self.dialog.connect('delete-event', self._on_delete_event)
66 self.dialog.set_title(self.title)
67 self.dialog.set_deletable(self.cancellable)
69 self.dialog.set_response_sensitive(gtk.RESPONSE_CANCEL, \
70 self.cancellable)
72 self.progressbar = gtk.ProgressBar()
73 self.progressbar.set_ellipsize(pango.ELLIPSIZE_END)
75 # If the window is shown after the first update, set the progress
76 # info so that when the window appears, data is there already
77 if self._initial_progress is not None:
78 self.progressbar.set_fraction(self._initial_progress)
79 if self._initial_message is not None:
80 self.progressbar.set_text(self._initial_message)
82 self.dialog.vbox.add(self.progressbar)
83 if not gpodder.ui.fremantle:
84 self.indicator = SpinningProgressIndicator()
85 self.dialog.set_image(self.indicator)
86 self.dialog.show_all()
88 gobject.source_remove(self.source_id)
89 self.source_id = gobject.timeout_add(self.INTERVAL, self._update_gui)
90 return False
92 def _update_gui(self):
93 if self.indicator:
94 self.indicator.step_animation()
95 if not self._progress_set and self.progressbar:
96 self.progressbar.pulse()
97 return True
99 def on_message(self, message):
100 if self.progressbar:
101 self.progressbar.set_text(message)
102 else:
103 self._initial_message = message
105 def on_progress(self, progress):
106 self._progress_set = True
107 if self.progressbar:
108 self.progressbar.set_fraction(progress)
109 else:
110 self._initial_progress = progress
112 def on_finished(self):
113 if self.dialog is not None:
114 self.dialog.destroy()
115 gobject.source_remove(self.source_id)