]> sjero.net Git - linphone/blobdiff - coreapi/message_storage.c
Add strtok in ortp for mingw compilation
[linphone] / coreapi / message_storage.c
index 90665c26c29e5c08af8d4ac09dc698801489e732..765543f7a321a29b379e939cf2651197b5cb0544 100644 (file)
@@ -1,82 +1,60 @@
-/*
-message_storage.c
-Copyright (C) 2012  Belledonne Communications, Grenoble, France
-
-This program is free software; you can redistribute it and/or
-modify it under the terms of the GNU General Public License
-as published by the Free Software Foundation; either version 2
-of the License, or (at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-*/
-
-#include "private.h"
-#include "linphonecore.h"
-
-#ifdef WIN32
-
-static inline char *my_ctime_r(const time_t *t, char *buf){
-       strcpy(buf,ctime(t));
-       return buf;
-}
-
-#else
-#define my_ctime_r ctime_r
-#endif
-
-#ifdef MSG_STORAGE_ENABLED
-
-#include "sqlite3.h"
-
+/*\r
+message_storage.c\r
+Copyright (C) 2012  Belledonne Communications, Grenoble, France\r
+\r
+This program is free software; you can redistribute it and/or\r
+modify it under the terms of the GNU General Public License\r
+as published by the Free Software Foundation; either version 2\r
+of the License, or (at your option) any later version.\r
+\r
+This program is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+GNU General Public License for more details.\r
+\r
+You should have received a copy of the GNU General Public License\r
+along with this program; if not, write to the Free Software\r
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
+*/\r
+\r
+#include "private.h"\r
+#include "linphonecore.h"\r
+\r
+#ifdef WIN32\r
+\r
+static inline char *my_ctime_r(const time_t *t, char *buf){\r
+       strcpy(buf,ctime(t));\r
+       return buf;\r
+}\r
+\r
+#else\r
+#define my_ctime_r ctime_r\r
+#endif\r
+\r
+#ifdef MSG_STORAGE_ENABLED\r
+\r
+#include "sqlite3.h"\r
+\r
 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"};
-
-#define CONFIG_FILE ".linphone-history.db"
-
-char *linphone_message_storage_get_config_file(const char *filename){
-       const int path_max=1024;
-       char *config_file=(char *)malloc(path_max*sizeof(char));
-       if (filename==NULL) filename=CONFIG_FILE;
-       /*try accessing a local file first if exists*/
-       if (access(CONFIG_FILE,F_OK)==0){
-               snprintf(config_file,path_max,"%s",filename);
-       }else{
-#ifdef WIN32
-               const char *appdata=getenv("APPDATA");
-               if (appdata){
-                       snprintf(config_file,path_max,"%s\\%s",appdata,LINPHONE_CONFIG_DIR);
-                       CreateDirectory(config_file,NULL);
-                       snprintf(config_file,path_max,"%s\\%s\\%s",appdata,LINPHONE_CONFIG_DIR,filename);
-               }
-#else
-               const char *home=getenv("HOME");
-               if (home==NULL) home=".";
-               snprintf(config_file,path_max,"%s/%s",home,filename);
-#endif
-       }
-       return config_file;
-}
-
-void create_chat_message(char **argv, void *data){
-       LinphoneChatRoom *cr = (LinphoneChatRoom *)data;
-       LinphoneChatMessage* new_message = linphone_chat_room_create_message(cr,argv[4]);
+static const char *months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};\r
+\r
+\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};
-       
-       if(atoi(argv[3])==INCOMING){
-               linphone_chat_message_set_from(new_message,linphone_address_new(argv[2]));
-       } else {
-               linphone_chat_message_set_from(new_message,linphone_address_new(argv[1]));
-       }
-
+       char tmp2[80]={0};\r
+       \r
+       if(atoi(argv[3])==LinphoneChatMessageIncoming){\r
+               from=linphone_address_new(argv[2]);\r
+       } else {\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
                sscanf(argv[5],"%3c %3c%d%d:%d:%d %d",tmp1,tmp2,&ret.tm_mday,\r
@@ -87,108 +65,190 @@ void create_chat_message(char **argv, void *data){
                }\r
                for(j=0;j<12;j++) { \r
                        if(strcmp(tmp2,months[j])==0) ret.tm_mon=j; \r
-               }
-       }
-       new_message->time=argv[5]!=NULL ? mktime(&ret) : time(NULL);
-       new_message->state=atoi(argv[7]);
-       cr->messages_hist=ms_list_prepend(cr->messages_hist,(void *)new_message);
-}
-
-static int callback(void *data, int argc, char **argv, char **colName){
-    create_chat_message(argv,data);
-    return 0;
-}
-
-void linphone_sql_request_message(sqlite3 *db,const char *stmt,void *data){
-       char* errmsg;
-       int ret;
-       ret=sqlite3_exec(db,stmt,callback,data,&errmsg);
-       if(ret != SQLITE_OK) {
-               printf("Error in creation: %s.\n", errmsg);
-       }
-}
-
-void linphone_sql_request(sqlite3* db,const char *stmt){
-       char* errmsg;
-       int ret;
-       ret=sqlite3_exec(db,stmt,0,0,&errmsg);
-       if(ret != SQLITE_OK) {
-               printf("Error in creation: %s.\n", errmsg);
-       }
-}
-
-void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact, 
-                     int direction, const char *message,const char *date, int read, int state){
-       LinphoneCore *lc=linphone_chat_room_get_lc(cr);
-       char *buf=sqlite3_mprintf("insert into history values(NULL,%Q,%Q,%i,%Q,%Q,%i,%i);",
-                                       local_contact,remote_contact,direction,message,date,read,state);
-       linphone_sql_request(lc->db,buf);
-}
-
-void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){
-       LinphoneCore *lc=linphone_chat_room_get_lc(cr);
-       char time_str[26];
-       char *buf=sqlite3_mprintf("update history set status=%i where message = %Q and time = %Q;",
-                      state,message,my_ctime_r(&date,time_str));
-       linphone_sql_request(lc->db,buf);
-}
-
-void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){
-       LinphoneCore *lc=linphone_chat_room_get_lc(cr);
-       char *buf=sqlite3_mprintf("update history set read=%i where remoteContact = %Q;",
-                      read,from);
-       linphone_sql_request(lc->db,buf);
-}
-
-MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){
-       LinphoneCore *lc=linphone_chat_room_get_lc(cr);
-       cr->messages_hist = NULL;
-       char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",to,nb_message);
-       linphone_sql_request_message(lc->db,buf,(void *)cr);
-       return cr->messages_hist;
-}
-
-void linphone_close_storage(sqlite3* db){
-       sqlite3_close(db);
-}
-
-void linphone_create_table(sqlite3* db){
-       char* errmsg;
-       int ret;
-       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);",
-               0,0,&errmsg);
-       if(ret != SQLITE_OK) {
-               printf("Error in creation: %s.\n", errmsg);
-       }
-}
-
-sqlite3 * linphone_message_storage_init(){
-       int ret;
-       char *errmsg;
-       sqlite3 *db;
-       char *filename;
-       filename=linphone_message_storage_get_config_file(NULL);
-       ret=sqlite3_open(filename,&db);
-       if(ret != SQLITE_OK) {
-               printf("Error in the opening: %s.\n", errmsg);
-               sqlite3_close(db);
-       }
-       linphone_create_table(db);
-       return db;
-}
-#else 
-
-void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact, 
-                     int direction, const char *message,const char *date, int read, int state){
-}
-
-void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){
-}
-
-void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){
-}
-
-MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){
-       return NULL;
-}
-#endif
\ No newline at end of file
+               }\r
+               ret.tm_isdst=-1;\r
+       }\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,new_message);\r
+}\r
+\r
+static int callback(void *data, int argc, char **argv, char **colName){\r
+    create_chat_message(argv,data);\r
+    return 0;\r
+}\r
+\r
+void linphone_sql_request_message(sqlite3 *db,const char *stmt,LinphoneChatRoom *cr){\r
+       char* errmsg=NULL;\r
+       int ret;\r
+       ret=sqlite3_exec(db,stmt,callback,cr,&errmsg);\r
+       if(ret != SQLITE_OK) {\r
+               ms_error("Error in creation: %s.\n", errmsg);\r
+               sqlite3_free(errmsg);\r
+       }\r
+}\r
+\r
+void linphone_sql_request(sqlite3* db,const char *stmt){\r
+       char* errmsg=NULL;\r
+       int ret;\r
+       ret=sqlite3_exec(db,stmt,0,0,&errmsg);\r
+       if(ret != SQLITE_OK) {\r
+               ms_error("linphone_sql_request: error sqlite3_exec(): %s.\n", errmsg);\r
+               sqlite3_free(errmsg);\r
+       }\r
+}\r
+\r
+void linphone_chat_message_store(LinphoneChatMessage *msg){\r
+       LinphoneCore *lc=linphone_chat_room_get_lc(msg->chat_room);\r
+       if (lc->db){\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
+                                               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
+               ms_free(peer);\r
+       }\r
+}\r
+\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_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 *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,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
+       \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
+       LinphoneCore *lc=linphone_chat_room_get_lc(cr);\r
+       MSList *ret;\r
+       \r
+       if (lc->db==NULL) return NULL;\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 ;",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
+void linphone_close_storage(sqlite3* db){\r
+       sqlite3_close(db);\r
+}\r
+\r
+void linphone_create_table(sqlite3* db){\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
+               ms_error("Error in creation: %s.\n", errmsg);\r
+               sqlite3_free(errmsg);\r
+       }\r
+}\r
+\r
+void linphone_core_message_storage_init(LinphoneCore *lc){\r
+       int ret;\r
+       const char *errmsg;\r
+       sqlite3 *db;\r
+       ret=sqlite3_open(lc->chat_db_file,&db);\r
+       if(ret != SQLITE_OK) {\r
+               errmsg=sqlite3_errmsg(db);\r
+               ms_error("Error in the opening: %s.\n", errmsg);\r
+               sqlite3_close(db);\r
+       }\r
+       linphone_create_table(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_chat_message_store(LinphoneChatMessage *cr){\r
+}\r
+\r
+void linphone_chat_message_store_state(LinphoneChatMessage *cr){\r
+}\r
+\r
+void linphone_chat_room_mark_as_read(LinphoneChatRoom *cr){\r
+}\r
+\r
+MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){\r
+       return NULL;\r
+}\r
+\r
+void linphone_chat_room_delete_history(LinphoneChatRoom *cr){\r
+}\r
+\r
+void linphone_core_message_storage_init(LinphoneCore *lc){\r
+}\r
+\r
+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