blob: 448a500bf4e671bb527b5a65ae4c0b6201244b9c [file] [log] [blame]
Alexandre Lisionf5fc4792015-03-17 09:15:43 -04001/*
2 * Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
3 * 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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
30#define ALIAS_TAG 0
31#define HOSTNAME_TAG 1
32#define USERNAME_TAG 2
33#define PASSWORD_TAG 3
34#define USERAGENT_TAG 4
35
36#import "AccRingVC.h"
37
Alexandre Lision7f3164c2015-06-12 11:45:37 -040038#import <accountmodel.h>
39#import <qitemselectionmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040040
Alexandre Lision7f3164c2015-06-12 11:45:37 -040041@interface AccRingVC ()
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040042
43@property (assign) IBOutlet NSTextField *aliasTextField;
44@property (assign) IBOutlet NSTextField *typeLabel;
45@property (assign) IBOutlet NSTextField *bootstrapField;
46@property (assign) IBOutlet NSTextField *hashField;
47
48@property (assign) IBOutlet NSButton *upnpButton;
49@property (assign) IBOutlet NSButton *autoAnswerButton;
50@property (assign) IBOutlet NSButton *userAgentButton;
51@property (assign) IBOutlet NSTextField *userAgentTextField;
Alexandre Lision28664352015-07-29 14:47:42 -040052@property (unsafe_unretained) IBOutlet NSButton *allowUnknown;
53@property (unsafe_unretained) IBOutlet NSButton *allowHistory;
54@property (unsafe_unretained) IBOutlet NSButton *allowContacts;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040055
56@end
57
58@implementation AccRingVC
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040059@synthesize typeLabel;
60@synthesize bootstrapField;
61@synthesize hashField;
62@synthesize aliasTextField;
63@synthesize upnpButton;
64@synthesize autoAnswerButton;
65@synthesize userAgentButton;
66@synthesize userAgentTextField;
Alexandre Lision28664352015-07-29 14:47:42 -040067@synthesize allowContacts, allowHistory, allowUnknown;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040068
69- (void)awakeFromNib
70{
71 NSLog(@"INIT Ring VC");
72 [aliasTextField setTag:ALIAS_TAG];
73 [userAgentTextField setTag:USERAGENT_TAG];
74 [bootstrapField setTag:HOSTNAME_TAG];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040075
76 QObject::connect(AccountModel::instance()->selectionModel(),
77 &QItemSelectionModel::currentChanged,
78 [=](const QModelIndex &current, const QModelIndex &previous) {
79 if(!current.isValid())
80 return;
81 [self loadAccount];
82 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040083}
84
Alexandre Lision7f3164c2015-06-12 11:45:37 -040085- (Account*) currentAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040086{
Alexandre Lision7f3164c2015-06-12 11:45:37 -040087 auto accIdx = AccountModel::instance()->selectionModel()->currentIndex();
88 return AccountModel::instance()->getAccountByModelIndex(accIdx);
89}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040090
Alexandre Lision7f3164c2015-06-12 11:45:37 -040091- (void)loadAccount
92{
93 auto account = [self currentAccount];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040094
95 [self.aliasTextField setStringValue:account->alias().toNSString()];
96
Alexandre Lision28664352015-07-29 14:47:42 -040097 [typeLabel setStringValue:@"RING"];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040098
Alexandre Lision28664352015-07-29 14:47:42 -040099 [allowUnknown setState:account->allowIncomingFromUnknown()];
100 [allowHistory setState:account->allowIncomingFromHistory()];
101 [allowContacts setState:account->allowIncomingFromContact()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400102
Alexandre Lision28664352015-07-29 14:47:42 -0400103 [allowHistory setEnabled:!account->allowIncomingFromUnknown()];
104 [allowContacts setEnabled:!account->allowIncomingFromUnknown()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400105
Alexandre Lision28664352015-07-29 14:47:42 -0400106 [upnpButton setState:account->isUpnpEnabled()];
107 [userAgentButton setState:account->hasCustomUserAgent()];
108 [userAgentTextField setEnabled:account->hasCustomUserAgent()];
109
110 [autoAnswerButton setState:account->isAutoAnswer()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400111 [userAgentTextField setStringValue:account->userAgent().toNSString()];
112
113 [bootstrapField setStringValue:account->hostname().toNSString()];
114
Alexandre Lision28664352015-07-29 14:47:42 -0400115 if([account->username().toNSString() isEqualToString:@""])
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400116 [hashField setStringValue:@"Reopen account to see your hash"];
117 else
Alexandre Lision28664352015-07-29 14:47:42 -0400118 [hashField setStringValue:account->username().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400119
120}
121
122- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400123 [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400124}
125
126- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400127 [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400128}
129
130- (IBAction)toggleCustomAgent:(NSButton *)sender {
131 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400132 [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400133}
134
Alexandre Lision28664352015-07-29 14:47:42 -0400135- (IBAction)toggleAllowFromUnknown:(id)sender {
136 [self currentAccount]->setAllowIncomingFromUnknown([sender state] == NSOnState);
137 [allowHistory setEnabled:![sender state] == NSOnState];
138 [allowContacts setEnabled:![sender state] == NSOnState];
139}
140- (IBAction)toggleAllowFromHistory:(id)sender {
141 [self currentAccount]->setAllowIncomingFromHistory([sender state] == NSOnState);
142}
143- (IBAction)toggleAllowFromContacts:(id)sender {
144 [self currentAccount]->setAllowIncomingFromContact([sender state] == NSOnState);
145}
146
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400147#pragma mark - NSTextFieldDelegate methods
148
149- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
150{
151 return YES;
152}
153
154-(void)controlTextDidChange:(NSNotification *)notif
155{
156 NSTextField *textField = [notif object];
157
158 switch ([textField tag]) {
159 case ALIAS_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400160 [self currentAccount]->setAlias([[textField stringValue] UTF8String]);
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400161 [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400162 break;
163 case HOSTNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400164 [self currentAccount]->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400165 break;
166 case PASSWORD_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400167 [self currentAccount]->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400168 break;
169 case USERAGENT_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400170 [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400171 break;
172 default:
173 break;
174 }
175}
176
177@end