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 | f46768f | 2016-04-18 09:56:26 -0400 | [diff] [blame] | 83 | - (IBAction)removeAccount:(id)sender { |
| 84 | AccountModel::instance().remove(AccountModel::instance().selectedAccount()); |
| 85 | AccountModel::instance().save(); |
| 86 | } |
| 87 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 88 | - (void)loadAccount |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 89 | { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 90 | auto account = AccountModel::instance().selectedAccount(); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 91 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 92 | [aliasTextField setStringValue:account->alias().toNSString()]; |
| 93 | [serverHostTextField setStringValue:account->hostname().toNSString()]; |
| 94 | [usernameTextField setStringValue:account->username().toNSString()]; |
| 95 | [passwordTextField setStringValue:account->password().toNSString()]; |
| 96 | [clearTextField setStringValue:account->password().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 97 | |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 98 | [upnpButton setState:AccountModel::instance().selectedAccount()->isUpnpEnabled()]; |
| 99 | [userAgentButton setState:AccountModel::instance().selectedAccount()->hasCustomUserAgent()]; |
| 100 | [userAgentTextField setEnabled:AccountModel::instance().selectedAccount()->hasCustomUserAgent()]; |
| 101 | [autoAnswerButton setState:AccountModel::instance().selectedAccount()->isAutoAnswer()]; |
| 102 | [userAgentTextField setStringValue:account->userAgent().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 105 | - (IBAction)tryRegistration:(id)sender { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 106 | AccountModel::instance().selectedAccount() << Account::EditAction::SAVE; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | - (IBAction)showPassword:(NSButton *)sender { |
| 110 | if (sender.state == NSOnState) { |
| 111 | clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame]; |
| 112 | [clearTextField setTag:passwordTextField.tag]; |
| 113 | [clearTextField setDelegate:self]; |
| 114 | [clearTextField setBounds:passwordTextField.bounds]; |
| 115 | [clearTextField setStringValue:passwordTextField.stringValue]; |
| 116 | [clearTextField becomeFirstResponder]; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 117 | [self.view addSubview:clearTextField]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 118 | [passwordTextField setHidden:YES]; |
| 119 | } else { |
| 120 | [passwordTextField setStringValue:clearTextField.stringValue]; |
| 121 | [passwordTextField setHidden:NO]; |
| 122 | [clearTextField removeFromSuperview]; |
| 123 | clearTextField = nil; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Debug purpose |
| 129 | */ |
| 130 | -(void) dumpFrame:(CGRect) frame WithName:(NSString*) name |
| 131 | { |
| 132 | NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); |
| 133 | } |
| 134 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 135 | #pragma mark - NSTextFieldDelegate methods |
| 136 | |
| 137 | - (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor |
| 138 | { |
| 139 | return YES; |
| 140 | } |
| 141 | |
| 142 | -(void)controlTextDidChange:(NSNotification *)notif |
| 143 | { |
| 144 | NSTextField *textField = [notif object]; |
| 145 | |
| 146 | switch ([textField tag]) { |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 147 | case TagViews::ALIAS: |
| 148 | AccountModel::instance().selectedAccount()->setAlias([[textField stringValue] UTF8String]); |
| 149 | AccountModel::instance().selectedAccount()->setDisplayName([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 150 | break; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 151 | case TagViews::HOSTNAME: |
| 152 | AccountModel::instance().selectedAccount()->setHostname([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 153 | break; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 154 | case TagViews::USERNAME: |
| 155 | AccountModel::instance().selectedAccount()->setUsername([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 156 | break; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 157 | case TagViews::PASSWORD: |
| 158 | AccountModel::instance().selectedAccount()->setPassword([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 159 | break; |
Alexandre Lision | c1f9666 | 2016-03-23 17:24:19 -0400 | [diff] [blame] | 160 | case TagViews::USERAGENT: |
| 161 | AccountModel::instance().selectedAccount()->setUserAgent([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 162 | break; |
| 163 | default: |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | @end |