[bijiben/wip/sadiq/rewrite: 5/13] Add provider base class
- From: Mohammed Sadiq <pksadiq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [bijiben/wip/sadiq/rewrite: 5/13] Add provider base class
- Date: Wed, 28 Feb 2018 15:04:17 +0000 (UTC)
commit efe4f80a38ebf19aaa7a9bdaf9397737b09ae430
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date: Mon Feb 26 17:16:09 2018 +0530
Add provider base class
src/providers/bjb-provider.c | 378 ++++++++++++++++++++++++++++++++++++++++++
src/providers/bjb-provider.h | 58 +++++++
2 files changed, 436 insertions(+), 0 deletions(-)
---
diff --git a/src/providers/bjb-provider.c b/src/providers/bjb-provider.c
new file mode 100644
index 0000000..03e2c4f
--- /dev/null
+++ b/src/providers/bjb-provider.c
@@ -0,0 +1,378 @@
+/* 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;
+
+ /* TODO: move this to subclasses and use a hashtable */
+ GList *notes;
+} BjbProviderPrivate;
+
+struct _BjbProvider
+{
+ GObject parent_instance;
+};
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (BjbProvider, bjb_provider, G_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)
+{
+ BjbProvider *self = (BjbProvider *)object;
+ BjbProviderPrivate *priv = bjb_provider_get_instance_private (self);
+
+ BJB_ENTRY;
+
+
+
+ G_OBJECT_CLASS (bjb_provider_parent_class)->dispose (object);
+
+ BJB_EXIT;
+}
+
+static void
+bjb_provider_finalize (GObject *object)
+{
+ BjbProvider *self = (BjbProvider *)object;
+ BjbProviderPrivate *priv = bjb_provider_get_instance_private (self);
+
+ BJB_ENTRY;
+
+ G_OBJECT_CLASS (bjb_provider_parent_class)->finalize (object);
+
+ BJB_EXIT;
+}
+
+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->set_property = bjb_provider_set_property;
+ object_class->dispose = bjb_provider_dispose;
+ object_class->finalize = bjb_provider_finalize;
+
+ properties[PROP_UID] =
+ g_param_spec_string ("uid",
+ "Uid",
+ "A unique id for the provider",
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+ properties[PROP_NAME] =
+ g_param_spec_string ("name",
+ "Name",
+ "The name of the provider",
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+ properties[PROP_ICON] =
+ g_param_spec_string ("icon",
+ "Icon",
+ "The icon for the provider",
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+ properties[PROP_DOMAIN] =
+ g_param_spec_string ("domain",
+ "Domain",
+ "The domain address of the provider",
+ 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",
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
+ g_sn.
+ g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+bjb_provider_init (BjbProvider *self)
+{
+ BJB_ENTRY;
+
+
+
+ BJB_EXIT;
+}
+
+BjbProvider *
+bjb_provider_new (void)
+{
+ return g_object_new (BJB_TYPE_PROVIDER,
+ NULL);
+}
+
+/**
+ * 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,
+ GAsyncReadCallback callback,
+ gpointer user_data)
+{
+ BJB_ENTRY;
+
+ g_return_if_fail (BJB_IS_PROVIDER (self));
+ g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ 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().
+ *
+ * A %NULL may be returned on error or if no items are
+ * present to load. Check for error with @error.
+ *
+ * Returns: (transfer full) (nullable): A list of items or %NULL.
+ * @error set on error.
+ */
+GList *
+bjb_provider_load_items_finish (BjbProvider *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ GList *items;
+
+ BJB_ENTRY;
+
+ g_return_val_if_fail (BJB_IS_PROVIDER (self), NULL);
+ g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
+
+ items = BJB_PROVIDER_GET_CLASS (self)->load_items_finish (self, result, error);
+
+ BJB_RETURN (items);
+}
+
diff --git a/src/providers/bjb-provider.h b/src/providers/bjb-provider.h
new file mode 100644
index 0000000..caf2ebf
--- /dev/null
+++ b/src/providers/bjb-provider.h
@@ -0,0 +1,58 @@
+/* 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>
+
+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);
+ GList *(*load_items_finish) (BjbProvider *self,
+ GAsyncResult *result,
+ GError **error);
+};
+
+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,
+ GAsyncReadCallback callback,
+ gpointer user_data);
+GList *bjb_provider_load_items_finish (BjbProvider *self,
+ GAsyncResult *result,
+ GError **error);
+
+G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]