Skip to content

Instantly share code, notes, and snippets.

@fwarren
Last active August 25, 2017 23:49
Show Gist options
  • Save fwarren/e1b1a0b072c2365447d93d4850362c66 to your computer and use it in GitHub Desktop.
Save fwarren/e1b1a0b072c2365447d93d4850362c66 to your computer and use it in GitHub Desktop.
Python Finch dbus message notifier
#!/usr/bin/python2
#
# simple d-busified evolution mail notifier
#
# notifies you about incoming mail with a sound and
# displays a tray icon in the notification area until
# you read some message in evolution again
#
# Thomas Perl <[email protected]> 2007-03-28
#
# See http://thpinfo.com/2007/hacks/ for updates
#
# Changes:
#
# 2007-04-20
# + Better unread tracking
# + new_mail_folders handling
# + Use pynotify for neat notifications
# + Customizable wave sounds
#
# 2007-08-29
# + Add wave file playing limit
#
# 2007-09-13
# + Add support for getting X11 idle time
# -> skip sound playback when idle
# + Add support for getting X11 focused window class
# -> skip notification display when evolution is focused
#
# http://www.pygtk.org/pygtk2reference/
import dbus
import dbus.glib
import dbus.decorators
import pynotify
import gobject
import gtk
import os
import os.path
import glob
import random
import time
import sys
pynotify.init('imrecvd')
notification = None
flash_icon_timer = 0
bus = dbus.SessionBus()
icon = gtk.StatusIcon()
icon.set_visible( True)
icon.set_from_file(os.path.join(os.path.expanduser('~'),'.local/share/icons/24x24/status/user-invisible.png'))
icon.set_tooltip( 'No new messages')
def update_tooltip(show_notification = True):
global notification, icon, flash_icon_timer
message = 'You have new IM messages'
notification = pynotify.Notification( 'New IM received!', message)
icon.set_from_file(os.path.join(os.path.expanduser('~'),'.local/share/icons/24x24/status/user-available.png'))
icon.set_blinking(True)
icon.set_tooltip( 'New messages received')
if flash_icon_timer != 0:
gobject.source_remove(flash_icon_timer)
flash_icon_timer = 0
flash_icon_timer = gobject.timeout_add(5000, flash_icon)
return False
def flash_icon():
global flash_icon_timer, icon
gobject.source_remove(flash_icon_timer)
flash_icon_timer = 0
icon.set_blinking(False)
# fired when new IM comes in via dBus
def message_received(account, sender, message, conversation, flags):
global notification
gobject.idle_add(update_tooltip)
# fired when new IM is sent
def message_sent(account, message, id):
global icon
activate(icon);
# stop flashing or indication that a message has been received
def activate(icon):
global notification
if icon.get_tooltip_text() == 'New messages received':
icon.set_blinking(False)
icon.set_tooltip('No new messages')
icon.set_from_icon_name( 'user-invisible')
def popup( data, event_button, event_time):
create_context_menu(event_button, event_time);
def create_context_menu(event_button, event_time, data=None):
context_menu = gtk.Menu()
about_item = gtk.MenuItem( "About")
about_item.connect( "activate", item_about)
about_item.show()
exit_item = gtk.MenuItem( "Exit")
exit_item.connect( "activate", item_exit)
exit_item.show()
sep = gtk.SeparatorMenuItem()
sep.show()
context_menu.append( about_item)
context_menu.append( sep)
context_menu.append( exit_item)
context_menu.popup(None, None, None, event_button, event_time)
def item_about(data=None):
about = gtk.AboutDialog()
about.set_program_name("Finch Indicator")
about.set_version("0.1")
about.set_copyright("(c) Fred Warren")
about.set_comments("Taskbar indicator for Finch Messenger")
about.set_website("http://www.elder-geek.net")
about.set_logo(gtk.gdk.pixbuf_new_from_file(os.path.dirname(os.path.realpath(__file__)) + "/fww.png"))
about.run()
about.destroy()
def item_exit(data=None):
loop.quit()
icon.connect("activate", activate)
icon.connect("popup_menu", popup)
bus.add_signal_receiver( message_received, dbus_interface="im.pidgin.purple.PurpleInterface", signal_name="ReceivedImMsg")
bus.add_signal_receiver( message_received, dbus_interface="im.pidgin.purple.PurpleInterface", signal_name="ReceivedChatMsg")
bus.add_signal_receiver( message_sent, dbus_interface="im.pidgin.purple.PurpleInterface", signal_name="SendingImMsg")
bus.add_signal_receiver( message_sent, dbus_interface="im.pidgin.purple.PurpleInterface", signal_name="SendingChatMsg")
loop = gobject.MainLoop()
loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment