diff --git a/coreapi/chat.c b/coreapi/chat.c index f94b87862..537a4b7ae 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -59,6 +59,28 @@ LinphoneChatRoom * linphone_core_create_chat_room(LinphoneCore *lc, const char * } return NULL; } + +static int chat_room_compare(LinphoneChatRoom* room, const char* to) { + return strcmp(linphone_address_as_string_uri_only(linphone_chat_room_get_peer_address(room)), to); /*return 0 if equals*/ +} + +/** + * Create a new chat room for messaging from a sip uri like sip:joe@sip.linphone.org if not already existing, else return exisiting one + * @param lc #LinphoneCore object + * @param to destination address for messages + * @return #LinphoneChatRoom where messaging can take place. + */ +LinphoneChatRoom* linphone_core_get_or_create_chat_room(LinphoneCore* lc, const char* to) { + if (ms_list_size(lc->chatrooms) == 0) + return linphone_core_create_chat_room(lc, to); + + MSList* found = ms_list_find_custom(lc->chatrooms, (MSCompareFunc) chat_room_compare, to); + if (found != NULL) { + return (LinphoneChatRoom*)found->data; + } else { + return linphone_core_create_chat_room(lc, to); + } +} /** * Destroy a LinphoneChatRoom. diff --git a/coreapi/help/java/org/linphone/core/tutorials/TutorialChatRoom.java b/coreapi/help/java/org/linphone/core/tutorials/TutorialChatRoom.java index 276d291e5..e3594970b 100644 --- a/coreapi/help/java/org/linphone/core/tutorials/TutorialChatRoom.java +++ b/coreapi/help/java/org/linphone/core/tutorials/TutorialChatRoom.java @@ -119,7 +119,7 @@ public class TutorialChatRoom implements LinphoneCoreListener, LinphoneChatMessa try { // Next step is to create a chat room - LinphoneChatRoom chatRoom = lc.createChatRoom(destinationSipAddress); + LinphoneChatRoom chatRoom = lc.getOrCreateChatRoom(destinationSipAddress); // Send message LinphoneChatMessage chatMessage = chatRoom.createLinphoneChatMessage("Hello world"); diff --git a/coreapi/linphonecore.h b/coreapi/linphonecore.h index 6f7af4e5d..2397ec293 100644 --- a/coreapi/linphonecore.h +++ b/coreapi/linphonecore.h @@ -852,6 +852,7 @@ typedef void (*LinphoneChatMessageStateChangeCb)(LinphoneChatMessage* msg,Linpho LINPHONE_PUBLIC void linphone_core_set_chat_database_path(LinphoneCore *lc, const char *path); LINPHONE_PUBLIC LinphoneChatRoom * linphone_core_create_chat_room(LinphoneCore *lc, const char *to); +LINPHONE_PUBLIC LinphoneChatRoom * linphone_core_get_or_create_chat_room(LinphoneCore *lc, const char *to); LINPHONE_PUBLIC LinphoneChatRoom *linphone_core_get_chat_room(LinphoneCore *lc, const LinphoneAddress *addr); LINPHONE_PUBLIC void linphone_chat_room_destroy(LinphoneChatRoom *cr); LINPHONE_PUBLIC LinphoneChatMessage* linphone_chat_room_create_message(LinphoneChatRoom *cr,const char* message); diff --git a/coreapi/linphonecore_jni.cc b/coreapi/linphonecore_jni.cc index 55ea828bd..b1b93d152 100644 --- a/coreapi/linphonecore_jni.cc +++ b/coreapi/linphonecore_jni.cc @@ -1123,13 +1123,13 @@ JNIEXPORT jobject JNICALL Java_org_linphone_core_LinphoneCoreImpl_getPresenceMod RETURN_USER_DATA_OBJECT("PresenceModelImpl", linphone_presence_model, model) } -extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_createChatRoom(JNIEnv* env +extern "C" jlong Java_org_linphone_core_LinphoneCoreImpl_getOrCreateChatRoom(JNIEnv* env ,jobject thiz ,jlong lc ,jstring jto) { const char* to = env->GetStringUTFChars(jto, NULL); - LinphoneChatRoom* lResult = linphone_core_create_chat_room((LinphoneCore*)lc,to); + LinphoneChatRoom* lResult = linphone_core_get_or_create_chat_room((LinphoneCore*)lc,to); env->ReleaseStringUTFChars(jto, to); return (jlong)lResult; } diff --git a/java/common/org/linphone/core/LinphoneCore.java b/java/common/org/linphone/core/LinphoneCore.java index b763107d7..cf587e370 100644 --- a/java/common/org/linphone/core/LinphoneCore.java +++ b/java/common/org/linphone/core/LinphoneCore.java @@ -705,7 +705,7 @@ public interface LinphoneCore { * * @return {@link LinphoneChatRoom} where messaging can take place. */ - LinphoneChatRoom createChatRoom(String to); + LinphoneChatRoom getOrCreateChatRoom(String to); /** * Set the native video window id where the video is to be displayed. * On Android, it must be of type {@link AndroidVideoWindowImpl} diff --git a/java/impl/org/linphone/core/LinphoneCoreImpl.java b/java/impl/org/linphone/core/LinphoneCoreImpl.java index bc9d3d1ba..b28646c7b 100644 --- a/java/impl/org/linphone/core/LinphoneCoreImpl.java +++ b/java/impl/org/linphone/core/LinphoneCoreImpl.java @@ -83,7 +83,7 @@ class LinphoneCoreImpl implements LinphoneCore { private native int getPresenceInfo(long nativePtr); private native void setPresenceModel(long nativePtr, long presencePtr); private native Object getPresenceModel(long nativePtr); - private native long createChatRoom(long nativePtr,String to); + private native long getOrCreateChatRoom(long nativePtr,String to); private native void enableVideo(long nativePtr,boolean vcap_enabled,boolean display_enabled); private native boolean isVideoEnabled(long nativePtr); private native void setFirewallPolicy(long nativePtr, int enum_value); @@ -385,8 +385,8 @@ class LinphoneCoreImpl implements LinphoneCore { public synchronized PresenceModel getPresenceModel() { return (PresenceModel)getPresenceModel(nativePtr); } - public synchronized LinphoneChatRoom createChatRoom(String to) { - return new LinphoneChatRoomImpl(createChatRoom(nativePtr,to)); + public synchronized LinphoneChatRoom getOrCreateChatRoom(String to) { + return new LinphoneChatRoomImpl(getOrCreateChatRoom(nativePtr,to)); } public synchronized void setPreviewWindow(Object w) { setPreviewWindowId(nativePtr,w);