SmartPanelItemsViewModel.cpp 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/***************************************************************************
 * Copyright (C) 2016 by Savoir-faire Linux                                *
 * Author: Jäger 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/>.   *
 **************************************************************************/
#include "pch.h"

#include "SmartPanelItemsViewModel.h"

23 24
#include "configurationmanager_interface.h"

25 26 27 28 29
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Data::Json;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Windows::UI::Core;
30
using namespace Windows::Globalization::DateTimeFormatting;
31 32 33 34 35 36 37 38


using namespace RingClientUWP;
using namespace ViewModel;

SmartPanelItemsViewModel::SmartPanelItemsViewModel()
{
    itemsList_ = ref new Vector<SmartPanelItem^>();
39 40 41
    itemsListFiltered_ = ref new Vector<SmartPanelItem^>();
    itemsListBannedFiltered_ = ref new Vector<SmartPanelItem^>();

42
    RingD::instance->stateChange += ref new RingClientUWP::StateChange(this, &RingClientUWP::ViewModel::SmartPanelItemsViewModel::OnstateChange);
43 44
}

45 46 47 48
bool
SmartPanelItemsViewModel::isInCall()
{
    for (auto item : itemsList) {
49 50
        if (item->_callId && item->_callStatus == CallStatus::IN_PROGRESS)
            return true;
51
    }
52
    return false;
53 54
}

55
SmartPanelItem^
56
SmartPanelItemsViewModel::findItem(String^ callId)
57 58
{
    for each (SmartPanelItem^ item in itemsList)
59
        if (item->_callId == callId)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
            return item;

    return nullptr;
}

SmartPanelItem^
SmartPanelItemsViewModel::findItem(Contact^ contact)
{
    for each (SmartPanelItem^ item in itemsList)
        if (item->_contact == contact)
            return item;

    return nullptr;
}

75 76 77 78 79 80 81 82 83 84
SmartPanelItem^
SmartPanelItemsViewModel::findItemByRingID(String^ ringID)
{
    for each (SmartPanelItem^ item in itemsList)
        if (item->_contact->ringID_ == ringID)
            return item;

    return nullptr;
}

85
unsigned int
86
SmartPanelItemsViewModel::getIndex(String^ callId)
87 88 89
{
    unsigned int i;
    for (i = 0; i < itemsList_->Size; i++) {
90
        if (itemsList_->GetAt(i)->_callId == callId)
91 92 93 94 95 96 97 98 99 100 101 102 103 104
            break;
    }
    return i;
}

unsigned int
SmartPanelItemsViewModel::getIndex(Contact^ contact)
{
    unsigned int i;
    for (i = 0; i < itemsList_->Size; i++) {
        if (itemsList_->GetAt(i)->_contact == contact)
            break;
    }
    return i;
105 106
}

107 108 109 110 111 112 113 114 115 116 117
unsigned int
SmartPanelItemsViewModel::getFilteredIndex(Contact^ contact)
{
    unsigned int i;
    for (i = 0; i < itemsList_->Size; i++) {
        if (itemsListFiltered_->GetAt(i)->_contact == contact)
            break;
    }
    return i;
}

118 119 120 121
void RingClientUWP::ViewModel::SmartPanelItemsViewModel::removeItem(SmartPanelItem ^ item)
{
    unsigned int index;

122
    if (itemsList->IndexOf(item, &index)) {
123
        itemsList->RemoveAt(index);
124 125 126 127 128

        if (itemsListFiltered->IndexOf(item, &index)) {
            itemsListFiltered->RemoveAt(index);
        }
    }
129
}
130 131 132

void RingClientUWP::ViewModel::SmartPanelItemsViewModel::moveItemToTheTop(SmartPanelItem ^ item)
{
atraczyk's avatar
atraczyk committed
133 134 135 136
    unsigned int spi_index, cl_index;

    if (itemsList->IndexOf(item, &spi_index)) {
        if (spi_index != 0) {
137 138 139 140 141 142 143 144 145 146 147
            String^ accountIdAssociated = getAssociatedAccountId(item);
            if (auto contactListModel = AccountsViewModel::instance->getContactListModel(Utils::toString(accountIdAssociated))) {
                auto contactList = contactListModel->_contactsList;
                auto contactListItem = contactListModel->findContactByName(item->_contact->_name);
                contactList->IndexOf(contactListItem, &cl_index);
                contactList->RemoveAt(cl_index);
                contactList->Append(contactListItem);
                contactListModel->saveContactsToFile();

                itemsList->RemoveAt(spi_index);
                itemsList->InsertAt(0, item);
148 149 150 151 152 153
            }
        }
        if (itemsListFiltered->IndexOf(item, &spi_index)) {
            if (spi_index != 0) {
                itemsListFiltered->RemoveAt(spi_index);
                itemsListFiltered->InsertAt(0, item);
154 155
                item->_isHovered = false;
            }
156 157 158
        }
    }
}
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

void RingClientUWP::ViewModel::SmartPanelItemsViewModel::OnstateChange(Platform::String ^callId, RingClientUWP::CallStatus state, int code)
{
    auto item = SmartPanelItemsViewModel::instance->findItem(callId);

    if (!item) {
        WNG_("item not found");
        return;
    }

    item->_callStatus = state;

    Windows::Globalization::Calendar^ calendar = ref new Windows::Globalization::Calendar();
    Windows::Foundation::DateTime dateTime = calendar->GetDateTime();
    auto timestampFormatter = ref new DateTimeFormatter("day month year hour minute second");

    switch (state) {
176 177 178 179
    case CallStatus::NONE:
    {
        break;
    }
180 181 182
    case CallStatus::ENDED:
    {
        item->_contact->_lastTime = "Last call : " + timestampFormatter->Format(dateTime) + ".";
183 184 185
        String^ accountIdAssociated = getAssociatedAccountId(item);
        if (auto contactListModel = AccountsViewModel::instance->getContactListModel(Utils::toString(accountIdAssociated)))
            contactListModel->saveContactsToFile();
186
        refreshFilteredItemsList();
187 188 189 190 191
        break;
    }
    case CallStatus::IN_PROGRESS:
    {
        item->_contact->_lastTime = "in progress.";
192
        item->startCallTimer();
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        break;
    }
    case CallStatus::PEER_PAUSED:
    {
        item->_contact->_lastTime = "paused by "+ item->_contact->_name+".";
        break;
    }
    case CallStatus::PAUSED:
    {
        item->_contact->_lastTime = "paused.";
        break;
    }
    case CallStatus::OUTGOING_REQUESTED:
    case CallStatus::OUTGOING_RINGING:
    case CallStatus::SEARCHING:
    {
        item->_contact->_lastTime = "looking for " + item->_contact->_name + ".";
        break;
    }
    case CallStatus::INCOMING_RINGING:
        item->_contact->_lastTime = "incoming call from " + item->_contact->_name + ".";
214 215 216 217 218
        refreshFilteredItemsList();
        break;
    case CallStatus::AUTO_ANSWERING:
        item->_contact->_lastTime = "in progress.";
        refreshFilteredItemsList();
219 220 221 222 223
        break;
    default:
        break;
    }
}
224 225 226 227 228 229 230 231 232 233 234

String^
SmartPanelItemsViewModel::getAssociatedAccountId(SmartPanelItem^ item)
{
    if (item->_contact->_accountIdAssociated->IsEmpty())
        return AccountListItemsViewModel::instance->_selectedItem->_account->accountID_;
    else
        return item->_contact->_accountIdAssociated;
}

void
235
SmartPanelItemsViewModel::update(const std::vector<std::string>& properties)
236
{
237 238 239 240 241
    for each (SmartPanelItem^ item in itemsListFiltered_) {
        for each (std::string prop in properties) {
            item->raiseNotifyPropertyChanged(Utils::toPlatformString(prop));
            item->_contact->raiseNotifyPropertyChanged(Utils::toPlatformString(prop));
        }
242
    }
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
}

void
SmartPanelItemsViewModel::refreshFilteredItemsList()
{
    auto selectedAccountId = AccountListItemsViewModel::instance->getSelectedAccountId();

    std::for_each(begin(itemsList_), end(itemsList_),
        [selectedAccountId, this](SmartPanelItem^ item) {
        static unsigned spi_index;
        auto isInList = itemsListFiltered->IndexOf(item, &spi_index);
        auto isCall = ( item->_callStatus != CallStatus::NONE &&
                        item->_callStatus != CallStatus::ENDED ) ? true : false;
        auto account = AccountsViewModel::instance->findItem(item->_contact->_accountIdAssociated);
        // 1. Filters out non-account contacts that are NOT incoming calls
        // 2. Filters out account contacts that don't trust us yet
        if ((item->_contact->_accountIdAssociated == selectedAccountId || isCall) &&
            (item->_contact->_isTrusted ||
            (account->_dhtPublicInCalls && item->_contact->_isIncognitoContact &&
             item->_contact->_trustStatus == TrustStatus::INCOMING_CONTACT_REQUEST ||
             item->_contact->_trustStatus == TrustStatus::UNKNOWN))) {
            if (!isInList) {
                if (isCall)
                    itemsListFiltered_->InsertAt(0, item);
                else
                    itemsListFiltered_->Append(item);
            }
        }
        else if (isInList) {
            itemsListFiltered_->RemoveAt(spi_index);
        }
    });
    refreshFilteredBannedItemsList();
}

void
SmartPanelItemsViewModel::refreshFilteredBannedItemsList()
{
    auto selectedAccountId = AccountListItemsViewModel::instance->getSelectedAccountId();

    std::for_each(begin(itemsList_), end(itemsList_),
        [selectedAccountId, this](SmartPanelItem^ item) {
        static unsigned spi_index;
        auto isInList = itemsListBannedFiltered->IndexOf(item, &spi_index);
        auto isBanned = item->_contact->_trustStatus == TrustStatus::BLOCKED ? true : false;
        auto account = AccountsViewModel::instance->findItem(item->_contact->_accountIdAssociated);
        // Filters out non-account contacts that are NOT banned
        if (item->_contact->_accountIdAssociated == selectedAccountId && isBanned) {
            if (!isInList) {
                itemsListBannedFiltered->InsertAt(0, item);
            }
        }
        else if (isInList) {
            itemsListBannedFiltered->RemoveAt(spi_index);
        }
    });
}