blob: d9d1472d778f76751dd6fc42d0736bac7b248a79 [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 Lisionf46768f2016-04-18 09:56:26 -040083- (IBAction)removeAccount:(id)sender {
84 AccountModel::instance().remove(AccountModel::instance().selectedAccount());
85 AccountModel::instance().save();
86}
87
Alexandre Lision7f3164c2015-06-12 11:45:37 -040088- (void)loadAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040089{
Alexandre Lisionc1f96662016-03-23 17:24:19 -040090 auto account = AccountModel::instance().selectedAccount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040091
Alexandre Lisionc1f96662016-03-23 17:24:19 -040092 [aliasTextField setStringValue:account->alias().toNSString()];
93 [serverHostTextField setStringValue:account->hostname().toNSString()];
94 [usernameTextField setStringValue:account->username().toNSString()];
95 [passwordTextField setStringValue:account->password().toNSString()];
96 [clearTextField setStringValue:account->password().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040097
Alexandre Lisionc1f96662016-03-23 17:24:19 -040098 [upnpButton setState:AccountModel::instance().selectedAccount()->isUpnpEnabled()];
99 [userAgentButton setState:AccountModel::instance().selectedAccount()->hasCustomUserAgent()];
100 [userAgentTextField setEnabled:AccountModel::instance().selectedAccount()->hasCustomUserAgent()];
101 [autoAnswerButton setState:AccountModel::instance().selectedAccount()->isAutoAnswer()];
102 [userAgentTextField setStringValue:account->userAgent().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400103}
104
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400105- (IBAction)tryRegistration:(id)sender {
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400106 AccountModel::instance().selectedAccount() << Account::EditAction::SAVE;
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400107}
108
109- (IBAction)showPassword:(NSButton *)sender {
110 if (sender.state == NSOnState) {
111 clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame];
112 [clearTextField setTag:passwordTextField.tag];
113 [clearTextField setDelegate:self];
114 [clearTextField setBounds:passwordTextField.bounds];
115 [clearTextField setStringValue:passwordTextField.stringValue];
116 [clearTextField becomeFirstResponder];
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400117 [self.view addSubview:clearTextField];
Alexandre Lision4de68ce2015-04-24 18:22:49 -0400118 [passwordTextField setHidden:YES];
119 } else {
120 [passwordTextField setStringValue:clearTextField.stringValue];
121 [passwordTextField setHidden:NO];
122 [clearTextField removeFromSuperview];
123 clearTextField = nil;
124 }
125}
126
127/**
128 * Debug purpose
129 */
130-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
131{
132 NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
133}
134
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400135#pragma mark - NSTextFieldDelegate methods
136
137- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
138{
139 return YES;
140}
141
142-(void)controlTextDidChange:(NSNotification *)notif
143{
144 NSTextField *textField = [notif object];
145
146 switch ([textField tag]) {
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400147 case TagViews::ALIAS:
148 AccountModel::instance().selectedAccount()->setAlias([[textField stringValue] UTF8String]);
149 AccountModel::instance().selectedAccount()->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400150 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400151 case TagViews::HOSTNAME:
152 AccountModel::instance().selectedAccount()->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400153 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400154 case TagViews::USERNAME:
155 AccountModel::instance().selectedAccount()->setUsername([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400156 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400157 case TagViews::PASSWORD:
158 AccountModel::instance().selectedAccount()->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400159 break;
Alexandre Lisionc1f96662016-03-23 17:24:19 -0400160 case TagViews::USERAGENT:
161 AccountModel::instance().selectedAccount()->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400162 break;
163 default:
164 break;
165 }
166}
167@end