root/branches/jingle/src/adhoc_commands.py

Revision 8205, 16.0 kB (checked in by asterix, 18 months ago)

[elghinn] code cleanup

Line 
1# -*- coding: utf-8 -*-
2##      adhoc_commands.py
3##
4## Copyright (C) 2006 Yann Le Boulanger <asterix@lagaule.org>
5##                    Nikos Kouremenos <nkour@jabber.org>
6##
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published
9## by the Free Software Foundation; version 2 only.
10##
11## This program is distributed in the hope that it will be useful,
12## but WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14## GNU General Public License for more details.
15
16# FIXME: think if we need caching command list. it may be wrong if there will
17# be entities that often change the list, it may be slow to fetch it every time
18
19import gobject
20import gtk
21
22from common import xmpp, gajim, dataforms
23
24import gtkgui_helpers
25import dialogs
26import dataforms_widget
27
28class CommandWindow:
29        '''Class for a window for single ad-hoc commands session. Note, that
30        there might be more than one for one account/jid pair in one moment.
31
32        TODO: maybe put this window into MessageWindow? consider this when
33        TODO: it will be possible to manage more than one window of one
34        TODO: account/jid pair in MessageWindowMgr.
35
36        TODO: gtk 2.10 has a special wizard-widget, consider using it...'''
37
38        def __init__(self, account, jid):
39                '''Create new window.'''
40
41                # an account object
42                self.account = gajim.connections[account]
43                self.jid = jid
44
45                self.pulse_id=None      # to satisfy self.setup_pulsing()
46                self.commandlist=None   # a list of (commandname, commanddescription)
47
48                # command's data
49                self.commandnode = None
50                self.sessionid = None
51                self.dataform = None
52
53                # retrieving widgets from xml
54                self.xml = gtkgui_helpers.get_glade('adhoc_commands_window.glade')
55                self.window = self.xml.get_widget('adhoc_commands_window')
56                self.window.connect('delete-event', self.on_adhoc_commands_window_delete_event)
57                for name in ('back_button', 'forward_button',
58                        'execute_button','close_button','stages_notebook',
59                        'retrieving_commands_stage_vbox',
60                        'command_list_stage_vbox','command_list_vbox',
61                        'sending_form_stage_vbox','sending_form_progressbar',
62                        'notes_label','no_commands_stage_vbox','error_stage_vbox',
63                        'error_description_label'):
64                        self.__dict__[name] = self.xml.get_widget(name)
65
66                # creating data forms widget
67                self.data_form_widget = dataforms_widget.DataFormWidget()
68                self.data_form_widget.show()
69                self.sending_form_stage_vbox.pack_start(self.data_form_widget)
70
71                # setting initial stage
72                self.stage1()
73
74                # displaying the window
75                self.xml.signal_autoconnect(self)
76                self.window.show_all()
77
78# these functions are set up by appropriate stageX methods
79        def stage_finish(self, *anything): pass
80        def stage_back_button_clicked(self, *anything): assert False
81        def stage_forward_button_clicked(self, *anything): assert False
82        def stage_execute_button_clicked(self, *anything): assert False
83        def stage_close_button_clicked(self, *anything): assert False
84        def stage_adhoc_commands_window_delete_event(self, *anything): assert False
85        def do_nothing(self, *anything): return False
86
87# widget callbacks
88        def on_back_button_clicked(self, *anything):
89                return self.stage_back_button_clicked(*anything)
90
91        def on_forward_button_clicked(self, *anything):
92                return self.stage_forward_button_clicked(*anything)
93
94        def on_execute_button_clicked(self, *anything):
95                return self.stage_execute_button_clicked(*anything)
96
97        def on_close_button_clicked(self, *anything):
98                return self.stage_close_button_clicked(*anything)
99
100        def on_adhoc_commands_window_destroy(self, *anything):
101                # TODO: do all actions that are needed to remove this object from memory...
102                self.remove_pulsing()
103
104        def on_adhoc_commands_window_delete_event(self, *anything):
105                return self.stage_adhoc_commands_window_delete_event(self.window)
106
107        def __del__(self):
108                print "Object has been deleted."
109
110# stage 1: waiting for command list
111        def stage1(self):
112                '''Prepare the first stage. Request command list,
113                set appropriate state of widgets.'''
114                # close old stage...
115                self.stage_finish()
116
117                # show the stage
118                self.stages_notebook.set_current_page(
119                        self.stages_notebook.page_num(
120                                self.retrieving_commands_stage_vbox))
121
122                # set widgets' state
123                self.close_button.set_sensitive(True)
124                self.back_button.set_sensitive(False)
125                self.forward_button.set_sensitive(False)
126                self.execute_button.set_sensitive(False)
127
128                # request command list
129                self.request_command_list()
130                self.setup_pulsing(
131                        self.xml.get_widget('retrieving_commands_progressbar'))
132
133                # setup the callbacks
134                self.stage_finish = self.stage1_finish
135                self.stage_close_button_clicked = self.stage1_close_button_clicked
136                self.stage_adhoc_commands_window_delete_event = self.stage1_adhoc_commands_window_delete_event
137
138        def stage1_finish(self):
139                self.remove_pulsing()
140
141        def stage1_close_button_clicked(self, widget):
142                # cancelling in this stage is not critical, so we don't
143                # show any popups to user
144                self.stage1_finish()
145                self.window.destroy()
146
147        def stage1_adhoc_commands_window_delete_event(self, widget):
148                self.stage1_finish()
149                return True
150
151# stage 2: choosing the command to execute
152        def stage2(self):
153                '''Populate the command list vbox with radiobuttons
154                (FIXME: if there is more commands, maybe some kind of list?),
155                set widgets' state.'''
156                # close old stage
157                self.stage_finish()
158
159                assert len(self.commandlist)>0
160
161                self.stages_notebook.set_current_page(
162                        self.stages_notebook.page_num(
163                                self.command_list_stage_vbox))
164
165                self.close_button.set_sensitive(True)
166                self.back_button.set_sensitive(False)
167                self.forward_button.set_sensitive(True)
168                self.execute_button.set_sensitive(False)
169
170                # build the commands list radiobuttons
171                first_radio = None
172                for (commandnode, commandname) in self.commandlist:
173                        radio = gtk.RadioButton(first_radio, label=commandname)
174                        radio.connect("toggled", self.on_command_radiobutton_toggled, commandnode)
175                        if not first_radio:
176                                first_radio = radio
177                                self.commandnode = commandnode
178                        self.command_list_vbox.pack_start(radio, expand=False)
179                self.command_list_vbox.show_all()
180
181                self.stage_finish = self.stage2_finish
182                self.stage_close_button_clicked = self.stage2_close_button_clicked
183                self.stage_forward_button_clicked = self.stage2_forward_button_clicked
184                self.stage_adhoc_commands_window_delete_event = self.do_nothing
185
186        def stage2_finish(self):
187                '''Remove widgets we created. Not needed when the window is destroyed.'''
188                def remove_widget(widget):
189                        self.command_list_vbox.remove(widget)
190                self.command_list_vbox.foreach(remove_widget)
191
192        def stage2_close_button_clicked(self, widget):
193                self.stage_finish()
194                self.window.destroy()
195
196        def stage2_forward_button_clicked(self, widget):
197                self.stage3()
198
199        def on_command_radiobutton_toggled(self, widget, commandnode):
200                self.commandnode = commandnode
201
202        def on_check_commands_1_button_clicked(self, widget):
203                self.stage1()
204
205# stage 3: command invocation
206        def stage3(self):
207                # close old stage
208                self.stage_finish()
209
210                assert isinstance(self.commandnode, unicode)
211
212                self.form_status = None
213
214                self.stages_notebook.set_current_page(
215                        self.stages_notebook.page_num(
216                                self.sending_form_stage_vbox))
217
218                self.close_button.set_sensitive(True)
219                self.back_button.set_sensitive(False)
220                self.forward_button.set_sensitive(False)
221                self.execute_button.set_sensitive(False)
222
223                self.stage3_submit_form()
224
225                self.stage_finish = self.stage3_finish
226                self.stage_back_button_clicked = self.stage3_back_button_clicked
227                self.stage_forward_button_clicked = self.stage3_forward_button_clicked
228                self.stage_execute_button_clicked = self.stage3_execute_button_clicked
229                self.stage_close_button_clicked = self.stage3_close_button_clicked
230                self.stage_adhoc_commands_window_delete_event = self.stage3_close_button_clicked
231
232        def stage3_finish(self):
233                pass
234
235        def stage3_close_button_clicked(self, widget):
236                ''' We are in the middle of executing command. Ask user if he really want to cancel
237                the process, then... cancel it. '''
238                # this works also as a handler for window_delete_event, so we have to return appropriate
239                # values
240                if self.form_status == 'completed':
241                        if widget!=self.window:
242                                self.window.destroy()
243                        return False
244
245                dialog = dialogs.HigDialog(self.window, gtk.DIALOG_DESTROY_WITH_PARENT | gtk.DIALOG_MODAL,
246                        gtk.BUTTONS_YES_NO, 'Cancel confirmation',
247                        'You are in process of executing command. Do you really want to cancel it?')
248                dialog.popup()
249                if dialog.get_response()==gtk.RESPONSE_YES:
250                        self.send_cancel()
251                        if widget==self.window:
252                                return False
253                        else:
254                                self.window.destroy()
255                        return False
256                return True
257
258        def stage3_back_button_clicked(self, widget):
259                self.stage3_submit_form('prev')
260
261        def stage3_forward_button_clicked(self, widget):
262                self.stage3_submit_form('next')
263
264        def stage3_execute_button_clicked(self, widget):
265                self.stage3_submit_form('execute')
266
267        def stage3_submit_form(self, action='execute'):
268                self.data_form_widget.set_sensitive(False)
269                if self.data_form_widget.get_data_form():
270                        self.data_form_widget.data_form.type='submit'
271                else:
272                        self.data_form_widget.hide()
273
274                self.close_button.set_sensitive(True)
275                self.back_button.set_sensitive(False)
276                self.forward_button.set_sensitive(False)
277                self.execute_button.set_sensitive(False)
278
279                self.sending_form_progressbar.show()
280                self.setup_pulsing(self.sending_form_progressbar)
281                self.send_command(action)
282
283        def stage3_next_form(self, command):
284                assert isinstance(command, xmpp.Node)
285
286                self.remove_pulsing()
287                self.sending_form_progressbar.hide()
288
289                if not self.sessionid:
290                        self.sessionid = command.getAttr('sessionid')
291
292                self.form_status = command.getAttr('status')
293
294                self.commandnode = command.getAttr('node')
295                if command.getTag('x'):
296                        self.dataform = dataforms.ExtendForm(node=command.getTag('x'))
297
298                        self.data_form_widget.set_sensitive(True)
299                        try:
300                                self.data_form_widget.data_form=self.dataform
301                        except dataforms.Error:
302                                # FIXME: translate
303                                self.stage5(error='Service sent malformed data', senderror=True)
304                                return
305                        self.data_form_widget.show()
306                        if self.data_form_widget.title:
307                                self.window.set_title("%s - Ad-hoc Commands - Gajim" % \
308                                        self.data_form_widget.title)
309                else:
310                        self.data_form_widget.hide()
311
312                actions = command.getTag('actions')
313                if actions:
314                        # actions, actions, actions...
315                        self.close_button.set_sensitive(True)
316                        self.back_button.set_sensitive(actions.getTag('prev') is not None)
317                        self.forward_button.set_sensitive(actions.getTag('next') is not None)
318                        self.execute_button.set_sensitive(True)
319                else:
320                        self.close_button.set_sensitive(True)
321                        self.back_button.set_sensitive(False)
322                        self.forward_button.set_sensitive(False)
323                        self.execute_button.set_sensitive(True)
324
325                if self.form_status == 'completed':
326                        self.close_button.set_sensitive(True)
327                        self.back_button.hide()
328                        self.forward_button.hide()
329                        self.execute_button.hide()
330                        self.close_button.show()
331                        self.stage_adhoc_commands_window_delete_event = self.stage3_close_button_clicked
332
333                note = command.getTag('note')
334                if note:
335                        self.notes_label.set_text(note.getData().decode('utf-8'))
336                        self.notes_label.set_no_show_all(False)
337                        self.notes_label.show()
338                else:
339                        self.notes_label.set_no_show_all(True)
340                        self.notes_label.hide()
341
342# stage 4: no commands are exposed
343        def stage4(self):
344                '''Display the message. Wait for user to close the window'''
345                # close old stage
346                self.stage_finish()
347
348                self.stages_notebook.set_current_page(
349                        self.stages_notebook.page_num(
350                                self.no_commands_stage_vbox))
351
352                self.close_button.set_sensitive(True)
353                self.back_button.set_sensitive(False)
354                self.forward_button.set_sensitive(False)
355                self.execute_button.set_sensitive(False)
356
357                self.stage_finish = self.do_nothing
358                self.stage_close_button_clicked = self.stage4_close_button_clicked
359                self.stage_adhoc_commands_window_delete_event = self.do_nothing
360
361        def stage4_close_button_clicked(self, widget):
362                self.window.destroy()
363
364        def on_check_commands_2_button_clicked(self, widget):
365                self.stage1()
366
367# stage 5: an error has occured
368        def stage5(self, error=None, errorid=None, senderror=False):
369                '''Display the error message. Wait for user to close the window'''
370                # FIXME: sending error to responder
371                # close old stage
372                self.stage_finish()
373
374                assert errorid or error
375
376                if errorid:
377                        # we've got error code, display appropriate message
378                        try:
379                                errorname = xmpp.NS_STANZAS + ' ' + str(errorid)
380                                errordesc = xmpp.ERRORS[errorname][2]
381                                error = errordesc.decode('utf-8')
382                                del errorname, errordesc
383                        except KeyError:        # when stanza doesn't have error description
384                                error = 'Service returned an error.'
385                elif error:
386                        # we've got error message
387                        pass
388                else:
389                        # we don't know what's that, bailing out
390                        assert False
391
392                self.stages_notebook.set_current_page(
393                        self.stages_notebook.page_num(
394                                self.error_stage_vbox))
395
396                self.close_button.set_sensitive(True)
397                self.back_button.hide()
398                self.forward_button.hide()
399                self.execute_button.hide()
400
401                self.error_description_label.set_text(error)
402
403                self.stage_finish = self.do_nothing
404                self.stage_close_button_clicked = self.stage5_close_button_clicked
405                self.stage_adhoc_commands_window_delete_event = self.do_nothing
406
407        def stage5_close_button_clicked(self, widget):
408                self.window.destroy()
409
410# helpers to handle pulsing in progressbar
411        def setup_pulsing(self, progressbar):
412                '''Set the progressbar to pulse. Makes a custom
413                function to repeatedly call progressbar.pulse() method.'''
414                assert not self.pulse_id
415                assert isinstance(progressbar, gtk.ProgressBar)
416
417                def callback():
418                        progressbar.pulse()
419                        return True     # important to keep callback be called back!
420
421                # 12 times per second (80 miliseconds)
422                self.pulse_id = gobject.timeout_add(80, callback)
423
424        def remove_pulsing(self):
425                '''Stop pulsing, useful when especially when removing widget.'''
426                if self.pulse_id:
427                        gobject.source_remove(self.pulse_id)
428                self.pulse_id=None
429
430# handling xml stanzas
431        def request_command_list(self):
432                '''Request the command list. Change stage on delivery.'''
433                query = xmpp.Iq(typ='get', to=xmpp.JID(self.jid), xmlns=xmpp.NS_DISCO_ITEMS)
434                query.setQuerynode(xmpp.NS_COMMANDS)
435
436                def callback(response):
437                        '''Called on response to query.'''
438                        # FIXME: move to connection_handlers.py
439                        # is error => error stage
440                        error = response.getError()
441                        if error:
442                                # extracting error description from xmpp/protocol.py
443                                self.stage5(errorid = error)
444                                return
445
446                        # no commands => no commands stage
447                        # commands => command selection stage
448                        query = response.getTag('query')
449                        if query:
450                                items = query.getTags('item')
451                        else:
452                                items = []
453                        if len(items)==0:
454                                self.commandlist = []
455                                self.stage4()
456                        else:
457                                self.commandlist = [(t.getAttr('node'), t.getAttr('name')) for t in items]
458                                self.stage2()
459
460                self.account.connection.SendAndCallForResponse(query, callback)
461
462        def send_command(self, action='execute'):
463                '''Send the command with data form. Wait for reply.'''
464                # create the stanza
465                assert isinstance(self.commandnode, unicode)
466                assert action in ('execute', 'prev', 'next', 'complete')
467
468                stanza = xmpp.Iq(typ='set', to=self.jid)
469                cmdnode = stanza.addChild('command', attrs={
470                                'xmlns':xmpp.NS_COMMANDS,
471                                'node':self.commandnode,
472                                'action':action
473                        })
474
475                if self.sessionid:
476                        cmdnode.setAttr('sessionid', self.sessionid)
477
478                if self.data_form_widget.data_form:
479#                       cmdnode.addChild(node=dataforms.DataForm(tofill=self.data_form_widget.data_form))
480                        # FIXME: simplified form to send
481                       
482                        cmdnode.addChild(node=self.data_form_widget.data_form)
483
484                def callback(response):
485                        # FIXME: move to connection_handlers.py
486                        err = response.getError()
487                        if err:
488                                self.stage5(errorid = err)
489                        else:
490                                self.stage3_next_form(response.getTag('command'))
491
492                self.account.connection.SendAndCallForResponse(stanza, callback)
493
494        def send_cancel(self):
495                '''Send the command with action='cancel'. '''
496                assert self.commandnode
497                if self.sessionid and self.account.connection:
498                        # we already have sessionid, so the service sent at least one reply.
499                        stanza = xmpp.Iq(typ='set', to=self.jid)
500                        stanza.addChild('command', attrs={
501                                        'xmlns':xmpp.NS_COMMANDS,
502                                        'node':self.commandnode,
503                                        'sessionid':self.sessionid,
504                                        'action':'cancel'
505                                })
506
507                        self.account.connection.send(stanza)
508                else:
509                        # we did not received any reply from service; FIXME: we should wait and
510                        # then send cancel; for now we do nothing
511                        pass
Note: See TracBrowser for help on using the browser.