Show
Ignore:
Timestamp:
01/15/07 19:51:27 (23 months ago)
Author:
asterix
Message:

merge usefull diff from trunk

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/gajim_0.11/src/common/commands.py

    r7829 r7841  
    2626                ''' This returns True if that command should be visible and invokable 
    2727                for others. 
    28                 samejid - True when command is invoked by an entity with the same bare jid. 
    29                 ''' 
     28                samejid - True when command is invoked by an entity with the same bare  
     29                jid.''' 
    3030                return True 
    3131 
     
    3535                self.sessionid = sessionid 
    3636 
    37         def buildResponse(self, request, status='executing', defaultaction=None, actions=None): 
     37        def buildResponse(self, request, status = 'executing', defaultaction = None, 
     38        actions = None): 
    3839                assert status in ('executing', 'completed', 'canceled') 
    3940 
     
    4647                if defaultaction is not None or actions is not None: 
    4748                        if defaultaction is not None: 
    48                                 assert defaultaction in ('cancel', 'execute', 'prev', 'next', 'complete') 
     49                                assert defaultaction in ('cancel', 'execute', 'prev', 'next', 
     50                                        'complete') 
    4951                                attrs = {'action': defaultaction} 
    5052                        else: 
     
    5557 
    5658        def badRequest(self, stanza): 
    57                 self.connection.connection.send(xmpp.Error(stanza, xmpp.NS_STANZAS+' bad-request')) 
     59                self.connection.connection.send(xmpp.Error(stanza, xmpp.NS_STANZAS + \ 
     60                        ' bad-request')) 
    5861 
    5962        def cancel(self, request): 
    60                 response, cmd = self.buildResponse(request, status='canceled') 
     63                response, cmd = self.buildResponse(request, status = 'canceled') 
    6164                self.connection.connection.send(response) 
    6265                return False    # finish the session 
     
    6467class ChangeStatusCommand(AdHocCommand): 
    6568        commandnode = 'change-status' 
    66         commandname = 'Change status information' 
     69        commandname = _('Change status information') 
    6770 
    6871        @staticmethod 
     
    7376        def execute(self, request): 
    7477                # first query... 
    75                 response, cmd = self.buildResponse(request, defaultaction='execute', actions=['execute']) 
     78                response, cmd = self.buildResponse(request, defaultaction = 'execute', 
     79                        actions = ['execute']) 
    7680                 
    77                 cmd.addChild(node=dataforms.SimpleDataForm( 
    78                         title='Change status', 
    79                         instructions='Set the presence type and description', 
    80                         fields=[ 
     81                cmd.addChild(node = dataforms.SimpleDataForm( 
     82                        title = _('Change status'), 
     83                        instructions = _('Set the presence type and description'), 
     84                        fields = [ 
    8185                                dataforms.Field('list-single', 
    82                                         var='presence-type', 
    83                                         label='Type of presence:', 
    84                                         options=[ 
    85                                                 (u'free-for-chat', u'Free for chat'), 
    86                                                 (u'online', u'Online'), 
    87                                                 (u'away', u'Away'), 
    88                                                 (u'xa', u'Extended away'), 
    89                                                 (u'dnd', u'Do not disturb'), 
    90                                                 (u'offline', u'Offline - disconnect')], 
    91                                         value='online', 
    92                                         required=True), 
     86                                        var = 'presence-type', 
     87                                        label = 'Type of presence:', 
     88                                        options = [ 
     89                                                (u'free-for-chat', _('Free for chat')), 
     90                                                (u'online', _('Online')), 
     91                                                (u'away', _('Away')), 
     92                                                (u'xa', _('Extended away')), 
     93                                                (u'dnd', _('Do not disturb')), 
     94                                                (u'offline', _('Offline - disconnect'))], 
     95                                        value = 'online', 
     96                                        required = True), 
    9397                                dataforms.Field('text-multi', 
    94                                         var='presence-desc', 
    95                                         label='Presence description:')])) 
     98                                        var = 'presence-desc', 
     99                                        label = _('Presence description:'))])) 
    96100 
    97101                self.connection.connection.send(response) 
     
    105109                # check if the data is correct 
    106110                try: 
    107                         form=dataforms.SimpleDataForm(extend=request.getTag('command').getTag('x')) 
     111                        form = dataforms.SimpleDataForm(extend = request.getTag('command').\ 
     112                                getTag('x')) 
    108113                except: 
    109114                        self.badRequest(request) 
    110115                        return False 
    111                  
     116 
    112117                try: 
    113118                        presencetype = form['presence-type'].value 
     
    126131                        presencedesc = u'' 
    127132 
    128                 response, cmd = self.buildResponse(request, status='completed') 
    129                 cmd.addChild('note', {}, 'The status has been changed.') 
     133                response, cmd = self.buildResponse(request, status = 'completed') 
     134                cmd.addChild('note', {}, _('The status has been changed.')) 
    130135 
    131136                # if going offline, we need to push response so it won't go into 
    132137                # queue and disappear 
    133                 self.connection.connection.send(response, presencetype=='offline') 
     138                self.connection.connection.send(response, presencetype == 'offline') 
    134139 
    135140                # send new status 
    136                 gajim.interface.roster.send_status(self.connection.name, presencetype, presencedesc) 
     141                gajim.interface.roster.send_status(self.connection.name, presencetype, 
     142                        presencedesc) 
    137143 
    138144                return False    # finish the session 
     145 
     146def find_current_groupchats(account): 
     147        import message_control 
     148        rooms = [] 
     149        for gc_control in gajim.interface.msg_win_mgr.get_controls( 
     150        message_control.TYPE_GC): 
     151                acct = gc_control.account 
     152                # check if account is the good one 
     153                if acct != account: 
     154                        continue 
     155                room_jid = gc_control.room_jid 
     156                nick = gc_control.nick 
     157                if gajim.gc_connected[acct].has_key(room_jid) and \ 
     158                gajim.gc_connected[acct][room_jid]: 
     159                        rooms.append((room_jid, nick,)) 
     160        return rooms 
     161 
     162 
     163class LeaveGroupchatsCommand(AdHocCommand): 
     164        commandnode = 'leave-groupchats' 
     165        commandname = 'Leave Groupchats' 
     166 
     167        @staticmethod 
     168        def isVisibleFor(samejid): 
     169                ''' Change status is visible only if the entity has the same bare jid. ''' 
     170                return samejid 
     171 
     172        def execute(self, request): 
     173                # first query... 
     174                response, cmd = self.buildResponse(request, defaultaction = 'execute', 
     175                        actions=['execute']) 
     176                options = [] 
     177                account = self.connection.name 
     178                for gc in find_current_groupchats(account): 
     179                        options.append((u'%s' %(gc[0]), _('%(nickname)s on %(room_jid)s') % \ 
     180                                {'nickname': gc[1], 'room_jid': gc[0]})) 
     181                if not len(options): 
     182                        response, cmd = self.buildResponse(request, status = 'completed') 
     183                        cmd.addChild('note', {}, _('You have not joined a groupchat.')) 
     184                 
     185                        self.connection.connection.send(response) 
     186                        return False 
     187 
     188                cmd.addChild(node=dataforms.SimpleDataForm( 
     189                        title = _('Leave Groupchats'), 
     190                        instructions = _('Choose the groupchats you want to leave'), 
     191                        fields=[ 
     192                                dataforms.Field('list-multi', 
     193                                        var = 'groupchats', 
     194                                        label = _('Groupchats'), 
     195                                        options = options, 
     196                                        required = True)])) 
     197 
     198                self.connection.connection.send(response) 
     199 
     200                # for next invocation 
     201                self.execute = self.leavegroupchats 
     202                 
     203                return True     # keep the session 
     204 
     205        def leavegroupchats(self, request): 
     206                # check if the data is correct 
     207                try: 
     208                        form = dataforms.SimpleDataForm(extend = request.getTag('command').\ 
     209                                getTag('x')) 
     210                except: 
     211                        self.badRequest(request) 
     212                        return False 
     213 
     214                try: 
     215                        gc = form['groupchats'].values 
     216                except: # KeyError if there's no presence-type field in form or 
     217                        # AttributeError if that field is of wrong type 
     218                        self.badRequest(request) 
     219                        return False 
     220                account = self.connection.name 
     221                try: 
     222                        for room_jid in gc: 
     223                                gc_control = gajim.interface.msg_win_mgr.get_control(room_jid, 
     224                                        account) 
     225                                gc_control.parent_win.remove_tab(gc_control, None, force = True) 
     226                except: # KeyError if there's no presence-type field in form or 
     227                        self.badRequest(request) 
     228                        return False 
     229                response, cmd = self.buildResponse(request, status = 'completed') 
     230                note = _('You leaved the following groupchats:') 
     231                for room_jid in gc: 
     232                        note += '\n\t' + room_jid 
     233                cmd.addChild('note', {}, note) 
     234 
     235                self.connection.connection.send(response) 
     236                return False 
     237 
    139238 
    140239class ConnectionCommands: 
     
    143242                # a list of all commands exposed: node -> command class 
    144243                self.__commands = {} 
    145                 for cmdobj in (ChangeStatusCommand,): 
     244                for cmdobj in (ChangeStatusCommand, LeaveGroupchatsCommand): 
    146245                        self.__commands[cmdobj.commandnode] = cmdobj 
    147246 
     
    165264                                q.addChild('item', { 
    166265                                        # TODO: find the jid 
    167                                         'jid': self.getOurBareJID()+u'/'+self.server_resource, 
     266                                        'jid': self.getOurBareJID() + u'/' + self.server_resource, 
    168267                                        'node': node, 
    169268                                        'name': cmd.commandname}) 
     
    210309                        if node not in self.__commands.keys(): 
    211310                                self.connection.send( 
    212                                         xmpp.Error(iq_obj, xmpp.NS_STANZAS+' item-not-found')) 
     311                                        xmpp.Error(iq_obj, xmpp.NS_STANZAS + ' item-not-found')) 
    213312                                raise xmpp.NodeProcessed 
    214313 
     
    221320 
    222321                        # create new instance and run it 
    223                         obj = newcmd(conn=self, jid=jid, sessionid=sessionid) 
     322                        obj = newcmd(conn = self, jid = jid, sessionid = sessionid) 
    224323                        rc = obj.execute(iq_obj) 
    225324                        if rc: 
     
    237336 
    238337                        try: 
    239                                 if action == 'cancel':          rc = obj.cancel(iq_obj) 
    240                                 elif action == 'prev':          rc = obj.prev(iq_obj) 
    241                                 elif action == 'next':          rc = obj.next(iq_obj) 
     338                                if action == 'cancel': 
     339                                        rc = obj.cancel(iq_obj) 
     340                                elif action == 'prev': 
     341                                        rc = obj.prev(iq_obj) 
     342                                elif action == 'next': 
     343                                        rc = obj.next(iq_obj) 
    242344                                elif action == 'execute' or action is None: 
    243                                                                 rc = obj.execute(iq_obj) 
    244                                 elif action == 'complete':      rc = obj.complete(iq_obj) 
     345                                        rc = obj.execute(iq_obj) 
     346                                elif action == 'complete': 
     347                                        rc = obj.complete(iq_obj) 
    245348                                else: 
    246349                                        # action is wrong. stop the session, send error 
     
    257360 
    258361                        raise xmpp.NodeProcessed 
    259