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 "AccGeneralVC.h" |
| 20 | |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 21 | #import <accountmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 22 | #import <protocolmodel.h> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 23 | #import <qitemselectionmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 24 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 25 | @interface AccGeneralVC () { |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 26 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 27 | __unsafe_unretained IBOutlet NSTextField *aliasTextField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 28 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 29 | __unsafe_unretained IBOutlet NSTextField *serverHostTextField; |
| 30 | __unsafe_unretained IBOutlet NSTextField *usernameTextField; |
| 31 | __unsafe_unretained IBOutlet NSSecureTextField *passwordTextField; |
| 32 | NSTextField *clearTextField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 33 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 34 | __unsafe_unretained IBOutlet NSButton *upnpButton; |
| 35 | __unsafe_unretained IBOutlet NSButton *autoAnswerButton; |
| 36 | __unsafe_unretained IBOutlet NSButton *userAgentButton; |
| 37 | __unsafe_unretained IBOutlet NSTextField *userAgentTextField; |
| 38 | } |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 39 | @end |
| 40 | |
| 41 | @implementation AccGeneralVC |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 42 | |
| 43 | //Tags for views |
| 44 | typedef NS_ENUM(NSInteger, TagViews) { |
| 45 | ALIAS = 0, |
| 46 | HOSTNAME = 1, |
| 47 | USERNAME = 2, |
| 48 | PASSWORD = 3, |
| 49 | USERAGENT = 4, |
| 50 | }; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 51 | |
| 52 | - (void)awakeFromNib |
| 53 | { |
| 54 | NSLog(@"INIT General VC"); |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 55 | [aliasTextField setTag:TagViews::ALIAS]; |
| 56 | [serverHostTextField setTag:TagViews::HOSTNAME]; |
| 57 | [usernameTextField setTag:TagViews::USERNAME]; |
| 58 | [passwordTextField setTag:TagViews::PASSWORD]; |
| 59 | [userAgentTextField setTag:TagViews::USERAGENT]; |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 60 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 61 | QObject::connect(AccountModel::instance().selectionModel(), |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 62 | &QItemSelectionModel::currentChanged, |
| 63 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 64 | if(!current.isValid()) |
| 65 | return; |
| 66 | [self loadAccount]; |
| 67 | }); |
| 68 | } |
| 69 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 70 | - (IBAction)toggleUpnp:(NSButton *)sender { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 71 | AccountModel::instance().selectedAccount()->setUpnpEnabled([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | - (IBAction)toggleAutoAnswer:(NSButton *)sender { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 75 | AccountModel::instance().selectedAccount()->setAutoAnswer([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | - (IBAction)toggleCustomAgent:(NSButton *)sender { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 79 | [userAgentTextField setEnabled:[sender state] == NSOnState]; |
| 80 | AccountModel::instance().selectedAccount()->setHasCustomUserAgent([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 83 | - (void)loadAccount |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 84 | { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 85 | auto account = AccountModel::instance().selectedAccount(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 86 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 87 | [aliasTextField setStringValue:account->alias().toNSString()]; |
| 88 | [serverHostTextField setStringValue:account->hostname().toNSString()]; |
| 89 | [usernameTextField setStringValue:account->username().toNSString()]; |
| 90 | [passwordTextField setStringValue:account->password().toNSString()]; |
| 91 | [clearTextField setStringValue:account->password().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 92 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 93 | [upnpButton setState:AccountModel::instance().selectedAccount()->isUpnpEnabled()]; |
| 94 | [userAgentButton setState:AccountModel::instance().selectedAccount()->hasCustomUserAgent()]; |
| 95 | [userAgentTextField setEnabled:AccountModel::instance().selectedAccount()->hasCustomUserAgent()]; |
| 96 | [autoAnswerButton setState:AccountModel::instance().selectedAccount()->isAutoAnswer()]; |
| 97 | [userAgentTextField setStringValue:account->userAgent().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 98 | } |
| 99 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 100 | - (IBAction)tryRegistration:(id)sender { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 101 | AccountModel::instance().selectedAccount() << Account::EditAction::SAVE; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | - (IBAction)showPassword:(NSButton *)sender { |
| 105 | if (sender.state == NSOnState) { |
| 106 | clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame]; |
| 107 | [clearTextField setTag:passwordTextField.tag]; |
| 108 | [clearTextField setDelegate:self]; |
| 109 | [clearTextField setBounds:passwordTextField.bounds]; |
| 110 | [clearTextField setStringValue:passwordTextField.stringValue]; |
| 111 | [clearTextField becomeFirstResponder]; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 112 | [self.view addSubview:clearTextField]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 113 | [passwordTextField setHidden:YES]; |
| 114 | } else { |
| 115 | [passwordTextField setStringValue:clearTextField.stringValue]; |
| 116 | [passwordTextField setHidden:NO]; |
| 117 | [clearTextField removeFromSuperview]; |
| 118 | clearTextField = nil; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Debug purpose |
| 124 | */ |
| 125 | -(void) dumpFrame:(CGRect) frame WithName:(NSString*) name |
| 126 | { |
| 127 | NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); |
| 128 | } |
| 129 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 130 | #pragma mark - NSTextFieldDelegate methods |
| 131 | |
| 132 | - (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor |
| 133 | { |
| 134 | return YES; |
| 135 | } |
| 136 | |
| 137 | -(void)controlTextDidChange:(NSNotification *)notif |
| 138 | { |
| 139 | NSTextField *textField = [notif object]; |
| 140 | |
| 141 | switch ([textField tag]) { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 142 | case TagViews::ALIAS: |
| 143 | AccountModel::instance().selectedAccount()->setAlias([[textField stringValue] UTF8String]); |
| 144 | AccountModel::instance().selectedAccount()->setDisplayName([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 145 | break; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 146 | case TagViews::HOSTNAME: |
| 147 | AccountModel::instance().selectedAccount()->setHostname([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 148 | break; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 149 | case TagViews::USERNAME: |
| 150 | AccountModel::instance().selectedAccount()->setUsername([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 151 | break; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 152 | case TagViews::PASSWORD: |
| 153 | AccountModel::instance().selectedAccount()->setPassword([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 154 | break; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame^] | 155 | case TagViews::USERAGENT: |
| 156 | AccountModel::instance().selectedAccount()->setUserAgent([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 157 | break; |
| 158 | default: |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | @end |