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 | */ |
| 19 | #define ALIAS_TAG 0 |
| 20 | #define HOSTNAME_TAG 1 |
| 21 | #define USERNAME_TAG 2 |
| 22 | #define PASSWORD_TAG 3 |
| 23 | #define USERAGENT_TAG 4 |
| 24 | |
| 25 | |
| 26 | #import "AccGeneralVC.h" |
| 27 | |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 28 | #import <accountmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 29 | #import <protocolmodel.h> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 30 | #import <qitemselectionmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 31 | |
| 32 | @interface AccGeneralVC () |
| 33 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 34 | @property (assign) IBOutlet NSView *boxingParameters; |
| 35 | @property (assign) IBOutlet NSView *boxingCommon; |
| 36 | |
| 37 | @property (assign) IBOutlet NSTextField *aliasTextField; |
| 38 | @property (assign) IBOutlet NSTextField *typeLabel; |
| 39 | |
| 40 | @property (assign) IBOutlet NSTextField *serverHostTextField; |
| 41 | @property (assign) IBOutlet NSTextField *usernameTextField; |
| 42 | @property (assign) IBOutlet NSSecureTextField *passwordTextField; |
Alexandre Lision | 0327c0e | 2015-07-29 16:14:30 -0400 | [diff] [blame] | 43 | @property (strong) NSTextField *clearTextField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 44 | |
| 45 | @property (assign) IBOutlet NSButton *upnpButton; |
| 46 | @property (assign) IBOutlet NSButton *autoAnswerButton; |
| 47 | @property (assign) IBOutlet NSButton *userAgentButton; |
| 48 | |
| 49 | @property (assign) IBOutlet NSTextField *userAgentTextField; |
| 50 | |
| 51 | @end |
| 52 | |
| 53 | @implementation AccGeneralVC |
| 54 | @synthesize typeLabel; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 55 | @synthesize boxingParameters; |
| 56 | @synthesize boxingCommon; |
| 57 | @synthesize aliasTextField; |
| 58 | @synthesize serverHostTextField; |
| 59 | @synthesize usernameTextField; |
| 60 | @synthesize passwordTextField; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 61 | @synthesize clearTextField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 62 | @synthesize upnpButton; |
| 63 | @synthesize autoAnswerButton; |
| 64 | @synthesize userAgentButton; |
| 65 | @synthesize userAgentTextField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 66 | |
| 67 | - (void)awakeFromNib |
| 68 | { |
| 69 | NSLog(@"INIT General VC"); |
| 70 | [aliasTextField setTag:ALIAS_TAG]; |
| 71 | [serverHostTextField setTag:HOSTNAME_TAG]; |
| 72 | [usernameTextField setTag:USERNAME_TAG]; |
| 73 | [passwordTextField setTag:PASSWORD_TAG]; |
| 74 | [userAgentTextField setTag:USERAGENT_TAG]; |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 75 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 76 | QObject::connect(AccountModel::instance().selectionModel(), |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 77 | &QItemSelectionModel::currentChanged, |
| 78 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 79 | if(!current.isValid()) |
| 80 | return; |
| 81 | [self loadAccount]; |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | - (Account*) currentAccount |
| 86 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 87 | auto accIdx = AccountModel::instance().selectionModel()->currentIndex(); |
| 88 | return AccountModel::instance().getAccountByModelIndex(accIdx); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | - (IBAction)toggleUpnp:(NSButton *)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 92 | [self currentAccount]->setUpnpEnabled([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | - (IBAction)toggleAutoAnswer:(NSButton *)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 96 | [self currentAccount]->setAutoAnswer([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | - (IBAction)toggleCustomAgent:(NSButton *)sender { |
| 100 | [self.userAgentTextField setEnabled:[sender state] == NSOnState]; |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 101 | [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 102 | } |
| 103 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 104 | - (void)loadAccount |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 105 | { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 106 | auto account = [self currentAccount]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 107 | |
Alexandre Lision | 5c0803f | 2016-04-04 16:13:50 -0400 | [diff] [blame] | 108 | [boxingParameters.subviews setValue:@NO forKeyPath:@"hidden"]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 109 | |
Alexandre Lision | 5c0803f | 2016-04-04 16:13:50 -0400 | [diff] [blame] | 110 | [self.aliasTextField setStringValue:account->alias().toNSString()]; |
| 111 | [self.serverHostTextField setStringValue:account->hostname().toNSString()]; |
| 112 | [self.usernameTextField setStringValue:account->username().toNSString()]; |
| 113 | [self.passwordTextField setStringValue:account->password().toNSString()]; |
| 114 | [self.clearTextField setStringValue:account->password().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 115 | |
| 116 | switch (account->protocol()) { |
| 117 | case Account::Protocol::SIP: |
| 118 | [self.typeLabel setStringValue:@"SIP"]; |
| 119 | break; |
| 120 | case Account::Protocol::IAX: |
| 121 | [self.typeLabel setStringValue:@"IAX"]; |
| 122 | break; |
| 123 | case Account::Protocol::RING: |
| 124 | [self.typeLabel setStringValue:@"RING"]; |
| 125 | break; |
| 126 | |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 131 | [upnpButton setState:[self currentAccount]->isUpnpEnabled()]; |
| 132 | [userAgentButton setState:[self currentAccount]->hasCustomUserAgent()]; |
| 133 | [userAgentTextField setEnabled:[self currentAccount]->hasCustomUserAgent()]; |
| 134 | [self.autoAnswerButton setState:[self currentAccount]->isAutoAnswer()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 135 | [self.userAgentTextField setStringValue:account->userAgent().toNSString()]; |
| 136 | } |
| 137 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 138 | - (IBAction)tryRegistration:(id)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 139 | [self currentAccount] << Account::EditAction::SAVE; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | - (IBAction)showPassword:(NSButton *)sender { |
| 143 | if (sender.state == NSOnState) { |
| 144 | clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame]; |
| 145 | [clearTextField setTag:passwordTextField.tag]; |
| 146 | [clearTextField setDelegate:self]; |
| 147 | [clearTextField setBounds:passwordTextField.bounds]; |
| 148 | [clearTextField setStringValue:passwordTextField.stringValue]; |
| 149 | [clearTextField becomeFirstResponder]; |
| 150 | [boxingParameters addSubview:clearTextField]; |
| 151 | [passwordTextField setHidden:YES]; |
| 152 | } else { |
| 153 | [passwordTextField setStringValue:clearTextField.stringValue]; |
| 154 | [passwordTextField setHidden:NO]; |
| 155 | [clearTextField removeFromSuperview]; |
| 156 | clearTextField = nil; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Debug purpose |
| 162 | */ |
| 163 | -(void) dumpFrame:(CGRect) frame WithName:(NSString*) name |
| 164 | { |
| 165 | NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); |
| 166 | } |
| 167 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 168 | #pragma mark - NSTextFieldDelegate methods |
| 169 | |
| 170 | - (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor |
| 171 | { |
| 172 | return YES; |
| 173 | } |
| 174 | |
| 175 | -(void)controlTextDidChange:(NSNotification *)notif |
| 176 | { |
| 177 | NSTextField *textField = [notif object]; |
| 178 | |
| 179 | switch ([textField tag]) { |
| 180 | case ALIAS_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 181 | [self currentAccount]->setAlias([[textField stringValue] UTF8String]); |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 182 | [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 183 | break; |
| 184 | case HOSTNAME_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 185 | [self currentAccount]->setHostname([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 186 | break; |
| 187 | case USERNAME_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 188 | [self currentAccount]->setUsername([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 189 | break; |
| 190 | case PASSWORD_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 191 | [self currentAccount]->setPassword([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 192 | break; |
| 193 | case USERAGENT_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 194 | [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 195 | break; |
| 196 | default: |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | @end |