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 interefaces."""
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
if response == gtk.RESPONSE_YES:
99
@deprecated_method(zero_eight)
100
def progress_bar(self):
101
"""See UIFactory.nested_progress_bar()."""
102
# this in turn is abstract, and creates either a tty or dots
103
# bar depending on what we think of the terminal
104
return bzrlib.progress.ProgressBar()
106
def get_password(self, prompt='', **kwargs):
107
"""Prompt the user for a password.
109
:param prompt: The prompt to present the user
110
:param kwargs: Arguments which will be expanded into the prompt.
111
This lets front ends display different things if
113
:return: The password string, return None if the user
114
canceled the request.
116
dialog = PasswordDialog(prompt % kwargs)
117
response = dialog.run()
118
passwd = dialog.passwd
120
if response == gtk.RESPONSE_OK:
125
def nested_progress_bar(self):
126
"""Return a nested progress bar.
128
The actual bar type returned depends on the progress module which
129
may return a tty or dots bar depending on the terminal.
131
FIXME: It should return a GtkProgressBar actually.
133
if self._progress_bar_stack is None:
134
self._progress_bar_stack = bzrlib.progress.ProgressBarStack(
135
klass=self._bar_type)
136
return self._progress_bar_stack.get_nested()
138
def clear_term(self):
139
"""Prepare the terminal for output.
141
It has no sense when talking about GTK."""