ChatRoom.cpp 5.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
ChatRoom.cpp
Copyright (C) 2015  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
14
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
15 16 17 18 19 20 21 22
*/

#include "Address.h"
#include "ApiLock.h"
#include "ChatRoom.h"

#include <collection.h>

23
using namespace BelledonneCommunications::Linphone::Native;
24 25 26 27 28 29 30 31 32
using namespace Platform;
using namespace Platform::Collections;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;


static void AddChatMessageToVector(void *vMessage, void *vector)
{
	::LinphoneChatMessage *chatMessage = (::LinphoneChatMessage*)vMessage;
33 34 35
	RefToPtrProxy<IVector<ChatMessage^>^> *list = reinterpret_cast< RefToPtrProxy<IVector<ChatMessage^>^> *>(vector);
	IVector<ChatMessage^>^ messages = (list) ? list->Ref() : nullptr;
	ChatMessage^ message = (ChatMessage^) Utils::GetChatMessage(chatMessage);
36 37 38
	messages->Append(message);
}

39
IVector<ChatMessage^>^ ChatRoom::History::get()
40 41
{
	API_LOCK;
42
	IVector<ChatMessage^>^ history = ref new Vector<ChatMessage^>();
43
	MSList* messages = linphone_chat_room_get_history(this->room, 0);
44
	RefToPtrProxy<IVector<ChatMessage^>^> *historyPtr = new RefToPtrProxy<IVector<ChatMessage^>^>(history);
45 46 47 48
	ms_list_for_each2(messages, AddChatMessageToVector, historyPtr);
	return history;
}

49
int ChatRoom::HistorySize::get()
50 51 52 53 54
{
	API_LOCK;
	return linphone_chat_room_get_history_size(this->room);
}

55
Platform::Boolean ChatRoom::IsRemoteComposing::get()
56 57 58 59 60
{
	API_LOCK;
	return (linphone_chat_room_is_remote_composing(this->room) == TRUE);
}

61
Address^ ChatRoom::PeerAddress::get()
62 63
{
	API_LOCK;
64
	return (Address^) Utils::CreateAddress((void*)linphone_chat_room_get_peer_address(this->room));
65 66
}
	
67
int ChatRoom::UnreadMessageCount::get()
68 69 70 71 72
{
	API_LOCK;
	return linphone_chat_room_get_unread_messages_count(this->room);
}

73
void ChatRoom::Compose()
74 75 76 77 78
{
	API_LOCK;
	linphone_chat_room_compose(this->room);
}

79
ChatMessage^ ChatRoom::CreateFileTransferMessage(Platform::String^ type, Platform::String^ subtype, Platform::String^ name, int size, Platform::String^ filepath)
80 81
{
	API_LOCK;
82 83 84 85
	const char *ctype = Utils::pstoccs(type);
	const char *csubtype = Utils::pstoccs(subtype);
	const char *cname = Utils::pstoccs(name);
	const char *cfilepath = Utils::pstoccs(filepath);
86 87 88 89 90 91 92 93
	LinphoneContent *content = linphone_core_create_content(linphone_chat_room_get_core(this->room));
	::LinphoneChatMessage *msg;
	linphone_content_set_type(content, ctype);
	linphone_content_set_subtype(content, csubtype);
	linphone_content_set_size(content, size);
	linphone_content_set_name(content, cname);
	msg = linphone_chat_room_create_file_transfer_message(this->room, content);
	linphone_chat_message_set_file_transfer_filepath(msg, cfilepath);
94
	ChatMessage^ chatMessage = (ChatMessage^) Utils::GetChatMessage(msg);
95 96 97 98 99 100 101
	delete(ctype);
	delete(csubtype);
	delete(cname);
	delete(cfilepath);
	return chatMessage;
}

102
ChatMessage^ ChatRoom::CreateMessage(Platform::String^ message)
103 104
{
	API_LOCK;
105
	const char* msg = Utils::pstoccs(message);
106
	::LinphoneChatMessage *cm = linphone_chat_room_create_message(this->room, msg);
107
	ChatMessage^ chatMessage = (ChatMessage^) Utils::GetChatMessage(cm);
108 109 110 111
	delete(msg);
	return chatMessage;
}

112
void ChatRoom::DeleteHistory()
113 114 115 116 117
{
	API_LOCK;
	linphone_chat_room_delete_history(this->room);
}

118
void ChatRoom::DeleteMessage(ChatMessage^ message)
119 120 121 122 123
{
	API_LOCK;
	linphone_chat_room_delete_message(this->room, message->message);
}

124
void ChatRoom::MarkAsRead()
125 126 127 128 129 130 131
{
	API_LOCK;
	linphone_chat_room_mark_as_read(this->room);
}

static void chat_room_callback(::LinphoneChatMessage* msg, ::LinphoneChatMessageState state, void* ud)
{
132 133
	RefToPtrProxy<ChatMessageListener^> *proxy = reinterpret_cast< RefToPtrProxy<ChatMessageListener^> *>(ud);
	ChatMessageListener^ listener = (proxy) ? proxy->Ref() : nullptr;
134 135

	if (listener != nullptr) {
136 137
		ChatMessage^ lChatMessage = (ChatMessage^)Utils::GetChatMessage(msg);
		listener->MessageStateChanged(lChatMessage, (ChatMessageState) state);
138 139 140
	}
}

141
void ChatRoom::SendMessage(ChatMessage^ message, ChatMessageListener^ listener)
142 143 144 145 146 147
{
	API_LOCK;
	RefToPtrProxy<ChatMessageListener^> *listenerPtr = new RefToPtrProxy<ChatMessageListener^>(listener);
	linphone_chat_room_send_message2(this->room, message->message, chat_room_callback, listenerPtr);
}

148
ChatRoom::ChatRoom(::LinphoneChatRoom *cr)
149 150 151 152
	: room(cr)
{
	API_LOCK;
	RefToPtrProxy<ChatRoom^> *chat_room = new RefToPtrProxy<ChatRoom^>(this);
153
	linphone_chat_room_ref(this->room);
154 155 156
	linphone_chat_room_set_user_data(this->room, chat_room);
}

157
ChatRoom::~ChatRoom()
158 159
{
	API_LOCK;
160 161 162
	if (this->room != nullptr) {
		linphone_chat_room_unref(this->room);
	}
163 164 165
	RefToPtrProxy<ChatRoom^> *chat_room = reinterpret_cast< RefToPtrProxy<ChatRoom^> *>(linphone_chat_room_get_user_data(this->room));
	delete chat_room;
}