]> sjero.net Git - linphone/blobdiff - coreapi/message_storage.c
add number of unread messages
[linphone] / coreapi / message_storage.c
index c62fa1ddb580ea8061526dd67d8482ef9502f906..0c41c20d078659712f4a7d954c143f38393ee342 100644 (file)
@@ -78,11 +78,12 @@ static int callback(void *data, int argc, char **argv, char **colName){
 }\r
 \r
 void linphone_sql_request_message(sqlite3 *db,const char *stmt,LinphoneChatRoom *cr){\r
-       char* errmsg;\r
+       char* errmsg=NULL;\r
        int ret;\r
        ret=sqlite3_exec(db,stmt,callback,cr,&errmsg);\r
        if(ret != SQLITE_OK) {\r
                printf("Error in creation: %s.\n", errmsg);\r
+               sqlite3_free(errmsg);\r
        }\r
 }\r
 \r
@@ -99,7 +100,7 @@ void linphone_sql_request(sqlite3* db,const char *stmt){
 void linphone_chat_message_store(LinphoneChatMessage *msg){\r
        LinphoneCore *lc=linphone_chat_room_get_lc(msg->chat_room);\r
        if (lc->db){\r
-               const char *peer=msg->chat_room->peer;\r
+               char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(msg->chat_room));\r
                char *local_contact=linphone_address_as_string_uri_only(linphone_chat_message_get_local_address(msg));\r
                char datebuf[26];\r
                char *buf=sqlite3_mprintf("insert into history values(NULL,%Q,%Q,%i,%Q,%Q,%i,%i);",\r
@@ -107,6 +108,7 @@ void linphone_chat_message_store(LinphoneChatMessage *msg){
                linphone_sql_request(lc->db,buf);\r
                sqlite3_free(buf);\r
                ms_free(local_contact);\r
+               ms_free(peer);\r
        }\r
 }\r
 \r
@@ -127,19 +129,45 @@ void linphone_chat_room_mark_as_read(LinphoneChatRoom *cr){
        \r
        if (lc->db==NULL) return ;\r
 \r
+       char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));\r
        char *buf=sqlite3_mprintf("update history set read=%i where remoteContact = %Q;",\r
-                      read,cr->peer);\r
+                      read,peer);\r
        linphone_sql_request(lc->db,buf);\r
        sqlite3_free(buf);\r
+       ms_free(peer);\r
+}\r
+\r
+int linphone_chat_room_get_unread_messages_count(LinphoneChatRoom *cr){\r
+       LinphoneCore *lc=linphone_chat_room_get_lc(cr);\r
+       int numrows=0;\r
+       \r
+       if (lc->db==NULL) return 0;\r
+       \r
+       char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));\r
+       char *buf=sqlite3_mprintf("select count(*) from history where remoteContact = %Q and read = 0;",peer);\r
+       sqlite3_stmt *selectStatement;\r
+       int returnValue = sqlite3_prepare_v2(lc->db,buf,-1,&selectStatement,NULL);\r
+       if (returnValue == SQLITE_OK){\r
+               if(sqlite3_step(selectStatement) == SQLITE_ROW){\r
+                       numrows= sqlite3_column_int(selectStatement, 0);\r
+               }\r
+       }\r
+       sqlite3_finalize(selectStatement);\r
+       sqlite3_free(buf);\r
+       ms_free(peer);\r
+       return numrows;\r
 }\r
 \r
 void linphone_chat_room_delete_history(LinphoneChatRoom *cr){\r
        LinphoneCore *lc=cr->lc;\r
        \r
        if (lc->db==NULL) return ;\r
-       char *buf=sqlite3_mprintf("delete from history where remoteContact = %Q;",cr->peer);\r
+       \r
+       char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));\r
+       char *buf=sqlite3_mprintf("delete from history where remoteContact = %Q;",peer);\r
        linphone_sql_request(lc->db,buf);\r
        sqlite3_free(buf);\r
+       ms_free(peer);\r
 }\r
 \r
 MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){\r
@@ -147,13 +175,14 @@ MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){
        MSList *ret;\r
        \r
        if (lc->db==NULL) return NULL;\r
-       \r
+       char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));\r
        cr->messages_hist = NULL;\r
-       char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",cr->peer,nb_message);\r
+       char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",peer,nb_message);\r
        linphone_sql_request_message(lc->db,buf,cr);\r
        sqlite3_free(buf);\r
        ret=cr->messages_hist;\r
        cr->messages_hist=NULL;\r
+       ms_free(peer);\r
        return ret;\r
 }\r
 \r
@@ -162,12 +191,13 @@ void linphone_close_storage(sqlite3* db){
 }\r
 \r
 void linphone_create_table(sqlite3* db){\r
-       char* errmsg;\r
+       char* errmsg=NULL;\r
        int ret;\r
        ret=sqlite3_exec(db,"CREATE TABLE if not exists history (id INTEGER PRIMARY KEY AUTOINCREMENT, localContact TEXT NOT NULL, remoteContact TEXT NOT NULL, direction INTEGER, message TEXT, time TEXT NOT NULL, read INTEGER, status INTEGER);",\r
                0,0,&errmsg);\r
        if(ret != SQLITE_OK) {\r
                printf("Error in creation: %s.\n", errmsg);\r
+               sqlite3_free(errmsg);\r
        }\r
 }\r
 \r
@@ -216,4 +246,8 @@ void linphone_core_message_storage_init(LinphoneCore *lc){
 void linphone_core_message_storage_close(LinphoneCore *lc){\r
 }\r
 \r
+int linphone_chat_room_get_unread_messages_count(LinphoneChatRoom *cr){\r
+       return 0;\r
+}\r
+\r
 #endif\r