1
'''emesene's notification system'''
2
# -*- coding: utf-8 -*-
4
# This file is part of emesene.
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.
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.
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
21
from e3 import Message
26
log = logging.getLogger('gui.gtkui.Notification')
29
#TODO update multiple message on notification
31
'''emesene's notification system'''
33
DESCRIPTION = 'emesene\'s notification system'
35
WEBSITE = 'www.emesene.org'
37
def __init__(self, session):
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)
46
self.notifier = extension.get_default('notificationGUI')
49
self.session.signals.conv_message.subscribe(
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)
64
self.notify_online = False
65
self.last_online = None
67
def _on_filetransfer_completed(self,args):
68
self.notifier("File transfer successful", "", 'notification-message-email', 'file-transf-completed')
70
def _on_filetransfer_canceled(self,args):
71
self.notifier("File transfer canceled", "", 'notification-message-email', 'file-transf-canceled')
73
def _on_filetransfer_invitation(self, arg1, arg2):
74
if isinstance(arg1.sender, str): # prevent notifying when we send a file
76
contact = self.session.contacts.get(arg1.sender.account)
77
self._notify(contact, contact.nick, "File transfer invitation")
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')
83
def _on_message(self, cid, account, msgobj, cedict={}):
85
This is called when a new message arrives to a user.
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,))
94
self._notify(contact, contact.nick , msgobj.body)
96
def _on_contact_attr_changed(self, account, change_type, old_value,
99
This is called when an attribute of a contact changes
101
if change_type != 'status':
104
contact = self.session.contacts.get(account)
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:
114
self.notify_online = (t - self.last_online > 1)
117
self.last_online = time.time()
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)
127
def _notify(self, contact, title, text):
129
This creates and shows the nofification
131
if contact.picture is not None and contact.picture != "":
132
uri = "file://" + contact.picture
134
uri = 'notification-message-im'
136
self.notifier(title, text, uri,'message-im')