3 Copyright (C) 2012 Belledonne Communications, Grenoble, France
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "linphonecore.h"
25 static inline char *my_ctime_r(const time_t *t, char *buf){
31 #define my_ctime_r ctime_r
34 #ifdef MSG_STORAGE_ENABLED
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"};
41 #define CONFIG_FILE ".linphone-history.db"
43 char *linphone_message_storage_get_config_file(const char *filename){
44 const int path_max=1024;
45 char *config_file=(char *)malloc(path_max*sizeof(char));
46 if (filename==NULL) filename=CONFIG_FILE;
47 /*try accessing a local file first if exists*/
48 if (access(CONFIG_FILE,F_OK)==0){
49 snprintf(config_file,path_max,"%s",filename);
52 const char *appdata=getenv("APPDATA");
54 snprintf(config_file,path_max,"%s\\%s",appdata,LINPHONE_CONFIG_DIR);
55 CreateDirectory(config_file,NULL);
56 snprintf(config_file,path_max,"%s\\%s\\%s",appdata,LINPHONE_CONFIG_DIR,filename);
59 const char *home=getenv("HOME");
60 if (home==NULL) home=".";
61 snprintf(config_file,path_max,"%s/%s",home,filename);
67 void create_chat_message(char **argv, void *data){
68 LinphoneChatRoom *cr = (LinphoneChatRoom *)data;
69 LinphoneChatMessage* new_message = linphone_chat_room_create_message(cr,argv[4]);
74 if(atoi(argv[3])==INCOMING){
75 linphone_chat_message_set_from(new_message,linphone_address_new(argv[2]));
77 linphone_chat_message_set_from(new_message,linphone_address_new(argv[1]));
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);
93 new_message->state=atoi(argv[7]);
94 cr->messages_hist=ms_list_prepend(cr->messages_hist,(void *)new_message);
97 static int callback(void *data, int argc, char **argv, char **colName){
98 create_chat_message(argv,data);
102 void linphone_sql_request_message(sqlite3 *db,const char *stmt,void *data){
105 ret=sqlite3_exec(db,stmt,callback,data,&errmsg);
106 if(ret != SQLITE_OK) {
107 printf("Error in creation: %s.\n", errmsg);
111 void linphone_sql_request(sqlite3* db,const char *stmt){
114 ret=sqlite3_exec(db,stmt,0,0,&errmsg);
115 if(ret != SQLITE_OK) {
116 printf("Error in creation: %s.\n", errmsg);
120 void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact,
121 int direction, const char *message,const char *date, int read, int state){
122 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
123 char *buf=sqlite3_mprintf("insert into history values(NULL,%Q,%Q,%i,%Q,%Q,%i,%i);",
124 local_contact,remote_contact,direction,message,date,read,state);
125 linphone_sql_request(lc->db,buf);
128 void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){
129 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
131 char *buf=sqlite3_mprintf("update history set status=%i where message = %Q and time = %Q;",
132 state,message,my_ctime_r(&date,time_str));
133 linphone_sql_request(lc->db,buf);
136 void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){
137 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
138 char *buf=sqlite3_mprintf("update history set read=%i where remoteContact = %Q;",
140 linphone_sql_request(lc->db,buf);
143 MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){
144 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
145 cr->messages_hist = NULL;
146 char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",to,nb_message);
147 linphone_sql_request_message(lc->db,buf,(void *)cr);
148 return cr->messages_hist;
151 void linphone_close_storage(sqlite3* db){
155 void linphone_create_table(sqlite3* db){
158 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);",
160 if(ret != SQLITE_OK) {
161 printf("Error in creation: %s.\n", errmsg);
165 sqlite3 * linphone_message_storage_init(){
170 filename=linphone_message_storage_get_config_file(NULL);
171 ret=sqlite3_open(filename,&db);
172 if(ret != SQLITE_OK) {
173 printf("Error in the opening: %s.\n", errmsg);
176 linphone_create_table(db);
181 void linphone_core_set_history_message(LinphoneChatRoom *cr,const char *local_contact,const char *remote_contact,
182 int direction, const char *message,const char *date, int read, int state){
185 void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){
188 void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){
191 MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){