From ccad52f971510e791b6c25cf2038e450781ce262 Mon Sep 17 00:00:00 2001
From: Andrea Gianarda <andrea.gianarda@belledonne-communications.com>
Date: Fri, 6 Nov 2020 12:03:19 +0100
Subject: [PATCH] Add methods to convert from chat room state to conference
 state and viceversa

---
 coreapi/private_functions.h        | 10 ++++++
 include/linphone/api/c-chat-room.h |  1 -
 src/c-wrapper/api/c-chat-room.cpp  | 54 +++++++++++++++++++++++++++++-
 src/chat/chat-room/chat-room.cpp   |  7 ++--
 4 files changed, 67 insertions(+), 5 deletions(-)

diff --git a/coreapi/private_functions.h b/coreapi/private_functions.h
index 74d67075c5..24608a5369 100644
--- a/coreapi/private_functions.h
+++ b/coreapi/private_functions.h
@@ -666,6 +666,16 @@ void linphone_xml_rpc_request_set_current_callbacks(LinphoneXmlRpcRequest *reque
 
 void linphone_core_invalidate_friends_maps(LinphoneCore *lc);
 
+// The following methods are private and they allow a conversion from conference state enum to chat room state enum and viceversa.
+// This allows to easily go from one type to another one ensuring that they are synchronized and ease debugging in case they are not
+// Note that these methods must be updated if either the value of conference state or chat room state change.
+// The compiler will throw an error if not doing it:
+// <pathToLiblinphone>/src/c-wrapper/api/c-chat-room.cpp: In function ‘LinphoneChatRoomState linphone_conference_state_to_chat_room_state(LinphoneConferenceState)’:
+// <pathToLiblinphone>/src/c-wrapper/api/c-chat-room.cpp:676:9: error: enumeration value ‘<name>’ not handled in switch [-Werror=switch]
+LinphoneConferenceState linphone_chat_room_state_to_conference_state(LinphoneChatRoomState state);
+LinphoneChatRoomState linphone_conference_state_to_chat_room_state(LinphoneConferenceState state);
+
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/include/linphone/api/c-chat-room.h b/include/linphone/api/c-chat-room.h
index 976ff94af2..32352a5ffd 100644
--- a/include/linphone/api/c-chat-room.h
+++ b/include/linphone/api/c-chat-room.h
@@ -579,7 +579,6 @@ LINPHONE_PUBLIC LINPHONE_DEPRECATED void linphone_chat_room_send_chat_message(Li
  */
 LINPHONE_PUBLIC LINPHONE_DEPRECATED LinphoneChatMessage* linphone_chat_room_create_message(LinphoneChatRoom *chat_room,const char* message);
 
-
 /**
  * @}
  */
diff --git a/src/c-wrapper/api/c-chat-room.cpp b/src/c-wrapper/api/c-chat-room.cpp
index a0aa8103dd..d3ec7330f0 100644
--- a/src/c-wrapper/api/c-chat-room.cpp
+++ b/src/c-wrapper/api/c-chat-room.cpp
@@ -316,7 +316,7 @@ LinphoneChatMessage *linphone_chat_room_find_message (LinphoneChatRoom *cr, cons
 }
 
 LinphoneChatRoomState linphone_chat_room_get_state (const LinphoneChatRoom *cr) {
-	return (LinphoneChatRoomState)L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getState();
+	return linphone_conference_state_to_chat_room_state(static_cast<LinphoneConferenceState>(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getState()));
 }
 
 bool_t linphone_chat_room_has_been_left (const LinphoneChatRoom *cr) {
@@ -643,3 +643,55 @@ LinphoneChatRoom *_linphone_server_group_chat_room_new (LinphoneCore *core, Linp
 	return NULL;
 #endif
 }
+
+// Convert chat room enum to conference state enum
+LinphoneConferenceState linphone_chat_room_state_to_conference_state(LinphoneChatRoomState state) {
+	// No default statement to trigger an error in case a new value is added to LinphoneChatRoomState
+	switch (state) {
+		case LinphoneChatRoomStateNone:
+			return LinphoneConferenceStateNone;
+		case LinphoneChatRoomStateInstantiated:
+			return LinphoneConferenceStateInstantiated;
+		case LinphoneChatRoomStateCreationPending:
+			return LinphoneConferenceStateCreationPending;
+		case LinphoneChatRoomStateCreated:
+			return LinphoneConferenceStateCreated;
+		case LinphoneChatRoomStateCreationFailed:
+			return LinphoneConferenceStateCreationFailed;
+		case LinphoneChatRoomStateTerminationPending:
+			return LinphoneConferenceStateTerminationPending;
+		case LinphoneChatRoomStateTerminated:
+			return LinphoneConferenceStateTerminated;
+		case LinphoneChatRoomStateTerminationFailed:
+			return LinphoneConferenceStateTerminationFailed;
+		case LinphoneChatRoomStateDeleted:
+			return LinphoneConferenceStateDeleted;
+	}
+	return LinphoneConferenceStateNone;
+}
+
+// Convert conference state enum to chat room state enum
+LinphoneChatRoomState linphone_conference_state_to_chat_room_state(LinphoneConferenceState state) {
+	// No default statement to trigger an error in case a new value is added to LinphoneConferenceState
+	switch (state) {
+		case LinphoneConferenceStateNone:
+			return LinphoneChatRoomStateNone;
+		case LinphoneConferenceStateInstantiated:
+			return LinphoneChatRoomStateInstantiated;
+		case LinphoneConferenceStateCreationPending:
+			return LinphoneChatRoomStateCreationPending;
+		case LinphoneConferenceStateCreated:
+			return LinphoneChatRoomStateCreated;
+		case LinphoneConferenceStateCreationFailed:
+			return LinphoneChatRoomStateCreationFailed;
+		case LinphoneConferenceStateTerminationPending:
+			return LinphoneChatRoomStateTerminationPending;
+		case LinphoneConferenceStateTerminated:
+			return LinphoneChatRoomStateTerminated;
+		case LinphoneConferenceStateTerminationFailed:
+			return LinphoneChatRoomStateTerminationFailed;
+		case LinphoneConferenceStateDeleted:
+			return LinphoneChatRoomStateDeleted;
+	}
+	return LinphoneChatRoomStateNone;
+}
diff --git a/src/chat/chat-room/chat-room.cpp b/src/chat/chat-room/chat-room.cpp
index 683c96d688..a34f33ff95 100644
--- a/src/chat/chat-room/chat-room.cpp
+++ b/src/chat/chat-room/chat-room.cpp
@@ -263,10 +263,11 @@ void ChatRoomPrivate::notifyStateChanged () {
 	if (q->getCore()->getCCore()->state == LinphoneGlobalStartup) {
 		lDebug() << "Chat room [" << q->getConferenceId() << "] state changed to: " << Utils::toString(q->getState());
 	} else {
-		lInfo() << "Chat room [" << q->getConferenceId() << "] state changed to: " << q->getState();
+		lInfo() << "Chat room [" << q->getConferenceId() << "] state changed to: " << Utils::toString(q->getState());
 	}
-	linphone_core_notify_chat_room_state_changed(q->getCore()->getCCore(), cr, (LinphoneChatRoomState)q->getState());
-	_linphone_chat_room_notify_state_changed(cr, (LinphoneChatRoomState)q->getState());
+	LinphoneChatRoomState chatRoomCState = linphone_conference_state_to_chat_room_state(static_cast<LinphoneConferenceState>(q->getState()));
+	linphone_core_notify_chat_room_state_changed(q->getCore()->getCCore(), cr, chatRoomCState);
+	_linphone_chat_room_notify_state_changed(cr, chatRoomCState);
 }
 
 void ChatRoomPrivate::notifyUndecryptableChatMessageReceived (const shared_ptr<ChatMessage> &chatMessage) {
-- 
GitLab