1 /* gtd-task-model.c
2 *
3 * Copyright 2018 Georges Basile Stavracas Neto <georges.stavracas@gmail.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 "GtdTaskModel"
22
23 #include "gtd-debug.h"
24 #include "gtd-list-store.h"
25 #include "gtd-manager.h"
26 #include "gtd-task.h"
27 #include "gtd-task-list.h"
28 #include "gtd-task-model.h"
29 #include "gtd-task-model-private.h"
30
31 struct _GtdTaskModel
32 {
33 GObject parent;
34
35 GtkFlattenListModel *model;
36
37 guint number_of_tasks;
38
39 GtdManager *manager;
40 };
41
42 static void g_list_model_iface_init (GListModelInterface *iface);
43
44 G_DEFINE_TYPE_WITH_CODE (GtdTaskModel, gtd_task_model, G_TYPE_OBJECT,
45 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, g_list_model_iface_init))
46
47 enum
48 {
49 PROP_0,
50 PROP_MANAGER,
51 N_PROPS
52 };
53
54 static GParamSpec *properties [N_PROPS];
55
56
57 /*
58 * Callbacks
59 */
60
61 static void
on_model_items_changed_cb(GListModel * model,guint position,guint n_removed,guint n_added,GtdTaskModel * self)62 on_model_items_changed_cb (GListModel *model,
63 guint position,
64 guint n_removed,
65 guint n_added,
66 GtdTaskModel *self)
67 {
68 GTD_TRACE_MSG ("Child model changed with position=%u, n_removed=%u, n_added=%u", position, n_removed, n_added);
69
70 g_list_model_items_changed (G_LIST_MODEL (self), position, n_removed, n_added);
71 }
72
73
74 /*
75 * GListModel iface
76 */
77
78 static gpointer
gtd_task_model_get_item(GListModel * model,guint position)79 gtd_task_model_get_item (GListModel *model,
80 guint position)
81 {
82 GtdTaskModel *self = (GtdTaskModel*) model;
83
84 return g_list_model_get_item (G_LIST_MODEL (self->model), position);
85 }
86
87 static guint
gtd_task_model_get_n_items(GListModel * model)88 gtd_task_model_get_n_items (GListModel *model)
89 {
90 GtdTaskModel *self = (GtdTaskModel*) model;
91
92 return g_list_model_get_n_items (G_LIST_MODEL (self->model));
93 }
94
95 static GType
gtd_task_model_get_item_type(GListModel * model)96 gtd_task_model_get_item_type (GListModel *model)
97 {
98 return GTD_TYPE_TASK;
99 }
100
101 static void
g_list_model_iface_init(GListModelInterface * iface)102 g_list_model_iface_init (GListModelInterface *iface)
103 {
104 iface->get_item = gtd_task_model_get_item;
105 iface->get_n_items = gtd_task_model_get_n_items;
106 iface->get_item_type = gtd_task_model_get_item_type;
107 }
108
109
110 /*
111 * GObject overrides
112 */
113
114 static void
gtd_task_model_finalize(GObject * object)115 gtd_task_model_finalize (GObject *object)
116 {
117 GtdTaskModel *self = (GtdTaskModel *)object;
118
119 g_clear_object (&self->manager);
120 g_clear_object (&self->model);
121
122 G_OBJECT_CLASS (gtd_task_model_parent_class)->finalize (object);
123 }
124
125
126 static void
gtd_task_model_constructed(GObject * object)127 gtd_task_model_constructed (GObject *object)
128 {
129 GtdTaskModel *self = (GtdTaskModel *)object;
130 GListModel *model;
131
132 g_assert (self->manager != NULL);
133
134 model = gtd_manager_get_task_lists_model (self->manager);
135
136 g_signal_connect_object (self->model,
137 "items-changed",
138 G_CALLBACK (on_model_items_changed_cb),
139 self,
140 0);
141 gtk_flatten_list_model_set_model (self->model, model);
142
143 G_OBJECT_CLASS (gtd_task_model_parent_class)->constructed (object);
144 }
145
146 static void
gtd_task_model_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)147 gtd_task_model_get_property (GObject *object,
148 guint prop_id,
149 GValue *value,
150 GParamSpec *pspec)
151 {
152 GtdTaskModel *self = GTD_TASK_MODEL (object);
153
154 switch (prop_id)
155 {
156 case PROP_MANAGER:
157 g_value_set_object (value, self->manager);
158 break;
159
160 default:
161 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162 }
163 }
164
165 static void
gtd_task_model_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)166 gtd_task_model_set_property (GObject *object,
167 guint prop_id,
168 const GValue *value,
169 GParamSpec *pspec)
170 {
171 GtdTaskModel *self = GTD_TASK_MODEL (object);
172
173 switch (prop_id)
174 {
175 case PROP_MANAGER:
176 g_assert (self->manager == NULL);
177 self->manager = g_value_dup_object (value);
178 break;
179
180 default:
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
182 }
183 }
184
185 static void
gtd_task_model_class_init(GtdTaskModelClass * klass)186 gtd_task_model_class_init (GtdTaskModelClass *klass)
187 {
188 GObjectClass *object_class = G_OBJECT_CLASS (klass);
189
190 object_class->finalize = gtd_task_model_finalize;
191 object_class->constructed = gtd_task_model_constructed;
192 object_class->get_property = gtd_task_model_get_property;
193 object_class->set_property = gtd_task_model_set_property;
194
195 properties[PROP_MANAGER] = g_param_spec_object ("manager",
196 "Manager",
197 "Manager",
198 GTD_TYPE_MANAGER,
199 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
200
201 g_object_class_install_properties (object_class, N_PROPS, properties);
202 }
203
204 static void
gtd_task_model_init(GtdTaskModel * self)205 gtd_task_model_init (GtdTaskModel *self)
206 {
207 self->model = gtk_flatten_list_model_new (NULL);
208 }
209
210 GtdTaskModel*
_gtd_task_model_new(GtdManager * manager)211 _gtd_task_model_new (GtdManager *manager)
212 {
213 return g_object_new (GTD_TYPE_TASK_MODEL,
214 "manager", manager,
215 NULL);
216 }
217