Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions Mailnag/plugins/libnotifyplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@
from gi.repository import Notify, Gio, Gtk
from Mailnag.common.plugins import Plugin, HookTypes
from Mailnag.common.i18n import _
from Mailnag.common.subproc import start_subprocess
from Mailnag.common.exceptions import InvalidOperationException
from Mailnag.daemon.mails import Mail

NOTIFICATION_MODE_COUNT = '0'
NOTIFICATION_MODE_SHORT_SUMMARY = '3'
NOTIFICATION_MODE_SUMMARY = '1'
NOTIFICATION_MODE_SINGLE = '2'
SUPPORTED_DESKTOP_ENVIRONMENTS = [
"gnome",
"cinnamon",
]

plugin_defaults = {
'notification_mode' : NOTIFICATION_MODE_SHORT_SUMMARY,
Expand All @@ -56,7 +59,7 @@ def __init__(self) -> None:
self._lock = threading.Lock()
self._notification_server_wait_event = threading.Event()
self._notification_server_ready = False
self._is_gnome = False
self._is_supported_env = False
self._mails_added_hook: Optional[Callable[[list[Mail], list[Mail]], None]] = None


Expand All @@ -69,7 +72,7 @@ def enable(self) -> None:
# initialize Notification
if not self._initialized:
Notify.init("Mailnagger")
self._is_gnome = self._is_gnome_environment(['XDG_CURRENT_DESKTOP', 'GDMSESSION'])
self._is_supported_env = self._is_supported_environment(['XDG_CURRENT_DESKTOP', 'GDMSESSION'])
self._initialized = True

def mails_added_hook(new_mails: list[Mail], all_mails: list[Mail]) -> None:
Expand Down Expand Up @@ -228,7 +231,7 @@ def _notify_short_summary(self, new_mails: list[Mail], all_mails: list[Mail]) ->
n += 1
i += 1

if self._is_gnome:
if self._is_supported_env:
senders = "<i>%s</i>" % ", ".join(lst)
else:
senders = ", ".join(lst)
Expand Down Expand Up @@ -258,13 +261,13 @@ def _notify_summary(self, new_mails: list[Mail], all_mails: list[Mail]) -> None:
ubound = len(mails) if len(mails) <= self._max_mails else self._max_mails

for i in range(ubound):
if self._is_gnome:
if self._is_supported_env:
body += "%s:\n<i>%s</i>\n\n" % (self._get_sender(mails[i]), mails[i].subject)
else:
body += "%s - %s\n" % (ellipsize(self._get_sender(mails[i]), 20), ellipsize(mails[i].subject, 20))

if len(mails) > self._max_mails:
if self._is_gnome:
if self._is_supported_env:
body += "<i>%s</i>" % _("(and {0} more)").format(str(len(mails) - self._max_mails))
else:
body += _("(and {0} more)").format(str(len(mails) - self._max_mails))
Expand Down Expand Up @@ -295,7 +298,7 @@ def _notify_single(self, new_mails, all_mails):
# Remember the associated message, so we know when to remove the notification:
n.mail = mail
notification_id = str(id(n))
if self._is_gnome:
if self._is_supported_env:
n.add_action("mark-as-read", _("Mark as read"),
self._notification_action_handler, (mail, notification_id))
n.show()
Expand Down Expand Up @@ -332,7 +335,7 @@ def _get_notification(
n.set_category("email")
n.set_hint_string("desktop-entry", "mailnagger")

if self._is_gnome:
if self._is_supported_env:
n.add_action("default", "default", self._notification_action_handler, None)

return n
Expand All @@ -357,7 +360,7 @@ def _notification_action_handler(
if action == "default":
mailclient = get_default_mail_reader()
if mailclient is not None:
start_subprocess(mailclient)
Gio.AppInfo.launch(mailclient)

# clicking the notification bubble has closed all notifications
# so clear the reference array as well.
Expand Down Expand Up @@ -390,9 +393,10 @@ def _prepend_new_mails(self, new_mails: list[Mail], all_mails: list[Mail]) -> li
return new_mails + [m for m in all_mails if m not in new_mails]


def _is_gnome_environment(self, env_vars: list[str]) -> bool:
def _is_supported_environment(self, env_vars: list[str]) -> bool:
for var in env_vars:
if 'gnome' in os.environ.get(var, '').lower().split(':'):
desktop_env = os.environ.get(var, "").lower()
if any(env in desktop_env for env in SUPPORTED_DESKTOP_ENVIRONMENTS):
return True
return False

Expand All @@ -402,10 +406,7 @@ def get_default_mail_reader() -> Optional[str]:
app_info = Gio.AppInfo.get_default_for_type("x-scheme-handler/mailto", False)

if app_info is not None:
executable = Gio.AppInfo.get_executable(app_info)

if (executable != None) and (len(executable) > 0):
mail_reader = executable
mail_reader = app_info

return mail_reader

Expand Down