1
 
# Copyright (C) 2006 Szilveszter Farkas <szilveszter.farkas@gmail.com>
 
2
 
# Copyright (C) 2007 Jelmer Vernooij <jelmer@samba.org>
 
4
 
# This program is free software; you can redistribute it and/or modify
 
5
 
# it under the terms of the GNU General Public License as published by
 
6
 
# the Free Software Foundation; either version 2 of the License, or
 
7
 
# (at your option) any later version.
 
9
 
# This program is distributed in the hope that it will be useful,
 
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 
# GNU General Public License for more details.
 
14
 
# You should have received a copy of the GNU General Public License
 
15
 
# along with this program; if not, write to the Free Software
 
16
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
25
 
import bzrlib.progress
 
26
 
from bzrlib.ui import UIFactory
 
29
 
class PromptDialog(gtk.Dialog):
 
30
 
    """ Prompt the user for a yes/no answer. """
 
31
 
    def __init__(self, prompt):
 
32
 
        gtk.Dialog.__init__(self)
 
34
 
        label = gtk.Label(prompt)
 
35
 
        self.vbox.pack_start(label, padding=10)
 
39
 
        self.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES, gtk.STOCK_NO, 
 
43
 
class GtkProgressBar(gtk.ProgressBar):
 
44
 
    def __init__(self, stack):
 
45
 
        super(GtkProgressBar, self).__init__()
 
46
 
        self.set_fraction(0.0)
 
50
 
        self._stack.remove(self)
 
58
 
    def update(self, msg=None, current=None, total=None):
 
61
 
        if None not in (current, total) and total > 0:
 
62
 
            self.set_fraction(1.0 * current / total)
 
63
 
        while gtk.events_pending():
 
67
 
class GtkProgressBarStack(gtk.Window):
 
69
 
        super(GtkProgressBarStack, self).__init__(type=gtk.WINDOW_TOPLEVEL)
 
70
 
        self.set_border_width(0)
 
71
 
        self.set_title("Progress")
 
72
 
        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
 
73
 
        self.vbox = gtk.VBox()
 
75
 
        self.set_resizable(False)
 
77
 
    def _adapt_size(self):
 
78
 
        self.resize(250, 15 * len(self.vbox.get_children()))
 
81
 
        nested = GtkProgressBar(self)
 
82
 
        self.vbox.pack_start(nested)
 
89
 
        if len(self.vbox.get_children()) == 0: # If there is nothing to show, don't leave a ghost window here
 
93
 
class PasswordDialog(gtk.Dialog):
 
94
 
    """ Prompt the user for a password. """
 
95
 
    def __init__(self, prompt):
 
96
 
        gtk.Dialog.__init__(self)
 
98
 
        label = gtk.Label(prompt)
 
99
 
        self.vbox.pack_start(label, padding=10)
 
101
 
        self.entry = gtk.Entry()
 
102
 
        self.entry.set_visibility(False)
 
103
 
        self.vbox.pack_end(self.entry, padding=10)
 
107
 
        self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
 
109
 
    def _get_passwd(self):
 
110
 
        return self.entry.get_text()
 
112
 
    passwd = property(_get_passwd)
 
115
 
class GtkUIFactory(UIFactory):
 
116
 
    """A UI factory for GTK user interfaces."""
 
121
 
        """Create a GtkUIFactory.
 
124
 
        super(GtkUIFactory, self).__init__()
 
125
 
        self._progress_bar_stack = None
 
127
 
    def get_boolean(self, prompt):
 
128
 
        """GtkDialog with yes/no answers"""
 
129
 
        dialog = PromptDialog(prompt)
 
130
 
        response = dialog.run()
 
132
 
        return (response == gtk.RESPONSE_YES)
 
134
 
    def get_password(self, prompt='', **kwargs):
 
135
 
        """Prompt the user for a password.
 
137
 
        :param prompt: The prompt to present the user
 
138
 
        :param kwargs: Arguments which will be expanded into the prompt.
 
139
 
                       This lets front ends display different things if
 
141
 
        :return: The password string, return None if the user 
 
142
 
                 canceled the request.
 
144
 
        dialog = PasswordDialog(prompt % kwargs)
 
145
 
        response = dialog.run()
 
146
 
        passwd = dialog.passwd
 
148
 
        if response == gtk.RESPONSE_OK:
 
153
 
    def nested_progress_bar(self):
 
154
 
        """Return a nested progress bar.
 
156
 
        if self._progress_bar_stack is None:
 
157
 
            self._progress_bar_stack = GtkProgressBarStack()
 
158
 
        return self._progress_bar_stack.get_nested()
 
160
 
    def set_progress_bar_vbox(self, vbox):
 
161
 
        """Change the vbox to put progress bars in.
 
163
 
        self._progress_bar_stack = vbox
 
165
 
    def clear_term(self):
 
166
 
        """Prepare the terminal for output.
 
168
 
        It has no sense when talking about GTK."""