]> git.datanom.net - gtk_lensdatabase.git/blob - src/lensmaker.c
On the way
[gtk_lensdatabase.git] / src / lensmaker.c
1 /*
2 * lensmaker.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 "lensmaker.h"
25
26 enum {
27 PROP_ID = 1,
28 PROP_NAME,
29 N_PROPERTIES
30 };
31
32 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
33 static void lens_maker_finalize (GObject *object);
34 static void lens_maker_dispose (GObject *gobject);
35
36 typedef struct {
37 /* add your private declarations here */
38 gint id;
39 gchar* name;
40 GList* lens;
41 } LensMakerPrivate;
42
43 struct _LensMaker {
44 GObject parent;
45 /* add your public declarations here */
46 LensMakerPrivate *priv;
47 };
48
49 G_DEFINE_TYPE_WITH_PRIVATE(LensMaker, lens_maker, G_TYPE_OBJECT)
50
51 static void lens_maker_set_property ( GObject *object,
52 guint prop_id,
53 const GValue *value,
54 GParamSpec *pspec) {
55 LensMakerPrivate *priv = lens_maker_get_instance_private(LENS_MAKER (object));
56
57 switch (prop_id) {
58 case PROP_ID:
59 priv->id = g_value_get_int (value);
60 break;
61 case PROP_NAME:
62 if (priv->name)
63 g_free(priv->name);
64 priv->name = g_strdup(g_value_get_string (value));
65 break;
66
67 default:
68 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
69 break;
70 }
71 }
72
73 static void lens_maker_get_property ( GObject *object,
74 guint prop_id,
75 GValue *value,
76 GParamSpec *pspec) {
77 LensMakerPrivate *priv = lens_maker_get_instance_private(LENS_MAKER (object));
78
79 switch (prop_id) {
80 case PROP_ID:
81 g_value_set_int (value, priv->id);
82 break;
83 case PROP_NAME:
84 g_value_set_string (value, priv->name);
85 break;
86
87 default:
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
89 break;
90 }
91 }
92
93 static void lens_maker_class_init (LensMakerClass *klass) {
94 GObjectClass *g_object_class;
95
96 g_object_class = G_OBJECT_CLASS (klass);
97
98 g_object_class->dispose = lens_maker_dispose;
99 g_object_class->finalize = lens_maker_finalize;
100
101 g_object_class->set_property = lens_maker_set_property;
102 g_object_class->get_property = lens_maker_get_property;
103
104 obj_properties[PROP_ID] =
105 g_param_spec_int ("id",
106 "Id",
107 "Database id.",
108 G_MININT,
109 G_MAXINT,
110 0,
111 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
112 obj_properties[PROP_NAME] =
113 g_param_spec_string ("lensmaker",
114 "LensMaker",
115 "Lens maker of the lens.",
116 NULL /* default value */,
117 G_PARAM_READWRITE);
118
119 g_object_class_install_properties (g_object_class,
120 N_PROPERTIES,
121 obj_properties);
122 }
123
124 static void lens_maker_dispose (GObject *gobject) {
125 //LensMakerPrivate *priv = lens_maker_get_instance_private(LENS_MAKER(gobject));
126
127 /* In dispose(), you are supposed to free all types referenced from this
128 * object which might themselves hold a reference to self. Generally,
129 * the most simple solution is to unref all members on which you own a
130 * reference.
131 */
132
133 /* dispose() might be called multiple times, so we must guard against
134 * calling g_object_unref() on an invalid GObject by setting the member
135 * NULL; g_clear_object() does this for us.
136 * g_clear_object (&priv->input_stream);
137 */
138
139 /* Always chain up to the parent class; there is no need to check if
140 * the parent class implements the dispose() virtual function: it is
141 * always guaranteed to do so
142 */
143 G_OBJECT_CLASS(lens_maker_parent_class)->dispose (gobject);
144 }
145
146 static void free_glist_items(gpointer data, gpointer user_data) {
147 if (data) {
148 Lens* lens = LENS(data);
149 g_object_unref(lens);
150 }
151 }
152
153 static void lens_maker_finalize (GObject *object) {
154 LensMaker *self;
155
156 g_return_if_fail (IS_LENS_MAKER (object));
157
158 self = LENS_MAKER (object);
159
160 LensMakerPrivate *priv = lens_maker_get_instance_private(self);
161 g_free(priv->name);
162 g_list_foreach(priv->lens, (GFunc)free_glist_items, NULL);
163 g_list_free(priv->lens);
164
165 G_OBJECT_CLASS (lens_maker_parent_class)->finalize (object);
166 }
167
168 static void lens_maker_init (LensMaker *self) {
169 LensMakerPrivate *priv = lens_maker_get_instance_private(self);
170 priv->name = NULL;
171 priv->lens = NULL;
172 }
173
174 static gpointer copy_list(gconstpointer src, gpointer data) {
175 Lens* lens = lens_get_copy(LENS(src), (GError**) data);
176 return lens;
177 }
178
179 LensMaker *lens_maker_new (gint id) {
180 return LENS_MAKER(g_object_new (TYPE_LENS_MAKER, "id", id, NULL));
181 }
182
183 gint lens_maker_get_id(LensMaker* self) {
184 g_return_val_if_fail (IS_LENS_MAKER (self), -1);
185 LensMakerPrivate *priv = lens_maker_get_instance_private(self);
186 return priv->id;
187 }
188
189 void lens_maker_set_name(LensMaker* self, const gchar* name, GError** error) {
190 g_return_if_fail (IS_LENS_MAKER (self) || name != NULL || error == NULL || *error == NULL);
191
192 LensMakerPrivate *priv = lens_maker_get_instance_private(self);
193 g_free(priv->name);
194 priv->name = g_strdup(name);
195 }
196
197 void lens_maker_add_lens(LensMaker* self, const Lens* lens, GError** error) {
198 g_return_if_fail (IS_LENS_MAKER (self) || lens != NULL || error == NULL || *error == NULL);
199
200 LensMakerPrivate *priv = lens_maker_get_instance_private(self);
201 Lens* l = lens_get_copy(lens, error);
202 if (!*error)
203 priv->lens = g_list_append(priv->lens, l);
204 }
205
206 void lens_maker_remove_lens(LensMaker* self, Lens* lens, GError** error) {
207 g_return_if_fail (IS_LENS_MAKER (self) || lens != NULL || error == NULL || *error == NULL);
208
209 LensMakerPrivate *priv = lens_maker_get_instance_private(self);
210 priv->lens = g_list_remove_all(priv->lens, lens);
211 g_object_unref(lens);
212 }
213
214 Lens* lens_maker_get_lens(LensMaker* self, gint id, GError** error) {
215 g_return_val_if_fail (IS_LENS_MAKER (self) || id >= 0 || error == NULL || *error == NULL, NULL);
216
217 LensMakerPrivate *priv = lens_maker_get_instance_private(self);
218 Lens* lens = NULL;
219 gint needle;
220 GList* iter = priv->lens;
221 while (iter) {
222 lens = iter->data;
223 g_object_get(lens, "id", &needle, NULL);
224
225 if (id == needle)
226 break;
227 else
228 lens = NULL;
229
230 iter = iter->next;
231 }
232
233 Lens* l = lens_get_copy(lens, error);
234 if (*error)
235 return NULL;
236 else
237 return l;
238 }
239
240 LensMaker* lens_maker_get_copy(const LensMaker* self, GError** error) {
241 g_return_val_if_fail (IS_LENS_MAKER (self) || error == NULL || *error == NULL, NULL);
242
243 LensMakerPrivate *priv = lens_maker_get_instance_private((LensMaker*)self);
244 LensMaker* lens_maker = lens_maker_new(priv->id);
245 g_object_set(lens_maker, "lensmaker", priv->name, NULL);
246 LensMakerPrivate *newpriv = lens_maker_get_instance_private(lens_maker);
247 newpriv->lens = g_list_copy_deep(priv->lens, copy_list, error);
248 if (*error) {
249 g_object_unref(lens_maker);
250 return NULL;
251 }
252
253 return lens_maker;
254 }
This page took 1.500756 seconds and 6 git commands to generate.