[bijiben/wip/sadiq/rewrite: 3/12] Add plain note class
- From: Mohammed Sadiq <pksadiq src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [bijiben/wip/sadiq/rewrite: 3/12] Add plain note class
- Date: Wed, 7 Mar 2018 14:14:03 +0000 (UTC)
commit 100f40d3ba798fd8c7b139ac3023a3c5a771ee8b
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date: Sun Feb 25 12:52:42 2018 +0530
Add plain note class
src/notes/bjb-plain-note.c | 154 ++++++++++++++++++++++++++++++++++++++++++++
src/notes/bjb-plain-note.h | 36 ++++++++++
2 files changed, 190 insertions(+), 0 deletions(-)
---
diff --git a/src/notes/bjb-plain-note.c b/src/notes/bjb-plain-note.c
new file mode 100644
index 0000000..86a1c34
--- /dev/null
+++ b/src/notes/bjb-plain-note.c
@@ -0,0 +1,154 @@
+#define G_LOG_DOMAIN "bjb-plain-note"
+
+#include "config.h"
+
+#include "bjb-trace.h"
+
+#include "bjb-plain-note.h"
+
+/**
+ * SECTION: bjb-plain-note
+ * @title: BjbPlainNote
+ * @short_description:
+ * @include: "bjb-plain-note.h"
+ *
+ *
+ */
+
+struct _BjbPlainNote
+{
+ gchar *content;
+ BjbNote parent_instance;
+};
+
+G_DEFINE_TYPE (BjbPlainNote, bjb_plain_note, BJB_TYPE_NOTE)
+
+static gchar *
+bjb_plain_note_get_text_content (BjbNote *note)
+{
+ return g_strdup (BJB_PLAIN_NOTE (note)->content);
+}
+
+static void
+bjb_plain_note_set_content_from_buffer (BjbNote *note,
+ GtkTextBuffer *buffer)
+{
+ BjbPlainNote *self = BJB_PLAIN_NOTE (note);
+ GtkTextIter start, end;
+ g_autofree gchar *title = NULL;
+ g_autofree gchar *content = NULL;
+ gint line_count;
+
+ line_count = gtk_text_buffer_get_line_count (buffer);
+
+ /* If we have at least one line set the title */
+ if (line_count > 0)
+ {
+ gtk_text_buffer_get_start_iter (buffer, &start);
+ gtk_text_buffer_get_iter_at_line (buffer, &end, 1);
+ title = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+ }
+
+ /* We might have some content too */
+ if (line_count > 1)
+ {
+ start = end;
+ gtk_text_buffer_get_end_iter (buffer, &end);
+ content = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+ }
+
+ g_free (self->content);
+ self->content = g_strdup (content);
+ bjb_item_set_title (BJB_ITEM (note), title);
+}
+
+static void
+bjb_plain_note_dispose (GObject *object)
+{
+ BJB_ENTRY;
+
+ G_OBJECT_CLASS (bjb_plain_note_parent_class)->dispose (object);
+
+ BJB_EXIT;
+}
+
+static void
+bjb_plain_note_finalize (GObject *object)
+{
+ BjbPlainNote *self = (BjbPlainNote *)object;
+
+ BJB_ENTRY;
+
+ g_clear_pointer (&self->content, g_free);
+
+ G_OBJECT_CLASS (bjb_plain_note_parent_class)->finalize (object);
+
+ BJB_EXIT;
+}
+
+static void
+bjb_plain_note_class_init (BjbPlainNoteClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ BjbNoteClass *note_class = BJB_NOTE_CLASS (klass);
+
+ object_class->dispose = bjb_plain_note_dispose;
+ object_class->finalize = bjb_plain_note_finalize;
+
+ note_class->get_text_content = bjb_plain_note_get_text_content;
+ note_class->get_raw_content = bjb_plain_note_get_text_content;
+ note_class->set_content_from_buffer = bjb_plain_note_set_content_from_buffer;
+}
+
+static void
+bjb_plain_note_init (BjbPlainNote *self)
+{
+ BJB_ENTRY;
+
+
+
+ BJB_EXIT;
+}
+
+
+static BjbPlainNote *
+bjb_plain_note_create_from_data (const gchar *data)
+{
+ gchar **split_data;
+ gchar *title = NULL, *content = NULL;
+
+ g_assert (data != NULL);
+
+ /* We shall have at most 2 parts: title and content */
+ /* TODO: free split_data */
+ split_data = g_strsplit (data, "\n", 2);
+
+ if (split_data[0] != NULL)
+ {
+ title = split_data[0];
+
+ if (split_data[1] != NULL)
+ content = split_data[1];
+ }
+
+ return g_object_new (BJB_TYPE_PLAIN_NOTE,
+ "title", title,
+ "content", content,
+ NULL);
+}
+
+BjbPlainNote *
+bjb_plain_note_new (void)
+{
+ return g_object_new (BJB_TYPE_PLAIN_NOTE,
+ NULL);
+}
+
+BjbPlainNote *
+bjb_plain_note_new_from_data (const gchar *data)
+{
+ if (data == NULL)
+ return bjb_plain_note_new ();
+
+ return bjb_plain_note_create_from_data (data);
+}
diff --git a/src/notes/bjb-plain-note.h b/src/notes/bjb-plain-note.h
new file mode 100644
index 0000000..d57b38f
--- /dev/null
+++ b/src/notes/bjb-plain-note.h
@@ -0,0 +1,36 @@
+/* bjb-plain-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 <gtk/gtk.h>
+#include <glib-object.h>
+#include "bjb-note.h"
+
+G_BEGIN_DECLS
+
+#define BJB_TYPE_PLAIN_NOTE (bjb_plain_note_get_type ())
+
+G_DECLARE_FINAL_TYPE (BjbPlainNote, bjb_plain_note, BJB, PLAIN_NOTE, BjbNote)
+
+BjbPlainNote *bjb_plain_note_new (void);
+BjbPlainNote *bjb_plain_note_new_from_data (const gchar *text);
+
+G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]