[maemo-commits] [maemo-commits] r9063 - in projects/haf/trunk/libossomime: . libossomime tests

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Thu Jan 11 15:24:17 EET 2007
Author: martyn
Date: 2007-01-11 15:24:13 +0200 (Thu, 11 Jan 2007)
New Revision: 9063

Modified:
   projects/haf/trunk/libossomime/ChangeLog
   projects/haf/trunk/libossomime/libossomime/osso-uri.c
   projects/haf/trunk/libossomime/tests/test-all.c
Log:
	* osso-uri.c: (uri_get_desktop_file_by_scheme),
	(uri_get_desktop_file_action), (uri_set_defaults_file),
	(osso_uri_is_default_action), (osso_uri_is_default_action_by_uri),
	(osso_uri_get_default_action),
	(osso_uri_get_default_action_by_uri):

	* tests/test-all.c: Finished the tests for setting a default
	normal, neutral and fallback action and added 3 more tests for
	setting the 3 types of new action but by the old API and checking
	they are set with the new & old API.


Modified: projects/haf/trunk/libossomime/ChangeLog
===================================================================
--- projects/haf/trunk/libossomime/ChangeLog	2007-01-11 13:23:17 UTC (rev 9062)
+++ projects/haf/trunk/libossomime/ChangeLog	2007-01-11 13:24:13 UTC (rev 9063)
@@ -1,3 +1,15 @@
+2007-01-11  Martyn Russell  <martyn at imendio.com>
+
+	* tests/test-all.c: Finished the tests for setting a default
+	normal, neutral and fallback action and added 3 more tests for
+	setting the 3 types of new action but by the old API and checking
+	they are set with the new & old API.
+
+	* libossomime/osso-uri.c: Fixed 6 memory leaks with valgrind and
+	changed the way that we save the defaults file, now we remove any
+	instance of old the scheme in the NEW groups if saving a neutral
+	or fallback action.
+
 2007-01-09  Martyn Russell  <martyn at imendio.com>
 
 	* docs/mime-desktop-file-mapping.txt: Updated mistakes and

Modified: projects/haf/trunk/libossomime/libossomime/osso-uri.c
===================================================================
--- projects/haf/trunk/libossomime/libossomime/osso-uri.c	2007-01-11 13:23:17 UTC (rev 9062)
+++ projects/haf/trunk/libossomime/libossomime/osso-uri.c	2007-01-11 13:24:13 UTC (rev 9063)
@@ -63,8 +63,8 @@
 #define APP_LAUNCH_BANNER_METHOD            "app_launch_banner"
 
 
-/*  #define DEBUG_MSG(x)  */
-  #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");  
+#define DEBUG_MSG(x)
+/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");   */
 
 /* The ID is the group name in the desktop file for this
  * action, the domain is the translation domain used for the
@@ -124,6 +124,8 @@
 static gboolean          uri_get_desktop_file_by_scheme        (const gchar       *scheme,
 								gchar            **filename,
 								gchar            **action_name);
+static OssoURIAction *   uri_get_desktop_file_action           (const gchar       *scheme,
+								const gchar       *desktop_file_and_action);
 static gboolean          uri_set_defaults_file                 (const gchar       *scheme,
 								const gchar       *mime_type,
 								const gchar       *desktop_file,
@@ -1009,6 +1011,41 @@
 	return FALSE;
 }
 
+static OssoURIAction *   
+uri_get_desktop_file_action (const gchar *scheme,
+			     const gchar *desktop_file_and_action)
+{
+	OssoURIAction  *action = NULL;
+	gchar         **strv;
+	
+	g_return_val_if_fail (scheme != NULL, NULL);
+	g_return_val_if_fail (desktop_file_and_action != NULL, NULL);
+	
+	/* Should be in the format of '<desktop file>;<action name>' */
+	strv = g_strsplit (desktop_file_and_action, ":", -1);
+	
+	if (g_strv_length (strv) == 2) {
+		GSList        *actions, *l;
+		OssoURIAction *this_action;
+		
+		actions = uri_get_desktop_file_actions (strv[0], scheme);
+		for (l = actions; l && !action; l = l->next) {
+			this_action = l->data;
+			
+			if (strcmp (this_action->id, strv[1]) == 0) {
+				action = osso_uri_action_ref (this_action);
+			}
+		}
+		
+		g_slist_foreach (actions, (GFunc) osso_uri_action_unref, NULL);
+		g_slist_free (actions);
+	}
+	
+	g_strfreev (strv);
+
+	return action;
+}
+
 static gboolean
 uri_set_defaults_file (const gchar *scheme,
 		       const gchar *mime_type,
@@ -1035,7 +1072,22 @@
 					filename,
 					G_KEY_FILE_KEEP_COMMENTS, 
 					NULL);
+	if (ok && !mime_type) {
+		gchar *group;
 
+		/* First thing is first, we remove any exising NEW group with
+		 * same scheme, the reason for this is that if you have a
+		 * neutral action as the default it will always choose the
+		 * mime-type based action BEFORE the OLD group action.
+		 */
+		
+		group = g_strdup_printf (OSSO_URI_DEFAULTS_GROUP_FORMAT,
+					 scheme_lower);
+		g_key_file_remove_group (key_file, group, NULL);
+		
+		g_free (group);
+	}
+
 	if (desktop_file) {
 		if (mime_type && action_id) {
 			gchar *group;
@@ -1549,6 +1601,10 @@
 		g_free (desktop_file2);
 	}
 
+	if (default_action) {
+		osso_uri_action_unref (default_action);
+	}
+
 	return equal;
 }
 
@@ -1618,6 +1674,10 @@
 		g_free (desktop_file2);
 	}
 
+	if (default_action) {
+		osso_uri_action_unref (default_action);
+	}
+
 	return equal;
 }
 
@@ -1736,22 +1796,6 @@
 	    info->mime_type && 
 	    info->mime_type != '\0') {
 		mime_type = g_strdup (info->mime_type);
-	} else {
-		/* We fallback to the old function here because it is
-		 * used for neutral and fallback actions.
-		 */
-
-		DEBUG_MSG (("URI: **** Getting default action by falling back to old "
-			    "function just using scheme:'%s'", 
-			    scheme));
-
-		gnome_vfs_file_info_unref (info);
-		gnome_vfs_uri_unref (uri);
-		
-		action = osso_uri_get_default_action (scheme, error);
-		g_free (scheme);
-
-		return action;
 	}
 
 	DEBUG_MSG (("URI: Getting default action by uri:'%s', with scheme:'%s' and mime type'%s'", 
@@ -1776,48 +1820,46 @@
 	DEBUG_MSG (("URI: Getting default actions from file:'%s'", full_path));
 
 	if (ok) {
-		gchar **strv;
-		gchar  *str;
-		gchar  *group;
+		gchar *str;
 
-		str = strchr (mime_type, '/');
-		if (str) {
-			str[0] = '-';
-		}
+		if (mime_type) {
+			gchar *group;
 
-		group = g_strdup_printf (OSSO_URI_DEFAULTS_GROUP_FORMAT, scheme);
-		str = g_key_file_get_string (key_file, group, mime_type, NULL);
-		DEBUG_MSG (("URI: Found string:'%s' in group:'%s'", str, group));
-		g_free (group);
+			str = strchr (mime_type, '/');
+			if (str) {
+				str[0] = '-';
+			}
 
-		/* Should be in the format of '<desktop file>;<action name>' */
-		if (str) {
-			strv = g_strsplit (str, ":", 2);
-			g_free (str);
+			group = g_strdup_printf (OSSO_URI_DEFAULTS_GROUP_FORMAT, scheme);
+			str = g_key_file_get_string (key_file, group, mime_type, NULL);
+			DEBUG_MSG (("URI: Found string:'%s' in group:'%s'", str, group));
+			g_free (group);
 
-			if (g_strv_length (strv) == 2) {
-				GSList        *actions, *l;
-				OssoURIAction *this_action;
+			if (str) {
+				action = uri_get_desktop_file_action (scheme, str);
+				g_free (str);
+			}
+		}
 
-				actions = uri_get_desktop_file_actions (strv[0], scheme);
-				for (l = actions; l && !action; l = l->next) {
-					this_action = l->data;
+		if (!action) {
+			/* We fallback to the old function here because it is
+			 * used for neutral and fallback actions.
+			 */
+			
+			DEBUG_MSG (("URI: Getting default action by falling back to old "
+				    "function just using scheme:'%s'", 
+				    scheme));
 
-					if (strcmp (this_action->id, strv[1]) == 0) {
-						action = osso_uri_action_ref (this_action);
-					}
-				}
-
-				g_slist_foreach (actions, 
-						 (GFunc) osso_uri_action_unref, 
-						 NULL);
-				g_slist_free (actions);
+			str = g_key_file_get_string (key_file, OSSO_URI_DEFAULTS_GROUP, scheme, NULL);
+			if (str) {
+				action = uri_get_desktop_file_action (scheme, str);
+				g_free (str);
 			}
-
-			g_strfreev (strv);
 		}
 	}
 
+	g_key_file_free (key_file);
+
 	g_free (desktop_file);
 	g_free (filename);
 	g_free (mime_type);

Modified: projects/haf/trunk/libossomime/tests/test-all.c
===================================================================
--- projects/haf/trunk/libossomime/tests/test-all.c	2007-01-11 13:23:17 UTC (rev 9062)
+++ projects/haf/trunk/libossomime/tests/test-all.c	2007-01-11 13:24:13 UTC (rev 9063)
@@ -223,12 +223,8 @@
 static void
 test_system_default_actions (void)
 {
-	GSList        *actions;
-	GSList        *l;
-	OssoURIAction *default_action;
-	OssoURIAction *action = NULL;
-	const gchar   *uri_str;
-	gboolean       success;
+	GSList      *actions;
+	const gchar *uri_str;
 
 	print_header ("Testing default actions (system)");
 
@@ -278,7 +274,66 @@
 	actions = osso_uri_get_actions_by_uri ("http://www.imendio.com/sliff.sloff", OSSO_URI_ACTION_FALLBACK, NULL);
 	assert_int (g_slist_length (actions), 1);
 	osso_uri_free_actions (actions);
+}
 
+static void
+test_local_default_actions (void)
+{
+	GSList        *actions, *l;
+	OssoURIAction *action;
+	OssoURIAction *default_action;
+	const gchar   *uri_str;
+	gboolean       success;
+
+	/* FIXME: Implement. */
+	
+	print_header ("Testing default actions (local)");
+
+	/* Make sure any old local override is removed. */
+	unlink (TEST_DATADIR "-local/applications/uri-action-defaults.list");
+
+	/* The browser should be the default for http. */
+	g_print ("For http\n");
+	actions = osso_uri_get_actions_by_uri ("http://www.imendio.com", -1, NULL);
+	assert_int (g_slist_length (actions), 4);
+
+	/* The default. */
+	assert_bool (is_default_action (actions, "uri_link_open_link", NULL));
+
+	/* Override the default. */
+	action = NULL;
+	for (l = actions; l; l = l->next) {
+		action = l->data;
+
+		if (!osso_uri_is_default_action (action, NULL)) {
+			break;
+		}
+
+		action = NULL;
+	}
+
+	/* Note: If this fails, there's a bug in the test or the test data. */
+	if (!action) {
+		g_error ("Found no non-default action, test bug.");
+	}
+	
+	if (!osso_uri_set_default_action ("http", action, NULL)) {
+		g_error ("Couldn't set default...\n");
+	}
+
+	assert_bool (!is_default_action (actions, "http", NULL));
+	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), NULL));
+
+	/* Reset the default. */
+	if (!osso_uri_set_default_action ("http", NULL, NULL)) {
+		g_error ("Couldn't set default...\n");
+	}
+
+	/* We're back to the system default. */
+	assert_bool (is_default_action (actions, "uri_link_open_link", NULL));
+	
+	osso_uri_free_actions (actions);
+
 	/*
 	 * Test getting and setting default actions 
 	 */
@@ -293,7 +348,7 @@
 
 	/* Test setting a NORMAL action */
 	uri_str = "http://www.google.co.uk/intl/en_uk/images/logo.gif";
-	l = actions = osso_uri_get_actions_by_uri (uri_str, -1, NULL);
+	actions = osso_uri_get_actions_by_uri (uri_str, -1, NULL);
 
 	for (l = actions; l; l = l->next) {
 		action = l->data;
@@ -318,7 +373,30 @@
 	osso_uri_action_unref (action);
 
 	/* Test setting a NEUTRAL action */ 
+	actions = osso_uri_get_actions_by_uri (uri_str, OSSO_URI_ACTION_NEUTRAL, NULL);
 
+	for (l = actions; l; l = l->next) {
+		action = l->data;
+
+		if (strcmp (osso_uri_action_get_name (action), "uri_link_save_link") == 0) {
+			break;
+		} 
+
+		action = NULL;
+	}
+
+	assert_expr (action != NULL);
+
+	osso_uri_action_ref (action);
+	osso_uri_free_actions (actions);
+	
+	success = osso_uri_set_default_action_by_uri (uri_str, action, NULL);
+	assert_bool (success);
+
+	actions = osso_uri_get_actions_by_uri (uri_str, -1, NULL);
+	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), uri_str));
+	osso_uri_action_unref (action);
+
 	/* Test setting a FALLBACK action */ 
 	uri_str = "http://www.imendio.com/sliff.sloff";
 	actions = osso_uri_get_actions_by_uri (uri_str, OSSO_URI_ACTION_FALLBACK, NULL);
@@ -337,66 +415,81 @@
 	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), uri_str));
 	osso_uri_action_unref (action);
 
+	/* Clean up for new set of tests */
+	unlink (TEST_DATADIR "-local/applications/uri-action-defaults.list");
+
 	/* Test setting a new NORMAL action with the old API */ 
-	/* Test setting a new NEUTRAL action with the old API */ 
-	/* Test setting a new FALLBACK action with the old API */ 
-}
+	uri_str = "http://www.google.co.uk/intl/en_uk/images/logo.gif";
+	actions = osso_uri_get_actions_by_uri (uri_str, -1, NULL);
 
-static void
-test_local_default_actions (void)
-{
-	GSList        *actions, *l;
-	OssoURIAction *action;
+	for (l = actions; l; l = l->next) {
+		action = l->data;
 
-	/* FIXME: Implement. */
-	
-	print_header ("Testing default actions (local)");
+		if (strcmp (osso_uri_action_get_name (action), "addr_ap_address_book") == 0) {
+			break;
+		} 
 
-	/* Make sure any old local override is removed. */
-	unlink (TEST_DATADIR "-local/applications/uri-action-defaults.list");
+		action = NULL;
+	}
 
-	/* The browser should be the default for http. */
-	g_print ("For http\n");
-	actions = osso_uri_get_actions_by_uri ("http://www.imendio.com", -1, NULL);
-	assert_int (g_slist_length (actions), 4);
+	assert_expr (action != NULL);
 
-	/* The default. */
-	assert_bool (is_default_action (actions, "uri_link_open_link", NULL));
+	osso_uri_action_ref (action);
+	osso_uri_free_actions (actions);
 
-	/* Override the default. */
-	action = NULL;
+	success = osso_uri_set_default_action ("http", action, NULL);
+	assert_bool (success);
+
+	actions = osso_uri_get_actions_by_uri (uri_str, -1, NULL);
+	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), uri_str));
+	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), NULL));
+	osso_uri_action_unref (action);
+	
+	/* Test setting a new NEUTRAL action with the old API */ 
+	actions = osso_uri_get_actions_by_uri (uri_str, OSSO_URI_ACTION_NEUTRAL, NULL);
+
 	for (l = actions; l; l = l->next) {
 		action = l->data;
 
-		if (!osso_uri_is_default_action (action, NULL)) {
+		if (strcmp (osso_uri_action_get_name (action), "uri_link_save_link") == 0) {
 			break;
-		}
+		} 
 
 		action = NULL;
 	}
 
-	/* Note: If this fails, there's a bug in the test or the test data. */
-	if (!action) {
-		g_error ("Found no non-default action, test bug.");
-	}
+	assert_expr (action != NULL);
+
+	osso_uri_action_ref (action);
+	osso_uri_free_actions (actions);
 	
-	if (!osso_uri_set_default_action ("http", action, NULL)) {
-		g_error ("Couldn't set default...\n");
-	}
+	success = osso_uri_set_default_action ("http", action, NULL);
+	assert_bool (success);
 
-	assert_bool (!is_default_action (actions, "http", NULL));
+	actions = osso_uri_get_actions_by_uri (uri_str, -1, NULL);
+	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), uri_str));
 	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), NULL));
+	osso_uri_action_unref (action);
 
-	/* Reset the default. */
-	if (!osso_uri_set_default_action ("http", NULL, NULL)) {
-		g_error ("Couldn't set default...\n");
-	}
+	/* Test setting a new FALLBACK action with the old API */ 
+	uri_str = "http://www.imendio.com/sliff.sloff";
+	actions = osso_uri_get_actions_by_uri (uri_str, OSSO_URI_ACTION_FALLBACK, NULL);
+	assert_int (g_slist_length (actions), 1);
 
-	/* We're back to the system default. */
-	assert_bool (is_default_action (actions, "uri_link_open_link", NULL));
+	action = actions->data;
+	assert_string (osso_uri_action_get_name (action), "uri_link_open_link_fallback");
+
+	osso_uri_action_ref (action);
+	osso_uri_free_actions (actions);
 	
-	osso_uri_free_actions (actions);
+	success = osso_uri_set_default_action ("http", action, NULL);
+	assert_bool (success);
 
+	actions = osso_uri_get_actions_by_uri (uri_str, -1, NULL);
+	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), uri_str));
+	assert_bool (is_default_action (actions, osso_uri_action_get_name (action), NULL));
+	osso_uri_action_unref (action);
+
 	/* Clean up. */
 	unlink (TEST_DATADIR "-local/applications/uri-action-defaults.list");
 }
@@ -408,11 +501,6 @@
 	GError   *error = NULL;
 	gboolean  ret;
 
-	if (!gnome_vfs_init()) {
-		g_error ("Could not initialise GnomeVFS");
-		return 1;
-	}
-
 	/* Use our custom data here. */
 	g_setenv ("XDG_DATA_DIRS", TEST_DATADIR, TRUE);
 	g_setenv ("XDG_DATA_HOME", TEST_DATADIR "-local", TRUE);
@@ -450,6 +538,11 @@
 		return 1;
 	}
 
+	if (!gnome_vfs_init()) {
+		g_error ("Could not initialise GnomeVFS");
+		return 1;
+	}
+
 	if (0) {	
 		test_get_mime_types ();
 		test_get_actions ();
@@ -458,6 +551,8 @@
 	test_system_default_actions ();
 	test_local_default_actions ();
 
+	gnome_vfs_shutdown ();
+
 	return 0;
 }
 


More information about the maemo-commits mailing list