blob: f63215326644af2eb4ddb9a1ba910ec8bfe9f619 [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"
26#include "fileutils.h"
27
28#include "account_schema.h"
29
30#include "SmartPanel.xaml.h"
31
32using namespace Windows::ApplicationModel::Core;
33using namespace Windows::Storage;
34using namespace Windows::UI::Core;
35
36using namespace RingClientUWP;
37using namespace RingClientUWP::Utils;
38
39void
40debugOutputWrapper(const std::string& str)
41{
42 MSG_(str);
43}
44
45void
46RingClientUWP::RingD::reloadAccountList()
47{
48 RingClientUWP::ViewModel::AccountsViewModel::instance->clearAccountList();
49 std::vector<std::string> accountList = DRing::getAccountList();
50 std::vector<std::string>::reverse_iterator rit = accountList.rbegin();
51 for (; rit != accountList.rend(); ++rit) {
52 std::map<std::string,std::string> accountDetails = DRing::getAccountDetails(*rit);
53 std::string ringID(accountDetails.find(ring::Conf::CONFIG_ACCOUNT_USERNAME)->second);
54 if(!ringID.empty())
55 ringID = ringID.substr(5);
56 RingClientUWP::ViewModel::AccountsViewModel::instance->add(
57 accountDetails.find(ring::Conf::CONFIG_ACCOUNT_ALIAS)->second, //name
58 ringID, //ringid
atraczyk797fa1a2016-08-31 09:55:53 -040059 accountDetails.find(ring::Conf::CONFIG_ACCOUNT_TYPE)->second, //type
60 *rit);
atraczykb724d332016-08-30 15:25:59 -040061 }
62 // load user preferences
63 Configuration::UserPreferences::instance->load();
64}
65
66void
67RingClientUWP::RingD::startDaemon()
68{
69 create_task([&]()
70 {
71 using SharedCallback = std::shared_ptr<DRing::CallbackWrapperBase>;
72 using namespace std::placeholders;
73
74 std::map<std::string, SharedCallback> callHandlers = {
75 // use IncomingCall only to register the call client sided, use StateChange to determine the impact on the UI
76 DRing::exportable_callback<DRing::CallSignal::IncomingCall>([this](
77 const std::string& accountId,
78 const std::string& callId,
79 const std::string& from)
80 {
81 MSG_("<IncomingCall>");
82 MSG_("accountId = " + accountId);
83 MSG_("callId = " + callId);
84 MSG_("from = " + from);
85
86 auto accountId2 = toPlatformString(accountId);
87 auto callId2 = toPlatformString(callId);
88 auto from2 = toPlatformString(from);
89
90 incomingCall(accountId2, callId2, from2);
91
92 }),
93 DRing::exportable_callback<DRing::CallSignal::StateChange>([this](
94 const std::string& callId,
95 const std::string& state,
96 int code)
97 {
98 MSG_("<StateChange>");
99 MSG_("callId = " + callId);
100 MSG_("state = " + state);
101 MSG_("code = " + std::to_string(code));
102
103 auto callId2 = toPlatformString(callId);
104 auto state2 = toPlatformString(state);
105
106 stateChange(callId2, state2, code);
107
108 }),
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400109 DRing::exportable_callback<DRing::ConfigurationSignal::IncomingAccountMessage>([&](
atraczykb724d332016-08-30 15:25:59 -0400110 const std::string& accountId,
111 const std::string& from,
112 const std::map<std::string, std::string>& payloads)
113 {
114 MSG_("<IncomingAccountMessage>");
115 MSG_("accountId = " + accountId);
116 MSG_("from = " + from);
117
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400118 auto accountId2 = toPlatformString(accountId);
119 auto from2 = toPlatformString(from);
120
atraczykb724d332016-08-30 15:25:59 -0400121 for (auto i : payloads) {
122 MSG_("payload = " + i.second);
123 auto payload = Utils::toPlatformString(i.second);
Nicolas Jager6e30ad82016-08-26 13:00:27 -0400124 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
125 CoreDispatcherPriority::Low, ref new DispatchedHandler([=]()
126 {
127 incomingAccountMessage(accountId2, from2, payload);
128 }));
atraczykb724d332016-08-30 15:25:59 -0400129 }
130 }),
131 DRing::exportable_callback<DRing::ConfigurationSignal::AccountsChanged>([this]()
132 {
133 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal,
134 ref new DispatchedHandler([=]() {
135 reloadAccountList();
136 }));
137 })
138 };
139
140 registerCallHandlers(callHandlers);
141
142 std::map<std::string, SharedCallback> dringDebugOutHandler;
143 dringDebugOutHandler.insert(DRing::exportable_callback<DRing::Debug::MessageSend>
144 (std::bind(&debugOutputWrapper, _1)));
145 registerCallHandlers(dringDebugOutHandler);
146
147 std::map<std::string, SharedCallback> getAppPathHandler =
148 {
149 DRing::exportable_callback<DRing::ConfigurationSignal::GetAppDataPath>
150 ([this](std::vector<std::string>* paths) {
151 paths->emplace_back(localFolder_);
152 })
153 };
154 registerCallHandlers(getAppPathHandler);
155
156 DRing::init(static_cast<DRing::InitFlag>(DRing::DRING_FLAG_CONSOLE_LOG |
157 DRing::DRING_FLAG_DEBUG |
158 DRing::DRING_FLAG_AUTOANSWER));
159
160 if (!DRing::start()) {
161 ERR_("\ndaemon didn't start.\n");
162 return;
163 }
164 else {
165 if (!hasConfig)
166 {
167 std::map<std::string, std::string> ringAccountDetails;
168 ringAccountDetails.insert(std::make_pair(ring::Conf::CONFIG_ACCOUNT_ALIAS, accountName));
169 ringAccountDetails.insert(std::make_pair(ring::Conf::CONFIG_ACCOUNT_TYPE,"RING"));
170 DRing::addAccount(ringAccountDetails);
171 }
172 else {
173 CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal,
174 ref new DispatchedHandler([=]() {
175 reloadAccountList();
176 }));
177 }
178 while (true) {
179 DRing::pollEvents();
180 Sleep(1000);
181 dequeueTasks();
182 }
183 DRing::fini();
184 }
185 });
186}
187
188RingD::RingD()
189{
190 localFolder_ = Utils::toString(ApplicationData::Current->LocalFolder->Path);
191}
192
193void
194RingD::dequeueTasks()
195{
196 for (int i = 0; i < tasksList_.size(); i++) {
197 auto task = tasksList_.front();
198 switch (task->request) {
199 case Request::None:
200 default:
201 break;
202 }
203 tasksList_.pop();
204 }
205}