[bijiben/wip/sadiq/rewrite: 6/15] Add provider base class



commit 9d3dd10af84f0528b60d7fa4193f1fcf9182b912
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Mon Feb 26 17:16:09 2018 +0530

    Add provider base class

 src/providers/bjb-provider.c |  434 ++++++++++++++++++++++++++++++++++++++++++
 src/providers/bjb-provider.h |   75 ++++++++
 2 files changed, 509 insertions(+), 0 deletions(-)
---
diff --git a/src/providers/bjb-provider.c b/src/providers/bjb-provider.c
new file mode 100644
index 0000000..dfb04c8
--- /dev/null
+++ b/src/providers/bjb-provider.c
@@ -0,0 +1,434 @@
+/* bjb-provider.c
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "bjb-provider"
+
+#include "config.h"
+
+#include "bjb-trace.h"
+
+#include "bjb-provider.h"
+
+/**
+ * SECTION: bjb-provider
+ * @title: BjbProvider
+ * @short_description:
+ * @include: "bjb-provider.h"
+ *
+ *
+ */
+
+typedef struct
+{
+  gchar *uid;
+  gchar *name;
+  gchar *icon;
+  gchar *domain;
+  gchar *user_name;
+
+  gboolean loaded;
+} BjbProviderPrivate;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (BjbProvider, bjb_provider, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_UID,
+  PROP_NAME,
+  PROP_ICON,
+  PROP_DOMAIN,
+  PROP_USER_NAME,
+  N_PROPS
+};
+
+enum {
+  ITEM_ADDED,
+  ITEM_DELETED,
+  ITEM_TRASHED,
+  ITEM_RESTORED,
+  ITEM_UPDATED,
+  READY,
+  N_SIGNALS
+};
+
+static GParamSpec *properties[N_PROPS];
+static guint signals[N_SIGNALS];
+
+static void
+bjb_provider_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  BjbProvider *self = (BjbProvider *)object;
+
+  switch (prop_id)
+    {
+    case PROP_UID:
+      g_value_set_string (value, bjb_provider_get_uid (self));
+      break;
+
+    case PROP_NAME:
+      g_value_set_string (value, bjb_provider_get_name (self));
+      break;
+
+    case PROP_ICON:
+      g_value_set_string (value, bjb_provider_get_icon (self));
+      break;
+
+    case PROP_DOMAIN:
+      g_value_set_string (value, bjb_provider_get_domain (self));
+      break;
+
+    case PROP_USER_NAME:
+      g_value_set_string (value, bjb_provider_get_user_name (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+bjb_provider_dispose (GObject *object)
+{
+  BJB_ENTRY;
+
+  G_OBJECT_CLASS (bjb_provider_parent_class)->dispose (object);
+
+  BJB_EXIT;
+}
+
+static void
+bjb_provider_finalize (GObject *object)
+{
+  BJB_ENTRY;
+
+  G_OBJECT_CLASS (bjb_provider_parent_class)->finalize (object);
+
+  BJB_EXIT;
+}
+
+static GList *
+bjb_provider_real_get_notes (BjbProvider *self)
+{
+  g_assert (BJB_IS_PROVIDER (self));
+
+  /* Derived classes should implement this, if supported */
+  return NULL;
+}
+
+static GList *
+bjb_provider_real_get_trash_notes (BjbProvider *self)
+{
+  g_assert (BJB_IS_PROVIDER (self));
+
+  /* Derived classes should implement this, if supported */
+  return NULL;
+}
+
+static GList *
+bjb_provider_real_get_notebooks (BjbProvider *self)
+{
+  g_assert (BJB_IS_PROVIDER (self));
+
+  /* Derived classes should implement this, if supported */
+  return NULL;
+}
+
+static void
+bjb_provider_class_init (BjbProviderClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = bjb_provider_get_property;
+  object_class->dispose = bjb_provider_dispose;
+  object_class->finalize = bjb_provider_finalize;
+
+  klass->get_notes = bjb_provider_real_get_notes;
+  klass->get_trash_notes = bjb_provider_real_get_trash_notes;
+  klass->get_notebooks = bjb_provider_real_get_notebooks;
+
+  properties[PROP_UID] =
+    g_param_spec_string ("uid",
+                         "Uid",
+                         "A unique id for the provider",
+                         NULL,
+                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+  properties[PROP_NAME] =
+    g_param_spec_string ("name",
+                         "Name",
+                         "The name of the provider",
+                         NULL,
+                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+  properties[PROP_ICON] =
+    g_param_spec_string ("icon",
+                         "Icon",
+                         "The icon for the provider",
+                         NULL,
+                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+  properties[PROP_DOMAIN] =
+    g_param_spec_string ("domain",
+                         "Domain",
+                         "The domain address of the provider",
+                         NULL,
+                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+  properties[PROP_USER_NAME] =
+    g_param_spec_string ("user-name",
+                         "User Name",
+                         "The user name used for the provider",
+                         NULL,
+                         G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+  /**
+   * BjbProvider::ready:
+   * @self: a #BjbProvider
+   *
+   * The "ready" signal is emitted after every item is loaded from provider
+   *
+   * Since: 3.28
+   */
+  signals [READY] =
+    g_signal_new ("ready",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (BjbProviderClass, ready),
+                  NULL, NULL,
+                  NULL,
+                  G_TYPE_NONE, 0);
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+bjb_provider_init (BjbProvider *self)
+{
+  BJB_ENTRY;
+
+
+
+  BJB_EXIT;
+}
+
+/**
+ * bjb_item_get_uid:
+ * @self: a #BjbProvider
+ *
+ * Get the uid of the provider
+ *
+ * Returns (transfer full) (nullable): the uid of the provider. free
+ * with g_free().
+ */
+gchar *
+bjb_provider_get_uid (BjbProvider *self)
+{
+  gchar *uid;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_PROVIDER (self), NULL);
+
+  uid = BJB_PROVIDER_GET_CLASS (self)->get_uid (self);
+
+  BJB_RETURN (uid);
+}
+
+/**
+ * bjb_item_get_name:
+ * @self: a #BjbProvider
+ *
+ * Get the name of the provider
+ *
+ * Returns (transfer full) (nullable): the name of the provider. free
+ * with g_free().
+ */
+gchar *
+bjb_provider_get_name (BjbProvider *self)
+{
+  gchar *name;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_PROVIDER (self), NULL);
+
+  name = BJB_PROVIDER_GET_CLASS (self)->get_name (self);
+
+  BJB_RETURN (name);
+}
+
+/**
+ * bjb_item_get_icon:
+ * @self: a #BjbProvider
+ *
+ * Get the icon of the provider
+ *
+ * Returns (transfer full) (nullable): the icon of the provider. free
+ * with g_free().
+ */
+gchar *
+bjb_provider_get_icon (BjbProvider *self)
+{
+  gchar *icon;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_PROVIDER (self), NULL);
+
+  icon = BJB_PROVIDER_GET_CLASS (self)->get_icon (self);
+
+  BJB_RETURN (icon);
+}
+
+/**
+ * bjb_item_get_domain:
+ * @self: a #BjbProvider
+ *
+ * Get the domain of the provider
+ *
+ * Returns (transfer full) (nullable): the domain address of the provider.
+ * Free with g_free().
+ */
+gchar *
+bjb_provider_get_domain (BjbProvider *self)
+{
+  gchar *domain;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_PROVIDER (self), NULL);
+
+  domain = BJB_PROVIDER_GET_CLASS (self)->get_domain (self);
+
+  BJB_RETURN (domain);
+}
+
+/**
+ * bjb_item_get_user_name:
+ * @self: a #BjbProvider
+ *
+ * Get the user_name of the provider
+ *
+ * Returns (transfer full) (nullable): the user name connected to the provider.
+ * Free with g_free().
+ */
+gchar *
+bjb_provider_get_user_name (BjbProvider *self)
+{
+  gchar *user_name;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_PROVIDER (self), NULL);
+
+  user_name = BJB_PROVIDER_GET_CLASS (self)->get_user_name (self);
+
+  BJB_RETURN (user_name);
+}
+
+/**
+ * bjb_provider_load_items_async:
+ * @self: a #BjbProvider
+ * @cancellable: (nullable): a #GCancellable or %NULL
+ * @callback: a #GAsyncReadyCallback, or %NULL
+ * @user_data: closure data for @callback
+ *
+ * Asynchronously load all items (Notes and Notebooks) from the provider
+ *
+ * @callback should complete the operation by calling bjb_provider_load_items_finish().
+ */
+void
+bjb_provider_load_items_async (BjbProvider         *self,
+                               GCancellable        *cancellable,
+                               GAsyncReadyCallback  callback,
+                               gpointer             user_data)
+{
+  BjbProviderPrivate *priv = bjb_provider_get_instance_private (self);
+
+  BJB_ENTRY;
+
+  g_return_if_fail (BJB_IS_PROVIDER (self));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+  g_return_if_fail (priv->loaded == FALSE);
+
+  priv->loaded = TRUE;
+
+  BJB_PROVIDER_GET_CLASS (self)->load_items_async (self, cancellable,
+                                                   callback, user_data);
+
+  BJB_EXIT;
+}
+
+/**
+ * bjb_provider_load_items_finish:
+ * @self: a #BjbProvider
+ * @result: a #GAsyncResult provided to callback
+ * @error: a location for a #GError or %NULL
+ *
+ * Completes an asynchronous loading of items initiated with
+ * bjb_provider_load_items_async().
+ *
+ * Returns: %TRUE if items loaded successfully
+ */
+gboolean
+bjb_provider_load_items_finish (BjbProvider   *self,
+                                GAsyncResult  *result,
+                                GError       **error)
+{
+  gboolean ret;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_PROVIDER (self), FALSE);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
+
+  ret = BJB_PROVIDER_GET_CLASS (self)->load_items_finish (self, result, error);
+
+  if (ret)
+    g_signal_emit (self, signals[READY], 0);
+
+  BJB_RETURN (ret);
+}
+
+/**
+ * bjb_provider_get_notes:
+ * @self: a #BjbProvider
+ *
+ * Get the list of notes loaded by the provider. This should be
+ * called only after bjb_provider_load_items_async().
+ *
+ * Returns: (transfer none) (nullable): List of notes
+ */
+GList *
+bjb_provider_get_notes (BjbProvider *self)
+{
+  GList *notes;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_PROVIDER (self), NULL);
+
+  notes = BJB_PROVIDER_GET_CLASS (self)->get_notes (self);
+
+  BJB_RETURN (notes);
+}
diff --git a/src/providers/bjb-provider.h b/src/providers/bjb-provider.h
new file mode 100644
index 0000000..d03d084
--- /dev/null
+++ b/src/providers/bjb-provider.h
@@ -0,0 +1,75 @@
+/* bjb-provider.h
+ *
+ * Copyright 2018 Mohammed Sadiq <sadiq sadiqpk 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <glib-object.h>
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_PROVIDER (bjb_provider_get_type ())
+
+G_DECLARE_DERIVABLE_TYPE (BjbProvider, bjb_provider, BJB, PROVIDER, GObject)
+
+struct _BjbProviderClass
+{
+  GObjectClass parent_class;
+
+  void        (*load_items_async)      (BjbProvider          *self,
+                                        GCancellable         *cancellable,
+                                        GAsyncReadyCallback   callback,
+                                        gpointer              user_data);
+  gboolean    (*load_items_finish)     (BjbProvider           *self,
+                                        GAsyncResult          *result,
+                                        GError               **error);
+
+  GList      *(*get_notes)             (BjbProvider           *self);
+  GList      *(*get_trash_notes)       (BjbProvider           *self);
+  GList      *(*get_notebooks)         (BjbProvider           *self);
+
+  gchar      *(*get_uid)               (BjbProvider           *self);
+  gchar      *(*get_name)              (BjbProvider           *self);
+  gchar      *(*get_icon)              (BjbProvider           *self);
+  gchar      *(*get_domain)            (BjbProvider           *self);
+  gchar      *(*get_user_name)         (BjbProvider           *self);
+
+  void        (*ready)                 (BjbProvider           *self);
+};
+
+gchar      *bjb_provider_get_uid               (BjbProvider         *self);
+gchar      *bjb_provider_get_name              (BjbProvider         *self);
+gchar      *bjb_provider_get_icon              (BjbProvider         *self);
+gchar      *bjb_provider_get_domain            (BjbProvider         *self);
+gchar      *bjb_provider_get_user_name         (BjbProvider         *self);
+
+void        bjb_provider_load_items_async      (BjbProvider         *self,
+                                                GCancellable        *cancellable,
+                                                GAsyncReadyCallback  callback,
+                                                gpointer             user_data);
+gboolean    bjb_provider_load_items_finish     (BjbProvider         *self,
+                                                GAsyncResult        *result,
+                                                GError             **error);
+
+GList      *bjb_provider_get_notes             (BjbProvider         *self);
+GList      *bjb_provider_get_trash_notes       (BjbProvider         *self);
+GList      *bjb_provider_get_notebooks         (BjbProvider         *self);
+
+G_END_DECLS


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