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



commit 612604ea9e8458433d59656fc056ae6b935d1e2d
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 ac21619..810881b 100644
--- a/src/applet.c
+++ b/src/applet.c
@@ -434,6 +434,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);
@@ -446,8 +447,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);
@@ -460,8 +465,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);
 	}
 }
 
@@ -662,58 +668,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,
@@ -723,7 +677,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);
@@ -743,7 +697,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 c68c2cd..26a5ad6 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -878,3 +878,56 @@ utils_next_available_name (GSList *connections, const char *format)
 	return cname;
 }
 
+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 9e26442..1f3e434 100644
--- a/src/utils/utils.h
+++ b/src/utils/utils.h
@@ -52,5 +52,7 @@ gboolean utils_mac_valid (const struct ether_addr *addr);
 
 char *utils_next_available_name (GSList *connections, const char *format);
 
+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]