[gtranslator/gtk4: 56/79] Move GtrDropDownOption class to their own file




commit b4511970972750f0e0b99e6937960b8dfab0abec
Author: Daniel GarcĂ­a Moreno <dani danigm net>
Date:   Fri Jul 22 14:47:49 2022 +0200

    Move GtrDropDownOption class to their own file

 src/gtr-dl-teams.c         | 140 ++-------------------------------------
 src/gtr-drop-down-option.c | 158 +++++++++++++++++++++++++++++++++++++++++++++
 src/gtr-drop-down-option.h |  35 ++++++++++
 src/meson.build            |   1 +
 4 files changed, 199 insertions(+), 135 deletions(-)
---
diff --git a/src/gtr-dl-teams.c b/src/gtr-dl-teams.c
index c20ab7ac..cf9d0219 100644
--- a/src/gtr-dl-teams.c
+++ b/src/gtr-dl-teams.c
@@ -25,6 +25,7 @@
 #include "gtr-window.h"
 #include "gtr-utils.h"
 #include "gtr-profile-manager.h"
+#include "gtr-drop-down-option.h"
 #include <libsoup/soup.h>
 
 #include <json-glib/json-glib.h>
@@ -32,137 +33,6 @@
 
 #include <glib/gi18n.h>
 
-
-#define GTR_TYPE_DROP_DOWN_OPTION (gtr_drop_down_option_get_type ())
-G_DECLARE_FINAL_TYPE (GtrDropDownOption, gtr_drop_down_option, GTR, DROP_DOWN_OPTION, GObject)
-
-enum {
-  PROP_0,
-  PROP_NAME,
-  PROP_DESCRIPTION,
-  N_PROPERTIES
-};
-
-static GParamSpec *option_properties[N_PROPERTIES] = { NULL, };
-
-struct _GtrDropDownOption {
-  GObject parent_instance;
-  char *name;
-  char *description;
-};
-
-G_DEFINE_TYPE (GtrDropDownOption, gtr_drop_down_option, G_TYPE_OBJECT);
-
-static void
-gtr_drop_down_option_init (GtrDropDownOption *option)
-{
-  option->name = NULL;
-  option->description = NULL;
-}
-
-static void
-gtr_drop_down_option_finalize (GObject *object)
-{
-  GtrDropDownOption *option = GTR_DROP_DOWN_OPTION (object);
-
-  g_free (option->name);
-  g_free (option->description);
-
-  G_OBJECT_CLASS (gtr_drop_down_option_parent_class)->finalize (object);
-}
-
-static void
-gtr_drop_down_set_property (GObject      *object,
-                            guint         property_id,
-                            const GValue *value,
-                            GParamSpec   *pspec)
-{
-  GtrDropDownOption *self = GTR_DROP_DOWN_OPTION (object);
-
-  switch (property_id)
-    {
-    case PROP_NAME:
-      if (self->name)
-        g_free (self->name);
-      self->name = g_value_dup_string (value);
-      break;
-    case PROP_DESCRIPTION:
-      if (self->description)
-        g_free (self->description);
-      self->description = g_value_dup_string (value);
-      break;
-    default:
-      /* We don't have any other property... */
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-      break;
-    }
-}
-
-static void
-gtr_drop_down_get_property (GObject    *object,
-                            guint       property_id,
-                            GValue     *value,
-                            GParamSpec *pspec)
-{
-  GtrDropDownOption *self = GTR_DROP_DOWN_OPTION (object);
-
-  switch (property_id)
-    {
-    case PROP_NAME:
-      g_value_set_string (value, self->name);
-      break;
-    case PROP_DESCRIPTION:
-      g_value_set_string (value, self->description);
-      break;
-    default:
-      /* We don't have any other property... */
-      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-      break;
-    }
-}
-
-static void
-gtr_drop_down_option_class_init (GtrDropDownOptionClass *class)
-{
-  GObjectClass *object_class = G_OBJECT_CLASS (class);
-
-  object_class->finalize = gtr_drop_down_option_finalize;
-  object_class->set_property = gtr_drop_down_set_property;
-  object_class->get_property = gtr_drop_down_get_property;
-
-  option_properties[PROP_NAME] =
-    g_param_spec_string ("name", "Name", "Name",
-                      NULL,
-                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
-
-  option_properties[PROP_DESCRIPTION] =
-    g_param_spec_string ("description", "Description", "Description",
-                      NULL,
-                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
-
-  g_object_class_install_properties (object_class,
-                                     N_PROPERTIES,
-                                     option_properties);
-}
-
-static GtrDropDownOption *
-gtr_drop_down_option_new (const char *name, const char *description)
-{
-  GtrDropDownOption *option = g_object_new (GTR_TYPE_DROP_DOWN_OPTION,
-                                            "name", name,
-                                            "description", description,
-                                            NULL);
-  return option;
-}
-
-static gboolean
-gtr_drop_down_option_equal (GtrDropDownOption *opt1, GtrDropDownOption *opt2)
-{
-    if (strcmp (opt1->name, opt2->name))
-        return FALSE;
-    return TRUE;
-}
-
 typedef struct
 {
   GtkWidget *titlebar;
@@ -894,28 +764,28 @@ gtr_dl_teams_save_combo_selected (GtkWidget  *widget,
 
   if (strcmp(name, "combo_modules") == 0)
     {
-      const GtrDropDownOption *opt = GTR_DROP_DOWN_OPTION (
+      GtrDropDownOption *opt = GTR_DROP_DOWN_OPTION (
         gtk_drop_down_get_selected_item (GTK_DROP_DOWN (priv->modules_combobox))
       );
       if (!opt)
         return;
       if (priv->selected_module)
         g_free (priv->selected_module);
-      priv->selected_module = g_strdup (opt->name);
+      priv->selected_module = g_strdup (gtr_drop_down_option_get_name (opt));
 
       /* Reload module details on module change */
       gtr_dl_teams_load_module_details_json (widget, self);
     }
   else if (strcmp(name, "combo_teams") == 0)
     {
-      const GtrDropDownOption *opt = GTR_DROP_DOWN_OPTION (
+      GtrDropDownOption *opt = GTR_DROP_DOWN_OPTION (
         gtk_drop_down_get_selected_item (GTK_DROP_DOWN (priv->teams_combobox))
       );
       if (!opt)
         return;
       if (priv->selected_team)
         g_free (priv->selected_team);
-      priv->selected_team = g_strdup (opt->name);
+      priv->selected_team = g_strdup (gtr_drop_down_option_get_name (opt));
     }
   else if (strcmp(name, "combo_branches") == 0)
     {
diff --git a/src/gtr-drop-down-option.c b/src/gtr-drop-down-option.c
new file mode 100644
index 00000000..bb642a5f
--- /dev/null
+++ b/src/gtr-drop-down-option.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2022  Daniel Garcia Moreno <danigm gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <gtr-drop-down-option.h>
+
+enum {
+  PROP_0,
+  PROP_NAME,
+  PROP_DESCRIPTION,
+  N_PROPERTIES
+};
+
+static GParamSpec *option_properties[N_PROPERTIES] = { NULL, };
+
+struct _GtrDropDownOption {
+  GObject parent_instance;
+  char *name;
+  char *description;
+};
+
+G_DEFINE_TYPE (GtrDropDownOption, gtr_drop_down_option, G_TYPE_OBJECT);
+
+static void
+gtr_drop_down_option_init (GtrDropDownOption *option)
+{
+  option->name = NULL;
+  option->description = NULL;
+}
+
+static void
+gtr_drop_down_option_finalize (GObject *object)
+{
+  GtrDropDownOption *option = GTR_DROP_DOWN_OPTION (object);
+
+  g_free (option->name);
+  g_free (option->description);
+
+  G_OBJECT_CLASS (gtr_drop_down_option_parent_class)->finalize (object);
+}
+
+static void
+gtr_drop_down_set_property (GObject      *object,
+                            guint         property_id,
+                            const GValue *value,
+                            GParamSpec   *pspec)
+{
+  GtrDropDownOption *self = GTR_DROP_DOWN_OPTION (object);
+
+  switch (property_id)
+    {
+    case PROP_NAME:
+      if (self->name)
+        g_free (self->name);
+      self->name = g_value_dup_string (value);
+      break;
+    case PROP_DESCRIPTION:
+      if (self->description)
+        g_free (self->description);
+      self->description = g_value_dup_string (value);
+      break;
+    default:
+      /* We don't have any other property... */
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gtr_drop_down_get_property (GObject    *object,
+                            guint       property_id,
+                            GValue     *value,
+                            GParamSpec *pspec)
+{
+  GtrDropDownOption *self = GTR_DROP_DOWN_OPTION (object);
+
+  switch (property_id)
+    {
+    case PROP_NAME:
+      g_value_set_string (value, self->name);
+      break;
+    case PROP_DESCRIPTION:
+      g_value_set_string (value, self->description);
+      break;
+    default:
+      /* We don't have any other property... */
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gtr_drop_down_option_class_init (GtrDropDownOptionClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->finalize = gtr_drop_down_option_finalize;
+  object_class->set_property = gtr_drop_down_set_property;
+  object_class->get_property = gtr_drop_down_get_property;
+
+  option_properties[PROP_NAME] =
+    g_param_spec_string ("name", "Name", "Name",
+                      NULL,
+                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
+
+  option_properties[PROP_DESCRIPTION] =
+    g_param_spec_string ("description", "Description", "Description",
+                      NULL,
+                      G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
+
+  g_object_class_install_properties (object_class,
+                                     N_PROPERTIES,
+                                     option_properties);
+}
+
+GtrDropDownOption *
+gtr_drop_down_option_new (const char *name, const char *description)
+{
+  GtrDropDownOption *option = g_object_new (GTR_TYPE_DROP_DOWN_OPTION,
+                                            "name", name,
+                                            "description", description,
+                                            NULL);
+  return option;
+}
+
+gboolean
+gtr_drop_down_option_equal (GtrDropDownOption *opt1, GtrDropDownOption *opt2)
+{
+    if (strcmp (opt1->name, opt2->name))
+        return FALSE;
+    return TRUE;
+}
+
+const char *
+gtr_drop_down_option_get_name (GtrDropDownOption *opt)
+{
+  return opt->name;
+}
+
+const char *
+gtr_drop_down_option_get_description (GtrDropDownOption *opt)
+{
+  return opt->description;
+}
diff --git a/src/gtr-drop-down-option.h b/src/gtr-drop-down-option.h
new file mode 100644
index 00000000..682efdd8
--- /dev/null
+++ b/src/gtr-drop-down-option.h
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2022  Daniel Garcia Moreno <danigm gnome org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#pragma once
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GTR_TYPE_DROP_DOWN_OPTION (gtr_drop_down_option_get_type ())
+
+G_DECLARE_FINAL_TYPE (GtrDropDownOption, gtr_drop_down_option, GTR, DROP_DOWN_OPTION, GObject)
+
+GtrDropDownOption*  gtr_drop_down_option_new (const char *name, const char *description);
+gboolean gtr_drop_down_option_equal (GtrDropDownOption *opt1, GtrDropDownOption *opt2);
+const char *gtr_drop_down_option_get_name (GtrDropDownOption *opt);
+const char *gtr_drop_down_option_get_description (GtrDropDownOption *opt);
+
+G_END_DECLS
+
diff --git a/src/meson.build b/src/meson.build
index 475a2645..85f4e2a7 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -67,6 +67,7 @@ sources = files(
   'gtr-view.c',
   'gtr-projects.c',
   'gtr-dl-teams.c',
+  'gtr-drop-down-option.c',
   'gtr-filter-selection.c',
   'gtr-lang-button.c',
   'gtr-progress.c',


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