From 74a5e02dc2f9e646069e67175c3b516f0df2282f Mon Sep 17 00:00:00 2001 From: Patrick Ulbrich Date: Mon, 22 Jul 2013 20:37:29 +0200 Subject: [PATCH] plugin hook api changes, enabled single notification mode and misc other fixes mailchecker, plugins: pass both, new and all mails to MAILS_ADDED hook mailchecker: fixed wrong count for filtered new mails libnotify plugin: added some constants, enabled single notification mode, uncommented notification_id spamfilter plugin: renamed variable --- Mailnag/common/plugins.py | 4 ++-- Mailnag/daemon/mailchecker.py | 18 ++++++++++-------- Mailnag/plugins/dbusplugin.py | 4 ++-- Mailnag/plugins/libnotifyplugin.py | 15 ++++++++------- Mailnag/plugins/soundplugin.py | 2 +- Mailnag/plugins/spamfilterplugin.py | 4 ++-- 6 files changed, 25 insertions(+), 22 deletions(-) diff --git a/Mailnag/common/plugins.py b/Mailnag/common/plugins.py index 99e049d..946ed2b 100644 --- a/Mailnag/common/plugins.py +++ b/Mailnag/common/plugins.py @@ -41,7 +41,7 @@ class HookTypes: # OUT: None MAIL_CHECK = 'mail-check' # func signature: - # IN: all mails, count of new mails + # IN: new mails, all mails # OUT: None MAILS_ADDED = 'mails-added' # func signature: @@ -49,7 +49,7 @@ class HookTypes: # OUT: None MAILS_REMOVED = 'mails-removed' # func signature: - # IN: new mails + # IN: all mails # OUT: filtered mails FILTER_MAILS = 'filter-mails' diff --git a/Mailnag/daemon/mailchecker.py b/Mailnag/daemon/mailchecker.py index 4e209f8..79d31ff 100644 --- a/Mailnag/daemon/mailchecker.py +++ b/Mailnag/daemon/mailchecker.py @@ -67,32 +67,34 @@ class MailChecker: all_mails = self._mailsyncer.sync(accounts) unseen_mails = [] - new_mail_count = 0 + new_mails = [] for mail in all_mails: if self._reminder.contains(mail.id): # mail was fetched before if self._reminder.unseen(mail.id): # mail was not marked as seen unseen_mails.append(mail) if self._firstcheck: - new_mail_count += 1 + new_mails.append(mail) else: # mail is fetched the first time unseen_mails.append(mail) - new_mail_count += 1 + new_mails.append(mail) # apply filter plugin hooks - filtered_mails = unseen_mails + filtered_unseen_mails = unseen_mails for f in self._hookreg.get_hook_funcs(HookTypes.FILTER_MAILS): - filtered_mails = try_call( lambda: f(filtered_mails), filtered_mails ) + filtered_unseen_mails = try_call( lambda: f(filtered_unseen_mails), filtered_unseen_mails ) + + filtered_new_mails = [m for m in new_mails if m in filtered_unseen_mails] # TODO : signal MailsRemoved if not all mails have been removed # (i.e. if mailcount has been decreased) if len(all_mails) == 0: for f in self._hookreg.get_hook_funcs(HookTypes.MAILS_REMOVED): - try_call( lambda: f(filtered_mails) ) - elif (new_mail_count > 0) and (filtered_mails > 0): + try_call( lambda: f(filtered_unseen_mails) ) + elif len(filtered_new_mails) > 0: for f in self._hookreg.get_hook_funcs(HookTypes.MAILS_ADDED): - try_call( lambda: f(filtered_mails, new_mail_count) ) + try_call( lambda: f(filtered_new_mails, filtered_unseen_mails) ) self._reminder.save(all_mails) diff --git a/Mailnag/plugins/dbusplugin.py b/Mailnag/plugins/dbusplugin.py index 16df33d..f105a43 100644 --- a/Mailnag/plugins/dbusplugin.py +++ b/Mailnag/plugins/dbusplugin.py @@ -41,9 +41,9 @@ class DBusPlugin(Plugin): controller = self.get_mailnag_controller() self._dbusservice = DBusService(controller) - def mails_added_hook(all_mails, new_mail_count): + def mails_added_hook(new_mails, all_mails): self._dbusservice.set_mails(all_mails) - self._dbusservice.MailsAdded(new_mail_count) + self._dbusservice.MailsAdded(len(new_mails)) def mails_removed_hook(remaining_mails): self._dbusservice.set_mails(remaining_mails) diff --git a/Mailnag/plugins/libnotifyplugin.py b/Mailnag/plugins/libnotifyplugin.py index f3e7eae..67fafa6 100644 --- a/Mailnag/plugins/libnotifyplugin.py +++ b/Mailnag/plugins/libnotifyplugin.py @@ -25,7 +25,10 @@ from gi.repository import Notify from common.plugins import Plugin, HookTypes from common.i18n import _ -plugin_defaults = { 'notification_mode' : '1' } +NOTIFICATION_MODE_SINGLE = '0' +NOTIFICATION_MODE_SUMMARY = '1' + +plugin_defaults = { 'notification_mode' : NOTIFICATION_MODE_SUMMARY } class LibNotifyPlugin(Plugin): @@ -48,12 +51,10 @@ class LibNotifyPlugin(Plugin): Notify.init("Mailnag") self._initialized = True - def mails_added_hook(all_mails, new_mail_count): + def mails_added_hook(new_mails, all_mails): config = self.get_config() - if config['notification_mode'] == 0: - # TODO : not supported by plugin API yet - # self._notify_single(new_mails) - pass + if config['notification_mode'] == NOTIFICATION_MODE_SINGLE: + self._notify_single(new_mails) else: self._notify_summary(all_mails) @@ -150,7 +151,7 @@ class LibNotifyPlugin(Plugin): def _notify_single(self, mails): for mail in mails: n = self._get_notification(mail.sender, mail.subject, "mail-unread") - # notification_id = str(id(n)) + notification_id = str(id(n)) # n.add_action("mark-as-read", _("Mark as read"), self._notification_action_handler, (mail, notification_id), None) n.show() self._notifications[notification_id] = n diff --git a/Mailnag/plugins/soundplugin.py b/Mailnag/plugins/soundplugin.py index 342a2a7..3b2e883 100644 --- a/Mailnag/plugins/soundplugin.py +++ b/Mailnag/plugins/soundplugin.py @@ -40,7 +40,7 @@ class SoundPlugin(Plugin): def enable(self): self._gsettings = Gio.Settings.new('org.gnome.shell') - def mails_added_hook(all_mails, new_mail_count): + def mails_added_hook(new_mails, all_mails): if self._gsettings.get_int('saved-session-presence') != 2: config = self.get_config() gstplay(get_data_file(config['soundfile'])) diff --git a/Mailnag/plugins/spamfilterplugin.py b/Mailnag/plugins/spamfilterplugin.py index e46f1f7..58fd1d3 100644 --- a/Mailnag/plugins/spamfilterplugin.py +++ b/Mailnag/plugins/spamfilterplugin.py @@ -37,9 +37,9 @@ class SpamfilterPlugin(Plugin): config = self.get_config() self._filter_list = config['filter_text'].replace('\n', '').split(',') - def filter_mails_hook(new_mails): + def filter_mails_hook(mails): lst = [] - for m in new_mails: + for m in mails: if not self._is_filtered(m): lst.append(m) return lst