1 /* gbp-todo-item.c
2  *
3  * Copyright 2017-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "gbp-todo-item"
22 
23 #include "gbp-todo-item.h"
24 
25 #define MAX_TODO_LINES 5
26 
27 struct _GbpTodoItem
28 {
29   GObject      parent_instance;
30   GBytes      *bytes;
31   const gchar *path;
32   guint        lineno;
33   const gchar *lines[MAX_TODO_LINES];
34 };
35 
G_DEFINE_FINAL_TYPE(GbpTodoItem,gbp_todo_item,G_TYPE_OBJECT)36 G_DEFINE_FINAL_TYPE (GbpTodoItem, gbp_todo_item, G_TYPE_OBJECT)
37 
38 static void
39 gbp_todo_item_finalize (GObject *object)
40 {
41   GbpTodoItem *self = (GbpTodoItem *)object;
42 
43   g_clear_pointer (&self->bytes, g_bytes_unref);
44 
45   G_OBJECT_CLASS (gbp_todo_item_parent_class)->finalize (object);
46 }
47 
48 static void
gbp_todo_item_class_init(GbpTodoItemClass * klass)49 gbp_todo_item_class_init (GbpTodoItemClass *klass)
50 {
51   GObjectClass *object_class = G_OBJECT_CLASS (klass);
52 
53   object_class->finalize = gbp_todo_item_finalize;
54 }
55 
56 static void
gbp_todo_item_init(GbpTodoItem * self)57 gbp_todo_item_init (GbpTodoItem *self)
58 {
59 }
60 
61 /**
62  * gbp_todo_item_new:
63  * @bytes: a buffer containing all the subsequent data
64  *
65  * This creates a new #GbpTodoItem.
66  *
67  * Getting the parameters right to this function is essential.
68  *
69  * @bytes should contain a buffer that can be used to access
70  * the raw string pointers used in later API calls to this
71  * object.
72  *
73  * This is done to avoid fragmentation due to lots of
74  * small string allocations.
75  *
76  * Returns: (transfer full): A newly allocated #GbpTodoItem
77  *
78  * Since: 3.32
79  */
80 GbpTodoItem *
gbp_todo_item_new(GBytes * bytes)81 gbp_todo_item_new (GBytes *bytes)
82 {
83   GbpTodoItem *self;
84 
85   g_assert (bytes != NULL);
86 
87   self = g_object_new (GBP_TYPE_TODO_ITEM, NULL);
88   self->bytes = g_bytes_ref (bytes);
89 
90   return self;
91 }
92 
93 void
gbp_todo_item_add_line(GbpTodoItem * self,const gchar * line)94 gbp_todo_item_add_line (GbpTodoItem *self,
95                         const gchar *line)
96 {
97   g_assert (GBP_IS_TODO_ITEM (self));
98   g_assert (line != NULL);
99 
100   for (guint i = 0; i < G_N_ELEMENTS (self->lines); i++)
101     {
102       if (self->lines[i] == NULL)
103         {
104           self->lines[i] = line;
105           break;
106         }
107     }
108 }
109 
110 const gchar *
gbp_todo_item_get_line(GbpTodoItem * self,guint nth)111 gbp_todo_item_get_line (GbpTodoItem *self,
112                         guint        nth)
113 {
114   g_return_val_if_fail (GBP_IS_TODO_ITEM (self), NULL);
115 
116   if (nth < G_N_ELEMENTS (self->lines))
117     return self->lines[nth];
118 
119   return NULL;
120 }
121 
122 guint
gbp_todo_item_get_lineno(GbpTodoItem * self)123 gbp_todo_item_get_lineno (GbpTodoItem *self)
124 {
125   g_return_val_if_fail (GBP_IS_TODO_ITEM (self), 0);
126 
127   return self->lineno;
128 }
129 
130 void
gbp_todo_item_set_lineno(GbpTodoItem * self,guint lineno)131 gbp_todo_item_set_lineno (GbpTodoItem *self,
132                           guint        lineno)
133 {
134   g_return_if_fail (GBP_IS_TODO_ITEM (self));
135 
136   self->lineno = lineno;
137 }
138 
139 const gchar *
gbp_todo_item_get_path(GbpTodoItem * self)140 gbp_todo_item_get_path (GbpTodoItem *self)
141 {
142   g_return_val_if_fail (GBP_IS_TODO_ITEM (self), NULL);
143 
144   return self->path;
145 }
146 
147 void
gbp_todo_item_set_path(GbpTodoItem * self,const gchar * path)148 gbp_todo_item_set_path (GbpTodoItem *self,
149                         const gchar *path)
150 {
151   g_return_if_fail (GBP_IS_TODO_ITEM (self));
152 
153   self->path = path;
154 }
155