SmartPanel.xaml.cpp 95.6 KB
Newer Older
1
/***************************************************************************
Nicolas Jager's avatar
Nicolas Jager committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * 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/>.   *
 **************************************************************************/
19

Nicolas Jager's avatar
Nicolas Jager committed
20
21
#include "pch.h"
#include "SmartPanel.xaml.h"
22
23
24
25
#include "ResourceManager.h"
#include "Wizard.xaml.h"
#include "WelcomePage.xaml.h"

26
#include <MemoryBuffer.h>   // IMemoryBufferByteAccess
27
#include "callmanager_interface.h"
28
29
30
31
32
33
34
35
#include "configurationmanager_interface.h"
#include "presencemanager_interface.h"

#include <string>
#include <direct.h>
#include <regex>

#include "lodepng.h"
Nicolas Jager's avatar
Nicolas Jager committed
36
37
38
39

using namespace Platform;

using namespace RingClientUWP;
40
using namespace RingClientUWP::Controls;
Nicolas Jager's avatar
Nicolas Jager committed
41
42
43
using namespace RingClientUWP::Views;
using namespace RingClientUWP::ViewModel;
using namespace Windows::Media::Capture;
44
using namespace Windows::Media::MediaProperties;
Nicolas Jager's avatar
Nicolas Jager committed
45
46
47
using namespace Windows::UI::Xaml;
using namespace Windows::Storage;
using namespace Windows::UI::Xaml::Media::Imaging;
48
using namespace Windows::UI::Xaml::Media::Animation;
Nicolas Jager's avatar
Nicolas Jager committed
49
50
51
52
using namespace Windows::UI::Xaml::Shapes;
using namespace Windows::UI::Xaml::Media;
using namespace Concurrency;
using namespace Windows::Foundation;
53
54
55
using namespace Windows::Graphics::Imaging;
using namespace Windows::Foundation;
using namespace Concurrency;
Nicolas Jager's avatar
Nicolas Jager committed
56
using namespace Platform::Collections;
57
using namespace Windows::UI::Popups;
58
using namespace Windows::ApplicationModel::Core;
59
using namespace Windows::ApplicationModel::DataTransfer;
60
61
using namespace Windows::Storage;
using namespace Windows::UI::Core;
62
using namespace Windows::System::Threading;
63

64
65
using namespace Windows::UI::Xaml::Interop;

Nicolas Jager's avatar
Nicolas Jager committed
66
67
68
69
SmartPanel::SmartPanel()
{
    InitializeComponent();

70
    /* populate account list */
Nicolas Jager's avatar
Nicolas Jager committed
71
    _accountsList_->ItemsSource = AccountListItemsViewModel::instance->itemsList;
72

73
74
75
76
77
78
79
80
81
82
83
84
85
    /* populate account's device list */
    // As the user must have the account in question selected before interacting with
    // it's device list, no filtered list is required.
    _devicesIdList_->ItemsSource = RingDeviceItemsViewModel::instance->itemsList;

    /* populate the contact list */
    _smartList_->ItemsSource = SmartPanelItemsViewModel::instance->itemsListFiltered;

    /* populate banned contact list */
    _bannedContactList_->ItemsSource = SmartPanelItemsViewModel::instance->itemsListBannedFiltered;

    /* populate contact request list */
    _incomingContactRequestList_->ItemsSource = ContactRequestItemsViewModel::instance->itemsListFiltered;
86

87
    /* connect delegates */
88
    Configuration::UserPreferences::instance->selectIndex += ref new SelectIndex([&](int index) {
89
90
        if (_accountsList_) {
            auto accountsListSize = dynamic_cast<Vector<AccountListItem^>^>(_accountsList_->ItemsSource)->Size;
91
            if (accountsListSize > index) {
92
                _accountsList_->SelectedIndex = index;
93
            }
94
95
            else if (accountsListSize) {
                _accountsList_->SelectedIndex = 0;
96
            }
97
            updateNotificationsState();
98
        }
Nicolas Jager's avatar
Nicolas Jager committed
99
    });
100

101
    Configuration::UserPreferences::instance->loadProfileImage += ref new LoadProfileImage([this]() {
102
        MSG_("LOADING PROFILE IMAGE");
103
104
105
106
        StorageFolder^ localfolder = ApplicationData::Current->LocalFolder;
        String^ image_path = localfolder->Path + "\\.profile\\profile_image.png";
        auto uri = ref new Windows::Foundation::Uri(image_path);
        _selectedAccountAvatar_->ImageSource = ref new BitmapImage(uri);
107
        Configuration::UserPreferences::instance->profileImageLoaded = true;
atraczyk's avatar
atraczyk committed
108
    });
atraczyk's avatar
atraczyk committed
109
110
111
112
    AccountsViewModel::instance->updateScrollView += ref new UpdateScrollView([this]() {
        _accountsListScrollView_->UpdateLayout();
        _accountsListScrollView_->ScrollToVerticalOffset(_accountsListScrollView_->ScrollableHeight);
    });
113
114
115
116
117
118
119
120
121

    RingD::instance->networkChanged += ref new NetworkChanged([&]() {
        CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(
            CoreDispatcherPriority::High, ref new DispatchedHandler([=]()
        {
            _networkConnectivityNotificationRow_->Height = RingD::instance->_hasInternet ? GridLength(0.) : GridLength(32.);
        }));
    });

122
    RingD::instance->incomingCall += ref new RingClientUWP::IncomingCall([&](
123
124
125
126
        String^ accountId, String^ callId, String^ from)
    {
        auto contactListModel = AccountsViewModel::instance->getContactListModel(Utils::toString(accountId));
        auto contact = contactListModel->findContactByRingId(from);
127

128
129
130
        if (contact == nullptr) {
            contact = contactListModel->addNewContact(from, from, TrustStatus::UNKNOWN, true);
        }
131
132
133
134
135

        if (contact == nullptr) {
            return;
        }

136
        contact->_isIncognitoContact = true;
137

138
        RingD::instance->lookUpAddress(Utils::toString(accountId), from);
139

140
141
142
143
144
        // buffer a toast
        RingD::instance->unpoppedToasts.insert(std::make_pair(contact->ringID_,
            [callId](String^ username) {
            RingD::instance->ShowCallToast(RingD::instance->isInBackground, callId, username);
        }));
Nicolas Jager's avatar
Nicolas Jager committed
145

146
147
        if (auto item = SmartPanelItemsViewModel::instance->findItem(contact)) {
            item->_callId = callId;
148
149
150
151
            SmartPanelItemsViewModel::instance->moveItemToTheTop(item);
            _smartList_->UpdateLayout();
            _smartList_->ScrollIntoView(item);
        }
152
    });
153
154
155
    AccountsViewModel::instance->newUnreadMessage += ref new NewUnreadMessage(this, &SmartPanel::updateContactNotificationsState);
    AccountsViewModel::instance->contactDataModified += ref new ContactDataModified(this, &SmartPanel::OncontactDataModified);
    AccountsViewModel::instance->newUnreadContactRequest += ref new NewUnreadContactRequest(this, &SmartPanel::updateNotificationsState);
156
    RingD::instance->stateChange += ref new StateChange(this, &SmartPanel::OnstateChange);
157
    RingD::instance->devicesListRefreshed += ref new RingClientUWP::DevicesListRefreshed(this, &RingClientUWP::Views::SmartPanel::OndevicesListRefreshed);
158
    AccountsViewModel::instance->contactAdded += ref new ContactAdded([&](String^ accountId, Contact^ contact) {
159
160
        auto smartPanelItem = ref new SmartPanelItem();
        smartPanelItem->_contact = contact;
atraczyk's avatar
atraczyk committed
161
        SmartPanelItemsViewModel::instance->itemsList->InsertAt(0, smartPanelItem);
162
163
164
        if (contact->_accountIdAssociated == AccountListItemsViewModel::instance->getSelectedAccountId()) {
            SmartPanelItemsViewModel::instance->itemsListFiltered->InsertAt(0, smartPanelItem);
        }
165
    });
Nicolas Jager's avatar
Nicolas Jager committed
166

167
168
169
170
171
172
173
174
175
176
177
    RingD::instance->vCardUpdated += ref new VCardUpdated([&](Contact^ contact)
    {
        Utils::runOnUIThread([this, contact]() {
            SmartPanelItemsViewModel::instance->update({ "_bestName2", "_avatarImage", "_contact" });
        });
    });

    RingD::instance->registrationStateRegistered += ref new RingClientUWP::RegistrationStateRegistered(this, &SmartPanel::OnregistrationStateChanged);
    RingD::instance->registrationStateUnregistered += ref new RingClientUWP::RegistrationStateUnregistered(this, &SmartPanel::OnregistrationStateChanged);
    RingD::instance->registrationStateTrying += ref new RingClientUWP::RegistrationStateTrying(this, &SmartPanel::OnregistrationStateChanged);

178
    RingD::instance->exportOnRingEnded += ref new RingClientUWP::ExportOnRingEnded(this, &RingClientUWP::Views::SmartPanel::OnexportOnRingEnded);
179
    RingD::instance->accountUpdated += ref new RingClientUWP::AccountUpdated(this, &RingClientUWP::Views::SmartPanel::OnaccountUpdated);
Nicolas Jager's avatar
Nicolas Jager committed
180
    RingD::instance->registeredNameFound += ref new RingClientUWP::RegisteredNameFound(this, &RingClientUWP::Views::SmartPanel::OnregisteredNameFound);
Nicolas Jager's avatar
Nicolas Jager committed
181

182
183
184
    RingD::instance->finishCaptureDeviceEnumeration += ref new RingClientUWP::FinishCaptureDeviceEnumeration([this]() {
        populateVideoDeviceSettingsComboBox();
    });
185
186
    RingD::instance->registrationStateErrorGeneric += ref new RingClientUWP::RegistrationStateErrorGeneric(this, &RingClientUWP::Views::SmartPanel::OnregistrationStateErrorGeneric);
    RingD::instance->registrationStateRegistered += ref new RingClientUWP::RegistrationStateRegistered(this, &RingClientUWP::Views::SmartPanel::OnregistrationStateRegistered);
187
    RingD::instance->callPlaced += ref new RingClientUWP::CallPlaced(this, &RingClientUWP::Views::SmartPanel::OncallPlaced);
188
    RingD::instance->incomingAccountMessage += ref new RingClientUWP::IncomingAccountMessage(this, &RingClientUWP::Views::SmartPanel::OnincomingAccountMessage);
Nicolas Jager's avatar
Nicolas Jager committed
189

190
191
    RingD::instance->newBuddyNotification += ref new RingClientUWP::NewBuddyNotification(this, &RingClientUWP::Views::SmartPanel::OnnewBuddyNotification);

192
193
    RingD::instance->nameRegistered += ref new RingClientUWP::NameRegistered(this, &SmartPanel::OnnameRegistred);

194
195
    _networkConnectivityNotificationRow_->Height = Utils::hasInternet() ? 0 : 32;

196
    selectMenu(MenuOpen::CONTACTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
197
198
}

199
200
201
202
203
204
void
SmartPanel::OnnameRegistred(bool status, String ^accountId)
{
    updatePageContent();
}

205
206
207
208
209
210
void
SmartPanel::OnregistrationStateChanged(const std::string& accountId)
{
    AccountListItemsViewModel::instance->update(ViewModel::NotifyStrings::notifyAccountItem);
}

211
212
213
214
215
216
217
218
219
220
221
void
SmartPanel::OnstateChange(Platform::String ^callId, RingClientUWP::CallStatus state, int code)
{
    auto item = SmartPanelItemsViewModel::instance->findItem(callId);

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

    switch (state) {
222
223
224
225
226
    case CallStatus::INCOMING_RINGING:
    {
        updateCallAnimationState(item, true);
        break;
    }
227
228
229
    case CallStatus::NONE:
    case CallStatus::ENDED:
    {
230
        updateCallAnimationState(item, false);
231
        auto callsList = DRing::getCallList();
atraczyk's avatar
atraczyk committed
232
233
        if (callsList.empty())
            _settingsMenuButton_->Visibility = VIS::Visible;
234

235
236
237
238
        break;
    }
    case CallStatus::IN_PROGRESS:
    {
239
        updateCallAnimationState(item, false);
240
        SmartPanelItemsViewModel::instance->_selectedItem = item;
241
242
        _smartList_->SelectedIndex = SmartPanelItemsViewModel::instance->getFilteredIndex(item->_contact);
        _settingsMenuButton_->Visibility = VIS::Collapsed;
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
        summonVideoPage();
        break;
    }
    case CallStatus::PEER_PAUSED:
    case CallStatus::PAUSED:
    {
        SmartPanelItemsViewModel::instance->_selectedItem = item;
        summonVideoPage();
        break;
    }
    default:
        break;
    }
}

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
void
SmartPanel::updateCallAnimationState(SmartPanelItem^ item, bool state)
{
    auto items = _smartList_->Items;
    for (int i = 0; i < items->Size; ++i) {
        if (auto message = dynamic_cast<SmartPanelItem^>(items->GetAt(i))) {
            if (message == item) {
                auto depObj = _smartList_->ItemContainerGenerator->ContainerFromItem(items->GetAt(i));
                auto gridElement = Utils::xaml::FindVisualChildByName(depObj, "_contactAvatarGrid_");
                auto grid = dynamic_cast<Grid^>(gridElement);
                auto ellipse = dynamic_cast<Ellipse^>(grid->Children->GetAt(1));
                auto eventTrigger = dynamic_cast<EventTrigger^>(ellipse->Triggers->GetAt(0));
                auto beginStoryboard = dynamic_cast<BeginStoryboard^>(eventTrigger->Actions->GetAt(0));
                if (beginStoryboard) {
                    if (state)
                        beginStoryboard->Storyboard->Begin();
                    else
                        beginStoryboard->Storyboard->Stop();
                }
            }
        }
    }
}

Nicolas Jager's avatar
Nicolas Jager committed
282
283
284
void
RingClientUWP::Views::SmartPanel::updatePageContent()
{
Nicolas Jager's avatar
Nicolas Jager committed
285
286
    auto accountListItem = AccountListItemsViewModel::instance->_selectedItem;
    if (!accountListItem)
Nicolas Jager's avatar
Nicolas Jager committed
287
288
        return;

289
290
291
292
    _selectedAccountName_->Text = accountListItem->_account->_bestName;
    _selectedAccountUsername_->Text = accountListItem->_account->_bestName2;
    _deviceId_->Text = accountListItem->_account->_deviceId;
    _deviceName_->Text = accountListItem->_account->_deviceName;
293

Nicolas Jager's avatar
Nicolas Jager committed
294
    _devicesMenuButton_->Visibility = (accountListItem->_account->accountType_ == "RING")
295
296
                                      ? Windows::UI::Xaml::Visibility::Visible
                                      : Windows::UI::Xaml::Visibility::Collapsed;
297

298
    _enabledState_->IsOn = accountListItem->_account->_active;
299
    _upnpState_->IsOn = accountListItem->_account->_upnpState;
300
301
302
303
    _autoAnswerToggle_->IsOn = accountListItem->_account->_autoAnswer;
    _dhtPublicInCallsToggle_->IsOn = accountListItem->_account->_dhtPublicInCalls;
    _turnEnabledToggle_->IsOn = accountListItem->_account->_turnEnabled;
    _turnEditionTextBox_->Text = accountListItem->_account->_turnAddress;
Nicolas Jager's avatar
Nicolas Jager committed
304

305
    if (_RegisterStateEdition_->IsOn) {
Nicolas Jager's avatar
Nicolas Jager committed
306
307
308
309
310
311
312
313
        _usernameTextBoxEdition_->IsEnabled = true;
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;

    } else {
        _usernameTextBoxEdition_->IsEnabled = false;
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }

314
315
316
317
318
    ContactRequestItemsViewModel::instance->refreshFilteredItemsList();
    ContactRequestItemsViewModel::instance->update(ViewModel::NotifyStrings::notifyContactRequestItem);

    SmartPanelItemsViewModel::instance->refreshFilteredItemsList();
    SmartPanelItemsViewModel::instance->update(ViewModel::NotifyStrings::notifySmartPanelItem);
Nicolas Jager's avatar
Nicolas Jager committed
319
320
321
322
}

void RingClientUWP::Views::SmartPanel::setMode(RingClientUWP::Views::SmartPanel::Mode mode)
{
323
324
    _contactsTitleRow_->Height = (mode == RingClientUWP::Views::SmartPanel::Mode::Normal) ? 54 : 0;
    _rowRingTxtBx_->Height = (mode == RingClientUWP::Views::SmartPanel::Mode::Normal)? 48 : 0;
325
    selectMenu(MenuOpen::CONTACTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
326
327
328
329
330
331
}

void RingClientUWP::Views::SmartPanel::_addAccountBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
332

333
334
335
    _accountCreationMenuGrid_->UpdateLayout();
    _accountCreationMenuScrollViewer_->ScrollToVerticalOffset(0);

336
    _createAccountYes_->IsEnabled = false;
337
338

    _accountTypeComboBox_->SelectedIndex = 0;
Nicolas Jager's avatar
Nicolas Jager committed
339
340
341
    _RegisterStateEdition_->IsOn = true;
    _accountAliasTextBox_->Text = "";
    _usernameTextBox_->Text = "";
Nicolas Jager's avatar
Nicolas Jager committed
342
343

    checkStateAddAccountMenu();
Nicolas Jager's avatar
Nicolas Jager committed
344
345
346
347
}

void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
348
349
    switch (_accountTypeComboBox_->SelectedIndex) {
    case 0: /* RING account */
350
    {
Nicolas Jager's avatar
Nicolas Jager committed
351
352
        RingD::instance->createRINGAccount(_accountAliasTextBox_->Text
                                           , _ringPasswordBoxAccountCreation_->Password
353
                                           , true
Nicolas Jager's avatar
Nicolas Jager committed
354
355
                                           , (_RegisterState_->IsOn) ? _usernameTextBox_->Text : "");

356
357
        _ringPasswordBoxAccountCreation_->Password = "";
        _ringPasswordBoxAccountCreationCheck_->Password = "";
Nicolas Jager's avatar
Nicolas Jager committed
358
        _usernameTextBox_->Text = "";
atraczyk's avatar
atraczyk committed
359
        break;
360
361
    }
    break;
362
    case 1: /* SIP account */
363
    {
Nicolas Jager's avatar
Nicolas Jager committed
364
        RingD::instance->createSIPAccount(_accountAliasTextBox_->Text, _sipPasswordBoxAccountCreation_->Password, _sipHostnameTextBox_->Text, _sipUsernameTextBox_->Text);
365
366
367
        _sipPasswordBoxAccountCreation_->Password = "";
        _sipUsernameTextBox_->Text = "";
        _sipHostnameTextBox_->Text = "";
368
369
370
371
        break;
    }
    default:
        break;
atraczyk's avatar
atraczyk committed
372
    }
373
374
375

    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
376
377

    selectMenu(MenuOpen::ACCOUNTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
378
379
380
381
}

void RingClientUWP::Views::SmartPanel::_createAccountNo__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
382
    selectMenu(MenuOpen::ACCOUNTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
383
384
385
386
387
}

void
SmartPanel::_smartList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
    auto listbox = safe_cast<ListBox^>(sender);
    auto item = safe_cast<SmartPanelItem^>(listbox->SelectedItem);

    if (!item)
        return;

    auto contact = item->_contact;

    if (contact == nullptr)
    {
        ERR_("SmartPanelItem without contact");
        return;
    }

    if (contact->_contactStatus == ContactStatus::WAITING_FOR_ACTIVATION) {
        return;
    }

    /* we set the current selected item */
    if (SmartPanelItemsViewModel::instance->_selectedItem != item) {
        SmartPanelItemsViewModel::instance->_selectedItem = item;

        /* at this point we check if a call is in progress with the current selected contact*/
        auto selectedAccountId = AccountListItemsViewModel::instance->getSelectedAccountId();
        auto contactListModel = AccountsViewModel::instance->getContactListModel(Utils::toString(selectedAccountId));
        // TODO: don't clear messages if the user does not see the message panel from the VideoPage
        if (contact) {
            contact->_unreadMessages = 0;
            updateNotificationsState();
            contactListModel->saveContactsToFile();
        }

        /* summon the video when in a call */
        if (item->_callStatus == CallStatus::IN_PROGRESS
            || item->_callStatus == CallStatus::PAUSED
            || item->_callStatus == CallStatus::PEER_PAUSED) {
            summonVideoPage();
            return;
        }

        /* else, summon the message text page*/
        summonMessageTextPage();
    }
Nicolas Jager's avatar
Nicolas Jager committed
431
432
433
434
435
436
}

void
SmartPanel::_accountList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
    auto listbox = safe_cast<ListBox^>(sender);
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
    // disable deselection from listbox
    undoListBoxDeselection(listbox, e);
    auto accountListItem = safe_cast<AccountListItem^>(listbox->SelectedItem);
    if (accountListItem == nullptr) {
        listbox->SelectedIndex = 0;
        accountListItem = safe_cast<AccountListItem^>(listbox->SelectedItem);
    }
    _selectedAccountName_->Text = accountListItem->_account->_bestName;
    _selectedAccountUsername_->Text = accountListItem->_account->_bestName2;
    AccountListItemsViewModel::instance->_selectedItem = accountListItem;
    accountListItem->_isSelected = true;

    SmartPanelItemsViewModel::instance->update({ "_avatarImage" });
    ContactRequestItemsViewModel::instance->update({ "_avatarImage" });

    updatePageContent();
    updateNotificationsState();

    Configuration::UserPreferences::instance->PREF_ACCOUNT_INDEX = _accountsList_->SelectedIndex;
    Configuration::UserPreferences::instance->PREF_ACCOUNT_ID = accountListItem->_account->accountID_;
    Configuration::UserPreferences::instance->save();
}

void
SmartPanel::undoListBoxDeselection(ListBox^ listBox, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
    // disable deselection from listbox
    if (listBox->SelectedItem == nullptr) {
        if (e->RemovedItems->Size > 0) {
466
            Object^ itemToReselect = e->RemovedItems->GetAt(0);
467
            for each (auto item in listBox->Items) {
468
                if (item == itemToReselect) {
469
                    listBox->SelectedItem = itemToReselect;
470
471
472
473
474
                    continue;
                }
            }
        }
    }
475
476
}

477
void
478
SmartPanel::_rejectIncomingCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
479
480
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
481
482
483
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
484
485
            auto callId = item->_callId;
            RingD::instance->refuseIncommingCall(callId);
486
487
        }
    }
488
489
}

490
void
491
SmartPanel::_acceptIncomingCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
492
493
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
494
495
496
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
497
            auto callId = item->_callId;
498
499
500
501
502

            for (auto it : SmartPanelItemsViewModel::instance->itemsList)
                if (it->_callStatus != CallStatus::IN_PROGRESS)
                    RingD::instance->pauseCall(Utils::toString(it->_callId));

503
            _settingsMenuButton_->Visibility = VIS::Collapsed;
504

505
            RingD::instance->acceptIncommingCall(callId);
506
507
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
508
509
510
}

void
511
SmartPanel::_callBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
Nicolas Jager's avatar
Nicolas Jager committed
512
513
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
514
    if (button) {
515
516
517
        /* force to hide the button, avoid attempting to call several times... */
        button->Visibility = Windows::UI::Xaml::Visibility::Collapsed;

518
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
519
520
521
522
        if (item)
            placeCall(item);
    }
}
523

524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
void
SmartPanel::placeCall(SmartPanelItem^ item)
{
    auto contact = item->_contact;
    if (contact) {
        // select item
        //SmartPanelItemsViewModel::instance->_selectedItem = item;
        unsigned index = SmartPanelItemsViewModel::instance->getFilteredIndex(item->_contact);
        //_smartList_->SelectedIndex = index;

        for (auto it : SmartPanelItemsViewModel::instance->itemsList)
            if (it->_callStatus == CallStatus::IN_PROGRESS)
                RingD::instance->pauseCall(Utils::toString(it->_callId));

        if (item->_callStatus == CallStatus::ENDED || item->_callStatus == CallStatus::NONE) {
            _settingsMenuButton_->Visibility = VIS::Collapsed;
            item->_callStatus = CallStatus::OUTGOING_REQUESTED;
            RingD::instance->placeCall(contact);
            auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
            auto lookingForString = loader->GetString("_callsLookingFor_");
            item->_contact->_lastTime = lookingForString + item->_contact->_name + ".";
        }

        /* move the item of the top of the list */
        if (_smartList_->Items->IndexOf(item, &index)) {
            SmartPanelItemsViewModel::instance->moveItemToTheTop(item);
            _smartList_->UpdateLayout();
            _smartList_->ScrollIntoView(item);
552
553
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
554
555
556
557
558
}

void RingClientUWP::Views::SmartPanel::_cancelCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
559
560
561
562
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
            RingD::instance->cancelOutGoingCall2(item->_callId);
563
            item->_callStatus = CallStatus::TERMINATING;
564
565
566
            return;
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
567
568
}

569
570
void
SmartPanel::SmartPanelItem_Grid_PointerEntered(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
Nicolas Jager's avatar
Nicolas Jager committed
571
572
573
574
{
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);

Nicolas Jager's avatar
Nicolas Jager committed
575
    for (auto it : SmartPanelItemsViewModel::instance->itemsList) {
576
        it->_isHovered = false;
Nicolas Jager's avatar
Nicolas Jager committed
577
578
    }

579
    item->_isHovered = true;
Nicolas Jager's avatar
Nicolas Jager committed
580
581
}

582
583
void
SmartPanel::SmartPanelItem_Grid_PointerExited(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
Nicolas Jager's avatar
Nicolas Jager committed
584
585
586
587
{
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);

588
589
590
591
    // to avoid visual bug, do it on the whole list
    for each (auto it in SmartPanelItemsViewModel::instance->itemsList) {
        it->_isHovered = false;
    }
Nicolas Jager's avatar
Nicolas Jager committed
592
593
}

594
595
void
SmartPanel::SmartPanelItem_Grid_PointerMoved(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
596
{
597
598
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);
599

600
601
    for (auto it : SmartPanelItemsViewModel::instance->itemsList)
        it->_isHovered = false;
602

603
    item->_isHovered = true;
604
605
}

Nicolas Jager's avatar
Nicolas Jager committed
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
void RingClientUWP::Views::SmartPanel::checkStateAddAccountMenu()
{
    bool isRingAccountType = (_accountTypeComboBox_->SelectedIndex == 0) ? true : false;

    bool isAccountAlias = (_accountAliasTextBox_->Text->IsEmpty()) ? false : true;

    if (isAccountAlias) {
        _accountAliasValid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        _accountAliasInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    } else {
        _accountAliasValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _accountAliasInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    }

    if (isRingAccountType) {
        bool isPublic = _RegisterState_->IsOn;

Nicolas Jager's avatar
Nicolas Jager committed
623
624
        bool isUsernameValid = (_usernameValid_->Visibility == Windows::UI::Xaml::Visibility::Visible
                                && !_usernameTextBox_->Text->IsEmpty()) ? true : false;
Nicolas Jager's avatar
Nicolas Jager committed
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

        bool isPasswordValid = (_ringPasswordBoxAccountCreation_->Password->IsEmpty()) ? false : true;

        bool isRingPasswordCheck = (_ringPasswordBoxAccountCreation_->Password
                                    == _ringPasswordBoxAccountCreationCheck_->Password
                                    && isPasswordValid)
                                   ? true : false;

        if (isPasswordValid) {
            _passwordValid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
            _passwordInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        }
        else {
            _passwordValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
            _passwordInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        }

        if (isRingPasswordCheck) {
            _passwordCheckValid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
            _passwordCheckInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        }
        else {
            _passwordCheckValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
            _passwordCheckInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        }

Nicolas Jager's avatar
Nicolas Jager committed
651
652
653
654
655
656
657
        if (isUsernameValid) {
            _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
            _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        } else {
            _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
            _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        }
Nicolas Jager's avatar
Nicolas Jager committed
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728

        if (isPublic)
            if (isUsernameValid && isAccountAlias && isRingPasswordCheck && isPasswordValid)
                _createAccountYes_->IsEnabled = true;
            else
                _createAccountYes_->IsEnabled = false;
        else if (isAccountAlias && isRingPasswordCheck && isPasswordValid)
            _createAccountYes_->IsEnabled = true;
        else
            _createAccountYes_->IsEnabled = false;

    } else {
        if (isAccountAlias)
            _createAccountYes_->IsEnabled = true;
        else
            _createAccountYes_->IsEnabled = false;
    }

}

void RingClientUWP::Views::SmartPanel::checkStateEditionMenu()
{
    if (AccountListItemsViewModel::instance->_selectedItem == nullptr)
        return;
    bool isRingAccountType = (AccountListItemsViewModel::instance->_selectedItem->_account->accountType_ == "RING")
                             ? true : false;

    bool isAccountAlias = (_accountAliasTextBoxEdition_->Text->IsEmpty()) ? false : true;

    bool isAlreadyRegistered = (_RegisterStateEdition_->IsEnabled) ? false : true;

    if (isAccountAlias) {
        _accountAliasValidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        _accountAliasInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }
    else {
        _accountAliasValidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _accountAliasInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    }

    if (isRingAccountType) {
        bool isPublic = _RegisterStateEdition_->IsOn;

        bool isUsernameValid = (_usernameValidEdition_->Visibility == Windows::UI::Xaml::Visibility::Visible)
                               ? true : false;

        bool isPasswordValid = (_ringPasswordBoxAccountCreation_->Password->IsEmpty()) ? false : true;

        bool isRingPasswordCheck = (_ringPasswordBoxAccountCreation_->Password
                                    == _ringPasswordBoxAccountCreationCheck_->Password
                                    && isPasswordValid)
                                   ? true : false;
        if (isPublic)
            if (isUsernameValid && isAccountAlias || isAlreadyRegistered) {
                _acceptAccountModification_->IsEnabled = true;
            } else {
                _acceptAccountModification_->IsEnabled = false;
            }
        else if (isAccountAlias)
            _acceptAccountModification_->IsEnabled = true;
        else
            _acceptAccountModification_->IsEnabled = false;
    }
    else {
        if (isAccountAlias)
            _acceptAccountModification_->IsEnabled = true;
        else
            _acceptAccountModification_->IsEnabled = false;
    }
}

729
730
731
732
733
734
735
736
737
738
739
void RingClientUWP::Views::SmartPanel::ringTxtBxPlaceHolderDelay(String^ placeHolderText, int delayInMilliSeconds)
{
    _ringTxtBx_->PlaceholderText = placeHolderText;
    TimeSpan delay;
    delay.Duration = 10000 * delayInMilliSeconds;
    ThreadPoolTimer^ delayTimer = ThreadPoolTimer::CreateTimer(
                                      ref new TimerElapsedHandler([this](ThreadPoolTimer^ source)
    {
        Dispatcher->RunAsync(CoreDispatcherPriority::High,
                             ref new DispatchedHandler([this]()
        {
740
741
            auto placeHolderText = ResourceMananger::instance->getStringResource("_ringTxtBxPlaceholderText_");
            _ringTxtBx_->PlaceholderText = placeHolderText;
742
743
744
745
        }));
    }), delay);
}

746
747
748
749
750
751
752
753
754
755
void RingClientUWP::Views::SmartPanel::showLinkThisDeviceStep1()
{
    _step1Menu_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _step2Menu_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;

    _nextstep_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _addAccountYes_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _addAccountNo_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}

Nicolas Jager's avatar
Nicolas Jager committed
756
757


758
759
void RingClientUWP::Views::SmartPanel::_addDevice__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
760
761
    _devicesMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _addingDeviceGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
Nicolas Jager's avatar
Nicolas Jager committed
762
    _closePin_->Visibility = Windows::UI::Xaml::Visibility::Visible;
763
764
765
}


766
void RingClientUWP::Views::SmartPanel::OndevicesListRefreshed(Map<String^, String^>^ deviceMap)
767
{
768
769
770
    if (!AccountListItemsViewModel::instance->_selectedItem)
        return;

Nicolas Jager's avatar
Nicolas Jager committed
771
    _waitingDevicesList_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792

    RingDeviceItemsViewModel::instance->itemsList->Clear();

    for each (auto device in deviceMap) {
        if (AccountListItemsViewModel::instance->_selectedItem->_account->_deviceId == device->Key) {
            AccountListItemsViewModel::instance->_selectedItem->_account->_deviceName == device->Value;
            _deviceName_->Text = device->Value;
        }
        else {
            RingDeviceItemsViewModel::instance->itemsList->Append(ref new RingDeviceItem(device->Key, device->Value));
        }
    }

    if (deviceMap->Size < 2) {
        _noDevicesList_->Visibility = VIS::Visible;
        _devicesIdList_->Visibility = VIS::Collapsed;
    }
    else {
        _noDevicesList_->Visibility = VIS::Collapsed;
        _devicesIdList_->Visibility = VIS::Visible;
    }
793
794
795
796
797
}


void RingClientUWP::Views::SmartPanel::_pinGeneratorYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
798
    requestPin();
799
800
801
802
803
804
805
}


void RingClientUWP::Views::SmartPanel::_pinGeneratorNo__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    _addingDeviceGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _devicesMenuButton_->IsChecked = false;
Nicolas Jager's avatar
Nicolas Jager committed
806
    _passwordForPinGenerator_->Password = "";
807
808

    selectMenu(MenuOpen::CONTACTS_LIST);
809
}
810

811
812
813
814

void RingClientUWP::Views::SmartPanel::OnexportOnRingEnded(Platform::String ^accountId, Platform::String ^pin)
{
    _waitingAndResult_->Text = pin;
Nicolas Jager's avatar
Nicolas Jager committed
815
816
    _closePin_->Visibility = Windows::UI::Xaml::Visibility::Visible;

817
    RingD::instance->hideLoadingOverlay("PIN generated", SuccessColor, 500);
Nicolas Jager's avatar
Nicolas Jager committed
818
819
820
821
822
}


void RingClientUWP::Views::SmartPanel::_closePin__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
823
    selectMenu(MenuOpen::CONTACTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
824
825

    // refacto : do something better...
826
827
    auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
    _waitingAndResult_->Text = loader->GetString("_accountsWaitingAndResult_.Text");
828
}
829

830
831
void RingClientUWP::Views::SmartPanel::_editAccountMenuButton__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
832
833
    _scrollViewerEditionMenu_->UpdateLayout();
    _scrollViewerEditionMenu_->ScrollToVerticalOffset(0);
Nicolas Jager's avatar
Nicolas Jager committed
834

835
    auto account = AccountListItemsViewModel::instance->_selectedItem->_account;
Nicolas Jager's avatar
Nicolas Jager committed
836
837
838

    _accountAliasTextBoxEdition_->Text = account->name_;
    _accountEditionGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
839
    _createAccountYes_->IsEnabled = false;
Nicolas Jager's avatar
Nicolas Jager committed
840
841
842
843
844
845
846

    _sipAccountStackEdition_->Visibility = (account->accountType_ == "SIP")
                                           ? Windows::UI::Xaml::Visibility::Visible
                                           :Windows::UI::Xaml::Visibility::Collapsed;
    _ringStackEdition_->Visibility = (account->accountType_ == "RING")
                                     ? Windows::UI::Xaml::Visibility::Visible
                                     : Windows::UI::Xaml::Visibility::Collapsed;
847
848
849
850
851

    _ringStackEdition2_->Visibility = (account->accountType_ == "RING")
        ? Windows::UI::Xaml::Visibility::Visible
        : Windows::UI::Xaml::Visibility::Collapsed;

Nicolas Jager's avatar
Nicolas Jager committed
852
    _sipHostnameEdition_->Text = account->_sipHostname;
853
    _sipUsernameEditionTextBox_->Text = account->_sipUsername;
Nicolas Jager's avatar
Nicolas Jager committed
854
855
856
857
858
859
860
861
862
863
864
    _sipPasswordEdition_->Password = account->_sipPassword;

    auto registeredName = Utils::toPlatformString(RingD::instance->registeredName(account));

    if (registeredName) {
        _RegisterStateEdition_->IsOn = true; // keep this before _usernameTextBoxEdition_

        _usernameTextBoxEdition_->IsEnabled = false;
        _usernameTextBoxEdition_->Text = registeredName;
        _RegisterStateEdition_->IsEnabled = false;

865
866
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappenEdition_->Text = loader->GetString("_whatWillHappen_0_");
Nicolas Jager's avatar
Nicolas Jager committed
867
868
869
870
871
872
873
874
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    } else {
        _RegisterStateEdition_->IsOn = false;

        _usernameTextBoxEdition_->IsEnabled = false;
        _usernameTextBoxEdition_->Text = "";
        _RegisterStateEdition_->IsEnabled = true;

875
876
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappenEdition_->Text = loader->GetString("_whatWillHappen_1_");
Nicolas Jager's avatar
Nicolas Jager committed
877
878
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }
879

880
881
882
883
884
885
886
887
888
    auto nBannedContacts = AccountsViewModel::instance->bannedContacts(account->accountID_);
    if (nBannedContacts) {
        _accountsShowBannedList_->IsEnabled = true;
    }
    else {
        _accountsShowBannedList_->IsEnabled = false;
    }
    setBannedListState(false);

Nicolas Jager's avatar
Nicolas Jager committed
889
    checkStateEditionMenu();
890
891
892
893
894
895
896
897
}


void RingClientUWP::Views::SmartPanel::_acceptAccountModification__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto account = AccountListItemsViewModel::instance->_selectedItem->_account;
    auto accountId = account->accountID_;

898
    account->name_ = _accountAliasTextBoxEdition_->Text;
899

900
    if (account->accountType_ == "RING") {
901
902
        if (_RegisterStateEdition_->IsOn)
            account->_username = _usernameTextBoxEdition_->Text;
903
904
905
906
907
908
909
910
911
912
913
914
915
        account->_active = _enabledState_->IsOn;
        account->_upnpState = _upnpState_->IsOn;
        account->_autoAnswer = _autoAnswerToggle_->IsOn;
        account->_dhtPublicInCalls = _dhtPublicInCallsToggle_->IsOn;
        account->_turnEnabled = _turnEnabledToggle_->IsOn;
        if (_turnEditionTextBox_->Text != "")
            account->_turnAddress = _turnEditionTextBox_->Text;
    }
    else {
        account->_sipHostname = _sipHostnameEdition_->Text;
        account->_sipUsername = _sipUsernameEditionTextBox_->Text;
        account->_sipPassword = _sipPasswordEdition_->Password;
    }
Nicolas Jager's avatar
Nicolas Jager committed
916

917
    RingD::instance->updateAccount(accountId);
918

919
    selectMenu(MenuOpen::ACCOUNTS_LIST);
920

921
922
    updatePageContent();

Nicolas Jager's avatar
Nicolas Jager committed
923
    if (_usernameValidEdition_->Visibility == Windows::UI::Xaml::Visibility::Visible && _usernameTextBoxEdition_->Text->Length() > 2)
924
        RingD::instance->registerName(account->accountID_, "", _usernameTextBoxEdition_->Text);
925
926
927
928
929
}


void RingClientUWP::Views::SmartPanel::_cancelAccountModification__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Nicolas Jager's avatar
Nicolas Jager committed
930
    _accountEditionGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
931
932
933
934
935
936
}


void RingClientUWP::Views::SmartPanel::OnaccountUpdated(RingClientUWP::Account ^account)
{
    updatePageContent();
937
    updateNotificationsState();
938
939
940
941
}

void RingClientUWP::Views::SmartPanel::_passwordBoxAccountCreationCheck__PasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Nicolas Jager's avatar
Nicolas Jager committed
942
    checkStateAddAccountMenu();
943
}
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967


void RingClientUWP::Views::SmartPanel::_accountTypeComboBox__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
    auto accountTypeComboBox = dynamic_cast<ComboBox^>(sender);

    /* avoid exception at start */
    if (_ringAccountCreationStack_ == nullptr && _sipAccountCreationStack_ == nullptr)
        return;

    /* empty everything, avoid to keep credentials in memory... */
    _ringPasswordBoxAccountCreation_->Password = "";
    _ringPasswordBoxAccountCreationCheck_->Password = "";
    _sipPasswordBoxAccountCreation_->Password = "";
    _sipUsernameTextBox_->Text = "";
    _sipHostnameTextBox_->Text = "";

    if (accountTypeComboBox->SelectedIndex == 0 /* RING type is selected */) {
        _ringAccountCreationStack_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        _sipAccountCreationStack_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _createAccountYes_->IsEnabled = false;
    } else { /* SIP type is selected */
        _ringAccountCreationStack_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _sipAccountCreationStack_->Visibility = Windows::UI::Xaml::Visibility::Visible;
Nicolas Jager's avatar
Nicolas Jager committed
968
        _createAccountYes_->IsEnabled = (_accountAliasTextBox_->Text->IsEmpty()) ? false : true;
969
970
971
    }
}

Nicolas Jager's avatar
Nicolas Jager committed
972
void RingClientUWP::Views::SmartPanel::_accountAliasTextBox__TextChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::TextChangedEventArgs^ e)
973
{
Nicolas Jager's avatar
Nicolas Jager committed
974
975
976
977
978
979
    checkStateAddAccountMenu();
}

void RingClientUWP::Views::SmartPanel::_accountAliasTextBoxEdition__TextChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::TextChangedEventArgs^ e)
{
    checkStateEditionMenu();
980
981
}

982
983
984
985
986
987
988
void RingClientUWP::Views::SmartPanel::_selectedAccountAvatarContainer__PointerEntered(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    _photoboothIcon_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _shaderPhotoboothIcon_->Visibility = Windows::UI::Xaml::Visibility::Visible;
}


atraczyk's avatar
atraczyk committed
989
990
void
RingClientUWP::Views::SmartPanel::_selectedAccountAvatarContainer__PointerReleased(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
991
{
992
993
994
995
    create_task(Configuration::getProfileImageAsync()).then([&](task<BitmapImage^> image){
        try {
            if (auto bitmapImage = image.get()) {
                _selectedAccountAvatar_->ImageSource = bitmapImage;
atraczyk's avatar
atraczyk committed
996
            }
997
998
999
        }
        catch (Platform::Exception^ e) {
            EXC_(e);
1000
1001
1002
1003
1004
1005
1006
1007
1008
        }
    });
}

void RingClientUWP::Views::SmartPanel::_selectedAccountAvatarContainer__PointerExited(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    _photoboothIcon_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _shaderPhotoboothIcon_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}
1009
1010
1011
1012
1013
1014

void RingClientUWP::Views::SmartPanel::_smartList__PointerExited(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{

}

Nicolas Jager's avatar
Nicolas Jager committed
1015
1016
1017
1018
1019
// NAME SERVICE

void RingClientUWP::Views::SmartPanel::_registerOnBlockchainEdition__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto account = AccountListItemsViewModel::instance->_selectedItem->_account;
1020
    RingD::instance->registerName(account->accountID_, "", _usernameTextBoxEdition_->Text);
Nicolas Jager's avatar
Nicolas Jager committed
1021
1022
1023
1024
1025
}


void RingClientUWP::Views::SmartPanel::_usernameTextBoxEdition__KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
1026
1027
    auto accountId = AccountListItemsViewModel::instance->getSelectedAccountId();
    RingD::instance->lookUpName(Utils::toString(accountId), _usernameTextBoxEdition_->Text);
Nicolas Jager's avatar
Nicolas Jager committed
1028
1029
1030
1031
1032
1033
1034

    _usernameValidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;

    checkStateEditionMenu();
}

1035
void RingClientUWP::Views::SmartPanel::OnregisteredNameFound(RingClientUWP::LookupStatus status, const std::string& accountId, const std::string& address, const std::string& name)
Nicolas Jager's avatar
Nicolas Jager committed
1036
{
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
    // In the case where the lookup was for a local account's username, we need just
    // update the account's username property and return.
    if (status == LookupStatus::SUCCESS) {
        if (Account^ account = AccountsViewModel::instance->findAccountByRingID(Utils::toPlatformString(address))) {
            MSG_("Account username lookup complete");
            account->_username = Utils::toPlatformString(name);
            // If the the account is currently selected, update the account best-name
            if (AccountListItemsViewModel::instance->_selectedItem->_account == account) {
                _selectedAccountName_->Text = account->_bestName;
               _selectedAccountUsername_->Text = account->_bestName2;
            }
            return;
        }
    }

Nicolas Jager's avatar
Nicolas Jager committed
1052
    if (menuOpen == MenuOpen::ACCOUNTS_LIST) { // if true, we did the lookup for a new account
1053
1054
1055
1056
1057
1058
1059
1060
        /* note : this code do both check for edit and creation menu. It doesn't affect the use and it's easier to
           implement. */
        auto currentNameEdition = Utils::toString(_usernameTextBoxEdition_->Text);
        if (currentNameEdition == name) {
            switch (status)
            {
            case LookupStatus::SUCCESS:
            case LookupStatus::INVALID_NAME:
1061
            case LookupStatus::ERRORR:
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
                _usernameValidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
                _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;
                break;
            case LookupStatus::NOT_FOUND:
                _usernameValidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;
                _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
                break;
            }
            checkStateEditionMenu();
            return;
        }
1073

1074
1075
1076
1077
1078
1079
        auto currentNameCreation = Utils::toString(_usernameTextBox_->Text);
        if (currentNameCreation == name) {
            switch (status)
            {
            case LookupStatus::SUCCESS:
            case LookupStatus::INVALID_NAME:
1080
            case LookupStatus::ERRORR:
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
                _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
                _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
                break;
            case LookupStatus::NOT_FOUND:
                _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
                _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
                break;
            }
            checkStateAddAccountMenu();
            return;
1091
        }
1092

Nicolas Jager's avatar
Nicolas Jager committed
1093
1094
    }
    else { // if false, we are looking for a registered user (contact)
1095
        auto contactListModel = AccountsViewModel::instance->getContactListModel(std::string(accountId));
1096
        auto contact = contactListModel->findContactByName(Utils::toPlatformString(name));
Nicolas Jager's avatar
Nicolas Jager committed
1097

1098
1099
1100
1101
        // Try looking up a contact added by address
        if (contact == nullptr)
            contact = contactListModel->findContactByName(Utils::toPlatformString(address));

Nicolas Jager's avatar
Nicolas Jager committed
1102
1103
1104
        if (contact == nullptr)
            return;

1105
1106
        switch (status) {
        case LookupStatus::SUCCESS:
1107
        {
Nicolas Jager's avatar
Nicolas Jager committed
1108
1109
1110
            if (contact->_contactStatus == ContactStatus::WAITING_FOR_ACTIVATION) {
                contact->_contactStatus = ContactStatus::READY;
                contact->ringID_ = Utils::toPlatformString(address);
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
                contact->_avatarColorString = Utils::getRandomColorStringFromString(contact->ringID_);
                auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
                ringTxtBxPlaceHolderDelay(loader->GetString("_contactsUserAdded_"), 5000);

                // send contact request
                if (contact->_trustStatus == TrustStatus::UNKNOWN) {
                    auto vcard = Configuration::UserPreferences::instance->getVCard();
                    RingD::instance->sendContactRequest(accountId, address, vcard->asString());
                    contact->_name = Utils::toPlatformString(name);
                    contact->_trustStatus = TrustStatus::CONTACT_REQUEST_SENT;
                    SmartPanelItemsViewModel::instance->refreshFilteredItemsList();
                    SmartPanelItemsViewModel::instance->update(ViewModel::NotifyStrings::notifySmartPanelItem);
                }
                else if (contact->_trustStatus == TrustStatus::INCOMING_CONTACT_REQUEST) {
                    contact->_name = Utils::toPlatformString(name);
                    ContactRequestItemsViewModel::instance->update(ViewModel::NotifyStrings::notifyContactRequestItem);
                }
1128
                contactListModel->saveContactsToFile();
Nicolas Jager's avatar
Nicolas Jager committed
1129
            }
1130
            else {
1131
1132
1133
1134
1135
1136
                /* in that case we delete a possible temporary item */
                for each (Contact^ contact in contactListModel->_contactsList) {
                    if (contact->_contactStatus == ContactStatus::WAITING_FOR_ACTIVATION
                            && contact->_name == Utils::toPlatformString(name)) {
                        auto item = SmartPanelItemsViewModel::instance->findItem(contact);
                        contactListModel->deleteContact(contact);
1137
1138
1139
1140
                        SmartPanelItemsViewModel::instance->removeItem(item);
                    }
                }
            }
1141
1142
1143
1144
1145
            if (!contact->subscribed_) {
                MSG_("account: " + accountId + " subscribing to buddy presence for ringID: " + Utils::toString(contact->ringID_));
                RingD::instance->subscribeBuddy(accountId, Utils::toString(contact->ringID_), true);
                contact->subscribed_ = true;
            }
1146
1147
        }
        break;
1148
        case LookupStatus::INVALID_NAME:
1149
1150
1151
1152
        {
            MSG_("INVALID_NAME LOOKUP RESULT");
            std::regex sha1_regex("[0-9a-f]{40}");
            if (std::regex_match(name, sha1_regex)) {
1153
                /* first we check if some contact is registred with this ring id */
1154
                auto contactAlreadyRecorded = contactListModel->findContactByRingId(Utils::toPlatformString(name));
1155
1156
1157
                if (contactAlreadyRecorded) {
                    /* delete the contact added recently */
                    auto item = SmartPanelItemsViewModel::instance->findItem(contact);
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
                    if (item->_contact->_trustStatus != TrustStatus::INCOMING_CONTACT_REQUEST &&
                        item->_contact->_trustStatus != TrustStatus::UNKNOWN &&
                        item->_contact->_contactStatus == ContactStatus::WAITING_FOR_ACTIVATION) {
                        contactListModel->deleteContact(contact);
                        SmartPanelItemsViewModel::instance->removeItem(item);
                        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
                        ringTxtBxPlaceHolderDelay(loader->GetString("_contactsContactExists_"), 5000);
                    }
                    else if (item->_contact->_trustStatus == TrustStatus::UNKNOWN) {
                        SmartPanelItemsViewModel::instance->refreshFilteredItemsList();
                        SmartPanelItemsViewModel::instance->update(ViewModel::NotifyStrings::notifySmartPanelItem);
                    }
1170
                }
1171
1172