Changeset 9306

Show
Ignore:
Timestamp:
02/05/08 23:13:16 (10 months ago)
Author:
asterix
Message:

Prevent traceback when gajim-remote is used while gajim is not running. Add a check_gajim_running command to gajim-remote

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/gajim-remote.py

    r8972 r9306  
    6060 
    6161        def __init__(self): 
    62                 self.argv_len = len(sys.argv)  
     62                self.argv_len = len(sys.argv) 
    6363                # define commands dict. Prototype : 
    6464                # { 
     
    7474                                        [ 
    7575                                                #User gets help for the command, specified by this parameter 
    76                                                 (_('command'),  
     76                                                (_('command'), 
    7777                                                _('show help on command'), False) 
    7878                                        ] 
    79                                 ],  
     79                                ], 
    8080                        'toggle_roster_appearance' : [ 
    8181                                        _('Shows or hides the roster window'), 
    8282                                        [] 
    83                                 ],  
     83                                ], 
    8484                        'show_next_pending_event': [ 
    8585                                        _('Pops up a window with the next pending event'), 
     
    9494                                        ] 
    9595 
    96                                 ],       
     96                                ], 
    9797                        'list_accounts': [ 
    9898                                        _('Prints a list of registered accounts'), 
    9999                                        [] 
    100                                 ],  
     100                                ], 
    101101                        'change_status': [ 
    102102                                        _('Changes the status of account or accounts'), 
    103103                                        [ 
    104104#offline, online, chat, away, xa, dnd, invisible should not be translated 
    105                                                 (_('status'), _('one of: offline, online, chat, away, xa, dnd, invisible '), True),  
    106                                                 (_('message'), _('status message'), False),  
     105                                                (_('status'), _('one of: offline, online, chat, away, xa, dnd, invisible '), True), 
     106                                                (_('message'), _('status message'), False), 
    107107                                                (_('account'), _('change status of account "account". ' 
    108108                'If not specified, try to change status of all accounts that have ' 
     
    110110                                        ] 
    111111                                ], 
    112                         'open_chat': [  
    113                                         _('Shows the chat dialog so that you can send messages to a contact'),  
     112                        'open_chat': [ 
     113                                        _('Shows the chat dialog so that you can send messages to a contact'), 
    114114                                        [ 
    115115                                                ('jid', _('JID of the contact that you want to chat with'), 
    116                                                         True),  
     116                                                        True), 
    117117                                                (_('account'), _('if specified, contact is taken from the ' 
    118118                                                'contact list of this account'), False) 
     
    122122                                        _('Sends new chat message to a contact in the roster. Both OpenPGP key ' 
    123123                                        'and account are optional. If you want to set only \'account\', ' 
    124                                         'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),  
     124                                        'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'), 
    125125                                        [ 
    126126                                                ('jid', _('JID of the contact that will receive the message'), True), 
     
    135135                                        _('Sends new single message to a contact in the roster. Both OpenPGP key ' 
    136136                                        'and account are optional. If you want to set only \'account\', ' 
    137                                         'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'),  
     137                                        'without \'OpenPGP key\', just set \'OpenPGP key\' to \'\'.'), 
    138138                                        [ 
    139139                                                ('jid', _('JID of the contact that will receive the message'), True), 
     
    145145                                                        'using this account'), False), 
    146146                                        ] 
    147                                 ],  
     147                                ], 
    148148                        'send_groupchat_message':[ 
    149                                         _('Sends new message to a groupchat you\'ve joined.'),  
     149                                        _('Sends new message to a groupchat you\'ve joined.'), 
    150150                                        [ 
    151151                                                ('room_jid', _('JID of the room that will receive the message'), True), 
     
    156156                                ], 
    157157                        'contact_info': [ 
    158                                         _('Gets detailed info on a contact'),  
     158                                        _('Gets detailed info on a contact'), 
    159159                                        [ 
    160160                                                ('jid', _('JID of the contact'), True) 
     
    162162                                ], 
    163163                        'account_info': [ 
    164                                         _('Gets detailed info on a account'),  
     164                                        _('Gets detailed info on a account'), 
    165165                                        [ 
    166166                                                ('account', _('Name of the account'), True) 
     
    189189                        'prefs_del': [ 
    190190                                        _('Deletes a preference item'), 
    191                                         [  
    192                                                 (_('key'), _('name of the preference to be deleted'), True)  
     191                                        [ 
     192                                                (_('key'), _('name of the preference to be deleted'), True) 
    193193                                        ] 
    194194                                ], 
     
    200200                        'remove_contact': [ 
    201201                                        _('Removes contact from roster'), 
    202                                         [  
     202                                        [ 
    203203                                                ('jid', _('JID of the contact'), True), 
    204204                                                (_('account'), _('if specified, contact is taken from the ' 
     
    209209                        'add_contact': [ 
    210210                                        _('Adds contact to roster'), 
    211                                         [  
     211                                        [ 
    212212                                                (_('jid'), _('JID of the contact'), True), 
    213213                                                (_('account'), _('Adds new contact to this account'), False) 
     
    227227                                                (_('account'), _(''), False) 
    228228                                        ] 
    229                                 ],                               
     229                                ], 
    230230 
    231231                        'get_unread_msgs_number': [ 
     
    240240                                ], 
    241241                        'send_xml': [ 
    242                                         _('Sends custom XML'),  
     242                                        _('Sends custom XML'), 
    243243                                        [ 
    244244                                                ('xml', _('XML to send'), True), 
     
    264264                                        ] 
    265265                                ], 
     266                        'check_gajim_running':[ 
     267                                        _('Check if Gajim is running'), 
     268                                        [] 
     269                                ], 
    266270 
    267271                        } 
     272                self.sbus = None 
    268273                if self.argv_len  < 2 or \ 
    269274                        sys.argv[1] not in self.commands.keys(): # no args or bad args 
     
    278283                if self.command == 'handle_uri': 
    279284                        self.handle_uri() 
     285                if self.command == 'check_gajim_running': 
     286                        print self.check_gajim_running() 
     287                        sys.exit(0) 
    280288                self.init_connection() 
    281289                self.check_arguments() 
     
    324332                                for pref_key in pref_keys: 
    325333                                        result = '%s = %s' % (pref_key, res[pref_key]) 
    326                                         if isinstance(result, unicode):  
     334                                        if isinstance(result, unicode): 
    327335                                                print result.encode(PREFERRED_ENCODING) 
    328336                                        else: 
     
    333341                                print unicode(res).encode(PREFERRED_ENCODING) 
    334342 
     343        def check_gajim_running(self): 
     344                if not self.sbus: 
     345                        try: 
     346                                self.sbus = dbus.SessionBus() 
     347                        except: 
     348                                raise exceptions.SessionBusNotPresent 
     349 
     350                test = False 
     351                if hasattr(self.sbus, 'name_has_owner'): 
     352                        if self.sbus.name_has_owner(SERVICE): 
     353                                test = True 
     354                elif dbus.dbus_bindings.bus_name_has_owner(self.sbus.get_connection(), 
     355                SERVICE): 
     356                        test = True 
     357                return test 
     358 
    335359        def init_connection(self): 
    336360                ''' create the onnection to the session dbus, 
     
    341365                        raise exceptions.SessionBusNotPresent 
    342366 
     367                if not self.check_gajim_running(): 
     368                        send_error(_('It seems Gajim is not running. So you can\'t use gajim-remote.')) 
    343369                obj = self.sbus.get_object(SERVICE, OBJ_PATH) 
    344370                interface = dbus.Interface(obj, INTERFACE) 
     
    369395                        command_props = self.commands[command] 
    370396                        arguments_str = self.make_arguments_row(command_props[1]) 
    371                         str = _('Usage: %s %s %s \n\t %s') % (BASENAME, command,  
     397                        str = _('Usage: %s %s %s \n\t %s') % (BASENAME, command, 
    372398                                        arguments_str, command_props[0]) 
    373399                        if len(command_props[1]) > 0: 
     
    384410                commands.sort() 
    385411                for command in commands: 
    386                         str += '  ' + command  
     412                        str += '  ' + command 
    387413                        for argument in self.commands[command][1]: 
    388414                                str += ' ' 
     
    458484                        if args[argv_len][2]: 
    459485                                send_error(_('Argument "%s" is not specified. \n' 
    460                                         'Type "%s help %s" for more info') %  
     486                                        'Type "%s help %s" for more info') % 
    461487                                        (args[argv_len][0], BASENAME, self.command)) 
    462488                self.arguments = [] 
     
    486512                        self.command = sys.argv[1] = 'join_room' 
    487513                        return 
    488                          
     514 
    489515                sys.exit(0) 
    490516