]> sjero.net Git - linphone/blob - coreapi/message_storage.c
history chat delete
[linphone] / coreapi / message_storage.c
1 /*\r
2 message_storage.c\r
3 Copyright (C) 2012  Belledonne Communications, Grenoble, France\r
4 \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
9 \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
14 \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
18 */\r
19 \r
20 #include "private.h"\r
21 #include "linphonecore.h"\r
22 \r
23 #ifdef WIN32\r
24 \r
25 static inline char *my_ctime_r(const time_t *t, char *buf){\r
26         strcpy(buf,ctime(t));\r
27         return buf;\r
28 }\r
29 \r
30 #else\r
31 #define my_ctime_r ctime_r\r
32 #endif\r
33 \r
34 #ifdef MSG_STORAGE_ENABLED\r
35 \r
36 #include "sqlite3.h"\r
37 \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
40 \r
41 #define CONFIG_FILE ".linphone-history.db"\r
42 \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
50         }else{\r
51 #ifdef WIN32\r
52                 const char *appdata=getenv("APPDATA");\r
53                 if (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
57                 }\r
58 #else\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
62 #endif\r
63         }\r
64         return config_file;\r
65 }\r
66 \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
70         struct tm ret={0};\r
71         char tmp1[80]={0};\r
72         char tmp2[80]={0};\r
73         \r
74         if(atoi(argv[3])==INCOMING){\r
75                 linphone_chat_message_set_from(new_message,linphone_address_new(argv[2]));\r
76         } else {\r
77                 linphone_chat_message_set_from(new_message,linphone_address_new(argv[1]));\r
78         }\r
79 \r
80         if(argv[5]!=NULL){\r
81                 int i,j;\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
84                 ret.tm_year-=1900;\r
85                 for(i=0;i<7;i++) { \r
86                         if(strcmp(tmp1,days[i])==0) ret.tm_wday=i; \r
87                 }\r
88                 for(j=0;j<12;j++) { \r
89                         if(strcmp(tmp2,months[j])==0) ret.tm_mon=j; \r
90                 }\r
91         }\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
95 }\r
96 \r
97 static int callback(void *data, int argc, char **argv, char **colName){\r
98     create_chat_message(argv,data);\r
99     return 0;\r
100 }\r
101 \r
102 void linphone_sql_request_message(sqlite3 *db,const char *stmt,void *data){\r
103         char* errmsg;\r
104         int ret;\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
108         }\r
109 }\r
110 \r
111 void linphone_sql_request(sqlite3* db,const char *stmt){\r
112         char* errmsg;\r
113         int ret;\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
117         }\r
118 }\r
119 \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
126 }\r
127 \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
130         char time_str[26];\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
134 }\r
135 \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
139                        read,from);\r
140         linphone_sql_request(lc->db,buf);\r
141 }\r
142 \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
146 }\r
147 \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
154 }\r
155 \r
156 void linphone_close_storage(sqlite3* db){\r
157         sqlite3_close(db);\r
158 }\r
159 \r
160 void linphone_create_table(sqlite3* db){\r
161         char* errmsg;\r
162         int ret;\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
164                 0,0,&errmsg);\r
165         if(ret != SQLITE_OK) {\r
166                 printf("Error in creation: %s.\n", errmsg);\r
167         }\r
168 }\r
169 \r
170 sqlite3 * linphone_message_storage_init(){\r
171         int ret;\r
172         char *errmsg;\r
173         sqlite3 *db;\r
174         char *filename;\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
179                 sqlite3_close(db);\r
180         }\r
181         linphone_create_table(db);\r
182         return db;\r
183 }\r
184 #else \r
185 \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
188 }\r
189 \r
190 void linphone_core_set_message_state(LinphoneChatRoom *cr,const char *message, int state, time_t date){\r
191 }\r
192 \r
193 void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read){\r
194 }\r
195 \r
196 MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message){\r
197         return NULL;\r
198 }\r
199 \r
200 void linphone_core_delete_history(LinphoneCore *lc,const char *from){\r
201 }\r
202 #endif