[anjuta] am-project: Display an error when trying to add a directory as a source



commit 4cff4a02c06043b87759590cfee184d987988f56
Author: SÃbastien Granjoux <seb sfo free fr>
Date:   Thu Feb 16 21:44:58 2012 +0100

    am-project: Display an error when trying to add a directory as a source

 plugins/am-project/amp-node.c   |   27 ++++++++++++++++++++++++
 plugins/am-project/amp-node.h   |    7 ++++-
 plugins/am-project/amp-source.c |   10 +++++++++
 plugins/am-project/amp-target.c |   43 +++++++++-----------------------------
 4 files changed, 52 insertions(+), 35 deletions(-)
---
diff --git a/plugins/am-project/amp-node.c b/plugins/am-project/amp-node.c
index cf64fce..89fc471 100644
--- a/plugins/am-project/amp-node.c
+++ b/plugins/am-project/amp-node.c
@@ -37,6 +37,7 @@
 #include "amp-source.h"
 
 #include <libanjuta/anjuta-debug.h>
+#include <libanjuta/interfaces/ianjuta-project.h>
 
 #include <glib/gi18n.h>
 
@@ -45,6 +46,32 @@
 #include <ctype.h>
 
 
+/* Helper functions
+ *---------------------------------------------------------------------------*/
+
+void
+amp_set_error (GError **error, gint code, const gchar *message)
+{
+        if (error != NULL) {
+                if (*error != NULL) {
+                        gchar *tmp;
+
+                        /* error already created, just change the code
+                         * and prepend the string */
+                        (*error)->code = code;
+                        tmp = (*error)->message;
+                        (*error)->message = g_strconcat (message, "\n\n", tmp, NULL);
+                        g_free (tmp);
+
+                } else {
+                        *error = g_error_new_literal (IANJUTA_PROJECT_ERROR,
+                                                      code,
+                                                      message);
+                }
+        }
+}
+
+
 /* Public functions
  *---------------------------------------------------------------------------*/
 
diff --git a/plugins/am-project/amp-node.h b/plugins/am-project/amp-node.h
index bfddadb..fddc3cc 100644
--- a/plugins/am-project/amp-node.h
+++ b/plugins/am-project/amp-node.h
@@ -69,7 +69,7 @@ struct _AmpNodeClass
 											                AmpNode *parent,
 											                AmpProject *project,
 											                GError **error);
-	
+
 	gboolean					(*update)				(AmpNode *node,
 											                AmpNode *parent);
 
@@ -77,7 +77,7 @@ struct _AmpNodeClass
 											                AmpNode *parent,
 											                AmpProject *project,
 											                GError **error);
-	
+
 	gboolean					(*write)				(AmpNode *node,
 											                AmpNode *parent,
 											                AmpProject *project,
@@ -87,6 +87,9 @@ struct _AmpNodeClass
 GType amp_node_get_type (void) G_GNUC_CONST;
 
 
+void				amp_set_error (GError **error, gint code, const gchar *message);
+
+
 AnjutaProjectNode * amp_node_new_valid				(AnjutaProjectNode *parent,
 				                                 AnjutaProjectNodeType type,
 				                                 GFile *file,
diff --git a/plugins/am-project/amp-source.c b/plugins/am-project/amp-source.c
index 71fb1eb..ead7589 100644
--- a/plugins/am-project/amp-source.c
+++ b/plugins/am-project/amp-source.c
@@ -89,6 +89,16 @@ amp_source_node_new (GFile *file, AnjutaProjectNodeType type)
 AnjutaProjectNode*
 amp_source_node_new_valid (GFile *file, AnjutaProjectNodeType type, GError **error)
 {
+	/* Validate source name */
+
+	if (g_file_query_file_type (file, G_FILE_QUERY_INFO_NONE, NULL) == G_FILE_TYPE_DIRECTORY)
+	{
+		amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+		               _("Source file must be a regular file, not a directory"));
+
+		return NULL;
+	}
+
 	return amp_source_node_new (file, type);
 }
 
diff --git a/plugins/am-project/amp-target.c b/plugins/am-project/amp-target.c
index 9d6a5a5..a33d0f4 100644
--- a/plugins/am-project/amp-target.c
+++ b/plugins/am-project/amp-target.c
@@ -55,30 +55,7 @@ struct _AmpTargetNode {
 
 
 
-/* Helper functions
- *---------------------------------------------------------------------------*/
 
-static void
-error_set (GError **error, gint code, const gchar *message)
-{
-        if (error != NULL) {
-                if (*error != NULL) {
-                        gchar *tmp;
-
-                        /* error already created, just change the code
-                         * and prepend the string */
-                        (*error)->code = code;
-                        tmp = (*error)->message;
-                        (*error)->message = g_strconcat (message, "\n\n", tmp, NULL);
-                        g_free (tmp);
-
-                } else {
-                        *error = g_error_new_literal (IANJUTA_PROJECT_ERROR,
-                                                      code,
-                                                      message);
-                }
-        }
-}
 
 
 /* Tagged token list
@@ -358,8 +335,8 @@ amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const
 	/* Validate target name */
 	if (!name || strlen (name) <= 0)
 	{
-		error_set (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
-			   _("Please specify target name"));
+		amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+		               _("Please specify target name"));
 		return NULL;
 	}
 	{
@@ -372,8 +349,8 @@ amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const
 			ptr++;
 		}
 		if (failed) {
-			error_set (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
-				   _("Target name can only contain alphanumeric, '_', '-', '/' or '.' characters"));
+			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+			               _("Target name can only contain alphanumeric, '_', '-', '/' or '.' characters"));
 			return NULL;
 		}
 	}
@@ -387,8 +364,8 @@ amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const
 		if (strlen (basename) < 7 ||
 		    strncmp (basename, "lib", strlen("lib")) != 0 ||
 		    strcmp (&basename[strlen(basename) - 3], ".la") != 0) {
-			error_set (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
-				   _("Shared library target name must be of the form 'libxxx.la'"));
+			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+			               _("Shared library target name must be of the form 'libxxx.la'"));
 			return NULL;
 		}
 	}
@@ -396,16 +373,16 @@ amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const
 		if (strlen (basename) < 6 ||
 		    strncmp (basename, "lib", strlen("lib")) != 0 ||
 		    strcmp (&basename[strlen(basename) - 2], ".a") != 0) {
-			error_set (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
-				   _("Static library target name must be of the form 'libxxx.a'"));
+			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+			               _("Static library target name must be of the form 'libxxx.a'"));
 			return NULL;
 		}
 	}
 	else if ((type & ANJUTA_PROJECT_ID_MASK) == ANJUTA_PROJECT_LT_MODULE) {
 		if (strlen (basename) < 4 ||
 		    strcmp (&basename[strlen(basename) - 3], ".la") != 0) {
-			error_set (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
-				   _("Module target name must be of the form 'xxx.la'"));
+			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+			               _("Module target name must be of the form 'xxx.la'"));
 			return NULL;
 		}
 	}



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