Wizard.xaml.cpp 15.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/**************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux                                *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com>              *
* Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com>          *
*                                                                         *
* This program is free software; you can redistribute it and/or modify    *
* it under the terms of the GNU General Public License as published by    *
* the Free Software Foundation; either version 3 of the License, or       *
* (at your option) any later version.                                     *
*                                                                         *
* This program is distributed in the hope that it will be useful,         *
* but WITHOUT ANY WARRANTY; without even the implied warranty of          *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
* GNU General Public License for more details.                            *
*                                                                         *
* You should have received a copy of the GNU General Public License       *
* along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
**************************************************************************/

atraczyk's avatar
atraczyk committed
20
#include "pch.h"
21
#include <direct.h>
atraczyk's avatar
atraczyk committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include "Wizard.xaml.h"
#include "MainPage.xaml.h"

using namespace RingClientUWP::Views;

using namespace Concurrency;
using namespace Platform;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using namespace Windows::Media::Capture;
40 41 42
using namespace Windows::Storage;
using namespace Windows::UI::Xaml::Media::Imaging;
using namespace Windows::UI::Xaml::Shapes;
43 44
using namespace Windows::ApplicationModel::Core;
using namespace Windows::UI::Core;
atraczyk's avatar
atraczyk committed
45 46 47 48

Wizard::Wizard()
{
    InitializeComponent();
Nicolas Jager's avatar
Nicolas Jager committed
49 50
    /* connect to delegates */
    RingD::instance->registeredNameFound += ref new RingClientUWP::RegisteredNameFound(this, &RingClientUWP::Views::Wizard::OnregisteredNameFound);
51
    RingD::instance->registrationStateErrorGeneric += ref new RingClientUWP::RegistrationStateErrorGeneric(this, &RingClientUWP::Views::Wizard::OnregistrationStateErrorGeneric);
Nicolas Jager's avatar
Nicolas Jager committed
52 53 54 55
}

void RingClientUWP::Views::Wizard::OnNavigatedTo(Windows::UI::Xaml::Navigation::NavigationEventArgs ^ e)
{
56 57 58 59 60 61 62 63 64 65 66 67 68 69
    if (!RingD::instance->daemonRunning) {
        RingD::instance->init();
    }

    collapseMenus(nullptr, nullptr);
    _password_->Password = "";
    _passwordCheck_->Password = "";
    _ArchivePassword_->Password = "";
    _PINTextBox_->Text = "";
    _fullnameTextBox_->Text = "";
    _usernameTextBox_->Text = "";
    _RegisterState_->IsOn = true;

    RingD::instance->isInWizard = true;
atraczyk's avatar
atraczyk committed
70 71 72 73 74
}

void
Wizard::_createAccountYes__Click(Object^ sender, RoutedEventArgs^ e)
{
75
    this->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([this] () {
76
        RingD::instance->isInWizard = false;
77
        this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(RingClientUWP::MainPage::typeid));
78
        _password_->Password = usePassword ? _password_->Password : "";
Nicolas Jager's avatar
Nicolas Jager committed
79 80 81 82
        RingD::instance->createRINGAccount(_fullnameTextBox_->Text
                                           , _password_->Password
                                           , true // upnp by default set to true
                                           , (_RegisterState_->IsOn)? _usernameTextBox_->Text : "");
atraczyk's avatar
atraczyk committed
83 84 85 86 87 88
    }));
}

void
Wizard::_showCreateAccountMenuBtn__Click(Object^ sender, RoutedEventArgs^ e)
{
89 90 91
    _addStack_->Visibility = VIS::Collapsed;
    _createStack_->Margin = Windows::UI::Xaml::Thickness(0.0, 0.0, 0.0, 50.0);

atraczyk's avatar
atraczyk committed
92 93 94 95 96 97 98 99 100 101 102 103
    _accountAddMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _showAddAccountMenuTitle_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _showAddAccountMenuBtn_->Visibility = Windows::UI::Xaml::Visibility::Visible;

    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _showCreateAccountMenuTitle_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _showCreateAccountMenuBtn_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
}

void
Wizard::_showAddAccountMenuBtn__Click(Object^ sender, RoutedEventArgs^ e)
{
104 105 106
    _createStack_->Visibility = VIS::Collapsed;
    _addStack_->Margin = Windows::UI::Xaml::Thickness(0.0, 0.0, 0.0, 50.0);

atraczyk's avatar
atraczyk committed
107 108 109 110 111 112 113
    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _showCreateAccountMenuTitle_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _showCreateAccountMenuBtn_->Visibility = Windows::UI::Xaml::Visibility::Visible;

    _accountAddMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _showAddAccountMenuTitle_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    _showAddAccountMenuBtn_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
114 115 116 117 118
}

void
Wizard::_avatarWebcamCaptureBtn__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
119 120 121 122 123
    create_task(Configuration::getProfileImageAsync()).then([&](task<BitmapImage^> image){
        try {
            if (auto bitmapImage = image.get()) {
                auto brush = ref new ImageBrush();
                auto circle = ref new Ellipse();
124 125
                circle->Height = 80;
                circle->Width = 80;
126 127 128
                brush->ImageSource = bitmapImage;
                circle->Fill = brush;
                _avatarWebcamCaptureBtn_->Content = circle;
atraczyk's avatar
atraczyk committed
129
            }
130 131 132
        }
        catch (Platform::Exception^ e) {
            EXC_(e);
133 134
        }
    });
135 136 137 138
}

void RingClientUWP::Views::Wizard::_addAccountYes__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
139 140 141 142 143
    if (_PINTextBox_->Text->IsEmpty() || _ArchivePassword_->Password->IsEmpty()) {
        _addAccountYes_->IsEnabled = false;
        return;
    }

144 145 146 147
    RingD::instance->_startingStatus = StartingStatus::REGISTERING_THIS_DEVICE;

    this->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([this]() {
        this->Frame->Navigate(Windows::UI::Xaml::Interop::TypeName(RingClientUWP::MainPage::typeid));
148 149 150
        RingD::instance->registerThisDevice(_PINTextBox_->Text, _ArchivePassword_->Password);
        _ArchivePassword_->Password = "";
        _PINTextBox_->Text = "";
151 152
    }));
}
153

Nicolas Jager's avatar
Nicolas Jager committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
void RingClientUWP::Views::Wizard::_password__PasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    isPasswordValid = (_password_->Password->Length() > 0)
                      ? true : false;

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

    isPasswordsMatching = (_password_->Password
                           == _passwordCheck_->Password
                           && _password_->Password->Length() > 0)
                          ? true : false;
173

Nicolas Jager's avatar
Nicolas Jager committed
174 175 176 177 178 179 180 181 182 183 184 185
    if (isPasswordsMatching) {
        _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;
    }

    checkState();
}

186 187
void
Wizard::_passwordCheck__PasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
188
{
Nicolas Jager's avatar
Nicolas Jager committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    isPasswordsMatching = (_password_->Password
                           == _passwordCheck_->Password
                           && _password_->Password->Length() > 0)
                          ? true : false;

    if (isPasswordsMatching) {
        _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;
    }

    checkState();
204
}
Nicolas Jager's avatar
Nicolas Jager committed
205 206 207

void RingClientUWP::Views::Wizard::checkState()
{
208 209 210 211 212 213 214
    if (!_password_)
        return;

    auto passwordValidation = ((isPasswordsMatching && isPasswordValid) || !usePassword);

    if ((isPublic && passwordValidation && isUsernameValid && isFullNameValid)
            || (!isPublic && passwordValidation && isFullNameValid))
Nicolas Jager's avatar
Nicolas Jager committed
215 216 217 218 219 220 221 222 223 224 225 226 227
        _createAccountYes_->IsEnabled = true;
    else
        _createAccountYes_->IsEnabled = false;
}

void RingClientUWP::Views::Wizard::_usernameTextBox__KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
    auto alias = dynamic_cast<TextBox^>(sender)->Text;

    if (alias->IsEmpty()) {
        _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    } else {
228 229
        auto accountId = ViewModel::AccountListItemsViewModel::instance->getSelectedAccountId();
        RingD::instance->lookUpName(Utils::toString(accountId), alias);
Nicolas Jager's avatar
Nicolas Jager committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
        _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }
}

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

    if (_usernameTextBox_ == nullptr) // avoid trouble when InitializeComponent is called for Wizard.
        return;

    isPublic = toggleSwitch->IsOn;

    if (isPublic) {
        _usernameTextBox_->IsEnabled = true;
246 247
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappen_->Text = loader->GetString("_whatWillHappenEditionUid_.Text");
Nicolas Jager's avatar
Nicolas Jager committed
248 249
    } else {
        _usernameTextBox_->IsEnabled = false;
250 251
        auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
        _whatWillHappen_->Text = loader->GetString("_whatWillHappen_2_");
Nicolas Jager's avatar
Nicolas Jager committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    }

    checkState();
}

void RingClientUWP::Views::Wizard::_fullnameTextBox__KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
    isFullNameValid = !_fullnameTextBox_->Text->IsEmpty();

    if (isFullNameValid) {
        _fullnameValid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        _fullnameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    } else {
        _fullnameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _fullnameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
    }

    checkState();
}

void RingClientUWP::Views::Wizard::collapseMenus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
274 275 276 277 278 279
    _createStack_->Visibility = VIS::Visible;
    _addStack_->Visibility = VIS::Visible;

    _createStack_->Margin = Windows::UI::Xaml::Thickness(0.0, 0.0, 0.0, 0.0);
    _addStack_->Margin = Windows::UI::Xaml::Thickness(0.0, 36.0, 0.0, 50.0);

Nicolas Jager's avatar
Nicolas Jager committed
280 281 282 283 284 285 286 287 288
    _accountAddMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _showAddAccountMenuTitle_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _showAddAccountMenuBtn_->Visibility = Windows::UI::Xaml::Visibility::Visible;

    _accountCreationMenuGrid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _showCreateAccountMenuTitle_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    _showCreateAccountMenuBtn_->Visibility = Windows::UI::Xaml::Visibility::Visible;
}

289
void RingClientUWP::Views::Wizard::OnregisteredNameFound(LookupStatus status,  const std::string& accountId, const std::string& address, const std::string& name)
Nicolas Jager's avatar
Nicolas Jager committed
290 291 292 293 294
{
    switch (status)
    {
    case LookupStatus::SUCCESS:
    case LookupStatus::INVALID_NAME:
295
    case LookupStatus::ERRORR:
Nicolas Jager's avatar
Nicolas Jager committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309
        _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        isUsernameValid = false;
        break;
    case LookupStatus::NOT_FOUND:
        _usernameValid_->Visibility = Windows::UI::Xaml::Visibility::Visible;
        _usernameInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        isUsernameValid = true;
        break;
    }

    checkState();
}

310 311
void RingClientUWP::Views::Wizard::OnregistrationStateErrorGeneric(const std::string &accountId)
{
312 313
    auto loader = ref new Windows::ApplicationModel::Resources::ResourceLoader();
    _response_->Text = loader->GetString("_accountsCredentialsExpired_");
314
    _addAccountYes_->IsEnabled = false;
315 316 317 318 319 320
}

void RingClientUWP::Views::Wizard::_PINTextBox__GotFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    _response_->Text = "";
}
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

void RingClientUWP::Views::Wizard::_ArchivePassword__KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
    if (_PINTextBox_->Text->IsEmpty() || _ArchivePassword_->Password->IsEmpty())
        _addAccountYes_->IsEnabled = false;
    else
        _addAccountYes_->IsEnabled = true;
}

void RingClientUWP::Views::Wizard::_PINTextBox__KeyUp(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
    if (_PINTextBox_->Text->IsEmpty() || _ArchivePassword_->Password->IsEmpty())
        _addAccountYes_->IsEnabled = false;
    else
        _addAccountYes_->IsEnabled = true;
}
337 338 339 340 341 342

void
Wizard::_createAccountNo__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{

}
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

void
Wizard::_usePasswordState__Toggled(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
    if (!_password_)
        return;

    usePassword = _usePasswordState_->IsOn;

    if (!usePassword) {
        _password_->IsEnabled = false;
        _passwordCheck_->IsEnabled = false;
        _passwordValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _passwordInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _passwordCheckValid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
        _passwordCheckInvalid_->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
    }
    else {
        _password_->IsEnabled = true;
        _passwordCheck_->IsEnabled = true;
        _password__PasswordChanged(nullptr, nullptr);
        _passwordCheck__PasswordChanged(nullptr, nullptr);
    }

    checkState();
}