[network-manager-applet] applet: escape notification text



commit d9229062db0b04a0a8b3d69edce2a60d28c06027
Author: Dan Williams <dcbw redhat com>
Date:   Mon Dec 7 21:44:15 2009 -0800

    applet: escape notification text
    
    Notification messages are a subset of HTML, and thus have to have
    characters like '&' escaped, otherwise the notification daemon
    displays a blank alert.  Consolidate the escaping code so that
    we can use it for both VPN banners and other notifications.  Ran
    into this problem when the applet was trying to display a connect
    notification on connection to the 'AT&T MEdia Net' connection
    created by the mobile broadband wizard.

 src/applet.c      |   70 +++++++++-------------------------------------------
 src/utils/utils.c |   53 ++++++++++++++++++++++++++++++++++++++++
 src/utils/utils.h |    2 +
 3 files changed, 67 insertions(+), 58 deletions(-)
---
diff --git a/src/applet.c b/src/applet.c
index c401702..e1084ca 100644
--- a/src/applet.c
+++ b/src/applet.c
@@ -625,6 +625,7 @@ applet_do_notify (NMApplet *applet,
 {
 	NotifyNotification *notify;
 	GError *error = NULL;
+	char *escaped;
 
 	g_return_if_fail (applet != NULL);
 	g_return_if_fail (summary != NULL);
@@ -637,8 +638,12 @@ applet_do_notify (NMApplet *applet,
 
 	applet_clear_notify (applet);
 
-	notify = notify_notification_new (summary, message,
-	                                  icon ? icon : GTK_STOCK_NETWORK, NULL);
+	escaped = utils_escape_notify_message (message);
+	notify = notify_notification_new (summary,
+	                                  escaped,
+	                                  icon ? icon : GTK_STOCK_NETWORK,
+	                                  NULL);
+	g_free (escaped);
 	applet->notification = notify;
 
 	notify_notification_attach_to_status_icon (notify, applet->status_icon);
@@ -651,8 +656,9 @@ applet_do_notify (NMApplet *applet,
 	}
 
 	if (!notify_notification_show (notify, &error)) {
-		g_warning ("Failed to show notification: %s", error->message);
-		g_error_free (error);
+		g_warning ("Failed to show notification: %s",
+		           error && error->message ? error->message : "(unknown)");
+		g_clear_error (&error);
 	}
 }
 
@@ -865,58 +871,6 @@ make_vpn_disconnection_message (NMVPNConnection *vpn,
 	return g_strdup_printf (_("\nThe VPN connection '%s' disconnected."), nm_setting_connection_get_id (s_con));
 }
 
-typedef struct {
-	const char *tag;
-	const char *replacement;
-} Tag;
-
-static Tag banner_tags[] = {
-	{ "<center>", NULL },
-	{ "</center>", NULL },
-	{ "<p>", "\n" },
-	{ "</p>", NULL },
-	{ "<B>", "<b>" },
-	{ "</B>", "</b>" },
-	{ "<I>", "<i>" },
-	{ "</I>", "</i>" },
-	{ "<u>", "<u>" },
-	{ "</u>", "</u>" },
-	{ NULL, NULL }
-};
-
-static char *
-construct_vpn_banner (const char *src)
-{
-	const char *p = src;
-	GString *banner;
-
-	/* Filter the banner text and get rid of some HTML tags since the
-	 * notification spec only allows a subset of HTML.
-	 */
-
-	banner = g_string_sized_new (strlen (src) + 5);
-	g_string_append_c (banner, '\n');
-	while (*p) {
-		Tag *t = &banner_tags[0];
-		gboolean found = FALSE;
-
-		while (t->tag) {
-			if (strncasecmp (p, t->tag, strlen (t->tag)) == 0) {
-				p += strlen (t->tag);
-				if (t->replacement)
-					g_string_append (banner, t->replacement);
-				found = TRUE;
-				break;
-			}
-			t++;
-		}
-		if (!found)
-			g_string_append_c (banner, *p++);
-	}
-
-	return g_string_free (banner, FALSE);
-}
-
 static void
 vpn_connection_state_changed (NMVPNConnection *vpn,
                               NMVPNConnectionState state,
@@ -926,7 +880,7 @@ vpn_connection_state_changed (NMVPNConnection *vpn,
 	NMApplet *applet = NM_APPLET (user_data);
 	NMConnection *connection;
 	const char *banner;
-	char *title = NULL, *msg = NULL;
+	char *title = NULL, *msg;
 	gboolean device_activating, vpn_activating;
 
 	device_activating = applet_is_any_device_activating (applet);
@@ -946,7 +900,7 @@ vpn_connection_state_changed (NMVPNConnection *vpn,
 		banner = nm_vpn_connection_get_banner (vpn);
 		if (banner && strlen (banner)) {
 			title = _("VPN Login Message");
-			msg = construct_vpn_banner (banner);
+			msg = g_strdup_printf ("%s\n", banner);
 			applet_do_notify (applet, NOTIFY_URGENCY_LOW, title, msg,
 			                  "gnome-lockscreen", NULL, NULL, NULL, NULL);
 			g_free (msg);
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 6f21f54..09a5fef 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -838,3 +838,56 @@ utils_hash_ap (const GByteArray *ssid,
 	return g_compute_checksum_for_data (G_CHECKSUM_MD5, input, sizeof (input));
 }
 
+typedef struct {
+	const char *tag;
+	const char *replacement;
+} Tag;
+
+static Tag escaped_tags[] = {
+	{ "<center>", NULL },
+	{ "</center>", NULL },
+	{ "<p>", "\n" },
+	{ "</p>", NULL },
+	{ "<B>", "<b>" },
+	{ "</B>", "</b>" },
+	{ "<I>", "<i>" },
+	{ "</I>", "</i>" },
+	{ "<u>", "<u>" },
+	{ "</u>", "</u>" },
+	{ "&", "&amp;" },
+	{ NULL, NULL }
+};
+
+char *
+utils_escape_notify_message (const char *src)
+{
+	const char *p = src;
+	GString *escaped;
+
+	/* Filter the source text and get rid of some HTML tags since the
+	 * notification spec only allows a subset of HTML.  Substitute
+	 * HTML code for characters like & that are invalid in HTML.
+	 */
+
+	escaped = g_string_sized_new (strlen (src) + 5);
+	while (*p) {
+		Tag *t = &escaped_tags[0];
+		gboolean found = FALSE;
+
+		while (t->tag) {
+			if (strncasecmp (p, t->tag, strlen (t->tag)) == 0) {
+				p += strlen (t->tag);
+				if (t->replacement)
+					g_string_append (escaped, t->replacement);
+				found = TRUE;
+				break;
+			}
+			t++;
+		}
+		if (!found)
+			g_string_append_c (escaped, *p++);
+	}
+
+	return g_string_free (escaped, FALSE);
+}
+
diff --git a/src/utils/utils.h b/src/utils/utils.h
index 64f8b28..07d29d8 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -55,5 +55,7 @@ char *utils_hash_ap (const GByteArray *ssid,
                      guint32 wpa_flags,
                      guint32 rsn_flags);
 
+char *utils_escape_notify_message (const char *src);
+
 #endif /* UTILS_H */
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]