diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 98304a28c..ef57a4de0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -178,6 +178,7 @@ set(LINPHONE_CXX_OBJECTS_SOURCE_FILES content/content-manager.cpp content/content-type.cpp content/content.cpp + core/core-chat-room.cpp core/core.cpp core/paths/paths.cpp core/platform-helpers/platform-helpers.cpp diff --git a/src/core/core-chat-room.cpp b/src/core/core-chat-room.cpp new file mode 100644 index 000000000..ac50b37fa --- /dev/null +++ b/src/core/core-chat-room.cpp @@ -0,0 +1,162 @@ +/* + * core-chat-room.cpp + * Copyright (C) 2010-2017 Belledonne Communications SARL + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include + +#include "chat/chat-room/basic-chat-room.h" +#include "chat/chat-room/chat-room-p.h" +#include "chat/chat-room/real-time-text-chat-room.h" +#include "core-p.h" +#include "logger/logger.h" + +// TODO: Remove me later. +#include "c-wrapper/c-wrapper.h" + +// ============================================================================= + +using namespace std; + +LINPHONE_BEGIN_NAMESPACE + +// ----------------------------------------------------------------------------- +// Helpers. +// ----------------------------------------------------------------------------- + +static inline Address getCleanedPeerAddress (const Address &peerAddress) { + Address cleanedAddress = peerAddress; + cleanedAddress.clean(); + cleanedAddress.setPort(0); + return cleanedAddress; +} + +// ----------------------------------------------------------------------------- + +shared_ptr CorePrivate::createChatRoom (const Address &peerAddress, bool isRtt) { + shared_ptr chatRoom; + + if (isRtt) + chatRoom = ObjectFactory::create(cCore, peerAddress); + else + chatRoom = ObjectFactory::create(cCore, peerAddress); + + ChatRoomPrivate *dChatRoom = chatRoom->getPrivate(); + dChatRoom->setState(ChatRoom::State::Instantiated); + dChatRoom->setState(ChatRoom::State::Created); + + return chatRoom; +} + +void CorePrivate::insertChatRoom (const shared_ptr &chatRoom) { + L_ASSERT(chatRoom); + L_ASSERT(chatRoom->getState() == ChatRoom::State::Created); + + string peerAddress = getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(); + deleteChatRoom(peerAddress); + + chatRooms.push_back(chatRoom); + chatRoomsByUri[peerAddress] = chatRoom; +} + +void CorePrivate::deleteChatRoom (const string &peerAddress) { + auto it = chatRoomsByUri.find(peerAddress); + if (it != chatRoomsByUri.end()) + chatRooms.erase( + find_if(chatRooms.begin(), chatRooms.end(), [&peerAddress](const shared_ptr &chatRoom) { + return peerAddress == chatRoom->getPeerAddress().asStringUriOnly(); + }) + ); +} + +void CorePrivate::insertChatRoomWithDb (const shared_ptr &chatRoom) { + insertChatRoom(chatRoom); + mainDb->insertChatRoom( + getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(), + chatRoom->getCapabilities() + ); +} + +void CorePrivate::deleteChatRoomWithDb (const string &peerAddress) { + deleteChatRoom(peerAddress); + mainDb->deleteChatRoom(peerAddress); +} + +// ----------------------------------------------------------------------------- + +const list> &Core::getChatRooms () const { + L_D(); + return d->chatRooms; +} + +shared_ptr Core::findChatRoom (const Address &peerAddress) const { + L_D(); + auto it = d->chatRoomsByUri.find(getCleanedPeerAddress(peerAddress).asStringUriOnly()); + return it == d->chatRoomsByUri.cend() ? shared_ptr() : it->second; +} + +shared_ptr Core::createClientGroupChatRoom (const string &subject) { + L_D(); + + const char *factoryUri = linphone_core_get_conference_factory_uri(d->cCore); + if (!factoryUri) + return nullptr; + + return L_GET_CPP_PTR_FROM_C_OBJECT( + _linphone_client_group_chat_room_new(d->cCore, factoryUri, L_STRING_TO_C(subject)) + ); +} + +shared_ptr Core::getOrCreateBasicChatRoom (const Address &peerAddress, bool isRtt) { + L_D(); + + if (!peerAddress.isValid()) { + lWarning() << "Cannot find get or create chat room with invalid peer address."; + return nullptr; + } + + shared_ptr chatRoom = findChatRoom(peerAddress); + if (chatRoom) + return chatRoom; + + chatRoom = d->createChatRoom(peerAddress, isRtt); + d->insertChatRoomWithDb(chatRoom); + + return chatRoom; +} + +shared_ptr Core::getOrCreateBasicChatRoom (const string &peerAddress, bool isRtt) { + L_D(); + + LinphoneAddress *address = linphone_core_interpret_url(d->cCore, L_STRING_TO_C(peerAddress)); + if (!address) { + lError() << "Cannot make a valid address with: `" << peerAddress << "`."; + return nullptr; + } + + shared_ptr chatRoom = getOrCreateBasicChatRoom(*L_GET_CPP_PTR_FROM_C_OBJECT(address), isRtt); + linphone_address_unref(address); + return chatRoom; +} + +void Core::deleteChatRoom (const shared_ptr &chatRoom) { + CorePrivate *d = chatRoom->getCore()->cppCore->getPrivate(); + string peerAddress = getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(); + d->deleteChatRoomWithDb(peerAddress); +} + +LINPHONE_END_NAMESPACE diff --git a/src/core/core.cpp b/src/core/core.cpp index f13d69707..152276c9a 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -17,96 +17,21 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include -#include - -#include "chat/chat-room/basic-chat-room.h" -#include "chat/chat-room/chat-room-p.h" -#include "chat/chat-room/real-time-text-chat-room.h" #include "core-p.h" -#include "db/main-db.h" #include "logger/logger.h" -#include "object/object-p.h" #include "paths/paths.h" // TODO: Remove me later. #include "c-wrapper/c-wrapper.h" -#include "private.h" - -#define LINPHONE_DB "linphone.db" // ============================================================================= +#define LINPHONE_DB "linphone.db" + using namespace std; LINPHONE_BEGIN_NAMESPACE -// ----------------------------------------------------------------------------- -// Helpers. -// ----------------------------------------------------------------------------- - -static inline Address getCleanedPeerAddress (const Address &peerAddress) { - Address cleanedAddress = peerAddress; - cleanedAddress.clean(); - cleanedAddress.setPort(0); - return cleanedAddress; -} - -// ----------------------------------------------------------------------------- -// CorePrivate: ChatRoom. -// ----------------------------------------------------------------------------- - -shared_ptr CorePrivate::createChatRoom (const Address &peerAddress, bool isRtt) { - shared_ptr chatRoom; - - if (isRtt) - chatRoom = ObjectFactory::create(cCore, peerAddress); - else - chatRoom = ObjectFactory::create(cCore, peerAddress); - - ChatRoomPrivate *dChatRoom = chatRoom->getPrivate(); - dChatRoom->setState(ChatRoom::State::Instantiated); - dChatRoom->setState(ChatRoom::State::Created); - - return chatRoom; -} - -void CorePrivate::insertChatRoom (const shared_ptr &chatRoom) { - L_ASSERT(chatRoom); - L_ASSERT(chatRoom->getState() == ChatRoom::State::Created); - - string peerAddress = getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(); - deleteChatRoom(peerAddress); - - chatRooms.push_back(chatRoom); - chatRoomsByUri[peerAddress] = chatRoom; -} - -void CorePrivate::deleteChatRoom (const string &peerAddress) { - auto it = chatRoomsByUri.find(peerAddress); - if (it != chatRoomsByUri.end()) - chatRooms.erase( - find_if(chatRooms.begin(), chatRooms.end(), [&peerAddress](const shared_ptr &chatRoom) { - return peerAddress == chatRoom->getPeerAddress().asStringUriOnly(); - }) - ); -} - -void CorePrivate::insertChatRoomWithDb (const shared_ptr &chatRoom) { - insertChatRoom(chatRoom); - mainDb->insertChatRoom( - getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(), - chatRoom->getCapabilities() - ); -} - -void CorePrivate::deleteChatRoomWithDb (const string &peerAddress) { - deleteChatRoom(peerAddress); - mainDb->deleteChatRoom(peerAddress); -} - -// ============================================================================= - Core::Core (LinphoneCore *cCore) : Object(*new CorePrivate) { L_D(); d->cCore = cCore; @@ -131,10 +56,6 @@ Core::Core (LinphoneCore *cCore) : Object(*new CorePrivate) { d->insertChatRoom(chatRoom); } -// ----------------------------------------------------------------------------- -// Paths. -// ----------------------------------------------------------------------------- - string Core::getDataPath() const { L_D(); return Paths::getPath(Paths::Data, static_cast(d->cCore->platform_helper)); @@ -145,71 +66,4 @@ string Core::getConfigPath() const { return Paths::getPath(Paths::Config, static_cast(d->cCore->platform_helper)); } -// ----------------------------------------------------------------------------- -// ChatRoom. -// ----------------------------------------------------------------------------- - -const list> &Core::getChatRooms () const { - L_D(); - return d->chatRooms; -} - -shared_ptr Core::findChatRoom (const Address &peerAddress) const { - L_D(); - auto it = d->chatRoomsByUri.find(getCleanedPeerAddress(peerAddress).asStringUriOnly()); - return it == d->chatRoomsByUri.cend() ? shared_ptr() : it->second; -} - -shared_ptr Core::createClientGroupChatRoom (const string &subject) { - L_D(); - - const char *factoryUri = linphone_core_get_conference_factory_uri(d->cCore); - if (!factoryUri) - return nullptr; - - return L_GET_CPP_PTR_FROM_C_OBJECT( - _linphone_client_group_chat_room_new(d->cCore, factoryUri, L_STRING_TO_C(subject)) - ); -} - -shared_ptr Core::getOrCreateBasicChatRoom (const Address &peerAddress, bool isRtt) { - L_D(); - - if (!peerAddress.isValid()) { - lWarning() << "Cannot find get or create chat room with invalid peer address."; - return nullptr; - } - - shared_ptr chatRoom = findChatRoom(peerAddress); - if (chatRoom) - return chatRoom; - - chatRoom = d->createChatRoom(peerAddress, isRtt); - d->insertChatRoomWithDb(chatRoom); - - return chatRoom; -} - -shared_ptr Core::getOrCreateBasicChatRoom (const string &peerAddress, bool isRtt) { - L_D(); - - LinphoneAddress *address = linphone_core_interpret_url(d->cCore, L_STRING_TO_C(peerAddress)); - if (!address) { - lError() << "Cannot make a valid address with: `" << peerAddress << "`."; - return nullptr; - } - - shared_ptr chatRoom = getOrCreateBasicChatRoom(*L_GET_CPP_PTR_FROM_C_OBJECT(address), isRtt); - linphone_address_unref(address); - return chatRoom; -} - -void Core::deleteChatRoom (const shared_ptr &chatRoom) { - CorePrivate *d = chatRoom->getCore()->cppCore->getPrivate(); - string peerAddress = getCleanedPeerAddress(chatRoom->getPeerAddress()).asStringUriOnly(); - d->deleteChatRoomWithDb(peerAddress); -} - -// ----------------------------------------------------------------------------- - LINPHONE_END_NAMESPACE