[maemo-commits] [maemo-commits] r9014 - in projects/haf/trunk/libossomime: . docs libossomime tests tests/datadir/applications tests/datadir/applications/test

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Tue Jan 9 20:07:57 EET 2007
Author: martyn
Date: 2007-01-09 20:07:54 +0200 (Tue, 09 Jan 2007)
New Revision: 9014

Modified:
   projects/haf/trunk/libossomime/ChangeLog
   projects/haf/trunk/libossomime/docs/mime-desktop-file-mapping.txt
   projects/haf/trunk/libossomime/libossomime/osso-uri.c
   projects/haf/trunk/libossomime/libossomime/osso-uri.h
   projects/haf/trunk/libossomime/tests/datadir/applications/test/osso-addressbook.desktop
   projects/haf/trunk/libossomime/tests/datadir/applications/uri-action-defaults.list
   projects/haf/trunk/libossomime/tests/test-all.c
Log:
	* osso-uri.c: (uri_get_desktop_file_actions),
	(uri_get_desktop_file_info), (uri_get_desktop_file_by_filename),
	(uri_get_desktop_file_by_scheme), (uri_set_defaults_file),
	(osso_uri_get_scheme_from_uri), (osso_uri_is_default_action),
	(osso_uri_is_default_action_by_uri), (osso_uri_get_default_action),
	(osso_uri_get_default_action_by_uri),
	(osso_uri_set_default_action),
	(osso_uri_set_default_action_by_uri), (osso_uri_open):
	* osso-uri.h:

	* docs/mime-desktop-file-mapping.txt: Updated mistakes and
	additions to libossomime I added today.

	* tests/datadir/applications/test/osso-addressbook.desktop:

	* tests/datadir/applications/uri-action-defaults.list: 
	* tests/test-all.c: Test getting and setting the default action
	for special types of actions (like neutral and fallback actions).


Modified: projects/haf/trunk/libossomime/ChangeLog
===================================================================
--- projects/haf/trunk/libossomime/ChangeLog	2007-01-09 18:04:00 UTC (rev 9013)
+++ projects/haf/trunk/libossomime/ChangeLog	2007-01-09 18:07:54 UTC (rev 9014)
@@ -1,3 +1,21 @@
+2007-01-09  Martyn Russell  <martyn at imendio.com>
+
+	* docs/mime-desktop-file-mapping.txt: Updated mistakes and
+	additions to libossomime I added today.
+
+	* libossomime/osso-uri.[ch]: Updated API to include
+	osso_uri_is_default_action_by_uri(). Also fixed
+	osso_uri_get_default_action_by_uri() so that it worked with
+	neutral or fallback actions by using the old defaults file group
+	(because mime type is immaterial in these cases) and add to it the
+	action id so we know WHICH action in a desktop file to use.
+	
+	* tests/datadir/applications/test/osso-addressbook.desktop:
+
+	* tests/datadir/applications/uri-action-defaults.list: 
+	* tests/test-all.c: Test getting and setting the default action
+	for special types of actions (like neutral and fallback actions). 
+
 2007-01-04  Martyn Russell  <martyn at imendio.com>
 
 	* libossomime/osso-uri.c: Don't error if NULL is given for

Modified: projects/haf/trunk/libossomime/docs/mime-desktop-file-mapping.txt
===================================================================
--- projects/haf/trunk/libossomime/docs/mime-desktop-file-mapping.txt	2007-01-09 18:04:00 UTC (rev 9013)
+++ projects/haf/trunk/libossomime/docs/mime-desktop-file-mapping.txt	2007-01-09 18:07:54 UTC (rev 9014)
@@ -77,20 +77,36 @@
 for older desktop file. It will look like this (showing old and new
 formats together): 
 
-	[X-Osso-URI-Scheme <mime type>]
-	<scheme>=<desktop file>:<action name>
+	[X-Osso-URI-Scheme <scheme>]
+	<mime type>=<desktop file>:<action name>
 
 An example of new and old formats together would look like:
 
 	# For supporting older actions
 	[Default Actions]
-	https=hildon-browser;
+	https=hildon-browser.desktop;
 
 	# For new actions
 	[X-Osso-URI-Scheme http]
-	image-png=hildon-browser:X-Osso-URI-Action-Open
-	image-jpeg=hildon-image-viewer:X-Osso-URI-Action-Open
+	image-png=hildon-browser.desktop:X-Osso-URI-Action-Open
+	image-jpeg=hildon-image-viewer.desktop:X-Osso-URI-Action-Open
 
+In addition to this any fallback actions will use the old [Default
+Actions] group because you only know the scheme in that case. To make
+sure that this doesn't break existing defaults files, the action name
+is added to the end of the desktop file, for example:
+ 
+	[Default Actions]
+	<scheme>=<desktop file>:<action name> 
+
+This means it would look like this:
+
+        [Default Actions]
+        http=test/browser.desktop:X-Osso-URI-Action-Fallback
+
+        [X-Osso-URI-Scheme http]
+        image-gif=test/osso-addressbook.desktop:X-Osso-URI-Action-Add-Bookmark
+
 Examples:
 =========
 

Modified: projects/haf/trunk/libossomime/libossomime/osso-uri.c
===================================================================
--- projects/haf/trunk/libossomime/libossomime/osso-uri.c	2007-01-09 18:04:00 UTC (rev 9013)
+++ projects/haf/trunk/libossomime/libossomime/osso-uri.c	2007-01-09 18:07:54 UTC (rev 9014)
@@ -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
@@ -117,10 +117,13 @@
 								const gchar       *scheme);
 
 /* Defaults file */
-static gchar *           uri_get_desktop_file_by_filename      (const gchar       *filename,
-								const gchar       *scheme);
-
-static gchar *           uri_get_desktop_file_by_scheme        (const gchar       *scheme);
+static gboolean          uri_get_desktop_file_by_filename      (const gchar       *filename,
+								const gchar       *scheme,
+								gchar            **desktop_file,
+								gchar            **action_name);
+static gboolean          uri_get_desktop_file_by_scheme        (const gchar       *scheme,
+								gchar            **filename,
+								gchar            **action_name);
 static gboolean          uri_set_defaults_file                 (const gchar       *scheme,
 								const gchar       *mime_type,
 								const gchar       *desktop_file,
@@ -523,6 +526,8 @@
 					     G_KEY_FILE_KEEP_COMMENTS, 
 					     NULL);
 	if (!ok) {
+		DEBUG_MSG (("URI: Could not load filename:'%s' from data dirs",
+			    filename));
 		goto finish;
 	}
 
@@ -559,6 +564,8 @@
 						    scheme_lower,
 						    NULL);
 	} else {
+		DEBUG_MSG (("URI: Could not find scheme:'%s' in filename:'%s'",
+			    scheme_lower, filename));
 		goto finish;
 	}
 
@@ -599,6 +606,11 @@
 			if (!mime_type) {
 				mime_type = g_strdup (parent_mime_type);
 			}
+
+			/* If still no mime type, we must be neutral */
+			if (!mime_type) {
+				type = OSSO_URI_ACTION_NEUTRAL;
+			}
 		}
 		
 		name = g_key_file_get_string (key_file, strv[i],
@@ -864,14 +876,21 @@
  * Defaults file
  */
 
-static gchar *
-uri_get_desktop_file_by_filename (const gchar *filename, 
-				  const gchar *scheme)
+static gboolean
+uri_get_desktop_file_by_filename (const gchar  *filename, 
+				  const gchar  *scheme,
+				  gchar       **desktop_file,
+				  gchar       **action_id)
 {
 	GKeyFile *key_file;
-	gchar    *desktop_file = NULL;
 	gchar    *scheme_lower;
 
+	g_return_val_if_fail (desktop_file != NULL, FALSE);
+	g_return_val_if_fail (action_id != NULL, FALSE);
+	
+	*desktop_file = NULL;
+	*action_id = NULL;
+
 	scheme_lower = g_ascii_strdown (scheme, -1);
 
 	DEBUG_MSG (("URI: Checking for scheme:'%s' in defaults file:'%s'", 
@@ -888,27 +907,36 @@
 					     scheme_lower, 
 					     NULL);
 		if (str) {
+			gchar *p;
+
 			strv = g_strsplit (str, ";", -1);
 			if (strv) {
-				desktop_file = g_strdup (strv[0]);
+				*desktop_file = g_strdup (strv[0]);
 				g_strfreev (strv);
 				g_free (str);
 			} else {
-				desktop_file = str;
+				*desktop_file = str;
 			}
+
+			p = strchr (*desktop_file, ':');
+			if (p) {
+				*action_id = g_strdup (p + 1);
+				p[0] = '\0';
+			}
 		}
 	}
 
 	g_key_file_free (key_file);
 	g_free (scheme_lower);
 
-	return desktop_file;
+	return *desktop_file != NULL;
 }
 
-static gchar *
-uri_get_desktop_file_by_scheme (const gchar *scheme)
+static gboolean
+uri_get_desktop_file_by_scheme (const gchar *scheme,
+				gchar      **desktop_file,
+				gchar      **action_id)
 {
-	gchar              *desktop_file = NULL;
 	gchar              *filename;
 	gchar              *full_filename;
 	const gchar        *user_data_dir;
@@ -917,6 +945,12 @@
 	gchar              *scheme_lower;
 	gint                i = 0;
 
+	g_return_val_if_fail (desktop_file != NULL, FALSE);
+	g_return_val_if_fail (action_id != NULL, FALSE);
+
+	*desktop_file = NULL;
+	*action_id = NULL;
+
  	scheme_lower = g_ascii_strdown (scheme, -1);
 
 	/* If we use g_key_file_load_from_data_dirs() here then it
@@ -933,17 +967,16 @@
 	
 	/* Checking user dir ($home/.local/share/applications/...) first */
 	full_filename = g_build_filename (user_data_dir, filename, NULL);
-	desktop_file = uri_get_desktop_file_by_filename (full_filename, scheme_lower);
-
-	if (desktop_file) {
-		DEBUG_MSG (("URI: Found scheme:'%s' matches desktop file:'%s' in defaults file:'%s'",
-			    scheme_lower, desktop_file, full_filename));
+	if (uri_get_desktop_file_by_filename (full_filename, scheme_lower, desktop_file, action_id)) {
+		DEBUG_MSG (("URI: Found scheme:'%s' matches desktop file:'%s' with "
+			    "action:'%s' in defaults file:'%s'",
+			    scheme_lower, *desktop_file, *action_id, full_filename));
 		
 		g_free (full_filename);
 		g_free (filename);
 		g_free (scheme_lower);
 
-		return desktop_file;
+		return TRUE;
 	} 
 
 	g_free (full_filename);
@@ -951,17 +984,17 @@
 	/* Checking system dirs ($prefix/share/applications/..., etc) second */
 	while ((dir = system_data_dirs[i++]) != NULL) {
 		full_filename = g_build_filename (dir, filename, NULL);
-		desktop_file = uri_get_desktop_file_by_filename (full_filename, scheme_lower);
+
+		if (uri_get_desktop_file_by_filename (full_filename, scheme_lower, desktop_file, action_id)) {
+			DEBUG_MSG (("URI: Found scheme:'%s' matches desktop file:'%s' "
+				    "with action:'%s' in defaults file:'%s'",
+				    scheme_lower, *desktop_file, *action_id, full_filename));
 		
-		if (desktop_file) {
-			DEBUG_MSG (("URI: Found scheme:'%s' matches desktop file:'%s' in defaults file:'%s'",
-				    scheme_lower, desktop_file, full_filename));
-		
 			g_free (full_filename);
 			g_free (filename);
 			g_free (scheme_lower);
 
-			return desktop_file;
+			return TRUE;
 		}
 
 		g_free (full_filename);
@@ -973,7 +1006,7 @@
 	g_free (filename);
 	g_free (scheme_lower);
 
-	return NULL;
+	return FALSE;
 }
 
 static gboolean
@@ -1033,14 +1066,23 @@
 			g_free (key);
 			g_free (group);
 		} else {
+			gchar *value;
+
 			/* Old scheme */
-			DEBUG_MSG (("URI: Added default desktop file:'%s' for "
-				    "scheme:'%s', (old method)",
-				    desktop_file, scheme_lower));
+			DEBUG_MSG (("URI: Added default desktop file:'%s' with "
+				    "action id:'%s' for scheme:'%s', (old method)",
+				    desktop_file, action_id, scheme_lower));
+
+			value = g_strdup_printf ("%s%s%s", 
+						 desktop_file, 
+						 action_id ? ":" : "",
+						 action_id ? action_id : "");
+
 			g_key_file_set_string (key_file, 
 					       OSSO_URI_DEFAULTS_GROUP,
 					       scheme_lower, 
-					       desktop_file);
+					       value);
+			g_free (value);
 		}
 	} else if (ok) {
 		DEBUG_MSG (("URI: Remove default for scheme:'%s'",
@@ -1443,6 +1485,15 @@
 	g_return_val_if_fail (action != NULL, FALSE);
 	g_return_val_if_fail (action->scheme != NULL, FALSE);
 
+	/* If new type of action we only need to make sure we have the
+	 * scheme to go on and we can then look for the default action
+	 * for that scheme in the old group ('Default Actions' in the
+	 * defaults file).
+	 */
+	if (action->id && !action->scheme) {
+		return FALSE;
+	}
+
 	default_action = osso_uri_get_default_action (action->scheme, error);
 
 	DEBUG_MSG (("URI: Checking desktop file is default for scheme:'%s':\n"
@@ -1501,14 +1552,100 @@
 	return equal;
 }
 
+gboolean
+osso_uri_is_default_action_by_uri (const gchar    *uri,
+				   OssoURIAction  *action,
+				   GError        **error)
+{
+	OssoURIAction *default_action;
+	gboolean       equal = FALSE;
+
+	g_return_val_if_fail (uri != NULL, FALSE);
+	g_return_val_if_fail (action != NULL, FALSE);
+
+	default_action = osso_uri_get_default_action_by_uri (uri, error);
+
+	DEBUG_MSG (("URI: Checking desktop file is default for scheme:'%s':\n"
+		    "\tdefault_action:%p\n"
+		    "\tdefault_action->desktop_file:'%s'\n"
+		    "\taction->desktop_file:'%s'",
+		    action->scheme,
+		    default_action,
+		    default_action ? default_action->desktop_file : "",
+		    action->desktop_file))
+
+	if (default_action && 
+	    default_action->desktop_file && 
+	    action->desktop_file) {
+		gchar *desktop_file1;
+		gchar *desktop_file2;
+
+		/* Change all "/" to "-" since there may be some differences here */
+		desktop_file1 = g_strdup (default_action->desktop_file);
+		g_strdelimit (desktop_file1, G_DIR_SEPARATOR_S, '-');
+		desktop_file2 = g_strdup (action->desktop_file);
+		g_strdelimit (desktop_file2, G_DIR_SEPARATOR_S, '-');
+
+		/* Compare desktop files to know if this is the same
+		 * action as the default action.
+		 * 
+		 * We compare the desktop file and the method since
+		 * these two are what the dbus service will use to
+		 * identify what is done with the action. 
+		 */
+		if (default_action->name && action->name &&
+		    default_action->method && action->method &&
+		    strcmp (desktop_file1, desktop_file2) == 0 && 
+		    strcmp (default_action->name, action->name) == 0 && 
+		    strcmp (default_action->method, action->method) == 0) {
+			equal = TRUE;
+		}
+		
+		DEBUG_MSG (("URI: Checking desktop file is default:\n"
+			    "\tfile1:'%s'\n"
+			    "\tfile2:'%s'\n"
+			    "\tname1:'%s'\n"
+			    "\tname2:'%s'\n"
+			    "\tmethod1:'%s'\n"
+			    "\tmethod2:'%s'\n"
+			    "\tEQUAL = %s",
+			    desktop_file1, desktop_file2,
+			    default_action->name, action->name,
+			    default_action->method, action->method,
+			    equal ? "YES" : "NO"))
+
+		g_free (desktop_file1);
+		g_free (desktop_file2);
+	}
+
+	return equal;
+}
+
 OssoURIAction *  
 osso_uri_get_default_action (const gchar  *scheme,
 			     GError      **error)
 {
-	GSList        *actions_found;
+	GSList        *actions;
+	GSList        *l;
 	OssoURIAction *action = NULL;
-	gchar         *filename;
+	gchar         *desktop_file;
+	gchar         *action_id;
 
+	/* We do some interesting stuff here. Before we had the new
+	 * type of desktop files for v1.0 we didn't have the action
+	 * name in the defaults file, but now we do so that we can use
+	 * the same group in the file as we did before but for neutral
+	 * and fallback actions because they have no need for checking
+	 * against mime type. The new format here is:
+	 *   
+	 *   [Default Actions]
+	 *   <scheme>=<desktop file>:<action name>
+	 * 
+	 * Previously the action name and the colon were not present
+	 * so this new method means that old defaults files should
+	 * still works.
+	 */
+
 	if (!scheme) {
 		g_set_error (error,
 			     OSSO_URI_ERROR,
@@ -1517,17 +1654,41 @@
 		return NULL;
 	}
 
-	filename = uri_get_desktop_file_by_scheme (scheme);
-	if (!filename) {
+	if (!uri_get_desktop_file_by_scheme (scheme, &desktop_file, &action_id)) {
+		DEBUG_MSG (("URI: No desktop file found for scheme:'%s'", scheme));
+		g_free (desktop_file);
+		g_free (action_id);
 		return NULL;
 	}
 
-	actions_found = uri_get_desktop_file_actions (filename, scheme);
-	action = osso_uri_action_ref (actions_found->data);
-	g_slist_foreach (actions_found, (GFunc) osso_uri_action_unref, NULL);
-	g_slist_free (actions_found);
-	g_free (filename);
+	actions = uri_get_desktop_file_actions (desktop_file, scheme);
 
+	for (l = actions; l && action_id; l = l->next) {
+		action = l->data;
+			
+		if (action->id && strcmp (action->id, action_id) == 0) {
+			break;
+		}
+		
+		action = NULL;
+	}
+		
+	/* If name not found, default to the first item */
+	if (!action) {
+		DEBUG_MSG (("URI: Using first action as default action"));
+		action = actions->data;
+	}
+	
+	if (action) {
+		osso_uri_action_ref (action);
+	}
+
+	g_slist_foreach (actions, (GFunc) osso_uri_action_unref, NULL);
+	g_slist_free (actions);
+
+	g_free (action_id);
+	g_free (desktop_file);
+
 	return action;
 }
 
@@ -1544,6 +1705,7 @@
 	gchar            *mime_type = NULL;
 	gchar            *desktop_file = NULL;
 	gchar            *filename;
+	gchar            *full_path = NULL;
 	gboolean          ok;
 
 	uri = gnome_vfs_uri_new (uri_str);
@@ -1563,7 +1725,6 @@
 			     OSSO_URI_ERROR,
 			     OSSO_URI_INVALID_URI,
 			     "The scheme could not be obtained from the uri.");
-
 		return NULL;
 	}
 
@@ -1576,15 +1737,21 @@
 	    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);
-		g_set_error (error,
-			     OSSO_URI_ERROR,
-			     OSSO_URI_INVALID_URI,
-			     "The mime type could not be obtained from the uri.");
 
-		return NULL;
+		return action;
 	}
 
 	DEBUG_MSG (("URI: Getting default action by uri:'%s', with scheme:'%s' and mime type'%s'", 
@@ -1602,10 +1769,12 @@
 
 	ok = g_key_file_load_from_data_dirs (key_file, 
 					     filename, 
-					     NULL, 
+					     &full_path, 
 					     G_KEY_FILE_KEEP_COMMENTS, 
 					     NULL);
 
+	DEBUG_MSG (("URI: Getting default actions from file:'%s'", full_path));
+
 	if (ok) {
 		gchar **strv;
 		gchar  *str;
@@ -1618,6 +1787,7 @@
 
 		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);
 
 		/* Should be in the format of '<desktop file>;<action name>' */
@@ -1662,6 +1832,7 @@
 			     GError        **error)
 {
 	const gchar *desktop_file = NULL;
+	const gchar *action_id = NULL;
 	gchar       *scheme_lower;
 	gboolean     ok;
 
@@ -1681,7 +1852,11 @@
 		desktop_file = action->desktop_file;
 	}
 
-	ok = uri_set_defaults_file (scheme_lower, NULL, desktop_file, NULL);
+	if (action && action->id && action->id[0] != '\0') {
+		action_id = action->id;
+	}
+
+	ok = uri_set_defaults_file (scheme_lower, NULL, desktop_file, action_id);
 	if (!ok) {
 		g_set_error (error,
 			     OSSO_URI_ERROR,
@@ -1708,6 +1883,15 @@
 	const gchar      *action_id = NULL;
 	gboolean          ok;
 
+	/* If we are a neutral or fallback action we just use the old
+	 * API because that only needs the scheme.
+	 */
+	if (action && 
+	    (action->type == OSSO_URI_ACTION_NEUTRAL || 
+	     action->type == OSSO_URI_ACTION_FALLBACK)) {
+		return osso_uri_set_default_action (action->scheme, action, error);
+	}
+
 	uri = gnome_vfs_uri_new (uri_str);
 	if (!uri) {
 		g_set_error (error,
@@ -1813,7 +1997,12 @@
 			    "since no action was given.",
 			    uri));
 		
-		action = osso_uri_get_default_action (scheme, error);
+		if (action->id) {
+			action = osso_uri_get_default_action_by_uri (uri, error);
+		} else {
+			action = osso_uri_get_default_action (scheme, error);
+		}
+
 		cleanup_action = TRUE;
 		
 		if (!action) {

Modified: projects/haf/trunk/libossomime/libossomime/osso-uri.h
===================================================================
--- projects/haf/trunk/libossomime/libossomime/osso-uri.h	2007-01-09 18:04:00 UTC (rev 9013)
+++ projects/haf/trunk/libossomime/libossomime/osso-uri.h	2007-01-09 18:07:54 UTC (rev 9014)
@@ -35,6 +35,12 @@
  *   - osso_uri_get_actions_by_uri()
  *   - osso_uri_get_default_action_by_uri()
  *   - osso_uri_set_default_action_by_uri()
+ *   - osso_uri_is_default_action_by_uri()
+ * 
+ * Considerations:
+ *   - Finding out if we have the default action by uri may be ok but
+ *     really we should do it by mime type and scheme, that makes more
+ *     sense to me.
  */
 
 #define OSSO_URI_ERROR osso_uri_error_quark()
@@ -250,6 +256,20 @@
 
 
 /**
+ * osso_uri_action_is_default_action_by_uri:
+ * @uri: A %const @gchar pointer to a URI.
+ * @action: A @OssoURIAction pointer. 
+ * @error: The address of a pointer to a @GError structure. This is
+ * optional and can be %NULL. 
+ *
+ * Return: The %TRUE if it is the default action or %FALSE.
+ **/
+gboolean           osso_uri_is_default_action_by_uri      (const gchar        *uri,
+							   OssoURIAction      *action,
+							   GError            **error);
+
+
+/**
  * osso_uri_action_get_default_action:
  * @scheme: A string which represents a scheme. 
  * @error: The address of a pointer to a @GError structure. This is
@@ -351,9 +371,9 @@
  *
  * Return: %TRUE if it was successfully set or %FALSE.
  **/
-gboolean  osso_uri_set_default_action_by_uri (const gchar    *uri_str,
-					      OssoURIAction  *action,
-					      GError        **error);
+gboolean           osso_uri_set_default_action_by_uri     (const gchar    *uri_str,
+							   OssoURIAction  *action,
+							   GError        **error);
 
 /**
  * osso_uri_open:

Modified: projects/haf/trunk/libossomime/tests/datadir/applications/test/osso-addressbook.desktop
===================================================================
--- projects/haf/trunk/libossomime/tests/datadir/applications/test/osso-addressbook.desktop	2007-01-09 18:04:00 UTC (rev 9013)
+++ projects/haf/trunk/libossomime/tests/datadir/applications/test/osso-addressbook.desktop	2007-01-09 18:07:54 UTC (rev 9014)
@@ -8,12 +8,13 @@
 X-Osso-Service=osso_addressbook
 Icon=qgn_list_addressbook
 StartupWMClass=osso-addressbook
-MimeType=text/x-vcard
+MimeType=text/x-vcard;text/plain;text/html;image/gif;
 
 [X-Osso-URI-Actions]
 mailto=X-Osso-URI-Action-Add-Contact;
 xmpp=X-Osso-URI-Action-Add-Account;
 sipto=X-Osso-URI-Action-Add-Account;
+http=X-Osso-URI-Action-Add-Bookmark;
 
 [X-Osso-URI-Action-Add-Contact]
 Method=add_account
@@ -24,3 +25,8 @@
 Method=add_account
 Name=addr_ap_address_book
 TranslationDomain=osso-addressbook
+
+[X-Osso-URI-Action-Add-Bookmark]
+Method=add_bookmark
+Name=addr_ap_address_book
+TranslationDomain=osso-addressbook

Modified: projects/haf/trunk/libossomime/tests/datadir/applications/uri-action-defaults.list
===================================================================
--- projects/haf/trunk/libossomime/tests/datadir/applications/uri-action-defaults.list	2007-01-09 18:04:00 UTC (rev 9013)
+++ projects/haf/trunk/libossomime/tests/datadir/applications/uri-action-defaults.list	2007-01-09 18:07:54 UTC (rev 9014)
@@ -7,3 +7,7 @@
 sipto=test-osso-addressbook.desktop
 mailto=test-email-engine.desktop
 chatto=test-chat-ui.desktop
+
+# For new actions
+[X-Osso-URI-Scheme http]
+text-html=test-browser.desktop:X-Osso-URI-Action-Open

Modified: projects/haf/trunk/libossomime/tests/test-all.c
===================================================================
--- projects/haf/trunk/libossomime/tests/test-all.c	2007-01-09 18:04:00 UTC (rev 9013)
+++ projects/haf/trunk/libossomime/tests/test-all.c	2007-01-09 18:07:54 UTC (rev 9014)
@@ -11,6 +11,72 @@
 #include <libgnomevfs/gnome-vfs.h>
 #include <osso-mime.h>
 
+#define assert_int(a, b) G_STMT_START {				        \
+	if (a != b) {						        \
+		g_log ("ASSERT INT",					\
+		       G_LOG_LEVEL_ERROR,				\
+		       "%s:%d: failed: (%s), expected %d, got %d",	\
+		       __FILE__,					\
+		       __LINE__,					\
+		       #a,						\
+		       a,						\
+		       b);						\
+	}								\
+	} G_STMT_END			
+
+#define assert_bool(a) G_STMT_START {					\
+	if (!a) {   					                \
+		g_log ("ASSERT BOOL",					\
+		       G_LOG_LEVEL_ERROR,				\
+		       "%s:%d: failed: (%s), expected to be TRUE",	\
+		       __FILE__,					\
+		       __LINE__,					\
+		       #a);						\
+	}						           	\
+	} G_STMT_END			
+
+#define assert_expr(a) G_STMT_START {					\
+	if (!(a)) {						        \
+		g_log ("ASSERT EXPR",					\
+		       G_LOG_LEVEL_ERROR,				\
+		       "%s:%d: failed: (%s)",				\
+		       __FILE__,					\
+		       __LINE__,					\
+		       #a);						\
+	}								\
+	} G_STMT_END			
+
+#define assert_string(a, b) G_STMT_START {				\
+	if (!a && b) {						        \
+		g_log ("ASSERT SRTING",					\
+		       G_LOG_LEVEL_ERROR,				\
+		       "%s:%d: failed: expected 'a' to be non-NULL",	\
+		       __FILE__,					\
+		       __LINE__);					\
+	} else if (a && !b) {						\
+		g_log ("ASSERT STRING",					\
+		       G_LOG_LEVEL_ERROR,				\
+		       "%s:%d: failed: expected 'b' to be non-NULL",	\
+		       __FILE__,					\
+		       __LINE__);					\
+	} else if (!a && !b) {						\
+		g_log ("ASSERT STRING",					\
+		       G_LOG_LEVEL_ERROR,				\
+		       "%s:%d: failed: 'a' & 'b' are NULL",		\
+		       __FILE__,					\
+		       __LINE__);					\
+	} else if (strcmp (a, b) != 0) {				\
+		g_log ("ASSERT STRING",					\
+		       G_LOG_LEVEL_ERROR,				\
+		       "%s:%d: failed: (%s), expected '%s', got '%s'",	\
+		       __FILE__,					\
+		       __LINE__,					\
+		       #a,						\
+		       a,						\
+		       b);						\
+	}								\
+	} G_STMT_END			
+
 static void
 print_header (const gchar *str)
 {
@@ -25,30 +91,6 @@
 }
 
 static void
-assert_int (int a, int b)
-{
-	if (a != b) {
-		g_error ("Got %d, expected %d\n", a, b);
-	}
-}
-
-static void
-assert_bool (gboolean a)
-{
-	if (!a) {
-		g_error ("Got FALSE, expected TRUE\n");
-	}
-}
-
-static void
-assert_string (const gchar *a, const gchar *b)
-{
-	if (strcmp (a, b) != 0) {
-		g_error ("Got %s, expected %s\n", a, b);
-	}
-}
-
-static void
 test_get_mime_types (void)
 {
 	GList *mimes;
@@ -146,16 +188,33 @@
 }
 
 static gboolean
-is_default_action (GSList *actions, const gchar *action_name)
+is_default_action (GSList      *actions, 
+		   const gchar *action_name, 
+		   const gchar *uri)
 {
 	GSList        *l;
 	OssoURIAction *action;
 
-	for (l = actions; l; l = l->next) {
-		action = l->data;
-		if (strcmp (osso_uri_action_get_name (action), action_name) == 0) {
-			return osso_uri_is_default_action (action, NULL);
+	/* NOTE: uri is only used for the new API calls */
+
+	if (!action_name) {
+		return FALSE;
+	}
+
+	if (uri) {
+		for (l = actions; l; l = l->next) {
+			action = l->data;
+			if (strcmp (osso_uri_action_get_name (action), action_name) == 0) {
+				return osso_uri_is_default_action_by_uri (uri, action, NULL);
+			}
 		}
+	} else {
+		for (l = actions; l; l = l->next) {
+			action = l->data;
+			if (strcmp (osso_uri_action_get_name (action), action_name) == 0) {
+				return osso_uri_is_default_action (action, NULL);
+			}
+		}
 	}
 
 	return FALSE;
@@ -164,7 +223,12 @@
 static void
 test_system_default_actions (void)
 {
-	GSList *actions;
+	GSList        *actions;
+	GSList        *l;
+	OssoURIAction *default_action;
+	OssoURIAction *action = NULL;
+	const gchar   *uri_str;
+	gboolean       success;
 
 	print_header ("Testing default actions (system)");
 
@@ -173,23 +237,25 @@
 
 	/* The browser should be the default for http. */
 	g_print ("For http\n");
-	actions = osso_uri_get_actions_by_uri ("http://www.nokia.com", -1, NULL);
-	assert_int (g_slist_length (actions), 3);
 
+	uri_str = "http://www.nokia.com";
+	actions = osso_uri_get_actions_by_uri (uri_str, -1, NULL);
+	assert_int (g_slist_length (actions), 4);
+
 	/* The default. */
-	assert_bool (is_default_action (actions, "uri_link_open_link"));
+	assert_bool (is_default_action (actions, "uri_link_open_link", uri_str));
 
 	/* Existing, non-default. */
-	assert_bool (!is_default_action (actions, "uri_link_save_link"));
+	assert_bool (!is_default_action (actions, "uri_link_save_link", uri_str));
 
 	/* Non-existing. */
-	assert_bool (!is_default_action (actions, "foo_open"));
+	assert_bool (!is_default_action (actions, "foo_open", uri_str));
 	
 	osso_uri_free_actions (actions);
 
 	/* Basic use of the new API to get actions by a URI */
 	actions = osso_uri_get_actions_by_uri ("http://www.nokia.com", OSSO_URI_ACTION_NORMAL, NULL);
-	assert_int (g_slist_length (actions), 1);
+	assert_int (g_slist_length (actions), 2);
 	osso_uri_free_actions (actions);
 
 	/* Getting neutral actions which apply in all conditions where
@@ -213,10 +279,67 @@
 	assert_int (g_slist_length (actions), 1);
 	osso_uri_free_actions (actions);
 
-	/* FIXME: Finish this and test
-	 * osso_uri_get_default_action_by_uri() &
-	 * osso_uri_set_default_action_by_uri() 
+	/*
+	 * Test getting and setting default actions 
 	 */
+
+	/* Set to nothing */
+	success = osso_uri_set_default_action_by_uri ("http://www.nokia.com", NULL, NULL);
+	assert_bool (success);
+
+	/* Test it is unset */
+	default_action = osso_uri_get_default_action_by_uri ("http://www.nokia.com", NULL);
+	assert_expr (default_action == NULL);
+
+	/* 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);
+
+	for (l = actions; l; l = l->next) {
+		action = l->data;
+
+		if (strcmp (osso_uri_action_get_name (action), "addr_ap_address_book") == 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 NEUTRAL 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);
+	assert_int (g_slist_length (actions), 1);
+
+	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);
+	
+	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 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 */ 
 }
 
 static void
@@ -235,10 +358,10 @@
 	/* 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), 3);
+	assert_int (g_slist_length (actions), 4);
 
 	/* The default. */
-	assert_bool (is_default_action (actions, "uri_link_open_link"));
+	assert_bool (is_default_action (actions, "uri_link_open_link", NULL));
 
 	/* Override the default. */
 	action = NULL;
@@ -261,8 +384,8 @@
 		g_error ("Couldn't set default...\n");
 	}
 
-	assert_bool (!is_default_action (actions, "http"));
-	assert_bool (is_default_action (actions, osso_uri_action_get_name (action)));
+	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)) {
@@ -270,7 +393,7 @@
 	}
 
 	/* We're back to the system default. */
-	assert_bool (is_default_action (actions, "uri_link_open_link"));
+	assert_bool (is_default_action (actions, "uri_link_open_link", NULL));
 	
 	osso_uri_free_actions (actions);
 


More information about the maemo-commits mailing list