Chinaunix首页 | 论坛 | 博客
  • 博客访问: 402042
  • 博文数量: 20
  • 博客积分: 5010
  • 博客等级: 大校
  • 技术积分: 1270
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-16 09:18
文章分类
文章存档

2011年(6)

2010年(2)

2009年(1)

2008年(11)

我的朋友

分类: Python/Ruby

2008-03-29 21:20:03

These days, i am studying the Python and DBus interface of Pidgin. After a few hours , a notification tool was done. The following is the code in Python.
 

#!/usr/bin/env python
#
# This is a Python script for new-msg-notification in Pidgin
# Copyright 2007-2008 (c) Zhang Shunchang <freebsd13@gmail.com>
#


import dbus
import gobject
if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
    import dbus.glib

def strip_ml_tags(in_text):
    """Description: Removes all HTML/XML-like tags from the input text.
    "
""
    # convert in_text to a mutable object (e.g. list)
    s_list = list(in_text)
    i,j = 0,0
    
    while i < len(s_list):
        # iterate until a left-angle bracket is found
        if s_list[i] == '<':
            while s_list[i] != '>':
                # pop everything from the the left-angle bracket until the right-angle bracket
                s_list.pop(i)
                
            # pops the right-angle bracket, too
            s_list.pop(i)
        else:
            i=i+1
            
    # convert the list back into text
    join_char=''
    return join_char.join(s_list)

def send_notification(notify_title, notify_text):
    notify_iface.Notify("New Pidgin Msg Notification", 0, "", notify_title, notify_text, [], {}, 0)

def received_im_msg(account, sender, message, conv, flags):
    text = ""
    text +=sender
    text +=" said:"
    text +=strip_ml_tags(message)
    send_notification("You got a new IM msg!", text)

from dbus.mainloop.glib import DBusGMainLoop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

# get the notify interface
bus = dbus.SessionBus()
bus.add_signal_receiver(received_im_msg, \
                dbus_interface = "im.pidgin.purple.PurpleInterface", \
                signal_name = "ReceivedImMsg")
notify_object = bus.get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
notify_iface=dbus.Interface(notify_object, "org.freedesktop.Notifications")

loop = gobject.MainLoop()
loop.run()

There are some things need to do, such as display the sender's local nickname, the smile face supported, change the Pidgin's tray status after click the notification.
References:
http://developer.pidgin.im/wiki/DbusHowto
http://developer.pidgin.im/doxygen/dev/html/conversation-signals.html#received-im-msg


The snapshot:


you can save the script as new_msg_notify.py and type:

$python new_msg_notify.py

in your SHELL prompt and enjoy it! :)


阅读(1977) | 评论(4) | 转发(0) |
0

上一篇:没有了

下一篇:[Perl]How to select a random line from a file?

给主人留下些什么吧!~~

crook2008-06-06 09:18:17

不错!

剑胆琴心2008-04-02 14:58:55

This is just a demo for study. you can add more functionality and integrate to Pidgin as a Python plugin. Please refer to:http://arstechnica.com/reviews/apps/pidgin-2-0.ars/4

chinaunix网友2008-04-01 22:43:48

I mean how to use it integrated with pidgin.

chinaunix网友2008-04-01 22:42:55

That's cool. How can use this script. daning106@gmail.com :)