All pastes #885527 Raw Edit

Unnamed

public text v1 · immutable
#885527 ·published 2008-01-30 23:16 UTC
rendered paste body
# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import gtk
import Plugin
import desktop
import emesenelib.common
from Parser import UserListDataType

ERROR = ''
try:
    import pynotify
    if not pynotify.init("emesene"):
        raise
except:
    ERROR = _( 'there was a problem initializing the pynotify module' )
    
class MainClass( Plugin.Plugin ):
    '''Main plugin class'''
    
    def __init__( self, controller, msn ):
        '''Contructor'''
        
        Plugin.Plugin.__init__( self, controller, msn )
        
        self.description = _( 'Notify about diferent events using pynotify [Extended version]' )
        self.authors = {'Mariano Guerra': 'luismarianoguerra at gmail dot com',
			'nitio' : 'nitio at emesene forum',
			'IngegnerLillo': 'ingegnerlillo at yahoo dot it',
			'Marco Trevisan (Treviño)': 'mail at 3v1n0 dot net'}
        self.website = 'http://www.emesene.org'
        self.displayName = _( 'LibNotify-Extended' )
        self.name = 'LibNotify-Extended'
        
        self.tray = self.controller.trayIcon.getNotifyObject()
        self.pixbuf = self.controller.theme.getImage( 'userPanel' )
        
        self.controller = controller
        self.parser = controller.unifiedParser
        
        self.notifications = []
        
        # callbacks ids
        self.onlineId = None
        self.newMailId = None

	self.config = controller.getConfig()
        self.config.readPluginConfig(self.name)
        self.notifyOnline = int(self.config.getPluginValue( self.name, 'notifyOnline', '1' ) )
        self.notifyMessage = int(self.config.getPluginValue( self.name, 'notifyMessage', '1' ) )
        self.notifyNudge = int(self.config.getPluginValue( self.name, 'notifyNudge', '1' ) )
        self.notifyMail = int(self.config.getPluginValue( self.name, 'notifyMail', '1' ) )
        self.notifyInactive = int(self.config.getPluginValue( self.name, 'notifyInactive', '1' ) )
        self.notifyBusy = int(self.config.getPluginValue( self.name, 'notifyBusy', '0' ) )
        
    def start( self ):
        '''start the plugin'''

	self.newMsgId = self.connect( 'message-received', self.newMsg )
        self.onlineId = self.connect( 'user-online', self.online )
        self.newMailId = self.connect( 'new-mail-notification', self.newMail )
        self.enabled = True
        
    def stop( self ):    
        '''
        stop the plugin
        '''
        
        # closes remaining notifications
        for i in self.notifications:
            i.close()

        self.notifications = []
            
        self.disconnect( self.onlineId )
        self.disconnect( self.newMailId )
	self.disconnect( self.newMsgId )
        self.enabled = False
        
    def check( self ):
        '''check if everything is OK to start the plugin
        return a tuple whith a boolean and a message
        if OK -> ( True , 'some message' )
        else -> ( False , 'error message' )'''
        global ERROR
        
        if ERROR != '':
            return ( False, ERROR )
        
        return ( True, 'Ok' )

    def newMsg( self, msnp, email ):
        '''called when someone send a messgae'''

        contact = self.msn.contactManager.getContact( email )

        text = ''
        for i in self.controller.mainWindow.userList.getContactLabel(contact):
            text += str( i )

        if self.controller.theme.hasUserDisplayPicture( contact ):
            self.pixbuf = self.controller.theme.getUserDisplayPicture( \
                contact, 48, 48 )
        
        notification = pynotify.Notification("emesene", "New message from\n" + text + " (" + email + ")\n", \
            attach=self.tray )

	#added by ingegnerlillo to make click on notification raise conversation window
	def openConversation( notification, action ):
#		Conversation = self.getOpenConversations()
		for conversation in self.getOpenConversations():
			#FIXME when the main window isn't active
			#TODO if conversation window is inactive switch to user-tab
			window = conversation.parentConversationWindow
			window.deiconify()
			window.present()
	
	#added by ingegnerlillo to make click on notification raise conversation window
	notification.add_action("default", "Default Action", openConversation)
	
        notification.set_icon_from_pixbuf( self.pixbuf )

        if self.controller.trayIcon and \
           isinstance(self.controller.trayIcon.tray, gtk.StatusIcon):
            tray = self.controller.trayIcon.tray
            try:
                notification.attach_to_status_icon(tray)
            except:
                print "cant set geometry hints on libnotify plugin"

	result = self.controller.conversationManager.getOpenConversation(email)
	if result != None and self.notifyInactive:
	    window, conversation = result
	    windowFocus = window.is_active()
	    tabFocus = (window.conversation == conversation)
	    inactive = True
	else
	    inactive = False

	if self.notifyMessage and inactive:
	    notification.show()
	    notification.connect( 'closed', self.on_notify_close )
	    self.notifications.append( notification )
	    self.pixbuf = self.controller.theme.getImage( 'userPanel' )

    def online( self, msnp, email, oldStatus ):
        '''called when someone get online'''

        if oldStatus != 'FLN':
            return

        contact = self.msn.contactManager.getContact( email )

        text = ''
        for i in self.controller.mainWindow.userList.getContactLabel(contact):
            text += str( i )

        if self.controller.theme.hasUserDisplayPicture( contact ):
            self.pixbuf = self.controller.theme.getUserDisplayPicture( \
                contact, 48, 48 )
        
        notification = pynotify.Notification("emesene", \
            text + " (" + email + ')\n' + _("is now online"), \
            attach=self.tray )

        notification.set_icon_from_pixbuf( self.pixbuf )
#        notification.add_action( 'default', 'Open conversation', \
#                                 self.on_notify_action )

        if self.controller.trayIcon and \
           isinstance(self.controller.trayIcon.tray, gtk.StatusIcon):
            tray = self.controller.trayIcon.tray
            try:
                notification.attach_to_status_icon(tray)
                #notification.set_hint("x", tray.get_geometry()[1].x)
                #notification.set_hint("y", tray.get_geometry()[1].y)
            except:
                print "cant set geometry hints on libnotify plugin"
            
        notification.show()
        notification.connect( 'closed', self.on_notify_close )
        self.notifications.append( notification )

        self.pixbuf = self.controller.theme.getImage( 'userPanel' )

    def newMail(self, msnp, From, FromAddr, Subject, MessageURL, PostURL, id):
        text = _('From: ')+From +' <'+FromAddr+'>\n'
        text += _('Subj: ') + emesenelib.common.escape( Subject )
        text += '\n\n<span foreground="#AAAAAA">emesene</span>'
        
        def openMail( notification, action ):
            desktop.open( \
                self.controller.hotmail.getLoginPage(MessageURL, PostURL, id))
        
        notification = pynotify.Notification(_( 'New email' ), \
                              text, \
                              attach=self.tray )
        notification.set_icon_from_pixbuf( self.pixbuf )
        notification.add_action( 'default', 'Open Mail', \
                                 openMail )
        if self.controller.trayIcon and \
           isinstance(self.controller.trayIcon.tray, gtk.StatusIcon):
            tray = self.controller.trayIcon.tray
            try:
                notification.attach_to_status_icon(tray)
            except:
                print "cant set geometry hints on libnotify plugin"

        notification.show()
        notification.connect( 'closed', self.on_notify_close )
        self.notifications.append( notification )
    
    def on_notify_close( self, n ):
        if n in self.notifications:
            self.notifications.remove( n )

#    def on_notify_action( self, notification, action ):
#        print ref
#        print str[:]
#        self.controller.newConversation( '' )


    def configure( self ):
        '''display a configuration dialog'''
        
        l = []

        # name, optionType, label, description, value, options

        l.append( Plugin.Option( 'notifyOnline', bool, _( 'Notify when online' ), _( 'Notify when someone gets online' ), (self.config.getPluginValue( self.name, 'notifyOnline', '1' ) == '1') ) )
        l.append(Plugin.Option( 'notifyMessage', bool, _( 'Notify on new message' ), _( 'Notify when someone sends you a message' ), (self.config.getPluginValue( self.name,  'notifyMessage', '1' ) == '1') ))
        l.append(Plugin.Option( 'notifyNudge', bool, _( 'Notify on Nudge' ), _( 'Notify when someone sends you a nudge' ), (self.config.getPluginValue( self.name, 'notifyNudge', '1' ) == '1') ))
        l.append(Plugin.Option( 'notifyInactive', bool, _( 'Only notify when window is inactive' ), _( 'Notify only when the message window is inactive' ), (self.config.getPluginValue( self.name, 'notifyInactive', '1' ) == '1') ))
        l.append(Plugin.Option( 'notifyBusy', bool, _( 'Disable notifications when busy' ), _( 'Disable notifications when you\'re busy' ), (self.config.getPluginValue( self.name, 'notifyBusy', '0' ) == '1') ))
        
        
        response = Plugin.ConfigWindow( _( 'Config Notify Plugin' ), l ).run()
        if response != None:
            if response.has_key( 'notifyOnline' ):
                self.config.setPluginValue( self.name, 'notifyOnline', str( int( response[ 'notifyOnline' ].value ) ) )
            if response.has_key( 'notifyMessage' ):
                self.config.setPluginValue( self.name, 'notifyMessage', str( int( response[ 'notifyMessage' ].value ) ) )
            if response.has_key( 'notifyNudge' ):
                self.config.setPluginValue( self.name, 'notifyNudge', str( int( response[ 'notifyNudge' ].value ) ) )
            if response.has_key( 'notifyInactive' ):
                self.config.setPluginValue( self.name, 'notifyInactive', str( int( response[ 'notifyInactive' ].value ) ) )
            if response.has_key( 'notifyBusy' ):
                self.config.setPluginValue( self.name, 'notifyBusy', str( int( response[ 'notifyBusy' ].value ) ) )

        self.notifyOnline = (self.config.getPluginValue( self.name, 'notifyOnline', '1' ) == '1' )
        self.notifyMessage = (self.config.getPluginValue( self.name, 'notifyMessage', '1' ) == '1' )
        self.notifyNudge = (self.config.getPluginValue( self.name, 'notifyNudge', '1' ) == '1' )
        self.notifyMail = (self.config.getPluginValue( self.name, 'notifyMail', '1' ) == '1' )
        self.notifyInactive = (self.config.getPluginValue( self.name, 'notifyInactive', '1' ) == '1' )
        self.notifyBusy = (self.config.getPluginValue( self.name, 'notifyBusy', '1' ) == '1' )
        
        return True