diff --git a/gajim/app_actions.py b/gajim/app_actions.py index 3878e3973a010e1b9c039e53a6648ef04f11e5af..c15ed1eecd54bf0bb0e93bc98ba876ca84903d71 100644 --- a/gajim/app_actions.py +++ b/gajim/app_actions.py @@ -21,6 +21,7 @@ from gajim.common import helpers from gajim.common.app import interface from gajim.common.exceptions import GajimGeneralException +from gajim.common.helpers import open_uri from gajim.common.regex import LINK_REGEX from gajim import dialogs @@ -259,16 +260,12 @@ def on_remove_event(_action, param): win.show_title() def on_open_link_from_msg_event(_action, param): - dict_ = param.unpack() - account, jid, type_ = dict_['account'], dict_['jid'], dict_['type_'] - to_search = app.events.get_events(account,jid)[-1].message # something better might be needed - search_result =LINK_REGEX.search(to_search).group() - if search_result is not None: - if search_result.startswith("www."): #for "legacy_prefixes" - search_result = "http://%s"%search_result - if search_result.startswith("ftp."): #for "legacy_prefixes" - search_result = "ftp://%s"%search_result - Gtk.show_uri_on_window(None,search_result,Gdk.CURRENT_TIME) + par_unpack = param.unpack() + dict_ = dict(par_unpack[0]) + account, jid, type_ = dict_['account'], dict_['jid'], dict_['type_'] # maybe use this to remove event? + for link in par_unpack[1]: + open_uri(link) + # Other Actions diff --git a/gajim/application.py b/gajim/application.py index 60c6b7da5ce6a98f1db54b00020e29da0259e77c..f6ba57327bcead32b0acbe7cbe68bb36ffe3e8e9 100644 --- a/gajim/application.py +++ b/gajim/application.py @@ -470,7 +470,7 @@ def _get_account_actions(account): ('-delete-motd', a.on_delete_motd, 'online', 's'), ('-open-event', a.on_open_event, 'always', 'a{sv}'), ('-remove-event', a.on_remove_event, 'always', 'a{sv}'), - ('-open-link-from-msg-event', a.on_open_link_from_msg_event, 'always', 'a{sv}'), + ('-open-link-from-msg-event', a.on_open_link_from_msg_event, 'always', '(a(sv)as)'), ('-import-contacts', a.on_import_contacts, 'online', 's'), ] diff --git a/gajim/gtk/notification.py b/gajim/gtk/notification.py index f6342a3255edc2c07e45e8a53478602d73af5aaf..4337aa2c8368621f2a70f36393154244c48a3b96 100644 --- a/gajim/gtk/notification.py +++ b/gajim/gtk/notification.py @@ -23,7 +23,7 @@ # # You should have received a copy of the GNU General Public License # along with Gajim. If not, see <http://www.gnu.org/licenses/>. - +import re import sys import logging @@ -37,7 +37,7 @@ from gajim.common import helpers from gajim.common import ged from gajim.common.const import StyleAttr -from gajim.common.i18n import _ +from gajim.common.i18n import _, ngettext from gajim.common.nec import EventHelper from gajim.common.regex import LINK_REGEX @@ -210,9 +210,18 @@ def popup(self, event_type, jid, account, type_='', icon_name=None, notification.add_button_with_target( _('Mark as Read'), action, variant_dict) if text is not None: - if LINK_REGEX.search(text) is not None: + search_result = list(LINK_REGEX.finditer(text)) + if len(search_result) != 0: + notification.set_body(self._make_links_nice(text)) action = 'app.{}-open-link-from-msg-event'.format(account) - notification.add_button_with_target(_('Open link'), action, variant_dict) + button_text = ngettext('Open %d link', + "Open %d links", + len(search_result), + len(search_result), + len(search_result)) + uris = GLib.Variant('as', [sr.group() for sr in search_result]) + variant_link = GLib.Variant('(a(sv)as)', (dict_, uris)) + notification.add_button_with_target(button_text, action, variant_link) # Only one notification per JID if event_type == _('Contact Changed Status'): @@ -233,6 +242,24 @@ def popup(self, event_type, jid, account, type_='', icon_name=None, app.app.send_notification(notif_id, notification) + def _make_links_nice(self, text: str) -> str: + if "body-hyperlinks" in self._daemon_capabilities: + def add_prefix(match: re.Match) -> str: + uri = match.group() + prefix = "" + if uri.startswith("www."): + prefix = "https" + elif uri.startswith("ftp."): + prefix = "ftp" + elif re.match(r'\w+@\S+\.\S*[^\s\W]', uri): # should match naked username + prefix = "xmpp" + return "<a href=\"%s\">%s</a>" % ("%s://%s" % (prefix, uri), uri) + + return LINK_REGEX.sub(add_prefix, text) + elif "body-markup" in self._daemon_capabilities: + return LINK_REGEX.sub("<u>\g<0></u>", text) + return text + @staticmethod def _get_avatar_for_notification(account, jid): scale = get_monitor_scale_factor()