blob: da9dd315140350c78a2b3eed3df0322da1d260ef [file] [log] [blame]
Loïc Siretfcb4ca62016-09-21 17:12:09 -04001/*
2 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
3 * Author: Loïc Siret <loic.siret@savoirfairelinux.com>
Kateryna Kostiukecaa3952018-07-13 16:00:34 -04004 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
Loïc Siretfcb4ca62016-09-21 17:12:09 -04005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21#import "RingWizardLinkAccountVC.h"
22//Cocoa
Loïc Siretfcb4ca62016-09-21 17:12:09 -040023#import <Quartz/Quartz.h>
24
25//Qt
26#import <QUrl>
27#import <QPixmap>
28
29//LRC
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040030#import <api/lrc.h>
31#import <api/newaccountmodel.h>
Loïc Siretfcb4ca62016-09-21 17:12:09 -040032
33#import "Constants.h"
Alexandre Lision849514f2016-10-25 14:07:51 -040034#import "views/NSImage+Extensions.h"
Andreas Traczyk92c101e2018-07-03 14:43:53 -040035#import "utils.h"
Loïc Siretfcb4ca62016-09-21 17:12:09 -040036
37@interface RingWizardLinkAccountVC ()
38
39@end
40
41@implementation RingWizardLinkAccountVC {
42 __unsafe_unretained IBOutlet NSView* initialContainer;
Loïc Siretfcb4ca62016-09-21 17:12:09 -040043
44 __unsafe_unretained IBOutlet NSView* loadingContainer;
45 __unsafe_unretained IBOutlet NSProgressIndicator* progressBar;
46
47 __unsafe_unretained IBOutlet NSView* errorContainer;
48
Anthony Léonard5dfbd6f2017-09-25 15:58:00 -040049 __unsafe_unretained IBOutlet NSTextField* pinTextField;
50 __unsafe_unretained IBOutlet NSButton* fileButton;
51 __unsafe_unretained IBOutlet NSSecureTextField* passwordTextField;
52
53 __unsafe_unretained IBOutlet NSButton* linkButton;
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -050054 __unsafe_unretained IBOutlet NSPopover* helpPINContainer;
55 __unsafe_unretained IBOutlet NSPopover* helpArchiveFileContainer;
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040056 __unsafe_unretained IBOutlet NSStackView* pinContainer;
57 __unsafe_unretained IBOutlet NSStackView* filePathContainer;
Anthony Léonard5dfbd6f2017-09-25 15:58:00 -040058 NSString *fileButtonTitleBackup;
59
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040060 QMetaObject::Connection accountCreated;
61 QMetaObject::Connection accountRemoved;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040062 QString accountToCreate;
Loïc Siretfcb4ca62016-09-21 17:12:09 -040063}
64
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -050065@synthesize accountModel, backupFile;
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040066
67-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel {
68 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
69 {
70 self.accountModel = accountModel;
71 }
72 return self;
Alexandre Lision9c1ec622016-11-14 21:40:59 -050073}
74
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040075- (void)viewDidLoad {
76 [super viewDidLoad];
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040077 [self.view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
78 [initialContainer setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
79 [loadingContainer setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
80 [errorContainer setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
Kateryna Kostiukbdaa2742018-09-04 15:49:30 -040081 fileButtonTitleBackup = NSLocalizedString(@"Select archive",
82 @"export account button title");
Alexandre Lision9c1ec622016-11-14 21:40:59 -050083}
84
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040085- (void)showImportViewOfType:(IMPORT_TYPE)type
Loïc Siretfcb4ca62016-09-21 17:12:09 -040086{
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040087 [pinContainer setHidden: type == IMPORT_FROM_BACKUP];
88 [filePathContainer setHidden: type == IMPORT_FROM_DEVICE];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040089 [self.delegate showView:initialContainer];
90 [fileButton setTitle:fileButtonTitleBackup];
91 backupFile = nil;
92 [pinTextField setStringValue:@""];
93 [pinTextField setEnabled:YES];
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -050094 [linkButton setEnabled:NO];
Kateryna Kostiukd69ab592019-01-15 13:34:13 -050095 self.passwordValue = @"";
96 self.pinValue = @"";
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040097 [passwordTextField setStringValue:@""];
Loïc Siretfcb4ca62016-09-21 17:12:09 -040098}
99
100- (void)showError
101{
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400102 [self.delegate showView:errorContainer];
103 QObject::disconnect(accountCreated);
104 QObject::disconnect(accountRemoved);
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400105}
106- (void)showLoading
107{
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400108 [progressBar startAnimation:nil];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400109 [self.delegate showView:loadingContainer];
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400110}
111
112- (IBAction)importRingAccount:(id)sender
113{
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400114 QObject::disconnect(accountCreated);
115 QObject::disconnect(accountRemoved);
116 accountCreated = QObject::connect(self.accountModel,
117 &lrc::api::NewAccountModel::accountAdded,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400118 [self] (const QString& accountID) {
Kateryna Kostiukd69ab592019-01-15 13:34:13 -0500119 if(accountID.compare(accountToCreate) != 0) {
120 return;
121 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400122 [self.delegate didLinkAccountWithSuccess:YES];
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400123 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID);
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400124 accountProperties.Ringtone.ringtonePath = QString::fromNSString(defaultRingtonePath());
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400125 self.accountModel->setAccountConfig(accountID, accountProperties);
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400126 [self registerDefaultPreferences];
127 QObject::disconnect(accountCreated);
128 QObject::disconnect(accountRemoved);
129 });
130 // account that is invalid will be removed, connect the signal to show error message
131 accountRemoved = QObject::connect(self.accountModel,
132 &lrc::api::NewAccountModel::accountRemoved,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400133 [self] (const QString& accountID) {
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400134 if(accountID.compare(accountToCreate) == 0) {
135 [self showError];
136 }
137 });
Kateryna Kostiukd69ab592019-01-15 13:34:13 -0500138 accountRemoved = QObject::connect(self.accountModel,
139 &lrc::api::NewAccountModel::invalidAccountDetected,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400140 [self] (const QString& accountID) {
Kateryna Kostiukd69ab592019-01-15 13:34:13 -0500141 if(accountID.compare(accountToCreate) == 0) {
142 [self showError];
143 }
144 });
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400145
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400146 [self showLoading];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400147 NSString *pin = backupFile ? @"" : (self.pinValue ? self.pinValue : @"");
148 NSString *archivePath = backupFile ? [backupFile path] : @"";
149 NSString *pathword = self.passwordValue ? self.passwordValue : @"";
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400150 accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::RING,
151 "",
152 QString::fromNSString(archivePath),
153 QString::fromNSString(pathword),
154 QString::fromNSString(pin));
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400155}
156
157- (IBAction)dismissViewWithError:(id)sender
158{
159 [self.delegate didLinkAccountWithSuccess:NO];
160}
161
Anthony Léonard5dfbd6f2017-09-25 15:58:00 -0400162- (IBAction)pickBackupFile:(id)sender
163{
164 NSOpenPanel* filePicker = [NSOpenPanel openPanel];
165 [filePicker setCanChooseFiles:YES];
166 [filePicker setCanChooseDirectories:NO];
167 [filePicker setAllowsMultipleSelection:NO];
168
169 if ([filePicker runModal] == NSFileHandlingPanelOKButton) {
170 if ([[filePicker URLs] count] == 1) {
171 backupFile = [[filePicker URLs] objectAtIndex:0];
172 [fileButton setTitle:[backupFile lastPathComponent]];
173 [pinTextField setEnabled:NO];
174 [pinTextField setStringValue:@""];
175 [linkButton setEnabled:YES];
176 }
177 }
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400178}
179
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -0500180- (IBAction)showPINHelp:(id)sender
181{
182 [helpPINContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
183}
184
185- (IBAction)showArchiveFileHelp:(id)sender
186{
187 [helpArchiveFileContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
188}
189
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400190/**
191 * Set default values for preferences
192 */
193- (void)registerDefaultPreferences
194{
Andreas Traczyk92c101e2018-07-03 14:43:53 -0400195 if (!appSandboxed()) {
196 // enable AutoStartup
197 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
198 if (loginItemsRef == nil) return;
199 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
200 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
201 if (itemRef) CFRelease(itemRef);
202 }
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400203
204 // enable Notifications
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500205 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::CallNotifications];
206 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::MessagesNotifications];
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400207}
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400208@end