From 8a9b9fcd1416a06d1ab5d6393dc981a3a8556082 Mon Sep 17 00:00:00 2001 From: Margaux Clerc Date: Thu, 7 Mar 2013 12:19:28 +0100 Subject: [PATCH] add message storage --- configure.ac | 19 +++ console/Makefile.am | 2 + coreapi/Makefile.am | 8 +- coreapi/callbacks.c | 1 + coreapi/chat.c | 48 ++++++- coreapi/linphonecore.c | 3 + coreapi/linphonecore.h | 4 +- coreapi/message_storage.c | 194 ++++++++++++++++++++++++++++ coreapi/private.h | 23 ++++ coreapi/sal.h | 4 +- coreapi/sal_eXosip2.c | 3 - coreapi/sal_eXosip2_presence.c | 21 +-- gtk/Makefile.am | 5 +- gtk/chat.c | 229 ++++++++++++++++----------------- gtk/main.c | 4 - 15 files changed, 418 insertions(+), 150 deletions(-) create mode 100644 coreapi/message_storage.c diff --git a/configure.ac b/configure.ac index 2e2e5623..890fcba2 100644 --- a/configure.ac +++ b/configure.ac @@ -669,6 +669,24 @@ if test x$enable_tunnel = xtrue; then AC_SUBST(TUNNEL_LIBS) fi +AC_ARG_ENABLE(msg-storage, + [AS_HELP_STRING([--enable-msg-storage=[yes/no]], [Turn on compilation of message storage (default=yes)])], + [case "${enableval}" in + yes) enable_msg_storage=true ;; + no) enable_msg_storage=false ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-msg-storage) ;; + esac], + [enable_msg_storage=true] +) +AM_CONDITIONAL(BUILD_MSG_STORAGE, test x$enable_msg_storage = xtrue) +if test x$enable_msg_storage = xtrue; then + PKG_CHECK_MODULES(SQLITE3,[ sqlite3 >= 3.7.0],[],[ + AC_MSG_ERROR([sqlite3 required for message storage not found.])] ) + SQLITE3_CFLAGS+="-DMSG_STORAGE_ENABLED" + AC_SUBST(SQLITE3_CFLAGS) + AC_SUBST(SQLITE3_LIBS) +fi + dnl check for db2html (docbook) to generate html user manual AC_CHECK_PROG(have_sgmltools, sgmltools, yes, no) @@ -781,6 +799,7 @@ printf "* %-30s %s\n" "GTK interface" $gtk_ui printf "* %-30s %s\n" "Account assistant" $build_wizard printf "* %-30s %s\n" "Console interface" $console_ui printf "* %-30s %s\n" "Tools" $build_tools +printf "* %-30s %s\n" "Message storage" $enable_msg_storage printf "* %-30s %s\n" "zRTP encryption (GPLv3)" $zrtp printf "* %-30s %s\n" "uPnP support" $build_upnp diff --git a/console/Makefile.am b/console/Makefile.am index 82ce998e..976cbeaf 100644 --- a/console/Makefile.am +++ b/console/Makefile.am @@ -14,6 +14,7 @@ COMMON_CFLAGS=\ $(READLINE_CFLAGS) \ $(OSIP_CFLAGS) \ $(ORTP_CFLAGS) \ + $(SQLITE3_CFLAGS) \ $(MEDIASTREAMER_CFLAGS) if BUILD_CONSOLE @@ -28,6 +29,7 @@ linphonec_SOURCES=linphonec.c linphonec.h commands.c linphonec_CFLAGS=$(COMMON_CFLAGS) $(CONSOLE_FLAGS) linphonec_LDADD=$(top_builddir)/coreapi/liblinphone.la \ $(READLINE_LIBS) \ + $(SQLITE3_LIBS) \ $(X11_LIBS) if BUILD_WIN32 diff --git a/coreapi/Makefile.am b/coreapi/Makefile.am index 90a1c320..bef0613b 100644 --- a/coreapi/Makefile.am +++ b/coreapi/Makefile.am @@ -47,6 +47,7 @@ liblinphone_la_SOURCES=\ lsd.c linphonecore_utils.h \ ec-calibrator.c \ conference.c \ + message_storage.c \ $(GITVERSION_FILE) if BUILD_UPNP @@ -73,7 +74,9 @@ liblinphone_la_LIBADD= \ $(MEDIASTREAMER_LIBS) \ $(ORTP_LIBS) $(OPENSSL_LIBS) \ $(TUNNEL_LIBS) \ - $(LIBSOUP_LIBS) + $(LIBSOUP_LIBS) \ + $(SQLITE3_LIBS) + if BUILD_TESTS noinst_PROGRAMS=test_lsd test_ecc test_numbers @@ -106,7 +109,8 @@ AM_CFLAGS=\ $(IPV6_CFLAGS) \ -DORTP_INET6 \ $(VIDEO_CFLAGS) \ - $(TUNNEL_CFLAGS) + $(TUNNEL_CFLAGS) \ + $(SQLITE3_CFLAGS) if BUILD_WIZARD AM_CFLAGS+= -DBUILD_WIZARD diff --git a/coreapi/callbacks.c b/coreapi/callbacks.c index 1c87f88c..d65e5367 100644 --- a/coreapi/callbacks.c +++ b/coreapi/callbacks.c @@ -934,6 +934,7 @@ static void text_delivery_update(SalOp *op, SalTextDeliveryStatus status){ LinphoneChatMessage *chat_msg=(LinphoneChatMessage* )sal_op_get_user_pointer(op); const MSList* calls = linphone_core_get_calls(chat_msg->chat_room->lc); + linphone_core_set_message_state(chat_msg->chat_room,chat_msg->message,chatStatusSal2Linphone(status),chat_msg->time); if (chat_msg && chat_msg->cb) { chat_msg->cb(chat_msg ,chatStatusSal2Linphone(status) diff --git a/coreapi/chat.c b/coreapi/chat.c index b9894ea8..5f79849d 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -62,12 +62,26 @@ void linphone_chat_room_destroy(LinphoneChatRoom *cr){ ms_free(cr->peer); } +#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 + static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage* msg){ const char *route=NULL; const char *identity=linphone_core_find_best_identity(cr->lc,cr->peer_url,&route); SalOp *op=NULL; LinphoneCall *call; char* content_type; + time_t t=time(NULL); + char buf[26]; + char *to; if (lp_config_get_int(cr->lc->config,"sip","chat_use_call_dialogs",0)){ if((call = linphone_core_get_call_by_remote_address(cr->lc,cr->peer))!=NULL){ @@ -82,6 +96,7 @@ static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatM } } } + msg->time=t; if (op==NULL){ /*sending out of calls*/ op = sal_op_new(cr->lc->sal); @@ -94,11 +109,15 @@ static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatM } if (msg->external_body_url) { content_type=ms_strdup_printf("message/external-body; access-type=URL; URL=\"%s\"",msg->external_body_url); - sal_message_send(op,identity,cr->peer,content_type, NULL); + sal_message_send(op,identity,cr->peer,content_type, NULL,my_ctime_r(&t,buf)); ms_free(content_type); } else { - sal_text_send(op, identity, cr->peer,msg->message); + sal_text_send(op, identity, cr->peer,msg->message,my_ctime_r(&t,buf)); } + to=linphone_address_as_string_uri_only (cr->peer_url); + linphone_core_set_history_message(cr,identity,to,OUTGOING,msg->message, + my_ctime_r(&t,buf),READ,LinphoneChatMessageStateInProgress); + ms_free(to); } /** @@ -130,8 +149,11 @@ void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessag LinphoneChatRoom *cr=NULL; LinphoneAddress *addr; char *cleanfrom; + const char *to; + char *from; LinphoneChatMessage* msg; const SalCustomHeader *ch; + char buf[26]; addr=linphone_address_new(sal_msg->from); linphone_address_clean(addr); @@ -142,7 +164,9 @@ void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessag } cr=NULL; } + to=linphone_core_get_identity(lc); cleanfrom=linphone_address_as_string(addr); + from=linphone_address_as_string_uri_only(addr); if (cr==NULL){ /* create a new chat room */ cr=linphone_core_create_chat_room(lc,cleanfrom); @@ -150,6 +174,7 @@ void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessag msg = linphone_chat_room_create_message(cr, sal_msg->text); linphone_chat_message_set_from(msg, cr->peer_url); msg->time=sal_msg->time; + msg->state=LinphoneChatMessageStateDelivered; ch=sal_op_get_custom_header(op); if (ch) msg->custom_headers=sal_custom_header_clone(ch); @@ -158,7 +183,11 @@ void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessag } linphone_address_destroy(addr); linphone_chat_room_message_received(cr,lc,msg); + linphone_core_set_history_message(cr,to,from,INCOMING, + msg->message,my_ctime_r(&msg->time,buf),NOT_READ, + LinphoneChatMessageStateDelivered); ms_free(cleanfrom); + ms_free(from); } /** @@ -215,6 +244,7 @@ LinphoneChatMessage* linphone_chat_room_create_message(LinphoneChatRoom *cr, con void linphone_chat_room_send_message2(LinphoneChatRoom *cr, LinphoneChatMessage* msg,LinphoneChatMessageStateChangeCb status_cb, void* ud) { msg->cb=status_cb; msg->cb_ud=ud; + msg->state=LinphoneChatMessageStateInProgress; _linphone_chat_room_send_message(cr, msg); } @@ -308,6 +338,15 @@ time_t linphone_chat_message_get_time(const LinphoneChatMessage* message) { return message->time; } +/** + * Get the state of the message + *@param message #LinphoneChatMessage obj + *@return #LinphoneChatMessageState + */ +LinphoneChatMessageState linphone_chat_message_get_state(const LinphoneChatMessage* message) { + return message->state; +} + /** * Get text part of this message * @return text or NULL if no text. @@ -347,6 +386,9 @@ LinphoneChatMessage* linphone_chat_message_clone(const LinphoneChatMessage* msg) void* message_userdata; char* external_body_url; LinphoneAddress* from; + time_t time; + SalCustomHeader *custom_headers; + LinphoneChatMessageState state; };*/ LinphoneChatMessage* new_message = linphone_chat_room_create_message(msg->chat_room,msg->message); if (msg->external_body_url) new_message->external_body_url=ms_strdup(msg->external_body_url); @@ -354,6 +396,8 @@ LinphoneChatMessage* linphone_chat_message_clone(const LinphoneChatMessage* msg) new_message->cb_ud=msg->cb_ud; new_message->message_userdata=msg->message_userdata; new_message->cb=msg->cb; + new_message->time=msg->time; + new_message->state=msg->state; if (msg->from) new_message->from=linphone_address_clone(msg->from); return new_message; } diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 69dc26a1..eac31906 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -1306,6 +1306,9 @@ static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vta #ifdef TUNNEL_ENABLED lc->tunnel=linphone_core_tunnel_new(lc); if (lc->tunnel) linphone_tunnel_configure(lc->tunnel); +#endif +#ifdef MSG_STORAGE_ENABLED + lc->db=linphone_message_storage_init(); #endif if (lc->vtable.display_status) lc->vtable.display_status(lc,_("Ready")); diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index 97d1663b..c1054922 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -673,6 +673,7 @@ LinphoneCore* linphone_chat_room_get_lc(LinphoneChatRoom *cr); void linphone_chat_room_set_user_data(LinphoneChatRoom *cr, void * ud); void * linphone_chat_room_get_user_data(LinphoneChatRoom *cr); +LinphoneChatMessageState linphone_chat_message_get_state(const LinphoneChatMessage* message); const char* linphone_chat_message_state_to_string(const LinphoneChatMessageState state); LinphoneChatMessage* linphone_chat_message_clone(const LinphoneChatMessage* message); void linphone_chat_message_set_from(LinphoneChatMessage* message, const LinphoneAddress* from); @@ -1417,7 +1418,8 @@ int linphone_core_get_audio_dscp(const LinphoneCore *lc); void linphone_core_set_video_dscp(LinphoneCore *lc, int dscp); int linphone_core_get_video_dscp(const LinphoneCore *lc); - +MSList *linphone_chat_room_get_history(const char *to,LinphoneChatRoom *cr,int nb_message); +void linphone_core_set_messages_flag_read(LinphoneChatRoom *cr,const char *from, int read); #ifdef __cplusplus } diff --git a/coreapi/message_storage.c b/coreapi/message_storage.c new file mode 100644 index 00000000..90665c26 --- /dev/null +++ b/coreapi/message_storage.c @@ -0,0 +1,194 @@ +/* +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" + +static const char *days[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}; +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]); + struct tm ret={0}; + char tmp1[80]={0}; + 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])); + } + + if(argv[5]!=NULL){ + int i,j; + sscanf(argv[5],"%3c %3c%d%d:%d:%d %d",tmp1,tmp2,&ret.tm_mday, + &ret.tm_hour,&ret.tm_min,&ret.tm_sec,&ret.tm_year); + ret.tm_year-=1900; + for(i=0;i<7;i++) { + if(strcmp(tmp1,days[i])==0) ret.tm_wday=i; + } + for(j=0;j<12;j++) { + if(strcmp(tmp2,months[j])==0) ret.tm_mon=j; + } + } + 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 diff --git a/coreapi/private.h b/coreapi/private.h index 41d453df..35578604 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -44,6 +44,10 @@ extern "C" { #include "upnp.h" #endif //BUILD_UPNP +#ifdef MSG_STORAGE_ENABLED +#include "sqlite3.h" +#endif + #ifndef LIBLINPHONE_VERSION #define LIBLINPHONE_VERSION LINPHONE_VERSION #endif @@ -125,6 +129,7 @@ struct _LinphoneChatMessage { LinphoneAddress* from; time_t time; SalCustomHeader *custom_headers; + LinphoneChatMessageState state; }; typedef struct StunCandidate{ @@ -392,6 +397,7 @@ struct _LinphoneChatRoom{ char *peer; LinphoneAddress *peer_url; void * user_data; + MSList *messages_hist; }; @@ -610,6 +616,9 @@ struct _LinphoneCore LinphoneTunnel *tunnel; char* device_id; MSList *last_recv_msg_ids; +#ifdef MSG_STORAGE_ENABLED + sqlite3 *db; +#endif #ifdef BUILD_UPNP UpnpContext *upnp; #endif //BUILD_UPNP @@ -691,6 +700,20 @@ void linphone_call_params_uninit(LinphoneCallParams *params); int linphone_upnp_init(LinphoneCore *lc); void linphone_upnp_destroy(LinphoneCore *lc); +#define OUTGOING 0 +#define INCOMING 1 + +#define NOT_READ 0 +#define READ 1 + +#ifdef MSG_STORAGE_ENABLED +sqlite3 * linphone_message_storage_init(); +#endif +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); + + #ifdef __cplusplus } #endif diff --git a/coreapi/sal.h b/coreapi/sal.h index 25d8d20b..ba232cca 100644 --- a/coreapi/sal.h +++ b/coreapi/sal.h @@ -430,8 +430,8 @@ int sal_register_refresh(SalOp *op, int expires); int sal_unregister(SalOp *h); /*Messaging */ -int sal_text_send(SalOp *op, const char *from, const char *to, const char *text); -int sal_message_send(SalOp *op, const char *from, const char *to, const char* content_type, const char *msg); +int sal_text_send(SalOp *op, const char *from, const char *to, const char *text, const char*t); +int sal_message_send(SalOp *op, const char *from, const char *to, const char* content_type, const char *msg, const char*t); /*presence Subscribe/notify*/ int sal_subscribe_presence(SalOp *op, const char *from, const char *to); diff --git a/coreapi/sal_eXosip2.c b/coreapi/sal_eXosip2.c index 1777c0d4..2ea0f9af 100644 --- a/coreapi/sal_eXosip2.c +++ b/coreapi/sal_eXosip2.c @@ -1800,7 +1800,6 @@ static void text_received(Sal *sal, eXosip_event_t *ev){ } }else ms_warning("No date header in SIP MESSAGE, we don't know when it was sent."); - content_type= osip_message_get_content_type(ev->request); if (!content_type) { ms_error("Could not get message because no content type"); @@ -1848,8 +1847,6 @@ static void text_received(Sal *sal, eXosip_event_t *ev){ osip_free(from); } - - static void other_request(Sal *sal, eXosip_event_t *ev){ ms_message("in other_request"); if (ev->request==NULL) return; diff --git a/coreapi/sal_eXosip2_presence.c b/coreapi/sal_eXosip2_presence.c index 1e49970b..2670af47 100644 --- a/coreapi/sal_eXosip2_presence.c +++ b/coreapi/sal_eXosip2_presence.c @@ -81,21 +81,8 @@ void sal_remove_in_subscribe(Sal *sal, SalOp *op){ sal->in_subscribes=ms_list_remove(sal->in_subscribes,op); } -#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 - -int sal_message_send(SalOp *op, const char *from, const char *to, const char* content_type, const char *msg){ +int sal_message_send(SalOp *op, const char *from, const char *to, const char* content_type, const char *msg, const char *t){ osip_message_t *sip=NULL; - time_t t=time(NULL); - char buf[26]; if(op->cid == -1) { @@ -111,7 +98,7 @@ int sal_message_send(SalOp *op, const char *from, const char *to, const char* co sal_op_get_from(op),sal_op_get_route(op)); if (sip!=NULL){ sal_exosip_add_custom_headers(sip,op->base.custom_headers); - osip_message_set_date(sip,my_ctime_r(&t,buf)); + osip_message_set_date(sip,t); osip_message_set_content_type(sip,content_type); if (msg) osip_message_set_body(sip,msg,strlen(msg)); sal_add_other(op->base.root,op,sip); @@ -141,8 +128,8 @@ int sal_message_send(SalOp *op, const char *from, const char *to, const char* co return 0; } -int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg) { - return sal_message_send(op,from,to,"text/plain",msg); +int sal_text_send(SalOp *op, const char *from, const char *to, const char *msg,const char *t) { + return sal_message_send(op,from,to,"text/plain",msg,t); } /*presence Subscribe/notify*/ int sal_subscribe_presence(SalOp *op, const char *from, const char *to){ diff --git a/gtk/Makefile.am b/gtk/Makefile.am index 831c69a8..ba965b69 100644 --- a/gtk/Makefile.am +++ b/gtk/Makefile.am @@ -55,7 +55,7 @@ linphone_SOURCES+= \ endif linphone_LDADD= $(top_builddir)/coreapi/liblinphone.la \ - $(LIBGTK_LIBS) $(NOTIFY1_LIBS) $(NOTIFY4_LIBS) $(LIBGTKMAC_LIBS) $(INTLLIBS) + $(LIBGTK_LIBS) $(NOTIFY1_LIBS) $(NOTIFY4_LIBS) $(LIBGTKMAC_LIBS) $(INTLLIBS) $(SQLITE3_LIBS) if BUILD_WIN32 @@ -79,7 +79,8 @@ AM_CFLAGS= -DIN_LINPHONE -I$(top_srcdir)/coreapi/ \ $(MEDIASTREAMER_CFLAGS) \ $(ORTP_CFLAGS) \ $(STRICT_OPTIONS) $(LIBGTK_CFLAGS) $(LIBGTKMAC_CFLAGS) $(IPV6_CFLAGS) \ - $(TUNNEL_CFLAGS) + $(TUNNEL_CFLAGS) \ + $(SQLITE3_CFLAGS) version_date.h: $(top_srcdir)/configure.ac diff --git a/gtk/chat.c b/gtk/chat.c index 76d34c2f..159ddd8b 100644 --- a/gtk/chat.c +++ b/gtk/chat.c @@ -29,15 +29,24 @@ void linphone_gtk_quit_chatroom(LinphoneChatRoom *cr) { GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list"); GtkWidget *w=g_object_get_data(G_OBJECT(friendlist),"chatview"); int idx = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"idx")); + g_return_if_fail(w!=NULL); - gtk_notebook_remove_page (GTK_NOTEBOOK(nb),idx); + gtk_notebook_remove_page(GTK_NOTEBOOK(nb),idx); linphone_gtk_create_chat_picture(FALSE); g_object_set_data(G_OBJECT(friendlist),"chatview",NULL); - g_object_set_data(G_OBJECT(w),"from_message",NULL); + g_object_set_data(G_OBJECT(w),"from_message",NULL); g_object_set_data(G_OBJECT(w),"cr",NULL); gtk_widget_destroy(w); } +const char* get_display_name(const LinphoneAddress *from){ + const char *display=linphone_address_get_display_name(from); + if (display==NULL || display[0]=='\0') { + display=linphone_address_get_username(from); + } + return display; +} + GtkWidget *create_tab_chat_header(LinphoneChatRoom *cr,const LinphoneAddress *uri){ GtkWidget *w=gtk_hbox_new (FALSE,0); GtkWidget *i=create_pixmap ("chat.png"); @@ -49,12 +58,7 @@ GtkWidget *create_tab_chat_header(LinphoneChatRoom *cr,const LinphoneAddress *ur gtk_button_set_relief(GTK_BUTTON(b),GTK_RELIEF_NONE); gtk_widget_set_size_request(b,25,20); g_signal_connect_swapped(G_OBJECT(b),"clicked",G_CALLBACK(linphone_gtk_quit_chatroom),cr); - - const char *display=linphone_address_get_display_name(uri); - if (display==NULL || display[0]=='\0') { - display=linphone_address_get_username(uri); - } - l=gtk_label_new (display); + l=gtk_label_new (get_display_name(uri)); gtk_box_pack_start (GTK_BOX(w),i,FALSE,FALSE,0); gtk_box_pack_start (GTK_BOX(w),l,FALSE,FALSE,0); gtk_box_pack_end(GTK_BOX(w),b,TRUE,TRUE,0); @@ -75,81 +79,73 @@ void udpate_tab_chat_header(GtkWidget *chat_view,const LinphoneAddress *uri,Linp gtk_button_set_relief(GTK_BUTTON(b),GTK_RELIEF_NONE); gtk_widget_set_size_request(b,25,20); g_signal_connect_swapped(G_OBJECT(b),"clicked",G_CALLBACK(linphone_gtk_quit_chatroom),cr); - - const char *display=linphone_address_get_display_name(uri); - if (display==NULL || display[0]=='\0') { - display=linphone_address_get_username(uri); - } - l=gtk_label_new (display); + l=gtk_label_new (get_display_name(uri)); gtk_box_pack_start (GTK_BOX(w),i,FALSE,FALSE,0); gtk_box_pack_start (GTK_BOX(w),l,FALSE,FALSE,0); gtk_box_pack_end(GTK_BOX(w),b,TRUE,TRUE,0); - gtk_notebook_set_tab_label(notebook,chat_view,w); gtk_widget_show_all(w); - } void linphone_gtk_push_text(GtkWidget *w, const LinphoneAddress *from, - const char *message, gboolean me,LinphoneChatRoom *cr, time_t t){ + gboolean me,LinphoneChatRoom *cr,LinphoneChatMessage *msg, gboolean hist){ GtkTextView *text=GTK_TEXT_VIEW(linphone_gtk_get_widget(w,"textview")); GtkTextBuffer *buffer=gtk_text_view_get_buffer(text); - GtkTextIter iter,begin,end; - gtk_text_buffer_get_start_iter(buffer,&begin); + GtkTextIter iter,begin; int off; - gtk_text_buffer_get_end_iter(buffer,&iter); - off=gtk_text_iter_get_offset(&iter); + char *from_str=linphone_address_as_string_uri_only(from); + char *from_message=(char *)g_object_get_data(G_OBJECT(w),"from_message"); GList *list=g_object_get_data(G_OBJECT(w),"list"); + time_t t; - if(g_strcmp0((char *)g_object_get_data(G_OBJECT(w),"from_message"),linphone_address_as_string(from))!=0){ + gtk_text_buffer_get_start_iter(buffer,&begin); + gtk_text_buffer_get_end_iter(buffer,&iter); + off=gtk_text_iter_get_offset(&iter); + if(g_strcmp0(from_message,from_str)!=0){ gtk_text_buffer_get_iter_at_offset(buffer,&iter,off); - const char *display=linphone_address_get_display_name(from); - if (display==NULL || display[0]=='\0') { - display=linphone_address_get_username(from); - } gtk_text_buffer_get_end_iter(buffer,&iter); - gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,display,-1,"bold",me ? "bg":NULL,NULL); + gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,get_display_name(from),-1,"bold",me ? "bg":NULL,NULL); gtk_text_buffer_get_end_iter(buffer,&iter); gtk_text_buffer_insert_with_tags_by_name(buffer,&iter," : ",-1,"bold",me ? "bg":NULL,NULL); gtk_text_buffer_get_end_iter(buffer,&iter); gtk_text_buffer_insert(buffer,&iter,"\n",-1); - g_object_set_data(G_OBJECT(w),"from_message",linphone_address_as_string(from)); + g_object_set_data(G_OBJECT(w),"from_message",from_str); } - gtk_text_buffer_get_end_iter(buffer,&iter); - gtk_text_buffer_get_iter_at_offset(buffer,&begin,off); - gtk_text_buffer_get_end_iter(buffer,&iter); - gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,message,-1,"margin",me ? "bg":NULL,NULL); + gtk_text_buffer_get_end_iter(buffer,&iter); + gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,linphone_chat_message_get_text(msg),-1,"margin",me ? "bg":NULL,NULL); gtk_text_buffer_get_end_iter(buffer,&iter); - gtk_text_buffer_insert(buffer,&iter,"\n",-1); - gtk_text_buffer_get_bounds (buffer, &begin, &end); - GHashTable *hash=(GHashTable *)g_object_get_data(G_OBJECT(linphone_gtk_get_main_window()),"history"); - if(me){ - g_hash_table_insert(hash,linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(cr)), - (gpointer)gtk_text_buffer_get_text(buffer,&begin,&end,FALSE)); - } else { - g_hash_table_insert(hash,linphone_address_as_string_uri_only(from), - (gpointer)gtk_text_buffer_get_text(buffer,&begin,&end,FALSE)); - } - g_object_set_data(G_OBJECT(linphone_gtk_get_main_window()),"history",hash); - - + gtk_text_buffer_insert(buffer,&iter,"\n",-1);; gtk_text_buffer_get_end_iter(buffer,&iter); - if(me){ - list=g_list_append(list,GINT_TO_POINTER(gtk_text_iter_get_line(&iter))); - gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending .. ",-1, - "italic","right","small","font_grey","bg",NULL); - g_object_set_data(G_OBJECT(w),"list",list); - } else { - struct tm *tm=localtime(&t); - char buf[80]; - strftime(buf,80,"Send at %H:%M",tm); - gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,buf,-1, - "italic","right","small","font_grey",NULL); + t=linphone_chat_message_get_time(msg); + switch (linphone_chat_message_get_state (msg)){ + case LinphoneChatMessageStateInProgress: + { + list=g_list_append(list,GINT_TO_POINTER(gtk_text_iter_get_line(&iter))); + gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending .. ",-1, + "right","small","italic","font_grey","bg",NULL); + g_object_set_data(G_OBJECT(w),"list",list); + break; + } + case LinphoneChatMessageStateDelivered: + { + struct tm *tm=localtime(&t); + char buf[80]; + strftime(buf,80,"%H:%M",tm); + gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,buf,-1, + "right","small","italic","font_grey",me ? "bg":NULL,NULL); + break; + } + case LinphoneChatMessageStateNotDelivered: + gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Error",-1, + "right","small","italic","font_grey",me ? "bg":NULL,NULL); + break; + default : gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending ..",-1, + "right","small","italic","font_grey",me ? "bg":NULL,NULL); } gtk_text_buffer_get_end_iter(buffer,&iter); gtk_text_buffer_insert(buffer,&iter,"\n",-1); GtkTextMark *mark=gtk_text_buffer_create_mark(buffer,NULL,&iter,FALSE); - gtk_text_view_scroll_mark_onscreen(text,mark); + gtk_text_view_scroll_mark_onscreen(text,mark); } const LinphoneAddress* linphone_gtk_get_used_identity(){ @@ -160,8 +156,6 @@ const LinphoneAddress* linphone_gtk_get_used_identity(){ else return linphone_core_get_primary_contact_parsed(lc); } - -/* function in dev for displaying ack*/ void update_chat_state_message(LinphoneChatMessageState state,LinphoneChatMessage *msg){ GtkWidget *main_window=linphone_gtk_get_main_window(); GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list"); @@ -181,7 +175,7 @@ void update_chat_state_message(LinphoneChatMessageState state,LinphoneChatMessag gtk_text_buffer_get_iter_at_line_offset(b,&start, GPOINTER_TO_INT(g_list_nth_data(list,0)), gtk_text_iter_get_chars_in_line(&iter)-1); - }else { + }else{ gtk_text_buffer_get_iter_at_line_offset(b,&start, GPOINTER_TO_INT(g_list_nth_data(list,0)),0); } @@ -209,7 +203,7 @@ void update_chat_state_message(LinphoneChatMessageState state,LinphoneChatMessag default : result="Sending .."; } gtk_text_buffer_insert_with_tags_by_name(b,&iter,result,-1, - "italic","right","small","font_grey","bg",NULL); + "right","small","italic","font_grey","bg",NULL); list=g_list_remove(list,g_list_nth_data(list,0)); g_object_set_data(G_OBJECT(page),"list",list); } @@ -231,59 +225,66 @@ void linphone_gtk_send_text(){ LinphoneChatMessage *msg; msg=linphone_chat_room_create_message(cr,entered); linphone_chat_room_send_message2(cr,msg,on_chat_state_changed,NULL); - linphone_gtk_push_text(w, - linphone_gtk_get_used_identity(), - entered,TRUE,cr,linphone_chat_message_get_time(msg)); + linphone_gtk_push_text(w,linphone_gtk_get_used_identity(), + TRUE,cr,msg,FALSE); gtk_entry_set_text(GTK_ENTRY(entry),""); } } +void display_history_message(GtkWidget *chat_view,MSList *messages,const LinphoneAddress *with){ + if(messages != NULL){ + MSList *it; + char *from_str; + char *with_str; + for(it=messages;it!=NULL;it=it->next){ + LinphoneChatMessage *msg=(LinphoneChatMessage *)it->data; + from_str=linphone_address_as_string_uri_only(linphone_chat_message_get_from(msg)); + with_str=linphone_address_as_string_uri_only(with); + linphone_gtk_push_text(chat_view,strcmp(from_str,with_str)==0? with : + linphone_chat_message_get_from(msg), + strcmp(from_str,with_str)==0? FALSE : TRUE, + linphone_chat_message_get_chat_room(msg),msg,TRUE); + } + g_object_set_data(G_OBJECT(chat_view),"from_message",NULL); + ms_free(from_str); + ms_free(with_str); + } +} + GtkWidget* linphone_gtk_init_chatroom(LinphoneChatRoom *cr, const LinphoneAddress *with){ GtkWidget *chat_view=linphone_gtk_create_widget("main","chatroom_frame"); GtkWidget *main_window=linphone_gtk_get_main_window (); - GHashTable *hash=g_object_get_data(G_OBJECT(main_window),"history"); GtkNotebook *notebook=(GtkNotebook *)linphone_gtk_get_widget(main_window,"viewswitch"); GtkWidget *text=linphone_gtk_get_widget(chat_view,"textview"); GdkColor color; + GdkColor colorb; int idx; - + GtkWidget *button; + GtkWidget *entry; + GList *list=NULL; + MSList *messages; + char *with_str; + color.red = 32512; color.green = 32512; color.blue = 32512; - - GdkColor colorb; colorb.red = 56832; colorb.green = 60928; colorb.blue = 61952; - gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW(text),GTK_WRAP_WORD); - gtk_text_view_set_editable (GTK_TEXT_VIEW(text),FALSE); - gtk_notebook_append_page (notebook,chat_view,create_tab_chat_header(cr,with)); + with_str=linphone_address_as_string_uri_only(with); + linphone_core_set_messages_flag_read(cr,with_str,1); + gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text),GTK_WRAP_WORD_CHAR); + gtk_text_view_set_editable(GTK_TEXT_VIEW(text),FALSE); + gtk_notebook_append_page(notebook,chat_view,create_tab_chat_header(cr,with)); idx = gtk_notebook_page_num(notebook, chat_view); gtk_notebook_set_current_page(notebook, idx); gtk_widget_show(chat_view); - g_object_set_data(G_OBJECT(chat_view),"cr",cr); g_object_set_data(G_OBJECT(chat_view),"idx",GINT_TO_POINTER(idx)); g_object_set_data(G_OBJECT(chat_view),"from_message",NULL); - g_object_set_data(G_OBJECT(chat_view),"from_chatroom",(gpointer) with); - - GList *list=NULL; g_object_set_data(G_OBJECT(chat_view),"list",list); - - gchar *buf=g_hash_table_lookup(hash,linphone_address_as_string_uri_only(with)); - if(buf != NULL){ - GtkTextIter start; - GtkTextIter end; - - GtkTextBuffer *text_buffer; - text_buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)); - gtk_text_buffer_get_bounds(text_buffer, &start, &end); - gtk_text_buffer_delete (text_buffer, &start, &end); - gtk_text_buffer_insert(text_buffer,&start,buf,-1); - } - gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)), "right","justification", GTK_JUSTIFY_RIGHT,NULL); gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)), @@ -300,13 +301,13 @@ GtkWidget* linphone_gtk_init_chatroom(LinphoneChatRoom *cr, const LinphoneAddres "margin","indent",10,NULL); gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)), "bg","paragraph-background-gdk",&colorb,NULL); - - GtkWidget *button = linphone_gtk_get_widget(chat_view,"send"); + messages = linphone_chat_room_get_history(with_str,cr,10); + display_history_message(chat_view,messages,with); + button = linphone_gtk_get_widget(chat_view,"send"); g_signal_connect_swapped(G_OBJECT(button),"clicked",(GCallback)linphone_gtk_send_text,NULL); - - GtkWidget *entry = linphone_gtk_get_widget(chat_view,"text_entry"); + entry = linphone_gtk_get_widget(chat_view,"text_entry"); g_signal_connect_swapped(G_OBJECT(entry),"activate",(GCallback)linphone_gtk_send_text,NULL); - + ms_free(with_str); return chat_view; } @@ -318,31 +319,31 @@ LinphoneChatRoom * linphone_gtk_create_chatroom(const LinphoneAddress *with){ void linphone_gtk_load_chatroom(LinphoneChatRoom *cr,const LinphoneAddress *uri,GtkWidget *chat_view){ GtkWidget *main_window=linphone_gtk_get_main_window (); - GHashTable *hash=g_object_get_data(G_OBJECT(main_window),"history"); - LinphoneAddress *from=(LinphoneAddress *)g_object_get_data(G_OBJECT(chat_view),"from_chatroom"); - if(g_strcmp0(linphone_address_as_string(from),linphone_address_as_string(uri))!=0) - { + LinphoneChatRoom *cr2=(LinphoneChatRoom *)g_object_get_data(G_OBJECT(chat_view),"cr"); + char *from_str=linphone_address_as_string(linphone_chat_room_get_peer_address (cr2)); + char *uri_str=linphone_address_as_string(uri); + char *uri_only=linphone_address_as_string_uri_only(uri); + MSList *messages=NULL; + + linphone_core_set_messages_flag_read(cr,uri_only,1); + if(g_strcmp0(from_str,uri_str)!=0){ GtkTextView *text_view=GTK_TEXT_VIEW(linphone_gtk_get_widget(chat_view,"textview")); GtkTextIter start; GtkTextIter end; - gchar *buf=g_hash_table_lookup(hash,linphone_address_as_string_uri_only(uri)); - GtkTextBuffer *text_buffer; + GtkTextBuffer *text_buffer; + text_buffer=gtk_text_view_get_buffer(text_view); gtk_text_buffer_get_bounds(text_buffer, &start, &end); - g_object_set_data(G_OBJECT(chat_view),"cr",cr); gtk_text_buffer_delete (text_buffer, &start, &end); - if(buf!=NULL){ - gtk_text_buffer_insert_with_tags_by_name(text_buffer,&start,buf,-1,"font_grey",NULL); - GtkTextMark *mark=gtk_text_buffer_create_mark(text_buffer, NULL, &start, FALSE); - gtk_text_view_scroll_to_mark(text_view,mark, 0, FALSE, 0, 0); - } - udpate_tab_chat_header(chat_view,uri,cr); g_object_set_data(G_OBJECT(chat_view),"cr",cr); - g_object_set_data(G_OBJECT(chat_view),"from_chatroom",(gpointer) uri); - g_object_set_data(G_OBJECT(chat_view),"from_message",linphone_address_as_string_uri_only(uri)); g_object_set_data(G_OBJECT(linphone_gtk_get_widget(main_window,"contact_list")),"chatview",(gpointer)chat_view); + messages = linphone_chat_room_get_history(uri_only,cr,10); + g_object_set_data(G_OBJECT(chat_view),"from_message",uri_str); + display_history_message(chat_view,messages,uri); } + ms_free(from_str); + ms_free(uri_str); } void linphone_gtk_chat_destroyed(GtkWidget *w){ @@ -360,8 +361,7 @@ void linphone_gtk_text_received(LinphoneCore *lc, LinphoneChatRoom *room, LinphoneChatMessage *msg){ GtkWidget *main_window=linphone_gtk_get_main_window(); GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list"); - GtkWidget *w; - + GtkWidget *w; w=(GtkWidget*)g_object_get_data(G_OBJECT(friendlist),"chatview"); if(w!=NULL){ @@ -371,12 +371,7 @@ void linphone_gtk_text_received(LinphoneCore *lc, LinphoneChatRoom *room, g_object_set_data(G_OBJECT(friendlist),"chatview",(gpointer)w); g_object_set_data(G_OBJECT(friendlist),"from",(gpointer)linphone_chat_message_get_from(msg)); } - - const char *display=linphone_address_get_display_name(linphone_chat_message_get_from(msg)); - if (display==NULL || display[0]=='\0') { - display=linphone_address_get_username(linphone_chat_message_get_from(msg)); - } - + get_display_name(linphone_chat_message_get_from(msg)); #ifdef HAVE_GTK_OSXs /* Notified when a new message is sent */ linphone_gtk_status_icon_set_blinking(TRUE); @@ -391,7 +386,7 @@ void linphone_gtk_text_received(LinphoneCore *lc, LinphoneChatRoom *room, } #endif linphone_gtk_push_text(w,linphone_chat_message_get_from(msg), - linphone_chat_message_get_text(msg),FALSE,room,linphone_chat_message_get_time(msg)); + FALSE,room,msg,FALSE); linphone_gtk_update_chat_picture(); //gtk_window_present(GTK_WINDOW(w)); /*gtk_window_set_urgency_hint(GTK_WINDOW(w),TRUE);*/ diff --git a/gtk/main.c b/gtk/main.c index b31396b0..b1dab3d6 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -1556,10 +1556,6 @@ static void linphone_gtk_configure_main_window(){ static gboolean buttons_have_borders; static gboolean show_abcd; GtkWidget *w=linphone_gtk_get_main_window(); - GHashTable *contacts_history; - - contacts_history=g_hash_table_new_full(g_str_hash, g_str_equal,g_free, NULL); - g_object_set_data(G_OBJECT(w),"history",(gpointer)contacts_history); if (!config_loaded){ title=linphone_gtk_get_ui_config("title","Linphone"); -- 2.39.2