blob: d5862fb4b87429f4e10cc916b6dd4e0e176d1605 [file] [log] [blame]
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04003 * 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 Lisionf5fc4792015-03-17 09:15:43 -040018 */
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 Lision91d11e52015-03-20 17:42:05 -040028#import <accountmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040029#import <protocolmodel.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040030#import <qitemselectionmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040031
32@interface AccGeneralVC ()
33
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040034@property (assign) IBOutlet NSView *boxingAccount;
35@property (assign) IBOutlet NSView *boxingParameters;
36@property (assign) IBOutlet NSView *boxingCommon;
37
38@property (assign) IBOutlet NSTextField *aliasTextField;
39@property (assign) IBOutlet NSTextField *typeLabel;
40
41@property (assign) IBOutlet NSTextField *serverHostTextField;
42@property (assign) IBOutlet NSTextField *usernameTextField;
43@property (assign) IBOutlet NSSecureTextField *passwordTextField;
Alexandre Lision0327c0e2015-07-29 16:14:30 -040044@property (strong) NSTextField *clearTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040045
46@property (assign) IBOutlet NSButton *upnpButton;
47@property (assign) IBOutlet NSButton *autoAnswerButton;
48@property (assign) IBOutlet NSButton *userAgentButton;
49
50@property (assign) IBOutlet NSTextField *userAgentTextField;
51
52@end
53
54@implementation AccGeneralVC
55@synthesize typeLabel;
56@synthesize boxingAccount;
57@synthesize boxingParameters;
58@synthesize boxingCommon;
59@synthesize aliasTextField;
60@synthesize serverHostTextField;
61@synthesize usernameTextField;
62@synthesize passwordTextField;
Alexandre Lision4de68ce2015-04-24 18:22:49 -040063@synthesize clearTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040064@synthesize upnpButton;
65@synthesize autoAnswerButton;
66@synthesize userAgentButton;
67@synthesize userAgentTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040068
69- (void)awakeFromNib
70{
71 NSLog(@"INIT General VC");
72 [aliasTextField setTag:ALIAS_TAG];
73 [serverHostTextField setTag:HOSTNAME_TAG];
74 [usernameTextField setTag:USERNAME_TAG];
75 [passwordTextField setTag:PASSWORD_TAG];
76 [userAgentTextField setTag:USERAGENT_TAG];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040077
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040078 QObject::connect(AccountModel::instance().selectionModel(),
Alexandre Lision7f3164c2015-06-12 11:45:37 -040079 &QItemSelectionModel::currentChanged,
80 [=](const QModelIndex &current, const QModelIndex &previous) {
81 if(!current.isValid())
82 return;
83 [self loadAccount];
84 });
85}
86
87- (Account*) currentAccount
88{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040089 auto accIdx = AccountModel::instance().selectionModel()->currentIndex();
90 return AccountModel::instance().getAccountByModelIndex(accIdx);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040091}
92
93- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -040094 [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040095}
96
97- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -040098 [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040099}
100
101- (IBAction)toggleCustomAgent:(NSButton *)sender {
102 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400103 [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400104}
105
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400106- (void)loadAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400107{
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400108 auto account = [self currentAccount];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400109
110 if([account->alias().toNSString() isEqualToString:@"IP2IP"]) {
111 [boxingAccount.subviews setValue:@YES forKeyPath:@"hidden"];
112 [boxingParameters.subviews setValue:@YES forKeyPath:@"hidden"];
113
114 NSLog(@"IP@IP");
115 // Put visible items at top of the frame
116 [boxingCommon setFrameOrigin:NSMakePoint(boxingAccount.frame.origin.x,
117 boxingAccount.frame.origin.y - 40)];
118 [boxingCommon setNeedsDisplay:YES];
119
120 } else {
121 [boxingAccount.subviews setValue:@NO forKeyPath:@"hidden"];
122 [boxingParameters.subviews setValue:@NO forKeyPath:@"hidden"];
123
124 [self.aliasTextField setStringValue:account->alias().toNSString()];
125 [self.serverHostTextField setStringValue:account->hostname().toNSString()];
126 [self.usernameTextField setStringValue:account->username().toNSString()];
127 [self.passwordTextField setStringValue:account->password().toNSString()];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400128 [self.clearTextField setStringValue:account->password().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400129 }
130
131 switch (account->protocol()) {
132 case Account::Protocol::SIP:
133 [self.typeLabel setStringValue:@"SIP"];
134 break;
135 case Account::Protocol::IAX:
136 [self.typeLabel setStringValue:@"IAX"];
137 break;
138 case Account::Protocol::RING:
139 [self.typeLabel setStringValue:@"RING"];
140 break;
141
142 default:
143 break;
144 }
145
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400146 [upnpButton setState:[self currentAccount]->isUpnpEnabled()];
147 [userAgentButton setState:[self currentAccount]->hasCustomUserAgent()];
148 [userAgentTextField setEnabled:[self currentAccount]->hasCustomUserAgent()];
149 [self.autoAnswerButton setState:[self currentAccount]->isAutoAnswer()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400150 [self.userAgentTextField setStringValue:account->userAgent().toNSString()];
151}
152
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400153- (IBAction)tryRegistration:(id)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400154 [self currentAccount] << Account::EditAction::SAVE;
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400155}
156
157- (IBAction)showPassword:(NSButton *)sender {
158 if (sender.state == NSOnState) {
159 clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame];
160 [clearTextField setTag:passwordTextField.tag];
161 [clearTextField setDelegate:self];
162 [clearTextField setBounds:passwordTextField.bounds];
163 [clearTextField setStringValue:passwordTextField.stringValue];
164 [clearTextField becomeFirstResponder];
165 [boxingParameters addSubview:clearTextField];
166 [passwordTextField setHidden:YES];
167 } else {
168 [passwordTextField setStringValue:clearTextField.stringValue];
169 [passwordTextField setHidden:NO];
170 [clearTextField removeFromSuperview];
171 clearTextField = nil;
172 }
173}
174
175/**
176 * Debug purpose
177 */
178-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
179{
180 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
181}
182
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400183#pragma mark - NSTextFieldDelegate methods
184
185- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
186{
187 return YES;
188}
189
190-(void)controlTextDidChange:(NSNotification *)notif
191{
192 NSTextField *textField = [notif object];
193
194 switch ([textField tag]) {
195 case ALIAS_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400196 [self currentAccount]->setAlias([[textField stringValue] UTF8String]);
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400197 [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400198 break;
199 case HOSTNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400200 [self currentAccount]->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400201 break;
202 case USERNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400203 [self currentAccount]->setUsername([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400204 break;
205 case PASSWORD_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400206 [self currentAccount]->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400207 break;
208 case USERAGENT_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400209 [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400210 break;
211 default:
212 break;
213 }
214}
215@end