[Xfce4-commits] r29111 - in thunar/branches/port-to-gio: . thunar

Jannis Pohlmann jannis at xfce.org
Tue Jan 6 17:54:34 CET 2009


Author: jannis
Date: 2009-01-06 16:54:33 +0000 (Tue, 06 Jan 2009)
New Revision: 29111

Modified:
   thunar/branches/port-to-gio/ChangeLog
   thunar/branches/port-to-gio/thunar/thunar-file.c
   thunar/branches/port-to-gio/thunar/thunar-file.h
   thunar/branches/port-to-gio/thunar/thunar-launcher.c
Log:
	* thunar/thunar-file.{c,h}: Port thunar_file_to_path_list() to GIO by
	  returning a list with GFile elements instead of ThunarVfsPath
	  elements. Add methods thunar_path_list_copy() and
	  thunar_path_list_free(). Add thunar_file_get_gfile() macro which
	  returns the GFile for a ThunarFile. This should later replace the
	  current thunar_file_get_path() macro but it is easier to have them
	  in parallel right now.
	* thunar/thunar-launcher.c: Port thunar_launcher_open_files(),
	  thunar_launcher_open_paths() and thunar_launcher_action_open() to
	  GIO.

Modified: thunar/branches/port-to-gio/ChangeLog
===================================================================
--- thunar/branches/port-to-gio/ChangeLog	2009-01-06 14:22:44 UTC (rev 29110)
+++ thunar/branches/port-to-gio/ChangeLog	2009-01-06 16:54:33 UTC (rev 29111)
@@ -1,5 +1,18 @@
 2009-01-06	Jannis Pohlmann <jannis at xfce.org>
 
+	* thunar/thunar-file.{c,h}: Port thunar_file_to_path_list() to GIO by
+	  returning a list with GFile elements instead of ThunarVfsPath
+	  elements. Add methods thunar_path_list_copy() and
+	  thunar_path_list_free(). Add thunar_file_get_gfile() macro which
+	  returns the GFile for a ThunarFile. This should later replace the
+	  current thunar_file_get_path() macro but it is easier to have them
+	  in parallel right now.
+	* thunar/thunar-launcher.c: Port thunar_launcher_open_files(), 
+	  thunar_launcher_open_paths() and thunar_launcher_action_open() to
+	  GIO.
+
+2009-01-06	Jannis Pohlmann <jannis at xfce.org>
+
 	* TODO-GIO: Add desktop actions, icon infos and a few other things to
 	  the TODO list.
 	* thunar/thunar-file.c: Implement thunar_file_list_get_applications()

Modified: thunar/branches/port-to-gio/thunar/thunar-file.c
===================================================================
--- thunar/branches/port-to-gio/thunar/thunar-file.c	2009-01-06 14:22:44 UTC (rev 29110)
+++ thunar/branches/port-to-gio/thunar/thunar-file.c	2009-01-06 16:54:33 UTC (rev 29111)
@@ -1,7 +1,7 @@
 /* $Id$ */
 /*-
  * Copyright (c) 2005-2007 Benedikt Meurer <benny at xfce.org>
- * Copyright (c) 2008      Jannis Pohlmann <jannis at xfce.org>
+ * Copyright (c) 2008-2009 Jannis Pohlmann <jannis at xfce.org>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -2556,13 +2556,13 @@
  * thunar_file_list_to_path_list:
  * @file_list : a #GList of #ThunarFile<!---->s.
  *
- * Transforms the @file_list to a #GList of #ThunarVfsPath<!---->s for
+ * Transforms the @file_list to a #GList of #GFile<!---->s for
  * the #ThunarFile<!---->s contained within @file_list.
  *
  * The caller is responsible to free the returned list using
- * thunar_vfs_path_list_free() when no longer needed.
+ * thunar_file_list_free() when no longer needed.
  *
- * Return value: the list of #ThunarVfsPath<!---->s for @file_list.
+ * Return value: the list of #GFile<!---->s for @file_list.
  **/
 GList*
 thunar_file_list_to_path_list (GList *file_list)
@@ -2571,10 +2571,46 @@
   GList *lp;
 
   for (lp = g_list_last (file_list); lp != NULL; lp = lp->prev)
-    path_list = g_list_prepend (path_list, thunar_vfs_path_ref (THUNAR_FILE (lp->data)->info->path));
+    path_list = g_list_prepend (path_list, g_object_ref (THUNAR_FILE (lp->data)->gfile));
 
   return path_list;
 }
 
 
 
+/**
+ * thunar_path_list_copy:
+ * @path_list : a #GList of #GFile<!---->s.
+ *
+ * Takes a deep copy of @path_list and returns the result. The 
+ * caller is responsible to free the returned list using 
+ * thunar_path_list_free().
+ */
+GList *
+thunar_path_list_copy (GList *path_list)
+{
+  GList *list = NULL;
+  GList *lp;
+
+  for (lp = g_list_last (path_list); lp != NULL; lp = g_list_previous (lp))
+    list = g_list_prepend (list, g_object_ref (lp->data));
+
+  return list;
+}
+
+
+
+/**
+ * thunar_path_list_free:
+ * @path_list : a #GList of #GFile<!---->s.
+ *
+ * Decrements the reference count on all the #GFile elements
+ * in the list and releases the memory used for the list 
+ * itself.
+ */
+void
+thunar_path_list_free (GList *path_list)
+{
+  g_list_foreach (path_list, (GFunc) g_object_unref, NULL);
+  g_list_free (path_list);
+}

Modified: thunar/branches/port-to-gio/thunar/thunar-file.h
===================================================================
--- thunar/branches/port-to-gio/thunar/thunar-file.h	2009-01-06 14:22:44 UTC (rev 29110)
+++ thunar/branches/port-to-gio/thunar/thunar-file.h	2009-01-06 16:54:33 UTC (rev 29111)
@@ -209,6 +209,8 @@
 
 GList            *thunar_file_list_get_applications  (GList *file_list);
 GList            *thunar_file_list_to_path_list      (GList *file_list);
+GList            *thunar_path_list_copy              (GList *path_list);
+void              thunar_path_list_free              (GList *path_list);
 
 gboolean         thunar_file_is_desktop              (const ThunarFile *file);
 
@@ -248,6 +250,21 @@
 #define thunar_file_get_info(file) (THUNAR_FILE ((file))->info)
 
 /**
+ * thunar_file_get_gfile:
+ * @file  : a #ThunarFile instance.
+ *
+ * Returns the #GFile, that refers to the location of the @file.
+ *
+ * Note, that there's no reference taken for the caller on the
+ * returned #GFile, so if you need the object for a longer
+ * period, you'll need to take a reference yourself using the
+ * g_object_ref() function.
+ *
+ * Return value: the path to the @file.
+ **/
+#define thunar_file_get_gfile(file) (THUNAR_FILE ((file))->gfile)
+
+/**
  * thunar_file_get_path:
  * @file  : a #ThunarFile instance.
  *

Modified: thunar/branches/port-to-gio/thunar/thunar-launcher.c
===================================================================
--- thunar/branches/port-to-gio/thunar/thunar-launcher.c	2009-01-06 14:22:44 UTC (rev 29110)
+++ thunar/branches/port-to-gio/thunar/thunar-launcher.c	2009-01-06 16:54:33 UTC (rev 29111)
@@ -1,6 +1,7 @@
 /* $Id$ */
 /*-
  * Copyright (c) 2005-2006 Benedikt Meurer <benny at xfce.org>
+ * Copyright (c) 2009      Jannis Pohlmann <jannis at xfce.org>
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -80,7 +81,7 @@
                                                                  GList                    *files);
 static void          thunar_launcher_open_files                 (ThunarLauncher           *launcher,
                                                                  GList                    *files);
-static void          thunar_launcher_open_paths                 (ThunarVfsMimeHandler     *mime_handler,
+static void          thunar_launcher_open_paths                 (GAppInfo                 *app_info,
                                                                  GList                    *path_list,
                                                                  ThunarLauncher           *launcher);
 static void          thunar_launcher_open_windows               (ThunarLauncher           *launcher,
@@ -561,44 +562,71 @@
 thunar_launcher_open_files (ThunarLauncher *launcher,
                             GList          *files)
 {
-  ThunarVfsMimeApplication *application;
-  ThunarVfsMimeDatabase    *database;
-  ThunarVfsMimeInfo        *info;
-  GHashTable               *applications;
-  GList                    *path_list;
-  GList                    *lp;
+  GHashTable *associations;
+  GAppInfo   *app_info;
+  GAppInfo   *existing_app_info;
+  GFile      *path;
+  GList      *applications = NULL;
+  GList      *app;
+  GList      *path_list;
+  GList      *lp;
+  gchar      *content_type;
+  gchar      *uri_scheme;
 
-  /* allocate a hash table to associate applications to URIs */
-  applications = g_hash_table_new_full (thunar_vfs_mime_application_hash,
-                                        thunar_vfs_mime_application_equal,
-                                        (GDestroyNotify) g_object_unref,
-                                        (GDestroyNotify) thunar_vfs_path_list_free);
+  /* allocate a hash table to associate applications with URIs */
+  associations = g_hash_table_new_full (g_direct_hash,
+                                        (GEqualFunc) g_app_info_equal,
+                                        NULL,
+                                        (GDestroyNotify) thunar_path_list_free);
 
-  /* take a reference on the mime database */
-  database = thunar_vfs_mime_database_get_default ();
-
   for (lp = files; lp != NULL; lp = lp->next)
     {
-      /* determine the default application for the MIME type */
-      info = thunar_file_get_mime_info (lp->data);
-      application = thunar_vfs_mime_database_get_default_application (database, info);
+      path = thunar_file_get_gfile (lp->data);
 
-      /* check if we have an application here */
-      if (G_LIKELY (application != NULL))
+      /* determine the default application for this file */
+      content_type = thunarx_file_info_get_mime_type (THUNARX_FILE_INFO (lp->data));
+      app_info = g_app_info_get_default_for_type (content_type, FALSE);
+      g_free (content_type);
+
+      /* FIXME: Not so sure this is a good idea */
+      if (G_UNLIKELY (app_info == NULL))
         {
-          /* check if we have that application already */
-          path_list = g_hash_table_lookup (applications, application);
-          if (G_LIKELY (path_list != NULL))
+          uri_scheme = g_file_get_uri_scheme (path);
+          app_info = g_app_info_get_default_for_uri_scheme (uri_scheme);
+          g_free (uri_scheme);
+        }
+
+      if (G_LIKELY (app_info != NULL))
+        {
+          /* check if we already have that application */
+          existing_app_info = NULL;
+          for (app = applications; app != NULL; app = g_list_next (app))
+            if (g_app_info_equal (app_info, app->data))
+              {
+                existing_app_info = app->data;
+                break;
+              }
+
+          if (G_LIKELY (existing_app_info != NULL))
             {
-              /* take a copy of the list as the old one will be dropped by the insert */
-              path_list = thunar_vfs_path_list_copy (path_list);
+              /* lookup the path list, duplicate it, append the file and replace the old list */
+              path_list = g_hash_table_lookup (associations, existing_app_info);
+              path_list = thunar_path_list_copy (path_list);
+              path_list = g_list_append (path_list, g_object_ref (path));
+              g_hash_table_replace (associations, existing_app_info, path_list);
             }
+          else
+            {
+              /* create a new path list and store it in the hash table */
+              path_list = NULL;
+              path_list = g_list_append (path_list, g_object_ref (path));
+              g_hash_table_insert (associations, app_info, path_list);
+              
+              /* remember the application for later */
+              applications = g_list_append (applications, g_object_ref (app_info));
+            }
 
-          /* append our new URI to the list */
-          path_list = thunar_vfs_path_list_append (path_list, thunar_file_get_path (lp->data));
-
-          /* (re)insert the URI list for the application */
-          g_hash_table_insert (applications, application, path_list);
+          g_object_unref (app_info);
         }
       else
         {
@@ -608,43 +636,45 @@
         }
     }
 
-  /* run all collected applications */
-  g_hash_table_foreach (applications, (GHFunc) thunar_launcher_open_paths, launcher);
+  /* run all the collected applications */
+  g_hash_table_foreach (associations, (GHFunc) thunar_launcher_open_paths, launcher);
 
-  /* release the reference on the mime database */
-  g_object_unref (G_OBJECT (database));
+  /* release the list of applications used */
+  g_list_foreach (applications, (GFunc) g_object_unref, NULL);
+  g_list_free (applications);
 
-  /* drop the applications hash table */
-  g_hash_table_destroy (applications);
+  /* drop the associations hash table */
+  g_hash_table_unref (associations);
 }
 
 
 
 static void
-thunar_launcher_open_paths (ThunarVfsMimeHandler *mime_handler,
-                            GList                *path_list,
-                            ThunarLauncher       *launcher)
+thunar_launcher_open_paths (GAppInfo       *app_info,
+                            GList          *path_list,
+                            ThunarLauncher *launcher)
 {
-  GdkScreen *screen;
-  GError    *error = NULL;
-  gchar     *message;
-  gchar     *name;
-  guint      n;
+  GdkAppLaunchContext *context;
+  GdkScreen           *screen;
+  GError              *error = NULL;
+  gchar               *message;
+  guint                n;
 
   /* determine the screen on which to launch the application */
   screen = (launcher->widget != NULL) ? gtk_widget_get_screen (launcher->widget) : NULL;
 
+  context = gdk_app_launch_context_new ();
+  gdk_app_launch_context_set_screen (context, screen);
+
   /* try to execute the application with the given URIs */
-  if (!thunar_vfs_mime_handler_exec (mime_handler, screen, path_list, &error))
+  if (!g_app_info_launch (app_info, path_list, G_APP_LAUNCH_CONTEXT (context), &error))
     {
       /* figure out the appropriate error message */
       n = g_list_length (path_list);
       if (G_LIKELY (n == 1))
         {
           /* we can give a precise error message here */
-          name = g_filename_display_name (thunar_vfs_path_get_name (path_list->data));
-          message = g_strdup_printf (_("Failed to open file \"%s\""), name);
-          g_free (name);
+          message = g_strdup_printf (_("Failed to open file \"%s\""), thunar_file_get_display_name (path_list->data));
         }
       else
         {
@@ -657,6 +687,8 @@
       g_error_free (error);
       g_free (message);
     }
+
+  g_object_unref (context);
 }
 
 
@@ -1005,13 +1037,13 @@
 thunar_launcher_action_open (GtkAction      *action,
                              ThunarLauncher *launcher)
 {
-  ThunarVfsMimeHandler *mime_handler;
-  GdkScreen            *screen;
-  gboolean              executable = TRUE;
-  GList                *selected_paths;
-  GList                *directories = NULL;
-  GList                *files = NULL;
-  GList                *lp;
+  GAppInfo  *app_info;
+  GdkScreen *screen;
+  gboolean   executable = TRUE;
+  GList     *selected_paths;
+  GList     *directories = NULL;
+  GList     *files = NULL;
+  GList     *lp;
 
   _thunar_return_if_fail (GTK_IS_ACTION (action));
   _thunar_return_if_fail (THUNAR_IS_LAUNCHER (launcher));
@@ -1020,13 +1052,13 @@
   screen = (launcher->widget != NULL) ? gtk_widget_get_screen (launcher->widget) : NULL;
 
   /* check if we have a mime handler associated with the action */
-  mime_handler = g_object_get_qdata (G_OBJECT (action), thunar_launcher_handler_quark);
-  if (G_LIKELY (mime_handler != NULL))
+  app_info = g_object_get_qdata (G_OBJECT (action), thunar_launcher_handler_quark);
+  if (G_LIKELY (app_info != NULL))
     {
       /* try to open the selected files using the given application */
       selected_paths = thunar_file_list_to_path_list (launcher->selected_files);
-      thunar_launcher_open_paths (mime_handler, selected_paths, launcher);
-      thunar_vfs_path_list_free (selected_paths);
+      thunar_launcher_open_paths (app_info, selected_paths, launcher);
+      thunar_path_list_free (selected_paths);
     }
   else if (g_list_length (launcher->selected_files) == 1 && thunar_file_is_directory (launcher->selected_files->data))
     {




More information about the Xfce4-commits mailing list