Commits

Josh VanderLinden committed 3a71ffa

Trying to streamline the tray icon

Comments (0)

Files changed (2)

 # Maintainer: Josh VanderLinden <arch@cloudlery.com>
 pkgname=zedify
 pkgver=20130225
-pkgrel=1
+pkgrel=2
 pkgdesc="Deliver message notifications to remote machines"
 arch=('any')
 url="http://bitbucket.org/codekoala/zedify"
+#!/usr/bin/env python2
+
 from collections import defaultdict
+from functools import wraps
+from os.path import abspath, dirname, join
 from threading import Thread, Event
+import sys
 import time
 
 from gi.repository import Notify
 
+from wx.lib.pubsub import pub
 import wx
 import zmq
 
 from zedify.version import __name__
 
 ctx = zmq.Context()
+
+RES_DIR = join(abspath(dirname(__file__)), 'resources')
 ID_ICON_TIMER = wx.NewId()
 
 
+def posix_only(f):
+    @wraps(f)
+    def wrapped(self, msg):
+        if 'win' not in sys.environ:
+            return f(self, msg)
+        else:
+            return None
+
+    return wrapped
+
+
 class ZedifyIcon(wx.TaskBarIcon):
 
     def __init__(self):
         self.total_unseen = 0
         self.blink_state = False
 
-        self.normal_icon = wx.IconFromBitmap(wx.Bitmap('/usr/share/zedify/tray.png'))
-        self.unseen_icon = wx.IconFromBitmap(wx.Bitmap('/usr/share/zedify/tray-unseen.png'))
+        self.normal_icon = get_icon('tray.png')
+        self.unseen_icon = get_icon('tray-unseen.png')
+        self.menu = self.CreatePopupMenu()
+
+        pub.subscribe(self.on_message_received, 'msg')
 
         self.set_tooltip()
 
         wx.EVT_TIMER(self, ID_ICON_TIMER, self.blink)
         self.icon_flash.Start(750)
 
-        self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
-
-        self.snatch = MessageSnatcher(self)
+        self.snatch = MessageSnatcher()
         self.snatch.start()
 
-    def CreatePopupMenu(self):
+    def CreatePopupMenu(self, counts=None):
         menu = wx.Menu()
 
-        create_menu_item(menu, 'Say Hello', self.on_hello)
-        menu.AppendSeparator()
+        if counts:
+            for info in counts.items():
+                create_menu_item(menu, '%s:\t%s' % info, lambda ev: None)
+            menu.AppendSeparator()
+
         create_menu_item(menu, 'Exit', self.on_exit)
 
         return menu
         else:
             self.blink_state = False
 
-    def on_left_down(self, event):
-        pass
-
-    def on_hello(self, event):
-        pass
+    def on_message_received(self, counts):
+        self.set_tooltip(counts)
+        self.CreatePopupMenu(counts)
 
     def on_exit(self, event):
         self.snatch.stop()
 class MessageSnatcher(Thread):
     """Thread that receives and handles messages from zmq"""
 
-    def __init__(self, parent):
+    def __init__(self):
         super(MessageSnatcher, self).__init__()
 
-        self.parent = parent
         self.unseen = defaultdict(int)
         self.last_user = None
         self.last_time = None
         try:
             self.methods = config.zedify.notify.split(',')
         except AttributeError:
-            self.methods = ['libnotify','bell']
+            self.methods = ['libnotify', 'bell']
 
-        self.libnotify_ready = Notify.init(__name__)
         self.event = Event()
+        self.libnotify_ready = Notify.init(__name__)
 
         # connect to zmq
         self.sub = ctx.socket(zmq.SUB)
 
             handler(msg)
 
-            self.last_time = time.time()
+            self.last_time = now
 
-        self.parent.set_tooltip(dict(self.unseen))
+        pub.sendMessage('msg', counts=dict(self.unseen.items()))
 
     def handle_pm(self, msg):
-        user = msg.get('user', None)
-        text = msg.get('msg', '')
+        for method in self.methods:
+            handler = getattr(self, '_handle_%s' % (method,), None)
+            if handler:
+                try:
+                    handler(msg)
+                except:
+                    pass
+
         self.unseen[msg['chat']] += 1
+        self.last_user = msg.get('user', None)
 
-        try:
-            if 'libnotify' in self.methods and self.libnotify_ready:
-                Notify.Notification.new(user, text, 'dialog-information').show()
-        except:
-            pass
+    @posix_only
+    def _handle_libnotify(self, msg):
+        if self.libnotify_ready:
+            Notify.Notification.new(msg['user'], msg['msg'],
+                                    'dialog-information').show()
 
-        try:
-            if 'espeak' in self.methods:
-                from espeak import espeak
+    @posix_only
+    def _handle_espeak(self, msg):
+        from espeak import espeak
 
-                if self.last_user != user:
-                    status = '%s said, "%s"' % (user, text)
-                else:
-                    status = text
+        text = msg['msg']
+        if self.last_user != msg['user']:
+            status = '%s said, "%s"' % (msg['user'], text)
+        else:
+            status = text
 
-                espeak.synth(msg['msg'])
-        except:
-            pass
+        espeak.synth(status)
 
-        try:
-            if 'tmux' in self.methods:
-                from subprocess import call
-                status = '%s sent you a message' % (user,)
-                call(['/usr/bin/tmux', 'display-message', status])
-        except:
-            pass
+    @posix_only
+    def _handle_bell(self, msg):
+        from subprocess import Popen
+        Popen(['/usr/bin/aplay', '-q', join(RES_DIR, 'receive.wav')])
 
-        try:
-            if 'bell' in self.methods:
-                from subprocess import Popen
-                Popen(['/usr/bin/aplay', '-q', '/usr/share/zedify/receive.wav'])
-        except:
-            pass
+    @posix_only
+    def _handle_tmux(self, msg):
+        from subprocess import call
 
-        self.last_user = user
+        status = '%s sent you a message' % (msg['user'],)
+        call(['/usr/bin/tmux', 'display-message', status])
 
     def handle_mention(self, msg):
         return self.handle_pm(msg)
 
         self.unseen[msg['in_buffer']] = 0
 
+    def handle_buffer_switch(self, msg):
+        self.unseen[msg['to_buffer']] = 0
+
+    handle_window_switch = handle_buffer_switch
+
     def stop(self):
         self.event.set()
 
     return item
 
 
+def get_icon(path):
+    return wx.IconFromBitmap(wx.Bitmap(join(RES_DIR, path)))
+
+
 if __name__ == '__main__':
     main()