[nautilus-actions] NadpReader: make sure an action has at least one profile



commit 3ff2b5d2d6b3d0d51cc0352968cef12831f3de9a
Author: Pierre Wieser <pwieser trychlos org>
Date:   Fri Dec 9 10:06:00 2011 +0100

    NadpReader: make sure an action has at least one profile

 ChangeLog                          |   12 ++++++++++
 src/io-desktop/nadp-desktop-file.c |   31 ++++++++++++++++++++++++++
 src/io-desktop/nadp-desktop-file.h |    2 +
 src/io-desktop/nadp-reader.c       |   43 ++++++++++++++++++++++++++----------
 4 files changed, 76 insertions(+), 12 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 82731df..b85757b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2011-12-09 Pierre Wieser <pwieser trychlos org>
 
+	* src/io-desktop/nadp-desktop-file.c:
+	* src/io-desktop/nadp-desktop-file.h (nadp_desktop_file_has_profile):
+	New function.
+
+	* src/io-desktop/nadp-reader.c
+	(item_from_desktop_path):
+	Replace the warning with an error message to be returned to the caller.
+	(read_done_action_read_profiles):
+	Attach at least a default profile to the action.
+	(read_done_action_load_profile):
+	Define a default profile if not found in .desktop file.
+
 	* src/core/na-icontext-factory.c: Fix mimetypes and schemes default values.
 
 	* src/core/na-icontext.c (is_compatible_scheme): Fix scheme test.
diff --git a/src/io-desktop/nadp-desktop-file.c b/src/io-desktop/nadp-desktop-file.c
index 411b454..2e5c86d 100644
--- a/src/io-desktop/nadp-desktop-file.c
+++ b/src/io-desktop/nadp-desktop-file.c
@@ -622,6 +622,37 @@ nadp_desktop_file_get_profiles( const NadpDesktopFile *ndf )
 }
 
 /**
+ * nadp_desktop_file_has_profile:
+ * @ndf: the #NadpDesktopFile instance.
+ * @profile_id: the identifier of the profile.
+ *
+ * Returns: %TRUE if a group can be found in the .desktop file for this profile,
+ * %FALSE else.
+ *
+ * Since: 3.1
+ */
+gboolean
+nadp_desktop_file_has_profile( const NadpDesktopFile *ndf, const gchar *profile_id )
+{
+	gboolean has_profile;
+	gchar *group_name;
+
+	g_return_val_if_fail( NADP_IS_DESKTOP_FILE( ndf ), FALSE );
+	g_return_val_if_fail( profile_id && g_utf8_strlen( profile_id, -1 ), FALSE );
+
+	has_profile = FALSE;
+
+	if( !ndf->private->dispose_has_run ){
+
+		group_name = g_strdup_printf( "%s %s", NADP_GROUP_PROFILE, profile_id );
+		has_profile = g_key_file_has_group( ndf->private->key_file, group_name );
+		g_free( group_name );
+	}
+
+	return( has_profile );
+}
+
+/**
  * nadp_desktop_file_remove_key:
  * @ndf: this #NadpDesktopFile instance.
  * @group: the group.
diff --git a/src/io-desktop/nadp-desktop-file.h b/src/io-desktop/nadp-desktop-file.h
index 437d2e8..511c53a 100644
--- a/src/io-desktop/nadp-desktop-file.h
+++ b/src/io-desktop/nadp-desktop-file.h
@@ -89,6 +89,8 @@ gchar           *nadp_desktop_file_get_file_type    ( const NadpDesktopFile *ndf
 gchar           *nadp_desktop_file_get_id           ( const NadpDesktopFile *ndf );
 GSList          *nadp_desktop_file_get_profiles     ( const NadpDesktopFile *ndf );
 
+gboolean         nadp_desktop_file_has_profile      ( const NadpDesktopFile *ndf, const gchar *profile_id );
+
 void             nadp_desktop_file_remove_key       ( const NadpDesktopFile *ndf, const gchar *group, const gchar *key );
 void             nadp_desktop_file_remove_profile   ( const NadpDesktopFile *ndf, const gchar *profile_id );
 
diff --git a/src/io-desktop/nadp-reader.c b/src/io-desktop/nadp-reader.c
index 2d26c69..f46923e 100644
--- a/src/io-desktop/nadp-reader.c
+++ b/src/io-desktop/nadp-reader.c
@@ -273,7 +273,7 @@ item_from_desktop_path( const NadpDesktopProvider *provider, DesktopPath *dps, G
 static NAIFactoryObject *
 item_from_desktop_file( const NadpDesktopProvider *provider, NadpDesktopFile *ndf, GSList **messages )
 {
-	static const gchar *thisfn = "nadp_reader_item_from_desktop_file";
+	/*static const gchar *thisfn = "nadp_reader_item_from_desktop_file";*/
 	NAIFactoryObject *item;
 	gchar *type;
 	NadpReaderData *reader_data;
@@ -289,7 +289,8 @@ item_from_desktop_file( const NadpDesktopProvider *provider, NadpDesktopFile *nd
 		item = NA_IFACTORY_OBJECT( na_object_menu_new());
 
 	} else {
-		g_warning( "%s: unknown type=%s", thisfn, type );
+		/* i18n: 'type' is the nature of the item: Action or Menu */
+		na_core_utils_slist_add_message( messages, _( "unknown type: %s" ), type );
 	}
 
 	if( item ){
@@ -640,17 +641,22 @@ read_done_item_is_writable( const NAIFactoryProvider *provider, NAObjectItem *it
 }
 
 /*
- * read and attach profiles in the specified order
- * profiles which may exist in .desktop files, but are not referenced
- * in the 'Profiles' string list are just ignored
+ * Read and attach profiles in the specified order
+ * - profiles which may exist in .desktop files, but are not referenced
+ *   in the 'Profiles' string list are just ignored
+ * - profiles which may be referenced in the action string list, but are not
+ *   found in the .desktop file are recreated with default values (plus a warning)
+ * - ensure that there is at least one profile attached to the action
  */
 static void
 read_done_action_read_profiles( const NAIFactoryProvider *provider, NAObjectAction *action, NadpReaderData *reader_data, GSList **messages )
 {
+	static const gchar *thisfn = "nadp_reader_read_done_action_read_profiles";
 	GSList *order;
 	GSList *ip;
 	gchar *profile_id;
 	NAObjectId *found;
+	NAObjectProfile *profile;
 
 	reader_data->action = action;
 	order = na_object_get_items_slist( action );
@@ -664,21 +670,34 @@ read_done_action_read_profiles( const NAIFactoryProvider *provider, NAObjectActi
 	}
 
 	na_core_utils_slist_free( order );
+
+	if( !na_object_get_items_count( action )){
+		g_warning( "%s: no profile found in .desktop file", thisfn );
+		profile = na_object_profile_new_with_defaults();
+		na_object_attach_profile( action, profile );
+	}
 }
 
 static void
 read_done_action_load_profile( const NAIFactoryProvider *provider, NadpReaderData *reader_data, const gchar *profile_id, GSList **messages )
 {
+	static const gchar *thisfn = "nadp_reader_read_done_action_load_profile";
 	NAObjectProfile *profile;
 
-	g_debug( "nadp_reader_read_done_action_load_profile: loading profile=%s", profile_id );
+	g_debug( "%s: loading profile=%s", thisfn, profile_id );
 
-	profile = na_object_profile_new();
+	profile = na_object_profile_new_with_defaults();
 	na_object_set_id( profile, profile_id );
 
-	na_ifactory_provider_read_item(
-			NA_IFACTORY_PROVIDER( provider ),
-			reader_data,
-			NA_IFACTORY_OBJECT( profile ),
-			messages );
+	if( nadp_desktop_file_has_profile( reader_data->ndf, profile_id )){
+		na_ifactory_provider_read_item(
+				NA_IFACTORY_PROVIDER( provider ),
+				reader_data,
+				NA_IFACTORY_OBJECT( profile ),
+				messages );
+
+	} else {
+		g_warning( "%s: profile '%s' not found in .desktop file", thisfn, profile_id );
+		na_object_attach_profile( reader_data->action, profile );
+	}
 }



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