/soc/2015/igor.gajowiak/chatlog: d809626cdbb6: Implemented loggi...

Igor Gajowiak igor.gajowiak at gmail.com
Wed Jul 1 19:13:21 EDT 2015


Changeset: d809626cdbb64664cc934c8d72266ea9fff930b4
Author:	 Igor Gajowiak <igor.gajowiak at gmail.com>
Date:	 2015-07-02 01:11 +0200
Branch:	 default
URL: https://hg.pidgin.im/soc/2015/igor.gajowiak/chatlog/rev/d809626cdbb6

Description:

Implemented logging messages in a database.

diffstat:

 libpurple/conversation.c          |    2 +
 libpurple/core.c                  |    3 +
 libpurple/genericlog.c            |  217 +++++++++++++++++++++++-
 libpurple/genericlog.h            |   51 ++++-
 libpurple/plugins/log/Makefile.am |    8 +
 libpurple/plugins/log/logsqlite.c |  335 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 593 insertions(+), 23 deletions(-)

diffs (truncated from 776 to 300 lines):

diff --git a/libpurple/conversation.c b/libpurple/conversation.c
--- a/libpurple/conversation.c
+++ b/libpurple/conversation.c
@@ -28,6 +28,7 @@
 #include "dbus-maybe.h"
 #include "debug.h"
 #include "enums.h"
+#include "genericlog.h"
 #include "notify.h"
 #include "prefs.h"
 #include "protocol.h"
@@ -632,6 +633,7 @@ void
 			ops->write_conv(conv, pmsg);
 	}
 
+	purple_genericlog_logim(account, pmsg);
 	add_message_to_history(conv, pmsg);
 
 	purple_signal_emit(purple_conversations_get_handle(),
diff --git a/libpurple/core.c b/libpurple/core.c
--- a/libpurple/core.c
+++ b/libpurple/core.c
@@ -28,6 +28,7 @@
 #include "debug.h"
 #include "dnsquery.h"
 #include "xfer.h"
+#include "genericlog.h"
 #include "glibcompat.h"
 #include "http.h"
 #include "idle.h"
@@ -175,6 +176,7 @@ purple_core_init(const char *ui)
 	 */
 	purple_plugins_init();
 
+	purple_genericlog_init();
 	purple_keyring_init(); /* before accounts */
 	purple_theme_manager_init();
 
@@ -271,6 +273,7 @@ purple_core_quit(void)
 	purple_statuses_uninit();
 	purple_accounts_uninit();
 	purple_keyring_uninit(); /* after accounts */
+	purple_genericlog_uninit();
 	purple_sound_uninit();
 	purple_theme_manager_uninit();
 	purple_xfers_uninit();
diff --git a/libpurple/genericlog.c b/libpurple/genericlog.c
--- a/libpurple/genericlog.c
+++ b/libpurple/genericlog.c
@@ -19,6 +19,8 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
  */
 
+#include  <string.h>
+
 #include "genericlog.h"
 
 #define PURPLE_GENERICLOG_GET_PRIVATE(obj) \
@@ -41,23 +43,160 @@ enum
 static GObjectClass *parent_class;
 static GParamSpec *properties[PROP_LAST];
 
+static GList *purple_genericlogs = NULL;
+static PurpleGenericLog *purple_genericlog_inuse = NULL;
+static GList *purple_genericlog_loaded_plugins = NULL;
+
 /**************************************************************************/
-/* API implementation
+/* API implementation                                                     */
 /**************************************************************************/
 
+PurpleGenericLog*
+purple_genericlog_find_by_id(const gchar* id)
+{
+	GList *it;
+	for (it = purple_genericlogs; it != NULL; it = it->next) {
+		PurpleGenericLog *log = it->data;
+		const gchar *curr_id = purple_genericlog_get_id(log);
 
+		if (g_strcmp0(id, curr_id) == 0)
+			return log;
+	}
+	return NULL;
+}
+
+PurpleGenericLog*
+purple_genericlog_get_inuse()
+{
+	return purple_genericlog_inuse;
+}
+
+const gchar*
+purple_genericlog_get_name(const PurpleGenericLog* log)
+{
+	PurpleGenericLogPrivate *log_priv = PURPLE_GENERICLOG_GET_PRIVATE(log);
+	return log_priv->name;
+}
+
+const gchar*
+purple_genericlog_get_id(const PurpleGenericLog* log)
+{
+	PurpleGenericLogPrivate *p_log = PURPLE_GENERICLOG_GET_PRIVATE(log);
+	return p_log->id;
+}
+
+void
+purple_genericlog_register(PurpleGenericLog* log)
+{
+	const gchar *id = NULL;
+	id = purple_genericlog_get_id(log);
+
+	g_return_if_fail(id != NULL);
+
+	if(purple_genericlog_find_by_id(id) != NULL) {
+		purple_debug_info("genericlog", "logger is already registered");
+		return;
+	}
+
+	g_object_ref(log);
+	purple_genericlogs = g_list_prepend(purple_genericlogs, log);
+}
+
+void
+purple_genericlog_unregister(PurpleGenericLog* log)
+{
+	PurpleGenericLog *fallback;
+
+	g_return_if_fail(log != NULL);
+
+	GList* log_node = g_list_find(purple_genericlogs, log);
+
+	g_return_if_fail(log_node != NULL);
+
+	fallback = purple_genericlog_find_by_id(PURPLE_GENERICLOG_DEFAULT);
+
+	if (purple_genericlog_inuse == log) {
+		if (purple_genericlog_inuse != fallback) {
+			purple_genericlog_set_active_log(PURPLE_GENERICLOG_DEFAULT);
+		} else {
+			purple_debug_error("genericlog", "unable to unregister the default log");
+			return;
+		}
+	}
+
+	g_object_unref(log);
+	purple_genericlogs = g_list_delete_link(purple_genericlogs, log_node);
+}
+
+GError*
+purple_genericlog_set_active_log(const gchar* id)
+{
+	GError *error = NULL;
+	PurpleGenericLog *log = purple_genericlog_find_by_id(id);
+
+	if(!log) {
+		return g_error_new(purple_genericlog_error_domain(),
+			PURPLE_GENERICLOG_ERROR_NOLOG, "log does not exist");
+	}
+
+	if(purple_genericlog_inuse) {
+		if(strcmp(id, purple_genericlog_get_id(purple_genericlog_inuse)) == 0) {
+			purple_debug_info("genericlog", "log is already active");
+			return NULL;
+		}
+		else {
+			PurpleGenericLogClass *klass = PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);
+			error = klass->deactivate();
+			if(error)
+				return error;
+
+			g_object_unref(purple_genericlog_inuse);
+			purple_genericlog_inuse = NULL;
+		}
+	}
+
+	g_assert(purple_genericlog_inuse == NULL);
+
+	PurpleGenericLogClass *klass = PURPLE_GENERICLOG_GET_CLASS(log);
+	error = klass->activate();
+	// TODO: should we activate previous log if this fails?
+	if (error)
+		return error;
+
+	purple_genericlog_inuse = log;
+	g_object_ref(purple_genericlog_inuse);
+
+	return NULL;
+}
+
+GError*
+purple_genericlog_logim(PurpleAccount *account, PurpleMessage *msg)
+{
+	PurpleGenericLogClass *klass =
+		PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);
+	return klass->log_im(account, msg);
+}
+
+GError*
+purple_genericlog_mark_as_read(PurpleMessage* msg)
+{
+	PurpleGenericLogClass *klass =
+		PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);
+	return klass->mark_as_read(msg);
+}
 
 /**************************************************************************/
-/* Error Codes
+/* Error Codes                                                            */
 /**************************************************************************/
 
-GQuark purple_genericlog_error_domain(void)
+GQuark
+purple_genericlog_error_domain(void)
 {
 	return g_quark_from_static_string("libpurple generic log");
 }
 
 /**************************************************************************/
-/* GObject stuff
+/* GObject stuff                                                          */
 /**************************************************************************/
 
 static void
@@ -127,8 +266,9 @@ purple_genericlog_class_init(PurpleGener
 	 */
 	klass->log_im = NULL;
 	klass->mark_as_read = NULL;
-	klass->get_conversations = NULL;
 	klass->get_messages = NULL;
+	klass->activate = NULL;
+	klass->deactivate = NULL;
 
 	klass->reserved1 = NULL;
 	klass->reserved2 = NULL;
@@ -163,7 +303,7 @@ GType purple_genericlog_get_type(void)
 		static const GTypeInfo info = {
 			.class_size = sizeof(PurpleGenericLogClass),
 			.class_init = (GClassInitFunc)purple_genericlog_class_init,
-			.instance_size = sizeof(PurpleMessage),
+			.instance_size = sizeof(PurpleGenericLog),
 			.instance_init = purple_genericlog_instance_init,
 		};
 
@@ -174,23 +314,76 @@ GType purple_genericlog_get_type(void)
 }
 
 /**************************************************************************/
-/* Log Subsystem
+/* Log Subsystem                                                          */
 /**************************************************************************/
 
 void
-purple_logng_init(void)
+purple_genericlog_init(void)
 {
-	// TODO: implement this
+	GList *plugins, *it;
+
+	plugins = purple_plugins_find_all();
+	for (it = plugins; it != NULL; it = it->next) {
+		PurplePlugin *plugin = PURPLE_PLUGIN(it->data);
+		PurplePluginInfo *info = purple_plugin_get_info(plugin);
+
+		if (strncmp(purple_plugin_info_get_id(info), "genericlog-", 11) != 0)
+			continue;
+
+		if (purple_plugin_load(plugin, NULL)) {
+			g_object_ref(plugin);
+			purple_genericlog_loaded_plugins = g_list_prepend(
+				purple_genericlog_loaded_plugins, plugin);
+		}
+	}
+	g_list_free(plugins);
+
+	GError *error = purple_genericlog_set_active_log(PURPLE_GENERICLOG_DEFAULT);
+
+	if(error) {
+		purple_debug_error("genericlog", "setting active log failed");
+		g_error_free(error);
+	}
 }
 
 void
-purple_logng_uninit(void)
+purple_genericlog_uninit(void)
 {
-	// TODO: implement this
+	GList *it;
+
+	if (purple_genericlog_inuse) {
+		PurpleGenericLogClass *klass =
+			PURPLE_GENERICLOG_GET_CLASS(purple_genericlog_inuse);
+		klass->deactivate();
+
+		g_object_unref(purple_genericlog_inuse);
+		purple_genericlog_inuse = NULL;
+	}
+
+	for (it = g_list_first(purple_genericlogs); it != NULL;
+		it = g_list_next(it))



More information about the Commits mailing list