[bijiben/wip/sadiq/rewrite: 2/9] Add local provider class



commit b9a12e61b85ac59b3163e22432c50affa4e7355b
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Mon Feb 26 22:24:08 2018 +0530

    Add local provider class

 src/providers/bjb-local-provider.c |  286 ++++++++++++++++++++++++++++++++++++
 src/providers/bjb-local-provider.h |   34 +++++
 2 files changed, 320 insertions(+), 0 deletions(-)
---
diff --git a/src/providers/bjb-local-provider.c b/src/providers/bjb-local-provider.c
new file mode 100644
index 0000000..8825fe0
--- /dev/null
+++ b/src/providers/bjb-local-provider.c
@@ -0,0 +1,286 @@
+/* bjb-local-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-local-provider"
+
+#include "config.h"
+
+#include <tracker-sparql.h>
+#include "bjb-plain-note.h"
+#include "bjb-trace.h"
+
+#include "bjb-local-provider.h"
+
+/**
+ * SECTION: bjb-local-provider
+ * @title: BjbLocalProvider
+ * @short_description:
+ * @include: "bjb-local-provider.h"
+ *
+ *
+ */
+
+struct _BjbLocalProvider
+{
+  BjbProvider parent_instance;
+
+  gchar *uid;
+  gchar *name;
+  gchar *icon;
+  gchar *domain;
+  gchar *user_name;
+
+  GFile *location;
+
+  TrackerSparqlConnection *tracker;
+
+  /* XXX: use a hashtable? */
+  GList *notes;
+};
+
+static void biji_local_provider_initable_iface_init (GInitableIface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (BjbLocalProvider, bjb_local_provider, BJB_TYPE_PROVIDER,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, biji_local_provider_initable_iface_init))
+
+static gboolean
+bjb_local_provider_initable_init (GInitable     *initable,
+                                  GCancellable  *cancellable,
+                                  GError       **error)
+{
+  BjbLocalProvider *self = (BjbLocalProvider *)initable;
+
+  BJB_ENTRY;
+
+  /* Let's try to create a connection */
+  self->tracker = tracker_sparql_connection_get (cancellable, error);
+
+  if (*error != NULL)
+    BJB_RETURN (FALSE);
+
+  BJB_RETURN (TRUE);
+}
+
+static void
+biji_local_provider_initable_iface_init (GInitableIface *iface)
+{
+  iface->init = bjb_local_provider_initable_init;
+}
+
+static void
+bjb_local_provider_dispose (GObject *object)
+{
+  BJB_ENTRY;
+
+  G_OBJECT_CLASS (bjb_local_provider_parent_class)->dispose (object);
+
+  BJB_EXIT;
+}
+
+static void
+bjb_local_provider_finalize (GObject *object)
+{
+  BJB_ENTRY;
+
+  G_OBJECT_CLASS (bjb_local_provider_parent_class)->finalize (object);
+
+  BJB_EXIT;
+}
+
+static gchar *
+bjb_local_provider_get_uid (BjbProvider *provider)
+{
+  return g_strdup (BJB_LOCAL_PROVIDER (provider)->uid);
+}
+
+static gchar *
+bjb_local_provider_get_name (BjbProvider *provider)
+{
+  return g_strdup (BJB_LOCAL_PROVIDER (provider)->name);
+}
+
+static gchar *
+bjb_local_provider_get_icon (BjbProvider *provider)
+{
+  return g_strdup (BJB_LOCAL_PROVIDER (provider)->icon);
+}
+
+static gchar *
+bjb_local_provider_get_domain (BjbProvider *provider)
+{
+  return g_strdup (BJB_LOCAL_PROVIDER (provider)->domain);
+}
+
+static gchar *
+bjb_local_provider_get_user_name (BjbProvider *provider)
+{
+  return g_strdup (BJB_LOCAL_PROVIDER (provider)->user_name);
+}
+
+static void
+bjb_local_provider_load_items (GTask        *task,
+                               gpointer      source_object,
+                               gpointer      task_data,
+                               GCancellable *cancellable)
+{
+  g_autoptr(GError) error = NULL;
+  BjbLocalProvider *self = source_object;
+  GFileEnumerator *enumerator;
+  gpointer file_info_ptr;
+
+  BJB_ENTRY;
+
+  g_assert (G_IS_TASK (task));
+  g_assert (BJB_IS_LOCAL_PROVIDER (source_object));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  enumerator = g_file_enumerate_children (self->location,
+                                          G_FILE_ATTRIBUTE_STANDARD_NAME","
+                                          G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE,
+                                          G_FILE_QUERY_INFO_NONE,
+                                          cancellable,
+                                          &error);
+
+  if (error != NULL)
+    {
+      g_task_return_error (task, g_steal_pointer (&error));
+      BJB_EXIT;
+    }
+
+  while ((file_info_ptr = g_file_enumerator_next_file (enumerator, cancellable, NULL)))
+    {
+      g_autoptr(GFileInfo) file_info = file_info_ptr;
+      g_autoptr(GFile) file = NULL;
+      g_autofree gchar *contents = NULL;
+      BjbPlainNote *note;
+      const gchar *name;
+
+      name = g_file_info_get_name (file_info);
+
+      if (!g_str_has_suffix (name, ".note"))
+        continue;
+
+      file = g_file_get_child (self->location, name);
+      g_file_load_contents (file, cancellable, &contents, NULL, NULL, NULL);
+
+      note = bjb_plain_note_new_from_data (contents);
+
+      self->notes = g_list_prepend (self->notes, note);
+    }
+
+  g_task_return_boolean (task, TRUE);
+
+  BJB_EXIT;
+}
+
+static void
+bjb_local_provider_load_items_async (BjbProvider         *provider,
+                                     GCancellable        *cancellable,
+                                     GAsyncReadyCallback  callback,
+                                     gpointer             user_data)
+{
+  BjbLocalProvider *self = (BjbLocalProvider *)provider;
+  g_autoptr(GTask) task = NULL;
+
+  BJB_ENTRY;
+
+  g_assert (BJB_IS_LOCAL_PROVIDER (self));
+  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = g_task_new (self, cancellable, callback, user_data);
+  g_task_set_source_tag (task, bjb_local_provider_load_items_async);
+
+  g_task_run_in_thread (task, bjb_local_provider_load_items);
+
+  BJB_EXIT;
+}
+
+static gboolean
+bjb_local_provider_load_items_finish (BjbProvider   *provider,
+                                      GAsyncResult  *result,
+                                      GError       **error)
+{
+  gboolean ret;
+
+  BJB_ENTRY;
+
+  g_assert (BJB_IS_PROVIDER (provider));
+  g_assert (G_IS_TASK (result));
+
+  ret = g_task_propagate_boolean (G_TASK (result), error);
+
+  BJB_RETURN (ret);
+}
+
+static GList *
+bjb_local_provider_get_notes (BjbProvider *provider)
+{
+  BJB_ENTRY;
+
+  g_assert (BJB_IS_PROVIDER (provider));
+
+  BJB_RETURN (BJB_LOCAL_PROVIDER (provider)->notes);
+}
+
+static void
+bjb_local_provider_class_init (BjbLocalProviderClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  BjbProviderClass *provider_class = BJB_PROVIDER_CLASS (klass);
+
+  object_class->dispose = bjb_local_provider_dispose;
+  object_class->finalize = bjb_local_provider_finalize;
+
+  provider_class->get_uid = bjb_local_provider_get_uid;
+  provider_class->get_name = bjb_local_provider_get_name;
+  provider_class->get_icon = bjb_local_provider_get_icon;
+  provider_class->get_domain = bjb_local_provider_get_domain;
+  provider_class->get_user_name = bjb_local_provider_get_user_name;
+
+  provider_class->load_items_async = bjb_local_provider_load_items_async;
+  provider_class->load_items_finish = bjb_local_provider_load_items_finish;
+
+  provider_class->get_notes = bjb_local_provider_get_notes;
+}
+
+static void
+bjb_local_provider_init (BjbLocalProvider *self)
+{
+  g_autofree gchar *path = NULL;
+
+  BJB_ENTRY;
+
+  self->uid = "local";
+
+  if (self->location == NULL)
+    {
+      path = g_build_filename (g_get_user_data_dir (), "bijiben", NULL);
+      self->location = g_file_new_for_path (path);
+    }
+
+  BJB_EXIT;
+}
+
+BjbLocalProvider *
+bjb_local_provider_new (GError **error)
+{
+  return g_initable_new (BJB_TYPE_LOCAL_PROVIDER, NULL, error,
+                         NULL);
+}
diff --git a/src/providers/bjb-local-provider.h b/src/providers/bjb-local-provider.h
new file mode 100644
index 0000000..fc9be85
--- /dev/null
+++ b/src/providers/bjb-local-provider.h
@@ -0,0 +1,34 @@
+/* bjb-local-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 "bjb-provider.h"
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_LOCAL_PROVIDER (bjb_local_provider_get_type ())
+
+G_DECLARE_FINAL_TYPE (BjbLocalProvider, bjb_local_provider, BJB, LOCAL_PROVIDER, BjbProvider)
+
+BjbLocalProvider *bjb_local_provider_new (GError **error);
+
+G_END_DECLS


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