1
# Copyright (C) 2006 Szilveszter Farkas <szilveszter.farkas@gmail.com>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
import bzrlib.progress
25
from bzrlib.symbol_versioning import (deprecated_method,
27
from bzrlib.ui import UIFactory
30
class PromptDialog(gtk.Dialog):
31
""" Prompt the user for a yes/no answer. """
32
def __init__(self, prompt):
33
gtk.Dialog.__init__(self)
35
label = gtk.Label(prompt)
36
self.vbox.pack_start(label, padding=10)
40
self.add_buttons(gtk.STOCK_YES, gtk.RESPONSE_YES, gtk.STOCK_NO, gtk.RESPONSE_NO)
43
class PasswordDialog(gtk.Dialog):
44
""" Prompt the user for a password. """
45
def __init__(self, prompt):
46
gtk.Dialog.__init__(self)
48
label = gtk.Label(prompt)
49
self.vbox.pack_start(label, padding=10)
51
self.entry = gtk.Entry()
52
self.entry.set_visibility(False)
53
self.vbox.pack_end(self.entry, padding=10)
57
self.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
59
def _get_passwd(self):
60
return self.entry.get_text()
62
passwd = property(_get_passwd)
65
class GtkUIFactory(UIFactory):
66
"""A UI factory for GTK user interfaces."""
72
"""Create a GtkUIFactory.
74
:param bar_type: The type of progress bar to create. It defaults to
75
letting the bzrlib.progress.ProgressBar factory auto
78
super(GtkUIFactory, self).__init__()
79
self._bar_type = bar_type
81
self.stdout = sys.stdout
85
self.stderr = sys.stderr
89
def get_boolean(self, prompt):
90
"""GtkDialog with yes/no answers"""
91
dialog = PromptDialog(prompt)
92
response = dialog.run()
94
return (response == gtk.RESPONSE_YES)
96
@deprecated_method(zero_eight)
97
def progress_bar(self):
98
"""See UIFactory.nested_progress_bar()."""
99
# this in turn is abstract, and creates either a tty or dots
100
# bar depending on what we think of the terminal
101
return bzrlib.progress.ProgressBar()
103
def get_password(self, prompt='', **kwargs):
104
"""Prompt the user for a password.
106
:param prompt: The prompt to present the user
107
:param kwargs: Arguments which will be expanded into the prompt.
108
This lets front ends display different things if
110
:return: The password string, return None if the user
111
canceled the request.
113
dialog = PasswordDialog(prompt % kwargs)
114
response = dialog.run()
115
passwd = dialog.passwd
117
if response == gtk.RESPONSE_OK:
122
def nested_progress_bar(self):
123
"""Return a nested progress bar.
125
The actual bar type returned depends on the progress module which
126
may return a tty or dots bar depending on the terminal.
128
FIXME: It should return a GtkProgressBar actually.
130
if self._progress_bar_stack is None:
131
self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
132
klass=self._bar_type)
133
return self._progress_bar_stack.get_nested()
135
def clear_term(self):
136
"""Prepare the terminal for output.
138
It has no sense when talking about GTK."""