]>
git.datanom.net - gtk_lensdatabase.git/blob - src/lens.c
4 * Copyright 2024 Michael Rasmussen <mir@datanom.net>
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.
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.
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,
36 static GParamSpec
*obj_properties
[N_PROPERTIES
] = { NULL
, };
37 static void lens_finalize (GObject
*object
);
38 static void lens_dispose (GObject
*gobject
);
41 /* add your private declarations here */
52 /* add your public declarations here */
55 G_DEFINE_TYPE_WITH_PRIVATE(Lens
, lens
, G_TYPE_OBJECT
)
57 static void lens_set_property ( GObject
*object
,
61 LensPrivate
*priv
= lens_get_instance_private(LENS (object
));
65 priv
->id
= g_value_get_int (value
);
70 priv
->sn
= g_strdup(g_value_get_string (value
));
75 priv
->data
= g_strdup(g_value_get_string (value
));
80 priv
->notes
= g_strdup(g_value_get_string (value
));
85 priv
->date
= g_strdup(g_value_get_string (value
));
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
94 static void lens_get_property ( GObject
*object
,
98 LensPrivate
*priv
= lens_get_instance_private(LENS (object
));
102 g_value_set_int (value
, priv
->id
);
105 g_value_set_string (value
, priv
->sn
);
108 g_value_set_string (value
, priv
->data
);
111 g_value_set_string (value
, priv
->notes
);
114 g_value_set_string (value
, priv
->date
);
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
123 static void lens_class_init (LensClass
*klass
) {
124 GObjectClass
*g_object_class
;
126 g_object_class
= G_OBJECT_CLASS (klass
);
128 g_object_class
->dispose
= lens_dispose
;
129 g_object_class
->finalize
= lens_finalize
;
131 g_object_class
->set_property
= lens_set_property
;
132 g_object_class
->get_property
= lens_get_property
;
134 obj_properties
[PROP_ID
] =
135 g_param_spec_int ("id",
141 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
);
142 obj_properties
[PROP_SN
] =
143 g_param_spec_string ("sn",
145 "Serial Number of the lens.",
146 NULL
/* default value */,
148 obj_properties
[PROP_DATA
] =
149 g_param_spec_string ("data",
152 NULL
/* default value */,
154 obj_properties
[PROP_NOTES
] =
155 g_param_spec_string ("notes",
158 NULL
/* default value */,
160 obj_properties
[PROP_DATE
] =
161 g_param_spec_string ("date",
163 "Date the lens was added.",
164 NULL
/* default value */,
165 G_PARAM_CONSTRUCT
| G_PARAM_READWRITE
);
167 g_object_class_install_properties (g_object_class
,
172 static void lens_dispose (GObject
*gobject
) {
173 //LensPrivate *priv = lens_get_instance_private(LENS(gobject));
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
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);
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
191 G_OBJECT_CLASS(lens_parent_class
)->dispose (gobject
);
194 static void lens_finalize (GObject
*object
) {
197 g_return_if_fail (IS_LENS (object
));
199 self
= LENS (object
);
201 LensPrivate
*priv
= lens_get_instance_private(self
);
207 G_OBJECT_CLASS (lens_parent_class
)->finalize (object
);
211 static void lens_init (Lens
*self
) {
212 LensPrivate
*priv
= lens_get_instance_private(self
);
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
);
223 gchar
* now
= get_current_timestamp();
224 Lens
* lens
= LENS(g_object_new (TYPE_LENS
, "id", id
, "sn", sn
, "data", data
, "date", now
, NULL
));
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
);
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
);
241 LensPrivate
*priv
= lens_get_instance_private(self
);
244 priv
->sn
= g_strdup(sn
);
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
);
251 LensPrivate
*priv
= lens_get_instance_private(self
);
254 priv
->data
= g_strdup(data
);
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
);
261 LensPrivate
*priv
= lens_get_instance_private(self
);
264 priv
->notes
= g_strdup(notes
);
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
);
271 LensPrivate
*priv
= lens_get_instance_private(self
);
274 priv
->date
= g_strdup(date
);
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);
281 LensPrivate
*priv
= lens_get_instance_private(self
);
286 gchar
* lens_to_string(Lens
* self
) {
287 g_return_val_if_fail (IS_LENS (self
), NULL
);
295 g_object_get(self
, "id", &id
, "data", &data
, "date", &date
, "sn", &sn
, "notes", ¬es
, NULL
);
297 gchar
* s
= g_strdup_printf("[%d [%s:%s:%s - %s]]", id
, sn
, data
, notes
, date
);
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
);
310 LensPrivate
*priv
= lens_get_instance_private((Lens
*)self
);
312 Lens
* lens
= lens_new(priv
->id
, priv
->sn
, priv
->data
, error
);
314 g_object_unref(lens
);
317 g_object_set(lens
, "notes", priv
->notes
, "date", priv
->date
, NULL
);
This page took 0.400832 seconds and 6 git commands to generate.