/* * lens.c * * Copyright 2024 Michael Rasmussen * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * * */ #include "lens.h" #include enum { PROP_ID = 1, PROP_SN, PROP_DATA, PROP_NOTES, PROP_DATE, N_PROPERTIES }; static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; static void lens_finalize (GObject *object); static void lens_dispose (GObject *gobject); typedef struct { /* add your private declarations here */ gint id; gchar* sn; gchar* data; gchar* notes; gchar* date; GList* lens; } LensPrivate; struct _Lens { GObject parent; /* add your public declarations here */ }; G_DEFINE_TYPE_WITH_PRIVATE(Lens, lens, G_TYPE_OBJECT) static void lens_set_property ( GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { LensPrivate *priv = lens_get_instance_private(LENS (object)); switch (prop_id) { case PROP_ID: priv->id = g_value_get_int (value); break; case PROP_SN: if (priv->sn) g_free(priv->sn); priv->sn = g_strdup(g_value_get_string (value)); break; case PROP_DATA: if (priv->data) g_free(priv->data); priv->data = g_strdup(g_value_get_string (value)); break; case PROP_NOTES: if (priv->notes) g_free(priv->notes); priv->notes = g_strdup(g_value_get_string (value)); break; case PROP_DATE: if (priv->date) g_free(priv->date); priv->date = g_strdup(g_value_get_string (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } } static void lens_get_property ( GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) { LensPrivate *priv = lens_get_instance_private(LENS (object)); switch (prop_id) { case PROP_ID: g_value_set_int (value, priv->id); break; case PROP_SN: g_value_set_string (value, priv->sn); break; case PROP_DATA: g_value_set_string (value, priv->data); break; case PROP_NOTES: g_value_set_string (value, priv->notes); break; case PROP_DATE: g_value_set_string (value, priv->date); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } } static void lens_class_init (LensClass *klass) { GObjectClass *g_object_class; g_object_class = G_OBJECT_CLASS (klass); g_object_class->dispose = lens_dispose; g_object_class->finalize = lens_finalize; g_object_class->set_property = lens_set_property; g_object_class->get_property = lens_get_property; obj_properties[PROP_ID] = g_param_spec_int ("id", "Id", "Database id.", G_MININT, G_MAXINT, 0, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); obj_properties[PROP_SN] = g_param_spec_string ("sn", "SerialNumber", "Serial Number of the lens.", NULL /* default value */, G_PARAM_READWRITE); obj_properties[PROP_DATA] = g_param_spec_string ("data", "Data", "Lens description.", NULL /* default value */, G_PARAM_READWRITE); obj_properties[PROP_NOTES] = g_param_spec_string ("notes", "Notes", "Lens notes.", NULL /* default value */, G_PARAM_READWRITE); obj_properties[PROP_DATE] = g_param_spec_string ("date", "Date", "Date the lens was added.", NULL /* default value */, G_PARAM_CONSTRUCT | G_PARAM_READWRITE); g_object_class_install_properties (g_object_class, N_PROPERTIES, obj_properties); } static void lens_dispose (GObject *gobject) { //LensPrivate *priv = lens_get_instance_private(LENS(gobject)); /* In dispose(), you are supposed to free all types referenced from this * object which might themselves hold a reference to self. Generally, * the most simple solution is to unref all members on which you own a * reference. */ /* dispose() might be called multiple times, so we must guard against * calling g_object_unref() on an invalid GObject by setting the member * NULL; g_clear_object() does this for us. * g_clear_object (&priv->input_stream); */ /* Always chain up to the parent class; there is no need to check if * the parent class implements the dispose() virtual function: it is * always guaranteed to do so */ G_OBJECT_CLASS(lens_parent_class)->dispose (gobject); } static void lens_finalize (GObject *object) { Lens *self; g_return_if_fail (IS_LENS (object)); self = LENS (object); LensPrivate *priv = lens_get_instance_private(self); g_free(priv->data); g_free(priv->date); g_free(priv->sn); g_free(priv->notes); G_OBJECT_CLASS (lens_parent_class)->finalize (object); } static void lens_init (Lens *self) { LensPrivate *priv = lens_get_instance_private(self); priv->data = NULL; priv->date = NULL; priv->sn = NULL; priv->notes = NULL;; } Lens *lens_new (gint id, const gchar* sn, const gchar* data, GError** error) { g_return_val_if_fail (sn == NULL || data == NULL || error == NULL || *error == NULL, NULL); gchar* now = get_current_timestamp(); Lens* lens = LENS(g_object_new (TYPE_LENS, "id", id, "sn", sn, "data", data, "date", now, NULL)); g_free(now); return lens; } gchar* get_current_timestamp() { GDateTime* dt = g_date_time_new_now_local(); gchar* now = g_date_time_format (dt, "%Y-%m-%d %H:%M:%S"); g_date_time_unref(dt); return now; } void lens_set_sn(Lens* self, const gchar* sn, GError** error) { g_return_if_fail (IS_LENS (self)); g_return_if_fail (sn == NULL || error == NULL || *error == NULL); LensPrivate *priv = lens_get_instance_private(self); if (priv->sn) g_free(priv->sn); priv->sn = g_strdup(sn); } void lens_set_data(Lens* self, const gchar* data, GError** error) { g_return_if_fail (IS_LENS (self)); g_return_if_fail (data == NULL || error == NULL || *error == NULL); LensPrivate *priv = lens_get_instance_private(self); if (priv->data) g_free(priv->data); priv->data = g_strdup(data); } void lens_set_notes(Lens* self, const gchar* notes, GError** error) { g_return_if_fail (IS_LENS (self)); g_return_if_fail (notes == NULL || error == NULL || *error == NULL); LensPrivate *priv = lens_get_instance_private(self); if (priv->notes) g_free(priv->notes); priv->notes = g_strdup(notes); } void lens_set_date(Lens* self, const gchar* date, GError** error) { g_return_if_fail (IS_LENS (self)); g_return_if_fail (date == NULL || error == NULL || *error == NULL); LensPrivate *priv = lens_get_instance_private(self); if (priv->date) g_free(priv->date); priv->date = g_strdup(date); } gint lens_get_id(Lens* self, GError** error) { g_return_val_if_fail (IS_LENS (self), -1); g_return_val_if_fail (error == NULL || *error == NULL, -1); LensPrivate *priv = lens_get_instance_private(self); return priv->id; } gchar* lens_to_string(Lens* self) { g_return_val_if_fail (IS_LENS (self), NULL); gchar* data; gchar* date; gchar* sn; gchar* notes; gint id; g_object_get(self, "id", &id, "data", &data, "date", &date, "sn", &sn, "notes", ¬es, NULL); gchar* s = g_strdup_printf("[%d [%s:%s:%s - %s]]", id, sn, data, notes, date); g_free(data); g_free(date); g_free(sn); g_free(notes); return s; } Lens* lens_get_copy(const Lens* self, GError** error) { g_return_val_if_fail (IS_LENS (self), NULL); g_return_val_if_fail (error == NULL || *error == NULL, NULL); LensPrivate *priv = lens_get_instance_private((Lens*)self); Lens* lens = lens_new(priv->id, priv->sn, priv->data, error); if (*error) { g_object_unref(lens); return NULL; } g_object_set(lens, "notes", priv->notes, "date", priv->date, NULL); return lens; }