From c0a2104a590e9e68226f836d22ff6e645f3e33d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Thu, 4 Apr 2013 11:24:21 -0400 Subject: Add ignore list capability This allow disabling logging for a specific contact. Fix bug https://bugs.freedesktop.org/show_bug.cgi?id=54033 --- telepathy-logger/conf-internal.h | 6 +- telepathy-logger/conf.c | 66 ++++++++++++++- telepathy-logger/log-manager.c | 178 ++++++++++++++++++++++++++++++++++++++- telepathy-logger/log-manager.h | 12 +++ 4 files changed, 256 insertions(+), 6 deletions(-) (limited to 'telepathy-logger') diff --git a/telepathy-logger/conf-internal.h b/telepathy-logger/conf-internal.h index bf80860..f88aa33 100644 --- a/telepathy-logger/conf-internal.h +++ b/telepathy-logger/conf-internal.h @@ -49,12 +49,10 @@ GType _tpl_conf_get_type (void); TplConf *_tpl_conf_dup (void); gboolean _tpl_conf_is_globally_enabled (TplConf *self); -gboolean _tpl_conf_is_account_ignored (TplConf *self, - const gchar *account_path); -// GSList *_tpl_conf_get_accounts_ignorelist (TplConf *self); +const gchar **_tpl_conf_get_ignorelist (TplConf *self); void _tpl_conf_globally_enable (TplConf *self, gboolean enable); -// void _tpl_conf_set_accounts_ignorelist (TplConf *self, GSList *newlist); +void _tpl_conf_set_ignorelist (TplConf *self, const gchar **newlist); G_END_DECLS #endif // __TPL_CONF_H__ diff --git a/telepathy-logger/conf.c b/telepathy-logger/conf.c index d9e21fb..768ece3 100644 --- a/telepathy-logger/conf.c +++ b/telepathy-logger/conf.c @@ -42,6 +42,7 @@ static TplConf *conf_singleton = NULL; typedef struct { gboolean test_mode; + gchar **ignore_list; GSettings *gsettings; } TplConfPriv; @@ -49,7 +50,8 @@ typedef struct enum /* properties */ { PROP_0, - PROP_GLOBALLY_ENABLED + PROP_GLOBALLY_ENABLED, + PROP_IGNORE_LIST, }; @@ -108,6 +110,9 @@ tpl_conf_finalize (GObject *obj) priv = GET_PRIV (obj); + g_strfreev (priv->ignore_list); + priv->ignore_list = NULL; + if (priv->gsettings != NULL) { g_object_unref (priv->gsettings); @@ -157,6 +162,12 @@ _tpl_conf_class_init (TplConfClass *klass) TRUE, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE)); + g_object_class_install_property (object_class, PROP_IGNORE_LIST, + g_param_spec_pointer ("ignore-list", + "Ignore List", + "List of TplEntities with which not to log conversations.", + G_PARAM_READWRITE)); + g_type_class_add_private (object_class, sizeof (TplConfPriv)); } @@ -178,6 +189,8 @@ _tpl_conf_init (TplConf *self) g_signal_connect (priv->gsettings, "changed::" KEY_ENABLED, G_CALLBACK (_notify_globally_enable), self); } + + priv->ignore_list = NULL; } @@ -243,3 +256,54 @@ _tpl_conf_globally_enable (TplConf *self, g_settings_set_boolean (GET_PRIV (self)->gsettings, KEY_ENABLED, enable); } + +/** + * _tpl_conf_set_accounts_ignorelist: + * @self: a TplConf instance + * @newlist: a NULL-terminated list of account/entity IDs that should not be logged + */ +void +_tpl_conf_set_ignorelist (TplConf *self, + const gchar **newlist) +{ + TplConfPriv *priv; + + g_return_if_fail (TPL_IS_CONF (self)); + + priv = GET_PRIV (self); + + if (!priv->test_mode) { + g_settings_set_strv (GET_PRIV (self)->gsettings, "ignorelist", newlist); + } + + g_strfreev (priv->ignore_list); + priv->ignore_list = g_strdupv ((gchar **) newlist); + + g_object_notify (G_OBJECT (self), "ignore-list"); +} + +/** + * _tpl_conf_get_accounts_ignorelist: + * @self: a TplConf instance + * + * Provides list of IDs in "account_id/entity_id" format. Events from or to + * this entities should not be logged. + * + * Return value: (transfer-full) a newly allocated NULL-terminated list of contact IDs. + * The list is owned by the @self and should not be freed. + */ +const gchar** +_tpl_conf_get_ignorelist (TplConf *self) +{ + TplConfPriv *priv; + + g_return_val_if_fail (TPL_IS_CONF (self), NULL); + + priv = GET_PRIV (self); + + if ((priv->ignore_list == NULL) && (!priv->test_mode)) { + priv->ignore_list = g_settings_get_strv (priv->gsettings, "ignorelist"); + } + + return (const gchar **) priv->ignore_list; +} diff --git a/telepathy-logger/log-manager.c b/telepathy-logger/log-manager.c index 37ff1d4..9fb3f6e 100644 --- a/telepathy-logger/log-manager.c +++ b/telepathy-logger/log-manager.c @@ -308,7 +308,6 @@ tpl_log_manager_dup_singleton (void) return g_object_new (TPL_TYPE_LOG_MANAGER, NULL); } - /* * _tpl_log_manager_add_event: * @manager: the log manager @@ -329,6 +328,9 @@ _tpl_log_manager_add_event (TplLogManager *manager, GList *l; gboolean retval = FALSE; + TplEntity *target; + TpAccount *account; + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), FALSE); g_return_val_if_fail (TPL_IS_EVENT (event), FALSE); @@ -341,6 +343,18 @@ _tpl_log_manager_add_event (TplLogManager *manager, return FALSE; } + account = tpl_event_get_account (event); + + /* check whether receiver is in the list of contacts to ignore */ + target = tpl_event_get_receiver (event); + if (tpl_log_manager_is_disabled_for_entity (manager, account, target)) + return FALSE; + + /* check whether sender is in the list of contacts to ignore */ + target = tpl_event_get_sender (event); + if (tpl_log_manager_is_disabled_for_entity (manager, account, target)) + return FALSE; + /* send the event to any writable log store */ for (l = priv->writable_stores; l != NULL; l = g_list_next (l)) { @@ -1441,3 +1455,165 @@ _tpl_log_manager_search_hit_copy (TplLogSearchHit *hit) return _tpl_log_manager_search_hit_new (hit->account, hit->target, hit->date); } + +static gchar * +_tpl_log_manager_build_identifier (TpAccount *account, + TplEntity *entity) +{ + gchar *identifier; + const gchar *acc_name = tp_proxy_get_object_path (account); + if (g_str_has_prefix (acc_name, TP_ACCOUNT_OBJECT_PATH_BASE)) + acc_name += strlen (TP_ACCOUNT_OBJECT_PATH_BASE); + + identifier = g_strconcat (acc_name, "/", tpl_entity_get_identifier (entity), NULL); + + return identifier; +} + +static gboolean +_tpl_log_manager_is_disabled_for_entity (TplLogManager *self, + const gchar *identifier) +{ + gint i; + TplLogManagerPriv *priv = self->priv; + const gchar **ignorelist; + + priv = self->priv; + ignorelist = _tpl_conf_get_ignorelist (priv->conf); + + for (i = 0; ignorelist && ignorelist[i]; i++) + { + if (g_strcmp0 (ignorelist[i], identifier) == 0) + { + return TRUE; + } + } + + return FALSE; +} + +/** + * tpl_log_manager_disable_for_entity: + * @self: the log manager + * @entity a TplEntity + * + * Disables logging of events for given entity. By default logging is enabled + * for all entities. + */ +void +tpl_log_manager_disable_for_entity (TplLogManager *self, + TpAccount *account, + TplEntity *entity) +{ + TplLogManagerPriv *priv; + gchar *identifier; + + g_return_if_fail (TPL_IS_LOG_MANAGER (self)); + g_return_if_fail (TP_IS_ACCOUNT (account)); + g_return_if_fail (TPL_IS_ENTITY (entity)); + + priv = self->priv; + identifier = _tpl_log_manager_build_identifier (account, entity); + if (!_tpl_log_manager_is_disabled_for_entity (self, identifier)) + { + const gchar **ignorelist = _tpl_conf_get_ignorelist (priv->conf); + gchar **newlist; + if (ignorelist) + { + gint newlen; + newlist = g_strdupv ((gchar **) ignorelist); + newlen = g_strv_length (newlist) + 1; + newlist = g_realloc (newlist, sizeof (gchar*) * newlen ); + newlist[newlen - 1] = g_strdup (identifier); + } + else + { + newlist = g_malloc0_n (2, sizeof (gchar*)); + newlist[0] = g_strdup (identifier); + } + + _tpl_conf_set_ignorelist (priv->conf, (const gchar **) newlist); + g_strfreev (newlist); + } + + g_free (identifier); +} + +/** + * tpl_log_manager_enable_for_entity: + * @self: the log manager + * @entity: a TplEntity + * + * Re-enables logging of events for entity previously disabled by + * tpl_log_manager_disable_for_entity(). By default logging is enabled for all + * entities. + */ +void +tpl_log_manager_enable_for_entity (TplLogManager *self, + TpAccount *account, + TplEntity *entity) +{ + TplLogManagerPriv *priv; + gchar *identifier; + + g_return_if_fail (TPL_IS_LOG_MANAGER (self)); + g_return_if_fail (TP_IS_ACCOUNT (account)); + g_return_if_fail (TPL_IS_ENTITY (entity)); + + priv = self->priv; + identifier = _tpl_log_manager_build_identifier (account, entity); + if (_tpl_log_manager_is_disabled_for_entity (self, identifier)) + { + gint i, j; + const gchar **ignorelist = _tpl_conf_get_ignorelist (priv->conf); + gchar **newlist; + + if (!ignorelist) + return; + + newlist = g_malloc0_n (g_strv_length ((gchar **) ignorelist) - 1, sizeof (gchar*)); + j = 0; + for (i = 0; ignorelist && ignorelist[i]; i++) + { + if (g_strcmp0 (ignorelist[i], identifier) != 0) + { + newlist[j] = g_strdup (ignorelist[i]); + j++; + } + } + + _tpl_conf_set_ignorelist (priv->conf, (const gchar **) newlist); + g_strfreev (newlist); + } + + g_free (identifier); +} + +/** + * tpl_log_manager_is_disabled_for_entity: + * @self: the log manager + * @entity: a TplEntity + * + * Checks, whether logging is disabled for given entity. By default, logging + * is enabled for all entities. + * + * Returns: %TRUE if logging for the entity has been disabled, %FALSE otherwise. + */ +gboolean +tpl_log_manager_is_disabled_for_entity (TplLogManager *self, + TpAccount *account, + TplEntity *entity) +{ + gboolean is_disabled; + gchar *identifier; + + g_return_val_if_fail (TPL_IS_LOG_MANAGER (self), FALSE); + g_return_val_if_fail (TP_IS_ACCOUNT (account), FALSE); + g_return_val_if_fail (TPL_IS_ENTITY (entity), FALSE); + + identifier = _tpl_log_manager_build_identifier (account, entity); + is_disabled = _tpl_log_manager_is_disabled_for_entity (self, identifier); + g_free (identifier); + + return is_disabled; +} diff --git a/telepathy-logger/log-manager.h b/telepathy-logger/log-manager.h index bd5e610..df8c0bc 100644 --- a/telepathy-logger/log-manager.h +++ b/telepathy-logger/log-manager.h @@ -175,6 +175,18 @@ gboolean tpl_log_manager_search_finish (TplLogManager *self, GList **hits, GError **error); +void tpl_log_manager_disable_for_entity (TplLogManager *self, + TpAccount *account, + TplEntity *entity); + +void tpl_log_manager_enable_for_entity (TplLogManager *self, + TpAccount *account, + TplEntity *entity); + +gboolean tpl_log_manager_is_disabled_for_entity (TplLogManager *self, + TpAccount *account, + TplEntity *entity); + void tpl_log_manager_search_free (GList *hits); G_END_DECLS -- cgit v1.2.1