AccountsViewModel.cpp 7.17 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
    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
AccountsViewModel::raiseContactAdded(String^ accountId, Contact ^ name)
{
    contactAdded(accountId, name);
}

void
AccountsViewModel::raiseContactDeleted(String^ accountId, Contact ^ name)
{
    contactDeleted(accountId, name);
}

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

57
void
58
AccountsViewModel::addRingAccount(std::string& alias, std::string& ringID, std::string& accountID, std::string& deviceId, bool upnpState)
59
{
Nicolas Jager's avatar
Nicolas Jager committed
60
    auto account = ref new Account(
61
                       Utils::toPlatformString(alias),
Nicolas Jager's avatar
Nicolas Jager committed
62
                       Utils::toPlatformString(ringID),
63
                       "RING",
Nicolas Jager's avatar
Nicolas Jager committed
64
                       Utils::toPlatformString(accountID),
65
                       Utils::toPlatformString(deviceId),
66
67
68
69
70
71
                       upnpState,
                       "" /* sip hostame not used woth ring account */,
                       "" /* sip username not used with ring account */,
                       "" /* sip password not used with ring */ );

    accountsList_->Append(account);
72
    contactListModels_->Insert(account->accountID_, ref new ContactListModel(account->accountID_));
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    updateScrollView();
    accountAdded(account);
}

void
AccountsViewModel::addSipAccount(std::string& alias, std::string& accountID, 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 */,
                       false /* upnpn not used with sip */,
                       Utils::toPlatformString(sipHostname),
                       Utils::toPlatformString(sipUsername),
                       Utils::toPlatformString(sipPassword)
                   );
91

Nicolas Jager's avatar
Nicolas Jager committed
92
    accountsList_->Append(account);
93
    contactListModels_->Insert(account->accountID_, ref new ContactListModel(account->accountID_));
atraczyk's avatar
atraczyk committed
94
    updateScrollView();
Nicolas Jager's avatar
Nicolas Jager committed
95
    accountAdded(account);
96
97
98
99
100
101
}

void
AccountsViewModel::clearAccountList()
{
    accountsList_->Clear();
Nicolas Jager's avatar
Nicolas Jager committed
102
    clearAccountsList();
103
104
105
106
107
108
109
110
111
112
}

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

    return nullptr;
}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
163
164
165
166
167
168
169
170
171
172
173
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

ContactListModel^
AccountsViewModel::getContactListModel(std::string& accountId)
{
    try {
        return contactListModels_->Lookup(Utils::toPlatformString(accountId));
    }
    catch (Platform::OutOfBoundsException^ e) {
        EXC_(e);
        return nullptr;
    }
}

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

void
AccountsViewModel::OnincomingAccountMessage(String ^ accountId, String ^ fromRingId, String ^ payload)
{
    auto contactListModel = getContactListModel(Utils::toString(accountId));

    auto contact = contactListModel->findContactByRingId(fromRingId);

    if (contact == nullptr)
        contact = contactListModel->addNewContact(fromRingId, fromRingId);

    auto item = SmartPanelItemsViewModel::instance->_selectedItem;

    if (contact == nullptr) {
        ERR_("contact not handled!");
        return;
    }

    RingD::instance->lookUpAddress(fromRingId);

    contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_CONTACT, payload);

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

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

    if (contact->ringID_ == fromRingId && contact != selectedContact) {
        contact->_unreadMessages++;
        newUnreadMessage();
        /* saveContactsToFile used to save the notification */
        contactListModel->saveContactsToFile();
    }
}

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;

        contact->_conversation->addMessage(""/* date not yet used*/, MSG_FROM_CONTACT, payload);

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

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

        if (contact != selectedContact) {
            contact->_unreadMessages++;
            newUnreadMessage();
            /* saveContactsToFile used to save the notification */
            contactListModel->saveContactsToFile();
        }
    }
}