fwarren

Tray Icon for Finch

Aug 16th, 2016
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.76 KB | None | 0 0
  1. #!/usr/bin/python2
  2. #
  3. # simple d-busified evolution mail notifier
  4. #
  5. # notifies you about incoming mail with a sound and
  6. # displays a tray icon in the notification area until
  7. # you read some message in evolution again
  8. #
  9. # Thomas Perl <[email protected]> 2007-03-28
  10. #
  11. # See http://thpinfo.com/2007/hacks/ for updates
  12. #
  13. # Changes:
  14. #
  15. # 2007-04-20
  16. #   + Better unread tracking
  17. #   + new_mail_folders handling
  18. #   + Use pynotify for neat notifications
  19. #   + Customizable wave sounds
  20. #
  21. # 2007-08-29
  22. #   + Add wave file playing limit
  23. #
  24. # 2007-09-13
  25. #   + Add support for getting X11 idle time
  26. #     -> skip sound playback when idle
  27. #   + Add support for getting X11 focused window class
  28. #     -> skip notification display when evolution is focused
  29. #
  30. # http://www.pygtk.org/pygtk2reference/
  31.  
  32. import dbus
  33. import dbus.glib
  34. import dbus.decorators
  35. import pynotify
  36. import gobject
  37. import gtk
  38. import os
  39. import os.path
  40. import glob
  41. import random
  42. import time
  43. import sys
  44.  
  45.  
  46. pynotify.init('purple-imrecvd')
  47. notification = None
  48. flash_icon_timer = 0
  49.  
  50. bus = dbus.SessionBus()
  51.  
  52. icon = gtk.StatusIcon()
  53. icon.set_visible( True)
  54. icon.set_from_icon_name( 'user-invisible')
  55. icon.set_tooltip( 'No new messages')
  56.  
  57.  
  58. def update_tooltip(show_notification = True):
  59.     global notification, icon, flash_icon_timer
  60.     message = 'You have new IM messages'
  61.     notification = pynotify.Notification( 'New IM received!', message)
  62.     icon.set_from_icon_name('user-available')
  63.     icon.set_blinking(True)
  64.     icon.set_tooltip( 'New messages received')
  65.     if flash_icon_timer != 0:
  66.         gobject.source_remove(flash_icon_timer)
  67.         flash_icon_timer = 0
  68.     flash_icon_timer = gobject.timeout_add(5000, flash_icon)
  69.     return False
  70.  
  71. def flash_icon():
  72.     global flash_icon_timer, icon
  73.     gobject.source_remove(flash_icon_timer)
  74.     flash_icon_timer = 0
  75.     icon.set_blinking(False)
  76.  
  77. # fired when new IM comes in via dBus
  78. def message_received(account, sender, message, conversation, flags):
  79.     global notification
  80.     gobject.idle_add(update_tooltip)
  81.  
  82. # fired when new IM is sent
  83. def message_sent(account, message, id):
  84.     global icon
  85.     activate(icon);
  86.  
  87. def newmail_read(): # this code never run
  88.     global notification, icon
  89.     update_tooltip()
  90.     gobject.idle_add( update_tooltip, False)
  91.  
  92. # stop flashing or indication that a message has been received
  93. def activate(icon):
  94.     global notification
  95.     if icon.get_tooltip_text() == 'New messages received':
  96.         icon.set_blinking(False)
  97.         icon.set_tooltip('No new messages')
  98.         icon.set_from_icon_name( 'user-invisible')
  99.  
  100. def destroyer(dialogobj, response_id, data=None):
  101.     global loop, dialog
  102.     if response_id == gtk.RESPONSE_OK:
  103.         loop.quit()
  104.     else:
  105.         dialog.hide()
  106.  
  107. def popup(button, widget, data=None):
  108.     global icon, dialog
  109.     dialog = gtk.MessageDialog(
  110.              parent = None,
  111.              flags = gtk.DIALOG_DESTROY_WITH_PARENT,
  112.              type = gtk.MESSAGE_INFO,
  113.              buttons = gtk.BUTTONS_OK_CANCEL,
  114.              message_format = "Do you want to Exit?")
  115.     dialog.set_title('Popup Window')
  116.     dialog.connect('response', destroyer)
  117.     dialog.show()
  118.  
  119. icon.connect("activate", activate)
  120. icon.connect("popup_menu", popup)
  121.  
  122. bus.add_signal_receiver( message_received, dbus_interface="im.pidgin.purple.PurpleInterface", signal_name="ReceivedImMsg")
  123. bus.add_signal_receiver( message_received, dbus_interface="im.pidgin.purple.PurpleInterface", signal_name="ReceivedChatMsg")
  124. bus.add_signal_receiver( message_sent, dbus_interface="im.pidgin.purple.PurpleInterface", signal_name="SendingImMsg")
  125. bus.add_signal_receiver( message_sent, dbus_interface="im.pidgin.purple.PurpleInterface", signal_name="SendingChatMsg")
  126. loop = gobject.MainLoop()
  127. loop.run()
Advertisement
Add Comment
Please, Sign In to add comment