From acce183e6dfd58ad026d73539922fce812f60d86 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Mon, 13 Jan 2014 18:16:26 +0100 Subject: [PATCH 08/12] log-store: add 'name' property We can now have differente instances of the same type of store without having to inherit to override their name. --- telepathy-logger/log-manager.c | 26 ++++++++++---- telepathy-logger/log-store-empathy.c | 14 +------- telepathy-logger/log-store-internal.h | 4 +-- telepathy-logger/log-store-pidgin.c | 43 +++++++++++++---------- telepathy-logger/log-store-sqlite.c | 49 ++++++++++++++++++++------- telepathy-logger/log-store-xml.c | 40 +++++++++++++--------- telepathy-logger/log-store.c | 25 +++++++++++--- tests/logger/dbus/test-tpl-log-store-pidgin.c | 5 +-- 8 files changed, 132 insertions(+), 74 deletions(-) diff --git a/telepathy-logger/log-manager.c b/telepathy-logger/log-manager.c index ff5f418..d032d87 100644 --- a/telepathy-logger/log-manager.c +++ b/telepathy-logger/log-manager.c @@ -192,11 +192,15 @@ static void add_log_store (TplLogManager *self, TplLogStore *store) { + gchar *name; + g_return_if_fail (TPL_IS_LOG_STORE (store)); + name = _tpl_log_store_dup_name (store); + if (!_tpl_log_manager_register_log_store (self, store)) - CRITICAL ("Failed to register store name=%s", - _tpl_log_store_get_name (store)); + CRITICAL ("Failed to register store name=%s", name); + g_free (name); /* drop the initial ref */ g_object_unref (store); @@ -351,10 +355,14 @@ _tpl_log_manager_add_event (TplLogManager *manager, result = _tpl_log_store_add_event (store, event, &loc_error); if (!result) { + gchar *name = _tpl_log_store_dup_name (store); + CRITICAL ("logstore name=%s: %s. " "Event may not be logged properly.", - _tpl_log_store_get_name (store), + name, loc_error != NULL ? loc_error->message : "no error message"); + + g_free (name); g_clear_error (&loc_error); } /* TRUE if at least one LogStore succeeds */ @@ -388,7 +396,7 @@ _tpl_log_manager_register_log_store (TplLogManager *self, TplLogStore *logstore) { TplLogManagerPriv *priv = self->priv; - const gchar *name = _tpl_log_store_get_name (logstore); + gchar *name = _tpl_log_store_dup_name (logstore); GList *l; g_return_val_if_fail (TPL_IS_LOG_MANAGER (self), FALSE); @@ -398,12 +406,17 @@ _tpl_log_manager_register_log_store (TplLogManager *self, for (l = priv->stores; l != NULL; l = g_list_next (l)) { TplLogStore *store = l->data; + gchar *n = _tpl_log_store_dup_name (store); - if (!tp_strdiff (name, _tpl_log_store_get_name (store))) + if (!tp_strdiff (name, n)) { DEBUG ("name=%s: already registered", name); + g_free (n); + g_free (name); return FALSE; } + + g_free (n); } if (_tpl_log_store_is_readable (logstore)) @@ -415,8 +428,9 @@ _tpl_log_manager_register_log_store (TplLogManager *self, /* reference just once, writable/readable lists are kept in sync with the * general list and never written separately */ priv->stores = g_list_prepend (priv->stores, g_object_ref (logstore)); - DEBUG ("LogStore name=%s registered", _tpl_log_store_get_name (logstore)); + DEBUG ("LogStore name=%s registered", name); + g_free (name); return TRUE; } diff --git a/telepathy-logger/log-store-empathy.c b/telepathy-logger/log-store-empathy.c index d83df57..7b6d646 100644 --- a/telepathy-logger/log-store-empathy.c +++ b/telepathy-logger/log-store-empathy.c @@ -42,25 +42,12 @@ _tpl_log_store_empathy_class_init (TplLogStoreEmpathyClass *klass) { } - -static const gchar * -log_store_empathy_get_name (TplLogStore *store) -{ - TplLogStoreXml *self = (TplLogStoreXml *) store; - - g_return_val_if_fail (TPL_IS_LOG_STORE_EMPATHY (self), NULL); - - return "Empathy"; -} - static void log_store_iface_init (gpointer g_iface, gpointer iface_data) { TplLogStoreInterface *iface = (TplLogStoreInterface *) g_iface; - iface->get_name = log_store_empathy_get_name; - /* We don't want to store new logs in Empathy's directory, just read the old * ones. */ iface->add_event = NULL; @@ -70,5 +57,6 @@ TplLogStore * _tpl_log_store_empathy_new (void) { return g_object_new (TPL_TYPE_LOG_STORE_EMPATHY, + "name", "Empathy", NULL); } diff --git a/telepathy-logger/log-store-internal.h b/telepathy-logger/log-store-internal.h index 9b0a666..158278b 100644 --- a/telepathy-logger/log-store-internal.h +++ b/telepathy-logger/log-store-internal.h @@ -61,7 +61,6 @@ typedef struct { GTypeInterface parent; - const gchar * (*get_name) (TplLogStore *self); gboolean (*exists) (TplLogStore *self, TpAccount *account, TplEntity *target, gint type_mask); gboolean (*add_event) (TplLogStore *self, TplEvent *event, @@ -87,7 +86,8 @@ typedef struct GType _tpl_log_store_get_type (void); -const gchar * _tpl_log_store_get_name (TplLogStore *self); +gchar * _tpl_log_store_dup_name (TplLogStore *self) + G_GNUC_WARN_UNUSED_RESULT; gboolean _tpl_log_store_exists (TplLogStore *self, TpAccount *account, TplEntity *target, gint type_mask); gboolean _tpl_log_store_add_event (TplLogStore *self, TplEvent *event, diff --git a/telepathy-logger/log-store-pidgin.c b/telepathy-logger/log-store-pidgin.c index 491edce..b815b71 100644 --- a/telepathy-logger/log-store-pidgin.c +++ b/telepathy-logger/log-store-pidgin.c @@ -46,13 +46,15 @@ struct _TplLogStorePidginPriv { - TpAccountManager *account_manager; + gchar *name; + TpAccountManager *account_manager; gchar *basedir; }; enum { PROP_0, + PROP_NAME, PROP_READABLE, }; @@ -63,7 +65,6 @@ static void tpl_log_store_pidgin_get_property (GObject *object, guint param_id, GParamSpec *pspec); static void tpl_log_store_pidgin_set_property (GObject *object, guint param_id, const GValue *value, GParamSpec *pspec); -static const gchar *log_store_pidgin_get_name (TplLogStore *store); static const gchar *log_store_pidgin_get_basedir (TplLogStorePidgin *self); static void log_store_pidgin_set_basedir (TplLogStorePidgin *self, const gchar *data); @@ -79,8 +80,13 @@ tpl_log_store_pidgin_get_property (GObject *object, GValue *value, GParamSpec *pspec) { + TplLogStorePidgin *self = TPL_LOG_STORE_PIDGIN (object); + switch (param_id) { + case PROP_NAME: + g_value_set_string (value, self->priv->name); + break; case PROP_READABLE: g_value_set_boolean (value, TRUE); break; @@ -97,8 +103,13 @@ tpl_log_store_pidgin_set_property (GObject *object, const GValue *value, GParamSpec *pspec) { + TplLogStorePidgin *self = TPL_LOG_STORE_PIDGIN (object); + switch (param_id) { + case PROP_NAME: + self->priv->name = g_value_dup_string (value); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); break; @@ -112,12 +123,20 @@ tpl_log_store_pidgin_dispose (GObject *self) TplLogStorePidginPriv *priv = TPL_LOG_STORE_PIDGIN (self)->priv; g_clear_object (&priv->account_manager); - g_free (priv->basedir); - priv->basedir = NULL; G_OBJECT_CLASS (tpl_log_store_pidgin_parent_class)->dispose (self); } +static void +tpl_log_store_pidgin_finalize (GObject *self) +{ + TplLogStorePidginPriv *priv = TPL_LOG_STORE_PIDGIN (self)->priv; + + g_free (priv->basedir); + g_free (priv->name); + + G_OBJECT_CLASS (tpl_log_store_pidgin_parent_class)->finalize (self); +} static void tpl_log_store_pidgin_class_init (TplLogStorePidginClass *klass) @@ -127,7 +146,9 @@ tpl_log_store_pidgin_class_init (TplLogStorePidginClass *klass) object_class->get_property = tpl_log_store_pidgin_get_property; object_class->set_property = tpl_log_store_pidgin_set_property; object_class->dispose = tpl_log_store_pidgin_dispose; + object_class->finalize = tpl_log_store_pidgin_finalize; + g_object_class_override_property (object_class, PROP_NAME, "name"); g_object_class_override_property (object_class, PROP_READABLE, "readable"); g_type_class_add_private (object_class, sizeof (TplLogStorePidginPriv)); @@ -143,18 +164,6 @@ tpl_log_store_pidgin_init (TplLogStorePidgin *self) self->priv->account_manager = tp_account_manager_dup (); } - -static const gchar * -log_store_pidgin_get_name (TplLogStore *store) -{ - TplLogStorePidgin *self = (TplLogStorePidgin *) store; - - g_return_val_if_fail (TPL_IS_LOG_STORE_PIDGIN (self), NULL); - - return TPL_LOG_STORE_PIDGIN_NAME; -} - - /* returns an absolute path for the base directory of LogStore */ static const gchar * log_store_pidgin_get_basedir (TplLogStorePidgin *self) @@ -1106,7 +1115,6 @@ log_store_iface_init (gpointer g_iface, { TplLogStoreInterface *iface = (TplLogStoreInterface *) g_iface; - iface->get_name = log_store_pidgin_get_name; iface->exists = log_store_pidgin_exists; iface->add_event = NULL; iface->get_dates = log_store_pidgin_get_dates; @@ -1121,5 +1129,6 @@ TplLogStore * _tpl_log_store_pidgin_new (void) { return g_object_new (TPL_TYPE_LOG_STORE_PIDGIN, + "name", TPL_LOG_STORE_PIDGIN_NAME, NULL); } diff --git a/telepathy-logger/log-store-sqlite.c b/telepathy-logger/log-store-sqlite.c index c0b00dc..ace554f 100644 --- a/telepathy-logger/log-store-sqlite.c +++ b/telepathy-logger/log-store-sqlite.c @@ -50,11 +50,14 @@ G_DEFINE_TYPE_WITH_CODE (TplLogStoreSqlite, _tpl_log_store_sqlite, enum /* properties */ { PROP_0, + PROP_NAME, PROP_READABLE, }; struct _TplLogStoreSqlitePrivate { + gchar *name; + sqlite3 *db; }; @@ -101,8 +104,13 @@ tpl_log_store_sqlite_get_property (GObject *self, GValue *value, GParamSpec *pspec) { + TplLogStoreSqlitePrivate *priv = TPL_LOG_STORE_SQLITE (self)->priv; + switch (id) { + case PROP_NAME: + g_value_set_string (value, priv->name); + break; case PROP_READABLE: /* this store should never be queried by the LogManager */ g_value_set_boolean (value, FALSE); @@ -114,6 +122,24 @@ tpl_log_store_sqlite_get_property (GObject *self, } } +static void +tpl_log_store_sqlite_set_property (GObject *self, + guint id, + const GValue *value, + GParamSpec *pspec) +{ + TplLogStoreSqlitePrivate *priv = TPL_LOG_STORE_SQLITE (self)->priv; + + switch (id) + { + case PROP_NAME: + priv->name = g_value_dup_string (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec); + break; + } +} static void purge_pending_messages (TplLogStoreSqlitePrivate *priv, @@ -270,7 +296,7 @@ out: static void -tpl_log_store_sqlite_dispose (GObject *self) +tpl_log_store_sqlite_finalize (GObject *self) { TplLogStoreSqlitePrivate *priv = TPL_LOG_STORE_SQLITE (self)->priv; @@ -280,7 +306,9 @@ tpl_log_store_sqlite_dispose (GObject *self) priv->db = NULL; } - G_OBJECT_CLASS (_tpl_log_store_sqlite_parent_class)->dispose (self); + g_free (priv->name); + + G_OBJECT_CLASS (_tpl_log_store_sqlite_parent_class)->finalize (self); } @@ -291,8 +319,10 @@ _tpl_log_store_sqlite_class_init (TplLogStoreSqliteClass *klass) gobject_class->constructor = tpl_log_store_sqlite_constructor; gobject_class->get_property = tpl_log_store_sqlite_get_property; - gobject_class->dispose = tpl_log_store_sqlite_dispose; + gobject_class->set_property = tpl_log_store_sqlite_set_property; + gobject_class->finalize = tpl_log_store_sqlite_finalize; + g_object_class_override_property (gobject_class, PROP_NAME, "name"); g_object_class_override_property (gobject_class, PROP_READABLE, "readable"); g_type_class_add_private (gobject_class, sizeof (TplLogStoreSqlitePrivate)); @@ -353,14 +383,6 @@ get_datetime (gint64 timestamp) return date; } - -static const char * -tpl_log_store_sqlite_get_name (TplLogStore *self) -{ - return TPL_LOG_STORE_SQLITE_NAME; -} - - static gboolean tpl_log_store_sqlite_add_message_counter (TplLogStore *self, TplEvent *message, @@ -617,7 +639,6 @@ out: static void log_store_iface_init (TplLogStoreInterface *iface) { - iface->get_name = tpl_log_store_sqlite_get_name; iface->add_event = tpl_log_store_sqlite_add_event; iface->get_entities = tpl_log_store_sqlite_get_entities; } @@ -625,7 +646,9 @@ log_store_iface_init (TplLogStoreInterface *iface) TplLogStore * _tpl_log_store_sqlite_dup (void) { - return g_object_new (TPL_TYPE_LOG_STORE_SQLITE, NULL); + return g_object_new (TPL_TYPE_LOG_STORE_SQLITE, + "name", TPL_LOG_STORE_SQLITE_NAME, + NULL); } diff --git a/telepathy-logger/log-store-xml.c b/telepathy-logger/log-store-xml.c index 34b896a..3a05dad 100644 --- a/telepathy-logger/log-store-xml.c +++ b/telepathy-logger/log-store-xml.c @@ -79,12 +79,14 @@ struct _TplLogStoreXmlPriv { + gchar *name; gchar *basedir; TpAccountManager *account_manager; }; enum { PROP_0, + PROP_NAME, PROP_READABLE, }; @@ -131,6 +133,8 @@ log_store_xml_finalize (GObject *object) TplLogStoreXml *self = TPL_LOG_STORE_XML (object); TplLogStoreXmlPriv *priv = self->priv; + g_free (self->priv->name); + if (priv->basedir != NULL) { g_free (priv->basedir); @@ -145,8 +149,13 @@ tpl_log_store_xml_get_property (GObject *object, GValue *value, GParamSpec *pspec) { + TplLogStoreXml *self = TPL_LOG_STORE_XML (object); + switch (param_id) { + case PROP_NAME: + g_value_set_string (value, self->priv->name); + break; case PROP_READABLE: g_value_set_boolean (value, TRUE); break; @@ -163,8 +172,13 @@ tpl_log_store_xml_set_property (GObject *object, const GValue *value, GParamSpec *pspec) { + TplLogStoreXml *self = TPL_LOG_STORE_XML (object); + switch (param_id) { + case PROP_NAME: + self->priv->name = g_value_dup_string (value); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); break; @@ -182,6 +196,7 @@ _tpl_log_store_xml_class_init (TplLogStoreXmlClass *klass) object_class->get_property = tpl_log_store_xml_get_property; object_class->set_property = tpl_log_store_xml_set_property; + g_object_class_override_property (object_class, PROP_NAME, "name"); g_object_class_override_property (object_class, PROP_READABLE, "readable"); g_type_class_add_private (object_class, sizeof (TplLogStoreXmlPriv)); @@ -634,6 +649,7 @@ log_store_xml_add_event (TplLogStore *store, GError **error) { TplLogStoreXml *self = TPL_LOG_STORE_XML (store); + gchar *name; g_return_val_if_fail (TPL_IS_EVENT (event), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); @@ -643,10 +659,13 @@ log_store_xml_add_event (TplLogStore *store, else if (TPL_IS_CALL_EVENT (event)) return add_call_event (self, TPL_CALL_EVENT (event), error); + name = _tpl_log_store_dup_name (store); + DEBUG ("TplEntry not handled by this LogStore (%s). " - "Ignoring Event", _tpl_log_store_get_name (store)); + "Ignoring Event", name); /* do not consider it an error, this LogStore simply do not want/need * this Event */ + g_free (name); return TRUE; } @@ -1691,18 +1710,6 @@ log_store_xml_get_entities (TplLogStore *store, return entities; } - -static const gchar * -log_store_xml_get_name (TplLogStore *store) -{ - TplLogStoreXml *self = (TplLogStoreXml *) store; - - g_return_val_if_fail (TPL_IS_LOG_STORE_XML (self), NULL); - - return "TpLogger"; -} - - /* returns am absolute path for the base directory of LogStore */ static const gchar * log_store_xml_get_basedir (TplLogStoreXml *self) @@ -1714,12 +1721,13 @@ log_store_xml_get_basedir (TplLogStoreXml *self) if (self->priv->basedir == NULL) { gchar *dir; - const char *name; + char *name; - name = _tpl_log_store_get_name ((TplLogStore *) self); + name = _tpl_log_store_dup_name ((TplLogStore *) self); dir = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (), name, "logs", NULL); log_store_xml_set_basedir (self, dir); + g_free (name); g_free (dir); } @@ -1876,7 +1884,6 @@ log_store_iface_init (gpointer g_iface, { TplLogStoreInterface *iface = (TplLogStoreInterface *) g_iface; - iface->get_name = log_store_xml_get_name; iface->exists = log_store_xml_exists; iface->add_event = log_store_xml_add_event; iface->get_dates = log_store_xml_get_dates; @@ -1894,5 +1901,6 @@ TplLogStore * _tpl_log_store_xml_new (void) { return g_object_new (TPL_TYPE_LOG_STORE_XML, + "name", "TpLogger", NULL); } diff --git a/telepathy-logger/log-store.c b/telepathy-logger/log-store.c index 6e1d10f..48952ec 100644 --- a/telepathy-logger/log-store.c +++ b/telepathy-logger/log-store.c @@ -37,6 +37,11 @@ * implement in order to be used into a #TplLogManager. */ +enum +{ + PROP_NAME = 1 +}; + static void _tpl_log_store_init (gpointer g_iface); GType @@ -66,6 +71,14 @@ _tpl_log_store_get_type (void) static void _tpl_log_store_init (gpointer g_iface) { + g_object_interface_install_property (g_iface, + g_param_spec_string ("name", + "Name", + "The name of the log store", + NULL, + G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | + G_PARAM_STATIC_STRINGS)); + /** * TplLogStore:readable: * @@ -84,14 +97,16 @@ _tpl_log_store_init (gpointer g_iface) G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); } -const gchar * -_tpl_log_store_get_name (TplLogStore *self) +gchar * +_tpl_log_store_dup_name (TplLogStore *self) { + gchar *name; + g_return_val_if_fail (TPL_IS_LOG_STORE (self), NULL); - if (!TPL_LOG_STORE_GET_INTERFACE (self)->get_name) - return NULL; - return TPL_LOG_STORE_GET_INTERFACE (self)->get_name (self); + g_object_get (self, "name", &name, NULL); + + return name; } diff --git a/tests/logger/dbus/test-tpl-log-store-pidgin.c b/tests/logger/dbus/test-tpl-log-store-pidgin.c index 01cbd79..4a1aac8 100644 --- a/tests/logger/dbus/test-tpl-log-store-pidgin.c +++ b/tests/logger/dbus/test-tpl-log-store-pidgin.c @@ -346,11 +346,12 @@ static void test_get_name (PidginTestCaseFixture *fixture, gconstpointer user_data) { - const gchar *name; + gchar *name; - name = _tpl_log_store_get_name (TPL_LOG_STORE (fixture->store)); + name = _tpl_log_store_dup_name (TPL_LOG_STORE (fixture->store)); g_assert_cmpstr (name, ==, "Pidgin"); + g_free (name); } static void -- 1.8.4.2