[gnome-notes/wip-note-new-gobject: 7/7] import-provider: Port to G_DECLARE_FINAL_TYPE Related to #82




commit 3a0f8f0fc802a56556a790381de4176a9113b7c5
Author: Isaque Galdino <igaldino gmail com>
Date:   Tue Jan 26 21:36:57 2021 -0300

    import-provider: Port to G_DECLARE_FINAL_TYPE
    Related to #82

 src/libbiji/provider/biji-import-provider.c | 291 +++++++++++-----------------
 src/libbiji/provider/biji-import-provider.h |  36 +---
 2 files changed, 116 insertions(+), 211 deletions(-)
---
diff --git a/src/libbiji/provider/biji-import-provider.c b/src/libbiji/provider/biji-import-provider.c
index 10f428d..cdd5cc0 100644
--- a/src/libbiji/provider/biji-import-provider.c
+++ b/src/libbiji/provider/biji-import-provider.c
@@ -17,8 +17,6 @@
  * with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-
-
 /*
  * TODO : re-implement notebooks
  * TODO : re-implement old imports preserving
@@ -27,7 +25,6 @@
 #include "biji-import-provider.h"
 #include "../deserializer/biji-tomboy-reader.h"
 
-
 /* Properties */
 enum {
   PROP_0,
@@ -38,222 +35,164 @@ enum {
 
 static GParamSpec *properties[IMPORT_PROV_PROP] = { NULL, };
 
-
-
-
-struct BijiImportProviderPrivate_
+struct _BijiImportProvider
 {
-  BijiProviderInfo info;
-  gchar            *uri;
-  gchar            *target; // the provider to import to
+  BijiProvider      parent_instance;
+  BijiProviderInfo  info;
+  char             *uri;
+  char             *target; // the provider to import to
   GHashTable       *items; // same as manager, notes key=path, coll key = name.
 };
 
-
-
-
-
-static void biji_import_provider_finalize (GObject *object);
-
-G_DEFINE_TYPE_WITH_PRIVATE (BijiImportProvider, biji_import_provider, BIJI_TYPE_PROVIDER)
-
+G_DEFINE_TYPE (BijiImportProvider, biji_import_provider, BIJI_TYPE_PROVIDER)
 
 #define ATTRIBUTES_FOR_NOTEBOOK "standard::content-type,standard::name"
 
-
-
-
-
 static BijiNoteObj *
-instanciate_note (BijiImportProvider *self, GFileInfo *info, GFile *container)
+instanciate_note (BijiImportProvider *self,
+                  GFileInfo          *info,
+                  GFile              *container)
 {
-  BijiNoteObj *retval = NULL;
-  const gchar *name;
-  gchar *path;
-  GdkRGBA *color;
-  BijiManager *manager;
-  GError *error = NULL;
-  BijiInfoSet *set;
-  gchar *html = NULL;
-  GList *notebooks = NULL;
-
-
-  retval = NULL;
-  manager = biji_provider_get_manager (BIJI_PROVIDER (self));
+  BijiNoteObj        *retval    = NULL;
+  GdkRGBA             color;
+  BijiManager        *manager   = biji_provider_get_manager (BIJI_PROVIDER (self));
+  BijiInfoSet        *set;
+  g_autofree char    *path      = NULL;
+  g_autofree char    *html      = NULL;
+  g_autoptr (GList)   notebooks = NULL;
+  g_autoptr (GError)  error     = NULL;
+  const char         *name      = g_file_info_get_name (info);
 
   /* First make sure it's a note */
-  name = g_file_info_get_name (info);
   if (!g_str_has_suffix (name, ".note"))
     return NULL;
 
   path = g_build_filename (g_file_get_path (container), name, NULL);
 
-
   /* Deserialize it */
-  biji_tomboy_reader_read   (path,
-                             &error,
-                             &set,
-                             &html,
-                             &notebooks);
+  biji_tomboy_reader_read (path, &error, &set, &html, &notebooks);
 
   if (error != NULL)
-  {
-    g_warning ("Could not import %s - %s", path, error->message);
-    g_free (path);
-    g_error_free (error);
-    return NULL;
-  }
-
+    {
+      g_warning ("Could not import %s - %s", path, error->message);
+      return NULL;
+    }
 
   g_debug ("note %s:%s \n%s\n%s", path, set->title, set->content, html);
 
-
-
   /* Create the note w/ default color */
-  color = g_new0 (GdkRGBA, 1);
-  biji_manager_get_default_color (manager, color);
+  biji_manager_get_default_color (manager, &color);
   retval = biji_manager_note_new_full (manager,
-                                         self->priv->target,
-                                         g_strdup (g_file_info_get_name (info)),
-                                         set,
-                                         html,
-                                         color);
+                                       self->target,
+                                       g_strdup (g_file_info_get_name (info)),
+                                       set,
+                                       html,
+                                       &color);
 
   BIJI_NOTE_OBJ_GET_CLASS (retval)->save_note (retval);
 
   return retval;
 }
 
-
-
 static void
-release_enum_cb (GObject *source, GAsyncResult *res, gpointer user_data)
+release_enum_cb (GObject      *source,
+                 GAsyncResult *res,
+                 gpointer      user_data)
 {
   g_file_enumerator_close_finish (G_FILE_ENUMERATOR (source), res, NULL);
   g_object_unref (source);
 }
 
-
-
 /* Some notes might have been added previously */
 static void
-go_through_notes_cb (GObject *object, GAsyncResult *res, gpointer data)
+go_through_notes_cb (GObject      *object,
+                     GAsyncResult *res,
+                     gpointer      data)
 {
-  GFileEnumerator *enumerator;
-  GList *l, *notes_info;
-  GFile *container;
-  BijiImportProvider *self;
-
-
-  enumerator = G_FILE_ENUMERATOR (object);
-  self = data;
-
-
-  container = g_file_enumerator_get_container (enumerator);
-  notes_info = g_file_enumerator_next_files_finish (enumerator, res, NULL);
-  g_file_enumerator_close_async (enumerator, G_PRIORITY_DEFAULT, NULL,
-                                 release_enum_cb, self);
-
+  GFileEnumerator    *enumerator = G_FILE_ENUMERATOR (object);
+  GFile              *container  = g_file_enumerator_get_container (enumerator);
+  GList              *notes_info = g_file_enumerator_next_files_finish (enumerator, res, NULL);
+  BijiImportProvider *self       = data;
 
+  g_file_enumerator_close_async (enumerator, G_PRIORITY_DEFAULT, NULL, release_enum_cb, self);
 
   /* Get the GList of notes and load them */
-  for ( l=notes_info; l !=NULL; l = l->next)
-  {
-    GFileInfo *info;
-    BijiNoteObj *iter;
-
-    info = G_FILE_INFO (l->data);
-    iter = instanciate_note (
-                  self,
-                  info,
-                  container);
-
-    if (iter != NULL)
+  for (GList *l = notes_info; l != NULL; l = l->next)
     {
-      g_hash_table_insert (self->priv->items,
-                           (gpointer) biji_item_get_uuid (BIJI_ITEM (iter)),
-                           (gpointer) iter);
+      GFileInfo   *info = G_FILE_INFO (l->data);
+      BijiNoteObj *iter = instanciate_note (self, info, container);
+
+      if (iter != NULL)
+        {
+          g_hash_table_insert (self->items,
+                               (gpointer) biji_item_get_uuid (BIJI_ITEM (iter)),
+                               (gpointer) iter);
+        }
     }
-  }
 
   g_list_free_full (notes_info, g_object_unref);
   BIJI_PROVIDER_GET_CLASS (self)->notify_loaded (BIJI_PROVIDER (self),
-                                                 g_hash_table_get_values (self->priv->items),
+                                                 g_hash_table_get_values (self->items),
                                                  BIJI_LIVING_ITEMS);
 
-
-
-
   /* Goodbye */
   g_object_unref (self);
 }
 
-
-
-
 static void
-list_notes_to_copy (GObject *src_obj, GAsyncResult *res, gpointer data)
+list_notes_to_copy (GObject      *src_obj,
+                    GAsyncResult *res,
+                    gpointer      data)
 {
-  GFileEnumerator *enumerator;
-  GError *error;
-  BijiImportProvider *self;
-
-  self = data;
-  error = NULL;
-  enumerator = g_file_enumerate_children_finish (G_FILE (src_obj), res, &error);
+  g_autoptr(GError)   error      = NULL;
+  BijiImportProvider *self       = data;
+  GFileEnumerator    *enumerator = g_file_enumerate_children_finish (G_FILE (src_obj), res, &error);
 
   if (error)
-  {
-    g_warning ("Enumerator failed : %s", error->message);
-    g_error_free (error);
-    g_file_enumerator_close_async (enumerator, G_PRIORITY_DEFAULT, NULL, release_enum_cb, self);
-  }
-
+    {
+      g_warning ("Enumerator failed : %s", error->message);
+      g_file_enumerator_close_async (enumerator, G_PRIORITY_DEFAULT, NULL, release_enum_cb, self);
+    }
   else
-  {
-    g_file_enumerator_next_files_async (enumerator, G_MAXINT, G_PRIORITY_DEFAULT, NULL,
-                                        go_through_notes_cb, self);
-  }
+    g_file_enumerator_next_files_async (enumerator,
+                                        G_MAXINT,
+                                        G_PRIORITY_DEFAULT,
+                                        NULL,
+                                        go_through_notes_cb,
+                                        self);
 }
 
-
-
 static void
 biji_import_provider_constructed (GObject *object)
 {
-  BijiImportProvider *self;
-  BijiImportProviderPrivate *priv;
-  GFile *to_import;
-
-
-  self = BIJI_IMPORT_PROVIDER (object);
-  priv = self->priv;
-
-
-  priv->info.unique_id = NULL;
-  priv->info.datasource = NULL;
-  priv->info.name = (gchar *) "import-provider";
-  priv->info.icon =  NULL;
-  priv->info.domain = NULL;
-  priv->info.user = NULL;
-
-
-  to_import = g_file_new_for_uri (self->priv->uri);
-
-  g_file_enumerate_children_async (to_import, ATTRIBUTES_FOR_NOTEBOOK,
-                                   G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
-                                   NULL, list_notes_to_copy, self);
-
-  g_object_unref (to_import);
+  BijiImportProvider *self      = BIJI_IMPORT_PROVIDER (object);
+  g_autoptr(GFile)    to_import = NULL;
+
+  self->info.unique_id  = NULL;
+  self->info.datasource = NULL;
+  self->info.name       = (char *) "import-provider";
+  self->info.icon       = NULL;
+  self->info.domain     = NULL;
+  self->info.user       = NULL;
+
+  to_import = g_file_new_for_uri (self->uri);
+
+  g_file_enumerate_children_async (to_import,
+                                   ATTRIBUTES_FOR_NOTEBOOK,
+                                   G_FILE_QUERY_INFO_NONE,
+                                   G_PRIORITY_DEFAULT,
+                                   NULL,
+                                   list_notes_to_copy,
+                                   self);
 }
 
-
 static const BijiProviderInfo *
-biji_import_provider_get_info (BijiProvider *self)
+biji_import_provider_get_info (BijiProvider *provider)
 {
-  return & (BIJI_IMPORT_PROVIDER (self)->priv->info);
-}
+  BijiImportProvider *self = BIJI_IMPORT_PROVIDER (provider);
 
+  return &self->info;
+}
 
 static void
 biji_import_provider_set_property (GObject      *object,
@@ -263,14 +202,13 @@ biji_import_provider_set_property (GObject      *object,
 {
   BijiImportProvider *self = BIJI_IMPORT_PROVIDER (object);
 
-
   switch (property_id)
     {
     case PROP_URI:
-      self->priv->uri = g_value_dup_string (value);
+      self->uri = g_value_dup_string (value);
       break;
     case PROP_TARGET:
-      self->priv->target = g_value_dup_string (value);
+      self->target = g_value_dup_string (value);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -278,7 +216,6 @@ biji_import_provider_set_property (GObject      *object,
     }
 }
 
-
 static void
 biji_import_provider_get_property (GObject    *object,
                                    guint       property_id,
@@ -290,10 +227,10 @@ biji_import_provider_get_property (GObject    *object,
   switch (property_id)
     {
     case PROP_URI:
-      g_value_set_string (value, self->priv->uri);
+      g_value_set_string (value, self->uri);
       break;
     case PROP_TARGET:
-      g_value_set_string (value, self->priv->target);
+      g_value_set_string (value, self->target);
       break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
@@ -301,20 +238,22 @@ biji_import_provider_get_property (GObject    *object,
     }
 }
 
+static void
+biji_import_provider_finalize (GObject *object)
+{
+  G_OBJECT_CLASS (biji_import_provider_parent_class)->finalize (object);
+}
 
 static void
 biji_import_provider_class_init (BijiImportProviderClass *klass)
 {
-  GObjectClass *g_object_class;
-  BijiProviderClass *provider_class;
-
-  g_object_class = G_OBJECT_CLASS (klass);
-  provider_class = BIJI_PROVIDER_CLASS (klass);
+  GObjectClass      *g_object_class = G_OBJECT_CLASS (klass);
+  BijiProviderClass *provider_class = BIJI_PROVIDER_CLASS (klass);
 
   g_object_class->set_property = biji_import_provider_set_property;
   g_object_class->get_property = biji_import_provider_get_property;
-  g_object_class->finalize = biji_import_provider_finalize;
-  g_object_class->constructed = biji_import_provider_constructed;
+  g_object_class->constructed  = biji_import_provider_constructed;
+  g_object_class->finalize     = biji_import_provider_finalize;
 
   provider_class->get_info = biji_import_provider_get_info;
 
@@ -336,31 +275,21 @@ biji_import_provider_class_init (BijiImportProviderClass *klass)
   g_object_class_install_properties (g_object_class, IMPORT_PROV_PROP, properties);
 }
 
-
-static void
-biji_import_provider_finalize (GObject *object)
-{
-  G_OBJECT_CLASS (biji_import_provider_parent_class)->finalize (object);
-}
-
-
 static void
 biji_import_provider_init (BijiImportProvider *self)
 {
-  self->priv = biji_import_provider_get_instance_private (self);
-  self->priv->items = g_hash_table_new (g_str_hash, g_str_equal);
+  self->items = g_hash_table_new (g_str_hash, g_str_equal);
 }
 
-
 BijiProvider *
-biji_import_provider_new (BijiManager *b,
-                          const gchar *target_provider,
-                          const gchar *uri)
+biji_import_provider_new (BijiManager *manager,
+                          const char  *target_provider,
+                          const char  *uri)
 {
   return g_object_new (BIJI_TYPE_IMPORT_PROVIDER,
-                       "manager", b,
-                       "target", target_provider,
-                       "uri", uri,
+                       "manager", manager,
+                       "target",  target_provider,
+                       "uri",     uri,
                        NULL);
 }
 
diff --git a/src/libbiji/provider/biji-import-provider.h b/src/libbiji/provider/biji-import-provider.h
index 22e08e1..02bb175 100644
--- a/src/libbiji/provider/biji-import-provider.h
+++ b/src/libbiji/provider/biji-import-provider.h
@@ -31,41 +31,17 @@
 
 #pragma once
 
-#include "../biji-manager.h"
 #include "biji-provider.h"
 
 G_BEGIN_DECLS
 
+#define BIJI_TYPE_IMPORT_PROVIDER (biji_import_provider_get_type ())
 
-#define BIJI_TYPE_IMPORT_PROVIDER             (biji_import_provider_get_type ())
-#define BIJI_IMPORT_PROVIDER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), BIJI_TYPE_IMPORT_PROVIDER, 
BijiImportProvider))
-#define BIJI_IMPORT_PROVIDER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), BIJI_TYPE_IMPORT_PROVIDER, 
BijiImportProviderClass))
-#define BIJI_IS_IMPORT_PROVIDER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), BIJI_TYPE_IMPORT_PROVIDER))
-#define BIJI_IS_IMPORT_PROVIDER_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), BIJI_TYPE_IMPORT_PROVIDER))
-#define BIJI_IMPORT_PROVIDER_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), BIJI_TYPE_IMPORT_PROVIDER, 
BijiImportProviderClass))
-
-typedef struct BijiImportProvider_         BijiImportProvider;
-typedef struct BijiImportProviderClass_    BijiImportProviderClass;
-typedef struct BijiImportProviderPrivate_  BijiImportProviderPrivate;
-
-struct BijiImportProvider_
-{
-  BijiProvider parent;
-  BijiImportProviderPrivate *priv;
-};
-
-struct BijiImportProviderClass_
-{
-  BijiProviderClass parent_class;
-};
-
-
-GType                   biji_import_provider_get_type             (void);
-
-
-BijiProvider           *biji_import_provider_new                  (BijiManager *manager,
-                                                                   const gchar *target_provider,
-                                                                   const gchar *uri);
+G_DECLARE_FINAL_TYPE (BijiImportProvider, biji_import_provider, BIJI, IMPORT_PROVIDER, BijiProvider)
 
+BijiProvider *biji_import_provider_new (BijiManager *manager,
+                                        const char  *target_provider,
+                                        const char  *uri);
 
 G_END_DECLS
+


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