[bijiben/wip/sadiq/rewrite: 3/3] Add note base class
- From: Mohammed Sadiq <pksadiq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [bijiben/wip/sadiq/rewrite: 3/3] Add note base class
- Date: Sun, 25 Feb 2018 06:27:13 +0000 (UTC)
commit d0e9f22bc30ffff2185644fd2d0614c5dda8f6cc
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date: Sun Feb 25 11:57:23 2018 +0530
Add note base class
src/notes/bjb-note.c | 255 ++++++++++++++++++++++++++++++++++++++++++++++++++
src/notes/bjb-note.h | 42 ++++++++
2 files changed, 297 insertions(+), 0 deletions(-)
---
diff --git a/src/notes/bjb-note.c b/src/notes/bjb-note.c
new file mode 100644
index 0000000..4e494b8
--- /dev/null
+++ b/src/notes/bjb-note.c
@@ -0,0 +1,255 @@
+/* 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_CONTENT_LENGTH,
+ 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 = BJB_NOTE (object);
+ BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+ switch (prop_id)
+ {
+ case PROP_CONTENT:
+ g_value_set_value (value, priv->content);
+ break;
+
+ case PROP_CONTENT_LENGTH:
+ g_value_set_int (value, priv->content_length);
+ break;
+
+ case PROP_RAW_CONTENT:
+ g_value_set_string (value, priv->raw_content);
+ 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 = BJB_NOTE (object);
+ BjbNotePrivate *priv = bjb_note_get_instance_private (self);
+
+ switch (prop_id)
+ {
+ case PROP_CONTENT:
+ bjb_note_set_content (self, g_value_get_string (value));
+ break;
+
+ case PROP_CONTENT_LENGTH:
+ bjb_note_set_content_length (self, g_value_get_int (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)
+{
+ 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);
+
+ properties[PROP_CONTENT_LENGTH] =
+ g_param_spec_int ("content-length",
+ "Content Length",
+ "The length of the content",
+ NULL,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+ /**
+ * BjbNote:raw-content:
+ *
+ * The raw content of the note. Say for example if note is saved
+ * as html raw-content would contain html tags
+ */
+ 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;
+}
+
+BjbNote *
+bjb_note_new (void)
+{
+ return g_object_new (BJB_TYPE_NOTE,
+ NULL);
+}
+
+/**
+ * bjb_note_get_text_content:
+ * @self: a #BjbNote
+ *
+ * Get the plain content of the note, without any formatting tags, if any
+ *
+ * Returns (transfer none) (nullable): the plain text content of the note
+ */
+const gchar *bjb_note_get_text_content (BjbNote *self)
+{
+ BjbItemPrivate *priv = bjb_item_get_instance_private (self);
+
+ g_return_val_if_fail (BJB_IS_ITEM (self), NULL);
+
+ return priv->content;
+}
+
+/**
+ * bjb_note_get_raw_content:
+ * @self: a #BjbNote
+ *
+ * Get the raw content, may contain XML tags, or whatever is used for
+ * formatting
+ *
+ * Returns (transfer none) (nullable): the raw text content of the note
+ */
+const gchar *bjb_note_get_raw_content (BjbNote *self)
+{
+ BjbItemPrivate *priv = bjb_item_get_instance_private (self);
+
+ g_return_val_if_fail (BJB_IS_ITEM (self), NULL);
+
+ return priv->raw_content;
+}
+
+/**
+ * bjb_note_set_content:
+ * @self: a #BjbNote
+ * @content: the text to set as content
+ * @raw_content: The raw_content including the formatting
+ * @content_length: the length of @content
+ *
+ * Set the content of the note
+ */
+void bjb_note_set_content (BjbNote *self,
+ const gchar *content,
+ const gchar *raw_content,
+ gint content_length)
+{
+ BjbItemPrivate *priv = bjb_item_get_instance_private (self);
+
+ g_retun_if_fail (BJB_IS_ITEM (self));
+
+ if (priv->content_length == content_length &&
+ g_strcmp0 (priv->content, content) == 0)
+ return;
+
+ g_free (priv->content);
+ g_free (priv->raw_content);
+
+ priv->content = g_strdup (content);
+ priv->raw_content = g_strdup (raw_content);
+ priv->content_length = content_length;
+
+ bjb_item_set_modified (BJB_ITEM (self));
+ g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CONTENT]);
+}
diff --git a/src/notes/bjb-note.h b/src/notes/bjb-note.h
new file mode 100644
index 0000000..7c07ac4
--- /dev/null
+++ b/src/notes/bjb-note.h
@@ -0,0 +1,42 @@
+/* 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>
+
+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;
+
+};
+
+const gchar *bjb_note_get_text_content (BjbNote *self);
+const gchar *bjb_note_get_raw_content (BjbNote *self);
+void bjb_note_set_content (BjbNote *self,
+ const gchar *content);
+
+G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]