Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2015 Savoir-Faire Linux Inc. |
| 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. |
| 18 | * |
| 19 | * Additional permission under GNU GPL version 3 section 7: |
| 20 | * |
| 21 | * If you modify this program, or any covered work, by linking or |
| 22 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 23 | * modified version of that library), containing parts covered by the |
| 24 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 25 | * grants you additional permission to convey the resulting work. |
| 26 | * Corresponding Source for a non-source form of such a combination |
| 27 | * shall include the source code for the parts of OpenSSL used as well |
| 28 | * as that of the covered work. |
| 29 | */ |
| 30 | #define ALIAS_TAG 0 |
| 31 | #define HOSTNAME_TAG 1 |
| 32 | #define USERNAME_TAG 2 |
| 33 | #define PASSWORD_TAG 3 |
| 34 | #define USERAGENT_TAG 4 |
| 35 | |
| 36 | |
| 37 | #import "AccGeneralVC.h" |
| 38 | |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 39 | #import <accountmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 40 | #import <protocolmodel.h> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 41 | #import <qitemselectionmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 42 | |
| 43 | @interface AccGeneralVC () |
| 44 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 45 | @property (assign) IBOutlet NSView *boxingAccount; |
| 46 | @property (assign) IBOutlet NSView *boxingParameters; |
| 47 | @property (assign) IBOutlet NSView *boxingCommon; |
| 48 | |
| 49 | @property (assign) IBOutlet NSTextField *aliasTextField; |
| 50 | @property (assign) IBOutlet NSTextField *typeLabel; |
| 51 | |
| 52 | @property (assign) IBOutlet NSTextField *serverHostTextField; |
| 53 | @property (assign) IBOutlet NSTextField *usernameTextField; |
| 54 | @property (assign) IBOutlet NSSecureTextField *passwordTextField; |
Alexandre Lision | 0327c0e | 2015-07-29 16:14:30 -0400 | [diff] [blame] | 55 | @property (strong) NSTextField *clearTextField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 56 | |
| 57 | @property (assign) IBOutlet NSButton *upnpButton; |
| 58 | @property (assign) IBOutlet NSButton *autoAnswerButton; |
| 59 | @property (assign) IBOutlet NSButton *userAgentButton; |
| 60 | |
| 61 | @property (assign) IBOutlet NSTextField *userAgentTextField; |
| 62 | |
| 63 | @end |
| 64 | |
| 65 | @implementation AccGeneralVC |
| 66 | @synthesize typeLabel; |
| 67 | @synthesize boxingAccount; |
| 68 | @synthesize boxingParameters; |
| 69 | @synthesize boxingCommon; |
| 70 | @synthesize aliasTextField; |
| 71 | @synthesize serverHostTextField; |
| 72 | @synthesize usernameTextField; |
| 73 | @synthesize passwordTextField; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 74 | @synthesize clearTextField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 75 | @synthesize upnpButton; |
| 76 | @synthesize autoAnswerButton; |
| 77 | @synthesize userAgentButton; |
| 78 | @synthesize userAgentTextField; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 79 | |
| 80 | - (void)awakeFromNib |
| 81 | { |
| 82 | NSLog(@"INIT General VC"); |
| 83 | [aliasTextField setTag:ALIAS_TAG]; |
| 84 | [serverHostTextField setTag:HOSTNAME_TAG]; |
| 85 | [usernameTextField setTag:USERNAME_TAG]; |
| 86 | [passwordTextField setTag:PASSWORD_TAG]; |
| 87 | [userAgentTextField setTag:USERAGENT_TAG]; |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 88 | |
| 89 | QObject::connect(AccountModel::instance()->selectionModel(), |
| 90 | &QItemSelectionModel::currentChanged, |
| 91 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 92 | if(!current.isValid()) |
| 93 | return; |
| 94 | [self loadAccount]; |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | - (Account*) currentAccount |
| 99 | { |
| 100 | auto accIdx = AccountModel::instance()->selectionModel()->currentIndex(); |
| 101 | return AccountModel::instance()->getAccountByModelIndex(accIdx); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | - (IBAction)toggleUpnp:(NSButton *)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 105 | [self currentAccount]->setUpnpEnabled([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | - (IBAction)toggleAutoAnswer:(NSButton *)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 109 | [self currentAccount]->setAutoAnswer([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | - (IBAction)toggleCustomAgent:(NSButton *)sender { |
| 113 | [self.userAgentTextField setEnabled:[sender state] == NSOnState]; |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 114 | [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 117 | - (void)loadAccount |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 118 | { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 119 | auto account = [self currentAccount]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 120 | |
| 121 | if([account->alias().toNSString() isEqualToString:@"IP2IP"]) { |
| 122 | [boxingAccount.subviews setValue:@YES forKeyPath:@"hidden"]; |
| 123 | [boxingParameters.subviews setValue:@YES forKeyPath:@"hidden"]; |
| 124 | |
| 125 | NSLog(@"IP@IP"); |
| 126 | // Put visible items at top of the frame |
| 127 | [boxingCommon setFrameOrigin:NSMakePoint(boxingAccount.frame.origin.x, |
| 128 | boxingAccount.frame.origin.y - 40)]; |
| 129 | [boxingCommon setNeedsDisplay:YES]; |
| 130 | |
| 131 | } else { |
| 132 | [boxingAccount.subviews setValue:@NO forKeyPath:@"hidden"]; |
| 133 | [boxingParameters.subviews setValue:@NO forKeyPath:@"hidden"]; |
| 134 | |
| 135 | [self.aliasTextField setStringValue:account->alias().toNSString()]; |
| 136 | [self.serverHostTextField setStringValue:account->hostname().toNSString()]; |
| 137 | [self.usernameTextField setStringValue:account->username().toNSString()]; |
| 138 | [self.passwordTextField setStringValue:account->password().toNSString()]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 139 | [self.clearTextField setStringValue:account->password().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | switch (account->protocol()) { |
| 143 | case Account::Protocol::SIP: |
| 144 | [self.typeLabel setStringValue:@"SIP"]; |
| 145 | break; |
| 146 | case Account::Protocol::IAX: |
| 147 | [self.typeLabel setStringValue:@"IAX"]; |
| 148 | break; |
| 149 | case Account::Protocol::RING: |
| 150 | [self.typeLabel setStringValue:@"RING"]; |
| 151 | break; |
| 152 | |
| 153 | default: |
| 154 | break; |
| 155 | } |
| 156 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 157 | [upnpButton setState:[self currentAccount]->isUpnpEnabled()]; |
| 158 | [userAgentButton setState:[self currentAccount]->hasCustomUserAgent()]; |
| 159 | [userAgentTextField setEnabled:[self currentAccount]->hasCustomUserAgent()]; |
| 160 | [self.autoAnswerButton setState:[self currentAccount]->isAutoAnswer()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 161 | [self.userAgentTextField setStringValue:account->userAgent().toNSString()]; |
| 162 | } |
| 163 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 164 | - (IBAction)tryRegistration:(id)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 165 | [self currentAccount] << Account::EditAction::SAVE; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | - (IBAction)showPassword:(NSButton *)sender { |
| 169 | if (sender.state == NSOnState) { |
| 170 | clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame]; |
| 171 | [clearTextField setTag:passwordTextField.tag]; |
| 172 | [clearTextField setDelegate:self]; |
| 173 | [clearTextField setBounds:passwordTextField.bounds]; |
| 174 | [clearTextField setStringValue:passwordTextField.stringValue]; |
| 175 | [clearTextField becomeFirstResponder]; |
| 176 | [boxingParameters addSubview:clearTextField]; |
| 177 | [passwordTextField setHidden:YES]; |
| 178 | } else { |
| 179 | [passwordTextField setStringValue:clearTextField.stringValue]; |
| 180 | [passwordTextField setHidden:NO]; |
| 181 | [clearTextField removeFromSuperview]; |
| 182 | clearTextField = nil; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Debug purpose |
| 188 | */ |
| 189 | -(void) dumpFrame:(CGRect) frame WithName:(NSString*) name |
| 190 | { |
| 191 | NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); |
| 192 | } |
| 193 | |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 194 | #pragma mark - NSTextFieldDelegate methods |
| 195 | |
| 196 | - (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor |
| 197 | { |
| 198 | return YES; |
| 199 | } |
| 200 | |
| 201 | -(void)controlTextDidChange:(NSNotification *)notif |
| 202 | { |
| 203 | NSTextField *textField = [notif object]; |
| 204 | |
| 205 | switch ([textField tag]) { |
| 206 | case ALIAS_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 207 | [self currentAccount]->setAlias([[textField stringValue] UTF8String]); |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 208 | [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 209 | break; |
| 210 | case HOSTNAME_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 211 | [self currentAccount]->setHostname([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 212 | break; |
| 213 | case USERNAME_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 214 | [self currentAccount]->setUsername([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 215 | break; |
| 216 | case PASSWORD_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 217 | [self currentAccount]->setPassword([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 218 | break; |
| 219 | case USERAGENT_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 220 | [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 221 | break; |
| 222 | default: |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | @end |