/* * Author: Chris Lord * * Copyright (c) 2007 OpenedHand Ltd - http://www.openedhand.com/ * * 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, 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. * */ /** * SECTION:jana-ecal-component * @short_description: An implementation of #JanaComponent using libecal * * #JanaEcalComponent is an implementation of #JanaComponent that provides a * wrapper over #ECalComponent, using libecal. */ #include "jana-ecal-component.h" #include "jana-ecal-time.h" #include #include #include static void component_interface_init (gpointer g_iface, gpointer iface_data); static JanaComponentType component_get_component_type (JanaComponent *self); static gboolean component_is_fully_represented (JanaComponent *self); static gchar * component_get_uid (JanaComponent *self); static gboolean component_supports_custom_props (JanaComponent *self); static GList * component_get_custom_props_list (JanaComponent *self); static gchar * component_get_custom_prop (JanaComponent *self, const gchar *name); static gboolean component_set_custom_prop (JanaComponent *self, const gchar *name, const gchar *value); G_DEFINE_TYPE_WITH_CODE (JanaEcalComponent, jana_ecal_component, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (JANA_TYPE_COMPONENT, component_interface_init)); #define COMPONENT_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ JANA_ECAL_TYPE_COMPONENT, JanaEcalComponentPrivate)) typedef struct _JanaEcalComponentPrivate JanaEcalComponentPrivate; struct _JanaEcalComponentPrivate { ECalComponent *comp; }; enum { PROP_ECALCOMP = 1, }; static void jana_ecal_component_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { JanaEcalComponentPrivate *priv = COMPONENT_PRIVATE (object); switch (property_id) { case PROP_ECALCOMP : g_value_set_object (value, priv->comp); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } } static void jana_ecal_component_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { JanaEcalComponentPrivate *priv = COMPONENT_PRIVATE (object); switch (property_id) { case PROP_ECALCOMP : priv->comp = E_CAL_COMPONENT (g_value_dup_object (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } } static void jana_ecal_component_dispose (GObject *object) { JanaEcalComponentPrivate *priv = COMPONENT_PRIVATE (object); if (priv->comp) { g_object_unref (priv->comp); priv->comp = NULL; } if (G_OBJECT_CLASS (jana_ecal_component_parent_class)->dispose) G_OBJECT_CLASS (jana_ecal_component_parent_class)->dispose (object); } static void jana_ecal_component_finalize (GObject *object) { G_OBJECT_CLASS (jana_ecal_component_parent_class)->finalize (object); } static void component_interface_init (gpointer g_iface, gpointer iface_data) { JanaComponentInterface *iface = (JanaComponentInterface *)g_iface; iface->get_component_type = component_get_component_type; iface->is_fully_represented = component_is_fully_represented; iface->get_uid = component_get_uid; iface->supports_custom_props = component_supports_custom_props; iface->get_custom_props_list = component_get_custom_props_list; iface->get_custom_prop = component_get_custom_prop; iface->set_custom_prop = component_set_custom_prop; } static void jana_ecal_component_class_init (JanaEcalComponentClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); g_type_class_add_private (klass, sizeof (JanaEcalComponentPrivate)); object_class->get_property = jana_ecal_component_get_property; object_class->set_property = jana_ecal_component_set_property; object_class->dispose = jana_ecal_component_dispose; object_class->finalize = jana_ecal_component_finalize; g_object_class_install_property ( object_class, PROP_ECALCOMP, g_param_spec_object ( "ecalcomp", "ECalComponent *", "The ECalComponent represented by this JanaComponent " "object.", E_TYPE_CAL_COMPONENT, G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY)); } static void jana_ecal_component_init (JanaEcalComponent *self) { } /** * jana_ecal_component_new_from_ecalcomp: * @component: An #ECalComponent * * Creates a new #JanaEcalComponent from an #ECalComponent. * * Returns: A new #JanaEcalComponent that wraps the given #ECalComponent, * cast as a #JanaComponent. */ JanaComponent * jana_ecal_component_new_from_ecalcomp (ECalComponent *component) { return JANA_COMPONENT (g_object_new (JANA_ECAL_TYPE_COMPONENT, "ecalcomp", component, NULL)); } static JanaComponentType component_get_component_type (JanaComponent *self) { /* Sub-classes should override this method */ return JANA_COMPONENT_NULL; } static gboolean component_is_fully_represented (JanaComponent *self) { /* Sub-classes should override this method */ return FALSE; } static gchar * component_get_uid (JanaComponent *self) { const char *uid = NULL; JanaEcalComponentPrivate *priv = COMPONENT_PRIVATE (self); e_cal_component_get_uid (priv->comp, &uid); return uid ? g_strdup (uid) : NULL; } static gboolean component_supports_custom_props (JanaComponent *self) { return TRUE; } static GList * component_get_custom_props_list (JanaComponent *self) { icalproperty *prop; icalcomponent *comp; GList *props = NULL; JanaEcalComponentPrivate *priv = COMPONENT_PRIVATE (self); comp = e_cal_component_get_icalcomponent (priv->comp); for (prop = icalcomponent_get_first_property (comp, ICAL_X_PROPERTY); prop; prop = icalcomponent_get_next_property ( comp, ICAL_X_PROPERTY)) { gchar **prop_pair = g_new (gchar *, 2); gchar *name = g_strdup (icalproperty_get_x_name (prop)); gchar *value = g_strdup (icalproperty_get_value_as_string ( prop)); prop_pair[0] = name; prop_pair[1] = value; props = g_list_prepend (props, prop_pair); } return props; } static gchar * component_get_custom_prop (JanaComponent *self, const gchar *name) { icalproperty *prop; icalcomponent *comp; JanaEcalComponentPrivate *priv = COMPONENT_PRIVATE (self); comp = e_cal_component_get_icalcomponent (priv->comp); /* See if the property exists first */ for (prop = icalcomponent_get_first_property (comp, ICAL_X_PROPERTY); prop; prop = icalcomponent_get_next_property ( comp, ICAL_X_PROPERTY)) { if (strcmp (icalproperty_get_x_name (prop), name) == 0) { return g_strdup (icalproperty_get_value_as_string ( prop)); } } return NULL; } static gboolean component_set_custom_prop (JanaComponent *self, const gchar *name, const gchar *value) { icalproperty *prop; icalcomponent *comp; gboolean exists = FALSE; JanaEcalComponentPrivate *priv = COMPONENT_PRIVATE (self); if (strncmp ("X-", name, 2) != 0) return FALSE; comp = e_cal_component_get_icalcomponent (priv->comp); /* See if the property exists first */ for (prop = icalcomponent_get_first_property (comp, ICAL_X_PROPERTY); prop; prop = icalcomponent_get_next_property ( comp, ICAL_X_PROPERTY)) { if (strcmp (icalproperty_get_x_name (prop), name) == 0) { exists = TRUE; break; } } if (!exists) { /* Create a new property */ prop = icalproperty_new (ICAL_X_PROPERTY); icalproperty_set_x_name (prop, name); } icalproperty_set_value (prop, icalvalue_new_from_string ( ICAL_X_VALUE, value)); if (!exists) { icalcomponent_add_property (comp, prop); /* FIXME: Check that the property added without errors - no * idea how to do this :( */ /*if (icalproperty_isa (prop) != ICAL_X_PROPERTY) { icalcomponent_remove_property (comp, prop); icalproperty_free (prop); return FALSE; }*/ } return TRUE; }