RingD.cpp 14.1 KB
Newer Older
1
/**************************************************************************
atraczyk's avatar
atraczyk committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
* Copyright (C) 2016 by Savoir-faire Linux                                *
* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com>              *
* Author: Traczyk Andreas <traczyk.andreas@savoirfairelinux.com>          *
*                                                                         *
* 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 3 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, see <http://www.gnu.org/licenses/>.   *
**************************************************************************/
#include "pch.h"

/* daemon */
#include <dring.h>
#include "callmanager_interface.h"
#include "configurationmanager_interface.h"
#include "presencemanager_interface.h"
#include "fileutils.h"
#include "account_schema.h"
atraczyk's avatar
atraczyk committed
28
#include "account_const.h"
atraczyk's avatar
atraczyk committed
29 30 31 32 33 34 35 36 37

#include "SmartPanel.xaml.h"

using namespace Windows::ApplicationModel::Core;
using namespace Windows::Storage;
using namespace Windows::UI::Core;

using namespace RingClientUWP;
using namespace RingClientUWP::Utils;
Nicolas Jager's avatar
Nicolas Jager committed
38
using namespace RingClientUWP::ViewModel;
atraczyk's avatar
atraczyk committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

void
debugOutputWrapper(const std::string& str)
{
    MSG_(str);
}

void
RingClientUWP::RingD::reloadAccountList()
{
    RingClientUWP::ViewModel::AccountsViewModel::instance->clearAccountList();
    std::vector<std::string> accountList = DRing::getAccountList();
    std::vector<std::string>::reverse_iterator rit = accountList.rbegin();
    for (; rit != accountList.rend(); ++rit) {
        std::map<std::string,std::string> accountDetails = DRing::getAccountDetails(*rit);
        std::string ringID(accountDetails.find(ring::Conf::CONFIG_ACCOUNT_USERNAME)->second);
        if(!ringID.empty())
            ringID = ringID.substr(5);
        RingClientUWP::ViewModel::AccountsViewModel::instance->add(
            accountDetails.find(ring::Conf::CONFIG_ACCOUNT_ALIAS)->second,      //name
            ringID,                                                             //ringid
60 61
            accountDetails.find(ring::Conf::CONFIG_ACCOUNT_TYPE)->second,       //type
            *rit);
atraczyk's avatar
atraczyk committed
62 63 64 65 66
    }
    // load user preferences
    Configuration::UserPreferences::instance->load();
}

Nicolas Jager's avatar
Nicolas Jager committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/* nb: send message during conversation not chat video message */
void RingClientUWP::RingD::sendAccountTextMessage(String^ message)
{
    /* account id */
    auto accountId = AccountsViewModel::instance->selectedAccount->accountID_;
    std::wstring accountId2(accountId->Begin());
    std::string accountId3(accountId2.begin(), accountId2.end());

    /* recipient */
    auto contact = ContactsViewModel::instance->selectedContact;
    auto toRingId = contact->ringID_;
    std::wstring toRingId2(toRingId->Begin());
    std::string toRingId3(toRingId2.begin(), toRingId2.end());

    /* payload(s) */
    std::wstring message2(message->Begin());
    std::string message3(message2.begin(), message2.end());
    std::map<std::string, std::string> payloads;
    payloads["text/plain"] = message3;

    /* daemon */
    auto sent = DRing::sendAccountTextMessage(accountId3, toRingId3, payloads);

    /* conversation */
    if (sent) {
        contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_ME, message);
93 94 95 96

        /* save contacts conversation to disk */
        contact->saveConversationToFile();

Nicolas Jager's avatar
Nicolas Jager committed
97 98 99 100 101
    } else {
        WNG_("message not sent, see daemon outputs");
    }
}

atraczyk's avatar
atraczyk committed
102 103 104
void
RingD::createRINGAccount(String^ alias)
{
105
    // refactoring : create a dedicated class constructor task and removes accountName from RingD
atraczyk's avatar
atraczyk committed
106 107 108 109 110 111 112
    accountName = Utils::toString(alias);
    tasksList_.push(ref new RingD::Task(Request::AddRingAccount));
}

void
RingD::createSIPAccount(String^ alias)
{
113
    // refactoring : create a dedicated class constructor task and removes accountName from RingD
atraczyk's avatar
atraczyk committed
114 115 116 117
    accountName = Utils::toString(alias);
    tasksList_.push(ref new RingD::Task(Request::AddSIPAccount));
}

118 119 120 121 122 123 124 125 126 127
void RingClientUWP::RingD::refuseIncommingCall(Call^ call)
{
    tasksList_.push(ref new RingD::Task(Request::RefuseIncommingCall, call));
}

void RingClientUWP::RingD::acceptIncommingCall(Call^ call)
{
    tasksList_.push(ref new RingD::Task(Request::AcceptIncommingCall, call));
}

Nicolas Jager's avatar
Nicolas Jager committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
void RingClientUWP::RingD::placeCall(Contact^ contact)
{
    auto to = contact->ringID_;
    auto accountId = AccountsViewModel::instance->selectedAccount->accountID_;

    auto to2 = Utils::toString(to);
    auto accountId2 = Utils::toString(accountId);

    auto callId2 = DRing::placeCall(accountId2, to2);

    if (callId2 == "") {
        WNG_("call not created, the daemon didn't return a call Id");
        return;
    }

    auto callId = Utils::toPlatformString(callId2);

    auto call = CallsViewModel::instance->addNewCall(accountId, callId, to);
    call->isOutGoing = true;

    if (call == nullptr) {
        WNG_("call not created, nullptr reason");
        return;
    }

    calling(call);

}

void
RingClientUWP::RingD::cancelOutGoingCall(Call^ call)
{
    tasksList_.push(ref new RingD::Task(Request::CancelOutGoingCall, call));
}

atraczyk's avatar
atraczyk committed
163 164 165
void
RingClientUWP::RingD::startDaemon()
{
166 167 168 169
    // TODO (during refactoring) : use namespace
    /* clear the calls list and instantiate the singleton (required)  */
    RingClientUWP::ViewModel::CallsViewModel::instance->clearCallsList();

atraczyk's avatar
atraczyk committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    create_task([&]()
    {
        using SharedCallback = std::shared_ptr<DRing::CallbackWrapperBase>;
        using namespace std::placeholders;

        std::map<std::string, SharedCallback> callHandlers = {
            // use IncomingCall only to register the call client sided, use StateChange to determine the impact on the UI
            DRing::exportable_callback<DRing::CallSignal::IncomingCall>([this](
                const std::string& accountId,
                const std::string& callId,
                const std::string& from)
            {
                MSG_("<IncomingCall>");
                MSG_("accountId = " + accountId);
                MSG_("callId = " + callId);
                MSG_("from = " + from);

                auto accountId2 = toPlatformString(accountId);
                auto callId2 = toPlatformString(callId);
                auto from2 = toPlatformString(from);

191 192 193 194 195 196
                CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
                    CoreDispatcherPriority::Normal, ref new DispatchedHandler([=]()
                {
                    incomingCall(accountId2, callId2, from2);
                    stateChange(callId2, "incoming call", 0);
                }));
atraczyk's avatar
atraczyk committed
197 198
            }),
            DRing::exportable_callback<DRing::CallSignal::StateChange>([this](
199 200 201
                        const std::string& callId,
                        const std::string& state,
                        int code)
atraczyk's avatar
atraczyk committed
202 203 204 205 206 207 208 209 210 211
            {
                MSG_("<StateChange>");
                MSG_("callId = " + callId);
                MSG_("state = " + state);
                MSG_("code = " + std::to_string(code));

                auto callId2 = toPlatformString(callId);
                auto state2 = toPlatformString(state);


212 213 214 215 216
                CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
                    CoreDispatcherPriority::Low, ref new DispatchedHandler([=]()
                {
                    stateChange(callId2, state2, code);
                }));
atraczyk's avatar
atraczyk committed
217
            }),
218
            DRing::exportable_callback<DRing::ConfigurationSignal::IncomingAccountMessage>([&](
219 220 221
                        const std::string& accountId,
                        const std::string& from,
                        const std::map<std::string, std::string>& payloads)
atraczyk's avatar
atraczyk committed
222 223 224 225 226
            {
                MSG_("<IncomingAccountMessage>");
                MSG_("accountId = " + accountId);
                MSG_("from = " + from);

227 228 229
                auto accountId2 = toPlatformString(accountId);
                auto from2 = toPlatformString(from);

atraczyk's avatar
atraczyk committed
230 231 232
                for (auto i : payloads) {
                    MSG_("payload = " + i.second);
                    auto payload = Utils::toPlatformString(i.second);
233 234 235 236 237
                    CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
                        CoreDispatcherPriority::Low, ref new DispatchedHandler([=]()
                    {
                        incomingAccountMessage(accountId2, from2, payload);
                    }));
atraczyk's avatar
atraczyk committed
238 239
                }
            }),
atraczyk's avatar
atraczyk committed
240
            DRing::exportable_callback<DRing::ConfigurationSignal::RegistrationStateChanged>([this](
241 242
                        const std::string& account_id, const std::string& state,
                        int detailsCode, const std::string& detailsStr)
atraczyk's avatar
atraczyk committed
243 244
            {
                MSG_("<RegistrationStateChanged>: ID = " + account_id + "state = " + state);
245
                if (state == DRing::Account::States::REGISTERED) {
atraczyk's avatar
atraczyk committed
246
                    CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal,
247
                    ref new DispatchedHandler([=]() {
248 249
                        auto frame = dynamic_cast<Frame^>(Window::Current->Content);
                        dynamic_cast<RingClientUWP::MainPage^>(frame->Content)->showLoadingOverlay(false, false);
atraczyk's avatar
atraczyk committed
250 251 252
                    }));
                }
            }),
atraczyk's avatar
atraczyk committed
253 254 255
            DRing::exportable_callback<DRing::ConfigurationSignal::AccountsChanged>([this]()
            {
                CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal,
Nicolas Jager's avatar
Nicolas Jager committed
256
                ref new DispatchedHandler([=]() {
atraczyk's avatar
atraczyk committed
257 258
                    reloadAccountList();
                }));
Nicolas Jager's avatar
Nicolas Jager committed
259 260 261 262 263 264 265
            }),
            /* to remove from daemon API, this callback is never used */
            //DRing::exportable_callback<DRing::CallSignal::NewCallCreated>([&](
            //            const std::string& accountId,
            //            const std::string& callId,
            //            const std::string& to)
            //{ /*...*/ })
atraczyk's avatar
atraczyk committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        };

        registerCallHandlers(callHandlers);

        std::map<std::string, SharedCallback> dringDebugOutHandler;
        dringDebugOutHandler.insert(DRing::exportable_callback<DRing::Debug::MessageSend>
                                    (std::bind(&debugOutputWrapper, _1)));
        registerCallHandlers(dringDebugOutHandler);

        std::map<std::string, SharedCallback> getAppPathHandler =
        {
            DRing::exportable_callback<DRing::ConfigurationSignal::GetAppDataPath>
            ([this](std::vector<std::string>* paths) {
                paths->emplace_back(localFolder_);
            })
        };
        registerCallHandlers(getAppPathHandler);

Nicolas Jager's avatar
Nicolas Jager committed
284
        DRing::init(static_cast<DRing::InitFlag>(DRing::DRING_FLAG_CONSOLE_LOG |
285
                    DRing::DRING_FLAG_DEBUG));
atraczyk's avatar
atraczyk committed
286 287 288 289 290 291 292 293

        if (!DRing::start()) {
            ERR_("\ndaemon didn't start.\n");
            return;
        }
        else {
            if (!hasConfig)
            {
294 295
                tasksList_.push(ref new RingD::Task(Request::AddRingAccount));
                tasksList_.push(ref new RingD::Task(Request::AddSIPAccount));
atraczyk's avatar
atraczyk committed
296 297 298
            }
            else {
                CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Normal,
Nicolas Jager's avatar
Nicolas Jager committed
299
                ref new DispatchedHandler([=]() {
atraczyk's avatar
atraczyk committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
                    reloadAccountList();
                }));
            }
            while (true) {
                DRing::pollEvents();
                Sleep(1000);
                dequeueTasks();
            }
            DRing::fini();
        }
    });
}

RingD::RingD()
{
    localFolder_ = Utils::toString(ApplicationData::Current->LocalFolder->Path);
}

void
RingD::dequeueTasks()
{
    for (int i = 0; i < tasksList_.size(); i++) {
        auto task = tasksList_.front();
323 324
        auto request = dynamic_cast<Task^>(task)->request;
        switch (request) {
atraczyk's avatar
atraczyk committed
325
        case Request::None:
326 327
            break;
        case Request::AddRingAccount:
328 329 330 331 332 333 334
        {
            std::map<std::string, std::string> ringAccountDetails;
            ringAccountDetails.insert(std::make_pair(ring::Conf::CONFIG_ACCOUNT_ALIAS, accountName));
            ringAccountDetails.insert(std::make_pair(ring::Conf::CONFIG_ACCOUNT_TYPE,"RING"));
            DRing::addAccount(ringAccountDetails);
        }
        break;
335
        case Request::AddSIPAccount:
336 337 338 339 340 341 342
        {
            std::map<std::string, std::string> sipAccountDetails;
            sipAccountDetails.insert(std::make_pair(ring::Conf::CONFIG_ACCOUNT_ALIAS, accountName + " (SIP)"));
            sipAccountDetails.insert(std::make_pair(ring::Conf::CONFIG_ACCOUNT_TYPE,"SIP"));
            DRing::addAccount(sipAccountDetails);
        }
        break;
343 344 345 346 347 348 349 350 351 352 353 354 355 356
        case Request::RefuseIncommingCall:
        {
            auto callId = task->_call->callId;
            auto callId2 = Utils::toString(callId);
            DRing::refuse(callId2);
        }
        break;
        case Request::AcceptIncommingCall:
        {
            auto callId = task->_call->callId;
            auto callId2 = Utils::toString(callId);
            DRing::accept(callId2);
        }
        break;
Nicolas Jager's avatar
Nicolas Jager committed
357 358 359 360 361 362 363
        case Request::CancelOutGoingCall:
        {
            auto callId = task->_call->callId;
            auto callId2 = Utils::toString(callId);
            DRing::hangUp(callId2);
        }
        break;
atraczyk's avatar
atraczyk committed
364 365 366 367 368 369
        default:
            break;
        }
        tasksList_.pop();
    }
}