blob: e1adf4feaf2206840161e2f50c5e36b364e5566e [file] [log] [blame]
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04001/*
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 Lision91d11e52015-03-20 17:42:05 -040039#import <accountmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040040#import <protocolmodel.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040041#import <qitemselectionmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040042
43@interface AccGeneralVC ()
44
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040045@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 Lision0327c0e2015-07-29 16:14:30 -040055@property (strong) NSTextField *clearTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040056
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 Lision4de68ce2015-04-24 18:22:49 -040074@synthesize clearTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040075@synthesize upnpButton;
76@synthesize autoAnswerButton;
77@synthesize userAgentButton;
78@synthesize userAgentTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040079
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 Lision7f3164c2015-06-12 11:45:37 -040088
89 QObject::connect(AccountModel::instance()->selectionModel(),
90 &QItemSelectionModel::currentChanged,
91 [=](const QModelIndex &current, 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 Lisionf5fc4792015-03-17 09:15:43 -0400102}
103
104- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400105 [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400106}
107
108- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400109 [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400110}
111
112- (IBAction)toggleCustomAgent:(NSButton *)sender {
113 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400114 [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400115}
116
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400117- (void)loadAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400118{
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400119 auto account = [self currentAccount];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400120
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 Lision4de68ce2015-04-24 18:22:49 -0400139 [self.clearTextField setStringValue:account->password().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400140 }
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 Lision7f3164c2015-06-12 11:45:37 -0400157 [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 Lisionf5fc4792015-03-17 09:15:43 -0400161 [self.userAgentTextField setStringValue:account->userAgent().toNSString()];
162}
163
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400164- (IBAction)tryRegistration:(id)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400165 [self currentAccount] << Account::EditAction::SAVE;
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400166}
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 Lisionf5fc4792015-03-17 09:15:43 -0400194#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 Lision7f3164c2015-06-12 11:45:37 -0400207 [self currentAccount]->setAlias([[textField stringValue] UTF8String]);
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400208 [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400209 break;
210 case HOSTNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400211 [self currentAccount]->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400212 break;
213 case USERNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400214 [self currentAccount]->setUsername([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400215 break;
216 case PASSWORD_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400217 [self currentAccount]->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400218 break;
219 case USERAGENT_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400220 [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400221 break;
222 default:
223 break;
224 }
225}
226@end