root/branches/gajim_0.11.1/src/adhoc_commands.py

Revision 8745, 16.2 kB (checked in by asterix, 12 months ago)

ad-hoc commands: update sessionid each time we get a reply in case it has changed

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 first_radio is None:
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() is None:
270                        self.data_form_widget.hide()
271                else:
272                        self.data_form_widget.data_form.type='submit'
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                self.sessionid = command.getAttr('sessionid')
290
291                self.form_status = command.getAttr('status')
292
293                self.commandnode = command.getAttr('node')
294                if command.getTag('x') is not None:
295                        self.dataform = dataforms.ExtendForm(node=command.getTag('x'))
296
297                        self.data_form_widget.set_sensitive(True)
298                        try:
299                                self.data_form_widget.data_form=self.dataform
300                        except dataforms.Error:
301                                # FIXME: translate
302                                self.stage5(error='Service sent malformed data', senderror=True)
303                                return
304                        self.data_form_widget.show()
305                        if self.data_form_widget.title:
306                                self.window.set_title("%s - Ad-hoc Commands - Gajim" % \
307                                        self.data_form_widget.title)
308                else:
309                        self.data_form_widget.hide()
310
311                actions = command.getTag('actions')
312                if actions is None:
313                        self.close_button.set_sensitive(True)
314                        self.back_button.set_sensitive(False)
315                        self.forward_button.set_sensitive(False)
316                        self.execute_button.set_sensitive(True)
317                else:
318                        # actions, actions, actions...
319                        self.close_button.set_sensitive(True)
320                        self.back_button.set_sensitive(actions.getTag('prev') is not None)
321                        self.forward_button.set_sensitive(actions.getTag('next') is not None)
322                        self.execute_button.set_sensitive(True)
323
324                if self.form_status == 'completed':
325                        self.close_button.set_sensitive(True)
326                        self.back_button.hide()
327                        self.forward_button.hide()
328                        self.execute_button.hide()
329                        self.close_button.show()
330                        self.stage_adhoc_commands_window_delete_event = self.stage3_close_button_clicked
331
332                note = command.getTag('note')
333                if note is not None:
334                        self.notes_label.set_text(note.getData().decode('utf-8'))
335                        self.notes_label.set_no_show_all(False)
336                        self.notes_label.show()
337                else:
338                        self.notes_label.set_no_show_all(True)
339                        self.notes_label.hide()
340
341# stage 4: no commands are exposed
342        def stage4(self):
343                '''Display the message. Wait for user to close the window'''
344                # close old stage
345                self.stage_finish()
346
347                self.stages_notebook.set_current_page(
348                        self.stages_notebook.page_num(
349                                self.no_commands_stage_vbox))
350
351                self.close_button.set_sensitive(True)
352                self.back_button.set_sensitive(False)
353                self.forward_button.set_sensitive(False)
354                self.execute_button.set_sensitive(False)
355
356                self.stage_finish = self.do_nothing
357                self.stage_close_button_clicked = self.stage4_close_button_clicked
358                self.stage_adhoc_commands_window_delete_event = self.do_nothing
359
360        def stage4_close_button_clicked(self, widget):
361                self.window.destroy()
362
363        def on_check_commands_2_button_clicked(self, widget):
364                self.stage1()
365
366# stage 5: an error has occured
367        def stage5(self, error=None, errorid=None, senderror=False):
368                '''Display the error message. Wait for user to close the window'''
369                # FIXME: sending error to responder
370                # close old stage
371                self.stage_finish()
372
373                assert errorid is not None or error is not None
374
375                if errorid is not None:
376                        # we've got error code, display appropriate message
377                        try:
378                                errorname = xmpp.NS_STANZAS + ' ' + str(errorid)
379                                errordesc = xmpp.ERRORS[errorname][2]
380                                error = errordesc.decode('utf-8')
381                                del errorname, errordesc
382                        except KeyError:        # when stanza doesn't have error description
383                                error = 'Service returned an error.'
384                elif error is not None:
385                        # we've got error message
386                        pass
387                else:
388                        # we don't know what's that, bailing out
389                        assert False
390
391                self.stages_notebook.set_current_page(
392                        self.stages_notebook.page_num(
393                                self.error_stage_vbox))
394
395                self.close_button.set_sensitive(True)
396                self.back_button.hide()
397                self.forward_button.hide()
398                self.execute_button.hide()
399
400                self.error_description_label.set_text(error)
401
402                self.stage_finish = self.do_nothing
403                self.stage_close_button_clicked = self.stage5_close_button_clicked
404                self.stage_adhoc_commands_window_delete_event = self.do_nothing
405
406        def stage5_close_button_clicked(self, widget):
407                self.window.destroy()
408
409# helpers to handle pulsing in progressbar
410        def setup_pulsing(self, progressbar):
411                '''Set the progressbar to pulse. Makes a custom
412                function to repeatedly call progressbar.pulse() method.'''
413                assert self.pulse_id is None
414                assert isinstance(progressbar, gtk.ProgressBar)
415
416                def callback():
417                        progressbar.pulse()
418                        return True     # important to keep callback be called back!
419
420                # 12 times per second (80 miliseconds)
421                self.pulse_id = gobject.timeout_add(80, callback)
422
423        def remove_pulsing(self):
424                '''Stop pulsing, useful when especially when removing widget.'''
425                if self.pulse_id is not None:
426                        gobject.source_remove(self.pulse_id)
427                self.pulse_id=None
428
429# handling xml stanzas
430        def request_command_list(self):
431                '''Request the command list. Change stage on delivery.'''
432                query = xmpp.Iq(typ='get', to=xmpp.JID(self.jid), xmlns=xmpp.NS_DISCO_ITEMS)
433                query.setQuerynode(xmpp.NS_COMMANDS)
434
435                def callback(response):
436                        '''Called on response to query.'''
437                        # FIXME: move to connection_handlers.py
438                        # is error => error stage
439                        error = response.getError()
440                        if error is not None:
441                                # extracting error description from xmpp/protocol.py
442                                self.stage5(errorid = error)
443                                return
444
445                        # no commands => no commands stage
446                        # commands => command selection stage
447                        query = response.getTag('query')
448                        if query:
449                                items = query.getTags('item')
450                        else:
451                                items = []
452                        if len(items)==0:
453                                self.commandlist = []
454                                self.stage4()
455                        else:
456                                self.commandlist = [(t.getAttr('node'), t.getAttr('name')) for t in items]
457                                self.stage2()
458
459                self.account.connection.SendAndCallForResponse(query, callback)
460
461        def send_command(self, action='execute'):
462                '''Send the command with data form. Wait for reply.'''
463                # create the stanza
464                assert isinstance(self.commandnode, unicode)
465                assert action in ('execute', 'prev', 'next', 'complete')
466
467                stanza = xmpp.Iq(typ='set', to=self.jid)
468                cmdnode = stanza.addChild('command', attrs={
469                                'xmlns':xmpp.NS_COMMANDS,
470                                'node':self.commandnode,
471                                'action':action
472                        })
473
474                if self.sessionid is not None:
475                        cmdnode.setAttr('sessionid', self.sessionid)
476
477                if self.data_form_widget.data_form is not None:
478#                       cmdnode.addChild(node=dataforms.DataForm(tofill=self.data_form_widget.data_form))
479                        # FIXME: simplified form to send
480                       
481                        cmdnode.addChild(node=self.data_form_widget.data_form)
482
483                def callback(response):
484                        # FIXME: move to connection_handlers.py
485                        err = response.getError()
486                        if err is not None:
487                                self.stage5(errorid = err)
488                        else:
489                                self.stage3_next_form(response.getTag('command'))
490
491                self.account.connection.SendAndCallForResponse(stanza, callback)
492
493        def send_cancel(self):
494                '''Send the command with action='cancel'. '''
495                assert self.commandnode is not None
496                if self.sessionid is not None and self.account.connection:
497                        # we already have sessionid, so the service sent at least one reply.
498                        stanza = xmpp.Iq(typ='set', to=self.jid)
499                        stanza.addChild('command', attrs={
500                                        'xmlns':xmpp.NS_COMMANDS,
501                                        'node':self.commandnode,
502                                        'sessionid':self.sessionid,
503                                        'action':'cancel'
504                                })
505
506                        self.account.connection.send(stanza)
507                else:
508                        # we did not received any reply from service; FIXME: we should wait and
509                        # then send cancel; for now we do nothing
510                        pass
Note: See TracBrowser for help on using the browser.