use nec / ged to dispatch gmail event
This commit is contained in:
parent
8595638cb4
commit
33c155d889
|
@ -1263,53 +1263,14 @@ ConnectionCaps, ConnectionHandlersBase, ConnectionJingle):
|
|||
self.connection.send(iq)
|
||||
raise common.xmpp.NodeProcessed
|
||||
|
||||
def _gMailQueryCB(self, con, gm):
|
||||
def _gMailQueryCB(self, con, iq_obj):
|
||||
"""
|
||||
Called when we receive results from Querying the server for mail messages
|
||||
in gmail account
|
||||
"""
|
||||
if not gm.getTag('mailbox'):
|
||||
return
|
||||
self.gmail_url = gm.getTag('mailbox').getAttr('url')
|
||||
if gm.getTag('mailbox').getNamespace() == common.xmpp.NS_GMAILNOTIFY:
|
||||
newmsgs = gm.getTag('mailbox').getAttr('total-matched')
|
||||
if newmsgs != '0':
|
||||
# there are new messages
|
||||
gmail_messages_list = []
|
||||
if gm.getTag('mailbox').getTag('mail-thread-info'):
|
||||
gmail_messages = gm.getTag('mailbox').getTags('mail-thread-info')
|
||||
for gmessage in gmail_messages:
|
||||
unread_senders = []
|
||||
for sender in gmessage.getTag('senders').getTags('sender'):
|
||||
if sender.getAttr('unread') != '1':
|
||||
continue
|
||||
if sender.getAttr('name'):
|
||||
unread_senders.append(sender.getAttr('name') + '< ' + \
|
||||
sender.getAttr('address') + '>')
|
||||
else:
|
||||
unread_senders.append(sender.getAttr('address'))
|
||||
|
||||
if not unread_senders:
|
||||
continue
|
||||
gmail_subject = gmessage.getTag('subject').getData()
|
||||
gmail_snippet = gmessage.getTag('snippet').getData()
|
||||
tid = int(gmessage.getAttr('tid'))
|
||||
if not self.gmail_last_tid or tid > self.gmail_last_tid:
|
||||
self.gmail_last_tid = tid
|
||||
gmail_messages_list.append({ \
|
||||
'From': unread_senders, \
|
||||
'Subject': gmail_subject, \
|
||||
'Snippet': gmail_snippet, \
|
||||
'url': gmessage.getAttr('url'), \
|
||||
'participation': gmessage.getAttr('participation'), \
|
||||
'messages': gmessage.getAttr('messages'), \
|
||||
'date': gmessage.getAttr('date')})
|
||||
self.gmail_last_time = int(gm.getTag('mailbox').getAttr(
|
||||
'result-time'))
|
||||
|
||||
jid = gajim.get_jid_from_account(self.name)
|
||||
log.debug(('You have %s new gmail e-mails on %s.') % (newmsgs, jid))
|
||||
self.dispatch('GMAIL_NOTIFY', (jid, newmsgs, gmail_messages_list))
|
||||
log.debug('gMailQueryCB')
|
||||
gajim.nec.push_incoming_event(GMailQueryReceivedEvent(None,
|
||||
conn=self, iq_obj=iq_obj))
|
||||
raise common.xmpp.NodeProcessed
|
||||
|
||||
def _rosterItemExchangeCB(self, con, msg):
|
||||
|
@ -2537,3 +2498,65 @@ class TimeResultReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
|||
return
|
||||
|
||||
return True
|
||||
|
||||
class GMailQueryReceivedEvent(nec.NetworkIncomingEvent):
|
||||
name = 'gmail-notify'
|
||||
base_network_events = []
|
||||
|
||||
def generate(self):
|
||||
if not self.conn:
|
||||
self.conn = self.base_event.conn
|
||||
if not self.iq_obj:
|
||||
self.iq_obj = self.base_event.xmpp_iq
|
||||
|
||||
if not self.iq_obj.getTag('mailbox'):
|
||||
return
|
||||
mb = self.iq_obj.getTag('mailbox')
|
||||
if not mb.getAttr('url'):
|
||||
return
|
||||
self.conn.gmail_url = mb.getAttr('url')
|
||||
if mb.getNamespace() != common.xmpp.NS_GMAILNOTIFY:
|
||||
return
|
||||
self.newmsgs = mb.getAttr('total-matched')
|
||||
if not self.newmsgs:
|
||||
return
|
||||
if self.newmsgs == '0':
|
||||
return
|
||||
# there are new messages
|
||||
self.gmail_messages_list = []
|
||||
if mb.getTag('mail-thread-info'):
|
||||
gmail_messages = mb.getTags('mail-thread-info')
|
||||
for gmessage in gmail_messages:
|
||||
unread_senders = []
|
||||
for sender in gmessage.getTag('senders').getTags(
|
||||
'sender'):
|
||||
if sender.getAttr('unread') != '1':
|
||||
continue
|
||||
if sender.getAttr('name'):
|
||||
unread_senders.append(sender.getAttr('name') + \
|
||||
'< ' + sender.getAttr('address') + '>')
|
||||
else:
|
||||
unread_senders.append(sender.getAttr('address'))
|
||||
|
||||
if not unread_senders:
|
||||
continue
|
||||
gmail_subject = gmessage.getTag('subject').getData()
|
||||
gmail_snippet = gmessage.getTag('snippet').getData()
|
||||
tid = int(gmessage.getAttr('tid'))
|
||||
if not self.conn.gmail_last_tid or \
|
||||
tid > self.conn.gmail_last_tid:
|
||||
self.conn.gmail_last_tid = tid
|
||||
self.gmail_messages_list.append({
|
||||
'From': unread_senders,
|
||||
'Subject': gmail_subject,
|
||||
'Snippet': gmail_snippet,
|
||||
'url': gmessage.getAttr('url'),
|
||||
'participation': gmessage.getAttr('participation'),
|
||||
'messages': gmessage.getAttr('messages'),
|
||||
'date': gmessage.getAttr('date')})
|
||||
self.conn.gmail_last_time = int(mb.getAttr('result-time'))
|
||||
|
||||
self.jid = gajim.get_jid_from_account(self.name)
|
||||
log.debug(('You have %s new gmail e-mails on %s.') % (self.newmsgs,
|
||||
self.jid))
|
||||
return True
|
|
@ -1288,11 +1288,12 @@ class Interface:
|
|||
notify.popup(event_type, jid, account, 'file-send-error', path,
|
||||
event_type, file_props['name'])
|
||||
|
||||
def handle_event_gmail_notify(self, account, array):
|
||||
jid = array[0]
|
||||
gmail_new_messages = int(array[1])
|
||||
gmail_messages_list = array[2]
|
||||
if gajim.config.get('notify_on_new_gmail_email'):
|
||||
def handle_event_gmail_notify(self, obj):
|
||||
jid = obj.jid
|
||||
gmail_new_messages = int(obj.newmsgs)
|
||||
gmail_messages_list = obj.gmail_messages_list
|
||||
if not gajim.config.get('notify_on_new_gmail_email'):
|
||||
return
|
||||
path = gtkgui_helpers.get_icon_path('gajim-new_email_recv', 48)
|
||||
title = _('New mail on %(gmail_mail_address)s') % \
|
||||
{'gmail_mail_address': jid}
|
||||
|
@ -1310,20 +1311,16 @@ class Interface:
|
|||
break
|
||||
senders = ',\n '.join(reversed(gmessage['From']))
|
||||
text += _('\n\nFrom: %(from_address)s\nSubject: '
|
||||
'%(subject)s\n%(snippet)s') % \
|
||||
{'from_address': senders,
|
||||
'%(subject)s\n%(snippet)s') % {'from_address': senders,
|
||||
'subject': gmessage['Subject'],
|
||||
'snippet': gmessage['Snippet']}
|
||||
cnt += 1
|
||||
|
||||
if gajim.config.get_per('soundevents', 'gmail_received', 'enabled'):
|
||||
helpers.play_sound('gmail_received')
|
||||
notify.popup(_('New E-mail'), jid, account, 'gmail',
|
||||
notify.popup(_('New E-mail'), jid, obj.conn.name, 'gmail',
|
||||
path_to_image=path, title=title, text=text)
|
||||
|
||||
if self.remote_ctrl:
|
||||
self.remote_ctrl.raise_signal('NewGmail', (account, array))
|
||||
|
||||
def handle_event_file_request_error(self, account, array):
|
||||
# ('FILE_REQUEST_ERROR', account, (jid, file_props, error_msg))
|
||||
jid, file_props, errmsg = array
|
||||
|
@ -2100,7 +2097,6 @@ class Interface:
|
|||
'CON_TYPE': [self.handle_event_con_type],
|
||||
'CONNECTION_LOST': [self.handle_event_connection_lost],
|
||||
'FILE_REQUEST': [self.handle_event_file_request],
|
||||
'GMAIL_NOTIFY': [self.handle_event_gmail_notify],
|
||||
'FILE_REQUEST_ERROR': [self.handle_event_file_request_error],
|
||||
'FILE_SEND_ERROR': [self.handle_event_file_send_error],
|
||||
'STANZA_ARRIVED': [self.handle_event_stanza_arrived],
|
||||
|
@ -2146,6 +2142,7 @@ class Interface:
|
|||
'JINGLE_ERROR': [self.handle_event_jingle_error],
|
||||
'PEP_RECEIVED': [self.handle_event_pep_received],
|
||||
'CAPS_RECEIVED': [self.handle_event_caps_received],
|
||||
'gmail-notify': [self.handle_event_gmail_notify],
|
||||
'http-auth-received': [self.handle_event_http_auth],
|
||||
'last-result-received': [self.handle_event_last_status_time],
|
||||
}
|
||||
|
|
|
@ -111,6 +111,8 @@ class Remote:
|
|||
self.on_os_info)
|
||||
gajim.ged.register_event_handler('time-result-received', ged.POSTGUI,
|
||||
self.on_time)
|
||||
gajim.ged.register_event_handler('gmail-nofify', ged.POSTGUI,
|
||||
self.on_gmail_notify)
|
||||
|
||||
def on_last_status_time(self, obj):
|
||||
self.raise_signal('LastStatusTime', (obj.conn.name, [
|
||||
|
@ -124,6 +126,10 @@ class Remote:
|
|||
self.raise_signal('EntityTime', (obj.conn.name, [obj.jid, obj.resource,
|
||||
obj.time_info]))
|
||||
|
||||
def on_gmail_notify(self, obj):
|
||||
self.raise_signal('NewGmail', (obj.conn.name, [obj.jid, obj.newmsgs,
|
||||
obj.gmail_messages_list]))
|
||||
|
||||
def raise_signal(self, signal, arg):
|
||||
if self.signal_object:
|
||||
try:
|
||||
|
|
Loading…
Reference in New Issue