Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 Savoir-faire Linux Inc. |
| 3 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #import "ConnectToAccManagerVC.h" |
| 21 | #import "utils.h" |
| 22 | |
| 23 | //LRC |
| 24 | #import <api/lrc.h> |
| 25 | #import <api/newaccountmodel.h> |
| 26 | |
| 27 | @interface ConnectToAccManagerVC () |
| 28 | |
| 29 | @end |
| 30 | |
| 31 | @implementation ConnectToAccManagerVC { |
| 32 | __unsafe_unretained IBOutlet NSView* initialContainer; |
| 33 | __unsafe_unretained IBOutlet NSView* loadingContainer; |
| 34 | __unsafe_unretained IBOutlet NSProgressIndicator* progressBar; |
| 35 | __unsafe_unretained IBOutlet NSView* errorContainer; |
| 36 | |
| 37 | __unsafe_unretained IBOutlet NSTextField* userNameField; |
| 38 | __unsafe_unretained IBOutlet NSTextField* accountManagerField; |
| 39 | __unsafe_unretained IBOutlet NSSecureTextField* passwordTextField; |
| 40 | } |
| 41 | |
| 42 | QMetaObject::Connection accountCreatedSuccess; |
| 43 | QMetaObject::Connection accountNotCreated; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 44 | QString accointId; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 45 | |
| 46 | @synthesize accountModel; |
| 47 | |
| 48 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel { |
| 49 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 50 | { |
| 51 | self.accountModel = accountModel; |
| 52 | } |
| 53 | return self; |
| 54 | } |
| 55 | |
| 56 | - (void)show |
| 57 | { |
| 58 | self.username = userNameField.stringValue = @""; |
| 59 | self.password = passwordTextField.stringValue = @""; |
| 60 | self.accountManager = accountManagerField.stringValue = @""; |
| 61 | [self.delegate showView:initialContainer]; |
| 62 | } |
| 63 | |
| 64 | - (void)showError |
| 65 | { |
| 66 | [self.delegate showView:errorContainer]; |
| 67 | } |
| 68 | - (void)showLoading |
| 69 | { |
| 70 | [progressBar startAnimation:nil]; |
| 71 | [self.delegate showView:loadingContainer]; |
| 72 | } |
| 73 | |
| 74 | - (void)viewDidLoad { |
| 75 | [super viewDidLoad]; |
| 76 | [self.view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
| 77 | } |
| 78 | |
| 79 | - (IBAction)dismissViewWithError:(id)sender |
| 80 | { |
kkostiuk | 67669d4 | 2021-09-30 09:43:54 -0400 | [diff] [blame^] | 81 | [self.delegate didSignInSuccess:NO accountId:""]; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | - (IBAction)startAgain:(id)sender |
| 85 | { |
| 86 | [self show]; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | - (IBAction)signIn:(id)sender |
| 91 | { |
| 92 | QObject::disconnect(accountCreatedSuccess); |
| 93 | QObject::disconnect(accountNotCreated); |
| 94 | accountCreatedSuccess = QObject::connect(self.accountModel, |
| 95 | &lrc::api::NewAccountModel::accountAdded, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 96 | [self] (const QString& accountID) { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 97 | if(accountID.compare(accointId) != 0) { |
| 98 | return; |
| 99 | } |
kkostiuk | 67669d4 | 2021-09-30 09:43:54 -0400 | [diff] [blame^] | 100 | [self.delegate didSignInSuccess:YES accountId: accointId]; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 101 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 102 | accountProperties.Ringtone.ringtonePath = QString::fromNSString(defaultRingtonePath()); |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 103 | self.accountModel->setAccountConfig(accountID, accountProperties); |
| 104 | QObject::disconnect(accountCreatedSuccess); |
| 105 | QObject::disconnect(accountNotCreated); |
| 106 | }); |
| 107 | accountNotCreated = QObject::connect(self.accountModel, |
| 108 | &lrc::api::NewAccountModel::accountRemoved, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 109 | [self] (const QString& accountID) { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 110 | if(accountID.compare(accointId) == 0) { |
| 111 | [self showError]; |
| 112 | } |
| 113 | }); |
| 114 | accountNotCreated = QObject::connect(self.accountModel, |
| 115 | &lrc::api::NewAccountModel::invalidAccountDetected, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 116 | [self] (const QString& accountID) { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 117 | if(accountID.compare(accointId) == 0) { |
| 118 | [self showError]; |
| 119 | } |
| 120 | }); |
| 121 | |
| 122 | [self showLoading]; |
| 123 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 124 | accointId = self.accountModel->connectToAccountManager(QString::fromNSString(userNameField.stringValue), QString::fromNSString(passwordTextField.stringValue), QString::fromNSString(accountManagerField.stringValue)); |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @end |