Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@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. |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 18 | */ |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 19 | #import "AccRingVC.h" |
| 20 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 21 | #import <accountmodel.h> |
| 22 | #import <qitemselectionmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 23 | |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 24 | #import "RegisterNameWC.h" |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 25 | #import "PasswordChangeWC.h" |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 26 | |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 27 | @interface AccRingVC () <RegisterNameDelegate> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 28 | |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 29 | @property (unsafe_unretained) IBOutlet NSTextField *aliasTextField; |
| 30 | @property (unsafe_unretained) IBOutlet NSTextField *bootstrapField; |
| 31 | @property (unsafe_unretained) IBOutlet NSTextField *blockchainField; |
| 32 | @property (unsafe_unretained) IBOutlet NSTextField *ringIDField; |
| 33 | @property (unsafe_unretained) IBOutlet NSButton *registerBlockchainNameButton; |
| 34 | @property (unsafe_unretained) IBOutlet NSTextField *registeredNameField; |
| 35 | |
| 36 | @property (unsafe_unretained) IBOutlet NSButton *upnpButton; |
| 37 | @property (unsafe_unretained) IBOutlet NSButton *autoAnswerButton; |
| 38 | @property (unsafe_unretained) IBOutlet NSButton *userAgentButton; |
| 39 | @property (unsafe_unretained) IBOutlet NSTextField *userAgentTextField; |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 40 | @property (unsafe_unretained) IBOutlet NSButton *allowUnknown; |
| 41 | @property (unsafe_unretained) IBOutlet NSButton *allowHistory; |
| 42 | @property (unsafe_unretained) IBOutlet NSButton *allowContacts; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 43 | |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 44 | @property AbstractLoadingWC* accountModal; |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 45 | @property PasswordChangeWC* passwordModal; |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 46 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 47 | @end |
| 48 | |
| 49 | @implementation AccRingVC |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 50 | @synthesize bootstrapField; |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 51 | @synthesize ringIDField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 52 | @synthesize aliasTextField; |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 53 | @synthesize blockchainField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 54 | @synthesize upnpButton; |
| 55 | @synthesize autoAnswerButton; |
| 56 | @synthesize userAgentButton; |
| 57 | @synthesize userAgentTextField; |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 58 | @synthesize allowContacts, allowHistory, allowUnknown; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 59 | |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 60 | typedef NS_ENUM(NSInteger, TagViews) { |
| 61 | ALIAS = 0, |
| 62 | HOSTNAME, |
| 63 | USERAGENT, |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 64 | BLOCKCHAIN, |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 65 | }; |
| 66 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 67 | - (void)awakeFromNib |
| 68 | { |
| 69 | NSLog(@"INIT Ring VC"); |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 70 | [aliasTextField setTag:TagViews::ALIAS]; |
| 71 | [userAgentTextField setTag:TagViews::USERAGENT]; |
| 72 | [bootstrapField setTag:TagViews::HOSTNAME]; |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 73 | [blockchainField setTag:TagViews::BLOCKCHAIN]; |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 74 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 75 | QObject::connect(AccountModel::instance().selectionModel(), |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 76 | &QItemSelectionModel::currentChanged, |
| 77 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 78 | if(!current.isValid()) |
| 79 | return; |
| 80 | [self loadAccount]; |
| 81 | }); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 84 | - (void)loadAccount |
| 85 | { |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 86 | auto account = AccountModel::instance().selectedAccount(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 87 | |
| 88 | [self.aliasTextField setStringValue:account->alias().toNSString()]; |
| 89 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 90 | [allowUnknown setState:account->allowIncomingFromUnknown()]; |
| 91 | [allowHistory setState:account->allowIncomingFromHistory()]; |
| 92 | [allowContacts setState:account->allowIncomingFromContact()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 93 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 94 | [allowHistory setEnabled:!account->allowIncomingFromUnknown()]; |
| 95 | [allowContacts setEnabled:!account->allowIncomingFromUnknown()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 96 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 97 | [upnpButton setState:account->isUpnpEnabled()]; |
| 98 | [userAgentButton setState:account->hasCustomUserAgent()]; |
| 99 | [userAgentTextField setEnabled:account->hasCustomUserAgent()]; |
| 100 | |
| 101 | [autoAnswerButton setState:account->isAutoAnswer()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 102 | [userAgentTextField setStringValue:account->userAgent().toNSString()]; |
| 103 | |
| 104 | [bootstrapField setStringValue:account->hostname().toNSString()]; |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 105 | [blockchainField setStringValue:account->nameServiceURL().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 106 | |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 107 | if([account->username().toNSString() isEqualToString:@""]) { |
| 108 | [ringIDField setStringValue:NSLocalizedString(@"Reopen account to see your hash", |
Alexandre Lision | 922380d | 2015-09-15 10:25:17 -0400 | [diff] [blame] | 109 | @"Show advice to user")]; |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 110 | } else { |
| 111 | [ringIDField setStringValue:account->username().toNSString()]; |
| 112 | } |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 113 | |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 114 | [self refreshRegisteredName:account]; |
| 115 | } |
| 116 | |
| 117 | - (void) refreshRegisteredName:(Account*) account |
| 118 | { |
| 119 | [self.registerBlockchainNameButton setHidden:!account->registeredName().isEmpty()]; |
| 120 | [self.registeredNameField setStringValue:account->registeredName().toNSString()]; |
| 121 | } |
| 122 | |
| 123 | - (IBAction)startNameRegistration:(id)sender |
| 124 | { |
| 125 | auto registerWC = [[RegisterNameWC alloc] initWithDelegate:self]; |
| 126 | #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9 |
| 127 | [self.view.window beginSheet:registerWC.window completionHandler:nil]; |
| 128 | #else |
| 129 | [NSApp beginSheet: registerWC.window |
| 130 | modalForWindow: self.view.window |
| 131 | modalDelegate: self |
| 132 | didEndSelector: nil |
| 133 | contextInfo: nil]; |
| 134 | #endif |
| 135 | self.accountModal = registerWC; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Anthony Léonard | 1f70f72 | 2017-10-02 10:53:32 -0400 | [diff] [blame] | 138 | - (IBAction)changePassword:(id)sender |
| 139 | { |
| 140 | auto passwordWC = [[PasswordChangeWC alloc] initWithAccount:AccountModel::instance().selectedAccount()]; |
| 141 | #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9 |
| 142 | [self.view.window beginSheet:passwordWC.window completionHandler:nil]; |
| 143 | #else |
| 144 | [NSApp beginSheet: passwordWC.window |
| 145 | modalForWindow: self.view.window |
| 146 | modalDelegate: self |
| 147 | didEndSelector: nil |
| 148 | contextInfo: nil]; |
| 149 | #endif |
| 150 | self.passwordModal = passwordWC; |
| 151 | } |
| 152 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 153 | - (IBAction)toggleUpnp:(NSButton *)sender { |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 154 | AccountModel::instance().selectedAccount()->setUpnpEnabled([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | - (IBAction)toggleAutoAnswer:(NSButton *)sender { |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 158 | AccountModel::instance().selectedAccount()->setAutoAnswer([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | - (IBAction)toggleCustomAgent:(NSButton *)sender { |
| 162 | [self.userAgentTextField setEnabled:[sender state] == NSOnState]; |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 163 | AccountModel::instance().selectedAccount()->setHasCustomUserAgent([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 166 | - (IBAction)toggleAllowFromUnknown:(id)sender { |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 167 | AccountModel::instance().selectedAccount()->setAllowIncomingFromUnknown([sender state] == NSOnState); |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 168 | [allowHistory setEnabled:![sender state] == NSOnState]; |
| 169 | [allowContacts setEnabled:![sender state] == NSOnState]; |
| 170 | } |
| 171 | - (IBAction)toggleAllowFromHistory:(id)sender { |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 172 | AccountModel::instance().selectedAccount()->setAllowIncomingFromHistory([sender state] == NSOnState); |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 173 | } |
| 174 | - (IBAction)toggleAllowFromContacts:(id)sender { |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 175 | AccountModel::instance().selectedAccount()->setAllowIncomingFromContact([sender state] == NSOnState); |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 178 | #pragma mark - NSTextFieldDelegate methods |
| 179 | |
| 180 | - (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor |
| 181 | { |
| 182 | return YES; |
| 183 | } |
| 184 | |
| 185 | -(void)controlTextDidChange:(NSNotification *)notif |
| 186 | { |
| 187 | NSTextField *textField = [notif object]; |
| 188 | |
| 189 | switch ([textField tag]) { |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 190 | case TagViews::ALIAS: |
| 191 | AccountModel::instance().selectedAccount()->setAlias([[textField stringValue] UTF8String]); |
| 192 | AccountModel::instance().selectedAccount()->setDisplayName([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 193 | break; |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 194 | case TagViews::HOSTNAME: |
| 195 | AccountModel::instance().selectedAccount()->setHostname([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 196 | break; |
Alexandre Lision | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 197 | case TagViews::USERAGENT: |
| 198 | AccountModel::instance().selectedAccount()->setUserAgent([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 199 | break; |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 200 | case TagViews::BLOCKCHAIN: |
| 201 | AccountModel::instance().selectedAccount()->setNameServiceURL([[textField stringValue] UTF8String]); |
| 202 | break; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 203 | default: |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
Alexandre Lision | 34079c2 | 2016-10-31 16:14:02 -0400 | [diff] [blame] | 208 | - (void) didRegisterNameWithSuccess |
| 209 | { |
| 210 | [self.accountModal close]; |
| 211 | [self refreshRegisteredName:AccountModel::instance().selectedAccount()]; |
| 212 | } |
| 213 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 214 | @end |