]> sjero.net Git - linphone/blobdiff - coreapi/message_storage.c
clean message storage API
[linphone] / coreapi / message_storage.c
index 3d6c45d93ce7c8dcb33c6499faffd447caf84075..c62fa1ddb580ea8061526dd67d8482ef9502f906 100644 (file)
@@ -38,44 +38,22 @@ static inline char *my_ctime_r(const time_t *t, char *buf){
 static const char *days[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};\r
 static const char *months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};\r
 \r
-#define CONFIG_FILE ".linphone-history.db"\r
-\r
-char *linphone_message_storage_get_config_file(const char *filename){\r
-       const int path_max=1024;\r
-       char *config_file=(char *)malloc(path_max*sizeof(char));\r
-       if (filename==NULL) filename=CONFIG_FILE;\r
-       /*try accessing a local file first if exists*/\r
-       if (access(CONFIG_FILE,F_OK)==0){\r
-               snprintf(config_file,path_max,"%s",filename);\r
-       }else{\r
-#ifdef WIN32\r
-               const char *appdata=getenv("APPDATA");\r
-               if (appdata){\r
-                       snprintf(config_file,path_max,"%s\\%s",appdata,LINPHONE_CONFIG_DIR);\r
-                       CreateDirectory(config_file,NULL);\r
-                       snprintf(config_file,path_max,"%s\\%s\\%s",appdata,LINPHONE_CONFIG_DIR,filename);\r
-               }\r
-#else\r
-               const char *home=getenv("HOME");\r
-               if (home==NULL) home=".";\r
-               snprintf(config_file,path_max,"%s/%s",home,filename);\r
-#endif\r
-       }\r
-       return config_file;\r
-}\r
 \r
-void create_chat_message(char **argv, void *data){\r
+static void create_chat_message(char **argv, void *data){\r
        LinphoneChatRoom *cr = (LinphoneChatRoom *)data;\r
        LinphoneChatMessage* new_message = linphone_chat_room_create_message(cr,argv[4]);\r
+       LinphoneAddress *from;\r
        struct tm ret={0};\r
        char tmp1[80]={0};\r
        char tmp2[80]={0};\r
        \r
-       if(atoi(argv[3])==INCOMING){\r
-               linphone_chat_message_set_from(new_message,linphone_address_new(argv[2]));\r
+       if(atoi(argv[3])==LinphoneChatMessageIncoming){\r
+               from=linphone_address_new(argv[2]);\r
        } else {\r
-               linphone_chat_message_set_from(new_message,linphone_address_new(argv[1]));\r
+               from=linphone_address_new(argv[1]);\r
        }\r
+       linphone_chat_message_set_from(new_message,from);\r
+       linphone_address_destroy(from);\r
 \r
        if(argv[5]!=NULL){\r
                int i,j;\r
@@ -91,7 +69,7 @@ void create_chat_message(char **argv, void *data){
        }\r
        new_message->time=argv[5]!=NULL ? mktime(&ret) : time(NULL);\r
        new_message->state=atoi(argv[7]);\r
-       cr->messages_hist=ms_list_prepend(cr->messages_hist,(void *)new_message);\r
+       cr->messages_hist=ms_list_prepend(cr->messages_hist,new_message);\r
 }\r
 \r
 static int callback(void *data, int argc, char **argv, char **colName){\r
@@ -99,58 +77,84 @@ static int callback(void *data, int argc, char **argv, char **colName){
     return 0;\r
 }\r
 \r
-void linphone_sql_request_message(sqlite3 *db,const char *stmt,void *data){\r
+void linphone_sql_request_message(sqlite3 *db,const char *stmt,LinphoneChatRoom *cr){\r
        char* errmsg;\r
        int ret;\r
-       ret=sqlite3_exec(db,stmt,callback,data,&errmsg);\r
+       ret=sqlite3_exec(db,stmt,callback,cr,&errmsg);\r
        if(ret != SQLITE_OK) {\r
                printf("Error in creation: %s.\n", errmsg);\r
        }\r
 }\r
 \r
 void linphone_sql_request(sqlite3* db,const char *stmt){\r
-       char* errmsg;\r
+       char* errmsg=NULL;\r
        int ret;\r
        ret=sqlite3_exec(db,stmt,0,0,&errmsg);\r
        if(ret != SQLITE_OK) {\r
-               printf("Error in creation: %s.\n", errmsg);\r
+               ms_error("linphone_sql_request: error sqlite3_exec(): %s.\n", errmsg);\r
+               sqlite3_free(errmsg);\r
        }\r
 }\r
 \r
-void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact, \r
-                     int direction, const char *message,const char *date, int read, int state){\r
-       LinphoneCore *lc=linphone_chat_room_get_lc(cr);\r
-       char *buf=sqlite3_mprintf("insert into history values(NULL,%Q,%Q,%i,%Q,%Q,%i,%i);",\r
-                                       local_contact,remote_contact,direction,message,date,read,state);\r
-       linphone_sql_request(lc->db,buf);\r
+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 *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
+                                               local_contact,peer,msg->dir,msg->message,my_ctime_r(&msg->time,datebuf),msg->is_read,msg->state);\r
+               linphone_sql_request(lc->db,buf);\r
+               sqlite3_free(buf);\r
+               ms_free(local_contact);\r
+       }\r
 }\r
 \r
-void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){\r
-       LinphoneCore *lc=linphone_chat_room_get_lc(cr);\r
-       char time_str[26];\r
-       char *buf=sqlite3_mprintf("update history set status=%i where message = %Q and time = %Q;",\r
-                      state,message,my_ctime_r(&date,time_str));\r
-       linphone_sql_request(lc->db,buf);\r
+void linphone_chat_message_store_state(LinphoneChatMessage *msg){\r
+       LinphoneCore *lc=msg->chat_room->lc;\r
+       if (lc->db){\r
+               char time_str[26];\r
+               char *buf=sqlite3_mprintf("update history set status=%i where message = %Q and time = %Q;",\r
+                       msg->state,msg->message,my_ctime_r(&msg->time,time_str));\r
+               linphone_sql_request(lc->db,buf);\r
+               sqlite3_free(buf);\r
+       }\r
 }\r
 \r
-void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){\r
+void linphone_chat_room_mark_as_read(LinphoneChatRoom *cr){\r
        LinphoneCore *lc=linphone_chat_room_get_lc(cr);\r
+       int read=1;\r
+       \r
+       if (lc->db==NULL) return ;\r
+\r
        char *buf=sqlite3_mprintf("update history set read=%i where remoteContact = %Q;",\r
-                      read,from);\r
+                      read,cr->peer);\r
        linphone_sql_request(lc->db,buf);\r
+       sqlite3_free(buf);\r
 }\r
 \r
-void linphone_core_delete_history(LinphoneCore *lc,const char *from){\r
-       char *buf=sqlite3_mprintf("delete from history where remoteContact = %Q;",from);\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
        linphone_sql_request(lc->db,buf);\r
+       sqlite3_free(buf);\r
 }\r
 \r
-MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){\r
+MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){\r
        LinphoneCore *lc=linphone_chat_room_get_lc(cr);\r
+       MSList *ret;\r
+       \r
+       if (lc->db==NULL) return NULL;\r
+       \r
        cr->messages_hist = NULL;\r
-       char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",to,nb_message);\r
-       linphone_sql_request_message(lc->db,buf,(void *)cr);\r
-       return cr->messages_hist;\r
+       char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",cr->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
+       return ret;\r
 }\r
 \r
 void linphone_close_storage(sqlite3* db){\r
@@ -167,36 +171,49 @@ void linphone_create_table(sqlite3* db){
        }\r
 }\r
 \r
-sqlite3 * linphone_message_storage_init(){\r
+void linphone_core_message_storage_init(LinphoneCore *lc){\r
        int ret;\r
-       char *errmsg;\r
+       char *errmsg=NULL;\r
        sqlite3 *db;\r
-       char *filename;\r
-       filename=linphone_message_storage_get_config_file(NULL);\r
-       ret=sqlite3_open(filename,&db);\r
+       ret=sqlite3_open(lc->chat_db_file,&db);\r
        if(ret != SQLITE_OK) {\r
                printf("Error in the opening: %s.\n", errmsg);\r
                sqlite3_close(db);\r
+               sqlite3_free(errmsg);\r
        }\r
        linphone_create_table(db);\r
-       return db;\r
+       lc->db=db;\r
+}\r
+\r
+void linphone_core_message_storage_close(LinphoneCore *lc){\r
+       if (lc->db){\r
+               sqlite3_close(lc->db);\r
+               lc->db=NULL;\r
+       }\r
 }\r
+\r
 #else \r
 \r
-void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact, \r
-                     int direction, const char *message,const char *date, int read, int state){\r
+void linphone_chat_message_store(LinphoneChatMessage *cr){\r
 }\r
 \r
-void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){\r
+void linphone_chat_message_store_state(LinphoneChatMessage *cr){\r
 }\r
 \r
-void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){\r
+void linphone_chat_room_mark_as_read(LinphoneChatRoom *cr){\r
 }\r
 \r
-MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){\r
+MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){\r
        return NULL;\r
 }\r
 \r
-void linphone_core_delete_history(LinphoneCore *lc,const char *from){\r
+void linphone_chat_room_delete_history(LinphoneChatRoom *cr){\r
+}\r
+\r
+void linphone_core_message_storage_init(LinphoneCore *lc){\r
 }\r
-#endif
\ No newline at end of file
+\r
+void linphone_core_message_storage_close(LinphoneCore *lc){\r
+}\r
+\r
+#endif\r