]> git.datanom.net - gtk_lensdatabase.git/blob - src/lens.c
On the way
[gtk_lensdatabase.git] / src / lens.c
1 /*
2 * lens.c
3 *
4 * Copyright 2024 Michael Rasmussen <mir@datanom.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 *
22 */
23
24 #include "lens.h"
25 #include <glib.h>
26
27 enum {
28 PROP_ID = 1,
29 PROP_SN,
30 PROP_DATA,
31 PROP_NOTES,
32 PROP_DATE,
33 N_PROPERTIES
34 };
35
36 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
37 static void lens_finalize (GObject *object);
38 static void lens_dispose (GObject *gobject);
39
40 typedef struct {
41 /* add your private declarations here */
42 gint id;
43 gchar* sn;
44 gchar* data;
45 gchar* notes;
46 gchar* date;
47 GList* lens;
48 } LensPrivate;
49
50 struct _Lens {
51 GObject parent;
52 /* add your public declarations here */
53 };
54
55 G_DEFINE_TYPE_WITH_PRIVATE(Lens, lens, G_TYPE_OBJECT)
56
57 static void lens_set_property ( GObject *object,
58 guint prop_id,
59 const GValue *value,
60 GParamSpec *pspec) {
61 LensPrivate *priv = lens_get_instance_private(LENS (object));
62
63 switch (prop_id) {
64 case PROP_ID:
65 priv->id = g_value_get_int (value);
66 break;
67 case PROP_SN:
68 if (priv->sn)
69 g_free(priv->sn);
70 priv->sn = g_strdup(g_value_get_string (value));
71 break;
72 case PROP_DATA:
73 if (priv->data)
74 g_free(priv->data);
75 priv->data = g_strdup(g_value_get_string (value));
76 break;
77 case PROP_NOTES:
78 if (priv->notes)
79 g_free(priv->notes);
80 priv->notes = g_strdup(g_value_get_string (value));
81 break;
82 case PROP_DATE:
83 if (priv->date)
84 g_free(priv->date);
85 priv->date = g_strdup(g_value_get_string (value));
86 break;
87
88 default:
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90 break;
91 }
92 }
93
94 static void lens_get_property ( GObject *object,
95 guint prop_id,
96 GValue *value,
97 GParamSpec *pspec) {
98 LensPrivate *priv = lens_get_instance_private(LENS (object));
99
100 switch (prop_id) {
101 case PROP_ID:
102 g_value_set_int (value, priv->id);
103 break;
104 case PROP_SN:
105 g_value_set_string (value, priv->sn);
106 break;
107 case PROP_DATA:
108 g_value_set_string (value, priv->data);
109 break;
110 case PROP_NOTES:
111 g_value_set_string (value, priv->notes);
112 break;
113 case PROP_DATE:
114 g_value_set_string (value, priv->date);
115 break;
116
117 default:
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
119 break;
120 }
121 }
122
123 static void lens_class_init (LensClass *klass) {
124 GObjectClass *g_object_class;
125
126 g_object_class = G_OBJECT_CLASS (klass);
127
128 g_object_class->dispose = lens_dispose;
129 g_object_class->finalize = lens_finalize;
130
131 g_object_class->set_property = lens_set_property;
132 g_object_class->get_property = lens_get_property;
133
134 obj_properties[PROP_ID] =
135 g_param_spec_int ("id",
136 "Id",
137 "Database id.",
138 G_MININT,
139 G_MAXINT,
140 0,
141 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
142 obj_properties[PROP_SN] =
143 g_param_spec_string ("sn",
144 "SerialNumber",
145 "Serial Number of the lens.",
146 NULL /* default value */,
147 G_PARAM_READWRITE);
148 obj_properties[PROP_DATA] =
149 g_param_spec_string ("data",
150 "Data",
151 "Lens description.",
152 NULL /* default value */,
153 G_PARAM_READWRITE);
154 obj_properties[PROP_NOTES] =
155 g_param_spec_string ("notes",
156 "Notes",
157 "Lens notes.",
158 NULL /* default value */,
159 G_PARAM_READWRITE);
160 obj_properties[PROP_DATE] =
161 g_param_spec_string ("date",
162 "Date",
163 "Date the lens was added.",
164 NULL /* default value */,
165 G_PARAM_CONSTRUCT | G_PARAM_READWRITE);
166
167 g_object_class_install_properties (g_object_class,
168 N_PROPERTIES,
169 obj_properties);
170 }
171
172 static void lens_dispose (GObject *gobject) {
173 //LensPrivate *priv = lens_get_instance_private(LENS(gobject));
174
175 /* In dispose(), you are supposed to free all types referenced from this
176 * object which might themselves hold a reference to self. Generally,
177 * the most simple solution is to unref all members on which you own a
178 * reference.
179 */
180
181 /* dispose() might be called multiple times, so we must guard against
182 * calling g_object_unref() on an invalid GObject by setting the member
183 * NULL; g_clear_object() does this for us.
184 * g_clear_object (&priv->input_stream);
185 */
186
187 /* Always chain up to the parent class; there is no need to check if
188 * the parent class implements the dispose() virtual function: it is
189 * always guaranteed to do so
190 */
191 G_OBJECT_CLASS(lens_parent_class)->dispose (gobject);
192 }
193
194 static void lens_finalize (GObject *object) {
195 Lens *self;
196
197 g_return_if_fail (IS_LENS (object));
198
199 self = LENS (object);
200
201 LensPrivate *priv = lens_get_instance_private(self);
202 g_free(priv->data);
203 g_free(priv->date);
204 g_free(priv->sn);
205 g_free(priv->notes);
206
207 G_OBJECT_CLASS (lens_parent_class)->finalize (object);
208 }
209
210
211 static void lens_init (Lens *self) {
212 LensPrivate *priv = lens_get_instance_private(self);
213 priv->data = NULL;
214 priv->date = NULL;
215 priv->sn = NULL;
216 priv->notes = NULL;;
217 }
218
219
220 Lens *lens_new (gint id, const gchar* sn, const gchar* data, GError** error) {
221 g_return_val_if_fail (sn == NULL || data == NULL || error == NULL || *error == NULL, NULL);
222
223 gchar* now = get_current_timestamp();
224 Lens* lens = LENS(g_object_new (TYPE_LENS, "id", id, "sn", sn, "data", data, "date", now, NULL));
225 g_free(now);
226 return lens;
227 }
228
229 gchar* get_current_timestamp() {
230 GDateTime* dt = g_date_time_new_now_local();
231 gchar* now = g_date_time_format (dt, "%Y-%m-%d %H:%M:%S");
232 g_date_time_unref(dt);
233
234 return now;
235 }
236
237 void lens_set_sn(Lens* self, const gchar* sn, GError** error) {
238 g_return_if_fail (IS_LENS (self));
239 g_return_if_fail (sn == NULL || error == NULL || *error == NULL);
240
241 LensPrivate *priv = lens_get_instance_private(self);
242 if (priv->sn)
243 g_free(priv->sn);
244 priv->sn = g_strdup(sn);
245 }
246
247 void lens_set_data(Lens* self, const gchar* data, GError** error) {
248 g_return_if_fail (IS_LENS (self));
249 g_return_if_fail (data == NULL || error == NULL || *error == NULL);
250
251 LensPrivate *priv = lens_get_instance_private(self);
252 if (priv->data)
253 g_free(priv->data);
254 priv->data = g_strdup(data);
255 }
256
257 void lens_set_notes(Lens* self, const gchar* notes, GError** error) {
258 g_return_if_fail (IS_LENS (self));
259 g_return_if_fail (notes == NULL || error == NULL || *error == NULL);
260
261 LensPrivate *priv = lens_get_instance_private(self);
262 if (priv->notes)
263 g_free(priv->notes);
264 priv->notes = g_strdup(notes);
265 }
266
267 void lens_set_date(Lens* self, const gchar* date, GError** error) {
268 g_return_if_fail (IS_LENS (self));
269 g_return_if_fail (date == NULL || error == NULL || *error == NULL);
270
271 LensPrivate *priv = lens_get_instance_private(self);
272 if (priv->date)
273 g_free(priv->date);
274 priv->date = g_strdup(date);
275 }
276
277 gint lens_get_id(Lens* self, GError** error) {
278 g_return_val_if_fail (IS_LENS (self), -1);
279 g_return_val_if_fail (error == NULL || *error == NULL, -1);
280
281 LensPrivate *priv = lens_get_instance_private(self);
282
283 return priv->id;
284 }
285
286 gchar* lens_to_string(Lens* self) {
287 g_return_val_if_fail (IS_LENS (self), NULL);
288
289 gchar* data;
290 gchar* date;
291 gchar* sn;
292 gchar* notes;
293 gint id;
294
295 g_object_get(self, "id", &id, "data", &data, "date", &date, "sn", &sn, "notes", &notes, NULL);
296
297 gchar* s = g_strdup_printf("[%d [%s:%s:%s - %s]]", id, sn, data, notes, date);
298 g_free(data);
299 g_free(date);
300 g_free(sn);
301 g_free(notes);
302
303 return s;
304 }
305
306 Lens* lens_get_copy(const Lens* self, GError** error) {
307 g_return_val_if_fail (IS_LENS (self), NULL);
308 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
309
310 LensPrivate *priv = lens_get_instance_private((Lens*)self);
311
312 Lens* lens = lens_new(priv->id, priv->sn, priv->data, error);
313 if (*error) {
314 g_object_unref(lens);
315 return NULL;
316 }
317 g_object_set(lens, "notes", priv->notes, "date", priv->date, NULL);
318
319 return lens;
320 }
This page took 0.400832 seconds and 6 git commands to generate.