blob: 77f7e876bbf49e1acbca45d7c64b298db3daadf9 [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;
Anthony Léonard5dfbd6f2017-09-25 15:58:00 -040056 NSString *fileButtonTitleBackup;
57
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040058 QMetaObject::Connection accountCreated;
59 QMetaObject::Connection accountRemoved;
60 std::string accountToCreate;
Loïc Siretfcb4ca62016-09-21 17:12:09 -040061}
62
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -050063@synthesize accountModel, backupFile;
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040064
65-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel {
66 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
67 {
68 self.accountModel = accountModel;
69 }
70 return self;
Alexandre Lision9c1ec622016-11-14 21:40:59 -050071}
72
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040073- (void)viewDidLoad {
74 [super viewDidLoad];
Kateryna Kostiukbdaa2742018-09-04 15:49:30 -040075 fileButtonTitleBackup = NSLocalizedString(@"Select archive",
76 @"export account button title");
Alexandre Lision9c1ec622016-11-14 21:40:59 -050077}
78
Loïc Siretfcb4ca62016-09-21 17:12:09 -040079- (void)show
80{
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040081 [self.delegate showView:initialContainer];
82 [fileButton setTitle:fileButtonTitleBackup];
83 backupFile = nil;
84 [pinTextField setStringValue:@""];
85 [pinTextField setEnabled:YES];
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -050086 [linkButton setEnabled:NO];
Kateryna Kostiukd69ab592019-01-15 13:34:13 -050087 self.passwordValue = @"";
88 self.pinValue = @"";
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040089 [passwordTextField setStringValue:@""];
Loïc Siretfcb4ca62016-09-21 17:12:09 -040090}
91
92- (void)showError
93{
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040094 [self.delegate showView:errorContainer];
95 QObject::disconnect(accountCreated);
96 QObject::disconnect(accountRemoved);
Loïc Siretfcb4ca62016-09-21 17:12:09 -040097}
98- (void)showLoading
99{
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400100 [progressBar startAnimation:nil];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400101 [self.delegate showView:loadingContainer];
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400102}
103
104- (IBAction)importRingAccount:(id)sender
105{
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400106 QObject::disconnect(accountCreated);
107 QObject::disconnect(accountRemoved);
108 accountCreated = QObject::connect(self.accountModel,
109 &lrc::api::NewAccountModel::accountAdded,
110 [self] (const std::string& accountID) {
Kateryna Kostiukd69ab592019-01-15 13:34:13 -0500111 if(accountID.compare(accountToCreate) != 0) {
112 return;
113 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400114 [self.delegate didLinkAccountWithSuccess:YES];
115 [self registerDefaultPreferences];
116 QObject::disconnect(accountCreated);
117 QObject::disconnect(accountRemoved);
118 });
119 // account that is invalid will be removed, connect the signal to show error message
120 accountRemoved = QObject::connect(self.accountModel,
121 &lrc::api::NewAccountModel::accountRemoved,
122 [self] (const std::string& accountID) {
123 if(accountID.compare(accountToCreate) == 0) {
124 [self showError];
125 }
126 });
Kateryna Kostiukd69ab592019-01-15 13:34:13 -0500127 accountRemoved = QObject::connect(self.accountModel,
128 &lrc::api::NewAccountModel::invalidAccountDetected,
129 [self] (const std::string& accountID) {
130 if(accountID.compare(accountToCreate) == 0) {
131 [self showError];
132 }
133 });
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400134
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400135 [self showLoading];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400136 NSString *pin = backupFile ? @"" : (self.pinValue ? self.pinValue : @"");
137 NSString *archivePath = backupFile ? [backupFile path] : @"";
138 NSString *pathword = self.passwordValue ? self.passwordValue : @"";
139 accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::RING, "",[archivePath UTF8String], [pathword UTF8String], [pin UTF8String]);
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400140}
141
142- (IBAction)dismissViewWithError:(id)sender
143{
144 [self.delegate didLinkAccountWithSuccess:NO];
145}
146
Anthony Léonard5dfbd6f2017-09-25 15:58:00 -0400147- (IBAction)pickBackupFile:(id)sender
148{
149 NSOpenPanel* filePicker = [NSOpenPanel openPanel];
150 [filePicker setCanChooseFiles:YES];
151 [filePicker setCanChooseDirectories:NO];
152 [filePicker setAllowsMultipleSelection:NO];
153
154 if ([filePicker runModal] == NSFileHandlingPanelOKButton) {
155 if ([[filePicker URLs] count] == 1) {
156 backupFile = [[filePicker URLs] objectAtIndex:0];
157 [fileButton setTitle:[backupFile lastPathComponent]];
158 [pinTextField setEnabled:NO];
159 [pinTextField setStringValue:@""];
160 [linkButton setEnabled:YES];
161 }
162 }
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400163}
164
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -0500165- (IBAction)showPINHelp:(id)sender
166{
167 [helpPINContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
168}
169
170- (IBAction)showArchiveFileHelp:(id)sender
171{
172 [helpArchiveFileContainer showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxYEdge];
173}
174
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400175/**
176 * Set default values for preferences
177 */
178- (void)registerDefaultPreferences
179{
Andreas Traczyk92c101e2018-07-03 14:43:53 -0400180 if (!appSandboxed()) {
181 // enable AutoStartup
182 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
183 if (loginItemsRef == nil) return;
184 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
185 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
186 if (itemRef) CFRelease(itemRef);
187 }
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400188
189 // enable Notifications
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500190 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::CallNotifications];
191 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::MessagesNotifications];
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400192}
Loïc Siretfcb4ca62016-09-21 17:12:09 -0400193@end