/soc/2015/igor.gajowiak/chatlog: 42756bbb95bd: Implemented getti...

Igor Gajowiak igor.gajowiak at gmail.com
Sat Jul 25 16:21:51 EDT 2015


Changeset: 42756bbb95bd1bc04895ca2770701ff05854caef
Author:	 Igor Gajowiak <igor.gajowiak at gmail.com>
Date:	 2015-07-25 22:21 +0200
Branch:	 default
URL: https://hg.pidgin.im/soc/2015/igor.gajowiak/chatlog/rev/42756bbb95bd

Description:

Implemented getting messages send to or received from a buddy. Refactored some queries to use common enums.

diffstat:

 libpurple/genericlog.c            |   11 ++
 libpurple/genericlog.h            |   25 +++++
 libpurple/plugins/log/logsqlite.c |  171 +++++++++++++++++++++++++++----------
 3 files changed, 159 insertions(+), 48 deletions(-)

diffs (truncated from 331 to 300 lines):

diff --git a/libpurple/genericlog.c b/libpurple/genericlog.c
--- a/libpurple/genericlog.c
+++ b/libpurple/genericlog.c
@@ -248,6 +248,15 @@ purple_genericlog_get_unseen_msgs(Purple
 }
 
 gboolean
+purple_genericlog_get_all_msgs(PurpleBuddy *buddy, GList **result,
+	GError **error)
+{
+	DEFINE_GENERICLOG_FUNC_WITH_RETURN(get_all_msgs,
+		"%s does not support getting all messages",
+		"Getting all messages from %s failed", buddy, result);
+}
+
+gboolean
 purple_genericlog_get_all_buddies(GList** result, GError** error)
 {
 	DEFINE_GENERICLOG_FUNC_WITH_RETURN(get_all_buddies,
@@ -341,6 +350,8 @@ purple_genericlog_class_init(PurpleGener
 	klass->log_im = NULL;
 	klass->mark_as_seen = NULL;
 	klass->get_unseen_msgs = NULL;
+	klass->get_all_msgs = NULL;
+	klass->get_all_buddies = NULL;
 
 	klass->reserved1 = NULL;
 	klass->reserved2 = NULL;
diff --git a/libpurple/genericlog.h b/libpurple/genericlog.h
--- a/libpurple/genericlog.h
+++ b/libpurple/genericlog.h
@@ -61,6 +61,9 @@ struct _PurpleGenericLog
  *                   In case of any error the result is not set.
  *                   The client is responsible for freeing the result with
  *                   g_list_free_full(result, g_object_unref).
+ * @get_all_msgs:    Gets all messages sent to or received from a buddy.
+ *                   Messages are sorted by their timestamp in ascending order.
+ *                   Returns %TRUE if operation succeeds, %FALSE otherwise.
  * @get_all_buddies: Gets a list of all #PurpleBuddy objects with at least one
  *                   message in the log. Returns %TRUE if operation succeeds,
  *                   %FALSE otherwise. The client is responsible for freeing
@@ -85,6 +88,8 @@ struct _PurpleGenericLogClass
 
 	gboolean (*get_unseen_msgs)(PurpleAccount *account, GList **result);
 
+	gboolean (*get_all_msgs)(PurpleBuddy *buddy, GList **result);
+
 	gboolean (*get_all_buddies)(GList **result);
 
 	void (*reserved1)(void);
@@ -236,6 +241,26 @@ purple_genericlog_get_unseen_msgs(Purple
 	GError **error);
 
 /**
+ * purple_genericlog_get_all_msgs:
+ * @buddy:  The buddy.
+ * @result: Return location for a #GList of #PurpleMessage objects.
+ *          This will not be set in case of any errors.
+ * @error:  Return location for a #GError or %NULL. If provided, this
+ *          will be set to the reason if getting messages fails.
+ *
+ * Gets all messages from the active log sent to or received from a buddy.
+ *
+ * Messages are sorted by their timestamp in ascending order.
+ * The caller is responsible for freeing the result with
+ * g_list_free_full(result, g_object_unref).
+ *
+ * Returns: %TRUE if succeeds, %FALSE otherwise.
+ */
+gboolean
+purple_genericlog_get_all_msgs(PurpleBuddy *buddy, GList **result,
+	GError **error);
+
+/**
  * purple_genericlog_get_all_buddies:
  * @result[out]: Return location for a #GList containing a list of #PurpleBuddy
  *               objects. This will not be set in case of any errors.
diff --git a/libpurple/plugins/log/logsqlite.c b/libpurple/plugins/log/logsqlite.c
--- a/libpurple/plugins/log/logsqlite.c
+++ b/libpurple/plugins/log/logsqlite.c
@@ -99,29 +99,40 @@ typedef enum
 static sqlite3_stmt *get_unseen_msgs_q = NULL;
 
 static const char *get_unseen_msgs_q_str =
-	"SELECT Messages.Id, Author, AuthorAlias, Recipient, Contents, "
-	"MsgTime, Flags, Seen "
-	"FROM Messages JOIN Accounts ON Accounts.Id = AccountId "
-	"WHERE Username = ?1 AND ProtocolId = ?2 AND Seen = 0 "
-	"ORDER BY Messages.Id DESC;";
+	"SELECT Id, Author, AuthorAlias, Recipient, Contents, MsgTime, Flags "
+	"FROM Messages "
+	"WHERE AccountId = ?1 AND Seen = 0 "
+	"ORDER BY MsgTime DESC, Id DESC;";
 
 typedef enum
 {
-	GET_UNSEEN_MSGS_QUERY_PARAM_USERNAME   = 1,
-	GET_UNSEEN_MSGS_QUERY_PARAM_PROTOCOLID = 2,
+	GET_UNSEEN_MSGS_QUERY_PARAM_ACCOUNTID  = 1,
 } GetUnseenMsgsQueryParam;
 
+static sqlite3_stmt *get_all_msgs_q = NULL;
+
+static const char *get_all_msgs_q_str =
+	"SELECT Id, Author, AuthorAlias, Recipient, Contents, MsgTime, Flags "
+	"FROM Messages "
+	"WHERE AccountId = ?1 AND (Author = ?2 OR Recipient = ?2) "
+	"ORDER BY MsgTime DESC, Id DESC;";
+
 typedef enum
 {
-	GET_UNSEEN_MSGS_QUERY_COL_ID          = 0,
-	GET_UNSEEN_MSGS_QUERY_COL_AUTHOR      = 1,
-	GET_UNSEEN_MSGS_QUERY_COL_AUTHORALIAS = 2,
-	GET_UNSEEN_MSGS_QUERY_COL_RECIPIENT   = 3,
-	GET_UNSEEN_MSGS_QUERY_COL_CONTENTS    = 4,
-	GET_UNSEEN_MSGS_QUERY_COL_MSGTIME     = 5,
-	GET_UNSEEN_MSGS_QUERY_COL_FLAGS       = 6,
-	GET_UNSEEN_MSGS_QUERY_COL_SEEN        = 7,
-} GetUnseenMsgsQueryCol;
+	GET_ALL_MSGS_QUERY_PARAM_ACCOUNTID = 1,
+	GET_ALL_MSGS_QUERY_PARAM_WHO       = 2,
+} GetAllMsgsQueryParam;
+
+typedef enum
+{
+	GET_MSGS_QUERY_COL_ID          = 0,
+	GET_MSGS_QUERY_COL_AUTHOR      = 1,
+	GET_MSGS_QUERY_COL_AUTHORALIAS = 2,
+	GET_MSGS_QUERY_COL_RECIPIENT   = 3,
+	GET_MSGS_QUERY_COL_CONTENTS    = 4,
+	GET_MSGS_QUERY_COL_MSGTIME     = 5,
+	GET_MSGS_QUERY_COL_FLAGS       = 6,
+} GetMsgsQueryCol;
 
 static sqlite3_stmt *mark_as_seen_q = NULL;
 
@@ -173,6 +184,7 @@ sqlitelog_finalize_queries()
 	sqlitelog_finalize_query(&insert_account_q);
 	sqlitelog_finalize_query(&insert_message_q);
 	sqlitelog_finalize_query(&get_unseen_msgs_q);
+	sqlitelog_finalize_query(&get_all_msgs_q);
 	sqlitelog_finalize_query(&mark_as_seen_q);
 	sqlitelog_finalize_query(&get_all_buddies_q);
 }
@@ -206,6 +218,7 @@ sqlitelog_prepare_queries()
 	    !sqlitelog_prepare_query(insert_account_q_str, &insert_account_q) ||
 		!sqlitelog_prepare_query(insert_message_q_str, &insert_message_q) ||
 		!sqlitelog_prepare_query(get_unseen_msgs_q_str, &get_unseen_msgs_q) ||
+		!sqlitelog_prepare_query(get_all_msgs_q_str, &get_all_msgs_q) ||
 		!sqlitelog_prepare_query(mark_as_seen_q_str, &mark_as_seen_q) ||
 		!sqlitelog_prepare_query(get_all_buddies_q_str, &get_all_buddies_q)) {
 
@@ -403,8 +416,8 @@ sqlitelog_insert_message(PurpleAccount *
 			seen) != SQLITE_OK ||
 		sqlite3_bind_int(insert_message_q, INSERT_MESSAGE_QUERY_PARAM_SEND,
 			send) != SQLITE_OK ||
-		sqlite3_bind_int(insert_message_q, INSERT_MESSAGE_QUERY_PARAM_ACCOUNTID,
-			account_id) != SQLITE_OK) {
+		sqlite3_bind_int64(insert_message_q,
+			INSERT_MESSAGE_QUERY_PARAM_ACCOUNTID, account_id) != SQLITE_OK) {
 		return FALSE;
 	}
 
@@ -420,45 +433,29 @@ sqlitelog_insert_message(PurpleAccount *
 }
 
 static gboolean
-sqlitelog_get_unseen_msgs_impl(PurpleAccount *account, GList **result)
+sqlitelog_execute_get_msgs_query(sqlite3_stmt *query, GList **result)
 {
-	g_assert(account);
+	g_assert(query);
 	g_assert(result);
 
-	g_assert(get_unseen_msgs_q);
-
-	const char *username = purple_account_get_username(account);
-	const char *protocol_id = purple_account_get_protocol_id(account);
-
-	if (sqlite3_bind_text(get_unseen_msgs_q,
-			GET_UNSEEN_MSGS_QUERY_PARAM_USERNAME, username, -1,
-			SQLITE_STATIC) != SQLITE_OK ||
-		sqlite3_bind_text(get_unseen_msgs_q,
-			GET_UNSEEN_MSGS_QUERY_PARAM_PROTOCOLID, protocol_id, -1,
-			SQLITE_STATIC) != SQLITE_OK) {
-		return FALSE;
-	}
-
 	GList *messages = NULL;
 
 	int rc;
-	while ((rc = sqlite3_step(get_unseen_msgs_q)) == SQLITE_ROW) {
-		unsigned id = sqlite3_column_int(get_unseen_msgs_q,
-			GET_UNSEEN_MSGS_QUERY_COL_ID);
+	while ((rc = sqlite3_step(query)) == SQLITE_ROW) {
+		unsigned id = sqlite3_column_int(query,
+			GET_MSGS_QUERY_COL_ID);
 		const char *author = (const char*) sqlite3_column_text(
-			get_unseen_msgs_q, GET_UNSEEN_MSGS_QUERY_COL_AUTHOR);
+			query, GET_MSGS_QUERY_COL_AUTHOR);
 		const char *author_alias = (const char*) sqlite3_column_text(
-			get_unseen_msgs_q, GET_UNSEEN_MSGS_QUERY_COL_AUTHORALIAS);
+			query, GET_MSGS_QUERY_COL_AUTHORALIAS);
 		const char *recipient = (const char*) sqlite3_column_text(
-			get_unseen_msgs_q, GET_UNSEEN_MSGS_QUERY_COL_RECIPIENT);
+			query, GET_MSGS_QUERY_COL_RECIPIENT);
 		const char *contents = (const char*) sqlite3_column_text(
-			get_unseen_msgs_q, GET_UNSEEN_MSGS_QUERY_COL_CONTENTS);
-		guint64 msg_time = sqlite3_column_int64(get_unseen_msgs_q,
-			GET_UNSEEN_MSGS_QUERY_COL_MSGTIME);
-		PurpleMessageFlags msg_flags = sqlite3_column_int(get_unseen_msgs_q,
-			GET_UNSEEN_MSGS_QUERY_COL_FLAGS);
-		int seen = sqlite3_column_int(get_unseen_msgs_q,
-			GET_UNSEEN_MSGS_QUERY_COL_SEEN);
+			query, GET_MSGS_QUERY_COL_CONTENTS);
+		guint64 msg_time = (guint64) sqlite3_column_int64(query,
+			GET_MSGS_QUERY_COL_MSGTIME);
+		PurpleMessageFlags msg_flags = sqlite3_column_int(query,
+			GET_MSGS_QUERY_COL_FLAGS);
 
 		PurpleMessage *msg = purple_message_new(author, author_alias,
 			recipient, contents, msg_time, msg_flags);
@@ -469,7 +466,7 @@ sqlitelog_get_unseen_msgs_impl(PurpleAcc
 		messages = g_list_prepend(messages, msg);
 	}
 
-	sqlite3_reset(get_accunt_id_q);
+	sqlite3_reset(query);
 
 	if (rc != SQLITE_DONE) {
 		g_list_free_full(messages, g_object_unref);
@@ -481,6 +478,69 @@ sqlitelog_get_unseen_msgs_impl(PurpleAcc
 }
 
 static gboolean
+sqlitelog_get_unseen_msgs_impl(PurpleAccount *account, GList **result)
+{
+	g_assert(account);
+	g_assert(result);
+
+	g_assert(get_unseen_msgs_q);
+
+	unsigned account_id;
+	if (!sqlitelog_get_account_id(account, &account_id))
+		return FALSE;
+
+	// If there are no messages sent or received from account
+	// then return empty list
+	if (account_id == SQLITELOG_DB_ID_NONE) {
+		*result = NULL;
+		return TRUE;
+	}
+
+	if (sqlite3_bind_int64(get_unseen_msgs_q,
+			GET_UNSEEN_MSGS_QUERY_PARAM_ACCOUNTID, account_id) != SQLITE_OK) {
+		return FALSE;
+	}
+
+	return sqlitelog_execute_get_msgs_query(get_unseen_msgs_q, result);
+}
+
+static gboolean
+sqlitelog_get_all_msgs_impl(PurpleBuddy *buddy, GList **result)
+{
+	g_assert(buddy);
+	g_assert(result);
+
+	g_assert(get_all_msgs_q);
+
+	PurpleAccount *account = purple_buddy_get_account(buddy);
+
+	g_return_val_if_fail(account != NULL, FALSE);
+
+	unsigned account_id;
+	if (!sqlitelog_get_account_id(account, &account_id))
+		return FALSE;
+
+	if (account_id == SQLITELOG_DB_ID_NONE) {
+		*result = NULL;
+		return TRUE;
+	}
+
+	const char *buddy_name = purple_buddy_get_name(buddy);
+
+	g_return_val_if_fail(buddy_name != NULL, FALSE);
+
+	if (sqlite3_bind_int64(get_all_msgs_q,
+			GET_ALL_MSGS_QUERY_PARAM_ACCOUNTID, account_id) != SQLITE_OK ||
+		sqlite3_bind_text(get_all_msgs_q,
+			GET_ALL_MSGS_QUERY_PARAM_WHO, buddy_name, -1, SQLITE_STATIC)
+		!= SQLITE_OK) {
+		return FALSE;
+	}
+
+	return sqlitelog_execute_get_msgs_query(get_all_msgs_q, result);
+}
+
+static gboolean
 sqlitelog_mark_as_seen_impl(unsigned msg_id)



More information about the Commits mailing list