blob: a209136bc8b797e1b3ec116e55d4622d937ad94a [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 */
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040019#import "AccGeneralVC.h"
20
Alexandre Lision91d11e52015-03-20 17:42:05 -040021#import <accountmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040022#import <protocolmodel.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040023#import <qitemselectionmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040024
Alexandre Lisionc1f96662016-03-23 17:24:19 -040025@interface AccGeneralVC () {
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040026
Alexandre Lisionc1f96662016-03-23 17:24:19 -040027 __unsafe_unretained IBOutlet NSTextField *aliasTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040028
Alexandre Lisionc1f96662016-03-23 17:24:19 -040029 __unsafe_unretained IBOutlet NSTextField *serverHostTextField;
30 __unsafe_unretained IBOutlet NSTextField *usernameTextField;
31 __unsafe_unretained IBOutlet NSSecureTextField *passwordTextField;
32 NSTextField *clearTextField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040033
Alexandre Lisionc1f96662016-03-23 17:24:19 -040034 __unsafe_unretained IBOutlet NSButton *upnpButton;
35 __unsafe_unretained IBOutlet NSButton *autoAnswerButton;
36 __unsafe_unretained IBOutlet NSButton *userAgentButton;
37 __unsafe_unretained IBOutlet NSTextField *userAgentTextField;
38}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040039@end
40
41@implementation AccGeneralVC
Alexandre Lisionc1f96662016-03-23 17:24:19 -040042
43//Tags for views
44typedef NS_ENUM(NSInteger, TagViews) {
45 ALIAS = 0,
46 HOSTNAME = 1,
47 USERNAME = 2,
48 PASSWORD = 3,
49 USERAGENT = 4,
Alexandre Lision52705022016-05-16 17:41:37 -040050 DTMF_SIP = 5,
51 DTMF_RTP = 6,
Alexandre Lisionc1f96662016-03-23 17:24:19 -040052};
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040053
54- (void)awakeFromNib
55{
56 NSLog(@"INIT General VC");
Alexandre Lisionc1f96662016-03-23 17:24:19 -040057 [aliasTextField setTag:TagViews::ALIAS];
58 [serverHostTextField setTag:TagViews::HOSTNAME];
59 [usernameTextField setTag:TagViews::USERNAME];
60 [passwordTextField setTag:TagViews::PASSWORD];
61 [userAgentTextField setTag:TagViews::USERAGENT];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040062
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040063 QObject::connect(AccountModel::instance().selectionModel(),
Alexandre Lision7f3164c2015-06-12 11:45:37 -040064 &QItemSelectionModel::currentChanged,
65 [=](const QModelIndex &current, const QModelIndex &previous) {
66 if(!current.isValid())
67 return;
68 [self loadAccount];
69 });
70}
71
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040072- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -040073 AccountModel::instance().selectedAccount()->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040074}
75
76- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -040077 AccountModel::instance().selectedAccount()->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040078}
79
80- (IBAction)toggleCustomAgent:(NSButton *)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -040081 [userAgentTextField setEnabled:[sender state] == NSOnState];
82 AccountModel::instance().selectedAccount()->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040083}
84
Alexandre Lision7f3164c2015-06-12 11:45:37 -040085- (void)loadAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040086{
Alexandre Lisionc1f96662016-03-23 17:24:19 -040087 auto account = AccountModel::instance().selectedAccount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040088
Alexandre Lisionc1f96662016-03-23 17:24:19 -040089 [aliasTextField setStringValue:account->alias().toNSString()];
90 [serverHostTextField setStringValue:account->hostname().toNSString()];
91 [usernameTextField setStringValue:account->username().toNSString()];
92 [passwordTextField setStringValue:account->password().toNSString()];
93 [clearTextField setStringValue:account->password().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040094
Alexandre Lisionc1f96662016-03-23 17:24:19 -040095 [upnpButton setState:AccountModel::instance().selectedAccount()->isUpnpEnabled()];
96 [userAgentButton setState:AccountModel::instance().selectedAccount()->hasCustomUserAgent()];
97 [userAgentTextField setEnabled:AccountModel::instance().selectedAccount()->hasCustomUserAgent()];
98 [autoAnswerButton setState:AccountModel::instance().selectedAccount()->isAutoAnswer()];
99 [userAgentTextField setStringValue:account->userAgent().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400100}
101
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400102- (IBAction)tryRegistration:(id)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400103 AccountModel::instance().selectedAccount() << Account::EditAction::SAVE;
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400104}
105
106- (IBAction)showPassword:(NSButton *)sender {
107 if (sender.state == NSOnState) {
108 clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame];
109 [clearTextField setTag:passwordTextField.tag];
110 [clearTextField setDelegate:self];
111 [clearTextField setBounds:passwordTextField.bounds];
112 [clearTextField setStringValue:passwordTextField.stringValue];
113 [clearTextField becomeFirstResponder];
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400114 [self.view addSubview:clearTextField];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400115 [passwordTextField setHidden:YES];
116 } else {
117 [passwordTextField setStringValue:clearTextField.stringValue];
118 [passwordTextField setHidden:NO];
119 [clearTextField removeFromSuperview];
120 clearTextField = nil;
121 }
122}
123
124/**
125 * Debug purpose
126 */
127-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
128{
129 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
130}
131
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400132#pragma mark - NSTextFieldDelegate methods
133
134- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
135{
136 return YES;
137}
138
139-(void)controlTextDidChange:(NSNotification *)notif
140{
141 NSTextField *textField = [notif object];
142
143 switch ([textField tag]) {
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400144 case TagViews::ALIAS:
145 AccountModel::instance().selectedAccount()->setAlias([[textField stringValue] UTF8String]);
146 AccountModel::instance().selectedAccount()->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400147 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400148 case TagViews::HOSTNAME:
149 AccountModel::instance().selectedAccount()->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400150 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400151 case TagViews::USERNAME:
152 AccountModel::instance().selectedAccount()->setUsername([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400153 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400154 case TagViews::PASSWORD:
155 AccountModel::instance().selectedAccount()->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400156 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400157 case TagViews::USERAGENT:
158 AccountModel::instance().selectedAccount()->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400159 break;
160 default:
161 break;
162 }
163}
Alexandre Lision52705022016-05-16 17:41:37 -0400164
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400165@end