Changeset 4503

Show
Ignore:
Timestamp:
04/14/09 11:47:09 (4 years ago)
Author:
nopper
Message:

Adding win32 statusicon and ballons implementation trough win32 apis

Location:
branch/UmitPlugins/source-plugins/tray-icon
Files:
3 added
2 modified

Legend:

Unmodified
Added
Removed
  • branch/UmitPlugins/source-plugins/tray-icon/setup.py

    r4364 r4503  
    2323setup( 
    2424    name='Tray Icon', 
    25     version='1.0', 
     25    version='2.0', 
    2626    author='Francesco Piccinno', 
    27     url='http://snippets.pornosecurity.org', 
     27    url='http://blog.archpwn.org', 
    2828    start_file='main', 
    29     provides='=tray-1.0', 
     29    provides='=tray-2.0', 
    3030    description='A simple tray icon for Umit', 
    3131    scripts=['sources/main.py'], 
    3232    data_files=[('data', ['dist/logo.png', 'dist/preferences.xml'])], 
     33    package_dir={'notification' : 'sources/notification'}, 
     34    packages=['notification'], 
     35    license='GPL', 
    3336    output='TrayIcon.ump' 
    3437) 
  • branch/UmitPlugins/source-plugins/tray-icon/sources/main.py

    r4499 r4503  
    3434from umit.plugin.Parser import Parser 
    3535 
     36WIN32_PRESENT = False 
     37 
     38if os.name == 'nt': 
     39    try: 
     40        from notification.win32 import StatusIcon 
     41        WIN32_PRESENT = True 
     42    except ImportError: 
     43        raise Exception('If you are using Windows you need Win32 Extensions package for python to use that plugin.\nDownload a copy from\n\nhttp://starship.python.net/crew/mhammond/win32/\n\nand reenable the plugin.') 
     44 
     45try: 
     46    import pynotify 
     47    NOTIFY_PRESENT = True 
     48except ImportError: 
     49    NOTIFY_PRESENT = False 
     50 
    3651class Preferences(object): 
    3752    def __init__(self): 
     
    7691            self.change_cb(self.parser['notifications']['type'].value) 
    7792 
    78 tray_prefs = Preferences() 
     93if not WIN32_PRESENT: 
     94    tray_prefs = Preferences() 
    7995 
    8096class TrayPlugin(Plugin): 
    8197    TYPE_LIBNOTIFY = 0 
    8298    TYPE_HIGTOOLTIP = 1 
     99    TYPE_WINDOWS = 2 
    83100 
    84101    def start(self, reader): 
     
    86103        self.toggle_visibility = True 
    87104 
     105        self.icon = None 
     106 
    88107        self.menu = gtk.Menu() 
    89108        self.menu.show() 
     
    97116        self.menu.append(item) 
    98117 
    99         logo = os.path.join(Path.icons_dir, 'umit_16.ico') 
    100         log.debug('Creating status icon with %s as logo' % logo) 
    101  
    102         self.icon = gtk.status_icon_new_from_file(logo) 
     118        self.type = self.notifier = None 
     119 
     120        # Force to use win32 module 
     121        if WIN32_PRESENT: 
     122            self.set_type(TrayPlugin.TYPE_WINDOWS) 
     123        else: 
     124            self.set_type(tray_prefs.parser['notifications']['type'].value) 
     125            tray_prefs.parser['notifications']['type'].value = self.type 
     126 
     127            logo = os.path.join(Path.icons_dir, 'umit_16.ico') 
     128            log.debug('Creating status icon with %s as logo' % logo) 
     129 
     130            self.icon = gtk.status_icon_new_from_file(logo) 
     131            tray_prefs.change_cb = self.set_type 
     132 
    103133        self.icon.connect('popup-menu', self.__on_right_click) 
    104134        self.icon.connect('activate', self.__on_activate) 
    105135 
    106         self.type = self.notifier = None 
    107  
    108         self.set_type(tray_prefs.parser['notifications']['type'].value) 
    109         tray_prefs.parser['notifications']['type'].value = self.type 
    110  
    111         tray_prefs.change_cb = self.set_type 
    112  
    113136    def set_type(self, typo): 
    114         if typo == TrayPlugin.TYPE_LIBNOTIFY: 
    115             try: 
    116                 # Try to use libnotify for notifications 
    117                 import pynotify 
    118  
     137        log.debug('called with type = %d' % typo) 
     138 
     139        if typo == TrayPlugin.TYPE_WINDOWS and WIN32_PRESENT: 
     140 
     141            self.icon = StatusIcon() 
     142            self.type = TrayPlugin.TYPE_WINDOWS 
     143 
     144            log.debug('Now notifications will use win32 api') 
     145 
     146        elif typo == TrayPlugin.TYPE_LIBNOTIFY and NOTIFY_PRESENT: 
     147             
     148            if pynotify.init('UMIT'): 
     149                self.notifier = pynotify.Notification 
    119150                self.type = TrayPlugin.TYPE_LIBNOTIFY 
    120151 
    121                 if pynotify.init('UMIT'): 
    122                     self.notifier = pynotify.Notification 
    123                     log.debug('Now notifications will use LibNotify') 
    124                 else: 
    125                     self.set_type(TrayPlugin.TYPE_HIGTOOLTIP) 
    126             except ImportError: 
     152                log.debug('Now notifications will use LibNotify') 
     153            else: 
    127154                self.set_type(TrayPlugin.TYPE_HIGTOOLTIP) 
    128155        else: 
     156 
    129157            self.type = TrayPlugin.TYPE_HIGTOOLTIP 
    130158            self.notifier = HIGTooltip() 
     
    132160            log.debug('Now notifications will use HIGTooltip') 
    133161 
     162    def remove_status_icon(self): 
     163        if not self.icon: 
     164            return 
     165 
     166        if self.type == TrayPlugin.TYPE_WINDOWS: 
     167            self.icon.remove() 
     168        else: 
     169            self.icon.set_visible(False) 
     170            del self.icon 
     171 
     172        self.icon = None 
     173 
    134174    def stop(self): 
    135         self.icon.set_visible(False) 
    136         self.icon = None 
     175        self.remove_status_icon() 
    137176 
    138177    def __on_right_click(self, w, evt, evt_time): 
    139         self.popup_menu.popup(None, None, gtk.status_icon_position_menu, evt, \ 
     178        self.popup_menu.popup(None, None, 
     179            (self.type != TrayPlugin.TYPE_WINDOWS) and \ 
     180                 (gtk.status_icon_position_menu)or (None), evt, \ 
    140181                              evt_time, w) 
    141182 
     
    166207                       (pass None for persistent notification) 
    167208        """ 
    168         rect = self.icon.get_geometry() 
    169  
    170         if rect is not None: 
    171             rect = rect[1] 
    172         else: 
    173             return 
    174  
    175         if self.type == TrayPlugin.TYPE_HIGTOOLTIP: 
     209 
     210        if self.type == TrayPlugin.TYPE_WINDOWS: 
     211            if self.icon.notify_icon: 
     212                title = pango.parse_markup(title)[1] 
     213                msg = pango.parse_markup(msg)[1] 
     214                self.icon.notify_icon.show_balloon(title, msg, 
     215                    (timeout is None) and (0) or (timeout), stock) 
     216 
     217        elif self.type == TrayPlugin.TYPE_HIGTOOLTIP: 
     218            rect = self.icon.get_geometry() 
     219 
     220            if rect is not None: 
     221                rect = rect[1] 
     222            else: 
     223                return 
     224 
    176225            data = HIGTooltipData(msg, title, stock, fname) 
    177226            self.notifier.show_at(self.icon, data, rect.x, rect.y, timeout) 
     
    225274        # Adjusting to the current preferences 
    226275 
    227         btn = (tray_prefs.parser['notifications']['type'].value == 0) and \ 
    228               (self.typenot) or (self.typehig) 
    229         btn.set_active(True) 
    230  
    231         try: 
    232             import pynotify 
    233         except ImportError: 
     276        if tray_prefs.parser['notifications']['type'].value == 0: 
     277            self.typenot.set_active(True) 
     278        else: 
     279            self.typehig.set_active(True) 
     280 
     281        if not NOTIFY_PRESENT: 
    234282            self.typenot.set_sensitive(False) 
    235283            self.typehig.set_active(True) 
     
    254302 
    255303__plugins__ = [TrayPlugin] 
    256 __pref_func__ = show_preferences 
     304 
     305# No preferences if you are using windows 
     306if os.name != 'nt': 
     307    __pref_func__ = show_preferences