[gnome-builder/wip/gtk4-port] libide/projects: add validation helper



commit beffc9ca97d7a0cd24c9a01ead2d8ef81203739c
Author: Christian Hergert <chergert redhat com>
Date:   Fri May 27 15:37:42 2022 -0700

    libide/projects: add validation helper
    
    This will validate input to see if we are likely to suceed at generating
    the particular template.

 src/libide/projects/ide-template-input.c | 61 +++++++++++++++++++++++++
 src/libide/projects/ide-template-input.h | 76 ++++++++++++++++++--------------
 2 files changed, 105 insertions(+), 32 deletions(-)
---
diff --git a/src/libide/projects/ide-template-input.c b/src/libide/projects/ide-template-input.c
index 4fa8a107e..03c8e7be2 100644
--- a/src/libide/projects/ide-template-input.c
+++ b/src/libide/projects/ide-template-input.c
@@ -934,3 +934,64 @@ ide_template_input_get_licenses_model (IdeTemplateInput *self)
 
   return G_LIST_MODEL (self->licenses);
 }
+
+static IdeProjectTemplate *
+find_template (IdeTemplateInput *self,
+               const char       *template_id)
+{
+  GListModel *model;
+  guint n_items;
+
+  g_assert (IDE_IS_TEMPLATE_INPUT (self));
+
+  if (template_id == NULL)
+    return NULL;
+
+  model = G_LIST_MODEL (self->templates);
+  n_items = g_list_model_get_n_items (model);
+
+  for (guint i = 0; i < n_items; i++)
+    {
+      g_autoptr(IdeProjectTemplate) template = g_list_model_get_item (model, i);
+      g_autofree char *id = ide_project_template_get_id (template);
+
+      if (ide_str_equal0 (template_id, id))
+        return g_steal_pointer (&template);
+    }
+
+  return NULL;
+}
+
+IdeTemplateInputValidation
+ide_template_input_validate (IdeTemplateInput *self)
+{
+  IdeTemplateInputValidation flags = 0;
+  IdeProjectTemplate *template;
+  g_autoptr(GFile) dest = NULL;
+  g_auto(GStrv) languages = NULL;
+
+  g_return_val_if_fail (IDE_IS_TEMPLATE_INPUT (self), 0);
+
+  if (!(template = find_template (self, self->template)))
+    flags |= IDE_TEMPLATE_INPUT_INVAL_TEMPLATE;
+
+  if (template && !ide_project_template_validate_app_id (template, self->app_id))
+    flags |= IDE_TEMPLATE_INPUT_INVAL_APP_ID;
+
+  if (template && !ide_project_template_validate_name (template, self->name))
+    flags |= IDE_TEMPLATE_INPUT_INVAL_NAME;
+
+  if (self->directory == NULL ||
+      self->name == NULL ||
+      !(dest = g_file_get_child (self->directory, self->name)) ||
+      g_file_query_exists (dest, NULL))
+    flags |= IDE_TEMPLATE_INPUT_INVAL_LOCATION;
+
+  if (self->language == NULL ||
+      self->template == NULL ||
+      !(languages = ide_project_template_get_languages (template)) ||
+      !g_strv_contains ((const char * const *)languages, self->language))
+    flags |= IDE_TEMPLATE_INPUT_INVAL_LANGUAGE;
+
+  return flags;
+}
diff --git a/src/libide/projects/ide-template-input.h b/src/libide/projects/ide-template-input.h
index b4d150497..1b0deb14b 100644
--- a/src/libide/projects/ide-template-input.h
+++ b/src/libide/projects/ide-template-input.h
@@ -32,63 +32,75 @@ G_BEGIN_DECLS
 
 #define IDE_TYPE_TEMPLATE_INPUT (ide_template_input_get_type())
 
+typedef enum
+{
+  IDE_TEMPLATE_INPUT_VALID          = 0,
+  IDE_TEMPLATE_INPUT_INVAL_NAME     = 1 << 0,
+  IDE_TEMPLATE_INPUT_INVAL_APP_ID   = 1 << 1,
+  IDE_TEMPLATE_INPUT_INVAL_LOCATION = 1 << 2,
+  IDE_TEMPLATE_INPUT_INVAL_LANGUAGE = 1 << 3,
+  IDE_TEMPLATE_INPUT_INVAL_TEMPLATE = 1 << 4,
+} IdeTemplateInputValidation;
+
 IDE_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (IdeTemplateInput, ide_template_input, IDE, TEMPLATE_INPUT, GObject)
 
 IDE_AVAILABLE_IN_ALL
-IdeTemplateInput *ide_template_input_new                     (void);
+IdeTemplateInput           *ide_template_input_new                     (void);
+IDE_AVAILABLE_IN_ALL
+const char                 *ide_template_input_get_author              (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-const char       *ide_template_input_get_author              (IdeTemplateInput *self);
+void                        ide_template_input_set_author              (IdeTemplateInput *self,
+                                                                        const char       *author);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_author              (IdeTemplateInput *self,
-                                                              const char       *author);
+GFile                      *ide_template_input_get_directory           (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-GFile            *ide_template_input_get_directory           (IdeTemplateInput *self);
+void                        ide_template_input_set_directory           (IdeTemplateInput *self,
+                                                                        GFile            *directory);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_directory           (IdeTemplateInput *self,
-                                                              GFile            *directory);
+const char                 *ide_template_input_get_language            (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-const char       *ide_template_input_get_language            (IdeTemplateInput *self);
+void                        ide_template_input_set_language            (IdeTemplateInput *self,
+                                                                        const char       *language);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_language            (IdeTemplateInput *self,
-                                                              const char       *language);
+gboolean                    ide_template_input_get_use_version_control (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-gboolean          ide_template_input_get_use_version_control (IdeTemplateInput *self);
+void                        ide_template_input_set_use_version_control (IdeTemplateInput *self,
+                                                                        gboolean          
use_version_control);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_use_version_control (IdeTemplateInput *self,
-                                                              gboolean          use_version_control);
+const char                 *ide_template_input_get_name                (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-const char       *ide_template_input_get_name                (IdeTemplateInput *self);
+void                        ide_template_input_set_name                (IdeTemplateInput *self,
+                                                                        const char       *name);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_name                (IdeTemplateInput *self,
-                                                              const char       *name);
+const char                 *ide_template_input_get_app_id              (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-const char       *ide_template_input_get_app_id              (IdeTemplateInput *self);
+void                        ide_template_input_set_app_id              (IdeTemplateInput *self,
+                                                                        const char       *app_id);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_app_id              (IdeTemplateInput *self,
-                                                              const char       *app_id);
+const char                 *ide_template_input_get_project_version     (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-const char       *ide_template_input_get_project_version     (IdeTemplateInput *self);
+void                        ide_template_input_set_project_version     (IdeTemplateInput *self,
+                                                                        const char       *project_version);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_project_version     (IdeTemplateInput *self,
-                                                              const char       *project_version);
+const char                 *ide_template_input_get_license_name        (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-const char       *ide_template_input_get_license_name        (IdeTemplateInput *self);
+void                        ide_template_input_set_license_name        (IdeTemplateInput *self,
+                                                                        const char       *license_name);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_license_name        (IdeTemplateInput *self,
-                                                              const char       *license_name);
+const char                 *ide_template_input_get_template            (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-const char       *ide_template_input_get_template            (IdeTemplateInput *self);
+void                        ide_template_input_set_template            (IdeTemplateInput *self,
+                                                                        const char       *template);
 IDE_AVAILABLE_IN_ALL
-void              ide_template_input_set_template            (IdeTemplateInput *self,
-                                                              const char       *template);
+GListModel                 *ide_template_input_get_templates_model     (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-GListModel       *ide_template_input_get_templates_model     (IdeTemplateInput *self);
+GListModel                 *ide_template_input_get_languages_model     (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-GListModel       *ide_template_input_get_languages_model     (IdeTemplateInput *self);
+GListModel                 *ide_template_input_get_licenses_model      (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-GListModel       *ide_template_input_get_licenses_model      (IdeTemplateInput *self);
+TmplScope                  *ide_template_input_to_scope                (IdeTemplateInput *self);
 IDE_AVAILABLE_IN_ALL
-TmplScope        *ide_template_input_to_scope                (IdeTemplateInput *self);
+IdeTemplateInputValidation  ide_template_input_validate                (IdeTemplateInput *self);
 
 G_END_DECLS


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