Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
| 3 | * Author: Loïc Siret <loic.siret@savoirfairelinux.com> |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 4 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 5 | * |
| 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 Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 23 | #import <Quartz/Quartz.h> |
| 24 | |
| 25 | //Qt |
| 26 | #import <QUrl> |
| 27 | #import <QPixmap> |
| 28 | |
| 29 | //LRC |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 30 | #import <api/lrc.h> |
| 31 | #import <api/newaccountmodel.h> |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 32 | |
| 33 | #import "Constants.h" |
Alexandre Lision | 849514f | 2016-10-25 14:07:51 -0400 | [diff] [blame] | 34 | #import "views/NSImage+Extensions.h" |
Andreas Traczyk | 92c101e | 2018-07-03 14:43:53 -0400 | [diff] [blame] | 35 | #import "utils.h" |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 36 | |
| 37 | @interface RingWizardLinkAccountVC () |
| 38 | |
| 39 | @end |
| 40 | |
| 41 | @implementation RingWizardLinkAccountVC { |
| 42 | __unsafe_unretained IBOutlet NSView* initialContainer; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 43 | |
| 44 | __unsafe_unretained IBOutlet NSView* loadingContainer; |
| 45 | __unsafe_unretained IBOutlet NSProgressIndicator* progressBar; |
| 46 | |
| 47 | __unsafe_unretained IBOutlet NSView* errorContainer; |
| 48 | |
Anthony Léonard | 5dfbd6f | 2017-09-25 15:58:00 -0400 | [diff] [blame] | 49 | __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 Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 54 | __unsafe_unretained IBOutlet NSPopover* helpPINContainer; |
| 55 | __unsafe_unretained IBOutlet NSPopover* helpArchiveFileContainer; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 56 | __unsafe_unretained IBOutlet NSStackView* pinContainer; |
| 57 | __unsafe_unretained IBOutlet NSStackView* filePathContainer; |
Anthony Léonard | 5dfbd6f | 2017-09-25 15:58:00 -0400 | [diff] [blame] | 58 | NSString *fileButtonTitleBackup; |
| 59 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 60 | QMetaObject::Connection accountCreated; |
| 61 | QMetaObject::Connection accountRemoved; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 62 | QString accountToCreate; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 65 | @synthesize accountModel, backupFile; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 66 | |
| 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 Lision | 9c1ec62 | 2016-11-14 21:40:59 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 75 | - (void)viewDidLoad { |
| 76 | [super viewDidLoad]; |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 77 | [self.view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
| 78 | [initialContainer setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
| 79 | [loadingContainer setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
| 80 | [errorContainer setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]; |
Kateryna Kostiuk | bdaa274 | 2018-09-04 15:49:30 -0400 | [diff] [blame] | 81 | fileButtonTitleBackup = NSLocalizedString(@"Select archive", |
| 82 | @"export account button title"); |
Alexandre Lision | 9c1ec62 | 2016-11-14 21:40:59 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 85 | - (void)showImportViewOfType:(IMPORT_TYPE)type |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 86 | { |
Kateryna Kostiuk | c7e68f3 | 2019-10-09 16:15:45 -0400 | [diff] [blame] | 87 | [pinContainer setHidden: type == IMPORT_FROM_BACKUP]; |
| 88 | [filePathContainer setHidden: type == IMPORT_FROM_DEVICE]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 89 | [self.delegate showView:initialContainer]; |
| 90 | [fileButton setTitle:fileButtonTitleBackup]; |
| 91 | backupFile = nil; |
| 92 | [pinTextField setStringValue:@""]; |
| 93 | [pinTextField setEnabled:YES]; |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 94 | [linkButton setEnabled:NO]; |
Kateryna Kostiuk | d69ab59 | 2019-01-15 13:34:13 -0500 | [diff] [blame] | 95 | self.passwordValue = @""; |
| 96 | self.pinValue = @""; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 97 | [passwordTextField setStringValue:@""]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | - (void)showError |
| 101 | { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 102 | [self.delegate showView:errorContainer]; |
| 103 | QObject::disconnect(accountCreated); |
| 104 | QObject::disconnect(accountRemoved); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 105 | } |
| 106 | - (void)showLoading |
| 107 | { |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 108 | [progressBar startAnimation:nil]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 109 | [self.delegate showView:loadingContainer]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | - (IBAction)importRingAccount:(id)sender |
| 113 | { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 114 | QObject::disconnect(accountCreated); |
| 115 | QObject::disconnect(accountRemoved); |
| 116 | accountCreated = QObject::connect(self.accountModel, |
| 117 | &lrc::api::NewAccountModel::accountAdded, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 118 | [self] (const QString& accountID) { |
Kateryna Kostiuk | d69ab59 | 2019-01-15 13:34:13 -0500 | [diff] [blame] | 119 | if(accountID.compare(accountToCreate) != 0) { |
| 120 | return; |
| 121 | } |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 122 | [self.delegate didLinkAccountWithSuccess:YES]; |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 123 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(accountID); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 124 | accountProperties.Ringtone.ringtonePath = QString::fromNSString(defaultRingtonePath()); |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 125 | self.accountModel->setAccountConfig(accountID, accountProperties); |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 126 | [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 Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 133 | [self] (const QString& accountID) { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 134 | if(accountID.compare(accountToCreate) == 0) { |
| 135 | [self showError]; |
| 136 | } |
| 137 | }); |
Kateryna Kostiuk | d69ab59 | 2019-01-15 13:34:13 -0500 | [diff] [blame] | 138 | accountRemoved = QObject::connect(self.accountModel, |
| 139 | &lrc::api::NewAccountModel::invalidAccountDetected, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 140 | [self] (const QString& accountID) { |
Kateryna Kostiuk | d69ab59 | 2019-01-15 13:34:13 -0500 | [diff] [blame] | 141 | if(accountID.compare(accountToCreate) == 0) { |
| 142 | [self showError]; |
| 143 | } |
| 144 | }); |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 145 | |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 146 | [self showLoading]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 147 | NSString *pin = backupFile ? @"" : (self.pinValue ? self.pinValue : @""); |
| 148 | NSString *archivePath = backupFile ? [backupFile path] : @""; |
| 149 | NSString *pathword = self.passwordValue ? self.passwordValue : @""; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 150 | accountToCreate = self.accountModel->createNewAccount(lrc::api::profile::Type::RING, |
| 151 | "", |
| 152 | QString::fromNSString(archivePath), |
| 153 | QString::fromNSString(pathword), |
| 154 | QString::fromNSString(pin)); |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | - (IBAction)dismissViewWithError:(id)sender |
| 158 | { |
| 159 | [self.delegate didLinkAccountWithSuccess:NO]; |
| 160 | } |
| 161 | |
Anthony Léonard | 5dfbd6f | 2017-09-25 15:58:00 -0400 | [diff] [blame] | 162 | - (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 Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 180 | - (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 Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 190 | /** |
| 191 | * Set default values for preferences |
| 192 | */ |
| 193 | - (void)registerDefaultPreferences |
| 194 | { |
Andreas Traczyk | 92c101e | 2018-07-03 14:43:53 -0400 | [diff] [blame] | 195 | 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 Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 203 | |
| 204 | // enable Notifications |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 205 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::CallNotifications]; |
| 206 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Preferences::MessagesNotifications]; |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 207 | } |
Loïc Siret | fcb4ca6 | 2016-09-21 17:12:09 -0400 | [diff] [blame] | 208 | @end |