2
# -*- coding: utf-8 -*-
4
# Copyright (C) 2007-2011 Tualatrix Chou <tualatrix@gmail.com>
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License version 3, as published
7
# by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program. If not, see <http://www.gnu.org/licenses/>.
20
from gi.repository import GObject, Gtk, Gdk, Pango, Vte
22
from gui.gtk import set_busy, unset_busy
25
class BaseDialog(Gtk.MessageDialog):
26
def __init__(self, **kwargs):
27
title = kwargs.pop('title', '')
28
message = kwargs.pop('message', '')
30
GObject.GObject.__init__(self, **kwargs)
36
self.set_content(message)
38
def set_title(self, title):
39
self.set_markup('<big><b>%s</b></big>' % title)
41
def set_content(self, message):
42
if self.get_property('text'):
43
self.format_secondary_markup(message)
45
self.set_markup(message)
51
def add_option_button(self, button):
52
'''Add an option button to the left. It will not grab the default response.'''
53
vbox = self.get_content_area()
54
hbuttonbox = vbox.get_children()[-1]
56
hbox = Gtk.HBox(spacing=12)
57
vbox.pack_start(hbox, False, False, 0)
58
vbox.remove(hbuttonbox)
60
new_hbuttonbox = Gtk.HButtonBox()
61
new_hbuttonbox.set_layout(Gtk.ButtonBoxStyle.START)
62
new_hbuttonbox.pack_start(button, True, True, 0)
64
hbox.pack_start(new_hbuttonbox, True, True, 0)
65
hbox.pack_start(hbuttonbox, True, True, 0)
67
hbuttonbox.get_children()[-1].grab_focus()
72
class ErrorDialog(BaseDialog):
73
def __init__(self, title='', message='', parent=None,
74
type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK):
75
BaseDialog.__init__(self, title=title, message=message,
76
parent=parent, message_type=type, buttons=buttons)
79
class InfoDialog(BaseDialog):
80
def __init__(self, title='', message='', parent=None,
81
type=Gtk.MessageType.INFO, buttons=Gtk.ButtonsType.OK):
82
BaseDialog.__init__(self, title=title, message=message,
83
parent=parent, message_type=type, buttons=buttons)
86
class WarningDialog(BaseDialog):
87
def __init__(self, title='', message='', parent=None,
88
type=Gtk.MessageType.WARNING, buttons=Gtk.ButtonsType.OK):
89
BaseDialog.__init__(self, title=title, message=message,
90
parent=parent, message_type=type, buttons=buttons)
93
class QuestionDialog(BaseDialog):
94
def __init__(self, title='', message='', parent=None,
95
type=Gtk.MessageType.QUESTION, buttons=Gtk.ButtonsType.YES_NO):
96
BaseDialog.__init__(self, title=title, message=message,
97
parent=parent, message_type=type, buttons=buttons)
100
class BusyDialog(Gtk.Dialog):
101
def __init__(self, parent=None):
102
GObject.GObject.__init__(self, parent=parent)
105
self.parent_window = parent
107
self.parent_window = None
110
set_busy(self.parent_window)
112
def unset_busy(self):
113
unset_busy(self.parent_window)
117
return super(BusyDialog, self).run()
121
super(BusyDialog, self).destroy()
124
class ProcessDialog(BusyDialog):
126
'error': (GObject.SignalFlags.RUN_FIRST, None, (GObject.TYPE_STRING,)),
127
'done': (GObject.SignalFlags.RUN_FIRST, None, ()),
130
def __init__(self, parent):
131
super(ProcessDialog, self).__init__(parent=parent)
133
self.set_border_width(6)
135
self.set_resizable(False)
136
self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
138
vbox = self.get_content_area()
141
self._label = Gtk.Label()
142
self._label.set_alignment(0, 0.5)
143
vbox.pack_start(self._label, False, False, 0)
145
self._progressbar = Gtk.ProgressBar()
146
self._progressbar.set_ellipsize(Pango.EllipsizeMode.END)
147
self._progressbar.set_size_request(320, -1)
148
vbox.pack_start(self._progressbar, False, False, 0)
153
self._progressbar.pulse()
155
def set_fraction(self, fraction):
156
self._progressbar.set_fraction(fraction)
158
def set_dialog_lable(self, text):
159
self._label.set_markup('<b><big>%s</big></b>' % text)
161
def set_progress_text(self, text):
162
self._progressbar.set_text(text)
164
def process_data(self):
165
return NotImplemented
168
class SmartTerminal(Vte.Terminal):
169
def insert(self, string):
170
self.feed(string, -1)
172
def future_insert(self, string):
173
#TODO use this in Gtk+3.0
174
column_count = self.get_column_count()
175
column, row = self.get_cursor_position()
177
column = column_count
178
if column != column_count:
179
self.feed(' ' * (column_count - column))
180
space_length = column_count - len(string)
181
string = string + ' ' * space_length
185
class TerminalDialog(ProcessDialog):
186
def __init__(self, parent):
187
super(TerminalDialog, self).__init__(parent=parent)
189
vbox = self.get_content_area()
191
self.set_position(Gtk.WindowPosition.CENTER_ALWAYS)
192
self.expendar = Gtk.Expander()
193
self.expendar.set_spacing(6)
194
self.expendar.set_label(_('Details'))
195
vbox.pack_start(self.expendar, False, False, 6)
197
self.terminal = SmartTerminal()
198
self.terminal.set_size_request(562, 362)
199
self.expendar.add(self.terminal)
204
class AuthenticateFailDialog(ErrorDialog):
206
ErrorDialog.__init__(self,
207
title=_('Could not authenticate'),
208
message=_('An unexpected error has occurred.'))
211
class ServerErrorDialog(ErrorDialog):
213
ErrorDialog.__init__(self,
214
title=_("Service hasn't initialized yet"),
215
message=_('You need to restart your computer.'))