AccountsViewModel.cpp 10.7 KB
Newer Older
Nicolas Jager's avatar
Nicolas Jager committed
1
/***************************************************************************
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * Copyright (C) 2016 by Savoir-faire Linux                                *
 * Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com>              *
 * Author: Traczyk Andreas <andreas.traczyk@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/>.   *
 **************************************************************************/

Nicolas Jager's avatar
Nicolas Jager committed
20 21 22 23 24 25
#include "pch.h"
#include "AccountsViewModel.h"

using namespace RingClientUWP;
using namespace ViewModel;

26 27
using namespace Windows::UI::Core;

Nicolas Jager's avatar
Nicolas Jager committed
28 29 30
AccountsViewModel::AccountsViewModel()
{
    accountsList_ = ref new Vector<Account^>();
31 32 33 34 35 36 37 38 39
    contactListModels_ = ref new Map<String^, ContactListModel^>();

    RingD::instance->incomingAccountMessage +=
        ref new RingClientUWP::IncomingAccountMessage(this, &AccountsViewModel::OnincomingAccountMessage);
    RingD::instance->incomingMessage +=
        ref new RingClientUWP::IncomingMessage(this, &AccountsViewModel::OnincomingMessage);
}

void
40
AccountsViewModel::raiseContactAdded(String^ accountId, Contact ^ contact)
41
{
42
    contactAdded(accountId, contact);
43 44 45
}

void
46
AccountsViewModel::raiseContactDeleted(String^ accountId, Contact ^ contact)
47
{
48
    contactDeleted(accountId, contact);
49 50 51
}

void
52
AccountsViewModel::raiseContactDataModified(String^ accountId, Contact ^ contact)
53
{
54
    contactDataModified(accountId, contact);
55
}
Nicolas Jager's avatar
Nicolas Jager committed
56

57
void
58 59 60 61 62 63 64 65 66 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
AccountsViewModel::raiseUnreadContactRequest()
{
    newUnreadContactRequest();
}

void
AccountsViewModel::addRingAccount(  std::string& alias,
                                    std::string& ringID,
                                    std::string& accountID,
                                    std::string& deviceId,
                                    std::string& deviceName,
                                    bool active,
                                    bool upnpState,
                                    bool autoAnswer,
                                    bool dhtPublicInCalls,
                                    bool turnEnabled,
                                    std::string& turnAddress)
{
    auto account = ref new Account( Utils::toPlatformString(alias),
                                    Utils::toPlatformString(ringID),
                                    "RING",
                                    Utils::toPlatformString(accountID),
                                    Utils::toPlatformString(deviceId),
                                    Utils::toPlatformString(deviceName),
                                    active,
                                    upnpState,
                                    autoAnswer,
                                    dhtPublicInCalls,
                                    turnEnabled,
                                    Utils::toPlatformString(turnAddress),
                                    "" /* sip hostame not used with ring account */,
                                    "" /* sip username not used with ring account */,
                                    "" /* sip password not used with ring */ );
    RingD::instance->lookUpAddress(accountID, Utils::toPlatformString(ringID));
    accountsList_->InsertAt(0, account);
93
    contactListModels_->Insert(account->accountID_, ref new ContactListModel(account->accountID_));
94 95 96 97
    accountAdded(account);
}

void
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
AccountsViewModel::addSipAccount(   std::string& alias,
                                    std::string& accountID,
                                    bool active,
                                    std::string& sipHostname,
                                    std::string& sipUsername,
                                    std::string& sipPassword)
{
    auto account = ref new Account( Utils::toPlatformString(alias),
                                    "" /* ring Id not used with sip */ ,
                                    "SIP",
                                    Utils::toPlatformString(accountID),
                                    "" /* device id not used with sip */,
                                    "" /* device name not used with sip */,
                                    active,
                                    false /* upnpn not used with sip */,
                                    false,
                                    true,
                                    false,
                                    "",
                                    Utils::toPlatformString(sipHostname),
                                    Utils::toPlatformString(sipUsername),
                                    Utils::toPlatformString(sipPassword));

    accountsList_->InsertAt(0, account);
122
    contactListModels_->Insert(account->accountID_, ref new ContactListModel(account->accountID_));
Nicolas Jager's avatar
Nicolas Jager committed
123
    accountAdded(account);
124 125 126 127 128 129
}

void
AccountsViewModel::clearAccountList()
{
    accountsList_->Clear();
Nicolas Jager's avatar
Nicolas Jager committed
130
    clearAccountsList();
131 132 133 134 135 136 137 138 139 140
}

Account ^ RingClientUWP::ViewModel::AccountsViewModel::findItem(String ^ accountId)
{
    for each (Account^ item in accountsList_)
        if (item->accountID_ == accountId)
            return item;

    return nullptr;
}
141

142 143 144 145 146 147 148 149 150 151
Account^
AccountsViewModel::findAccountByRingID(String ^ ringId)
{
    for each (Account^ item in accountsList_)
        if (item->ringID_ == ringId)
            return item;

    return nullptr;
}

152 153 154
ContactListModel^
AccountsViewModel::getContactListModel(std::string& accountId)
{
155
    if (contactListModels_->Size)
156
        return contactListModels_->Lookup(Utils::toPlatformString(accountId));
157
    return nullptr;
158 159 160 161 162 163
}

int
AccountsViewModel::unreadMessages(String ^ accountId)
{
    int messageCount = 0;
164
    auto acceptIncognitoMessages = findItem(accountId)->_dhtPublicInCalls;
165 166
    if (auto contactListModel = getContactListModel(Utils::toString(accountId))) {
        for each (auto contact in contactListModel->_contactsList) {
167 168
            if (acceptIncognitoMessages || contact->_trustStatus == TrustStatus::TRUSTED)
                messageCount += contact->_unreadMessages;
169 170 171 172 173
        }
    }
    return messageCount;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
int
AccountsViewModel::unreadContactRequests(String ^ accountId)
{
    int contactRequestCount = 0;
    if (auto contactListModel = getContactListModel(Utils::toString(accountId))) {
        for each (auto contact in contactListModel->_contactsList) {
            if (contact->_trustStatus == TrustStatus::INCOMING_CONTACT_REQUEST) {
                contactRequestCount += contact->_unreadContactRequest ? 1 : 0;
            }
        }
    }
    return contactRequestCount;
}

int
AccountsViewModel::bannedContacts(String^ accountId)
{
    int bannedContacts = 0;
    if (auto contactListModel = getContactListModel(Utils::toString(accountId))) {
        for each (auto contact in contactListModel->_contactsList) {
            if (contact->_trustStatus == TrustStatus::BLOCKED) {
                bannedContacts++;
            }
        }
    }
    return bannedContacts;
}

int
AccountsViewModel::activeAccounts()
{
    int totalActiveAccounts = 0;
    for (auto account : accountsList_) {
        if (account->_active)
            totalActiveAccounts++;
    }
    return totalActiveAccounts;
}

213 214 215 216 217 218 219
void
AccountsViewModel::OnincomingAccountMessage(String ^ accountId, String ^ fromRingId, String ^ payload)
{
    auto contactListModel = getContactListModel(Utils::toString(accountId));

    auto contact = contactListModel->findContactByRingId(fromRingId);

220 221 222 223
    bool itemSelected = false;
    if (auto selectedItem = SmartPanelItemsViewModel::instance->_selectedItem)
        itemSelected = (selectedItem->_contact == contact);
    auto isInBackground = RingD::instance->isInBackground;
224 225

    if (contact == nullptr) {
226 227 228 229 230 231 232 233 234 235 236 237 238 239
        contact = contactListModel->addNewContact(fromRingId, fromRingId, TrustStatus::UNKNOWN, true);
        RingD::instance->lookUpAddress(Utils::toString(accountId), fromRingId);
        if (!itemSelected || isInBackground || RingD::instance->isInvisible) {
            RingD::instance->unpoppedToasts.insert(std::make_pair(contact->ringID_,
                [isInBackground, fromRingId, payload](String^ username) {
                RingD::instance->ShowIMToast(isInBackground, fromRingId, payload, username);
            }));
        }
    }
    else {
        contact->_isIncognitoContact = true;
        if (!itemSelected || isInBackground || RingD::instance->isInvisible) {
            RingD::instance->ShowIMToast(isInBackground, fromRingId, payload);
        }
240 241
    }

242 243
    auto messageId = Utils::toPlatformString(Utils::genID(0LL, 9999999999999999999LL));
    contact->_conversation->addMessage(MSG_FROM_CONTACT, payload, std::time(nullptr), false, messageId);
244 245 246 247

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

248
    auto item = SmartPanelItemsViewModel::instance->_selectedItem;
249
    auto selectedContact = (item) ? item->_contact : nullptr;
250
    newUnreadMessage(contact);
251 252 253 254 255 256 257 258 259 260 261 262 263 264
}

void
AccountsViewModel::OnincomingMessage(String ^callId, String ^payload)
{
    auto itemlist = SmartPanelItemsViewModel::instance->itemsList;
    auto item = SmartPanelItemsViewModel::instance->findItem(callId);
    auto contact = item->_contact;
    auto contactListModel = getContactListModel(Utils::toString(contact->_accountIdAssociated));

    /* the contact HAS TO BE already registered */
    if (contact) {
        auto item = SmartPanelItemsViewModel::instance->_selectedItem;

265 266
        auto messageId = Utils::toPlatformString(Utils::genID(0LL, 9999999999999999999LL));
        contact->_conversation->addMessage(MSG_FROM_CONTACT, payload, std::time(nullptr), false, messageId);
267 268 269 270 271 272

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

        auto selectedContact = (item) ? item->_contact : nullptr;

273
        newUnreadMessage(contact);
274 275
    }
}