[nautilus-actions] Enhance validity conditions of a profile



commit 4a437edba78d32d19fa494c5463e1135523277b5
Author: Pierre Wieser <pwieser trychlos org>
Date:   Wed Oct 21 00:34:40 2009 +0200

    Enhance validity conditions of a profile

 ChangeLog                       |   14 ++++++
 src/common/na-utils.c           |    2 +-
 src/runtime/na-gconf-provider.c |   17 ++------
 src/runtime/na-object-profile.c |   91 +++++++++++++++++++++++++++++++++-----
 src/runtime/na-utils.c          |   30 +++++++++++++
 src/runtime/na-utils.h          |    4 ++
 6 files changed, 132 insertions(+), 26 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index a404ee4..1582b34 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2009-10-20 Pierre Wieser <pwieser trychlos org>
 
+	Enhance validity conditions of a profile.
+
+	* src/common/na-utils.c
+	(na_utils_string_list_to_text): default value is NULL.
+
+	* src/runtime/na-gconf-provider.c (read_item_action_profile_properties):
+	No more check just path and parameters.
+
+	* src/runtime/na-object-profile.c (object_is_valid):
+	Check for command, basenames, mimetypes and schemes.
+
+	* src/runtime/na-utils.c:
+	* src/runtime/na-utils.h (na_utils_get_first_word): New function.
+
 	* src/common/na-object-item.c (na_object_item_get_pixbuf):
 	Update comment.
 
diff --git a/src/common/na-utils.c b/src/common/na-utils.c
index 2f15187..71d8abe 100644
--- a/src/common/na-utils.c
+++ b/src/common/na-utils.c
@@ -106,7 +106,7 @@ na_utils_string_list_to_text( GSList *strlist )
 GSList *
 na_utils_text_to_string_list( const gchar *text )
 {
-	return( text_to_string_list( text, ";", "*" ));
+	return( text_to_string_list( text, ";", NULL ));
 }
 
 /**
diff --git a/src/runtime/na-gconf-provider.c b/src/runtime/na-gconf-provider.c
index 16cb920..12ae90f 100644
--- a/src/runtime/na-gconf-provider.c
+++ b/src/runtime/na-gconf-provider.c
@@ -635,35 +635,26 @@ read_item_action_profile( NAGConfProvider *provider, NAObjectAction *action, con
 static void
 read_item_action_profile_properties( NAGConfProvider *provider, GSList *entries, NAObjectProfile *profile )
 {
-	static const gchar *thisfn = "na_gconf_provider_read_item_action_profile_properties";
+	/*static const gchar *thisfn = "na_gconf_provider_read_item_action_profile_properties";*/
 	gchar *label, *path, *parameters;
 	GSList *basenames, *schemes, *mimetypes;
 	gboolean isfile, isdir, multiple, matchcase;
 
 	if( na_gconf_utils_get_string_from_entries( entries, ACTION_PROFILE_LABEL_ENTRY, &label )){
 		na_object_set_label( profile, label );
+		g_free( label );
 	}
 
-	path = NULL;
 	if( na_gconf_utils_get_string_from_entries( entries, ACTION_PATH_ENTRY, &path )){
 		na_object_profile_set_path( profile, path );
+		g_free( path );
 	}
 
-	parameters = NULL;
 	if( na_gconf_utils_get_string_from_entries( entries, ACTION_PARAMETERS_ENTRY, &parameters )){
 		na_object_profile_set_parameters( profile, parameters );
+		g_free( parameters );
 	}
 
-	if(( !path || !g_utf8_strlen( path, -1 )) && ( !parameters || !g_utf8_strlen( parameters, -1 ))){
-		g_warning(
-				"%s: no path nor parameters found for NAObjectProfile %p \"%s\"",
-				thisfn, ( void * ) profile, label );
-	}
-
-	g_free( label );
-	g_free( path );
-	g_free( parameters );
-
 	if( na_gconf_utils_get_string_list_from_entries( entries, ACTION_BASENAMES_ENTRY, &basenames )){
 		na_object_profile_set_basenames( profile, basenames );
 		na_utils_free_string_list( basenames );
diff --git a/src/runtime/na-object-profile.c b/src/runtime/na-object-profile.c
index 7dadb16..699cc6b 100644
--- a/src/runtime/na-object-profile.c
+++ b/src/runtime/na-object-profile.c
@@ -90,6 +90,11 @@ static NAObject *object_new( const NAObject *profile );
 static void      object_copy( NAObject *target, const NAObject *source );
 static gboolean  object_are_equal( const NAObject *a, const NAObject *b );
 static gboolean  object_is_valid( const NAObject *profile );
+static gboolean  is_valid_path_parameters( const NAObjectProfile *profile );
+static gboolean  is_valid_filenames( const NAObjectProfile *profile );
+static gboolean  is_valid_mimetypes( const NAObjectProfile *profile );
+static gboolean  is_valid_isfiledir( const NAObjectProfile *profile );
+static gboolean  is_valid_schemes( const NAObjectProfile *profile );
 
 static gchar    *object_id_new_id( const NAObjectId *object, const NAObjectId *new_parent );
 
@@ -1472,32 +1477,94 @@ object_are_equal( const NAObject *a, const NAObject *b )
 }
 
 /*
- * a valid NAObjectProfile requires rather a not empty command to be a
- * valid candidate to execution ; as the distinction path vs.parameters
- * is somewhat arbitrary, we check for at least one of these
+ * a valid NAObjectProfile requires :
+ * - the first word of path+parameters should be an executable file
+ * - filenames list of pseudo-regexp is not empty
+ * - mimetypes list is not empty
+ * - at least one of is_file, is_dir is set
+ * - schemes list is not empty
  */
 gboolean
 object_is_valid( const NAObject *profile )
 {
-	gboolean is_valid = TRUE;
+	gboolean is_valid = FALSE;
 
 	g_return_val_if_fail( NA_IS_OBJECT_PROFILE( profile ), FALSE );
 
 	if( !NA_OBJECT_PROFILE( profile )->private->dispose_has_run ){
 
-		if( is_valid ){
-			is_valid =
-				( NA_OBJECT_PROFILE( profile )->private->path &&
-						g_utf8_strlen( NA_OBJECT_PROFILE( profile )->private->path, -1 ) > 0 ) ||
-
-				( NA_OBJECT_PROFILE( profile )->private->parameters &&
-						g_utf8_strlen( NA_OBJECT_PROFILE( profile )->private->parameters, -1 ) > 0 );
-		}
+		is_valid =
+			is_valid_path_parameters( NA_OBJECT_PROFILE( profile )) &&
+			is_valid_filenames( NA_OBJECT_PROFILE( profile )) &&
+			is_valid_mimetypes( NA_OBJECT_PROFILE( profile )) &&
+			is_valid_isfiledir( NA_OBJECT_PROFILE( profile )) &&
+			is_valid_schemes( NA_OBJECT_PROFILE( profile ));
 	}
 
 	return( is_valid );
 }
 
+static gboolean
+is_valid_path_parameters( const NAObjectProfile *profile )
+{
+	gboolean valid;
+	gchar *command;
+	gchar *exe;
+
+	command = g_strdup_printf( "%s %s", profile->private->path, profile->private->parameters );
+	exe = na_utils_get_first_word( command );
+
+	valid =
+		g_file_test( exe, G_FILE_TEST_EXISTS ) &&
+		g_file_test( exe, G_FILE_TEST_IS_EXECUTABLE ) &&
+		!g_file_test( exe, G_FILE_TEST_IS_DIR );
+
+	g_free( exe );
+	g_free( command );
+
+	return( valid );
+}
+
+static gboolean
+is_valid_filenames( const NAObjectProfile *profile )
+{
+	gboolean valid;
+
+	valid = g_slist_length( profile->private->basenames ) > 0;
+
+	return( valid );
+}
+
+static gboolean
+is_valid_mimetypes( const NAObjectProfile *profile )
+{
+	gboolean valid;
+
+	valid = g_slist_length( profile->private->mimetypes ) > 0;
+
+	return( valid );
+}
+
+static gboolean
+is_valid_isfiledir( const NAObjectProfile *profile )
+{
+	gboolean valid;
+
+	valid = profile->private->is_file || profile->private->is_dir;
+
+	return( valid );
+}
+
+static gboolean
+is_valid_schemes( const NAObjectProfile *profile )
+{
+	gboolean valid;
+
+	valid = g_slist_length( profile->private->schemes ) > 0;
+
+	return( valid );
+}
+
 /*
  * new_parent is specifically set to be able to allocate a new id for
  * the current profile into the target parent
diff --git a/src/runtime/na-utils.c b/src/runtime/na-utils.c
index fd5cde4..1c48b34 100644
--- a/src/runtime/na-utils.c
+++ b/src/runtime/na-utils.c
@@ -191,6 +191,36 @@ na_utils_gslist_to_schema( GSList *list )
 }
 
 /**
+ * na_utils_get_first_word:
+ * @string: a space-separated string.
+ *
+ * Returns: the first word of @string, as a newly allocated string which
+ * should be g_free() by the caller.
+ */
+gchar *
+na_utils_get_first_word( const gchar *string )
+{
+	gchar **splitted, **iter;
+	gchar *word, *tmp;
+
+	splitted = g_strsplit( string, " ", 0 );
+	iter = splitted;
+	word = NULL;
+
+	while( *iter ){
+		tmp = g_strstrip( *iter );
+		if( g_utf8_strlen( tmp, -1 )){
+			word = g_strdup( tmp );
+			break;
+		}
+		iter++;
+	}
+
+	g_strfreev( splitted );
+	return( word );
+}
+
+/**
  * na_utils_path_extract_last_dir:
  * @path: a full path.
  *
diff --git a/src/runtime/na-utils.h b/src/runtime/na-utils.h
index b6831a7..7de40a3 100644
--- a/src/runtime/na-utils.h
+++ b/src/runtime/na-utils.h
@@ -47,6 +47,10 @@ gboolean na_utils_string_lists_are_equal( GSList *first, GSList *second );
  */
 gchar   *na_utils_gslist_to_schema( GSList *list );
 
+/* string manipulations
+ */
+gchar   *na_utils_get_first_word( const gchar *string );
+
 /* path manipulations
  */
 gchar   *na_utils_path_extract_last_dir( const gchar *path );



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