From 1ccf89e4fbd6618a9d4a77b54d62c1ce8bb4fa0d Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 9 Aug 2013 17:06:39 +0200 Subject: [PATCH] Init chatrooms at storage startup using sotred conversations + JNI glue to get chatrooms list --- coreapi/chat.c | 9 +++++ coreapi/linphonecore.h | 1 + coreapi/linphonecore_jni.cc | 18 ++++++++++ coreapi/message_storage.c | 34 ++++++++++++++++++- coreapi/private.h | 1 + .../org/linphone/core/LinphoneCore.java | 6 ++++ .../org/linphone/core/LinphoneCoreImpl.java | 14 ++++++++ 7 files changed, 82 insertions(+), 1 deletion(-) diff --git a/coreapi/chat.c b/coreapi/chat.c index 80d00a08e..f94b87862 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -31,6 +31,15 @@ * @{ */ +/** + * Returns an array of chat rooms + * @param lc #LinphoneCore object + * @return An array of #LinpĥoneChatRoom +**/ +MSList* linphone_core_get_chat_rooms(LinphoneCore *lc) { + return lc->chatrooms; +} + /** * Create a new chat room for messaging from a sip uri like sip:joe@sip.linphone.org * @param lc #LinphoneCore object diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index 7fb60b6aa..6f7af4e5d 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -865,6 +865,7 @@ LINPHONE_PUBLIC int linphone_chat_room_get_unread_messages_count(LinphoneChatRoo LINPHONE_PUBLIC LinphoneCore* linphone_chat_room_get_lc(LinphoneChatRoom *cr); LINPHONE_PUBLIC void linphone_chat_room_set_user_data(LinphoneChatRoom *cr, void * ud); LINPHONE_PUBLIC void * linphone_chat_room_get_user_data(LinphoneChatRoom *cr); +LINPHONE_PUBLIC MSList* linphone_core_get_chat_rooms(LinphoneCore *lc); LINPHONE_PUBLIC const char* linphone_chat_message_state_to_string(const LinphoneChatMessageState state); LINPHONE_PUBLIC LinphoneChatMessageState linphone_chat_message_get_state(const LinphoneChatMessage* message); diff --git a/coreapi/linphonecore_jni.cc b/coreapi/linphonecore_jni.cc index 436e1983e..526bd9459 100644 --- a/coreapi/linphonecore_jni.cc +++ b/coreapi/linphonecore_jni.cc @@ -2158,6 +2158,24 @@ extern "C" jboolean Java_org_linphone_core_LinphoneChatMessageImpl_isOutgoing(JN return (jboolean) linphone_chat_message_is_outgoing((LinphoneChatMessage*)ptr); } +extern "C" jlongArray Java_org_linphone_core_LinphoneCoreImpl_getChatRooms(JNIEnv* env + ,jobject thiz + ,jlong ptr) { + MSList* chats = linphone_core_get_chat_rooms((LinphoneCore*)ptr); + int chatsSize = ms_list_size(chats); + jlongArray jChats = env->NewLongArray(chatsSize); + jlong *jInternalArray = env->GetLongArrayElements(jChats, NULL); + + for (int i = 0; i < chatsSize; i++) { + jInternalArray[i] = (unsigned long) (chats->data); + chats = chats->next; + } + + env->ReleaseLongArrayElements(jChats, jInternalArray, 0); + + return jChats; +} + extern "C" void Java_org_linphone_core_LinphoneChatRoomImpl_sendMessage(JNIEnv* env ,jobject thiz ,jlong ptr diff --git a/coreapi/message_storage.c b/coreapi/message_storage.c index 13e46e147..c556e267e 100644 --- a/coreapi/message_storage.c +++ b/coreapi/message_storage.c @@ -76,6 +76,14 @@ static void create_chat_message(char **argv, void *data){ cr->messages_hist=ms_list_prepend(cr->messages_hist,new_message); } +// Called when fetching all conversations from database +static int callback_all(void *data, int argc, char **argv, char **colName){ + LinphoneCore* lc = (LinphoneCore*) data; + char* address = argv[0]; + linphone_core_create_chat_room(lc, address); + return 0; +} + static int callback(void *data, int argc, char **argv, char **colName){ create_chat_message(argv,data); return 0; @@ -94,13 +102,24 @@ void linphone_sql_request_message(sqlite3 *db,const char *stmt,LinphoneChatRoom void linphone_sql_request(sqlite3* db,const char *stmt){ char* errmsg=NULL; int ret; - ret=sqlite3_exec(db,stmt,0,0,&errmsg); + ret=sqlite3_exec(db,stmt,NULL,NULL,&errmsg); if(ret != SQLITE_OK) { ms_error("linphone_sql_request: error sqlite3_exec(): %s.\n", errmsg); sqlite3_free(errmsg); } } +// Process the request to fetch all chat contacts +void linphone_sql_request_all(sqlite3* db,const char *stmt, LinphoneCore* lc){ + char* errmsg=NULL; + int ret; + ret=sqlite3_exec(db,stmt,callback_all,lc,&errmsg); + if(ret != SQLITE_OK) { + ms_error("linphone_sql_request_all: error sqlite3_exec(): %s.\n", errmsg); + sqlite3_free(errmsg); + } +} + void linphone_chat_message_store(LinphoneChatMessage *msg){ LinphoneCore *lc=linphone_chat_room_get_lc(msg->chat_room); if (lc->db){ @@ -205,6 +224,13 @@ void linphone_create_table(sqlite3* db){ } } +void linphone_message_storage_init_chat_rooms(LinphoneCore *lc) { + if (lc->db==NULL) return NULL; + char *buf=sqlite3_mprintf("SELECT remoteContact FROM history Group By remoteContact;"); + linphone_sql_request_all(lc->db,buf,lc); + sqlite3_free(buf); +} + void linphone_core_message_storage_init(LinphoneCore *lc){ int ret; const char *errmsg; @@ -217,6 +243,9 @@ void linphone_core_message_storage_init(LinphoneCore *lc){ } linphone_create_table(db); lc->db=db; + + // Create a chatroom for each contact in the chat history + linphone_message_storage_init_chat_rooms(lc); } void linphone_core_message_storage_close(LinphoneCore *lc){ @@ -244,6 +273,9 @@ MSList *linphone_chat_room_get_history(LinphoneChatRoom *cr,int nb_message){ void linphone_chat_room_delete_history(LinphoneChatRoom *cr){ } +void linphone_message_storage_init_chat_rooms(LinphoneCore *lc) { +} + void linphone_core_message_storage_init(LinphoneCore *lc){ } diff --git a/coreapi/private.h b/coreapi/private.h index cae6254b0..e5e7113aa 100644 --- a/coreapi/private.h +++ b/coreapi/private.h @@ -730,6 +730,7 @@ void linphone_upnp_destroy(LinphoneCore *lc); #ifdef MSG_STORAGE_ENABLED sqlite3 * linphone_message_storage_init(); +void linphone_message_storage_init_chat_rooms(LinphoneCore *lc); #endif void linphone_chat_message_store(LinphoneChatMessage *msg); void linphone_chat_message_store_state(LinphoneChatMessage *msg); diff --git a/java/common/org/linphone/core/LinphoneCore.java b/java/common/org/linphone/core/LinphoneCore.java index 81b11d721..b763107d7 100644 --- a/java/common/org/linphone/core/LinphoneCore.java +++ b/java/common/org/linphone/core/LinphoneCore.java @@ -1320,4 +1320,10 @@ public interface LinphoneCore { * @param path the database where the chat messages will be stored. */ public void setChatDatabasePath(String path); + + /** + * Gets the chat rooms + * @return an array of LinphoneChatRoom + */ + public LinphoneChatRoom[] getChatRooms(); } diff --git a/java/impl/org/linphone/core/LinphoneCoreImpl.java b/java/impl/org/linphone/core/LinphoneCoreImpl.java index c822d6149..bc9d3d1ba 100644 --- a/java/impl/org/linphone/core/LinphoneCoreImpl.java +++ b/java/impl/org/linphone/core/LinphoneCoreImpl.java @@ -133,6 +133,7 @@ class LinphoneCoreImpl implements LinphoneCore { private native void setInCallTimeout(long nativePtr, int timeout); private native void setPrimaryContact(long nativePtr, String displayName, String username); private native void setChatDatabasePath(long nativePtr, String path); + private native long[] getChatRooms(long nativePtr); LinphoneCoreImpl(LinphoneCoreListener listener, File userConfig,File factoryConfig,Object userdata) throws IOException { mListener=listener; @@ -984,4 +985,17 @@ class LinphoneCoreImpl implements LinphoneCore { public void setChatDatabasePath(String path) { setChatDatabasePath(nativePtr, path); } + + public synchronized LinphoneChatRoom[] getChatRooms() { + long[] typesPtr = getChatRooms(nativePtr); + if (typesPtr == null) return null; + + LinphoneChatRoom[] proxies = new LinphoneChatRoom[typesPtr.length]; + + for (int i=0; i < proxies.length; i++) { + proxies[i] = new LinphoneChatRoomImpl(typesPtr[i]); + } + + return proxies; + } }