/soc/2015/igor.gajowiak/chatlog: 602f8dcbe3ab: Marking messages ...
Igor Gajowiak
igor.gajowiak at gmail.com
Wed Jul 8 04:09:47 EDT 2015
Changeset: 602f8dcbe3aba290b6c6b08d5a9cd448781ee312
Author: Igor Gajowiak <igor.gajowiak at gmail.com>
Date: 2015-07-08 10:09 +0200
Branch: default
URL: https://hg.pidgin.im/soc/2015/igor.gajowiak/chatlog/rev/602f8dcbe3ab
Description:
Marking messages as read.
diffstat:
libpurple/plugins/log/logsqlite.c | 43 +++++++++++++++++++++++++++++---------
1 files changed, 33 insertions(+), 10 deletions(-)
diffs (100 lines):
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
@@ -72,6 +72,7 @@ sqlite_log_create_tables()
"Protocol TEXT NOT NULL); "
"CREATE TABLE Messages("
+ "Id INTEGER PRIMARY KEY AUTOINCREMENT,"
"Author TEXT,"
"AuthorAlias TEXT,"
"Recipient TEXT NOT NULL,"
@@ -82,7 +83,7 @@ sqlite_log_create_tables()
"Read BOOLEAN DEFAULT 0,"
"FOREIGN KEY(AccountId) REFERENCES Accounts(Id)); "
- "CREATE INDEX SeenIndex ON Messages (Read); "
+ "CREATE INDEX ReadIndex ON Messages (Read); "
"CREATE TRIGGER AccountDeleted AFTER DELETE ON Accounts "
"BEGIN "
@@ -197,6 +198,7 @@ sqlite_log_log_im(PurpleAccount *account
sql_statement = g_strdup_printf(
"INSERT INTO Messages "
+ "(Author, AuthorAlias, Recipient, Contents, MsgTime, Flags, AccountId, Read) "
"VALUES(\'%s\', \'%s\', \'%s\', \'%s\', %lu, %d, %d, %d);",
purple_message_get_author(message),
purple_message_get_author_alias(message),
@@ -214,6 +216,10 @@ sqlite_log_log_im(PurpleAccount *account
"SQLITE database msg insert sql statement fail");
}
+ /* here we could have a problem because of conversion from int64 to int(32bit) */
+ sqlite3_int64 db_msg_id = sqlite3_last_insert_rowid(sqlite_log_db);
+ g_object_set_data(G_OBJECT(message), "sqlitelog-msg-id", GINT_TO_POINTER(db_msg_id));
+
g_free(sql_statement);
return NULL;
}
@@ -221,7 +227,22 @@ sqlite_log_log_im(PurpleAccount *account
static GError*
sqlite_log_mark_as_read(PurpleMessage *message)
{
- // TODO: implement this
+ gpointer id = g_object_get_data(G_OBJECT(message), "sqlitelog-msg-id");
+
+ if(id == NULL)
+ return g_error_new(purple_genericlog_error_domain(),
+ PURPLE_GENERICLOG_ERROR_BACKENDFAIL,
+ "SQLITE uninitialized dynamic property (sqlitelog-msg-id)");
+
+ char *sql_statement = g_strdup_printf("UPDATE Messages SET Read = 0 "
+ "WHERE Id = %d;", GPOINTER_TO_INT(id));
+
+ if(sqlite3_exec(sqlite_log_db, sql_statement, NULL, NULL, NULL) != SQLITE_OK) {
+ g_free(sql_statement);
+ return g_error_new(purple_genericlog_error_domain(),
+ PURPLE_GENERICLOG_ERROR_BACKENDFAIL,
+ "SQLITE database marking as read failed");
+ }
return NULL;
}
@@ -230,17 +251,19 @@ sqlite_log_get_unread_cb(void *data, int
{
GList **list = (GList**) data;
- PurpleMessage *msg = purple_message_new_incoming(argv[0], argv[1],
- atoi(argv[2]), strtoull(argv[3], NULL, 10));
+ PurpleMessage *msg = purple_message_new_incoming(argv[1], argv[2],
+ atoi(argv[3]), strtoull(argv[4], NULL, 10));
- if (strcmp(argv[4], "(null)") != 0)
- purple_message_set_author_alias(msg, argv[4]);
+ if (strcmp(argv[5], "(null)") != 0)
+ purple_message_set_author_alias(msg, argv[5]);
purple_message_set_flags(msg, purple_message_get_flags(msg) |
PURPLE_MESSAGE_NO_LOG);
- // Here we can use prepend because we ordered SQLite to
- // order elements in a descending order
+ g_object_set_data(G_OBJECT(msg), "sqlitelog-msg-id", GINT_TO_POINTER(atoi(argv[0])));
+
+ /* Here we can use prepend because we ordered SQLite to
+ * order elements in a descending order */
*list = g_list_prepend(*list, msg);
g_object_ref(msg);
return 0;
@@ -250,8 +273,8 @@ static GError*
sqlite_log_get_unread_msgs(PurpleAccount *account, GList **result)
{
char *sql_statement = g_strdup_printf(
- "SELECT Author, Contents, Flags, MsgTime, AuthorAlias "
- "FROM Messages JOIN Accounts ON Id = AccountId "
+ "SELECT Messages.Id, Author, Contents, Flags, MsgTime, AuthorAlias "
+ "FROM Messages JOIN Accounts ON Accounts.Id = AccountId "
"WHERE Username LIKE \'%s\' AND Read = 0 "
"ORDER BY Username, MsgTime desc",
purple_account_get_username(account));
More information about the Commits
mailing list