blob: a2d33b1ba92f41f79297fc9e8895e5d6a1216e0a [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 Lision4de68ce2015-04-24 18:22:49 -040055@property (assign) IBOutlet NSTextField *clearTextField;
56@property (assign) IBOutlet NSButton *tryRegisterButton;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040057
58@property (assign) IBOutlet NSButton *upnpButton;
59@property (assign) IBOutlet NSButton *autoAnswerButton;
60@property (assign) IBOutlet NSButton *userAgentButton;
61
62@property (assign) IBOutlet NSTextField *userAgentTextField;
63
64@end
65
66@implementation AccGeneralVC
67@synthesize typeLabel;
68@synthesize boxingAccount;
69@synthesize boxingParameters;
70@synthesize boxingCommon;
71@synthesize aliasTextField;
72@synthesize serverHostTextField;
73@synthesize usernameTextField;
74@synthesize passwordTextField;
Alexandre Lision4de68ce2015-04-24 18:22:49 -040075@synthesize clearTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040076@synthesize upnpButton;
77@synthesize autoAnswerButton;
78@synthesize userAgentButton;
79@synthesize userAgentTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040080
81- (void)awakeFromNib
82{
83 NSLog(@"INIT General VC");
84 [aliasTextField setTag:ALIAS_TAG];
85 [serverHostTextField setTag:HOSTNAME_TAG];
86 [usernameTextField setTag:USERNAME_TAG];
87 [passwordTextField setTag:PASSWORD_TAG];
88 [userAgentTextField setTag:USERAGENT_TAG];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040089
90 QObject::connect(AccountModel::instance()->selectionModel(),
91 &QItemSelectionModel::currentChanged,
92 [=](const QModelIndex &current, const QModelIndex &previous) {
93 if(!current.isValid())
94 return;
95 [self loadAccount];
96 });
97}
98
99- (Account*) currentAccount
100{
101 auto accIdx = AccountModel::instance()->selectionModel()->currentIndex();
102 return AccountModel::instance()->getAccountByModelIndex(accIdx);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400103}
104
105- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400106 [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400107}
108
109- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400110 [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400111}
112
113- (IBAction)toggleCustomAgent:(NSButton *)sender {
114 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400115 [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400116}
117
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400118- (void)loadAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400119{
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400120 auto account = [self currentAccount];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400121
122 if([account->alias().toNSString() isEqualToString:@"IP2IP"]) {
123 [boxingAccount.subviews setValue:@YES forKeyPath:@"hidden"];
124 [boxingParameters.subviews setValue:@YES forKeyPath:@"hidden"];
125
126 NSLog(@"IP@IP");
127 // Put visible items at top of the frame
128 [boxingCommon setFrameOrigin:NSMakePoint(boxingAccount.frame.origin.x,
129 boxingAccount.frame.origin.y - 40)];
130 [boxingCommon setNeedsDisplay:YES];
131
132 } else {
133 [boxingAccount.subviews setValue:@NO forKeyPath:@"hidden"];
134 [boxingParameters.subviews setValue:@NO forKeyPath:@"hidden"];
135
136 [self.aliasTextField setStringValue:account->alias().toNSString()];
137 [self.serverHostTextField setStringValue:account->hostname().toNSString()];
138 [self.usernameTextField setStringValue:account->username().toNSString()];
139 [self.passwordTextField setStringValue:account->password().toNSString()];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400140 [self.clearTextField setStringValue:account->password().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400141 }
142
143 switch (account->protocol()) {
144 case Account::Protocol::SIP:
145 [self.typeLabel setStringValue:@"SIP"];
146 break;
147 case Account::Protocol::IAX:
148 [self.typeLabel setStringValue:@"IAX"];
149 break;
150 case Account::Protocol::RING:
151 [self.typeLabel setStringValue:@"RING"];
152 break;
153
154 default:
155 break;
156 }
157
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400158 [upnpButton setState:[self currentAccount]->isUpnpEnabled()];
159 [userAgentButton setState:[self currentAccount]->hasCustomUserAgent()];
160 [userAgentTextField setEnabled:[self currentAccount]->hasCustomUserAgent()];
161 [self.autoAnswerButton setState:[self currentAccount]->isAutoAnswer()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400162 [self.userAgentTextField setStringValue:account->userAgent().toNSString()];
163}
164
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400165- (IBAction)tryRegistration:(id)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400166 [self currentAccount] << Account::EditAction::SAVE;
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400167}
168
169- (IBAction)showPassword:(NSButton *)sender {
170 if (sender.state == NSOnState) {
171 clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame];
172 [clearTextField setTag:passwordTextField.tag];
173 [clearTextField setDelegate:self];
174 [clearTextField setBounds:passwordTextField.bounds];
175 [clearTextField setStringValue:passwordTextField.stringValue];
176 [clearTextField becomeFirstResponder];
177 [boxingParameters addSubview:clearTextField];
178 [passwordTextField setHidden:YES];
179 } else {
180 [passwordTextField setStringValue:clearTextField.stringValue];
181 [passwordTextField setHidden:NO];
182 [clearTextField removeFromSuperview];
183 clearTextField = nil;
184 }
185}
186
187/**
188 * Debug purpose
189 */
190-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
191{
192 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
193}
194
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400195#pragma mark - NSTextFieldDelegate methods
196
197- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
198{
199 return YES;
200}
201
202-(void)controlTextDidChange:(NSNotification *)notif
203{
204 NSTextField *textField = [notif object];
205
206 switch ([textField tag]) {
207 case ALIAS_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400208 [self currentAccount]->setAlias([[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