cpw.nader.asynclogging-3: 40ef73f8: Made purple_common_log_read_finish retur...

morshed.nader at gmail.com morshed.nader at gmail.com
Tue Jan 3 05:15:26 EST 2012


----------------------------------------------------------------------
Revision: 40ef73f875299a8b9391b33f227ea97de3e86b89
Parent:   2e2174601c1907619e975758861eb6fc7d6cfb57
Author:   morshed.nader at gmail.com
Date:     01/03/12 05:14:18
Branch:   im.pidgin.cpw.nader.asynclogging-3
URL: http://d.pidgin.im/viewmtn/revision/info/40ef73f875299a8b9391b33f227ea97de3e86b89

Changelog: 

Made purple_common_log_read_finish return a newly allocated string, instead of one that would be freed later
Made PurpleTxtLogger have async methods (Need to implement write still, though)

Changes against parent 2e2174601c1907619e975758861eb6fc7d6cfb57

  patched  libpurple/commonlog.c
  patched  libpurple/htmllog.c
  patched  libpurple/txtlog.c

-------------- next part --------------
============================================================
--- libpurple/htmllog.c	9d5cc6b1e4979df7b05aa14f88ad1e487e78426a
+++ libpurple/htmllog.c	5cf4776cc696e07f01d72ae7739e471e9b517653
@@ -75,7 +75,6 @@ typedef struct {
 } purple_html_write_callback_data;
 
 typedef struct {
-	PurpleLogReadFlags *flags;
 	GAsyncReadyCallback cb;
 	gpointer userdata;
 } read_callback_data;
@@ -677,8 +676,7 @@ purple_html_log_read(PurpleLog *log, Pur
 {
 	gchar *read, *minus_header;
 
-	read = purple_common_log_read(log, flags, cancellable,
-		error);
+	read = purple_common_log_read(log, flags, cancellable, error);
 
 	if (read == NULL)
 		return NULL;
@@ -753,7 +751,6 @@ purple_html_log_read_async_2(GObject *ob
 			_read_res_callback_data_free));
 }
 
-
 static gssize
 purple_html_log_total_size(PurpleLog *log, PurpleLogChatType type,
 	const gchar *name, PurpleAccount *account, GCancellable *cancellable,
@@ -809,6 +806,7 @@ purple_html_log_finalize(GObject *object
 static void
 purple_html_log_finalize(GObject *object)
 {
+	/* XXX: Conditionally do this? */
 	write_footer(PURPLE_COMMON_LOG(object), "</body></html>\n");
 	G_OBJECT_CLASS(purple_html_log_parent_class)->finalize(object);
 }
============================================================
--- libpurple/txtlog.c	636b875d61ef75f5dc7b96711860cfb9f31ddac6
+++ libpurple/txtlog.c	00e7d09eb6d7103ecb27638b3554f1faf1651ed8
@@ -27,40 +27,49 @@
 #include "internal.h"
 #include "txtlog.h"
 
-G_DEFINE_TYPE (PurpleTxtLog, purple_txt_log, PURPLE_TYPE_COMMON_LOG)
 
-static gboolean purple_txt_log_create(PurpleLog *, GCancellable *, GError **);
-static gssize purple_txt_log_write(PurpleLog *, PurpleMessageFlags,
-	const gchar *, time_t, const gchar *, GCancellable *, GError **);
-static GList *purple_txt_log_list(PurpleLog *, PurpleLogChatType, const gchar *,
-	PurpleAccount *, GCancellable *, GError **);
-static gchar *purple_txt_log_read(PurpleLog *, PurpleLogReadFlags *,
-	GCancellable *, GError **);
-static gssize purple_txt_log_total_size(PurpleLog *, PurpleLogChatType,
-	const gchar *, PurpleAccount *, GCancellable *, GError **);
-static GList *purple_txt_log_list_syslog(PurpleLog *, PurpleAccount *,
-	GCancellable *, GError **);
+/* Helpful macros */
+#define SIMPLE_ASYNC_RESULT_NEW_GOOD(obj, cb, data, source, cmd) \
+	{ \
+		GSimpleAsyncResult *simple; \
+		simple = g_simple_async_result_new(G_OBJECT(obj), cb, data, source); \
+		cmd; \
+		g_simple_async_result_complete_in_idle(simple); \
+		g_object_unref(simple); \
+	}
 
-static void
-purple_txt_log_class_init(PurpleTxtLogClass *class)
-{
-	PurpleLogClass *log_class = PURPLE_LOG_CLASS(class);
+#define SIMPLE_ASYNC_RESULT_FROM_ERROR(obj, cb, data, err) \
+	{ \
+		GSimpleAsyncResult *simple; \
+		simple = g_simple_async_result_new_from_error(G_OBJECT(obj), cb, \
+			data, err); \
+		g_clear_error(&err); \
+		g_simple_async_result_complete_in_idle(simple); \
+		g_object_unref(simple); \
+	}
 
-	log_class->logger_name = _("TXT");
-	log_class->logger_id = "txt";
+#define SIMPLE_ASYNC_RESULT_FROM_NEW_ERROR(obj, cb, data, domain, code, msg) \
+	{ \
+		GSimpleAsyncResult *simple; \
+		simple = g_simple_async_result_new_error(G_OBJECT(obj), cb, data, \
+			domain, code, msg); \
+		g_simple_async_result_complete_in_idle(simple); \
+		g_object_unref(simple); \
+	}
 
-	log_class->write_fn = purple_txt_log_write;
-	log_class->list_fn = purple_txt_log_list;
-	log_class->read_fn = purple_txt_log_read;
-	log_class->total_size_fn = purple_txt_log_total_size;
-	log_class->list_syslog_fn = purple_txt_log_list_syslog;
-}
 
-static void
-purple_txt_log_init(PurpleTxtLog *txt_log)
-{
-}
+/* Local structures */
+typedef struct {
+	GAsyncReadyCallback cb;
+	gpointer userdata;
+} read_callback_data;
 
+
+/* Function prototypes */
+G_DEFINE_TYPE (PurpleTxtLog, purple_txt_log, PURPLE_TYPE_COMMON_LOG)
+
+static void purple_txt_log_read_async_2(GObject *, GAsyncResult *, gpointer);
+
 static gboolean
 purple_txt_log_create(PurpleLog *log, GCancellable *cancellable, GError **error)
 {
@@ -214,6 +223,15 @@ purple_txt_log_list(PurpleLog *log, Purp
 		PURPLE_TYPE_TXT_LOG, cancellable, error);
 }
 
+static void
+purple_txt_log_list_async(PurpleLog *log, PurpleLogChatType type,
+	const gchar *sn, PurpleAccount *account, gint io_priority,
+	GCancellable *cancellable, GAsyncReadyCallback cb, gpointer userdata)
+{
+	purple_common_log_list_async(type, sn, account, ".txt",
+		PURPLE_TYPE_TXT_LOG, io_priority, cancellable, cb, userdata);
+}
+
 static GList *
 purple_txt_log_list_syslog(PurpleLog *log, PurpleAccount *account,
 	GCancellable *cancellable, GError **error)
@@ -222,37 +240,26 @@ purple_txt_log_list_syslog(PurpleLog *lo
 		".txt", PURPLE_TYPE_TXT_LOG, cancellable, error);
 }
 
+static void
+purple_txt_log_list_syslog_async(PurpleLog *log, PurpleAccount *account,
+	gint io_priority, GCancellable *cancellable, GAsyncReadyCallback cb,
+	gpointer userdata)
+{
+	purple_common_log_list_async(PURPLE_LOG_SYSTEM, ".system", account,
+		".txt", PURPLE_TYPE_TXT_LOG, io_priority, cancellable, cb, userdata);
+}
+
 static gchar *
 purple_txt_log_read(PurpleLog *log, PurpleLogReadFlags *flags,
 	GCancellable *cancellable, GError **error)
 {
-	PurpleCommonLog *common_log = PURPLE_COMMON_LOG(log);
-	GFile *file;
-	const gchar *path;
 	gchar *read, *minus_header;
-	gboolean result;
 
-	if (flags != NULL)
-		*flags = 0;
+	read = purple_common_log_read(log, flags, cancellable, error);
 
-	path = purple_common_log_get_path(common_log);
-
-	if (path == NULL) {
-		g_set_error_literal(error,
-			G_FILE_ERROR,
-			G_IO_ERROR_NOT_FOUND,
-			_("Unable to find log path"));
-
+	if (read == NULL)
 		return NULL;
-	}
 
-	file = g_file_new_for_path(path);
-	result = g_file_load_contents(file, cancellable, &read, NULL, NULL, error);
-	g_object_unref(file);
-
-	if (!result)
-		return NULL;
-
 	purple_str_strip_char(read, '\r');
 	minus_header = strchr(read, '\n');
 
@@ -262,6 +269,59 @@ purple_txt_log_read(PurpleLog *log, Purp
 		return _process_txt_log(read, NULL);
 }
 
+static void
+purple_txt_log_read_async(PurpleLog *log, gint io_priority,
+	GCancellable *cancellable, GAsyncReadyCallback cb, gpointer userdata)
+{
+	read_callback_data *data;
+
+	data = g_new(read_callback_data, 1);
+	data->cb = cb;
+	data->userdata = userdata;
+
+	purple_common_log_read_async(log, io_priority,
+		cancellable, purple_txt_log_read_async_2, data);
+}
+
+static void
+purple_txt_log_read_async_2(GObject *obj, GAsyncResult *res, gpointer userdata)
+{
+	PurpleLog *log = PURPLE_LOG(obj);
+	GError *err = NULL;
+	gchar *read, *minus_header;
+	PurpleLogReadFlags flags;
+	read_callback_data *data = userdata;
+	_read_res_callback_data *res_data;
+
+	read = purple_common_log_read_finish(log, res, &flags, &err);
+
+	if (read == NULL) {
+		SIMPLE_ASYNC_RESULT_FROM_ERROR(log, data->cb, data->userdata, err);
+		return;
+	}
+
+	flags |= PURPLE_LOG_READ_NO_NEWLINE;
+	minus_header = strchr(read, '\n');
+
+	if (minus_header == NULL)
+		minus_header = read;
+	else {
+		minus_header = g_strdup(minus_header + 1);
+		g_free(read);
+	}
+
+	purple_str_strip_char(minus_header, '\r');
+
+	res_data = g_new(_read_res_callback_data, 1);
+	res_data->flags = flags;
+	res_data->text = minus_header;
+
+	SIMPLE_ASYNC_RESULT_NEW_GOOD(log, data->cb, data->userdata,
+		purple_txt_log_read_async,
+		g_simple_async_result_set_op_res_gpointer(simple, res_data,
+			_read_res_callback_data_free));
+}
+
 static gssize
 purple_txt_log_total_size(PurpleLog *log, PurpleLogChatType type,
 	const gchar *name, PurpleAccount *account, GCancellable *cancellable,
@@ -271,6 +331,39 @@ purple_txt_log_total_size(PurpleLog *log
 		cancellable, error);
 }
 
+static void
+purple_txt_log_total_size_async(PurpleLog *log, PurpleLogChatType type,
+	const gchar *name, PurpleAccount *account, gint io_priority,
+	GCancellable *cancellable, GAsyncReadyCallback cb, gpointer userdata)
+{
+	purple_common_log_total_size_async(type, name, account, ".txt",
+		io_priority, cancellable, cb, userdata);
+}
+
+static void
+purple_txt_log_class_init(PurpleTxtLogClass *class)
+{
+	PurpleLogClass *log_class = PURPLE_LOG_CLASS(class);
+
+	log_class->logger_name = _("TXT");
+	log_class->logger_id = "txt";
+
+	log_class->write_fn = purple_txt_log_write;
+	log_class->list_fn = purple_txt_log_list;
+	log_class->list_async = purple_txt_log_list_async;
+	log_class->read_fn = purple_txt_log_read;
+	log_class->read_async = purple_txt_log_read_async;
+	log_class->total_size_fn = purple_txt_log_total_size;
+	log_class->total_size_async = purple_txt_log_total_size_async;
+	log_class->list_syslog_fn = purple_txt_log_list_syslog;
+	log_class->list_syslog_async = purple_txt_log_list_syslog_async;
+}
+
+static void
+purple_txt_log_init(PurpleTxtLog *txt_log)
+{
+}
+
 void
 purple_txt_log_system_init(void)
 {
============================================================
--- libpurple/commonlog.c	d10fbd0b2483725a7f9e683ccb166ae46d65b88f
+++ libpurple/commonlog.c	f21d99ffe9bfdf92611b0d6f95618b0cba7754a8
@@ -822,7 +822,7 @@ purple_common_log_read_finish(PurpleLog 
 	if (flags != NULL)
 		*flags = res_data->flags;
 
-	return res_data->text;
+	return g_strdup(res_data->text);
 }
 
 /* XXX: Poorly named */


More information about the Commits mailing list