3 Copyright (C) 2012 Belledonne Communications, Grenoble, France
\r
5 This program is free software; you can redistribute it and/or
\r
6 modify it under the terms of the GNU General Public License
\r
7 as published by the Free Software Foundation; either version 2
\r
8 of the License, or (at your option) any later version.
\r
10 This program is distributed in the hope that it will be useful,
\r
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 GNU General Public License for more details.
\r
15 You should have received a copy of the GNU General Public License
\r
16 along with this program; if not, write to the Free Software
\r
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
\r
20 #include "private.h"
\r
21 #include "linphonecore.h"
\r
25 static inline char *my_ctime_r(const time_t *t, char *buf){
\r
26 strcpy(buf,ctime(t));
\r
31 #define my_ctime_r ctime_r
\r
34 #ifdef MSG_STORAGE_ENABLED
\r
36 #include "sqlite3.h"
\r
38 static const char *days[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
\r
39 static const char *months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
\r
41 #define CONFIG_FILE ".linphone-history.db"
\r
43 char *linphone_message_storage_get_config_file(const char *filename){
\r
44 const int path_max=1024;
\r
45 char *config_file=(char *)malloc(path_max*sizeof(char));
\r
46 if (filename==NULL) filename=CONFIG_FILE;
\r
47 /*try accessing a local file first if exists*/
\r
48 if (access(CONFIG_FILE,F_OK)==0){
\r
49 snprintf(config_file,path_max,"%s",filename);
\r
52 const char *appdata=getenv("APPDATA");
\r
54 snprintf(config_file,path_max,"%s\\%s",appdata,LINPHONE_CONFIG_DIR);
\r
55 CreateDirectory(config_file,NULL);
\r
56 snprintf(config_file,path_max,"%s\\%s\\%s",appdata,LINPHONE_CONFIG_DIR,filename);
\r
59 const char *home=getenv("HOME");
\r
60 if (home==NULL) home=".";
\r
61 snprintf(config_file,path_max,"%s/%s",home,filename);
\r
67 void create_chat_message(char **argv, void *data){
\r
68 LinphoneChatRoom *cr = (LinphoneChatRoom *)data;
\r
69 LinphoneChatMessage* new_message = linphone_chat_room_create_message(cr,argv[4]);
\r
74 if(atoi(argv[3])==INCOMING){
\r
75 linphone_chat_message_set_from(new_message,linphone_address_new(argv[2]));
\r
77 linphone_chat_message_set_from(new_message,linphone_address_new(argv[1]));
\r
82 sscanf(argv[5],"%3c %3c%d%d:%d:%d %d",tmp1,tmp2,&ret.tm_mday,
\r
83 &ret.tm_hour,&ret.tm_min,&ret.tm_sec,&ret.tm_year);
\r
86 if(strcmp(tmp1,days[i])==0) ret.tm_wday=i;
\r
88 for(j=0;j<12;j++) {
\r
89 if(strcmp(tmp2,months[j])==0) ret.tm_mon=j;
\r
92 new_message->time=argv[5]!=NULL ? mktime(&ret) : time(NULL);
\r
93 new_message->state=atoi(argv[7]);
\r
94 cr->messages_hist=ms_list_prepend(cr->messages_hist,(void *)new_message);
\r
97 static int callback(void *data, int argc, char **argv, char **colName){
\r
98 create_chat_message(argv,data);
\r
102 void linphone_sql_request_message(sqlite3 *db,const char *stmt,void *data){
\r
105 ret=sqlite3_exec(db,stmt,callback,data,&errmsg);
\r
106 if(ret != SQLITE_OK) {
\r
107 printf("Error in creation: %s.\n", errmsg);
\r
111 void linphone_sql_request(sqlite3* db,const char *stmt){
\r
114 ret=sqlite3_exec(db,stmt,0,0,&errmsg);
\r
115 if(ret != SQLITE_OK) {
\r
116 printf("Error in creation: %s.\n", errmsg);
\r
120 void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact,
\r
121 int direction, const char *message,const char *date, int read, int state){
\r
122 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
\r
123 char *buf=sqlite3_mprintf("insert into history values(NULL,%Q,%Q,%i,%Q,%Q,%i,%i);",
\r
124 local_contact,remote_contact,direction,message,date,read,state);
\r
125 linphone_sql_request(lc->db,buf);
\r
128 void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){
\r
129 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
\r
131 char *buf=sqlite3_mprintf("update history set status=%i where message = %Q and time = %Q;",
\r
132 state,message,my_ctime_r(&date,time_str));
\r
133 linphone_sql_request(lc->db,buf);
\r
136 void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){
\r
137 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
\r
138 char *buf=sqlite3_mprintf("update history set read=%i where remoteContact = %Q;",
\r
140 linphone_sql_request(lc->db,buf);
\r
143 void linphone_core_delete_history(LinphoneCore *lc,const char *from){
\r
144 char *buf=sqlite3_mprintf("delete from history where remoteContact = %Q;",from);
\r
145 linphone_sql_request(lc->db,buf);
\r
148 MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){
\r
149 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
\r
150 cr->messages_hist = NULL;
\r
151 char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",to,nb_message);
\r
152 linphone_sql_request_message(lc->db,buf,(void *)cr);
\r
153 return cr->messages_hist;
\r
156 void linphone_close_storage(sqlite3* db){
\r
160 void linphone_create_table(sqlite3* db){
\r
163 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
165 if(ret != SQLITE_OK) {
\r
166 printf("Error in creation: %s.\n", errmsg);
\r
170 sqlite3 * linphone_message_storage_init(){
\r
175 filename=linphone_message_storage_get_config_file(NULL);
\r
176 ret=sqlite3_open(filename,&db);
\r
177 if(ret != SQLITE_OK) {
\r
178 printf("Error in the opening: %s.\n", errmsg);
\r
181 linphone_create_table(db);
\r
186 void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact,
\r
187 int direction, const char *message,const char *date, int read, int state){
\r
190 void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){
\r
193 void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){
\r
196 MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){
\r
200 void linphone_core_delete_history(LinphoneCore *lc,const char *from){
\r