blob: ecd3eed107dd4a2c41f95ad5c3499771e1c115b9 [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,
50};
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040051
52- (void)awakeFromNib
53{
54 NSLog(@"INIT General VC");
Alexandre Lisionc1f96662016-03-23 17:24:19 -040055 [aliasTextField setTag:TagViews::ALIAS];
56 [serverHostTextField setTag:TagViews::HOSTNAME];
57 [usernameTextField setTag:TagViews::USERNAME];
58 [passwordTextField setTag:TagViews::PASSWORD];
59 [userAgentTextField setTag:TagViews::USERAGENT];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040060
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040061 QObject::connect(AccountModel::instance().selectionModel(),
Alexandre Lision7f3164c2015-06-12 11:45:37 -040062 &QItemSelectionModel::currentChanged,
63 [=](const QModelIndex &current, const QModelIndex &previous) {
64 if(!current.isValid())
65 return;
66 [self loadAccount];
67 });
68}
69
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040070- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -040071 AccountModel::instance().selectedAccount()->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040072}
73
74- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -040075 AccountModel::instance().selectedAccount()->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040076}
77
78- (IBAction)toggleCustomAgent:(NSButton *)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -040079 [userAgentTextField setEnabled:[sender state] == NSOnState];
80 AccountModel::instance().selectedAccount()->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040081}
82
Alexandre Lision7f3164c2015-06-12 11:45:37 -040083- (void)loadAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040084{
Alexandre Lisionc1f96662016-03-23 17:24:19 -040085 auto account = AccountModel::instance().selectedAccount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040086
Alexandre Lisionc1f96662016-03-23 17:24:19 -040087 [aliasTextField setStringValue:account->alias().toNSString()];
88 [serverHostTextField setStringValue:account->hostname().toNSString()];
89 [usernameTextField setStringValue:account->username().toNSString()];
90 [passwordTextField setStringValue:account->password().toNSString()];
91 [clearTextField setStringValue:account->password().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040092
Alexandre Lisionc1f96662016-03-23 17:24:19 -040093 [upnpButton setState:AccountModel::instance().selectedAccount()->isUpnpEnabled()];
94 [userAgentButton setState:AccountModel::instance().selectedAccount()->hasCustomUserAgent()];
95 [userAgentTextField setEnabled:AccountModel::instance().selectedAccount()->hasCustomUserAgent()];
96 [autoAnswerButton setState:AccountModel::instance().selectedAccount()->isAutoAnswer()];
97 [userAgentTextField setStringValue:account->userAgent().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040098}
99
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400100- (IBAction)tryRegistration:(id)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400101 AccountModel::instance().selectedAccount() << Account::EditAction::SAVE;
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400102}
103
104- (IBAction)showPassword:(NSButton *)sender {
105 if (sender.state == NSOnState) {
106 clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame];
107 [clearTextField setTag:passwordTextField.tag];
108 [clearTextField setDelegate:self];
109 [clearTextField setBounds:passwordTextField.bounds];
110 [clearTextField setStringValue:passwordTextField.stringValue];
111 [clearTextField becomeFirstResponder];
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400112 [self.view addSubview:clearTextField];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400113 [passwordTextField setHidden:YES];
114 } else {
115 [passwordTextField setStringValue:clearTextField.stringValue];
116 [passwordTextField setHidden:NO];
117 [clearTextField removeFromSuperview];
118 clearTextField = nil;
119 }
120}
121
122/**
123 * Debug purpose
124 */
125-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
126{
127 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
128}
129
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400130#pragma mark - NSTextFieldDelegate methods
131
132- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
133{
134 return YES;
135}
136
137-(void)controlTextDidChange:(NSNotification *)notif
138{
139 NSTextField *textField = [notif object];
140
141 switch ([textField tag]) {
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400142 case TagViews::ALIAS:
143 AccountModel::instance().selectedAccount()->setAlias([[textField stringValue] UTF8String]);
144 AccountModel::instance().selectedAccount()->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400145 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400146 case TagViews::HOSTNAME:
147 AccountModel::instance().selectedAccount()->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400148 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400149 case TagViews::USERNAME:
150 AccountModel::instance().selectedAccount()->setUsername([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400151 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400152 case TagViews::PASSWORD:
153 AccountModel::instance().selectedAccount()->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400154 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400155 case TagViews::USERAGENT:
156 AccountModel::instance().selectedAccount()->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400157 break;
158 default:
159 break;
160 }
161}
162@end