[bijiben/wip/sadiq/rewrite: 1/11] Add note base class



commit 1025359b178984f393658ca9238ce0e5f03e7d04
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Sun Feb 25 11:57:23 2018 +0530

    Add note base class

 src/notes/bjb-note.c |  234 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/notes/bjb-note.h |   50 +++++++++++
 2 files changed, 284 insertions(+), 0 deletions(-)
---
diff --git a/src/notes/bjb-note.c b/src/notes/bjb-note.c
new file mode 100644
index 0000000..915d420
--- /dev/null
+++ b/src/notes/bjb-note.c
@@ -0,0 +1,234 @@
+/* bjb-note.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-note"
+
+#include "config.h"
+
+#include "bjb-trace.h"
+
+#include "bjb-note.h"
+
+/**
+ * SECTION: bjb-note
+ * @title: BjbNote
+ * @short_description: The base class for Notes
+ */
+
+typedef struct
+{
+  gchar *content;
+
+  /*
+   * Content length is cached in associated GtkTextBuffer (ie, for open
+   * notes), so it's simpler to check if content length as changed
+   * before doing strcmp() to check if the note has changed.
+   */
+  gint content_length;
+
+  /*
+   * XXX: content and raw_content are a kind copies, may result
+   * around 1+ MiB of RAM for a few 100 notes with 1000+ characters.
+   */
+  gchar *raw_content;
+
+  /* TODO: Add a thumbnail image of the note */
+} BjbNotePrivate;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (BjbNote, bjb_note, BJB_TYPE_ITEM)
+
+enum {
+  PROP_0,
+  PROP_CONTENT,
+  PROP_RAW_CONTENT,
+  N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS];
+
+static void
+bjb_note_get_property (GObject    *object,
+                       guint       prop_id,
+                       GValue     *value,
+                       GParamSpec *pspec)
+{
+  BjbNote *self = (BjbNote *)object;
+
+  switch (prop_id)
+    {
+    case PROP_CONTENT:
+      g_value_set_string (value, bjb_note_get_text_content (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+bjb_note_set_property (GObject      *object,
+                       guint         prop_id,
+                       const GValue *value,
+                       GParamSpec   *pspec)
+{
+  BjbNote *self = (BjbNote *)object;
+
+  switch (prop_id)
+    {
+    case PROP_CONTENT:
+      bjb_note_set_text_content (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+bjb_note_finalize (GObject *object)
+{
+  BjbNote *self = (BjbNote *)object;
+  BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+  BJB_ENTRY;
+
+  g_clear_pointer (&priv->content, g_free);
+  g_clear_pointer (&priv->raw_content, g_free);
+
+  G_OBJECT_CLASS (bjb_note_parent_class)->finalize (object);
+
+  BJB_EXIT;
+}
+
+static void
+bjb_note_class_init (BjbNoteClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = bjb_note_get_property;
+  object_class->set_property = bjb_note_set_property;
+  object_class->finalize = bjb_note_finalize;
+
+  properties[PROP_CONTENT] =
+    g_param_spec_string ("content",
+                         "Content",
+                         "The content of the note",
+                         NULL,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+bjb_note_init (BjbNote *self)
+{
+  BJB_ENTRY;
+  BJB_EXIT;
+}
+
+/**
+ * bjb_note_get_text_content:
+ * @self: a #BjbNote
+ *
+ * Get the plain content of the note, without any formatting tags, if any
+ *
+ * Returns (transfer full) (nullable): the plain text content of the note
+ */
+gchar *
+bjb_note_get_text_content (BjbNote *self)
+{
+  BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+  gchar *content;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_NOTE (self), NULL);
+
+  content = BJB_NOTE_GET_CLASS (self)->get_text_content (self);
+
+  BJB_RETURN (content);
+}
+
+/**
+ * bjb_note_get_text_content:
+ * @self: a #BjbNote
+ *
+ * Get the plain content of the note, without any formatting tags, if any
+ *
+ * Returns (transfer full) (nullable): the plain text content of the note
+ */
+void
+bjb_note_set_text_content (BjbNote     *self,
+                           const gchar *content)
+{
+  BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+  BJB_ENTRY;
+
+  g_return_if_fail (BJB_IS_NOTE (self));
+
+  BJB_EXIT;
+}
+
+/**
+ * bjb_note_get_raw_content:
+ * @self: a #BjbNote
+ *
+ * Get the raw content, may contain XML tags, or whatever is used for
+ * formatting
+ *
+ * Returns (transfer full) (nullable): the raw text content of the note
+ */
+gchar *
+bjb_note_get_raw_content  (BjbNote *self)
+{
+  BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+  gchar *content;
+
+  BJB_ENTRY;
+
+  g_return_val_if_fail (BJB_IS_NOTE (self), NULL);
+
+  content = BJB_NOTE_GET_CLASS (self)->get_raw_content (self);
+
+  BJB_RETURN (content);
+}
+
+/**
+ * bjb_note_set_content_from_buffer:
+ * @self: a #BjbNote
+ * @buffer: a #GtkTextBuffer from which text has to be extracted
+ *
+ * Set the content of the note from buffer
+ */
+void
+bjb_note_set_content_from_buffer (BjbNote       *self,
+                                  GtkTextBuffer *buffer)
+{
+  BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+  BJB_ENTRY;
+
+  g_return_if_fail (BJB_IS_NOTE (self));
+
+  BJB_NOTE_GET_CLASS (self)->set_content_from_buffer (self, buffer);
+
+  BJB_EXIT;
+}
diff --git a/src/notes/bjb-note.h b/src/notes/bjb-note.h
new file mode 100644
index 0000000..0adeaea
--- /dev/null
+++ b/src/notes/bjb-note.h
@@ -0,0 +1,50 @@
+/* bjb-note.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-item.h"
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_NOTE (bjb_note_get_type ())
+
+G_DECLARE_DERIVABLE_TYPE (BjbNote, bjb_note, BJB, NOTE, BjbItem)
+
+struct _BjbNoteClass
+{
+  BjbItemClass parent_class;
+
+  gchar *(*get_text_content)        (BjbNote *self);
+  gchar *(*get_raw_content)         (BjbNote *self);
+  void   (*set_content_from_buffer) (BjbNote       *self,
+                                     GtkTextBuffer *buffer);
+};
+
+gchar *bjb_note_get_text_content        (BjbNote       *self);
+void   bjb_note_set_text_content        (BjbNote       *self,
+                                         const gchar   *content);
+
+gchar *bjb_note_get_raw_content         (BjbNote       *self);
+void   bjb_note_set_content_from_buffer (BjbNote       *self,
+                                         GtkTextBuffer *buffer);
+
+G_END_DECLS


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