Changeset 6677

Show
Ignore:
Timestamp:
08/27/06 00:51:33 (2 years ago)
Author:
asterix
Message:

handle advanced notification for new_message event only (without systray / roster / urgency hint that require a event refactorizatin)
use subprocess module as we depend on python2.4

Location:
trunk/src
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/common/helpers.py

    r6553 r6677  
    1919import sre 
    2020import os 
     21import subprocess 
    2122import urllib 
    2223import errno 
     
    361362                return is_in_dir 
    362363 
     364def exec_command(command): 
     365        '''command is a string that contain arguments''' 
     366#       os.system(command) 
     367        subprocess.Popen(command.split()) 
     368 
    363369def launch_browser_mailer(kind, uri): 
    364370        #kind = 'url' or 'mail' 
     
    384390                        if command == '': # if no app is configured 
    385391                                return 
    386                 # we add the uri in "" so we have good parsing from shell 
    387                 uri = uri.replace('"', '\\"') # escape " 
    388                 command = command + ' "' + uri + '" &' 
    389                 try: #FIXME: when we require python2.4+ use subprocess module 
    390                         os.system(command) 
     392                command = command + ' ' + uri 
     393                try: 
     394                        exec_command(command) 
    391395                except: 
    392396                        pass 
     
    407411                if command == '': # if no app is configured 
    408412                        return 
    409                 # we add the path in "" so we have good parsing from shell 
    410                 path_to_open = path_to_open.replace('"', '\\"') # escape " 
    411                 command = command + ' "' + path_to_open + '" &' 
    412                 try: #FIXME: when we require python2.4+ use subprocess module 
    413                         os.system(command) 
     413                command = command + ' ' + path_to_open 
     414                try: 
     415                        exec_command(command) 
    414416                except: 
    415417                        pass 
     
    437439                        return 
    438440                player = gajim.config.get('soundplayer') 
    439                 # we add the path in "" so we have good parsing from shell 
    440                 path_to_soundfile = path_to_soundfile.replace('"', '\\"') # escape " 
    441                 command = player + ' "' + path_to_soundfile + '" &' 
    442                 #FIXME: when we require 2.4+ use subprocess module 
    443                 os.system(command) 
     441                command = player + ' ' + path_to_soundfile 
     442                exec_command(command) 
    444443 
    445444def get_file_path_from_dnd_dropped_uri(uri): 
     
    728727        return filename 
    729728 
    730 def allow_showing_notification(account): 
     729def allow_showing_notification(account, type = None, advanced_notif_num = None): 
    731730        '''is it allowed to show nofication? 
    732         check OUR status and if we allow notifications for that status''' 
     731        check OUR status and if we allow notifications for that status 
     732        type is the option that need to be True ex: notify_on_signin''' 
     733        if advanced_notif_num != None: 
     734                popup = gajim.config.get_per('notifications', str(advanced_notif_num), 
     735                        'popup') 
     736                if popup == 'yes': 
     737                        return True 
     738                if popup == 'no': 
     739                        return False 
     740        if type and not gajim.config.get(type): 
     741                return False 
    733742        if gajim.config.get('autopopupaway'): # always show notification 
    734743                return True 
     
    737746        return False 
    738747 
    739 def allow_popup_window(account): 
     748def allow_popup_window(account, advanced_notif_num = None): 
    740749        '''is it allowed to popup windows?''' 
     750        if advanced_notif_num != None: 
     751                popup = gajim.config.get_per('notifications', str(advanced_notif_num), 
     752                        'auto_open') 
     753                if popup == 'yes': 
     754                        return True 
     755                if popup == 'no': 
     756                        return False 
    741757        autopopup = gajim.config.get('autopopup') 
    742758        autopopupaway = gajim.config.get('autopopupaway') 
     
    745761                return True 
    746762        return False 
     763 
     764def allow_sound_notification(sound_event, advanced_notif_num = None): 
     765        if advanced_notif_num != None: 
     766                sound = gajim.config.get_per('notifications', str(advanced_notif_num), 
     767                        'sound') 
     768                if sound == 'yes': 
     769                        return True 
     770                if sound == 'no': 
     771                        return False 
     772        if gajim.config.get_per('soundevents', sound_event, 'enabled'): 
     773                return True 
     774        return False 
  • trunk/src/gajim.py

    r6660 r6677  
    558558                        return 
    559559 
     560                advanced_notif_num = notify.get_advanced_notification('message_received', 
     561                        account, contact) 
     562 
    560563                # Is it a first or next message received ? 
    561564                first = False 
     
    572575                        # array: (jid, msg, time, encrypted, msg_type, subject) 
    573576                        self.roster.on_message(jid, message, array[2], account, array[3], 
    574                                 msg_type, subject, resource, msg_id, array[9]) 
     577                                msg_type, subject, resource, msg_id, array[9], advanced_notif_num) 
    575578                        nickname = gajim.get_name_from_jid(account, jid) 
    576579                # Check and do wanted notifications      
     
    579582                        msg = _('Subject: %s') % subject + '\n' + msg 
    580583                notify.notify('new_message', jid, account, [msg_type, first, nickname, 
    581                         msg]) 
     584                        msg], advanced_notif_num) 
    582585 
    583586                if self.remote_ctrl: 
     
    931934                        array[3])) 
    932935 
    933                 if gajim.config.get('notify_on_new_message') and \ 
    934                 helpers.allow_showing_notification(account): 
     936                if helpers.allow_showing_notification(account, 'notify_on_new_message'): 
    935937                        path = os.path.join(gajim.DATA_DIR, 'pixmaps', 'events', 
    936938                                'gc_invitation.png') 
  • trunk/src/notify.py

    r6554 r6677  
    3333                import dbus.service 
    3434 
    35 def notify(event, jid, account, parameters): 
     35def get_advanced_notification(event, account, contact): 
     36        num = 0 
     37        notif = gajim.config.get_per('notifications', str(num)) 
     38        while notif: 
     39                recipient_ok = False 
     40                status_ok = False 
     41                tab_opened_ok = False 
     42                # test event 
     43                if gajim.config.get_per('notifications', str(num), 'event') == event: 
     44                        # test recipient 
     45                        recipient_type = gajim.config.get_per('notifications', str(num), 
     46                                'recipient_type') 
     47                        recipients = gajim.config.get_per('notifications', str(num), 
     48                                'recipients').split() 
     49                        if recipient_type == 'all': 
     50                                recipient_ok = True 
     51                        elif recipient_type == 'contact' and contact.jid in recipients: 
     52                                recipient_ok = True 
     53                        elif recipient_type == 'group': 
     54                                for group in contact.groups: 
     55                                        if group in contact.groups: 
     56                                                recipient_ok = True 
     57                                                break 
     58                if recipient_ok: 
     59                        # test status 
     60                        our_status = gajim.SHOW_LIST[gajim.connections[account].connected] 
     61                        status = gajim.config.get_per('notifications', str(num), 'status') 
     62                        if status == 'all' or our_status in status.split(): 
     63                                status_ok = True 
     64                if status_ok: 
     65                        # test window_opened 
     66                        tab_opened = gajim.config.get_per('notifications', str(num), 
     67                                'tab_opened') 
     68                        if tab_opened == 'both': 
     69                                tab_opened_ok = True 
     70                        else: 
     71                                chat_control = False 
     72                                full_jid_with_resource = contact.jid 
     73                                if contact.resource: 
     74                                        full_jid_with_resource += '/' + contact.resource 
     75                                highest_contact = gajim.contacts.get_contact_with_highest_priority( 
     76                                        account, contact.jid) 
     77                                # Look for a chat control that has the given resource, or default to 
     78                                # one without resource 
     79                                if gajim.interface.msg_win_mgr.get_control(full_jid_with_resource, 
     80                                account): 
     81                                        chat_control = True 
     82                                elif not highest_contact or not highest_contact.resource: 
     83                                        # unknow contact or offline message 
     84                                        if gajim.interface.msg_win_mgr.get_control(contact.jid, account): 
     85                                                chat_control = True 
     86                                elif highest_contact and contact.resource != \ 
     87                                highest_contact.resource: 
     88                                        chat_control = False 
     89                                elif gajim.interface.msg_win_mgr.get_control(contact.jid, account): 
     90                                        chat_control = True 
     91                                if (chat_control and tab_opened == 'yes') or (not chat_control and \ 
     92                                tab_opened == 'no'): 
     93                                        tab_opened_ok = True 
     94                if tab_opened_ok: 
     95                        return num 
     96 
     97                num += 1 
     98                notif = gajim.config.get_per('notifications', str(num)) 
     99 
     100def notify(event, jid, account, parameters, advanced_notif_num = None): 
    36101        '''Check what type of notifications we want, depending on basic configuration 
    37102        of notifications and advanced one and do these notifications''' 
     
    39104        do_popup = False 
    40105        do_sound = False 
     106        do_cmd = False 
    41107        if (event == 'status_change'): 
    42108                new_show = parameters[0] 
     
    52118                gajim.block_signed_in_notifications[account_server]: 
    53119                        block_transport = True 
    54                 if gajim.config.get('notify_on_signin') and \ 
    55                 not gajim.block_signed_in_notifications[account] and not block_transport \ 
    56                 and helpers.allow_showing_notification(account): 
     120                if helpers.allow_showing_notification(account, 'notify_on_signin') and \ 
     121                not gajim.block_signed_in_notifications[account] and not block_transport: 
    57122                        do_popup = True 
    58123                if gajim.config.get_per('soundevents', 'contact_connected', 
     
    62127        elif (event == 'contact_disconnected'): 
    63128                status_message = parameters 
    64                 if gajim.config.get('notify_on_signout') \ 
    65                         and helpers.allow_showing_notification(account): 
     129                if helpers.allow_showing_notification(account, 'notify_on_signout'): 
    66130                        do_popup = True 
    67131                if gajim.config.get_per('soundevents', 'contact_disconnected', 
     
    73137                nickname = parameters[2] 
    74138                message = parameters[3] 
    75                 if gajim.config.get('notify_on_new_message') and \ 
    76                         helpers.allow_showing_notification(account) and first: 
     139                if helpers.allow_showing_notification(account, 'notify_on_new_message', 
     140                advanced_notif_num) and first: 
    77141                        do_popup = True 
    78                 if first and gajim.config.get_per('soundevents', 'first_message_received', 
    79                         'enabled'): 
     142                if first and helpers.allow_sound_notification('first_message_received', 
     143                advanced_notif_num): 
    80144                        do_sound = True 
    81                 elif not first and gajim.config.get_per('soundevents', 'next_message_received', 
    82                         'enabled'): 
     145                elif not first and helpers.allow_sound_notification( 
     146                'next_message_received', advanced_notif_num): 
    83147                        do_sound = True 
    84148        else: 
    85149                print '*Event not implemeted yet*' 
     150 
     151        if advanced_notif_num != None and gajim.config.get_per('notifications', 
     152        str(advanced_notif_num), 'run_command'): 
     153                do_cmd = True 
    86154                         
    87155        # Do the wanted notifications    
     
    162230 
    163231        if (do_sound): 
     232                snd_file = None 
     233                snd_event = None # If not snd_file, play the event 
    164234                if (event == 'new_message'): 
    165                         if first: 
    166                                  helpers.play_sound('first_message_received') 
     235                        if advanced_notif_num != None and gajim.config.get_per('notifications', 
     236                        str(advanced_notif_num), 'sound') == 'yes': 
     237                                snd_file = gajim.config.get_per('notifications', 
     238                                        str(advanced_notif_num), 'sound_file') 
     239                        elif advanced_notif_num != None and gajim.config.get_per( 
     240                        'notifications', str(advanced_notif_num), 'sound') == 'no': 
     241                                pass # do not set snd_event 
     242                        elif first: 
     243                                snd_event = 'first_message_received' 
    167244                        else: 
    168                                  helpers.play_sound('next_message_received') 
     245                                snd_event = 'next_message_received' 
    169246                elif event in ('contact_connected', 'contact_disconnected'): 
    170                         helpers.play_sound(event) 
    171           
     247                        snd_event = event 
     248                if snd_file: 
     249                        helpers.play_sound_file(snd_file) 
     250                if snd_event: 
     251                        helpers.play_sound(snd_event) 
     252 
     253        if do_cmd: 
     254                command = gajim.config.get_per('notifications', str(advanced_notif_num), 
     255                        'command') 
     256                try: 
     257                        helpers.exec_command(command) 
     258                except: 
     259                        pass 
    172260 
    173261def popup(event_type, jid, account, msg_type = '', path_to_image = None, 
  • trunk/src/roster_window.py

    r6676 r6677  
    24402440        def on_message(self, jid, msg, tim, account, encrypted = False, 
    24412441                        msg_type = '', subject = None, resource = '', msg_id = None, 
    2442                         user_nick = ''): 
     2442                        user_nick = '', advanced_notif_num = None): 
    24432443                '''when we receive a message''' 
    24442444                contact = None 
     
    24972497                        no_queue = False 
    24982498 
    2499                 popup = helpers.allow_popup_window(account) 
     2499                popup = helpers.allow_popup_window(account, advanced_notif_num) 
    25002500 
    25012501                if msg_type == 'normal' and popup: # it's single message to be autopopuped 
     
    25552555                        self.tree.set_cursor(path) 
    25562556                if gajim.interface.systray_capabilities: 
    2557                         gajim.interface.systray.add_jid(fjid, account, kind) 
     2557                        gajim.interface.systray.add_jid(fjid, account, kind, advanced_notif_num) 
    25582558 
    25592559        def on_preferences_menuitem_activate(self, widget): 
  • trunk/src/systray.py

    r6612 r6677  
    5959                self.popup_menus = [] 
    6060 
    61         def set_img(self): 
     61        def set_img(self, advanced_notif_num = None): 
    6262                if not gajim.interface.systray_enabled: 
    6363                        return 
     64                if advanced_notif_num: 
     65                        if gajim.config.get_per('notifications', str(advanced_notif_num), 
     66                        'systray') == 'no': 
     67                                return 
    6468                if len(self.jids) > 0: 
    6569                        state = 'message' 
     
    7276                        self.img_tray.set_from_pixbuf(image.get_pixbuf()) 
    7377 
    74         def add_jid(self, jid, account, typ): 
     78        def add_jid(self, jid, account, typ, advanced_notif_num = None): 
    7579                l = [account, jid, typ] 
    7680                # We can keep several single message 'cause we open them one by one 
    7781                if not l in self.jids or typ == 'normal': 
    7882                        self.jids.append(l) 
    79                         self.set_img() 
     83                        self.set_img(advanced_notif_num) 
    8084 
    8185        def remove_jid(self, jid, account, typ):