-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmessagingmenuplugin.py
212 lines (153 loc) · 5.91 KB
/
messagingmenuplugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# Copyright 2014 - 2016 Patrick Ulbrich <[email protected]>
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GLib', '2.0')
gi.require_version('MessagingMenu', '1.0')
from gi.repository import Gtk, Gio, MessagingMenu
from Mailnag.common.plugins import Plugin, HookTypes
from Mailnag.common.exceptions import InvalidOperationException
from Mailnag.common.i18n import _
PLUGIN_VERSION = "1.2"
MAX_VISIBLE_MAILS_LIMIT = 20.0
MAIL_ICON = 'mail-unread-symbolic'
OPEN_MAIL_READER_ICON = 'mail-read'
plugin_defaults = { 'max_visible_mails' : '10' }
class MessagingMenuPlugin(Plugin):
def __init__(self):
self._mails_added_hook = None
self._mails_removed_hook = None
self._app = None
self._mails = None
self._max_mails = 0
def enable(self):
icon = Gio.ThemedIcon.new(OPEN_MAIL_READER_ICON)
self._app = MessagingMenu.App.new('mailnag-messagingmenu.desktop')
self._app.connect('activate-source', self._source_activated)
self._app.register()
self._mails = []
self._max_mails = int(self.get_config()['max_visible_mails'])
def mails_added_hook(new_mails, all_mails):
self._rebuild_with_new(new_mails, all_mails)
def mails_removed_hook(remaining_mails):
self._rebuild_with_remaining(remaining_mails)
self._mails_added_hook = mails_added_hook
self._mails_removed_hook = mails_removed_hook
controller = self.get_mailnag_controller()
hooks = controller.get_hooks()
hooks.register_hook_func(HookTypes.MAILS_ADDED,
self._mails_added_hook)
hooks.register_hook_func(HookTypes.MAILS_REMOVED,
self._mails_removed_hook)
def disable(self):
controller = self.get_mailnag_controller()
hooks = controller.get_hooks()
if self._mails_added_hook != None:
hooks.unregister_hook_func(HookTypes.MAILS_ADDED,
self._mails_added_hook)
self._mails_added_hook = None
if self._mails_removed_hook != None:
hooks.unregister_hook_func(HookTypes.MAILS_REMOVED,
self._mails_removed_hook)
self._mails_removed_hook = None
if self._app != None:
self._app.unregister()
self._app = None
self._mails = None
def get_manifest(self):
return (_("MessagingMenu"),
_("Shows new mails in the MessagingMenu indicator."),
PLUGIN_VERSION,
"Patrick Ulbrich <[email protected]>")
def get_default_config(self):
return plugin_defaults
def has_config_ui(self):
return True
def get_config_ui(self):
box = Gtk.Box()
box.set_spacing(12)
box.set_orientation(Gtk.Orientation.HORIZONTAL)
label = Gtk.Label(_('Maximum number of visible mails:'))
spinner = Gtk.SpinButton.new_with_range(1.0, MAX_VISIBLE_MAILS_LIMIT, 1.0)
box.pack_start(label, False, False, 0)
box.pack_start(spinner, False, False, 0)
return box
def load_ui_from_config(self, config_ui):
config = self.get_config()
max_mails = float(config['max_visible_mails'])
spinner = config_ui.get_children()[1]
spinner.set_value(max_mails)
def save_ui_to_config(self, config_ui):
config = self.get_config()
spinner = config_ui.get_children()[1]
max_mails = spinner.get_value()
config['max_visible_mails'] = str(int(max_mails))
def _rebuild_with_new(self, new_mails, all_mails):
menu_reset = (len(self._mails) > 0) and (not self._app.has_source(self._mails[0].id))
# Clear messaging menu (remove current mails)
for m in self._mails:
self._app.remove_source(m.id)
if menu_reset:
# Reset detected (the Clear button has been pressed by the user).
# Discard current mails and start a new mail list.
self._mails = new_mails
else:
# Remove mails that are gone (e. g. removed in mail reader)
mails = [m for m in self._mails if m in all_mails]
# Add new mails to the top of the mail list.
self._mails = new_mails + mails
self._add_mails_to_menu()
def _rebuild_with_remaining(self, remaining_mails):
menu_reset = (len(self._mails) > 0) and (not self._app.has_source(self._mails[0].id))
# Clear messaging menu (remove current mails)
for m in self._mails:
self._app.remove_source(m.id)
if menu_reset:
# Reset detected (the Clear button has been pressed by the user).
# Discard current mails.
self._mails = []
else:
# Remove mails that are not in the remainder list.
self._mails = [m for m in self._mails if m in remaining_mails]
self._add_mails_to_menu()
def _add_mails_to_menu(self):
# Add mail list to the messaging menu.
if len(self._mails) > 0:
ubound = len(self._mails) if len(self._mails) <= self._max_mails \
else self._max_mails
for i in range(ubound):
m = self._mails[i]
name, addr = m.sender
sender = name if len(name) > 0 else addr
icon = Gio.ThemedIcon.new(MAIL_ICON)
label = "%s - %s" % (sender, m.subject)
if m.datetime > 0:
time = m.datetime * 1000 * 1000
self._app.append_source_with_time(m.id, icon, label, time)
else:
self._app.append_source_with_string(m.id, icon, label, '?')
self._app.draw_attention(m.id)
def _source_activated(self, app, source_id):
self._mails = [m for m in self._mails if m.id != source_id]
controller = self.get_mailnag_controller()
try:
controller.mark_mail_as_read(source_id)
except InvalidOperationException:
pass
# Rebuild to push up hidden mails
self._rebuild_with_new([], self._mails)