blob: 16d7761dda2a6bafe04a206cfff26a1bf064f36a [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 *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 Lision0327c0e2015-07-29 16:14:30 -040043@property (strong) NSTextField *clearTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040044
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 Lisionf5fc4792015-03-17 09:15:43 -040055@synthesize boxingParameters;
56@synthesize boxingCommon;
57@synthesize aliasTextField;
58@synthesize serverHostTextField;
59@synthesize usernameTextField;
60@synthesize passwordTextField;
Alexandre Lision4de68ce2015-04-24 18:22:49 -040061@synthesize clearTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040062@synthesize upnpButton;
63@synthesize autoAnswerButton;
64@synthesize userAgentButton;
65@synthesize userAgentTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040066
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 Lision7f3164c2015-06-12 11:45:37 -040075
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040076 QObject::connect(AccountModel::instance().selectionModel(),
Alexandre Lision7f3164c2015-06-12 11:45:37 -040077 &QItemSelectionModel::currentChanged,
78 [=](const QModelIndex &current, const QModelIndex &previous) {
79 if(!current.isValid())
80 return;
81 [self loadAccount];
82 });
83}
84
85- (Account*) currentAccount
86{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040087 auto accIdx = AccountModel::instance().selectionModel()->currentIndex();
88 return AccountModel::instance().getAccountByModelIndex(accIdx);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040089}
90
91- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -040092 [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040093}
94
95- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -040096 [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040097}
98
99- (IBAction)toggleCustomAgent:(NSButton *)sender {
100 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400101 [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400102}
103
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400104- (void)loadAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400105{
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400106 auto account = [self currentAccount];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400107
Alexandre Lision5c0803f2016-04-04 16:13:50 -0400108 [boxingParameters.subviews setValue:@NO forKeyPath:@"hidden"];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400109
Alexandre Lision5c0803f2016-04-04 16:13:50 -0400110 [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 Lisionf5fc4792015-03-17 09:15:43 -0400115
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 Lision7f3164c2015-06-12 11:45:37 -0400131 [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 Lisionf5fc4792015-03-17 09:15:43 -0400135 [self.userAgentTextField setStringValue:account->userAgent().toNSString()];
136}
137
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400138- (IBAction)tryRegistration:(id)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400139 [self currentAccount] << Account::EditAction::SAVE;
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400140}
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 Lisionf5fc4792015-03-17 09:15:43 -0400168#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 Lision7f3164c2015-06-12 11:45:37 -0400181 [self currentAccount]->setAlias([[textField stringValue] UTF8String]);
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400182 [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400183 break;
184 case HOSTNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400185 [self currentAccount]->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400186 break;
187 case USERNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400188 [self currentAccount]->setUsername([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400189 break;
190 case PASSWORD_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400191 [self currentAccount]->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400192 break;
193 case USERAGENT_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400194 [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400195 break;
196 default:
197 break;
198 }
199}
200@end