diff options
Diffstat (limited to 'telepathy-logger')
-rw-r--r-- | telepathy-logger/log-manager.c | 127 | ||||
-rw-r--r-- | telepathy-logger/log-manager.h | 3 | ||||
-rw-r--r-- | telepathy-logger/log-store-empathy.c | 12 | ||||
-rw-r--r-- | telepathy-logger/log-store.c | 36 | ||||
-rw-r--r-- | telepathy-logger/log-store.h | 8 |
5 files changed, 113 insertions, 73 deletions
diff --git a/telepathy-logger/log-manager.c b/telepathy-logger/log-manager.c index c7d38a1..f71e0e5 100644 --- a/telepathy-logger/log-manager.c +++ b/telepathy-logger/log-manager.c @@ -48,6 +48,8 @@ typedef struct { GList *stores; + GList *writable_stores; + GList *readable_stores; } TplLogManagerPriv; @@ -92,6 +94,9 @@ log_manager_finalize (GObject *object) g_list_foreach (priv->stores, (GFunc) g_object_unref, NULL); g_list_free (priv->stores); + /* no unref needed here, the only reference kept is in priv->stores */ + g_list_free (priv->writable_stores); + g_list_free (priv->readable_stores); G_OBJECT_CLASS (tpl_log_manager_parent_class)->finalize (object); } @@ -105,9 +110,10 @@ static GObject * log_manager_constructor (GType type, guint n_props, GObjectConstructParam *props) { - GObject *retval; + GObject *retval = NULL; TplLogManagerPriv *priv; - TplLogStoreEmpathy *tplogger; + TplLogStoreEmpathy *tplogger = NULL; + TplLogStoreEmpathy *empathy = NULL; if (manager_singleton) retval = g_object_ref (manager_singleton); @@ -115,28 +121,49 @@ log_manager_constructor (GType type, guint n_props, { retval = G_OBJECT_CLASS (tpl_log_manager_parent_class)->constructor (type, n_props, props); + if (retval == NULL) + return NULL; manager_singleton = TPL_LOG_MANAGER (retval); g_object_add_weak_pointer (retval, (gpointer *) & manager_singleton); priv = GET_PRIV (manager_singleton); - /* TODO currently I instantiate two LogStore, one read-only, and the - * default one, TpLogger, which will be used by add_message to store - * (write_access) new entries. - * NEXT step: use a LogStore map here, and instantiate LogStores - * using it. LogStore map should map names into (TPL_TYPE_LOG_STORE_FOO, - * is_writable, is_readable) to be passed to g_object_new cycling over - * its keys */ - tplogger = g_object_new (TPL_TYPE_LOG_STORE_EMPATHY, "name", "TpLogger", - "writable", FALSE, "readable", TRUE, NULL); + /* The TPL's default read-write logstore */ + tplogger = g_object_new (TPL_TYPE_LOG_STORE_EMPATHY, + "name", TPL_LOG_MANAGER_LOG_STORE_DEFAULT, + "writable", TRUE, + "readable", TRUE, + NULL); if (tplogger == NULL) + g_critical ("Error during TplLogStoreEmpathy (name=TpLogger) initialisation."); + else { - DEBUG ("Error during TplLogStoreEmpathy (name=TpLogger) initialisation."); - return NULL; + /* manual registration, to set up priv->stores for + * tpl_log_manager_register_log_store */ + priv->stores = g_list_prepend (priv->stores, tplogger); + priv->writable_stores = g_list_prepend (priv->writable_stores, + tplogger); + priv->readable_stores = g_list_prepend (priv->readable_stores, + tplogger); } - priv->stores = g_list_append (priv->stores, tplogger); + /* Load by default the Empathy's legacy 'past coversations' LogStore */ + empathy = g_object_new (TPL_TYPE_LOG_STORE_EMPATHY, + "name", "Empathy", + "writable", FALSE, + "readable", TRUE, + NULL); + if (empathy == NULL) + g_critical ("Error during TplLogStoreEmpathy (name=Empathy) initialisation."); + else if (!tpl_log_manager_register_log_store (manager_singleton, + TPL_LOG_STORE (empathy))) + g_critical ("Not able to register the TplLogStore with name=Emapathy. " + "Empathy's legacy logs won't be available."); + + /* internally referenced within register_logstore */ + if (empathy != NULL) + g_object_unref (empathy); } return retval; @@ -192,39 +219,26 @@ tpl_log_manager_add_message (TplLogManager *manager, { TplLogManagerPriv *priv; GList *l; - gboolean out = FALSE; - gboolean found = FALSE; - - /* TODO: currently it look for a fixed string (add_store) to know there to - * send messages. - * NEXT step: it will cycle priv->stores and check which has is_writable flag on, and send the log - * to every entry with it TRUE. Multiple writers are possible here */ - const gchar *add_store = "TpLogger"; + gboolean retval = FALSE; g_return_val_if_fail (TPL_IS_LOG_MANAGER (manager), FALSE); g_return_val_if_fail (TPL_IS_LOG_ENTRY (message), FALSE); priv = GET_PRIV (manager); - /* TODO find a way to select just one LogStore, ie just the default one, or - * just "zeitgeist" in case an application is only interested in a specific - * store */ - for (l = priv->stores; l != NULL; l = g_list_next (l)) + /* send the message to any writable log store */ + for (l = priv->writable_stores; l != NULL; l = g_list_next (l)) { TplLogStore *store = l->data; + gboolean result; - if (!tp_strdiff (tpl_log_store_get_name (store), add_store)) - { - out = tpl_log_store_add_message (store, message, error); - found = TRUE; - break; - } + result = tpl_log_store_add_message (store, message, error); + /* TRUE if at least one LogStore succeeds */ + retval = result || retval; } - - if (!found) - DEBUG ("Failed to find chosen log store to write to."); - - return out; + if (!retval) + g_critical ("Failed to write to at least writable LogStore."); + return retval; } @@ -242,17 +256,44 @@ tpl_log_manager_add_message (TplLogManager *manager, * @logstore has to properly implement all the search/query methods if the * #TplLogStore:readable is set to %TRUE. */ -void +gboolean tpl_log_manager_register_log_store (TplLogManager *self, TplLogStore *logstore) { TplLogManagerPriv *priv = GET_PRIV (self); + GList *l; + gboolean found = FALSE; + + g_return_val_if_fail (TPL_IS_LOG_MANAGER (self), FALSE); + g_return_val_if_fail (TPL_IS_LOG_STORE (logstore), FALSE); + g_return_val_if_fail (priv->stores != NULL, FALSE); + /* for consistency, at least the default log store is RW */ + g_return_val_if_fail (priv->writable_stores != NULL, FALSE); + g_return_val_if_fail (priv->readable_stores != NULL, FALSE); - g_return_if_fail (TPL_IS_LOG_MANAGER (self)); - g_return_if_fail (TPL_IS_LOG_STORE (logstore)); - g_return_if_fail (priv->stores != NULL); + /* check that the logstore name is not already used */ + for (l = priv->stores; l != NULL; l = g_list_next (l)) + { + TplLogStore *store = l->data; + const gchar *name = tpl_log_store_get_name (logstore); - priv->stores = g_list_append (priv->stores, logstore); + if (!tp_strdiff (name, tpl_log_store_get_name (store))) + found = TRUE; + } + if (found) + return FALSE; + + if (tpl_log_store_is_readable (logstore)) + priv->readable_stores = g_list_prepend (priv->readable_stores, logstore); + + if (tpl_log_store_is_writable (logstore)) + priv->writable_stores = g_list_prepend (priv->writable_stores, logstore); + + /* reference just once, writable/readable lists are kept in sync with the + * general list and never written separatedly */ + priv->stores = g_list_prepend (priv->stores, g_object_ref (logstore)); + + return TRUE; } @@ -1009,7 +1050,7 @@ _search_in_identifier_chats_new_thread (GSimpleAsyncResult *simple, lst = tpl_log_manager_search_in_identifier_chats_new (async_data->manager, chat_info->account, chat_info->chat_id, chat_info->search_text); - // TODO add destructor + /* TODO add destructor */ g_simple_async_result_set_op_res_gpointer (simple, lst, NULL); } diff --git a/telepathy-logger/log-manager.h b/telepathy-logger/log-manager.h index 013751c..e7d2b24 100644 --- a/telepathy-logger/log-manager.h +++ b/telepathy-logger/log-manager.h @@ -40,6 +40,7 @@ G_BEGIN_DECLS #define TPL_LOG_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), TPL_TYPE_LOG_MANAGER, TplLogManagerClass)) #define TPL_LOG_MANAGER_ERROR g_quark_from_static_string ("tpl-log-manager-error-quark") +#define TPL_LOG_MANAGER_LOG_STORE_DEFAULT "TpLogger" typedef enum { @@ -132,7 +133,7 @@ void tpl_log_manager_search_hit_free (TplLogSearchHit *hit); gint tpl_log_manager_search_hit_compare (TplLogSearchHit *a, TplLogSearchHit *b); -void tpl_log_manager_register_log_store (TplLogManager *self, +gboolean tpl_log_manager_register_log_store (TplLogManager *self, TplLogStore *logstore); G_END_DECLS diff --git a/telepathy-logger/log-store-empathy.c b/telepathy-logger/log-store-empathy.c index 801be9e..faf47d9 100644 --- a/telepathy-logger/log-store-empathy.c +++ b/telepathy-logger/log-store-empathy.c @@ -156,10 +156,10 @@ tpl_log_store_get_property (GObject *object, g_value_set_string (value, priv->name); break; case PROP_WRITABLE: - /* ignore */ + g_value_set_boolean (value, priv->writable); break; case PROP_READABLE: - /* ignore */ + g_value_set_boolean (value, priv->readable); break; case PROP_BASEDIR: g_value_set_string (value, priv->basedir); @@ -185,10 +185,10 @@ tpl_log_store_set_property (GObject *object, log_store_empathy_set_name (self, g_value_get_string (value)); break; case PROP_READABLE: - /* ignore */ + log_store_empathy_set_readable (self, g_value_get_boolean (value)); break; case PROP_WRITABLE: - /* ignore */ + log_store_empathy_set_writable (self, g_value_get_boolean (value)); break; case PROP_BASEDIR: log_store_empathy_set_basedir (self, g_value_get_string (value)); @@ -248,6 +248,7 @@ tpl_log_store_empathy_class_init (TplLogStoreEmpathyClass *klass) * * Wether the log store is readable. * Default: %TRUE + * * As defined in #TplLogStore. */ param_spec = g_param_spec_boolean ("readable", @@ -1280,10 +1281,7 @@ log_store_iface_init (gpointer g_iface, iface->search_in_identifier_chats_new = log_store_empathy_search_in_identifier_chats_new; iface->search_new = log_store_empathy_search_new; - iface->ack_message = NULL; iface->get_filtered_messages = log_store_empathy_get_filtered_messages; - iface->set_writable = log_store_empathy_set_writable; - iface->set_readable = log_store_empathy_set_readable; iface->is_writable = log_store_empathy_is_writable; iface->is_readable = log_store_empathy_is_readable; } diff --git a/telepathy-logger/log-store.c b/telepathy-logger/log-store.c index 1255636..255ded2 100644 --- a/telepathy-logger/log-store.c +++ b/telepathy-logger/log-store.c @@ -204,20 +204,6 @@ tpl_log_store_search_new (TplLogStore *self, } -void -tpl_log_store_ack_message (TplLogStore *self, - const gchar *chat_id, - gboolean chatroom, - TplLogEntry *message) -{ - if (!TPL_LOG_STORE_GET_INTERFACE (self)->ack_message) - return; - - TPL_LOG_STORE_GET_INTERFACE (self)->ack_message (self, chat_id, chatroom, - message); -} - - GList * tpl_log_store_get_filtered_messages (TplLogStore *self, TpAccount *account, @@ -233,3 +219,25 @@ tpl_log_store_get_filtered_messages (TplLogStore *self, return TPL_LOG_STORE_GET_INTERFACE (self)->get_filtered_messages (self, account, chat_id, chatroom, num_messages, filter, user_data); } + + +gboolean +tpl_log_store_is_writable (TplLogStore *self) +{ + if (!TPL_LOG_STORE_GET_INTERFACE (self)->is_writable) + return FALSE; + + return TPL_LOG_STORE_GET_INTERFACE (self)->is_writable (self); +} + + +gboolean +tpl_log_store_is_readable (TplLogStore *self) +{ + if (!TPL_LOG_STORE_GET_INTERFACE (self)->is_readable) + return FALSE; + + return TPL_LOG_STORE_GET_INTERFACE (self)->is_readable (self); +} + + diff --git a/telepathy-logger/log-store.h b/telepathy-logger/log-store.h index 5141318..53ba2d0 100644 --- a/telepathy-logger/log-store.h +++ b/telepathy-logger/log-store.h @@ -47,9 +47,7 @@ typedef struct { GTypeInterface parent; - void (*set_writable) (TplLogStore *self, gboolean data); gboolean (*is_writable) (TplLogStore *self); - void (*set_readable) (TplLogStore *self, gboolean data); gboolean (*is_readable) (TplLogStore *self); const gchar * (*get_name) (TplLogStore *self); @@ -67,8 +65,6 @@ typedef struct GList * (*search_new) (TplLogStore *self, const gchar *text); GList * (*search_in_identifier_chats_new) (TplLogStore *self, TpAccount *account, gchar const *identifier, const gchar *text); - void (*ack_message) (TplLogStore *self, const gchar *chat_id, - gboolean chatroom, TplLogEntry *message); GList * (*get_filtered_messages) (TplLogStore *self, TpAccount *account, const gchar *chat_id, gboolean chatroom, guint num_messages, TplLogMessageFilter filter, gpointer user_data); @@ -92,14 +88,10 @@ GList *tpl_log_store_get_chats (TplLogStore *self, TpAccount *account); GList *tpl_log_store_search_in_identifier_chats_new (TplLogStore *self, TpAccount *account, gchar const *identifier, const gchar *text); GList *tpl_log_store_search_new (TplLogStore *self, const gchar *text); -void tpl_log_store_ack_message (TplLogStore *self, - const gchar *chat_id, gboolean chatroom, TplLogEntry *message); GList *tpl_log_store_get_filtered_messages (TplLogStore *self, TpAccount *account, const gchar *chat_id, gboolean chatroom, guint num_messages, TplLogMessageFilter filter, gpointer user_data); -void tpl_log_store_set_writable (TplLogStore *self, gboolean data); gboolean tpl_log_store_is_writable (TplLogStore *self); -void tpl_log_store_set_readable (TplLogStore *self, gboolean data); gboolean tpl_log_store_is_readable (TplLogStore *self); |