/soc/2015/igor.gajowiak/chatlog: 66b0421171b3: Implemented delet...

Igor Gajowiak igor.gajowiak at gmail.com
Sat Aug 1 14:08:50 EDT 2015


Changeset: 66b0421171b3035eaffa6c8e8bec5671d9f6e7b2
Author:	 Igor Gajowiak <igor.gajowiak at gmail.com>
Date:	 2015-08-01 20:08 +0200
Branch:	 default
URL: https://hg.pidgin.im/soc/2015/igor.gajowiak/chatlog/rev/66b0421171b3

Description:

Implemented deleting a buddy from the log.

diffstat:

 libpurple/genericlog.c            |   9 +++++
 libpurple/genericlog.h            |  19 ++++++++++
 libpurple/plugins/log/logsqlite.c |  69 +++++++++++++++++++++++++++++++++++---
 3 files changed, 91 insertions(+), 6 deletions(-)

diffs (191 lines):

diff --git a/libpurple/genericlog.c b/libpurple/genericlog.c
--- a/libpurple/genericlog.c
+++ b/libpurple/genericlog.c
@@ -283,6 +283,14 @@ purple_genericlog_get_all_days(PurpleBud
 		"Getting all days from %s failed", buddy, result);
 }
 
+gboolean
+purple_genericlog_wipe_log_for_buddy(PurpleBuddy *buddy, GError **error)
+{
+	DEFINE_GENERICLOG_FUNC_WITH_RETURN(wipe_log_for_buddy,
+		"%s does not support removing messages",
+		"Removing messages from %s failed", buddy);
+}
+
 #undef DEFINE_GENERICLOG_FUNC_WITH_RETURN
 
 /**************************************************************************/
@@ -373,6 +381,7 @@ purple_genericlog_class_init(PurpleGener
 	klass->get_older_msgs = NULL;
 	klass->get_all_buddies = NULL;
 	klass->get_all_days = NULL;
+	klass->wipe_log_for_buddy = 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
@@ -82,6 +82,9 @@ struct _PurpleGenericLog
  *                   (guint64) (at 00:00). Returns %TRUE if operation succeeds,
  *                   %FALSE otherwise. The client is responsible for freeing
  *                   the result with g_array_unref.
+ * @wipe_log_for_buddy: Deletes all messages sent to or received from
+ *                      a given buddy. Returns %TRUE if operation succeeds,
+ *                      %FALSE otherwise.
  *
  * The abstract base class for all log implementations. A conforming plugin
  * must implement at least activate and deactivate methods. Not supported
@@ -111,6 +114,8 @@ struct _PurpleGenericLogClass
 
 	gboolean (*get_all_days)(PurpleBuddy *buddy, GArray **result);
 
+	gboolean (*wipe_log_for_buddy)(PurpleBuddy *buddy);
+
 	void (*reserved1)(void);
 	void (*reserved2)(void);
 	void (*reserved3)(void);
@@ -341,6 +346,20 @@ gboolean
 purple_genericlog_get_all_days(PurpleBuddy *buddy, GArray **result,
 	GError **error);
 
+/**
+ * purple_genericlog_wipe_log_for_buddy:
+ * @buddy: The buddy.
+ * @error: Return location for a #GError or %NULL. If provided, this
+ *         will be set to the reason if getting days fails.
+ *
+ * Deletes all messages sent to or received from a given buddy from the
+ * active log.
+ *
+ * Returns: %TRUE if succeeds. %FALSE otherwise.
+ */
+gboolean
+purple_genericlog_wipe_log_for_buddy(PurpleBuddy *buddy, GError **error);
+
 /**************************************************************************/
 /* Error Codes                                                            */
 /**************************************************************************/
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
@@ -265,6 +265,17 @@ typedef enum
 	GET_DAYS_QUERY_COL_TIME = 0
 } GetAllDaysQueryCol;
 
+static sqlite3_stmt *delete_buddy_q = NULL;
+
+static const char *delete_buddy_q_str =
+	"DELETE FROM Buddies WHERE Name = ?1 AND AccountId = ?2;";
+
+typedef enum
+{
+	DELETE_BUDDY_QUERY_PARAM_BUDDY_NAME = 1,
+	DELETE_BUDDY_QUERY_PARAM_ACCOUNTID  = 2,
+} DeleteBuddyQueryParam;
+
 static void
 sqlitelog_finalize_query(sqlite3_stmt **query)
 {
@@ -298,6 +309,7 @@ sqlitelog_finalize_queries()
 	sqlitelog_finalize_query(&mark_as_seen_q);
 	sqlitelog_finalize_query(&get_all_buddies_q);
 	sqlitelog_finalize_query(&get_all_days_q);
+	sqlitelog_finalize_query(&delete_buddy_q);
 }
 
 static gboolean
@@ -344,7 +356,8 @@ sqlitelog_prepare_queries()
 			&get_older_msgs_upper_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) ||
-		!sqlitelog_prepare_query(get_all_days_q_str, &get_all_days_q)) {
+		!sqlitelog_prepare_query(get_all_days_q_str, &get_all_days_q) ||
+		!sqlitelog_prepare_query(delete_buddy_q_str, &delete_buddy_q)) {
 
 		sqlitelog_finalize_queries();
 		return FALSE;
@@ -426,12 +439,8 @@ sqlitelog_create_tables(sqlite3 *db_hand
 
 		"CREATE TRIGGER BuddyDeleted AFTER DELETE ON Buddies "
 		"BEGIN "
+		"DELETE FROM Messages WHERE BuddyId = OLD.Id; "
 		"DELETE FROM Days WHERE BuddyId = OLD.Id; "
-		"END; "
-
-		"CREATE TRIGGER DayDeleted AFTER DELETE ON Days "
-		"BEGIN "
-		"DELETE FROM Messages WHERE DayId = OLD.Id; "
 		"END; ";
 
 	return sqlite3_exec(db_handle, query, NULL, NULL, NULL);
@@ -1004,6 +1013,38 @@ sqlitelog_get_all_days_impl(PurpleBuddy 
 	return TRUE;
 }
 
+static gboolean
+sqlitelog_delete_buddy_impl(PurpleBuddy *buddy)
+{
+	g_assert(buddy);
+
+	g_assert(delete_buddy_q);
+
+	PurpleAccount *account = purple_buddy_get_account(buddy);
+	g_return_val_if_fail(account != NULL, FALSE);
+
+	unsigned account_id = SQLITELOG_DB_ID_NONE;
+	if (!sqlitelog_get_account_id(account, &account_id))
+		return FALSE;
+
+	if (account_id == SQLITELOG_DB_ID_NONE)
+		return TRUE;
+
+	const char* name = purple_buddy_get_name(buddy);
+
+	if (sqlite3_bind_text(delete_buddy_q, DELETE_BUDDY_QUERY_PARAM_BUDDY_NAME,
+			name, -1, SQLITE_STATIC) != SQLITE_OK ||
+		sqlite3_bind_int64(delete_buddy_q, DELETE_BUDDY_QUERY_PARAM_ACCOUNTID,
+			account_id) != SQLITE_OK) {
+		return FALSE;
+	}
+
+	int rc = sqlite3_step(delete_buddy_q);
+	sqlite3_reset(delete_buddy_q);
+
+	return rc == SQLITE_DONE;
+}
+
 /**************************************************************************/
 /* PurpleGenericLog API implementation                                    */
 /**************************************************************************/
@@ -1247,6 +1288,20 @@ sqlitelog_get_all_days(PurpleBuddy *budd
 	return sqlitelog_get_all_days_impl(buddy, result);
 }
 
+static gboolean
+sqlitelog_wipe_log_for_buddy(PurpleBuddy *buddy)
+{
+	g_return_val_if_fail(buddy != NULL, FALSE);
+
+	if (!sqlitelog_db_handle) {
+		purple_debug_error(SQLITELOG_DEBUG_CATEGORY,
+			"Log is not active");
+		return FALSE;
+	}
+
+	return sqlitelog_delete_buddy_impl(buddy);
+}
+
 /**************************************************************************/
 /* GObject stuff                                                          */
 /**************************************************************************/
@@ -1295,6 +1350,8 @@ purple_sqlitelog_class_init (PurpleSqlit
 	klass->parent_class.get_older_msgs = sqlitelog_get_older_msgs;
 	klass->parent_class.get_all_buddies = sqlitelog_get_all_buddies;
 	klass->parent_class.get_all_days = sqlitelog_get_all_days;
+	klass->parent_class.wipe_log_for_buddy =
+		sqlitelog_wipe_log_for_buddy;
 }
 
 static GType



More information about the Commits mailing list