[bijiben/wip/sadiq/rewrite: 4/14] Add note base class



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

    Add note base class

 src/notes/bjb-note.c |  291 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/notes/bjb-note.h |   55 ++++++++++
 2 files changed, 346 insertions(+), 0 deletions(-)
---
diff --git a/src/notes/bjb-note.c b/src/notes/bjb-note.c
new file mode 100644
index 0000000..617e495
--- /dev/null
+++ b/src/notes/bjb-note.c
@@ -0,0 +1,291 @@
+/* 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;
+
+  /* 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;
+
+    case PROP_RAW_CONTENT:
+      g_value_set_string (value, bjb_note_get_raw_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;
+
+    case PROP_RAW_CONTENT:
+      bjb_note_set_raw_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_OBJECT_CLASS (bjb_note_parent_class)->finalize (object);
+
+  BJB_EXIT;
+}
+
+static gchar *
+bjb_note_real_get_text_content (BjbNote *self)
+{
+  BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+  g_assert (BJB_IS_NOTE (self));
+
+  return g_strdup (priv->content);
+}
+
+static void
+bjb_note_real_set_text_content (BjbNote     *self,
+                                const gchar *content)
+{
+  BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+  g_assert (BJB_IS_NOTE (self));
+
+  g_free (priv->content);
+  priv->content = g_strdup (content);
+}
+
+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;
+
+  klass->get_raw_content = bjb_note_real_get_text_content;
+  klass->set_raw_content = bjb_note_real_set_text_content;
+
+  properties[PROP_CONTENT] =
+    g_param_spec_string ("content",
+                         "Content",
+                         "The content of the note",
+                         NULL,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  /**
+   * BjbNote:raw-content:
+   *
+   * The raw-content of the note may include XML/markdown content,
+   * or whatever is used to keep the note formatted.
+   */
+  properties[PROP_RAW_CONTENT] =
+    g_param_spec_string ("raw-content",
+                         "Raw Content",
+                         "The raw 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)
+{
+  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_set_text_content:
+ * @self: a #BjbNote
+ * @content: the text content to set
+ *
+ * Set the plain content of the note, with no formatting tags, if any
+ */
+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));
+
+  g_free (priv->content);
+  priv->content = g_strdup (content);
+
+  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)
+{
+  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_raw_content:
+ * @self: a #BjbNote
+ * @content: the raw content to set
+ *
+ * Set the raw content of the note, with all formatting tags, if any
+ */
+void
+bjb_note_set_raw_content (BjbNote     *self,
+                          const gchar *content)
+{
+  BJB_ENTRY;
+
+  g_return_if_fail (BJB_IS_NOTE (self));
+
+  BJB_NOTE_GET_CLASS (self)->set_raw_content (self, content);
+
+  BJB_EXIT;
+}
+
+/**
+ * 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)
+{
+  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..b5e7a5b
--- /dev/null
+++ b/src/notes/bjb-note.h
@@ -0,0 +1,55 @@
+/* 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_raw_content)         (BjbNote       *self,
+                                     const gchar   *content);
+  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_raw_content         (BjbNote       *self,
+                                         const gchar   *content);
+
+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]