SmartPanel.xaml.cpp 116 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
    });
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 192 193
    RingD::instance->newBuddyNotification += ref new RingClientUWP::NewBuddyNotification(this, &RingClientUWP::Views::SmartPanel::OnnewBuddyNotification);

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

194
    selectMenu(MenuOpen::CONTACTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
195 196
}

197 198 199 200 201 202
void
SmartPanel::OnregistrationStateChanged(const std::string& accountId)
{
    AccountListItemsViewModel::instance->update(ViewModel::NotifyStrings::notifyAccountItem);
}

203 204 205 206 207 208 209 210 211 212 213
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) {
214 215 216 217 218
    case CallStatus::INCOMING_RINGING:
    {
        updateCallAnimationState(item, true);
        break;
    }
219 220 221
    case CallStatus::NONE:
    case CallStatus::ENDED:
    {
222
        updateCallAnimationState(item, false);
223
        auto callsList = DRing::getCallList();
atraczyk's avatar
atraczyk committed
224 225
        if (callsList.empty())
            _settingsMenuButton_->Visibility = VIS::Visible;
226

227 228 229 230
        break;
    }
    case CallStatus::IN_PROGRESS:
    {
231
        updateCallAnimationState(item, false);
232
        SmartPanelItemsViewModel::instance->_selectedItem = item;
233 234
        _smartList_->SelectedIndex = SmartPanelItemsViewModel::instance->getFilteredIndex(item->_contact);
        _settingsMenuButton_->Visibility = VIS::Collapsed;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
        summonVideoPage();
        break;
    }
    case CallStatus::PEER_PAUSED:
    case CallStatus::PAUSED:
    {
        SmartPanelItemsViewModel::instance->_selectedItem = item;
        summonVideoPage();
        break;
    }
    default:
        break;
    }
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
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
274 275 276
void
RingClientUWP::Views::SmartPanel::updatePageContent()
{
Nicolas Jager's avatar
Nicolas Jager committed
277 278
    auto accountListItem = AccountListItemsViewModel::instance->_selectedItem;
    if (!accountListItem)
Nicolas Jager's avatar
Nicolas Jager committed
279 280
        return;

281 282 283 284
    _selectedAccountName_->Text = accountListItem->_account->_bestName;
    _selectedAccountUsername_->Text = accountListItem->_account->_bestName2;
    _deviceId_->Text = accountListItem->_account->_deviceId;
    _deviceName_->Text = accountListItem->_account->_deviceName;
285

Nicolas Jager's avatar
Nicolas Jager committed
286
    _devicesMenuButton_->Visibility = (accountListItem->_account->accountType_ == "RING")
287 288
                                      ? Windows::UI::Xaml::Visibility::Visible
                                      : Windows::UI::Xaml::Visibility::Collapsed;
289

290
    _enabledState_->IsOn = accountListItem->_account->_active;
291
    _upnpState_->IsOn = accountListItem->_account->_upnpState;
292 293 294 295
    _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
296

297
    if (_RegisterStateEdition_->IsOn) {
Nicolas Jager's avatar
Nicolas Jager committed
298 299 300 301 302 303 304 305
        _usernameTextBoxEdition_->IsEnabled = true;
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;

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

306 307 308 309 310
    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
311 312 313 314
}

void RingClientUWP::Views::SmartPanel::setMode(RingClientUWP::Views::SmartPanel::Mode mode)
{
315 316
    _contactsTitleRow_->Height = (mode == RingClientUWP::Views::SmartPanel::Mode::Normal) ? 54 : 0;
    _rowRingTxtBx_->Height = (mode == RingClientUWP::Views::SmartPanel::Mode::Normal)? 48 : 0;
317
    selectMenu(MenuOpen::CONTACTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
318 319 320 321 322 323
}

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;
324

325 326 327
    _accountCreationMenuGrid_->UpdateLayout();
    _accountCreationMenuScrollViewer_->ScrollToVerticalOffset(0);

328
    _createAccountYes_->IsEnabled = false;
329 330

    _accountTypeComboBox_->SelectedIndex = 0;
Nicolas Jager's avatar
Nicolas Jager committed
331 332 333
    _RegisterStateEdition_->IsOn = true;
    _accountAliasTextBox_->Text = "";
    _usernameTextBox_->Text = "";
Nicolas Jager's avatar
Nicolas Jager committed
334 335

    checkStateAddAccountMenu();
Nicolas Jager's avatar
Nicolas Jager committed
336 337 338 339
}

void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
340 341
    switch (_accountTypeComboBox_->SelectedIndex) {
    case 0: /* RING account */
342
    {
Nicolas Jager's avatar
Nicolas Jager committed
343 344
        RingD::instance->createRINGAccount(_accountAliasTextBox_->Text
                                           , _ringPasswordBoxAccountCreation_->Password
345
                                           , true
Nicolas Jager's avatar
Nicolas Jager committed
346 347
                                           , (_RegisterState_->IsOn) ? _usernameTextBox_->Text : "");

348 349
        _ringPasswordBoxAccountCreation_->Password = "";
        _ringPasswordBoxAccountCreationCheck_->Password = "";
Nicolas Jager's avatar
Nicolas Jager committed
350
        _usernameTextBox_->Text = "";
351
        break;
352 353
    }
    break;
354
    case 1: /* SIP account */
355
    {
Nicolas Jager's avatar
Nicolas Jager committed
356
        RingD::instance->createSIPAccount(_accountAliasTextBox_->Text, _sipPasswordBoxAccountCreation_->Password, _sipHostnameTextBox_->Text, _sipUsernameTextBox_->Text);
357 358 359
        _sipPasswordBoxAccountCreation_->Password = "";
        _sipUsernameTextBox_->Text = "";
        _sipHostnameTextBox_->Text = "";
360 361 362 363
        break;
    }
    default:
        break;
364
    }
365 366 367

    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
368 369

    selectMenu(MenuOpen::ACCOUNTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
370 371 372 373
}

void RingClientUWP::Views::SmartPanel::_createAccountNo__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
374
    selectMenu(MenuOpen::ACCOUNTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
375 376 377 378 379
}

void
SmartPanel::_smartList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
380 381 382 383 384 385 386 387 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
    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
423 424 425 426 427 428
}

void
SmartPanel::_accountList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
    auto listbox = safe_cast<ListBox^>(sender);
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    // 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) {
458
            Object^ itemToReselect = e->RemovedItems->GetAt(0);
459
            for each (auto item in listBox->Items) {
460
                if (item == itemToReselect) {
461
                    listBox->SelectedItem = itemToReselect;
462 463 464 465 466
                    continue;
                }
            }
        }
    }
467 468
}

469
void
470
SmartPanel::_rejectIncomingCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
471 472
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
473 474 475
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
476 477
            auto callId = item->_callId;
            RingD::instance->refuseIncommingCall(callId);
478 479
        }
    }
480 481
}

482
void
483
SmartPanel::_acceptIncomingCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
484 485
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
486 487 488
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
489
            auto callId = item->_callId;
490 491 492 493 494

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

495
            _settingsMenuButton_->Visibility = VIS::Collapsed;
496

497
            RingD::instance->acceptIncommingCall(callId);
498 499
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
500 501 502
}

void
503
SmartPanel::_callBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
Nicolas Jager's avatar
Nicolas Jager committed
504 505
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
506
    if (button) {
507 508 509
        /* force to hide the button, avoid attempting to call several times... */
        button->Visibility = Windows::UI::Xaml::Visibility::Collapsed;

510
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
511 512 513 514
        if (item)
            placeCall(item);
    }
}
515

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
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);
544 545
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
546 547 548 549 550
}

void RingClientUWP::Views::SmartPanel::_cancelCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
551 552 553 554
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
            RingD::instance->cancelOutGoingCall2(item->_callId);
555
            item->_callStatus = CallStatus::TERMINATING;
556 557 558
            return;
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
559 560
}

561 562
void
SmartPanel::SmartPanelItem_Grid_PointerEntered(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
Nicolas Jager's avatar
Nicolas Jager committed
563 564 565 566
{
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);

567
    for (auto it : SmartPanelItemsViewModel::instance->itemsList) {
568
        it->_isHovered = false;
569 570
    }

571
    item->_isHovered = true;
Nicolas Jager's avatar
Nicolas Jager committed
572 573
}

574 575
void
SmartPanel::SmartPanelItem_Grid_PointerExited(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
Nicolas Jager's avatar
Nicolas Jager committed
576 577 578 579
{
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);

580 581 582 583
    // 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
584 585
}

586 587
void
SmartPanel::SmartPanelItem_Grid_PointerMoved(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
588
{
589 590
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);
591

592 593
    for (auto it : SmartPanelItemsViewModel::instance->itemsList)
        it->_isHovered = false;
594

595
    item->_isHovered = true;
596 597
}

Nicolas Jager's avatar
Nicolas Jager committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
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
615 616
        bool isUsernameValid = (_usernameValid_->Visibility == Windows::UI::Xaml::Visibility::Visible
                                && !_usernameTextBox_->Text->IsEmpty()) ? true : false;
Nicolas Jager's avatar
Nicolas Jager committed
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642

        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
643 644 645 646 647 648 649
        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
650 651 652 653 654 655 656 657 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

        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;
    }
}

721 722 723 724 725 726 727 728 729 730 731
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]()
        {
732 733
            auto placeHolderText = ResourceMananger::instance->getStringResource("_ringTxtBxPlaceholderText_");
            _ringTxtBx_->PlaceholderText = placeHolderText;
734 735 736 737
        }));
    }), delay);
}

738 739 740 741 742 743 744 745 746 747
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
748 749 750
Object ^ RingClientUWP::Views::IncomingVisibility::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    auto state = static_cast<CallStatus>(value);
751

752
    if (state == CallStatus::INCOMING_RINGING)
Nicolas Jager's avatar
Nicolas Jager committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
        return  Windows::UI::Xaml::Visibility::Visible;
    else
        return  Windows::UI::Xaml::Visibility::Collapsed;
}

Object ^ RingClientUWP::Views::IncomingVisibility::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    throw ref new Platform::NotImplementedException();
}

RingClientUWP::Views::IncomingVisibility::IncomingVisibility()
{}


Object ^ RingClientUWP::Views::OutGoingVisibility::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    auto state = static_cast<CallStatus>(value);

771 772 773
    if (state == CallStatus::SEARCHING
            || state == CallStatus::OUTGOING_RINGING
            || state == CallStatus::OUTGOING_REQUESTED)
Nicolas Jager's avatar
Nicolas Jager committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
        return  Windows::UI::Xaml::Visibility::Visible;
    else
        return  Windows::UI::Xaml::Visibility::Collapsed;
}

Object ^ RingClientUWP::Views::OutGoingVisibility::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    throw ref new Platform::NotImplementedException();
}

RingClientUWP::Views::OutGoingVisibility::OutGoingVisibility()
{}

Object ^ RingClientUWP::Views::HasAnActiveCall::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    auto state = static_cast<CallStatus>(value);

791
    if (state == CallStatus::NONE || state == CallStatus::ENDED)
Nicolas Jager's avatar
Nicolas Jager committed
792 793 794 795 796 797 798 799 800 801 802 803 804
        return Windows::UI::Xaml::Visibility::Collapsed;
    else
        return Windows::UI::Xaml::Visibility::Visible;
}

Object ^ RingClientUWP::Views::HasAnActiveCall::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    throw ref new Platform::NotImplementedException();
}

RingClientUWP::Views::HasAnActiveCall::HasAnActiveCall()
{}

805
Object ^ RingClientUWP::Views::NewMessageBubbleNotification::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
Nicolas Jager's avatar
Nicolas Jager committed
806 807 808 809 810 811 812 813 814
{
    auto unreadMessages = static_cast<uint32>(value);

    if (unreadMessages > 0)
        return Windows::UI::Xaml::Visibility::Visible;

    return Windows::UI::Xaml::Visibility::Collapsed;
}

815
Object ^ RingClientUWP::Views::NewMessageNotificationToNumber::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
Nicolas Jager's avatar
Nicolas Jager committed
816
{
817
    auto unreadMessages = static_cast<uint32>(value);
Nicolas Jager's avatar
Nicolas Jager committed
818

819 820 821 822 823
    if (unreadMessages > 9)
        return "9+";

    return unreadMessages.ToString();
}
824 825 826

void RingClientUWP::Views::SmartPanel::_addDevice__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
827 828
    _devicesMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _addingDeviceGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
Nicolas Jager's avatar
Nicolas Jager committed
829
    _closePin_->Visibility = Windows::UI::Xaml::Visibility::Visible;
830 831 832
}


833
void RingClientUWP::Views::SmartPanel::OndevicesListRefreshed(Map<String^, String^>^ deviceMap)
834
{
835 836 837
    if (!AccountListItemsViewModel::instance->_selectedItem)
        return;

Nicolas Jager's avatar
Nicolas Jager committed
838
    _waitingDevicesList_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859

    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;
    }
860 861 862 863 864
}


void RingClientUWP::Views::SmartPanel::_pinGeneratorYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
865
    requestPin();
866 867 868 869 870 871 872
}


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
873
    _passwordForPinGenerator_->Password = "";
874 875

    selectMenu(MenuOpen::CONTACTS_LIST);
876
}
877

878 879 880 881

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

884
    RingD::instance->hideLoadingOverlay("PIN generated", SuccessColor, 500);
Nicolas Jager's avatar
Nicolas Jager committed
885 886 887 888 889
}


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

    // refacto : do something better...
893 894
    auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
    _waitingAndResult_->Text = loader->GetString("_accountsWaitingAndResult_.Text");
895
}
896 897 898 899 900 901 902 903 904 905 906

Object ^ RingClientUWP::Views::AccountTypeToSourceImage::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    auto accountType = dynamic_cast<String^>(value);
    Uri^ uri = (accountType == "RING")
               ? ref new Uri("ms-appx:///Assets/AccountTypeRING.png")
               : ref new Uri("ms-appx:///Assets/AccountTypeSIP.png");

    return ref new BitmapImage(uri);
}

907 908 909 910 911 912 913
Object ^ RingClientUWP::Views::RingAccountTypeToVisibility::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    if (dynamic_cast<String^>(value) == "RING")
        return VIS::Visible;
    return VIS::Collapsed;
}

914 915 916 917 918 919 920 921 922 923
Object ^ RingClientUWP::Views::AccountTypeToSourceImage::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    throw ref new Platform::NotImplementedException();
}

RingClientUWP::Views::AccountTypeToSourceImage::AccountTypeToSourceImage()
{}

Object ^ RingClientUWP::Views::AccountSelectedToVisibility::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
924
    if ((bool)value == true)
925 926 927 928 929 930 931 932 933 934 935 936
        return Windows::UI::Xaml::Visibility::Visible;

    return Windows::UI::Xaml::Visibility::Collapsed;
}

Object ^ RingClientUWP::Views::AccountSelectedToVisibility::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    throw ref new Platform::NotImplementedException();
}

RingClientUWP::Views::AccountSelectedToVisibility::AccountSelectedToVisibility()
{}
937 938 939 940


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

944
    auto account = AccountListItemsViewModel::instance->_selectedItem->_account;
Nicolas Jager's avatar
Nicolas Jager committed
945 946 947

    _accountAliasTextBoxEdition_->Text = account->name_;
    _accountEditionGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
948
    _createAccountYes_->IsEnabled = false;
Nicolas Jager's avatar
Nicolas Jager committed
949 950 951 952 953 954 955

    _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;
956 957 958 959 960

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

Nicolas Jager's avatar
Nicolas Jager committed
961
    _sipHostnameEdition_->Text = account->_sipHostname;
962
    _sipUsernameEditionTextBox_->Text = account->_sipUsername;
Nicolas Jager's avatar
Nicolas Jager committed
963 964 965 966 967 968 969 970 971 972 973
    _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;

974 975
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappenEdition_->Text = loader->GetString("_whatWillHappen_0_");
Nicolas Jager's avatar
Nicolas Jager committed
976 977 978 979 980 981 982 983
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    } else {
        _RegisterStateEdition_->IsOn = false;

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

984 985
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappenEdition_->Text = loader->GetString("_whatWillHappen_1_");
Nicolas Jager's avatar
Nicolas Jager committed
986 987
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }
988

989 990 991 992 993 994 995 996 997
    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
998
    checkStateEditionMenu();
999 1000 1001 1002 1003 1004 1005 1006
}


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

1007
    account->name_ = _accountAliasTextBoxEdition_->Text;
1008

1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    if (account->accountType_ == "RING") {
        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
1023

1024
    RingD::instance->updateAccount(accountId);
1025

1026
    selectMenu(MenuOpen::ACCOUNTS_LIST);
1027

1028 1029
    updatePageContent();

Nicolas Jager's avatar
Nicolas Jager committed
1030
    if (_usernameValidEdition_->Visibility == Windows::UI::Xaml::Visibility::Visible && _usernameTextBoxEdition_->Text->Length() > 2)
atraczyk's avatar
atraczyk committed
1031
        RingD::instance->registerName_new(Utils::toString(account->accountID_), "", Utils::toString(_usernameTextBoxEdition_->Text));
1032 1033 1034 1035 1036
}


void RingClientUWP::Views::SmartPanel::_cancelAccountModification__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Nicolas Jager's avatar
Nicolas Jager committed
1037
    _accountEditionGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
1038 1039 1040 1041 1042 1043
}


void RingClientUWP::Views::SmartPanel::OnaccountUpdated(RingClientUWP::Account ^account)
{
    updatePageContent();
1044
    updateNotificationsState();
1045 1046 1047 1048
}

void RingClientUWP::Views::SmartPanel::_passwordBoxAccountCreationCheck__PasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Nicolas Jager's avatar
Nicolas Jager committed
1049
    checkStateAddAccountMenu();
1050
}
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074


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
1075
        _createAccountYes_->IsEnabled = (_accountAliasTextBox_->Text->IsEmpty()) ? false : true;
1076 1077 1078 1079 1080 1081
    }
}




Nicolas Jager's avatar
Nicolas Jager committed
1082
void RingClientUWP::Views::SmartPanel::_accountAliasTextBox__TextChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::TextChangedEventArgs^ e)
1083
{
Nicolas Jager's avatar
Nicolas Jager committed
1084 1085 1086 1087 1088 1089
    checkStateAddAccountMenu();
}

void RingClientUWP::Views::SmartPanel::_accountAliasTextBoxEdition__TextChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::TextChangedEventArgs^ e)
{
    checkStateEditionMenu();
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
}

Object ^ RingClientUWP::Views::CollapseEmptyString::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    auto stringValue = dynamic_cast<String^>(value);

    return (stringValue->IsEmpty())
           ? Windows::UI::Xaml::Visibility::Collapsed
           : Windows::UI::Xaml::Visibility::Visible;
}

Object ^ RingClientUWP::Views::CollapseEmptyString::ConvertBack(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    throw ref new Platform::NotImplementedException();
}

RingClientUWP::Views::CollapseEmptyString::CollapseEmptyString()
{}
1108 1109 1110 1111 1112 1113 1114 1115 1116


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;
}


1117 1118
void
RingClientUWP::Views::SmartPanel::_selectedAccountAvatarContainer__PointerReleased(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
1119
{
1120 1121 1122 1123
    create_task(Configuration::getProfileImageAsync()).then([&](task<BitmapImage^> image){
        try {
            if (auto bitmapImage = image.get()) {
                _selectedAccountAvatar_->ImageSource = bitmapImage;
atraczyk's avatar
atraczyk committed
1124
            }
1125 1126 1127
        }
        catch (Platform::Exception^ e) {
            EXC_(e);
1128 1129 1130 1131 1132 1133 1134 1135 1136
        }
    });
}

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;
}
1137 1138 1139 1140 1141 1142

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

}

Nicolas Jager's avatar
Nicolas Jager committed
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
// NAME SERVICE

void RingClientUWP::Views::SmartPanel::_registerOnBlockchainEdition__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto account = AccountListItemsViewModel::instance->_selectedItem->_account;
    RingD::instance->registerName_new(Utils::toString(account->accountID_), "", Utils::toString(_usernameTextBoxEdition_->Text));
}


void RingClientUWP::Views::SmartPanel::_usernameTextBoxEdition__KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
1154 1155
    auto accountId = AccountListItemsViewModel::instance->getSelectedAccountId();
    RingD::instance->lookUpName(Utils::toString(accountId), _usernameTextBoxEdition_->Text);
Nicolas Jager's avatar
Nicolas Jager committed
1156 1157 1158 1159 1160 1161 1162

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

    checkStateEditionMenu();
}

1163
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
1164
{
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
    // 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
1180
    if (menuOpen == MenuOpen::ACCOUNTS_LIST) { // if true, we did the lookup for a new account
1181 1182 1183 1184 1185 1186 1187 1188
        /* 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:
1189
            case LookupStatus::ERRORR:
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
                _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;
        }
1201

1202 1203 1204 1205 1206 1207
        auto currentNameCreation = Utils::toString(_usernameTextBox_->Text);
        if (currentNameCreation == name) {
            switch (status)
            {
            case LookupStatus::SUCCESS:
            case LookupStatus::INVALID_NAME:
1208
            case LookupStatus::ERRORR:
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
                _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;
1219
        }
1220

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

1226 1227 1228 1229
        // Try looking up a contact added by address
        if (contact == nullptr)
            contact = contactListModel->findContactByName(Utils::toPlatformString(address));

Nicolas Jager's avatar
Nicolas Jager committed
1230 1231 1232
        if (contact == nullptr)
            return;

1233 1234
        switch (status) {
        case LookupStatus::SUCCESS:
1235
        {
Nicolas Jager's avatar
Nicolas Jager committed
1236 1237 1238
            if (contact->_contactStatus == ContactStatus::WAITING_FOR_ACTIVATION) {
                contact->_contactStatus = ContactStatus::READY;
                contact->ringID_ = Utils::toPlatformString(address);
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
                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);
                }
1256
                contactListModel->saveContactsToFile();
Nicolas Jager's avatar
Nicolas Jager committed
1257
            }
1258
            else {
1259 1260 1261 1262 1263 1264
                /* 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);
1265 1266 1267 1268
                        SmartPanelItemsViewModel::instance->removeItem(item);
                    }
                }
            }
1269 1270 1271 1272 1273
            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;
            }
1274 1275
        }
        break;
1276
        case LookupStatus::INVALID_NAME:
1277 1278 1279 1280
        {
            MSG_("INVALID_NAME LOOKUP RESULT");
            std::regex sha1_regex("[0-9a-f]{40}");
            if (std::regex_match(name, sha1_regex)) {
1281
                /* first we check if some contact is registred with this ring id */
1282
                auto contactAlreadyRecorded = contactListModel->findContactByRingId(Utils::toPlatformString(name));
1283 1284 1285
                if (contactAlreadyRecorded) {
                    /* delete the contact added recently */
                    auto item = SmartPanelItemsViewModel::instance->findItem(contact);
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
                    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);
                    }
1298
                }
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
                else {
                    auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
                    ringTxtBxPlaceHolderDelay(loader->GetString("_contactsRingIdAdded_"), 5000);
                    contact->ringID_ = Utils::toPlatformString(name);
                    contact->_contactStatus = ContactStatus::READY;

                    if (contact && !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;
                    }
1310

1311 1312 1313 1314 1315 1316 1317 1318 1319
                    // send contact request
                    if (contact->_trustStatus == TrustStatus::UNKNOWN) {
                        auto vcard = Configuration::UserPreferences::instance->getVCard();
                        RingD::instance->sendContactRequest(accountId, address, vcard->asString());
                        contact->_trustStatus = TrustStatus::CONTACT_REQUEST_SENT;
                        SmartPanelItemsViewModel::instance->refreshFilteredItemsList();
                        SmartPanelItemsViewModel::instance->update(ViewModel::NotifyStrings::notifySmartPanelItem);
                    }
                }
Nicolas Jager's avatar
Nicolas Jager committed
1320 1321
            }
            else {
1322 1323
                auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
                ringTxtBxPlaceHolderDelay(loader->GetString("_contactsUsernameInvalid_"), 5000);
Nicolas Jager's avatar
Nicolas Jager committed
1324
                auto item = SmartPanelItemsViewModel::instance->findItem(contact);
1325
                contactListModel->deleteContact(contact);
Nicolas Jager's avatar
Nicolas Jager committed
1326 1327
                SmartPanelItemsViewModel::instance->removeItem(item);
            }
1328
            contactListModel->saveContactsToFile();
1329
            break;
1330
        }
1331 1332
        case LookupStatus::NOT_FOUND:
        {
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
            std::regex sha1_regex("[0-9a-f]{40}");
            if (std::regex_match(address, sha1_regex)) {
                RingD::instance->lookUpName(accountId, Utils::toPlatformString(address));
            }
            else {
                auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
                ringTxtBxPlaceHolderDelay(loader->GetString("_contactsUsernameNotFound_"), 5000);
                auto item = SmartPanelItemsViewModel::instance->findItem(contact);
                contactListModel->deleteContact(contact);
                SmartPanelItemsViewModel::instance->removeItem(item);
                contactListModel->saveContactsToFile();
            }
1345 1346 1347
            break;
        }
        case LookupStatus::ERRORR:
1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
            std::regex sha1_regex("[0-9a-f]{40}");
            if (std::regex_match(address, sha1_regex)) {
                RingD::instance->lookUpName(accountId, Utils::toPlatformString(address));
            }
            else {
                auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
                ringTxtBxPlaceHolderDelay(loader->GetString("_contactsNetworkError_"), 5000);
                auto item = SmartPanelItemsViewModel::instance->findItem(contact);
                contactListModel->deleteContact(contact);
                SmartPanelItemsViewModel::instance->removeItem(item);
                contactListModel->saveContactsToFile();
            }
1360 1361
            break;
        }
1362 1363 1364 1365 1366 1367

        auto unpoppedToast = RingD::instance->unpoppedToasts.find(Utils::toPlatformString(address));
        if (unpoppedToast != RingD::instance->unpoppedToasts.end()) {
            unpoppedToast->second(Utils::toPlatformString(name));
            RingD::instance->unpoppedToasts.erase(Utils::toPlatformString(address));
        }
Nicolas Jager's avatar
Nicolas Jager committed
1368 1369 1370 1371 1372 1373 1374 1375 1376
    }
}


void RingClientUWP::Views::SmartPanel::_RegisterState__Toggled(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto toggleSwitch = dynamic_cast<ToggleSwitch^>(sender);

    // avoid trouble when InitializeComponent is called.
1377
    if (_usernameTextBox_ == nullptr || _whatWillHappen_ == nullptr)
Nicolas Jager's avatar
Nicolas Jager committed
1378 1379 1380 1381 1382
        return;

    bool isPublic = toggleSwitch->IsOn;

    if (isPublic) {
1383 1384
        auto accountId = AccountListItemsViewModel::instance->getSelectedAccountId();
        RingD::instance->lookUpName(Utils::toString(accountId), _usernameTextBox_->Text);
Nicolas Jager's avatar
Nicolas Jager committed
1385
        _usernameTextBox_->IsEnabled = true;
1386 1387
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappen_->Text = loader->GetString("_whatWillHappen_0_");
Nicolas Jager's avatar
Nicolas Jager committed
1388 1389 1390
    }
    else {
        _usernameTextBox_->IsEnabled = false;
1391 1392
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappen_->Text = loader->GetString("_whatWillHappen_2_");
Nicolas Jager's avatar
Nicolas Jager committed
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
        _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }

    checkStateAddAccountMenu();
}

void RingClientUWP::Views::SmartPanel::_RegisterStateEdition__Toggled(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto toggleSwitch = dynamic_cast<ToggleSwitch^>(sender);

    // avoid trouble when InitializeComponent is called.
1405
    if (_usernameTextBoxEdition_ == nullptr || _whatWillHappen_ == nullptr)
Nicolas Jager's avatar
Nicolas Jager committed
1406 1407 1408 1409 1410
        return;

    bool isPublic = toggleSwitch->IsOn;

    if (isPublic) {
1411 1412
        auto accountId = AccountListItemsViewModel::instance->getSelectedAccountId();
        RingD::instance->lookUpName(Utils::toString(accountId), _usernameTextBoxEdition_->Text);
Nicolas Jager's avatar
Nicolas Jager committed
1413
        _usernameTextBoxEdition_->IsEnabled = true;
1414 1415
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappen_->Text = loader->GetString("_whatWillHappen_1_");
Nicolas Jager's avatar
Nicolas Jager committed
1416 1417 1418
    }
    else {
        _usernameTextBoxEdition_->IsEnabled = false;
1419 1420
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappen_->Text = loader->GetString("_whatWillHappen_2_");
Nicolas Jager's avatar
Nicolas Jager committed
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
        _usernameValidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }

    checkStateEditionMenu();
}


void RingClientUWP::Views::SmartPanel::_usernameTextBox__KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
1431 1432
    auto accountId = AccountListItemsViewModel::instance->getSelectedAccountId();
    RingD::instance->lookUpName(Utils::toString(accountId), _usernameTextBox_->Text);
Nicolas Jager's avatar
Nicolas Jager committed
1433 1434 1435 1436 1437

    _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}

1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463