SmartPanel.xaml.cpp 77.7 KB
Newer Older
Nicolas Jager's avatar
Nicolas Jager committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/***************************************************************************
 * Copyright (C) 2016 by Savoir-faire Linux                                *
 * Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com>              *
 * Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com>          *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/
#include "pch.h"
atraczyk's avatar
atraczyk committed
20
21
#include <string>
#include <direct.h>
Nicolas Jager's avatar
Nicolas Jager committed
22
#include "SmartPanel.xaml.h"
23
24
#include "qrencode.h"
#include <MemoryBuffer.h>   // IMemoryBufferByteAccess
Nicolas Jager's avatar
Nicolas Jager committed
25
26
27
28

using namespace Platform;

using namespace RingClientUWP;
29
using namespace RingClientUWP::Controls;
Nicolas Jager's avatar
Nicolas Jager committed
30
31
32
using namespace RingClientUWP::Views;
using namespace RingClientUWP::ViewModel;
using namespace Windows::Media::Capture;
33
using namespace Windows::Media::MediaProperties;
Nicolas Jager's avatar
Nicolas Jager committed
34
35
36
37
38
39
40
using namespace Windows::UI::Xaml;
using namespace Windows::Storage;
using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Windows::UI::Xaml::Shapes;
using namespace Windows::UI::Xaml::Media;
using namespace Concurrency;
using namespace Windows::Foundation;
41
42
43
using namespace Windows::Graphics::Imaging;
using namespace Windows::Foundation;
using namespace Concurrency;
Nicolas Jager's avatar
Nicolas Jager committed
44
45
using namespace Platform::Collections;

46
47
48
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Storage;
using namespace Windows::UI::Core;
49
using namespace Windows::System::Threading;
50

Nicolas Jager's avatar
Nicolas Jager committed
51
52
53
54
SmartPanel::SmartPanel()
{
    InitializeComponent();

Nicolas Jager's avatar
Nicolas Jager committed
55
56
    /* populate accounts listBox*/
    _accountsList_->ItemsSource = AccountListItemsViewModel::instance->itemsList;
57
58

    /* populate the smartlist */
59
    _smartList_->ItemsSource = SmartPanelItemsViewModel::instance->itemsList;
60

Nicolas Jager's avatar
Nicolas Jager committed
61
62
63
64

    /* populate the device list*/
///	_devicesIdList_ // not used so far

65
    /* connect delegates */
Nicolas Jager's avatar
Nicolas Jager committed
66
    Configuration::UserPreferences::instance->selectIndex += ref new SelectIndex([this](int index) {
67
68
69
70
        if (_accountsList_) {
            auto accountsListSize = dynamic_cast<Vector<AccountListItem^>^>(_accountsList_->ItemsSource)->Size;
            if (accountsListSize > index)
                _accountsList_->SelectedIndex = index;
71
72
73
74
            else {
                if (accountsListSize > 0)
                    _accountsList_->SelectedIndex = 0;
            }
75
        }
Nicolas Jager's avatar
Nicolas Jager committed
76
    });
77
78
79
80
81
    Configuration::UserPreferences::instance->loadProfileImage += ref new LoadProfileImage([this]() {
        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);
atraczyk's avatar
atraczyk committed
82
    });
83
    ContactsViewModel::instance->contactDataModified += ref new ContactDataModified([this](Contact^ contact) {
84
    });
atraczyk's avatar
atraczyk committed
85
86
87
88
    AccountsViewModel::instance->updateScrollView += ref new UpdateScrollView([this]() {
        _accountsListScrollView_->UpdateLayout();
        _accountsListScrollView_->ScrollToVerticalOffset(_accountsListScrollView_->ScrollableHeight);
    });
89
90
91
    RingD::instance->incomingCall += ref new RingClientUWP::IncomingCall([&](
    String^ accountId, String^ callId, String^ from) {
        ///auto from = call->from;
Nicolas Jager's avatar
Nicolas Jager committed
92
        auto contact = ContactsViewModel::instance->findContactByRingId(from);
93
94
95
96
97
98
99
100
101

        if (contact == nullptr)
            contact = ContactsViewModel::instance->addNewContact(from, from); // contact checked inside addNewContact.

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

102
        RingD::instance->lookUpAddress(from);
103
104
        contact->_accountIdAssociated = accountId;

105
        auto item = SmartPanelItemsViewModel::instance->findItem(contact);
106
        item->_callId = callId;
Nicolas Jager's avatar
Nicolas Jager committed
107

108
109
110
111
112
113
114
115
        /* move the item of the top of the list */
        unsigned int index;
        if (_smartList_->Items->IndexOf(item, &index)) {
            SmartPanelItemsViewModel::instance->moveItemToTheTop(item);
            _smartList_->UpdateLayout();
            _smartList_->ScrollIntoView(item);
        }

116
    });
Nicolas Jager's avatar
Nicolas Jager committed
117
    RingD::instance->stateChange += ref new StateChange([this](String^ callId, CallStatus state, int code) {
118

119
        auto item = SmartPanelItemsViewModel::instance->findItem(callId);
120
121
122
123
124
125

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

126
127
        switch (state) {
        case CallStatus::NONE:
128
        case CallStatus::ENDED:
129
        {
atraczyk's avatar
atraczyk committed
130
            bool isInCall = false;
Nicolas Jager's avatar
Nicolas Jager committed
131
            for (auto item : SmartPanelItemsViewModel::instance->itemsList) { // WTF!!! item instead of it!!!! (XXX)
atraczyk's avatar
atraczyk committed
132
133
134
135
136
137
138
139
                if (item->_callId && item->_callStatus == CallStatus::IN_PROGRESS) {
                    isInCall = true;
                    RingD::instance->currentCallId = item->_callId;
                    break;
                }
            }
            if (!isInCall)
                _settingsMenuButton_->Visibility = VIS::Visible;
140
141
142
143
            break;
        }
        case CallStatus::IN_PROGRESS:
        {
144
            SmartPanelItemsViewModel::instance->_selectedItem = item;
145
146
147
            summonVideoPage();
            break;
        }
148
149
150
        case CallStatus::PEER_PAUSED:
        case CallStatus::PAUSED:
        {
151
            SmartPanelItemsViewModel::instance->_selectedItem = item;
152
153
154
            summonVideoPage();
            break;
        }
155
156
157
        default:
            break;
        }
158
159

    });
160
    RingD::instance->devicesListRefreshed += ref new RingClientUWP::DevicesListRefreshed(this, &RingClientUWP::Views::SmartPanel::OndevicesListRefreshed);
161
162
163
164

    ContactsViewModel::instance->contactAdded += ref new ContactAdded([this](Contact^ contact) {
        auto smartPanelItem = ref new SmartPanelItem();
        smartPanelItem->_contact = contact;
165
        SmartPanelItemsViewModel::instance->itemsList->Append(smartPanelItem);
166
    });
Nicolas Jager's avatar
Nicolas Jager committed
167

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

172
173
174
    RingD::instance->finishCaptureDeviceEnumeration += ref new RingClientUWP::FinishCaptureDeviceEnumeration([this]() {
        populateVideoDeviceSettingsComboBox();
    });
175
176
    RingD::instance->registrationStateErrorGeneric += ref new RingClientUWP::RegistrationStateErrorGeneric(this, &RingClientUWP::Views::SmartPanel::OnregistrationStateErrorGeneric);
    RingD::instance->registrationStateRegistered += ref new RingClientUWP::RegistrationStateRegistered(this, &RingClientUWP::Views::SmartPanel::OnregistrationStateRegistered);
177
    RingD::instance->callPlaced += ref new RingClientUWP::CallPlaced(this, &RingClientUWP::Views::SmartPanel::OncallPlaced);
178
    RingD::instance->incomingAccountMessage += ref new RingClientUWP::IncomingAccountMessage(this, &RingClientUWP::Views::SmartPanel::OnincomingAccountMessage);
Nicolas Jager's avatar
Nicolas Jager committed
179

180
    selectMenu(MenuOpen::CONTACTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
181
182
183
184
185
}

void
RingClientUWP::Views::SmartPanel::updatePageContent()
{
Nicolas Jager's avatar
Nicolas Jager committed
186
187
    auto accountListItem = AccountListItemsViewModel::instance->_selectedItem;
    if (!accountListItem)
Nicolas Jager's avatar
Nicolas Jager committed
188
189
        return;

190
191
192
    auto name = accountListItem->_account->name_; // refacto remove name variable and use the link directly on the next line... like _upnpnState..._

    accountListItem->_isSelected = true;
Nicolas Jager's avatar
Nicolas Jager committed
193

Nicolas Jager's avatar
Nicolas Jager committed
194
195
    Configuration::UserPreferences::instance->PREF_ACCOUNT_INDEX = _accountsList_->SelectedIndex;
    Configuration::UserPreferences::instance->save();
Nicolas Jager's avatar
Nicolas Jager committed
196

Nicolas Jager's avatar
Nicolas Jager committed
197
    _selectedAccountName_->Text = name; // refacto : bind this in xaml directly
198
///    _devicesIdList_->ItemsSource = account->_devicesIdList;
Nicolas Jager's avatar
Nicolas Jager committed
199
    _deviceId_->Text = accountListItem->_account->_deviceId; /* this is the current device ...
Nicolas Jager's avatar
Nicolas Jager committed
200
    ... in the way to get all associated devices, we have to querry the daemon : */
201

Nicolas Jager's avatar
Nicolas Jager committed
202
    _devicesMenuButton_->Visibility = (accountListItem->_account->accountType_ == "RING")
203
204
                                      ? Windows::UI::Xaml::Visibility::Visible
                                      : Windows::UI::Xaml::Visibility::Collapsed;
205

Nicolas Jager's avatar
Nicolas Jager committed
206
    _shareMenuButton_->Visibility = (accountListItem->_account->accountType_ == "RING")
207
208
                                    ? Windows::UI::Xaml::Visibility::Visible
                                    : Windows::UI::Xaml::Visibility::Collapsed;
209
210

    _upnpState_->IsOn = accountListItem->_account->_upnpState;
Nicolas Jager's avatar
Nicolas Jager committed
211
212
213
214
215
216
217
218
219
220

    if (_upnpState_->IsOn) {
        _usernameTextBoxEdition_->IsEnabled = true;
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;

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

Nicolas Jager's avatar
Nicolas Jager committed
221
222
223
224
}

void RingClientUWP::Views::SmartPanel::setMode(RingClientUWP::Views::SmartPanel::Mode mode)
{
225
226
    _rowRingTxtBx_->Height = (mode == RingClientUWP::Views::SmartPanel::Mode::Normal)? 40 : 0;
    selectMenu(MenuOpen::CONTACTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
227
228
229
230
231
232
}

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

234
    _createAccountYes_->IsEnabled = false;
235
236
237

    _accountTypeComboBox_->SelectedIndex = 0;
    _upnpStateAccountCreation_->IsOn = true;
Nicolas Jager's avatar
Nicolas Jager committed
238
239
240
    _RegisterStateEdition_->IsOn = true;
    _accountAliasTextBox_->Text = "";
    _usernameTextBox_->Text = "";
Nicolas Jager's avatar
Nicolas Jager committed
241
242

    checkStateAddAccountMenu();
Nicolas Jager's avatar
Nicolas Jager committed
243
244
245
246
}

void RingClientUWP::Views::SmartPanel::_createAccountYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
247
248
    switch (_accountTypeComboBox_->SelectedIndex) {
    case 0: /* RING account */
249
    {
Nicolas Jager's avatar
Nicolas Jager committed
250
251
252
253
254
        RingD::instance->createRINGAccount(_accountAliasTextBox_->Text
                                           , _ringPasswordBoxAccountCreation_->Password
                                           , _upnpStateAccountCreation_->IsOn
                                           , (_RegisterState_->IsOn) ? _usernameTextBox_->Text : "");

255
256
        _ringPasswordBoxAccountCreation_->Password = "";
        _ringPasswordBoxAccountCreationCheck_->Password = "";
Nicolas Jager's avatar
Nicolas Jager committed
257
        _usernameTextBox_->Text = "";
atraczyk's avatar
atraczyk committed
258
        break;
259
260
    }
    break;
261
    case 1: /* SIP account */
262
    {
Nicolas Jager's avatar
Nicolas Jager committed
263
        RingD::instance->createSIPAccount(_accountAliasTextBox_->Text, _sipPasswordBoxAccountCreation_->Password, _sipHostnameTextBox_->Text, _sipUsernameTextBox_->Text);
264
265
266
        _sipPasswordBoxAccountCreation_->Password = "";
        _sipUsernameTextBox_->Text = "";
        _sipHostnameTextBox_->Text = "";
267
268
269
270
        break;
    }
    default:
        break;
atraczyk's avatar
atraczyk committed
271
    }
272
273
274

    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _accountsMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
275
276

    selectMenu(MenuOpen::ACCOUNTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
277
278
279
280
281
}


void RingClientUWP::Views::SmartPanel::_createAccountNo__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
282
    selectMenu(MenuOpen::ACCOUNTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
283
284
}

285
286
/* using the usual selection behaviour doesn't allow us to deselect by simple click. The selection is managed
   by Grid_PointerReleased */
Nicolas Jager's avatar
Nicolas Jager committed
287
288
289
void
SmartPanel::_smartList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
290
291
292
    auto listbox = dynamic_cast<ListBox^>(sender); // same as _smartList_
    listbox->SelectedItem = nullptr;
    return;
Nicolas Jager's avatar
Nicolas Jager committed
293
294
295
296
297
298
}

void
SmartPanel::_accountList__SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
    auto listbox = safe_cast<ListBox^>(sender);
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    // disable deselction from listbox
    if (listbox->SelectedItem == nullptr)
    {
        if (e->RemovedItems->Size > 0)
        {
            Object^ itemToReselect = e->RemovedItems->GetAt(0);
            for each (auto item in listbox->Items) {
                if (item == itemToReselect) {
                    listbox->SelectedItem = itemToReselect;
                    continue;
                }
            }
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
313
314
    auto account = safe_cast<AccountListItem^>(listbox->SelectedItem);
    AccountListItemsViewModel::instance->_selectedItem = account;
315

Nicolas Jager's avatar
Nicolas Jager committed
316
317
318
    updatePageContent();
}

319
320
void RingClientUWP::Views::SmartPanel::_ringTxtBx__Click(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
321
    RingD::instance->lookUpName(_ringTxtBx_->Text);
322
323
}

324
325
void
RingClientUWP::Views::SmartPanel::_rejectIncomingCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
326
327
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
328
329
330
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
331
332
            auto callId = item->_callId;
            RingD::instance->refuseIncommingCall(callId);
333
334
        }
    }
335
336
}

337
338
void
RingClientUWP::Views::SmartPanel::_acceptIncomingCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
339
340
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
341
342
343
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
344
            auto callId = item->_callId;
345
346
347
348
349

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

350

351
            RingD::instance->acceptIncommingCall(callId);
352
353
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
354
355
356
357
358
359
}

void
SmartPanel::_callContact__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
360
    if (button) {
361
362
363
        /* force to hide the button, avoid attempting to call several times... */
        button->Visibility = Windows::UI::Xaml::Visibility::Collapsed;

atraczyk's avatar
atraczyk committed
364
365
        _settingsMenuButton_->Visibility = VIS::Collapsed;

366
367
368
369
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
            auto contact = item->_contact;
            if (contact)
370
371
372
373
374
            {
                for (auto it : SmartPanelItemsViewModel::instance->itemsList)
                    if (it->_callStatus == CallStatus::IN_PROGRESS)
                        RingD::instance->pauseCall(Utils::toString(it->_callId));

375
                if (item->_callStatus == CallStatus::ENDED || item->_callStatus == CallStatus::NONE) {
Nicolas Jager's avatar
Nicolas Jager committed
376
                    item->_callStatus = CallStatus::OUTGOING_REQUESTED;
377
                    RingD::instance->placeCall(contact);
Nicolas Jager's avatar
Nicolas Jager committed
378
                    item->_contact->_lastTime = "looking for " + item->_contact->_name + ".";
379
380
                }

381
382
383
384
385
386
387
                /* move the item of the top of the list */
                unsigned int index;
                if (_smartList_->Items->IndexOf(item, &index)) {
                    SmartPanelItemsViewModel::instance->moveItemToTheTop(item);
                    _smartList_->UpdateLayout();
                    _smartList_->ScrollIntoView(item);
                }
388
            }
389
390
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
391
392
393
394
395
}

void RingClientUWP::Views::SmartPanel::_cancelCallBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto button = dynamic_cast<Button^>(e->OriginalSource);
396
397
398
399
    if (button) {
        auto item = dynamic_cast<SmartPanelItem^>(button->DataContext);
        if (item) {
            RingD::instance->cancelOutGoingCall2(item->_callId);
400
            item->_callStatus = CallStatus::TERMINATING;
401
402
403
            return;
        }
    }
Nicolas Jager's avatar
Nicolas Jager committed
404
405
406
407
408
409
410
}

void RingClientUWP::Views::SmartPanel::Grid_PointerEntered(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);

Nicolas Jager's avatar
Nicolas Jager committed
411
    for (auto it : SmartPanelItemsViewModel::instance->itemsList) {
412
        it->_isHovered = false;
Nicolas Jager's avatar
Nicolas Jager committed
413
414
    }

415
    item->_isHovered = true;
Nicolas Jager's avatar
Nicolas Jager committed
416
417
418
419
420
421
422
423
}


void RingClientUWP::Views::SmartPanel::Grid_PointerExited(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);

424
425
426
427
    // 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
428
429
}

430
431
void RingClientUWP::Views::SmartPanel::generateQRcode()
{
Nicolas Jager's avatar
Nicolas Jager committed
432
    auto ringId = AccountListItemsViewModel::instance->_selectedItem->_account->ringID_;
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
    auto ringId2 = Utils::toString(ringId);

    _ringId_->Text = ringId;

    auto qrcode = QRcode_encodeString(ringId2.c_str(),
                                      0, //Let the version be decided by libqrencode
                                      QR_ECLEVEL_L, // Lowest level of error correction
                                      QR_MODE_8, // 8-bit data mode
                                      1);

    if (!qrcode) {
        WNG_("Failed to generate QR code: ");
        return;
    }

    const int STRETCH_FACTOR = 4;
    const int widthQrCode = qrcode->width;
    const int widthBitmap = STRETCH_FACTOR * widthQrCode;

    unsigned char* qrdata = qrcode->data;

    auto frame = ref new SoftwareBitmap(BitmapPixelFormat::Bgra8, widthBitmap, widthBitmap, BitmapAlphaMode::Premultiplied);

    const int BYTES_PER_PIXEL = 4;


    BitmapBuffer^ buffer = frame->LockBuffer(BitmapBufferAccessMode::ReadWrite);
    IMemoryBufferReference^ reference = buffer->CreateReference();

    Microsoft::WRL::ComPtr<IMemoryBufferByteAccess> byteAccess;
    if (SUCCEEDED(reinterpret_cast<IUnknown*>(reference)->QueryInterface(IID_PPV_ARGS(&byteAccess))))
    {
        byte* data;
        unsigned capacity;
        byteAccess->GetBuffer(&data, &capacity);

        auto desc = buffer->GetPlaneDescription(0);

        unsigned char* row, * p;
        p = qrcode->data;

        for (int u = 0 ; u < widthBitmap ; u++) {
            for (int v = 0; v < widthBitmap; v++) {
476
477
                int x = static_cast<int>((float)u / (float)widthBitmap * (float)widthQrCode);
                int y = static_cast<int>((float)v / (float)widthBitmap * (float)widthQrCode);
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498

                auto currPixelRow = desc.StartIndex + desc.Stride * u + BYTES_PER_PIXEL * v;
                row = (p + (y * widthQrCode));

                if (*(row + x) & 0x1) {
                    data[currPixelRow + 3] = 255;
                }
            }
        }

    }
    delete reference;
    delete buffer;

    auto sbSource = ref new Media::Imaging::SoftwareBitmapSource();

    sbSource->SetBitmapAsync(frame);

    _selectedAccountQrCode_->Source = sbSource;
}

Nicolas Jager's avatar
Nicolas Jager committed
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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
516
517
        bool isUsernameValid = (_usernameValid_->Visibility == Windows::UI::Xaml::Visibility::Visible
                                && !_usernameTextBox_->Text->IsEmpty()) ? true : false;
Nicolas Jager's avatar
Nicolas Jager committed
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

        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
544
545
546
547
548
549
550
        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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623

        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;
                //_registerOnBlockchainEdition_->IsEnabled = true;
            } else {
                _acceptAccountModification_->IsEnabled = false;
                //_registerOnBlockchainEdition_->IsEnabled = false;
            }
        else if (isAccountAlias)
            _acceptAccountModification_->IsEnabled = true;
        else
            _acceptAccountModification_->IsEnabled = false;
    }
    else {
        if (isAccountAlias)
            _acceptAccountModification_->IsEnabled = true;
        else
            _acceptAccountModification_->IsEnabled = false;
    }
}

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
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]()
        {
            _ringTxtBx_->PlaceholderText = "";
        }));
    }), delay);
}

640
641
642
643
644
645
646
647
648
649
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
650
651
652
Object ^ RingClientUWP::Views::IncomingVisibility::Convert(Object ^ value, Windows::UI::Xaml::Interop::TypeName targetType, Object ^ parameter, String ^ language)
{
    auto state = static_cast<CallStatus>(value);
653

654
    if (state == CallStatus::INCOMING_RINGING)
Nicolas Jager's avatar
Nicolas Jager committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
        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);

673
674
675
    if (state == CallStatus::SEARCHING
            || state == CallStatus::OUTGOING_RINGING
            || state == CallStatus::OUTGOING_REQUESTED)
Nicolas Jager's avatar
Nicolas Jager committed
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
        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);

693
    if (state == CallStatus::NONE || state == CallStatus::ENDED)
Nicolas Jager's avatar
Nicolas Jager committed
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
        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()
{}

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

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

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

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

RingClientUWP::Views::NewMessageBubleNotification::NewMessageBubleNotification()
{}
724
725
726

void RingClientUWP::Views::SmartPanel::_addDevice__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
727
728
    _devicesMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _addingDeviceGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
Nicolas Jager's avatar
Nicolas Jager committed
729
    _closePin_->Visibility = Windows::UI::Xaml::Visibility::Visible;
730
731
732
733
734
}


void RingClientUWP::Views::SmartPanel::OndevicesListRefreshed(Platform::Collections::Vector<Platform::String ^, std::equal_to<Platform::String ^>, true> ^devicesList)
{
Nicolas Jager's avatar
Nicolas Jager committed
735
    _waitingDevicesList_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
Nicolas Jager's avatar
Nicolas Jager committed
736
    AccountListItemsViewModel::instance->_selectedItem->_account->_devicesIdList = devicesList;
737
    _devicesIdList_->ItemsSource = devicesList;
Nicolas Jager's avatar
Nicolas Jager committed
738
    _devicesIdList_->Visibility = Windows::UI::Xaml::Visibility::Visible;
739
740
741
742
743
}


void RingClientUWP::Views::SmartPanel::_pinGeneratorYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
744
    requestPin();
745
746
747
748
749
750
751
}


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
752
    _passwordForPinGenerator_->Password = "";
753
754

    selectMenu(MenuOpen::CONTACTS_LIST);
755
}
756

757
758
759
760

void RingClientUWP::Views::SmartPanel::OnexportOnRingEnded(Platform::String ^accountId, Platform::String ^pin)
{
    _waitingAndResult_->Text = pin;
Nicolas Jager's avatar
Nicolas Jager committed
761
762
763
764
765
766
767
    _closePin_->Visibility = Windows::UI::Xaml::Visibility::Visible;

}


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

    // refacto : do something better...
    _waitingAndResult_->Text = "Exporting account on the Ring...";
772
}
773
774
775
776


void RingClientUWP::Views::SmartPanel::_shareMenuDone__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
777
    selectMenu(MenuOpen::CONTACTS_LIST);
778
}
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801

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

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)
{
    //auto accountId = static_cast<bool(value);

Nicolas Jager's avatar
Nicolas Jager committed
802
    if (/*AccountListItemsViewModel::instance->_selectedItem->_account->_isSelected ==*/ (bool)value == true)
803
804
805
806
807
808
809
810
811
812
813
814
        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()
{}
815
816
817
818
819


void RingClientUWP::Views::SmartPanel::_editAccountMenuButton__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    auto account = AccountListItemsViewModel::instance->_selectedItem->_account;
Nicolas Jager's avatar
Nicolas Jager committed
820
821
822
823
824
825
826

    auto volatileAccountDetails = RingD::instance->getVolatileAccountDetails(account);

    _accountAliasTextBoxEdition_->Text = account->name_;
    _accountEditionGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _deleteAccountEdition_->IsOn = false;
    _deleteAccountEdition_->IsEnabled = (AccountListItemsViewModel::instance->itemsList->Size > 1)? true : false;
827
    _createAccountYes_->IsEnabled = false;
Nicolas Jager's avatar
Nicolas Jager committed
828
829
830
831
832
833
834
835
836
837
838

    _whatWilHappendeleteRingAccountEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _whatWilHappendeleteSipAccountEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;

    _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;
    _sipHostnameEdition_->Text = account->_sipHostname;
839
    _sipUsernameEditionTextBox_->Text = account->_sipUsername;
Nicolas Jager's avatar
Nicolas Jager committed
840
841
842
843
844
845
846
847
848
849
850
851
852
853
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;

        //_registerOnBlockchainEdition_->IsEnabled = false;
        _whatWilHappenEdition_->Text = "You are already registered.";
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    } else {
        _RegisterStateEdition_->IsOn = false;

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

        //_registerOnBlockchainEdition_->IsEnabled = false;
        _whatWilHappenEdition_->Text = "You are not yet registered.";
        _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }
865

Nicolas Jager's avatar
Nicolas Jager committed
866
    checkStateEditionMenu();
867
868
869
870
871
872
873
874
875
}


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


876
877
878
    // mettre ca en visibility du bouton delete
    auto accountsListSize = dynamic_cast<Vector<AccountListItem^>^>(_accountsList_->ItemsSource)->Size;

879
    /* if the delete button is toggled, just delete the account */
Nicolas Jager's avatar
Nicolas Jager committed
880
    if (_deleteAccountEdition_->IsOn == true && accountsListSize > 1) {
881
882
883
884
        AccountListItem^ item;
        for each (item in AccountListItemsViewModel::instance->itemsList)
            if (item->_account->accountID_ == accountId)
                break;
885

886
887
        if (item)
            AccountListItemsViewModel::instance->removeItem(item);
888

889
        RingD::instance->deleteAccount(accountId);
890

891
    } else { /* otherwise edit the account */
892

Nicolas Jager's avatar
Nicolas Jager committed
893
894
        account->name_ = _accountAliasTextBoxEdition_->Text;

Nicolas Jager's avatar
Nicolas Jager committed
895
896
897
898
899
900
901
902
        if (account->accountType_ == "RING") {
            account->_upnpState = _upnpState_->IsOn;
        }
        else {
            account->_sipHostname = _sipHostnameEdition_->Text;
            account->_sipUsername = _sipUsernameEditionTextBox_->Text;
            account->_sipPassword = _sipPasswordEdition_->Password;
        }
903
904
905

        RingD::instance->updateAccount(accountId);
    }
906

907
    selectMenu(MenuOpen::CONTACTS_LIST);
Nicolas Jager's avatar
Nicolas Jager committed
908
909

    if (_usernameValidEdition_->Visibility == Windows::UI::Xaml::Visibility::Visible && _usernameTextBoxEdition_->Text->Length() > 2)
atraczyk's avatar
atraczyk committed
910
        RingD::instance->registerName_new(Utils::toString(account->accountID_), "", Utils::toString(_usernameTextBoxEdition_->Text));
911
912
913
914
915
}


void RingClientUWP::Views::SmartPanel::_cancelAccountModification__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Nicolas Jager's avatar
Nicolas Jager committed
916
    _accountEditionGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
917
918
919
920
921
922
}


void RingClientUWP::Views::SmartPanel::OnaccountUpdated(RingClientUWP::Account ^account)
{
    updatePageContent();
923
924
925
926
}

void RingClientUWP::Views::SmartPanel::_passwordBoxAccountCreationCheck__PasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
Nicolas Jager's avatar
Nicolas Jager committed
927
    checkStateAddAccountMenu();
928
}
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953


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;
        _upnpStateAccountCreation_->IsOn = true;
    } 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
954
        _createAccountYes_->IsEnabled = (_accountAliasTextBox_->Text->IsEmpty()) ? false : true;
955
956
957
958
959
960
    }
}




Nicolas Jager's avatar
Nicolas Jager committed
961
void RingClientUWP::Views::SmartPanel::_accountAliasTextBox__TextChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::TextChangedEventArgs^ e)
962
{
Nicolas Jager's avatar
Nicolas Jager committed
963
964
965
966
967
968
    checkStateAddAccountMenu();
}

void RingClientUWP::Views::SmartPanel::_accountAliasTextBoxEdition__TextChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::TextChangedEventArgs^ e)
{
    checkStateEditionMenu();
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
}

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()
{}
987
988
989
990
991
992
993
994
995


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
996
997
void
RingClientUWP::Views::SmartPanel::_selectedAccountAvatarContainer__PointerReleased(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
998
999
{
    CameraCaptureUI^ cameraCaptureUI = ref new CameraCaptureUI();
1000
    cameraCaptureUI->PhotoSettings->Format = CameraCaptureUIPhotoFormat::Jpeg;
1001
    cameraCaptureUI->PhotoSettings->CroppedSizeInPixels = Size(40, 40);
1002
1003
1004
1005
1006
1007
1008
1009

    create_task(cameraCaptureUI->CaptureFileAsync(CameraCaptureUIMode::Photo))
    .then([this](StorageFile^ photoFile)
    {
        if (photoFile != nullptr) {
            auto brush = ref new ImageBrush();

            auto circle = ref new Ellipse();
1010
1011
            circle->Height = 40;
            circle->Width = 40;
1012
1013
1014
1015
1016
            auto path = photoFile->Path;
            auto uri = ref new Windows::Foundation::Uri(path);
            auto bitmapImage = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage();
            bitmapImage->UriSource = uri;

atraczyk's avatar
atraczyk committed
1017
            std::string fileBuffer = Utils::getStringFromFile(Utils::toString(photoFile->Path));
atraczyk's avatar
atraczyk committed
1018
1019
            std::string profilePath = RingD::instance->getLocalFolder() + ".profile";
            _mkdir(profilePath.c_str());
atraczyk's avatar
atraczyk committed
1020
            std::ofstream file((profilePath + "\\profile_image.png"),
1021
                               std::ios::out | std::ios::trunc | std::ios::binary);
atraczyk's avatar
atraczyk committed
1022
1023
1024
1025
            if (file.is_open()) {
                file << fileBuffer;
                file.close();
            }
1026

atraczyk's avatar
atraczyk committed
1027
            Configuration::UserPreferences::instance->PREF_PROFILE_HASPHOTO = true;
1028
1029
1030
            Configuration::UserPreferences::instance->save();

            brush->ImageSource = bitmapImage;
atraczyk's avatar
atraczyk committed
1031
            circle->Fill = brush;
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
            _selectedAccountAvatar_->ImageSource = bitmapImage;
        }
    });
}


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;
}
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056


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

}


void RingClientUWP::Views::SmartPanel::Grid_PointerMoved(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    auto grid = dynamic_cast<Grid^>(sender);
    auto item = dynamic_cast<SmartPanelItem^>(grid->DataContext);

    for (auto it : SmartPanelItemsViewModel::instance->itemsList)
1057
        it->_isHovered = false;
Nicolas Jager's avatar
Nicolas Jager committed
1058

1059
    item->_isHovered = true;
1060
}
1061

Nicolas Jager's avatar
Nicolas Jager committed
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
// 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(account->accountID_, "", _usernameTextBoxEdition_->Text);
    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)
{
    RingD::instance->lookUpName(_usernameTextBoxEdition_->Text);

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

    checkStateEditionMenu();
}

1083
void RingClientUWP::Views::SmartPanel::OnregisteredNameFound(RingClientUWP::LookupStatus status, const std::string& address, const std::string& name)
Nicolas Jager's avatar
Nicolas Jager committed
1084
{
Nicolas Jager's avatar
Nicolas Jager committed
1085
    if (menuOpen == MenuOpen::ACCOUNTS_LIST) { // if true, we did the lookup for a new account
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
        /* 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:
                _usernameValidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
                _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;
                break;
            case LookupStatus::INVALID_NAME:
                _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;
            case LookupStatus::ERRORR:
                _usernameValidEdition_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
                _usernameInvalidEdition_->Visibility = Windows::UI::Xaml::Visibility::Visible;
                break;
            }
            checkStateEditionMenu();
            return;
        }
1112

1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
        auto currentNameCreation = Utils::toString(_usernameTextBox_->Text);
        if (currentNameCreation == name) {
            switch (status)
            {
            case LookupStatus::SUCCESS:
                _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
                _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
                break;
            case LookupStatus::INVALID_NAME:
                _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;
            case LookupStatus::ERRORR:
                _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
                _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
                break;
            }
            checkStateAddAccountMenu();
            return;
1136
        }
1137

Nicolas Jager's avatar
Nicolas Jager committed
1138
1139
1140
1141
1142
1143
1144
    }
    else { // if false, we are looking for a registered user (contact)
        auto contact = ContactsViewModel::instance->findContactByName(Utils::toPlatformString(name));

        if (contact == nullptr)
            return;

1145
1146
        switch (status) {
        case LookupStatus::SUCCESS:
1147
        {
Nicolas Jager's avatar
Nicolas Jager committed
1148
1149
1150
1151
1152
1153
            if (contact->_contactStatus == ContactStatus::WAITING_FOR_ACTIVATION) {
                contact->_contactStatus = ContactStatus::READY;
                contact->ringID_ = Utils::toPlatformString(address);
                ringTxtBxPlaceHolderDelay("username found and added.", 5000);
                ContactsViewModel::instance->saveContactsToFile();
            }
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
            else {
                /* in that case we delete a possible suroggate */
                for each (Contact^ co in ContactsViewModel::instance->contactsList) {
                    if (co->_contactStatus == ContactStatus::WAITING_FOR_ACTIVATION
                            && co->_name == Utils::toPlatformString(name)) {
                        auto item = SmartPanelItemsViewModel::instance->findItem(co);
                        ContactsViewModel::instance->deleteContact(co);
                        SmartPanelItemsViewModel::instance->removeItem(item);
                    }

                }

            }

            /* open the text message page */
            auto item = SmartPanelItemsViewModel::instance->findItem(contact);
1170
1171
1172
1173
1174

            //if (item->_callStatus != CallStatus::INCOMING_RINGING) {// refacto : do something better
            //    SmartPanelItemsViewModel::instance->_selectedItem = item;
            //    summonMessageTextPage();
            //}
1175
1176
        }
        break;
1177
        case LookupStatus::INVALID_NAME:
Nicolas Jager's avatar
Nicolas Jager committed
1178
            if (name.length() == 40) {
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190

                /* first we check if some contact is registred with this ring id */
                auto contactAlreadyRecorded = ContactsViewModel::instance->findContactByRingId(Utils::toPlatformString(name));
                if (contactAlreadyRecorded) {
                    ringTxtBxPlaceHolderDelay("you already have a contact with this ring id.", 5000);
                    /* delete the contact added recently */
                    auto item = SmartPanelItemsViewModel::instance->findItem(contact);
                    ContactsViewModel::instance->deleteContact(contact);
                    SmartPanelItemsViewModel::instance->removeItem(item);

                    /* open the message text with the contact already recorder*/
                    item = SmartPanelItemsViewModel::instance->findItem(contactAlreadyRecorded);
1191
1192
1193
1194
1195

                    //if (item->_callStatus != CallStatus::INCOMING_RINGING) { // refacto : do something better
                    //    SmartPanelItemsViewModel::instance->_selectedItem = item;
                    //    summonMessageTextPage();
                    //}
1196
1197
1198
1199
                    break;
                }

                ringTxtBxPlaceHolderDelay("ring id added.", 5000); // refacto : we should check if it's an actual ring id
Nicolas Jager's avatar
Nicolas Jager committed
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
                contact->ringID_ = Utils::toPlatformString(name);
                contact->_contactStatus = ContactStatus::READY;
                ContactsViewModel::instance->saveContactsToFile();
            }
            else {
                ringTxtBxPlaceHolderDelay("username invalid.", 5000);
                auto item = SmartPanelItemsViewModel::instance->findItem(contact);
                ContactsViewModel::instance->deleteContact(contact);
                SmartPanelItemsViewModel::instance->removeItem(item);
                ContactsViewModel::instance->saveContactsToFile();
            }
1211
1212
1213
            break;
        case LookupStatus::NOT_FOUND:
        {
Nicolas Jager's avatar
Nicolas Jager committed
1214
1215
1216
1217
1218
            ringTxtBxPlaceHolderDelay("username not found.", 5000);
            auto item = SmartPanelItemsViewModel::instance->findItem(contact);
            ContactsViewModel::instance->deleteContact(contact);
            SmartPanelItemsViewModel::instance->removeItem(item);
            ContactsViewModel::instance->saveContactsToFile();
1219
1220
1221
            break;
        }
        case LookupStatus::ERRORR:
Nicolas Jager's avatar
Nicolas Jager committed
1222
1223
1224
1225
1226
            ringTxtBxPlaceHolderDelay("network error!", 5000);
            auto item = SmartPanelItemsViewModel::instance->findItem(contact);
            ContactsViewModel::instance->deleteContact(contact);
            SmartPanelItemsViewModel::instance->removeItem(item);
            ContactsViewModel::instance->saveContactsToFile();
1227
1228
            break;
        }
Nicolas Jager's avatar
Nicolas Jager committed
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
    }
}


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.
    if (_usernameTextBox_ == nullptr || _whatWilHappen_ == nullptr)
        return;

    bool isPublic = toggleSwitch->IsOn;

    if (isPublic) {
        RingD::instance->lookUpName(_usernameTextBox_->Text);
        _usernameTextBox_->IsEnabled = true;
        _whatWilHappen_->Text = "peoples will find you with your username.";
    }
    else {
        _usernameTextBox_->IsEnabled = false;
        _whatWilHappen_->Text = "you'll have to send your ringId.";
        _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.
    if (_usernameTextBoxEdition_ == nullptr /*|| _registerOnBlockchainEdition_ == nullptr*/ || _whatWilHappen_ == nullptr)
        return;

    bool isPublic = toggleSwitch->IsOn;

    if (isPublic) {
        RingD::instance->lookUpName(_usernameTextBoxEdition_->Text);
        _usernameTextBoxEdition_->IsEnabled = true;
        //_registerOnBlockchainEdition_->IsEnabled = true;
        _whatWilHappen_->Text = "You are not yet registered.";
    }
    else {
        _usernameTextBoxEdition_->IsEnabled = false;
        // _registerOnBlockchainEdition_->IsEnabled = false;
        _whatWilHappen_->Text = "You'll have to send your ringId.";
        _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)
{
    RingD::instance->lookUpName(_usernameTextBox_->Text);

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


Nicolas Jager's avatar
Nicolas Jager committed
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
}



void RingClientUWP::Views::SmartPanel::_deleteAccountEdition__Toggled(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e