~kobe24-lixiang/youker-assistant/trunk

« back to all changes in this revision

Viewing changes to backends/youker-assistant-daemon/src/gui/dialogs.py

  • Committer: Shine Huang
  • Date: 2013-08-26 05:59:11 UTC
  • mfrom: (72.1.35 youker-assistant)
  • Revision ID: hostc@163.com-20130826055911-1losjpsvdiaz5oyf
add monitorball func into systembus

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- coding: utf-8 -*-
3
 
### BEGIN LICENSE
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.
8
 
#
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.
13
 
#
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/>.
16
 
### END LICENSE
17
 
 
18
 
import thread
19
 
 
20
 
from gi.repository import GObject, Gtk, Gdk, Pango, Vte
21
 
 
22
 
from gui.gtk import set_busy, unset_busy
23
 
 
24
 
 
25
 
class BaseDialog(Gtk.MessageDialog):
26
 
    def __init__(self, **kwargs):
27
 
        title = kwargs.pop('title', '')
28
 
        message = kwargs.pop('message', '')
29
 
 
30
 
        GObject.GObject.__init__(self, **kwargs)
31
 
 
32
 
        if title:
33
 
            self.set_title(title)
34
 
 
35
 
        if message:
36
 
            self.set_content(message)
37
 
 
38
 
    def set_title(self, title):
39
 
        self.set_markup('<big><b>%s</b></big>' % title)
40
 
 
41
 
    def set_content(self, message):
42
 
        if self.get_property('text'):
43
 
            self.format_secondary_markup(message)
44
 
        else:
45
 
            self.set_markup(message)
46
 
    
47
 
    def launch(self):
48
 
        self.run()
49
 
        self.destroy()
50
 
 
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]
55
 
 
56
 
        hbox = Gtk.HBox(spacing=12)
57
 
        vbox.pack_start(hbox, False, False, 0)
58
 
        vbox.remove(hbuttonbox)
59
 
 
60
 
        new_hbuttonbox = Gtk.HButtonBox()
61
 
        new_hbuttonbox.set_layout(Gtk.ButtonBoxStyle.START)
62
 
        new_hbuttonbox.pack_start(button, True, True, 0)
63
 
 
64
 
        hbox.pack_start(new_hbuttonbox, True, True, 0)
65
 
        hbox.pack_start(hbuttonbox, True, True, 0)
66
 
 
67
 
        hbuttonbox.get_children()[-1].grab_focus()
68
 
 
69
 
        vbox.show_all()
70
 
 
71
 
 
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)
77
 
 
78
 
 
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)
84
 
 
85
 
 
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)
91
 
 
92
 
 
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)
98
 
 
99
 
 
100
 
class BusyDialog(Gtk.Dialog):
101
 
    def __init__(self, parent=None):
102
 
        GObject.GObject.__init__(self, parent=parent)
103
 
 
104
 
        if parent:
105
 
            self.parent_window = parent
106
 
        else:
107
 
            self.parent_window = None
108
 
 
109
 
    def set_busy(self):
110
 
        set_busy(self.parent_window)
111
 
 
112
 
    def unset_busy(self):
113
 
        unset_busy(self.parent_window)
114
 
 
115
 
    def run(self):
116
 
        self.set_busy()
117
 
        return super(BusyDialog, self).run()
118
 
 
119
 
    def destroy(self):
120
 
        self.unset_busy()
121
 
        super(BusyDialog, self).destroy()
122
 
 
123
 
 
124
 
class ProcessDialog(BusyDialog):
125
 
    __gsignals__ = {
126
 
        'error': (GObject.SignalFlags.RUN_FIRST, None, (GObject.TYPE_STRING,)),
127
 
        'done': (GObject.SignalFlags.RUN_FIRST, None, ()),
128
 
    }
129
 
 
130
 
    def __init__(self, parent):
131
 
        super(ProcessDialog, self).__init__(parent=parent)
132
 
 
133
 
        self.set_border_width(6)
134
 
        self.set_title('')
135
 
        self.set_resizable(False)
136
 
        self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
137
 
 
138
 
        vbox = self.get_content_area()
139
 
        vbox.set_spacing(6)
140
 
 
141
 
        self._label = Gtk.Label()
142
 
        self._label.set_alignment(0, 0.5)
143
 
        vbox.pack_start(self._label, False, False, 0)
144
 
 
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)
149
 
 
150
 
        self.show_all()
151
 
 
152
 
    def pulse(self):
153
 
        self._progressbar.pulse()
154
 
 
155
 
    def set_fraction(self, fraction):
156
 
        self._progressbar.set_fraction(fraction)
157
 
 
158
 
    def set_dialog_lable(self, text):
159
 
        self._label.set_markup('<b><big>%s</big></b>' % text)
160
 
 
161
 
    def set_progress_text(self, text):
162
 
        self._progressbar.set_text(text)
163
 
 
164
 
    def process_data(self):
165
 
        return NotImplemented
166
 
 
167
 
 
168
 
class SmartTerminal(Vte.Terminal):
169
 
    def insert(self, string):
170
 
        self.feed(string, -1)
171
 
 
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()
176
 
        if column == 0:
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
182
 
        self.feed(string)
183
 
 
184
 
 
185
 
class TerminalDialog(ProcessDialog):
186
 
    def __init__(self, parent):
187
 
        super(TerminalDialog, self).__init__(parent=parent)
188
 
 
189
 
        vbox = self.get_content_area()
190
 
 
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)
196
 
 
197
 
        self.terminal = SmartTerminal()
198
 
        self.terminal.set_size_request(562, 362)
199
 
        self.expendar.add(self.terminal)
200
 
 
201
 
        self.show_all()
202
 
 
203
 
 
204
 
class AuthenticateFailDialog(ErrorDialog):
205
 
    def __init__(self):
206
 
        ErrorDialog.__init__(self,
207
 
                             title=_('Could not authenticate'),
208
 
                             message=_('An unexpected error has occurred.'))
209
 
 
210
 
 
211
 
class ServerErrorDialog(ErrorDialog):
212
 
    def __init__(self):
213
 
        ErrorDialog.__init__(self,
214
 
                             title=_("Service hasn't initialized yet"),
215
 
                             message=_('You need to restart your computer.'))