~ubuntu-branches/ubuntu/oneiric/emesene/oneiric

« back to all changes in this revision

Viewing changes to plugins_base/CustomStatus.py

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-01-06 23:25:28 UTC
  • mfrom: (1.1.5 upstream) (5.2.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100106232528-aumo0oasgv98865j
Tags: 1.6-1
* New upstream release.
  - debian/patches/20_dont_build_own_libmimic.patch:
    + Refreshed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import Plugin
20
20
import gtk
21
21
import re
 
22
import gobject
22
23
from Parser import TAGS_NONE
23
24
 
24
25
PLUGIN_NAME = 'CustomStatus'
58
59
                        'filter', self.customStatus.customStatusParser)
59
60
        self.controller.emit('preferences-changed')  
60
61
        self.customStatus.unsetCustomStatus()
 
62
        self.customStatus.register_slash_commands()
 
63
        statusMenuItem = gtk.MenuItem (_('Custom status...'), False)
 
64
        statusMenuItem.connect("activate", self.on_status_menu_activated)
 
65
        self.controller.mainWindow.userPanel.statusMenu.add(statusMenuItem)
61
66
        self.enabled = True
62
67
 
63
68
    def stop(self):
64
69
        self.disconnect(self.onStatusChangeId)
65
70
        self.controller.unifiedParser.disconnect(self.customStatusParserId)
 
71
        self.customStatus.unregister_slash_commands()
66
72
        self.enabled = False
67
73
 
68
74
    def check(self):
72
78
        CustomStatusDialog(self.customStatus, DIALOG_TYPE_CONFIG).run()
73
79
        return True
74
80
 
 
81
    def on_status_menu_activated(self, item):
 
82
        self.customStatus.statusChanged(None, "AWY")
75
83
 
76
84
class CustomStatus:
77
85
    '''
86
94
        self.msn = msn
87
95
        self.config = config
88
96
        self.name = PLUGIN_NAME
 
97
        self.slash = controller.Slash
89
98
 
90
99
        # We need a semaphore to block the status changing
91
100
        self.doChange = True
133
142
            # We temporary block custom status changing to avoid double-dialog
134
143
            self.doChange = False
135
144
 
136
 
            response = CustomStatusDialog(self, DIALOG_TYPE_SET, status).run()
 
145
            response = CustomStatusDialog(self, DIALOG_TYPE_SET, status, "status_changed").run()
137
146
            if response != None:
138
147
                status_type, status_name,\
139
 
                    automessage, automessage_text = response
 
148
                    automessage, automessage_text, slash_command = response
140
149
                self.setCustomStatus(status_type, status_name,\
141
150
                                        automessage, automessage_text)
142
151
            # We enable again custom status changing
154
163
       
155
164
        if automessage_enabled and autoreply_message != '':            
156
165
            self.controller.config.user['autoReply'] = True
157
 
            self.controller.mainWindow.menu.activateAutoReply.set_active(True)
158
166
            self.controller.autoReplyMessage = autoreply_message
159
167
            self.controller.config.user['autoReplyMessage'] = autoreply_message
160
168
 
166
174
        if self.msn.getNick() != newnick:
167
175
            self.msn.changeNick(statusTag.sub('', self.msn.getNick()))
168
176
        self.controller.config.user['autoReply'] = False
169
 
        self.controller.mainWindow.menu.activateAutoReply.set_active(False)
170
177
 
171
178
    def loadSavedStatus(self):
172
179
        '''Method that loads saved custom statuses. It returns a list of lists 
173
 
        [statustype, statusname, autoreplyenabled, autoreplymessage]'''
 
180
        [statustype, statusname, autoreplyenabled, autoreplymessage, slashcommand]'''
174
181
 
175
182
        numStatus = self.config.getPluginValue(self.name,\
176
183
                                                'custom_status_count', 0)
187
194
                                                    'cAR'+str(i), None)
188
195
            cARMessage = self.config.getPluginValue(self.name,\
189
196
                                                    'cARMessage'+str(i), None)
190
 
            
 
197
            cCommand = self.config.getPluginValue(self.name, \
 
198
                                                    'cCommand'+str(i), "")
 
199
 
191
200
            if not (cStatus == None or cName == None or \
192
201
                    cAR == None or cARMessage == None):
193
 
                data.append([cStatus,cName,bool(int(cAR)),cARMessage])        
 
202
                data.append([cStatus,cName,bool(int(cAR)),cARMessage, cCommand])        
194
203
 
195
204
        return data
196
205
 
204
213
                                                        str(int(data[i][2])))
205
214
            self.config.setPluginValue(self.name, 'cARMessage'+str(i), \
206
215
                                                        data[i][3])
 
216
            self.config.setPluginValue(self.name, 'cCommand'+str(i), \
 
217
                                                        data[i][4])
207
218
 
208
219
        # We need to update custom statuses count
209
220
        self.config.setPluginValue(self.name, 'custom_status_count', len(data))
210
221
 
 
222
    def register_slash_commands(self):
 
223
        numStatus = self.config.getPluginValue(self.name,\
 
224
                                                'custom_status_count', 0)
 
225
        for i in range(int(numStatus)):
 
226
            cCommand = self.config.getPluginValue(self.name, \
 
227
                                                    'cCommand'+str(i), None)
 
228
            if not ((cCommand == None) or (cCommand == "")):
 
229
                self.slash.register(cCommand, self.on_slash_command, \
 
230
                                          _("Applies a custom status"))
 
231
 
 
232
    def unregister_slash_commands(self):
 
233
        numStatus = self.config.getPluginValue(self.name,\
 
234
                                                'custom_status_count', 0)
 
235
        for i in range(int(numStatus)):
 
236
            cCommand = self.config.getPluginValue(self.name, \
 
237
                                                    'cCommand'+str(i), None)
 
238
            if not ((cCommand == None) or (cCommand == "")):
 
239
                self.slash.unregister(cCommand)
 
240
 
 
241
    #search for al the custom statuses the one that matches the slash command
 
242
    def on_slash_command(self, slash_action):
 
243
        self.doChange = False
 
244
        data = self.loadSavedStatus()
 
245
        for i in data: #search every status
 
246
            if i[4] == slash_action.name: #if slash_command = custom status command
 
247
                self.setCustomStatus(i[0],i[1],i[2],i[3]) #change status
 
248
        self.doChange = True
 
249
 
211
250
 
212
251
class CustomStatusDialog(gtk.Dialog):
213
252
    '''
216
255
    '''
217
256
 
218
257
    def __init__(self, customStatus, dialog_type=DIALOG_TYPE_SET, \
219
 
                        current_status=None):
 
258
                        current_status=None, source=""):
220
259
        '''Constructor'''
221
260
        
222
261
        gtk.Dialog.__init__(self , _('Custom status setting'), None, \
229
268
        self.controller = self.customStatus.controller
230
269
 
231
270
        self.set_position(gtk.WIN_POS_CENTER_ON_PARENT)
232
 
        self.set_resizable(False)
233
 
        self.set_border_width(12)
 
271
        self.set_size_request(550,-1)
 
272
        self.set_resizable(True)
 
273
        self.set_border_width(10)
234
274
        self.set_has_separator(False)
235
 
        self.vbox.set_spacing(20)
 
275
        self.vbox.set_spacing(15)
 
276
 
 
277
        #if the dialog appears changing status, the timer starts
 
278
        #if the dialog is open with the Configure button in plugins manager, no timer 
 
279
        if source == "status_changed":
 
280
            self.timer_time = 10
 
281
            self.title_text = _('Custom status setting - Closing in: %ds')
 
282
            text = self.title_text % self.timer_time
 
283
            self.set_property("title",text)
 
284
            self.timer = gobject.timeout_add(1000,self.updateTimer)
 
285
            self.set_focus_on_map(False) #dialog appears unfocused
 
286
            self.connect('focus-in-event', self.disableTimer) #when focused, stop the timer
236
287
 
237
288
        # We load the saved custom statuses
238
289
        self.data = self.customStatus.loadSavedStatus()
251
302
 
252
303
        # Listeners
253
304
        self.connect('delete-event', self.closeCancel)
254
 
        self.create_panel.custom_status.connect('activate', self.closeOk)
 
305
        self.create_panel.custom_status.connect('activate', self.closeAccept)
255
306
        self.create_panel.custom_autoreply_message.connect(\
256
 
                                                'activate', self.closeOk)
 
307
                                                'activate', self.closeAccept)
257
308
        self.select_panel.btn_add.connect('clicked', self.addStatus)
258
309
        self.select_panel.btn_remove.connect('clicked', self.delStatus)
259
310
        if dialog_type == DIALOG_TYPE_SET:
270
321
        if status == None:
271
322
            return
272
323
 
273
 
        status_type,status_name, automessage, automessage_text = status
 
324
        status_type,status_name, automessage, automessage_text, command = status
274
325
        if automessage == False:
275
326
            automessage_text = ''
276
327
        self.select_panel.data.append([status_type, status_name, \
277
 
                                        automessage, automessage_text])
 
328
                                        automessage, automessage_text, command])
278
329
        self.select_panel.treeview.addStatusRow(status_type, status_name, \
279
 
                                                automessage,automessage_text)
 
330
                                                automessage,automessage_text,command)
280
331
        self.create_panel.clear()
281
332
 
282
333
    def delStatus(self, *args):
296
347
        self.dataOK = self.data[view_column[0]]
297
348
        self.response(gtk.RESPONSE_OK)
298
349
 
299
 
    def closeOk(self, *args):
300
 
        '''Click on OK button'''
 
350
    def closeAccept(self, *args):
 
351
        '''Click on Accept button'''
301
352
        self.response(gtk.RESPONSE_ACCEPT)
302
353
 
303
354
    def closeCancel(self, *args):
307
358
    def run(self):
308
359
        '''Method that manage the open and close of the dialog.'''
309
360
        response = gtk.Dialog.run(self)
310
 
        self.destroy()   
 
361
        self.destroy()
311
362
 
312
363
        # If we want to enable a custom status...
313
364
        if response == gtk.RESPONSE_ACCEPT or response == gtk.RESPONSE_OK:
 
365
            self.customStatus.unregister_slash_commands()
 
366
 
314
367
            # We save the configuration
315
368
            self.customStatus.saveSavedStatus(self.select_panel.data)
316
369
 
 
370
            self.customStatus.register_slash_commands()
 
371
 
317
372
            # We return the list containing choosed status settings.
318
373
            # If response is ACCEPT we use form data, else we use treeview data
319
374
            if response == gtk.RESPONSE_ACCEPT:
320
375
                return (self.create_panel.getStatus())
321
 
            return (self.dataOK)  
 
376
            return (self.dataOK)
322
377
        # ... or if we click on Cancel button
323
378
        else:   
324
379
            return None
325
380
 
 
381
    #this method updates the title of the dialog if the timer is running
 
382
    def updateTimer(self):
 
383
        self.timer_time = self.timer_time - 1
 
384
        text = self.title_text % self.timer_time
 
385
        self.set_property("title", text)
 
386
        if self.timer_time == 0:
 
387
            self.closeCancel()
 
388
            return False
 
389
        else:
 
390
            return True
 
391
 
 
392
    #disable the timer and change the title bar
 
393
    def disableTimer(self, widget, event):
 
394
        if self == widget:
 
395
            gobject.source_remove(self.timer)
 
396
            self.set_property("title", _('Custom status setting'))
326
397
 
327
398
class CustomStatusTreeView(gtk.TreeView):
328
399
    '''
333
404
    def __init__(self, controller, data):
334
405
        '''Constructor'''
335
406
        
336
 
        self.liststore = gtk.ListStore(str, str, bool, str)
 
407
        self.liststore = gtk.ListStore(str, str, bool, str, str)
337
408
        self.controller = controller
338
409
        self.data = data
339
410
 
344
415
        cell1 = gtk.CellRendererText()
345
416
        cell2 = gtk.CellRendererToggle()
346
417
        cell3 = gtk.CellRendererText()
 
418
        cell4 = gtk.CellRendererText()
347
419
 
348
420
        cell1.set_property('editable', True)
349
421
        cell1.connect('edited', self.onEdited, self.liststore, 1)
351
423
        cell2.connect('toggled', self.onToggled, self.liststore, 2)
352
424
        cell3.set_property('editable', True)
353
425
        cell3.connect('edited', self.onEdited, self.liststore, 3)
 
426
        cell4.set_property('editable', True)
 
427
        cell4.connect('edited', self.onEdited, self.liststore, 4)
354
428
 
355
429
        # Columns
356
430
        col0 = gtk.TreeViewColumn(_('Status'), cell0)
358
432
        col1 = gtk.TreeViewColumn(_('Status Name'), cell1, text=1)
359
433
        col2 = gtk.TreeViewColumn(_('A.R.'), cell2, active=2)
360
434
        col3 = gtk.TreeViewColumn(_('Autoreply message'), cell3, text=3)
 
435
        col4 = gtk.TreeViewColumn(_('Command'), cell4, text=4)
361
436
        
362
437
        col1.set_resizable(True)
363
438
        col3.set_resizable(True)
 
439
        col4.set_resizable(True)
364
440
        
365
441
        self.append_column(col0)
366
442
        self.append_column(col1)
367
443
        self.append_column(col2)
368
444
        self.append_column(col3)
 
445
        self.append_column(col4)
369
446
 
370
447
        # We add the data to the table
371
448
        for item in self.data:
372
449
            self.liststore.append(item)
373
450
    
374
 
    def addStatusRow(self, status, status_name, automessage, automessage_text):
 
451
    def addStatusRow(self, status, status_name, automessage, automessage_text, command):
375
452
        '''Method that adds a row to the table using the liststore object'''
376
453
        self.liststore.append([status, status_name, \
377
 
                                automessage, automessage_text])
 
454
                                automessage, automessage_text, command])
378
455
 
379
456
    def getSelectedRow(self):
380
457
        '''Method that returns the currently selected row'''
579
656
                                    self.status_type.get_active_iter()])[0], \
580
657
                 self.custom_status.get_text().strip(), \
581
658
                 self.custom_autoreply_checkbox.get_active(), \
582
 
                 self.custom_autoreply_message.get_text().strip())
 
659
                 self.custom_autoreply_message.get_text().strip(),"")
583
660
 
584
661
    def enableAutoreply (self, *args):
585
662
        '''Method that enables autoreply text entry'''