[nautilus-actions] Get the mimetype from NautilusFileInfo class



commit 4bb448acc00dbfcd03d530f8b4a5008c544b9d2b
Author: Pierre Wieser <pwieser trychlos org>
Date:   Wed Apr 14 20:13:18 2010 +0200

    Get the mimetype from NautilusFileInfo class
    
    This also fix bug #615646, preventing Nautilus to crash when one right-clicks
    on a 'special' desktop icon (computer, trash, etc.).

 ChangeLog                            |   10 +++++++++-
 src/core/na-selected-info.c          |   30 ++++++++++++++++++------------
 src/core/na-selected-info.h          |    2 +-
 src/plugin-tracker/na-tracker-dbus.c |   21 ++++++++++++++-------
 src/utils/nautilus-actions-run.c     |   23 +++++++++++++++--------
 5 files changed, 57 insertions(+), 29 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 5381852..2468611 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,14 @@
 2009-04-14 Pierre Wieser <pwieser trychlos org>
 
-	* src/core/na-selected-info.c (dump): New function.
+	* src/core/na-selected-info.c
+	(dump): New function.
+	(na_selected_info_create_for_uri): Accepts a mimetype.
+
+	* src/plugin-tracker/na-tracker-dbus.c
+	(na_tracker_dbus_get_selected_paths): Also sends the mimetype.
+
+	* src/utils/nautilus-actions-run.c (targets_from_selection):
+	Get the mimetype from DBus interface.
 
 2009-04-09 Pierre Wieser <pwieser trychlos org>
 
diff --git a/src/core/na-selected-info.c b/src/core/na-selected-info.c
index d30b788..d235105 100644
--- a/src/core/na-selected-info.c
+++ b/src/core/na-selected-info.c
@@ -63,7 +63,7 @@ static void            instance_finalize( GObject *object );
 
 static void            dump( const NASelectedInfo *nsi );
 static NASelectedInfo *new_from_nautilus_file_info( NautilusFileInfo *item );
-static NASelectedInfo *new_from_uri( const gchar *uri );
+static NASelectedInfo *new_from_uri( const gchar *uri, const gchar *mimetype );
 static void            query_file_attributes( NASelectedInfo *info );
 
 GType
@@ -194,10 +194,7 @@ na_selected_info_get_list_from_item( NautilusFileInfo *item )
 {
 	GList *selected;
 
-	gchar *uri = nautilus_file_info_get_uri( item );
-	NASelectedInfo *info = na_selected_info_create_for_uri( uri );
-	g_free( uri );
-
+	NASelectedInfo *info = new_from_nautilus_file_info( item );
 	selected = g_list_prepend( NULL, info );
 
 	return( selected );
@@ -404,17 +401,18 @@ na_selected_info_is_directory( const NASelectedInfo *nsi )
 /**
  * na_selected_info_create_for_uri:
  * @uri: an URI.
+ * @mimetype: the corresponding Nautilus mime type, or %NULL.
  *
  * Returns: a newly allocated #NASelectedInfo object for the given @uri.
  */
 NASelectedInfo *
-na_selected_info_create_for_uri( const gchar *uri )
+na_selected_info_create_for_uri( const gchar *uri, const gchar *mimetype )
 {
 	static const gchar *thisfn = "na_selected_info_create_for_uri";
 
-	g_debug( "%s", thisfn );
+	g_debug( "%s: uri=%s, mimetype=%s", thisfn, uri, mimetype );
 
-	NASelectedInfo *obj = new_from_uri( uri );
+	NASelectedInfo *obj = new_from_uri( uri, mimetype );
 
 	return( obj );
 }
@@ -437,18 +435,23 @@ static NASelectedInfo *
 new_from_nautilus_file_info( NautilusFileInfo *item )
 {
 	gchar *uri = nautilus_file_info_get_uri( item );
-	NASelectedInfo *info = new_from_uri( uri );
+	gchar *mimetype = nautilus_file_info_get_mime_type( item );
+	NASelectedInfo *info = new_from_uri( uri, mimetype );
+	g_free( mimetype );
 	g_free( uri );
 
 	return( info );
 }
 
 static NASelectedInfo *
-new_from_uri( const gchar *uri )
+new_from_uri( const gchar *uri, const gchar *mimetype )
 {
 	NASelectedInfo *info = g_object_new( NA_SELECTED_INFO_TYPE, NULL );
 
 	info->private->uri = g_strdup( uri );
+	if( mimetype ){
+		info->private->mimetype = g_strdup( mimetype );
+	}
 	info->private->location = g_file_new_for_uri( uri );
 	info->private->vfs = g_new0( NAGnomeVFSURI, 1 );
 
@@ -467,7 +470,7 @@ query_file_attributes( NASelectedInfo *nsi )
 
 	error = NULL;
 	GFileInfo *info = g_file_query_info( nsi->private->location,
-			G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
+			G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
 			G_FILE_QUERY_INFO_NONE, NULL, &error );
 
 	if( error ){
@@ -476,7 +479,10 @@ query_file_attributes( NASelectedInfo *nsi )
 		return;
 	}
 
-	nsi->private->mimetype = g_strdup( g_file_info_get_attribute_as_string( info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE ));
+	if( !nsi->private->mimetype ){
+		nsi->private->mimetype = g_strdup( g_file_info_get_attribute_as_string( info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ));
+	}
+
 	nsi->private->file_type = ( GFileType ) g_file_info_get_attribute_uint32( info, G_FILE_ATTRIBUTE_STANDARD_TYPE );
 
 	g_object_unref( info );
diff --git a/src/core/na-selected-info.h b/src/core/na-selected-info.h
index 18d3cb4..e4c9796 100644
--- a/src/core/na-selected-info.h
+++ b/src/core/na-selected-info.h
@@ -86,7 +86,7 @@ gchar          *na_selected_info_get_uri       ( const NASelectedInfo *nsi );
 gchar          *na_selected_info_get_uri_scheme( const NASelectedInfo *nsi );
 gboolean        na_selected_info_is_directory  ( const NASelectedInfo *nsi );
 
-NASelectedInfo *na_selected_info_create_for_uri( const gchar *uri );
+NASelectedInfo *na_selected_info_create_for_uri( const gchar *uri, const gchar *mimetype );
 
 G_END_DECLS
 
diff --git a/src/plugin-tracker/na-tracker-dbus.c b/src/plugin-tracker/na-tracker-dbus.c
index 670a16e..f9095c5 100644
--- a/src/plugin-tracker/na-tracker-dbus.c
+++ b/src/plugin-tracker/na-tracker-dbus.c
@@ -122,10 +122,9 @@ class_init( NATrackerDBusClass *klass )
 
 	/* Installation du mécanisme dâ??introspection,
 	 * permettant de faire lâ??appel de méthodes via un nom.
-	 * - Le second paramètre de cette fonction : &dbus_glib_dummy_object_object_info
-	 * est généré lors de lâ??appel à dbus-binding-tool
-	 * et sa définition peut être retrouvée dans le fichier
-	 * na-tracker-dbus-glue.h
+	 * - Le second paramètre de cette fonction (dbus_glib_na_tracker_dbus_object_info)
+	 * est généré lors de lâ??appel à dbus-binding-tool, et sa définition
+	 * peut être retrouvée dans le fichier na-tracker-dbus-glue.h
 	 */
 	dbus_g_object_type_install_info( NA_TRACKER_DBUS_TYPE, &dbus_glib_na_tracker_dbus_object_info );
 }
@@ -210,7 +209,14 @@ na_tracker_dbus_set_uris( NATrackerDBus *tracker, GList *files )
  * @paths: the location in which copy the strings to be sent.
  * @error: the location of a GError.
  *
- * Send on session DBus the list of URIs of currently selected items.
+ * Sends on session DBus the list of currently selected items, as two strings
+ * for each item :
+ * - the uri
+ * - the mimetype as returned by NautilusFileInfo.
+ *
+ * This is required as some particular items are only known by Nautilus
+ * (e.g. computer), and standard GLib functions are not able to retrieve
+ * their mimetype.
  *
  * Exported as GetSelectedPaths method on Tracker.Status interface.
  */
@@ -230,14 +236,15 @@ na_tracker_dbus_get_selected_paths( NATrackerDBus *tracker, char ***paths, GErro
 
 	if( !tracker->private->dispose_has_run ){
 
-		count = g_list_length( tracker->private->selected );
+		count = 2 * g_list_length( tracker->private->selected );
 		*paths = ( char ** ) g_new0( gchar *, 1+count );
 		iter = *paths;
 
 		for( it = tracker->private->selected ; it ; it = it->next ){
-
 			*iter = nautilus_file_info_get_uri(( NautilusFileInfo * ) it->data );
 			iter++;
+			*iter = nautilus_file_info_get_mime_type(( NautilusFileInfo * ) it->data );
+			iter++;
 		}
 	}
 
diff --git a/src/utils/nautilus-actions-run.c b/src/utils/nautilus-actions-run.c
index 2f049f8..f7dc6d7 100644
--- a/src/utils/nautilus-actions-run.c
+++ b/src/utils/nautilus-actions-run.c
@@ -74,7 +74,7 @@ static GOptionContext  *init_options( void );
 static NAObjectAction  *get_action( const gchar *id );
 static GList           *targets_from_selection( void );
 static GList           *targets_from_commandline( void );
-static GList           *get_selection_from_strv( const gchar **strv );
+static GList           *get_selection_from_strv( const gchar **strv, gboolean has_mimetype );
 static NAObjectProfile *get_profile_for_targets( NAObjectAction *action, GList *targets );
 static void             execute_action( NAObjectAction *action, NAObjectProfile *profile, GList *targets );
 static void             dump_targets( GList *targets );
@@ -251,9 +251,10 @@ get_action( const gchar *id )
 
 /*
  * the DBus.Tracker.Status interface returns a list of strings
- * where each item is the URI of a selected item.
+ * where each selected item brings up both its URI and its Nautilus
+ * mime type.
  *
- * We return to the caller a GList of NautilusFileInfo
+ * We return to the caller a GList of NASelectedInfo objects
  */
 static GList *
 targets_from_selection( void )
@@ -302,7 +303,7 @@ targets_from_selection( void )
 	}
 	g_debug( "%s: function call is ok", thisfn );
 
-	selection = get_selection_from_strv(( const gchar ** ) paths );
+	selection = get_selection_from_strv(( const gchar ** ) paths, TRUE );
 
 	g_strfreev( paths );
 
@@ -315,7 +316,7 @@ targets_from_selection( void )
 /*
  * get targets from command-line
  *
- * We return to the caller a GList of NautilusFileInfo
+ * We return to the caller a GList of NASelectedInfo objects.
  */
 static GList *
 targets_from_commandline( void )
@@ -325,13 +326,13 @@ targets_from_commandline( void )
 
 	g_debug( "%s", thisfn );
 
-	targets = get_selection_from_strv(( const gchar ** ) targets_array );
+	targets = get_selection_from_strv(( const gchar ** ) targets_array, FALSE );
 
 	return( targets );
 }
 
 static GList *
-get_selection_from_strv( const gchar **strv )
+get_selection_from_strv( const gchar **strv, gboolean has_mimetype )
 {
 	GList *list;
 	gchar **iter;
@@ -340,7 +341,13 @@ get_selection_from_strv( const gchar **strv )
 	iter = ( gchar ** ) strv;
 
 	while( *iter ){
-		NASelectedInfo *nsi = na_selected_info_create_for_uri( *iter );
+		const gchar *uri = ( const gchar * ) *iter;
+		const gchar *mimetype = NULL;
+		if( has_mimetype ){
+			iter++;
+			mimetype = ( const gchar * ) *iter;
+		}
+		NASelectedInfo *nsi = na_selected_info_create_for_uri( uri, mimetype );
 		list = g_list_prepend( list, nsi );
 		iter++;
 	}



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