blob: db4ad55460b0b6dfe786fc5175974a2dd317c19e [file] [log] [blame]
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -04001/*
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
42QMetaObject::Connection accountCreatedSuccess;
43QMetaObject::Connection accountNotCreated;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040044QString accointId;
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040045
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{
kkostiuk67669d42021-09-30 09:43:54 -040081 [self.delegate didSignInSuccess:NO accountId:""];
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040082}
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 Kostiukc867eb92020-03-08 13:15:17 -040096 [self] (const QString& accountID) {
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040097 if(accountID.compare(accointId) != 0) {
98 return;
99 }
kkostiuk67669d42021-09-30 09:43:54 -0400100 [self.delegate didSignInSuccess:YES accountId: accointId];
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -0400101 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID);
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400102 accountProperties.Ringtone.ringtonePath = QString::fromNSString(defaultRingtonePath());
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -0400103 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 Kostiukc867eb92020-03-08 13:15:17 -0400109 [self] (const QString& accountID) {
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -0400110 if(accountID.compare(accointId) == 0) {
111 [self showError];
112 }
113 });
114 accountNotCreated = QObject::connect(self.accountModel,
115 &lrc::api::NewAccountModel::invalidAccountDetected,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400116 [self] (const QString& accountID) {
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -0400117 if(accountID.compare(accointId) == 0) {
118 [self showError];
119 }
120 });
121
122 [self showLoading];
123
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400124 accointId = self.accountModel->connectToAccountManager(QString::fromNSString(userNameField.stringValue), QString::fromNSString(passwordTextField.stringValue), QString::fromNSString(accountManagerField.stringValue));
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -0400125}
126
127@end