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