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
42 static void create_chat_message(char **argv, void *data){
\r
43 LinphoneChatRoom *cr = (LinphoneChatRoom *)data;
\r
44 LinphoneChatMessage* new_message = linphone_chat_room_create_message(cr,argv[4]);
\r
45 LinphoneAddress *from;
\r
50 if(atoi(argv[3])==LinphoneChatMessageIncoming){
\r
51 from=linphone_address_new(argv[2]);
\r
53 from=linphone_address_new(argv[1]);
\r
55 linphone_chat_message_set_from(new_message,from);
\r
56 linphone_address_destroy(from);
\r
60 sscanf(argv[5],"%3c %3c%d%d:%d:%d %d",tmp1,tmp2,&ret.tm_mday,
\r
61 &ret.tm_hour,&ret.tm_min,&ret.tm_sec,&ret.tm_year);
\r
64 if(strcmp(tmp1,days[i])==0) ret.tm_wday=i;
\r
66 for(j=0;j<12;j++) {
\r
67 if(strcmp(tmp2,months[j])==0) ret.tm_mon=j;
\r
71 new_message->time=argv[5]!=NULL ? mktime(&ret) : time(NULL);
\r
72 new_message->state=atoi(argv[7]);
\r
73 cr->messages_hist=ms_list_prepend(cr->messages_hist,new_message);
\r
76 static int callback(void *data, int argc, char **argv, char **colName){
\r
77 create_chat_message(argv,data);
\r
81 void linphone_sql_request_message(sqlite3 *db,const char *stmt,LinphoneChatRoom *cr){
\r
84 ret=sqlite3_exec(db,stmt,callback,cr,&errmsg);
\r
85 if(ret != SQLITE_OK) {
\r
86 printf("Error in creation: %s.\n", errmsg);
\r
87 sqlite3_free(errmsg);
\r
91 void linphone_sql_request(sqlite3* db,const char *stmt){
\r
94 ret=sqlite3_exec(db,stmt,0,0,&errmsg);
\r
95 if(ret != SQLITE_OK) {
\r
96 ms_error("linphone_sql_request: error sqlite3_exec(): %s.\n", errmsg);
\r
97 sqlite3_free(errmsg);
\r
101 void linphone_chat_message_store(LinphoneChatMessage *msg){
\r
102 LinphoneCore *lc=linphone_chat_room_get_lc(msg->chat_room);
\r
104 char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(msg->chat_room));
\r
105 char *local_contact=linphone_address_as_string_uri_only(linphone_chat_message_get_local_address(msg));
\r
107 char *buf=sqlite3_mprintf("insert into history values(NULL,%Q,%Q,%i,%Q,%Q,%i,%i);",
\r
108 local_contact,peer,msg->dir,msg->message,my_ctime_r(&msg->time,datebuf),msg->is_read,msg->state);
\r
109 linphone_sql_request(lc->db,buf);
\r
111 ms_free(local_contact);
\r
116 void linphone_chat_message_store_state(LinphoneChatMessage *msg){
\r
117 LinphoneCore *lc=msg->chat_room->lc;
\r
120 char *buf=sqlite3_mprintf("update history set status=%i where message = %Q and time = %Q;",
\r
121 msg->state,msg->message,my_ctime_r(&msg->time,time_str));
\r
122 linphone_sql_request(lc->db,buf);
\r
127 void linphone_chat_room_mark_as_read(LinphoneChatRoom *cr){
\r
128 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
\r
131 if (lc->db==NULL) return ;
\r
133 char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
\r
134 char *buf=sqlite3_mprintf("update history set read=%i where remoteContact = %Q;",
\r
136 linphone_sql_request(lc->db,buf);
\r
141 int linphone_chat_room_get_unread_messages_count(LinphoneChatRoom *cr){
\r
142 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
\r
145 if (lc->db==NULL) return 0;
\r
147 char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
\r
148 char *buf=sqlite3_mprintf("select count(*) from history where remoteContact = %Q and read = 0;",peer);
\r
149 sqlite3_stmt *selectStatement;
\r
150 int returnValue = sqlite3_prepare_v2(lc->db,buf,-1,&selectStatement,NULL);
\r
151 if (returnValue == SQLITE_OK){
\r
152 if(sqlite3_step(selectStatement) == SQLITE_ROW){
\r
153 numrows= sqlite3_column_int(selectStatement, 0);
\r
156 sqlite3_finalize(selectStatement);
\r
162 void linphone_chat_room_delete_history(LinphoneChatRoom *cr){
\r
163 LinphoneCore *lc=cr->lc;
\r
165 if (lc->db==NULL) return ;
\r
167 char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
\r
168 char *buf=sqlite3_mprintf("delete from history where remoteContact = %Q;",peer);
\r
169 linphone_sql_request(lc->db,buf);
\r
174 MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){
\r
175 LinphoneCore *lc=linphone_chat_room_get_lc(cr);
\r
178 if (lc->db==NULL) return NULL;
\r
179 char *peer=linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr));
\r
180 cr->messages_hist = NULL;
\r
181 char *buf=sqlite3_mprintf("select * from history where remoteContact = %Q order by id DESC limit %i ;",peer,nb_message);
\r
182 linphone_sql_request_message(lc->db,buf,cr);
\r
184 ret=cr->messages_hist;
\r
185 cr->messages_hist=NULL;
\r
190 void linphone_close_storage(sqlite3* db){
\r
194 void linphone_create_table(sqlite3* db){
\r
197 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
199 if(ret != SQLITE_OK) {
\r
200 printf("Error in creation: %s.\n", errmsg);
\r
201 sqlite3_free(errmsg);
\r
205 void linphone_core_message_storage_init(LinphoneCore *lc){
\r
207 const char *errmsg;
\r
209 ret=sqlite3_open(lc->chat_db_file,&db);
\r
210 if(ret != SQLITE_OK) {
\r
211 errmsg=sqlite3_errmsg(db);
\r
212 printf("Error in the opening: %s.\n", errmsg);
\r
215 linphone_create_table(db);
\r
219 void linphone_core_message_storage_close(LinphoneCore *lc){
\r
221 sqlite3_close(lc->db);
\r
228 void linphone_chat_message_store(LinphoneChatMessage *cr){
\r
231 void linphone_chat_message_store_state(LinphoneChatMessage *cr){
\r
234 void linphone_chat_room_mark_as_read(LinphoneChatRoom *cr){
\r
237 MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){
\r
241 void linphone_chat_room_delete_history(LinphoneChatRoom *cr){
\r
244 void linphone_core_message_storage_init(LinphoneCore *lc){
\r
247 void linphone_core_message_storage_close(LinphoneCore *lc){
\r
250 int linphone_chat_room_get_unread_messages_count(LinphoneChatRoom *cr){
\r