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