blob: 1705e5725e7819e7331c56bbf6a04992a659b50f [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 "AccRingVC.h"
20
Alexandre Lision7f3164c2015-06-12 11:45:37 -040021#import <accountmodel.h>
22#import <qitemselectionmodel.h>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040023
Alexandre Lision34079c22016-10-31 16:14:02 -040024#import "RegisterNameWC.h"
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040025
Alexandre Lision34079c22016-10-31 16:14:02 -040026@interface AccRingVC () <RegisterNameDelegate>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040027
Alexandre Lision34079c22016-10-31 16:14:02 -040028@property (unsafe_unretained) IBOutlet NSTextField *aliasTextField;
29@property (unsafe_unretained) IBOutlet NSTextField *bootstrapField;
30@property (unsafe_unretained) IBOutlet NSTextField *blockchainField;
31@property (unsafe_unretained) IBOutlet NSTextField *ringIDField;
32@property (unsafe_unretained) IBOutlet NSButton *registerBlockchainNameButton;
33@property (unsafe_unretained) IBOutlet NSTextField *registeredNameField;
34
35@property (unsafe_unretained) IBOutlet NSButton *upnpButton;
36@property (unsafe_unretained) IBOutlet NSButton *autoAnswerButton;
37@property (unsafe_unretained) IBOutlet NSButton *userAgentButton;
38@property (unsafe_unretained) IBOutlet NSTextField *userAgentTextField;
Alexandre Lision28664352015-07-29 14:47:42 -040039@property (unsafe_unretained) IBOutlet NSButton *allowUnknown;
40@property (unsafe_unretained) IBOutlet NSButton *allowHistory;
41@property (unsafe_unretained) IBOutlet NSButton *allowContacts;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040042
Alexandre Lision34079c22016-10-31 16:14:02 -040043@property AbstractLoadingWC* accountModal;
44
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040045@end
46
47@implementation AccRingVC
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040048@synthesize bootstrapField;
Alexandre Lision34079c22016-10-31 16:14:02 -040049@synthesize ringIDField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040050@synthesize aliasTextField;
Alexandre Lision34079c22016-10-31 16:14:02 -040051@synthesize blockchainField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040052@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
Alexandre Lisionf46768f2016-04-18 09:56:26 -040058typedef NS_ENUM(NSInteger, TagViews) {
59 ALIAS = 0,
60 HOSTNAME,
61 USERAGENT,
Alexandre Lision34079c22016-10-31 16:14:02 -040062 BLOCKCHAIN,
Alexandre Lisionf46768f2016-04-18 09:56:26 -040063};
64
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040065- (void)awakeFromNib
66{
67 NSLog(@"INIT Ring VC");
Alexandre Lisionf46768f2016-04-18 09:56:26 -040068 [aliasTextField setTag:TagViews::ALIAS];
69 [userAgentTextField setTag:TagViews::USERAGENT];
70 [bootstrapField setTag:TagViews::HOSTNAME];
Alexandre Lision34079c22016-10-31 16:14:02 -040071 [blockchainField setTag:TagViews::BLOCKCHAIN];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040072
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040073 QObject::connect(AccountModel::instance().selectionModel(),
Alexandre Lision7f3164c2015-06-12 11:45:37 -040074 &QItemSelectionModel::currentChanged,
75 [=](const QModelIndex &current, const QModelIndex &previous) {
76 if(!current.isValid())
77 return;
78 [self loadAccount];
79 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040080}
81
Alexandre Lision7f3164c2015-06-12 11:45:37 -040082- (void)loadAccount
83{
Alexandre Lisionf46768f2016-04-18 09:56:26 -040084 auto account = AccountModel::instance().selectedAccount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040085
86 [self.aliasTextField setStringValue:account->alias().toNSString()];
87
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()];
Alexandre Lision34079c22016-10-31 16:14:02 -0400103 [blockchainField setStringValue:account->nameServiceURL().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400104
Alexandre Lision34079c22016-10-31 16:14:02 -0400105 if([account->username().toNSString() isEqualToString:@""]) {
106 [ringIDField setStringValue:NSLocalizedString(@"Reopen account to see your hash",
Alexandre Lision922380d2015-09-15 10:25:17 -0400107 @"Show advice to user")];
Alexandre Lision34079c22016-10-31 16:14:02 -0400108 } else {
109 [ringIDField setStringValue:account->username().toNSString()];
110 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400111
Alexandre Lision34079c22016-10-31 16:14:02 -0400112 [self refreshRegisteredName:account];
113}
114
115- (void) refreshRegisteredName:(Account*) account
116{
117 [self.registerBlockchainNameButton setHidden:!account->registeredName().isEmpty()];
118 [self.registeredNameField setStringValue:account->registeredName().toNSString()];
119}
120
121- (IBAction)startNameRegistration:(id)sender
122{
123 auto registerWC = [[RegisterNameWC alloc] initWithDelegate:self];
124#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
125 [self.view.window beginSheet:registerWC.window completionHandler:nil];
126#else
127 [NSApp beginSheet: registerWC.window
128 modalForWindow: self.view.window
129 modalDelegate: self
130 didEndSelector: nil
131 contextInfo: nil];
132#endif
133 self.accountModal = registerWC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400134}
135
136- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400137 AccountModel::instance().selectedAccount()->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400138}
139
140- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400141 AccountModel::instance().selectedAccount()->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400142}
143
144- (IBAction)toggleCustomAgent:(NSButton *)sender {
145 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400146 AccountModel::instance().selectedAccount()->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400147}
148
Alexandre Lision28664352015-07-29 14:47:42 -0400149- (IBAction)toggleAllowFromUnknown:(id)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400150 AccountModel::instance().selectedAccount()->setAllowIncomingFromUnknown([sender state] == NSOnState);
Alexandre Lision28664352015-07-29 14:47:42 -0400151 [allowHistory setEnabled:![sender state] == NSOnState];
152 [allowContacts setEnabled:![sender state] == NSOnState];
153}
154- (IBAction)toggleAllowFromHistory:(id)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400155 AccountModel::instance().selectedAccount()->setAllowIncomingFromHistory([sender state] == NSOnState);
Alexandre Lision28664352015-07-29 14:47:42 -0400156}
157- (IBAction)toggleAllowFromContacts:(id)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400158 AccountModel::instance().selectedAccount()->setAllowIncomingFromContact([sender state] == NSOnState);
Alexandre Lision28664352015-07-29 14:47:42 -0400159}
160
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400161#pragma mark - NSTextFieldDelegate methods
162
163- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
164{
165 return YES;
166}
167
168-(void)controlTextDidChange:(NSNotification *)notif
169{
170 NSTextField *textField = [notif object];
171
172 switch ([textField tag]) {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400173 case TagViews::ALIAS:
174 AccountModel::instance().selectedAccount()->setAlias([[textField stringValue] UTF8String]);
175 AccountModel::instance().selectedAccount()->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400176 break;
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400177 case TagViews::HOSTNAME:
178 AccountModel::instance().selectedAccount()->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400179 break;
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400180 case TagViews::USERAGENT:
181 AccountModel::instance().selectedAccount()->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400182 break;
Alexandre Lision34079c22016-10-31 16:14:02 -0400183 case TagViews::BLOCKCHAIN:
184 AccountModel::instance().selectedAccount()->setNameServiceURL([[textField stringValue] UTF8String]);
185 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400186 default:
187 break;
188 }
189}
190
Alexandre Lision34079c22016-10-31 16:14:02 -0400191- (void) didRegisterNameWithSuccess
192{
193 [self.accountModal close];
194 [self refreshRegisteredName:AccountModel::instance().selectedAccount()];
195}
196
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400197@end