blob: d7ed122220a12961f909d4d98740d224fe5b0344 [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#import "AccRingVC.h"
26
Alexandre Lision7f3164c2015-06-12 11:45:37 -040027#import <accountmodel.h>
28#import <qitemselectionmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040029
Alexandre Lision7f3164c2015-06-12 11:45:37 -040030@interface AccRingVC ()
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040031
32@property (assign) IBOutlet NSTextField *aliasTextField;
33@property (assign) IBOutlet NSTextField *typeLabel;
34@property (assign) IBOutlet NSTextField *bootstrapField;
35@property (assign) IBOutlet NSTextField *hashField;
36
37@property (assign) IBOutlet NSButton *upnpButton;
38@property (assign) IBOutlet NSButton *autoAnswerButton;
39@property (assign) IBOutlet NSButton *userAgentButton;
40@property (assign) IBOutlet NSTextField *userAgentTextField;
Alexandre Lision28664352015-07-29 14:47:42 -040041@property (unsafe_unretained) IBOutlet NSButton *allowUnknown;
42@property (unsafe_unretained) IBOutlet NSButton *allowHistory;
43@property (unsafe_unretained) IBOutlet NSButton *allowContacts;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040044
45@end
46
47@implementation AccRingVC
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040048@synthesize typeLabel;
49@synthesize bootstrapField;
50@synthesize hashField;
51@synthesize aliasTextField;
52@synthesize upnpButton;
53@synthesize autoAnswerButton;
54@synthesize userAgentButton;
55@synthesize userAgentTextField;
Alexandre Lision28664352015-07-29 14:47:42 -040056@synthesize allowContacts, allowHistory, allowUnknown;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040057
58- (void)awakeFromNib
59{
60 NSLog(@"INIT Ring VC");
61 [aliasTextField setTag:ALIAS_TAG];
62 [userAgentTextField setTag:USERAGENT_TAG];
63 [bootstrapField setTag:HOSTNAME_TAG];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040064
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040065 QObject::connect(AccountModel::instance().selectionModel(),
Alexandre Lision7f3164c2015-06-12 11:45:37 -040066 &QItemSelectionModel::currentChanged,
67 [=](const QModelIndex &current, const QModelIndex &previous) {
68 if(!current.isValid())
69 return;
70 [self loadAccount];
71 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040072}
73
Alexandre Lision7f3164c2015-06-12 11:45:37 -040074- (Account*) currentAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040075{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040076 auto accIdx = AccountModel::instance().selectionModel()->currentIndex();
77 return AccountModel::instance().getAccountByModelIndex(accIdx);
Alexandre Lision7f3164c2015-06-12 11:45:37 -040078}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040079
Alexandre Lision7f3164c2015-06-12 11:45:37 -040080- (void)loadAccount
81{
82 auto account = [self currentAccount];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040083
84 [self.aliasTextField setStringValue:account->alias().toNSString()];
85
Alexandre Lision28664352015-07-29 14:47:42 -040086 [typeLabel setStringValue:@"RING"];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040087
Alexandre Lision28664352015-07-29 14:47:42 -040088 [allowUnknown setState:account->allowIncomingFromUnknown()];
89 [allowHistory setState:account->allowIncomingFromHistory()];
90 [allowContacts setState:account->allowIncomingFromContact()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040091
Alexandre Lision28664352015-07-29 14:47:42 -040092 [allowHistory setEnabled:!account->allowIncomingFromUnknown()];
93 [allowContacts setEnabled:!account->allowIncomingFromUnknown()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040094
Alexandre Lision28664352015-07-29 14:47:42 -040095 [upnpButton setState:account->isUpnpEnabled()];
96 [userAgentButton setState:account->hasCustomUserAgent()];
97 [userAgentTextField setEnabled:account->hasCustomUserAgent()];
98
99 [autoAnswerButton setState:account->isAutoAnswer()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400100 [userAgentTextField setStringValue:account->userAgent().toNSString()];
101
102 [bootstrapField setStringValue:account->hostname().toNSString()];
103
Alexandre Lision28664352015-07-29 14:47:42 -0400104 if([account->username().toNSString() isEqualToString:@""])
Alexandre Lision922380d2015-09-15 10:25:17 -0400105 [hashField setStringValue:NSLocalizedString(@"Reopen account to see your hash",
106 @"Show advice to user")];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400107 else
Alexandre Lision28664352015-07-29 14:47:42 -0400108 [hashField setStringValue:account->username().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400109
110}
111
112- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400113 [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400114}
115
116- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400117 [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400118}
119
120- (IBAction)toggleCustomAgent:(NSButton *)sender {
121 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400122 [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400123}
124
Alexandre Lision28664352015-07-29 14:47:42 -0400125- (IBAction)toggleAllowFromUnknown:(id)sender {
126 [self currentAccount]->setAllowIncomingFromUnknown([sender state] == NSOnState);
127 [allowHistory setEnabled:![sender state] == NSOnState];
128 [allowContacts setEnabled:![sender state] == NSOnState];
129}
130- (IBAction)toggleAllowFromHistory:(id)sender {
131 [self currentAccount]->setAllowIncomingFromHistory([sender state] == NSOnState);
132}
133- (IBAction)toggleAllowFromContacts:(id)sender {
134 [self currentAccount]->setAllowIncomingFromContact([sender state] == NSOnState);
135}
136
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400137#pragma mark - NSTextFieldDelegate methods
138
139- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
140{
141 return YES;
142}
143
144-(void)controlTextDidChange:(NSNotification *)notif
145{
146 NSTextField *textField = [notif object];
147
148 switch ([textField tag]) {
149 case ALIAS_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400150 [self currentAccount]->setAlias([[textField stringValue] UTF8String]);
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400151 [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400152 break;
153 case HOSTNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400154 [self currentAccount]->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400155 break;
156 case PASSWORD_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400157 [self currentAccount]->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400158 break;
159 case USERAGENT_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400160 [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400161 break;
162 default:
163 break;
164 }
165}
166
167@end