cpw.nader.asynclogging-3: e967351b: Updated PurpleHtmlLog to have async read...

morshed.nader at gmail.com morshed.nader at gmail.com
Tue Jan 3 05:00:49 EST 2012


----------------------------------------------------------------------
Revision: e967351b0a13202f1ba310d524d8d01cadf5ac63
Parent:   ad978b8618d61e2fead0896972e416b334d1c2f2
Author:   morshed.nader at gmail.com
Date:     01/03/12 04:50:35
Branch:   im.pidgin.cpw.nader.asynclogging-3
URL: http://d.pidgin.im/viewmtn/revision/info/e967351b0a13202f1ba310d524d8d01cadf5ac63

Changelog: 

Updated PurpleHtmlLog to have async reads

Changes against parent ad978b8618d61e2fead0896972e416b334d1c2f2

  patched  libpurple/htmllog.c

-------------- next part --------------
============================================================
--- libpurple/htmllog.c	32d1e251fdee18e770c2fffe9bf53a8693773431
+++ libpurple/htmllog.c	9d5cc6b1e4979df7b05aa14f88ad1e487e78426a
@@ -28,6 +28,37 @@
 #include "debug.h"
 #include "htmllog.h"
 
+
+/* 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); \
+	}
+
+#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); \
+	}
+
+#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); \
+	}
+
+
 /* Local structures */
 typedef struct {
 	PurpleLog *log;
@@ -41,37 +72,46 @@ typedef struct {
 	gpointer userdata;
 	gboolean write_header;
 	GOutputStream *out_stream;
-} _purple_html_write_callback_data;
+} purple_html_write_callback_data;
 
+typedef struct {
+	PurpleLogReadFlags *flags;
+	GAsyncReadyCallback cb;
+	gpointer userdata;
+} read_callback_data;
+
+
 /* Function prototypes */
+static void purple_html_log_read_async_2(GObject *, GAsyncResult *, gpointer);
 static void purple_html_log_write_async_2(PurpleLog *, GAsyncResult *,
-	_purple_html_write_callback_data *);
+	purple_html_write_callback_data *);
 static void purple_html_log_write_async_3(GFile *, GAsyncResult *,
-	_purple_html_write_callback_data *);
+	purple_html_write_callback_data *);
 
 G_DEFINE_TYPE (PurpleHtmlLog, purple_html_log, PURPLE_TYPE_COMMON_LOG)
 
+
 /* XXX: Move the extension in CommonLog somehow to reduce duplicate code here/txt
  * and make things more configurable for future loggers? */
 static gboolean
 purple_html_log_create(PurpleLog *log, GCancellable *cancellable,
 	GError **error)
 {
-	return purple_log_common_create(log, ".html", cancellable, error);
+	return purple_common_log_create(log, ".html", cancellable, error);
 }
 
 static void
 purple_html_log_create_async(PurpleLog *log, gint io_priority,
 	GCancellable *cancellable, GAsyncReadyCallback cb, gpointer userdata)
 {
-	purple_log_common_create_async(log, ".html", io_priority, cancellable, cb,
+	purple_common_log_create_async(log, ".html", io_priority, cancellable, cb,
 		userdata);
 }
 
 static gboolean
 purple_html_log_create_finish(PurpleLog *log, GAsyncResult *res, GError **error)
 {
-	return purple_log_common_create_finish(log, res, error);
+	return purple_common_log_create_finish(log, res, error);
 }
 
 /* NOTE: This can return msg (which you may or may not want to g_free())
@@ -339,7 +379,7 @@ static void
 }
 
 static void
-purple_html_write_callback_data_free(_purple_html_write_callback_data *data)
+purple_html_write_callback_data_free(purple_html_write_callback_data *data)
 {
 	g_object_unref(log);
 	g_free(data->from);
@@ -360,10 +400,10 @@ purple_html_log_write_async(PurpleLog *l
 	const gchar *from, time_t time, const gchar *message, gint io_priority,
 	GCancellable *cancellable, GAsyncReadyCallback cb, gpointer userdata)
 {
-	_purple_html_write_callback_data *callback_data;
+	purple_html_write_callback_data *callback_data;
 
 	/* Set up the callback data and create the file if needed */
-	callback_data = g_new(_purple_html_write_callback_data, 1);
+	callback_data = g_new(purple_html_write_callback_data, 1);
 	callback_data->log = g_object_ref(log);
 	callback_data->type = type;
 	callback_data->from = g_strdup(from);
@@ -391,7 +431,7 @@ purple_html_log_write_async_2(PurpleLog 
 /* Open the file up for writing */
 static void
 purple_html_log_write_async_2(PurpleLog *log, GAsyncResult *res,
-	_purple_html_write_callback_data *data)
+	purple_html_write_callback_data *data)
 {
 	GFile *file;
 	GSimpleAsyncResult *simple;
@@ -441,7 +481,7 @@ purple_html_log_write_async_3(GFile *fil
 /* Create the header and write it to the file */
 static void
 purple_html_log_write_async_3(GFile *file, GAsyncResult *res,
-	_purple_html_write_callback_data *data)
+	purple_html_write_callback_data *data)
 {
 	PurpleLog *log = data->log;
 	GError *error;
@@ -601,7 +641,7 @@ purple_html_log_list(PurpleLog *log, Pur
 purple_html_log_list(PurpleLog *log, PurpleLogChatType type, const gchar *sn,
 	PurpleAccount *account, GCancellable *cancellable, GError **error)
 {
-	return purple_log_common_list(type, sn, account, ".html",
+	return purple_common_log_list(type, sn, account, ".html",
 		PURPLE_TYPE_HTML_LOG, cancellable, error);
 }
 
@@ -610,7 +650,7 @@ purple_html_log_list_async(PurpleLog *lo
 	const gchar *sn, PurpleAccount *account, gint io_priority,
 	GCancellable *cancellable, GAsyncReadyCallback cb, gpointer userdata)
 {
-	purple_log_common_list_async(type, sn, account, ".html",
+	purple_common_log_list_async(type, sn, account, ".html",
 		PURPLE_TYPE_HTML_LOG, io_priority, cancellable, cb, userdata);
 }
 
@@ -618,7 +658,7 @@ purple_html_log_list_syslog(PurpleLog *l
 purple_html_log_list_syslog(PurpleLog *log, PurpleAccount *account,
 	GCancellable *cancellable, GError **error)
 {
-	return purple_log_common_list(PURPLE_LOG_SYSTEM, ".system", account,
+	return purple_common_log_list(PURPLE_LOG_SYSTEM, ".system", account,
 		".html", PURPLE_TYPE_HTML_LOG, cancellable, error);
 }
 
@@ -627,7 +667,7 @@ purple_html_log_list_syslog_async(Purple
 	gint io_priority, GCancellable *cancellable, GAsyncReadyCallback cb,
 	gpointer userdata)
 {
-	purple_log_common_list_async(PURPLE_LOG_SYSTEM, ".system", account,
+	purple_common_log_list_async(PURPLE_LOG_SYSTEM, ".system", account,
 		".html", PURPLE_TYPE_HTML_LOG, io_priority, cancellable, cb, userdata);
 }
 
@@ -635,32 +675,17 @@ purple_html_log_read(PurpleLog *log, Pur
 purple_html_log_read(PurpleLog *log, PurpleLogReadFlags *flags,
 	GCancellable *cancellable, GError **error)
 {
-	GFile *file;
-	const gchar *path;
 	gchar *read, *minus_header;
-	gboolean result;
 
-	if (flags != NULL)
-		*flags = PURPLE_LOG_READ_NO_NEWLINE;
+	read = purple_common_log_read(log, flags, cancellable,
+		error);
 
-	path = purple_common_log_get_path(PURPLE_COMMON_LOG(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 (flags != NULL)
+		*flags |= PURPLE_LOG_READ_NO_NEWLINE;
 
-	if (!result)
-		return NULL;
-
 	minus_header = strchr(read, '\n');
 
 	if (minus_header == NULL)
@@ -679,16 +704,62 @@ purple_html_log_read_async(PurpleLog *lo
 purple_html_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_html_log_read_async_2, data);
 }
 
+static void
+purple_html_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_html_log_read_async,
+		g_simple_async_result_set_op_res_gpointer(simple, res_data,
+			_read_res_callback_data_free));
+}
+
+
 static gssize
 purple_html_log_total_size(PurpleLog *log, PurpleLogChatType type,
 	const gchar *name, PurpleAccount *account, GCancellable *cancellable,
 	GError **error)
 {
-	return purple_log_common_total_size(type, name, account, ".html",
+	return purple_common_log_total_size(type, name, account, ".html",
 		cancellable, error);
 }
 
@@ -697,7 +768,7 @@ purple_html_log_total_size_async(PurpleL
 	const gchar *name, PurpleAccount *account, gint io_priority,
 	GCancellable *cancellable, GAsyncReadyCallback cb, gpointer userdata)
 {
-	purple_log_common_total_size_async(type, name, account, ".html",
+	purple_common_log_total_size_async(type, name, account, ".html",
 		io_priority, cancellable, cb, userdata);
 }
 


More information about the Commits mailing list