blob: 73e96c714f5245ed9af2370d6e7123ffea46b5a8 [file] [log] [blame]
Nicolas Jager6e30ad82016-08-26 13:00:27 -04001/**************************************************************************
atraczykb724d332016-08-30 15:25:59 -04002* Copyright (C) 2016 by Savoir-faire Linux *
3* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
4* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com> *
5* *
6* This program is free software; you can redistribute it and/or modify *
7* it under the terms of the GNU General Public License as published by *
8* the Free Software Foundation; either version 3 of the License, or *
9* (at your option) any later version. *
10* *
11* This program is distributed in the hope that it will be useful, *
12* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14* GNU General Public License for more details. *
15* *
16* You should have received a copy of the GNU General Public License *
17* along with this program. If not, see <http://www.gnu.org/licenses/>. *
18**************************************************************************/
19#include "pch.h"
20
21/* daemon */
22#include <dring.h>
23#include "callmanager_interface.h"
24#include "configurationmanager_interface.h"
25#include "presencemanager_interface.h"
atraczyk14ba30c2016-09-22 18:31:59 -040026#include "videomanager_interface.h"
atraczykb724d332016-08-30 15:25:59 -040027#include "fileutils.h"
atraczykb724d332016-08-30 15:25:59 -040028#include "account_schema.h"
atraczyk196936e2016-09-02 15:31:53 -040029#include "account_const.h"
atraczykb724d332016-08-30 15:25:59 -040030
31#include "SmartPanel.xaml.h"
32
33using namespace Windows::ApplicationModel::Core;
34using namespace Windows::Storage;
35using namespace Windows::UI::Core;
atraczyk14ba30c2016-09-22 18:31:59 -040036using namespace Windows::Media;
37using namespace Windows::Media::MediaProperties;
38using namespace Windows::Media::Capture;
atraczykb724d332016-08-30 15:25:59 -040039
40using namespace RingClientUWP;
41using namespace RingClientUWP::Utils;
Nicolas Jager655df542016-08-31 10:24:47 -040042using namespace RingClientUWP::ViewModel;
atraczykb724d332016-08-30 15:25:59 -040043
44void
atraczykb724d332016-08-30 15:25:59 -040045RingClientUWP::RingD::reloadAccountList()
46{
47 RingClientUWP::ViewModel::AccountsViewModel::instance->clearAccountList();
Nicolas Jager813cf4d2016-10-06 10:54:46 -040048
atraczykb724d332016-08-30 15:25:59 -040049 std::vector<std::string> accountList = DRing::getAccountList();
50 std::vector<std::string>::reverse_iterator rit = accountList.rbegin();
Nicolas Jager813cf4d2016-10-06 10:54:46 -040051
atraczykb724d332016-08-30 15:25:59 -040052 for (; rit != accountList.rend(); ++rit) {
Nicolas Jager813cf4d2016-10-06 10:54:46 -040053
atraczykb724d332016-08-30 15:25:59 -040054 std::map<std::string,std::string> accountDetails = DRing::getAccountDetails(*rit);
atraczyk299aeb92016-09-21 15:13:15 -040055 std::string ringID(accountDetails.find(DRing::Account::ConfProperties::USERNAME)->second);
Nicolas Jager813cf4d2016-10-06 10:54:46 -040056
atraczykb724d332016-08-30 15:25:59 -040057 if(!ringID.empty())
58 ringID = ringID.substr(5);
Nicolas Jager813cf4d2016-10-06 10:54:46 -040059
atraczykb724d332016-08-30 15:25:59 -040060 RingClientUWP::ViewModel::AccountsViewModel::instance->add(
Nicolas Jager813cf4d2016-10-06 10:54:46 -040061 accountDetails.find(DRing::Account::ConfProperties::ALIAS)->second, // alias
62 ringID, // ringid
63 accountDetails.find(DRing::Account::ConfProperties::TYPE)->second, // account type
64 *rit, // account id
65 accountDetails.find(DRing::Account::ConfProperties::RING_DEVICE_ID)->second); // device id
atraczykb724d332016-08-30 15:25:59 -040066 }
Nicolas Jager813cf4d2016-10-06 10:54:46 -040067
atraczykb724d332016-08-30 15:25:59 -040068 // load user preferences
69 Configuration::UserPreferences::instance->load();
70}
71
Nicolas Jager655df542016-08-31 10:24:47 -040072/* nb: send message during conversation not chat video message */
73void RingClientUWP::RingD::sendAccountTextMessage(String^ message)
74{
75 /* account id */
76 auto accountId = AccountsViewModel::instance->selectedAccount->accountID_;
77 std::wstring accountId2(accountId->Begin());
78 std::string accountId3(accountId2.begin(), accountId2.end());
79
80 /* recipient */
Nicolas Jagerc551c362016-10-01 19:24:50 -040081 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
82 auto contact = item->_contact;
Nicolas Jager655df542016-08-31 10:24:47 -040083 auto toRingId = contact->ringID_;
84 std::wstring toRingId2(toRingId->Begin());
85 std::string toRingId3(toRingId2.begin(), toRingId2.end());
86
87 /* payload(s) */
88 std::wstring message2(message->Begin());
89 std::string message3(message2.begin(), message2.end());
90 std::map<std::string, std::string> payloads;
91 payloads["text/plain"] = message3;
92
93 /* daemon */
94 auto sent = DRing::sendAccountTextMessage(accountId3, toRingId3, payloads);
95
96 /* conversation */
97 if (sent) {
98 contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_ME, message);
atraczykf5be5462016-08-31 14:23:06 -040099
100 /* save contacts conversation to disk */
101 contact->saveConversationToFile();
102
Nicolas Jager655df542016-08-31 10:24:47 -0400103 } else {
104 WNG_("message not sent, see daemon outputs");
105 }
106}
107
atraczyk11e7baf2016-10-05 13:08:08 -0400108// send message during video call
109void RingClientUWP::RingD::sendSIPTextMessage(String^ message)
110{
111 /* account id */
112 auto accountId = AccountsViewModel::instance->selectedAccount->accountID_;
113 std::wstring accountId2(accountId->Begin());
114 std::string accountId3(accountId2.begin(), accountId2.end());
115
116 /* call */
117 auto item = SmartPanelItemsViewModel::instance->_selectedItem;
118 auto callId = item->_callId;
119 std::wstring callId2(callId->Begin());
120 std::string callId3(callId2.begin(), callId2.end());
121
122 /* recipient */
123 auto contact = item->_contact;
124
125 /* payload(s) */
126 std::wstring message2(message->Begin());
127 std::string message3(message2.begin(), message2.end());
128 std::map<std::string, std::string> payloads;
129 payloads["text/plain"] = message3;
130
131 /* daemon */
132 DRing::sendTextMessage(callId3, payloads, accountId3, true /*not used*/);
133 contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_ME, message);
134 contact->saveConversationToFile();
135}
136
atraczykb724d332016-08-30 15:25:59 -0400137void
atraczyk196936e2016-09-02 15:31:53 -0400138RingD::createRINGAccount(String^ alias)
139{
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400140 // refactoring : create a dedicated class constructor task and removes accountName from RingD
atraczyk196936e2016-09-02 15:31:53 -0400141 accountName = Utils::toString(alias);
142 tasksList_.push(ref new RingD::Task(Request::AddRingAccount));
143}
144
145void
146RingD::createSIPAccount(String^ alias)
147{
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400148 // refactoring : create a dedicated class constructor task and removes accountName from RingD
atraczyk196936e2016-09-02 15:31:53 -0400149 accountName = Utils::toString(alias);
150 tasksList_.push(ref new RingD::Task(Request::AddSIPAccount));
151}
152
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400153void RingClientUWP::RingD::refuseIncommingCall(String^ callId)
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400154{
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400155 tasksList_.push(ref new RingD::Task(Request::RefuseIncommingCall, callId));
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400156}
157
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400158void RingClientUWP::RingD::acceptIncommingCall(String^ callId)
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400159{
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400160 tasksList_.push(ref new RingD::Task(Request::AcceptIncommingCall, callId));
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400161}
162
Nicolas Jager5750df02016-09-13 11:20:33 -0400163void RingClientUWP::RingD::placeCall(Contact^ contact)
164{
Nicolas Jager9edbea32016-10-03 09:13:53 -0400165 MSG_("!--->> placeCall");
Nicolas Jager5750df02016-09-13 11:20:33 -0400166 auto to = contact->ringID_;
167 auto accountId = AccountsViewModel::instance->selectedAccount->accountID_;
168
169 auto to2 = Utils::toString(to);
170 auto accountId2 = Utils::toString(accountId);
171
172 auto callId2 = DRing::placeCall(accountId2, to2);
173
Nicolas Jager9edbea32016-10-03 09:13:53 -0400174
175
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400176 if (callId2.empty()) {
Nicolas Jager5750df02016-09-13 11:20:33 -0400177 WNG_("call not created, the daemon didn't return a call Id");
178 return;
179 }
180
181 auto callId = Utils::toPlatformString(callId2);
182
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400183 _callIdsList->Append(callId);
Nicolas Jager9edbea32016-10-03 09:13:53 -0400184
185 //auto con = ContactsViewModel::instance->findContactByName(to);
186 auto item = SmartPanelItemsViewModel::instance->findItem(contact);
187 item->_callId = callId;
188 MSG_("$1 place call with id : " + Utils::toString(item->_callId));
189
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400190 callPlaced(callId);
Nicolas Jager9edbea32016-10-03 09:13:53 -0400191
Nicolas Jager9edbea32016-10-03 09:13:53 -0400192}
193
194void RingClientUWP::RingD::cancelOutGoingCall2(String ^ callId)
195{
196 MSG_("$1 cancelOutGoingCall2 : " + Utils::toString(callId));
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400197 tasksList_.push(ref new RingD::Task(Request::HangUpCall, callId));
Nicolas Jager5750df02016-09-13 11:20:33 -0400198}
199
Nicolas Jager121bdf32016-09-13 12:12:15 -0400200
Nicolas Jager9edbea32016-10-03 09:13:53 -0400201void RingClientUWP::RingD::hangUpCall2(String ^ callId)
202{
203 MSG_("$1 hangUpCall2 : "+Utils::toString(callId));
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400204 tasksList_.push(ref new RingD::Task(Request::HangUpCall, callId));
Nicolas Jager9edbea32016-10-03 09:13:53 -0400205}
206
Nicolas Jager813cf4d2016-10-06 10:54:46 -0400207void RingClientUWP::RingD::askToRefreshKnownDevices(String^ accountId)
208{
209 auto task = ref new RingD::Task(Request::GetKnownDevices);
210 task->_accountId = accountId;
211
212 tasksList_.push(task);
213}
214
Nicolas Jager121bdf32016-09-13 12:12:15 -0400215void
atraczykb724d332016-08-30 15:25:59 -0400216RingClientUWP::RingD::startDaemon()
217{
218 create_task([&]()
219 {
220 using SharedCallback = std::shared_ptr<DRing::CallbackWrapperBase>;
221 using namespace std::placeholders;
222
Nicolas Jagerd83bc542016-09-16 12:07:31 -0400223 auto dispatcher = CoreApplication::MainView->CoreWindow->Dispatcher;
224
atraczykb724d332016-08-30 15:25:59 -0400225 std::map<std::string, SharedCallback> callHandlers = {
226 // use IncomingCall only to register the call client sided, use StateChange to determine the impact on the UI
227 DRing::exportable_callback<DRing::CallSignal::IncomingCall>([this](
228 const std::string& accountId,
229 const std::string& callId,
230 const std::string& from)
231 {
232 MSG_("<IncomingCall>");
233 MSG_("accountId = " + accountId);
234 MSG_("callId = " + callId);
235 MSG_("from = " + from);
236
237 auto accountId2 = toPlatformString(accountId);
238 auto callId2 = toPlatformString(callId);
239 auto from2 = toPlatformString(from);
240
Nicolas Jagerf494bda2016-09-16 08:43:43 -0400241 /* fix some issue in the daemon --> <...@...> */
242 from2 = Utils::TrimRingId(from2);
243
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400244 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
Nicolas Jager9edbea32016-10-03 09:13:53 -0400245 CoreDispatcherPriority::High, ref new DispatchedHandler([=]()
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400246 {
247 incomingCall(accountId2, callId2, from2);
Nicolas Jagerc551c362016-10-01 19:24:50 -0400248 stateChange(callId2, CallStatus::INCOMING_RINGING, 0);
Nicolas Jager9edbea32016-10-03 09:13:53 -0400249
Nicolas Jager9edbea32016-10-03 09:13:53 -0400250 auto contact = ContactsViewModel::instance->findContactByName(from2);
251 auto item = SmartPanelItemsViewModel::instance->findItem(contact);
252 item->_callId = callId2;
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400253 }));
atraczykb724d332016-08-30 15:25:59 -0400254 }),
255 DRing::exportable_callback<DRing::CallSignal::StateChange>([this](
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400256 const std::string& callId,
257 const std::string& state,
258 int code)
atraczykb724d332016-08-30 15:25:59 -0400259 {
260 MSG_("<StateChange>");
261 MSG_("callId = " + callId);
262 MSG_("state = " + state);
263 MSG_("code = " + std::to_string(code));
264
265 auto callId2 = toPlatformString(callId);
266 auto state2 = toPlatformString(state);
267
Nicolas Jager813cf4d2016-10-06 10:54:46 -0400268 auto state3 = translateCallStatus(state2);
Nicolas Jagerc551c362016-10-01 19:24:50 -0400269
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400270 if (state3 == CallStatus::ENDED)
271 DRing::hangUp(callId); // solve a bug in the daemon API.
272
atraczykb724d332016-08-30 15:25:59 -0400273
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400274 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
Nicolas Jager9edbea32016-10-03 09:13:53 -0400275 CoreDispatcherPriority::High, ref new DispatchedHandler([=]()
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400276 {
Nicolas Jagerc551c362016-10-01 19:24:50 -0400277 stateChange(callId2, state3, code);
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400278 }));
atraczykb724d332016-08-30 15:25:59 -0400279 }),
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400280 DRing::exportable_callback<DRing::ConfigurationSignal::IncomingAccountMessage>([&](
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400281 const std::string& accountId,
282 const std::string& from,
283 const std::map<std::string, std::string>& payloads)
atraczykb724d332016-08-30 15:25:59 -0400284 {
285 MSG_("<IncomingAccountMessage>");
286 MSG_("accountId = " + accountId);
287 MSG_("from = " + from);
288
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400289 auto accountId2 = toPlatformString(accountId);
290 auto from2 = toPlatformString(from);
291
atraczykb724d332016-08-30 15:25:59 -0400292 for (auto i : payloads) {
293 MSG_("payload = " + i.second);
294 auto payload = Utils::toPlatformString(i.second);
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400295 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
Nicolas Jager9edbea32016-10-03 09:13:53 -0400296 CoreDispatcherPriority::High, ref new DispatchedHandler([=]()
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400297 {
298 incomingAccountMessage(accountId2, from2, payload);
299 }));
atraczykb724d332016-08-30 15:25:59 -0400300 }
301 }),
atraczyk5c395ea2016-09-20 17:28:09 -0400302 DRing::exportable_callback<DRing::CallSignal::IncomingMessage>([&](
303 const std::string& callId,
304 const std::string& from,
305 const std::map<std::string, std::string>& payloads)
306 {
307 MSG_("<IncomingMessage>");
308 MSG_("callId = " + callId);
309 MSG_("from = " + from);
310
311 auto callId2 = toPlatformString(callId);
312 auto from2 = toPlatformString(from);
313
atraczyk5c395ea2016-09-20 17:28:09 -0400314 const std::string PROFILE_VCF = "x-ring/ring.profile.vcard";
315 static const unsigned int profileSize = PROFILE_VCF.size();
316
317 for (auto i : payloads) {
318 if (i.first.compare(0, profileSize, PROFILE_VCF) == 0) {
319 MSG_("VCARD");
320 return;
321 }
322 MSG_("payload.first = " + i.first);
323 MSG_("payload.second = " + i.second);
324
325 auto payload = Utils::toPlatformString(i.second);
326 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
Nicolas Jager9edbea32016-10-03 09:13:53 -0400327 CoreDispatcherPriority::High, ref new DispatchedHandler([=]()
atraczyk5c395ea2016-09-20 17:28:09 -0400328 {
Nicolas Jagerd57809f2016-10-06 11:31:55 -0400329 incomingMessage(callId2, payload);
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400330 MSG_("message recu :" + i.second);
atraczyk5c395ea2016-09-20 17:28:09 -0400331 }));
332 }
333 }),
atraczyk196936e2016-09-02 15:31:53 -0400334 DRing::exportable_callback<DRing::ConfigurationSignal::RegistrationStateChanged>([this](
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400335 const std::string& account_id, const std::string& state,
336 int detailsCode, const std::string& detailsStr)
atraczyk196936e2016-09-02 15:31:53 -0400337 {
338 MSG_("<RegistrationStateChanged>: ID = " + account_id + "state = " + state);
atraczyk1ddcb5a2016-09-07 16:18:30 -0400339 if (state == DRing::Account::States::REGISTERED) {
Nicolas Jager9edbea32016-10-03 09:13:53 -0400340 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400341 ref new DispatchedHandler([=]() {
atraczyk299aeb92016-09-21 15:13:15 -0400342 reloadAccountList();
atraczyk6290cc32016-10-07 08:47:15 -0400343 std::vector<std::string> accountList = DRing::getAccountList();
344 auto last_id = accountList.back();
345 if (!account_id.compare(last_id)) {
346 auto frame = dynamic_cast<Frame^>(Window::Current->Content);
347 dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(false, false);
348 }
atraczyk196936e2016-09-02 15:31:53 -0400349 }));
350 }
351 }),
atraczykb724d332016-08-30 15:25:59 -0400352 DRing::exportable_callback<DRing::ConfigurationSignal::AccountsChanged>([this]()
353 {
Nicolas Jager9edbea32016-10-03 09:13:53 -0400354 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High,
Nicolas Jager655df542016-08-31 10:24:47 -0400355 ref new DispatchedHandler([=]() {
atraczykb724d332016-08-30 15:25:59 -0400356 reloadAccountList();
357 }));
Nicolas Jager5750df02016-09-13 11:20:33 -0400358 }),
Nicolas Jagerd83bc542016-09-16 12:07:31 -0400359 DRing::exportable_callback<DRing::Debug::MessageSend>([&](const std::string& toto)
360 {
Nicolas Jager9edbea32016-10-03 09:13:53 -0400361 dispatcher->RunAsync(CoreDispatcherPriority::High,
Nicolas Jagerd83bc542016-09-16 12:07:31 -0400362 ref new DispatchedHandler([=]() {
363 RingDebug::instance->print(toto);
364 }));
Nicolas Jager813cf4d2016-10-06 10:54:46 -0400365 }),
366
367
368 DRing::exportable_callback<DRing::ConfigurationSignal::KnownDevicesChanged>([&](const std::string& accountId, const std::map<std::string, std::string>& devices)
369 {
370 dispatcher->RunAsync(CoreDispatcherPriority::High,
371 ref new DispatchedHandler([=]() {
372 RingDebug::instance->print("toto");
373 }));
Nicolas Jagerd83bc542016-09-16 12:07:31 -0400374 })
Nicolas Jager813cf4d2016-10-06 10:54:46 -0400375
376
atraczykb724d332016-08-30 15:25:59 -0400377 };
atraczykb724d332016-08-30 15:25:59 -0400378 registerCallHandlers(callHandlers);
379
atraczykb724d332016-08-30 15:25:59 -0400380 std::map<std::string, SharedCallback> getAppPathHandler =
381 {
382 DRing::exportable_callback<DRing::ConfigurationSignal::GetAppDataPath>
383 ([this](std::vector<std::string>* paths) {
384 paths->emplace_back(localFolder_);
385 })
386 };
387 registerCallHandlers(getAppPathHandler);
388
atraczyk14ba30c2016-09-22 18:31:59 -0400389 std::map<std::string, SharedCallback> incomingVideoHandlers =
390 {
391 DRing::exportable_callback<DRing::VideoSignal::DeviceEvent>
392 ([this]() {
393 MSG_("<DeviceEvent>");
394 }),
395 DRing::exportable_callback<DRing::VideoSignal::DecodingStarted>
396 ([this](const std::string &id, const std::string &shmPath, int width, int height, bool isMixer) {
397 MSG_("<DecodingStarted>");
398 Video::VideoManager::instance->rendererManager()->startedDecoding(
399 Utils::toPlatformString(id),
400 width,
401 height);
402 }),
403 DRing::exportable_callback<DRing::VideoSignal::DecodingStopped>
404 ([this](const std::string &id, const std::string &shmPath, bool isMixer) {
405 MSG_("<DecodingStopped>");
406 MSG_("Removing renderer id:" + id);
407 /*auto Id = Utils::toPlatformString(id);
408 auto renderer = Video::VideoManager::instance->rendererManager()->renderer(Id);
409 if (renderer)
410 renderer->isRendering = false;*/
411 Video::VideoManager::instance->rendererManager()->removeRenderer(Utils::toPlatformString(id));
412 })
413 };
414 registerVideoHandlers(incomingVideoHandlers);
415
416 using namespace Video;
417 std::map<std::string, SharedCallback> outgoingVideoHandlers =
418 {
419 DRing::exportable_callback<DRing::VideoSignal::GetCameraInfo>
420 ([this](const std::string& device,
Nicolas Jager9edbea32016-10-03 09:13:53 -0400421 std::vector<std::string> *formats,
422 std::vector<unsigned> *sizes,
423 std::vector<unsigned> *rates) {
atraczyk14ba30c2016-09-22 18:31:59 -0400424 MSG_("\n<GetCameraInfo>\n");
425 auto device_list = VideoManager::instance->captureManager()->deviceList;
426
427 for (unsigned int i = 0; i < device_list->Size; i++) {
428 auto dev = device_list->GetAt(i);
429 if (device == Utils::toString(dev->name())) {
430 auto channel = dev->channel();
431 Vector<Video::Resolution^>^ resolutions = channel->resolutionList();
432 for (auto res : resolutions) {
433 formats->emplace_back(Utils::toString(res->format()));
434 sizes->emplace_back(res->size()->width());
435 sizes->emplace_back(res->size()->height());
436 rates->emplace_back(res->activeRate()->value());
437 }
438 }
439 }
440 }),
441 DRing::exportable_callback<DRing::VideoSignal::SetParameters>
442 ([this](const std::string& device,
443 std::string format,
444 const int width,
445 const int height,
Nicolas Jager9edbea32016-10-03 09:13:53 -0400446 const int rate) {
atraczyk14ba30c2016-09-22 18:31:59 -0400447 MSG_("\n<SetParameters>\n");
448 VideoManager::instance->captureManager()->activeDevice->SetDeviceProperties(
449 Utils::toPlatformString(format),width,height,rate);
450 }),
451 DRing::exportable_callback<DRing::VideoSignal::StartCapture>
452 ([&](const std::string& device) {
453 MSG_("\n<StartCapture>\n");
Nicolas Jager9edbea32016-10-03 09:13:53 -0400454 dispatcher->RunAsync(CoreDispatcherPriority::High,
atraczyk14ba30c2016-09-22 18:31:59 -0400455 ref new DispatchedHandler([=]() {
456 VideoManager::instance->captureManager()->InitializeCameraAsync();
457 VideoManager::instance->captureManager()->videoFrameCopyInvoker->Start();
458 }));
459 }),
460 DRing::exportable_callback<DRing::VideoSignal::StopCapture>
461 ([&]() {
462 MSG_("\n<StopCapture>\n");
Nicolas Jager9edbea32016-10-03 09:13:53 -0400463 dispatcher->RunAsync(CoreDispatcherPriority::High,
atraczyk14ba30c2016-09-22 18:31:59 -0400464 ref new DispatchedHandler([=]() {
465 VideoManager::instance->captureManager()->StopPreviewAsync();
466 if (VideoManager::instance->captureManager()->captureTaskTokenSource)
467 VideoManager::instance->captureManager()->captureTaskTokenSource->cancel();
468 VideoManager::instance->captureManager()->videoFrameCopyInvoker->Stop();
469 }));
470 })
471 };
472 registerVideoHandlers(outgoingVideoHandlers);
473
Nicolas Jageraef65722016-09-06 08:30:14 -0400474 DRing::init(static_cast<DRing::InitFlag>(DRing::DRING_FLAG_CONSOLE_LOG |
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400475 DRing::DRING_FLAG_DEBUG));
atraczykb724d332016-08-30 15:25:59 -0400476
477 if (!DRing::start()) {
478 ERR_("\ndaemon didn't start.\n");
479 return;
480 }
481 else {
Nicolas Jagere4eb5132016-10-04 14:25:15 -0400482 switch (_startingStatus) {
483 case StartingStatus::REGISTERING_ON_THIS_PC:
484 {
atraczyk4464ace2016-09-01 09:37:37 -0400485 tasksList_.push(ref new RingD::Task(Request::AddRingAccount));
486 tasksList_.push(ref new RingD::Task(Request::AddSIPAccount));
Nicolas Jagere4eb5132016-10-04 14:25:15 -0400487 break;
atraczykb724d332016-08-30 15:25:59 -0400488 }
Nicolas Jagere4eb5132016-10-04 14:25:15 -0400489 case StartingStatus::REGISTERING_THIS_DEVICE:
490 {
491 tasksList_.push(ref new RingD::Task(Request::RegisterDevice, _pin, _password));
492 break;
493 }
494 case StartingStatus::NORMAL:
495 default:
496 {
497 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal,
Nicolas Jager655df542016-08-31 10:24:47 -0400498 ref new DispatchedHandler([=]() {
atraczykb724d332016-08-30 15:25:59 -0400499 reloadAccountList();
500 }));
Nicolas Jagere4eb5132016-10-04 14:25:15 -0400501 break;
atraczykb724d332016-08-30 15:25:59 -0400502 }
Nicolas Jagere4eb5132016-10-04 14:25:15 -0400503 }
504
atraczykb724d332016-08-30 15:25:59 -0400505 while (true) {
506 DRing::pollEvents();
atraczykb724d332016-08-30 15:25:59 -0400507 dequeueTasks();
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400508 Sleep(1000);
atraczykb724d332016-08-30 15:25:59 -0400509 }
510 DRing::fini();
511 }
512 });
513}
514
515RingD::RingD()
516{
517 localFolder_ = Utils::toString(ApplicationData::Current->LocalFolder->Path);
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400518 callIdsList_ = ref new Vector<String^>();
atraczykb724d332016-08-30 15:25:59 -0400519}
520
521void
522RingD::dequeueTasks()
523{
524 for (int i = 0; i < tasksList_.size(); i++) {
525 auto task = tasksList_.front();
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400526 auto request = dynamic_cast<Task^>(task)->request;
527 switch (request) {
atraczykb724d332016-08-30 15:25:59 -0400528 case Request::None:
atraczyk4464ace2016-09-01 09:37:37 -0400529 break;
530 case Request::AddRingAccount:
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400531 {
532 std::map<std::string, std::string> ringAccountDetails;
atraczyk299aeb92016-09-21 15:13:15 -0400533 ringAccountDetails.insert(std::make_pair(DRing::Account::ConfProperties::ALIAS, accountName));
534 ringAccountDetails.insert(std::make_pair(DRing::Account::ConfProperties::ARCHIVE_PASSWORD, accountName));
535 ringAccountDetails.insert(std::make_pair(DRing::Account::ConfProperties::TYPE,"RING"));
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400536 DRing::addAccount(ringAccountDetails);
537 }
538 break;
atraczyk4464ace2016-09-01 09:37:37 -0400539 case Request::AddSIPAccount:
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400540 {
541 std::map<std::string, std::string> sipAccountDetails;
atraczyk299aeb92016-09-21 15:13:15 -0400542 sipAccountDetails.insert(std::make_pair(DRing::Account::ConfProperties::ALIAS, accountName + " (SIP)"));
543 sipAccountDetails.insert(std::make_pair(DRing::Account::ConfProperties::TYPE,"SIP"));
Nicolas Jagerd76940f2016-08-31 14:44:04 -0400544 DRing::addAccount(sipAccountDetails);
545 }
546 break;
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400547 case Request::RefuseIncommingCall:
548 {
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400549 auto callId = task->_callId;
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400550 auto callId2 = Utils::toString(callId);
551 DRing::refuse(callId2);
552 }
553 break;
554 case Request::AcceptIncommingCall:
555 {
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400556 auto callId = task->_callId;
Nicolas Jagerf6a10322016-09-06 08:17:49 -0400557 auto callId2 = Utils::toString(callId);
558 DRing::accept(callId2);
559 }
560 break;
Nicolas Jager5750df02016-09-13 11:20:33 -0400561 case Request::CancelOutGoingCall:
Nicolas Jager121bdf32016-09-13 12:12:15 -0400562 case Request::HangUpCall:
Nicolas Jager5750df02016-09-13 11:20:33 -0400563 {
Nicolas Jager083b3ae2016-10-04 08:46:19 -0400564 auto callId = task->_callId;
565 DRing::hangUp(Utils::toString(callId));
Nicolas Jager5750df02016-09-13 11:20:33 -0400566 }
567 break;
Nicolas Jagere4eb5132016-10-04 14:25:15 -0400568 case Request::RegisterDevice:
569 {
570 auto pin = Utils::toString(_pin);
571 auto password = Utils::toString(_password);
572
573 std::map<std::string, std::string> deviceDetails;
574 deviceDetails.insert(std::make_pair(DRing::Account::ConfProperties::TYPE, "RING"));
575 //deviceDetails.insert(std::make_pair(DRing::Account::ConfProperties::UPNP_ENABLED, "true"));
576 //deviceDetails.insert(std::make_pair(DRing::Account::ConfProperties::ALIAS, "MonSuperUsername"));
577 deviceDetails.insert(std::make_pair(DRing::Account::ConfProperties::ARCHIVE_PIN, pin));
578 deviceDetails.insert(std::make_pair(DRing::Account::ConfProperties::ARCHIVE_PASSWORD, password));
579 DRing::addAccount(deviceDetails);
580 }
Nicolas Jager813cf4d2016-10-06 10:54:46 -0400581 case Request::GetKnownDevices:
582 {
583 auto accountId = task->_accountId;
584 auto accountId2 = Utils::toString(accountId);
585
586 auto devicesList = DRing::getKnownRingDevices(accountId2);
587 auto devicesList2 = translateKnownRingDevices(devicesList);
588
589 }
atraczykb724d332016-08-30 15:25:59 -0400590 default:
591 break;
592 }
593 tasksList_.pop();
594 }
595}
Nicolas Jagerc551c362016-10-01 19:24:50 -0400596
Nicolas Jager813cf4d2016-10-06 10:54:46 -0400597RingClientUWP::CallStatus RingClientUWP::RingD::translateCallStatus(String^ state)
Nicolas Jagerc551c362016-10-01 19:24:50 -0400598{
599 if (state == "INCOMING")
600 return CallStatus::INCOMING_RINGING;
601
602 if (state == "CURRENT")
603 return CallStatus::IN_PROGRESS;
604
605 if (state == "OVER")
606 return CallStatus::ENDED;
607
608 if (state == "RINGING")
609 return CallStatus::OUTGOING_RINGING;
610
611 if (state == "CONNECTING")
612 return CallStatus::SEARCHING;
613
614 return CallStatus::NONE;
615}
Nicolas Jager813cf4d2016-10-06 10:54:46 -0400616
617Vector<String^>^ RingClientUWP::RingD::translateKnownRingDevices(const std::map<std::string, std::string> devices)
618{
619 auto devicesList = ref new Vector<String^>();
620
621 for (auto i : devices) {
622 MSG_("devices.first = " + i.first);
623 MSG_("devices.second = " + i.second);
624 }
625
626
627 return devicesList;
628}