[gnome-builder/wip/gtk4-port] libide/projects: allow templates to validate name/app-id



commit 2fd1ff050b0ea2f43311377066150b417797a1dc
Author: Christian Hergert <chergert redhat com>
Date:   Fri May 27 14:23:49 2022 -0700

    libide/projects: allow templates to validate name/app-id

 src/libide/projects/ide-project-template.c | 89 ++++++++++++++++++++++++++++++
 src/libide/projects/ide-project-template.h | 10 ++++
 2 files changed, 99 insertions(+)
---
diff --git a/src/libide/projects/ide-project-template.c b/src/libide/projects/ide-project-template.c
index ad438415b..cfee83989 100644
--- a/src/libide/projects/ide-project-template.c
+++ b/src/libide/projects/ide-project-template.c
@@ -36,6 +36,74 @@ enum {
 
 static GParamSpec *properties [N_PROPS];
 
+static gboolean
+ide_project_template_real_validate_name (IdeProjectTemplate *self,
+                                         const char         *name)
+{
+  g_assert (IDE_IS_PROJECT_TEMPLATE (self));
+
+  if (name == NULL)
+    return FALSE;
+
+  if (g_unichar_isdigit (g_utf8_get_char (name)))
+    return FALSE;
+
+  /* TODO: Move this check to Meson template subclass */
+  if (ide_str_equal0 (name, "test"))
+    return FALSE;
+
+  for (const char *c = name; *c; c = g_utf8_next_char (c))
+    {
+      gunichar ch = g_utf8_get_char (c);
+
+      if (g_unichar_isspace (ch))
+        return FALSE;
+
+      if (ch == '/')
+        return FALSE;
+    }
+
+  return TRUE;
+}
+
+static gboolean
+ide_project_template_real_validate_app_id (IdeProjectTemplate *self,
+                                           const char         *app_id)
+{
+  guint n_dots = 0;
+
+  g_assert (IDE_IS_PROJECT_TEMPLATE (self));
+
+  /* Rely on defaults if empty */
+  if (ide_str_empty0 (app_id))
+    return TRUE;
+
+  if (!g_application_id_is_valid (app_id))
+    return FALSE;
+
+  /* Flatpak's require at least 3 parts to be valid, which is more than
+   * what g_application_id_is_valid() will require. Additionally, you
+   * cannot have "-" in Flatpak app ids.
+   */
+  for (const char *c = app_id; *c; c = g_utf8_next_char (c))
+    {
+      switch (*c)
+        {
+        case '-':
+          return FALSE;
+
+        case '.':
+          n_dots++;
+          break;
+
+        default:
+          break;
+        }
+    }
+
+  return n_dots >= 2;
+}
+
 static void
 ide_project_template_get_property (GObject    *object,
                                    guint       prop_id,
@@ -70,6 +138,9 @@ ide_project_template_class_init (IdeProjectTemplateClass *klass)
 
   object_class->get_property = ide_project_template_get_property;
 
+  klass->validate_name = ide_project_template_real_validate_name;
+  klass->validate_app_id = ide_project_template_real_validate_app_id;
+
   properties [PROP_ID] =
     g_param_spec_string ("id", NULL, NULL, NULL,
                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
@@ -221,3 +292,21 @@ ide_project_template_compare (IdeProjectTemplate *a,
 
   return ret;
 }
+
+gboolean
+ide_project_template_validate_name (IdeProjectTemplate *self,
+                                    const char         *name)
+{
+  g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), FALSE);
+
+  return IDE_PROJECT_TEMPLATE_GET_CLASS (self)->validate_name (self, name);
+}
+
+gboolean
+ide_project_template_validate_app_id (IdeProjectTemplate *self,
+                                      const char         *app_id)
+{
+  g_return_val_if_fail (IDE_IS_PROJECT_TEMPLATE (self), FALSE);
+
+  return IDE_PROJECT_TEMPLATE_GET_CLASS (self)->validate_app_id (self, app_id);
+}
diff --git a/src/libide/projects/ide-project-template.h b/src/libide/projects/ide-project-template.h
index 8e21e8cde..a7ee63081 100644
--- a/src/libide/projects/ide-project-template.h
+++ b/src/libide/projects/ide-project-template.h
@@ -54,6 +54,10 @@ struct _IdeProjectTemplateClass
                                   GAsyncResult         *result,
                                   GError              **error);
   gint        (*get_priority)    (IdeProjectTemplate   *self);
+  gboolean    (*validate_name)   (IdeProjectTemplate   *self,
+                                  const char           *name);
+  gboolean    (*validate_app_id) (IdeProjectTemplate   *self,
+                                  const char           *app_id);
 };
 
 IDE_AVAILABLE_IN_ALL
@@ -81,5 +85,11 @@ gboolean    ide_project_template_expand_finish   (IdeProjectTemplate   *self,
 IDE_AVAILABLE_IN_ALL
 gint        ide_project_template_compare         (IdeProjectTemplate   *a,
                                                   IdeProjectTemplate   *b);
+IDE_AVAILABLE_IN_ALL
+gboolean    ide_project_template_validate_name   (IdeProjectTemplate   *self,
+                                                  const char           *name);
+IDE_AVAILABLE_IN_ALL
+gboolean    ide_project_template_validate_app_id (IdeProjectTemplate   *self,
+                                                  const char           *app_id);
 
 G_END_DECLS


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