~ubuntu-branches/ubuntu/oneiric/emesene/oneiric-proposed

« back to all changes in this revision

Viewing changes to emesene/e3/common/notification.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2011-03-03 14:49:13 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20110303144913-0adl9cmw2s35lvzo
Tags: 2.0~git20110303-0ubuntu1
* New upstream git revision (LP: #728469).
* Remove debian/watch, debian/emesene.xpm, debian/install and
  debian/README.source files.
* Remove 21_svn2451_fix_avatar and 20_dont_build_own_libmimic patches.
* debian/control: modify python to python (>= 2.5) in Build-Depends field.
* debian/control: remove python-libmimic from Recommends field.
* debian/control: modify python-gtk2 (>= 2.10) to python-gtk2 (>= 2.12) in
  Depends field.
* debian/control: add python-appindicator and python-xmpp to Recommends
  field.
* debian/control: add python-papyon (>= 0.5.4) and python-webkit to Depends
  field.
* debian/control: update Description field.
* debian/control: add python-setuptools to Build-Depends field.
* debian/control: move python-dbus and python-notify to Depends field.
* Update debian/copyright file.
* Update debian/links file.
* debian/menu: update description field.
* Bump Standards-Version to 3.9.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''emesene's notification system'''
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
#    This file is part of emesene.
 
5
#
 
6
#    emesene is free software; you can redistribute it and/or modify
 
7
#    it under the terms of the GNU General Public License as published by
 
8
#    the Free Software Foundation; either version 3 of the License, or
 
9
#    (at your option) any later version.
 
10
#
 
11
#    emesene is distributed in the hope that it will be useful,
 
12
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#    GNU General Public License for more details.
 
15
#
 
16
#    You should have received a copy of the GNU General Public License
 
17
#    along with emesene; if not, write to the Free Software
 
18
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 
 
20
from e3 import status
 
21
from e3 import Message
 
22
import extension
 
23
 
 
24
import time
 
25
import logging
 
26
log = logging.getLogger('gui.gtkui.Notification')
 
27
 
 
28
#TODO add config
 
29
#TODO update multiple message on notification
 
30
class Notification():
 
31
    '''emesene's notification system'''
 
32
    NAME = 'Notification'
 
33
    DESCRIPTION = 'emesene\'s notification system'
 
34
    AUTHOR = 'Cando'
 
35
    WEBSITE = 'www.emesene.org'
 
36
 
 
37
    def __init__(self, session):
 
38
        """
 
39
        Class Constructor
 
40
        """
 
41
        self.session = session
 
42
        self.session.config.get_or_set('b_notify_contact_online', True)
 
43
        self.session.config.get_or_set('b_notify_contact_offline', True)
 
44
        self.session.config.get_or_set('b_notify_receive_message', True)
 
45
 
 
46
        self.notifier = extension.get_default('notificationGUI')
 
47
 
 
48
        if self.session:
 
49
            self.session.signals.conv_message.subscribe(
 
50
                self._on_message)
 
51
            self.session.signals.contact_attr_changed.subscribe(
 
52
                self._on_contact_attr_changed)
 
53
            self.session.signals.mail_received.subscribe(
 
54
                self._on_mail_received)
 
55
            self.session.signals.filetransfer_completed.subscribe(
 
56
                self._on_filetransfer_completed)
 
57
            self.session.signals.filetransfer_canceled.subscribe(
 
58
                self._on_filetransfer_canceled)
 
59
            self.session.signals.filetransfer_invitation.subscribe(
 
60
                self._on_filetransfer_invitation)
 
61
 
 
62
      
 
63
 
 
64
        self.notify_online = False
 
65
        self.last_online = None
 
66
 
 
67
    def _on_filetransfer_completed(self,args):
 
68
        self.notifier("File transfer successful", "", 'notification-message-email', 'file-transf-completed')
 
69
 
 
70
    def _on_filetransfer_canceled(self,args):
 
71
        self.notifier("File transfer canceled", "", 'notification-message-email', 'file-transf-canceled')
 
72
 
 
73
    def _on_filetransfer_invitation(self, arg1, arg2):
 
74
        if isinstance(arg1.sender, str): # prevent notifying when we send a file
 
75
            return
 
76
        contact = self.session.contacts.get(arg1.sender.account)
 
77
        self._notify(contact, contact.nick, "File transfer invitation")
 
78
        
 
79
    def _on_mail_received(self, message):
 
80
        ''' called when a new mail is received '''
 
81
        self.notifier("New mail from %s" % (message.address), message._subject, 'notification-message-email','mail-received')
 
82
 
 
83
    def _on_message(self, cid, account, msgobj, cedict={}):
 
84
        """
 
85
        This is called when a new message arrives to a user.
 
86
        """
 
87
        if self.session.config.b_notify_receive_message and \
 
88
            not(self.session.conversations[cid].get_parent().is_active()):
 
89
            contact = self.session.contacts.get(account)            
 
90
            if msgobj.type == Message.TYPE_NUDGE:
 
91
                # The message needs to be translated.
 
92
                self._notify(contact, contact.nick , _('%s just sent you a nudge!') % (contact.nick,))
 
93
            else:
 
94
                self._notify(contact, contact.nick , msgobj.body)
 
95
 
 
96
    def _on_contact_attr_changed(self, account, change_type, old_value,
 
97
            do_notify=True):
 
98
        """
 
99
        This is called when an attribute of a contact changes
 
100
        """
 
101
        if change_type != 'status':
 
102
            return
 
103
 
 
104
        contact = self.session.contacts.get(account)
 
105
        if not contact:
 
106
            return
 
107
 
 
108
        if contact.status == status.ONLINE:
 
109
            if not self.notify_online:
 
110
                # detects the first notification flood and enable the
 
111
                # online notifications after it to prevent log in flood
 
112
                if self.last_online is not None:
 
113
                    t = time.time()
 
114
                    self.notify_online = (t - self.last_online > 1)
 
115
                    self.last_online = t
 
116
                else:
 
117
                    self.last_online = time.time()
 
118
            else:
 
119
                if self.session.config.b_notify_contact_online:
 
120
                    text = _('is online')
 
121
                    self._notify(contact, contact.nick, text)
 
122
        elif contact.status == status.OFFLINE:
 
123
            if self.session.config.b_notify_contact_offline:
 
124
                text = _('is offline')
 
125
                self._notify(contact, contact.nick, text)
 
126
 
 
127
    def _notify(self, contact, title, text):
 
128
        """
 
129
        This creates and shows the nofification
 
130
        """
 
131
        if contact.picture is not None and contact.picture != "":
 
132
            uri = "file://" + contact.picture
 
133
        else:
 
134
            uri = 'notification-message-im'
 
135
 
 
136
        self.notifier(title, text, uri,'message-im')
 
137