blob: 8083a68e166c25412486613e51032fda6a2c7d54 [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"
Anthony Léonard1f70f722017-10-02 10:53:32 -040025#import "PasswordChangeWC.h"
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040026
Alexandre Lision34079c22016-10-31 16:14:02 -040027@interface AccRingVC () <RegisterNameDelegate>
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040028
Alexandre Lision34079c22016-10-31 16:14:02 -040029@property (unsafe_unretained) IBOutlet NSTextField *aliasTextField;
30@property (unsafe_unretained) IBOutlet NSTextField *bootstrapField;
31@property (unsafe_unretained) IBOutlet NSTextField *blockchainField;
32@property (unsafe_unretained) IBOutlet NSTextField *ringIDField;
33@property (unsafe_unretained) IBOutlet NSButton *registerBlockchainNameButton;
34@property (unsafe_unretained) IBOutlet NSTextField *registeredNameField;
35
36@property (unsafe_unretained) IBOutlet NSButton *upnpButton;
37@property (unsafe_unretained) IBOutlet NSButton *autoAnswerButton;
38@property (unsafe_unretained) IBOutlet NSButton *userAgentButton;
39@property (unsafe_unretained) IBOutlet NSTextField *userAgentTextField;
Alexandre Lision28664352015-07-29 14:47:42 -040040@property (unsafe_unretained) IBOutlet NSButton *allowUnknown;
41@property (unsafe_unretained) IBOutlet NSButton *allowHistory;
42@property (unsafe_unretained) IBOutlet NSButton *allowContacts;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040043
Alexandre Lision34079c22016-10-31 16:14:02 -040044@property AbstractLoadingWC* accountModal;
Anthony Léonard1f70f722017-10-02 10:53:32 -040045@property PasswordChangeWC* passwordModal;
Alexandre Lision34079c22016-10-31 16:14:02 -040046
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040047@end
48
49@implementation AccRingVC
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040050@synthesize bootstrapField;
Alexandre Lision34079c22016-10-31 16:14:02 -040051@synthesize ringIDField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040052@synthesize aliasTextField;
Alexandre Lision34079c22016-10-31 16:14:02 -040053@synthesize blockchainField;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040054@synthesize upnpButton;
55@synthesize autoAnswerButton;
56@synthesize userAgentButton;
57@synthesize userAgentTextField;
Alexandre Lision28664352015-07-29 14:47:42 -040058@synthesize allowContacts, allowHistory, allowUnknown;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040059
Alexandre Lisionf46768f2016-04-18 09:56:26 -040060typedef NS_ENUM(NSInteger, TagViews) {
61 ALIAS = 0,
62 HOSTNAME,
63 USERAGENT,
Alexandre Lision34079c22016-10-31 16:14:02 -040064 BLOCKCHAIN,
Alexandre Lisionf46768f2016-04-18 09:56:26 -040065};
66
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040067- (void)awakeFromNib
68{
69 NSLog(@"INIT Ring VC");
Alexandre Lisionf46768f2016-04-18 09:56:26 -040070 [aliasTextField setTag:TagViews::ALIAS];
71 [userAgentTextField setTag:TagViews::USERAGENT];
72 [bootstrapField setTag:TagViews::HOSTNAME];
Alexandre Lision34079c22016-10-31 16:14:02 -040073 [blockchainField setTag:TagViews::BLOCKCHAIN];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040074
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040075 QObject::connect(AccountModel::instance().selectionModel(),
Alexandre Lision7f3164c2015-06-12 11:45:37 -040076 &QItemSelectionModel::currentChanged,
77 [=](const QModelIndex &current, const QModelIndex &previous) {
78 if(!current.isValid())
79 return;
80 [self loadAccount];
81 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040082}
83
Alexandre Lision7f3164c2015-06-12 11:45:37 -040084- (void)loadAccount
85{
Alexandre Lisionf46768f2016-04-18 09:56:26 -040086 auto account = AccountModel::instance().selectedAccount();
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040087
88 [self.aliasTextField setStringValue:account->alias().toNSString()];
89
Alexandre Lision28664352015-07-29 14:47:42 -040090 [allowUnknown setState:account->allowIncomingFromUnknown()];
91 [allowHistory setState:account->allowIncomingFromHistory()];
92 [allowContacts setState:account->allowIncomingFromContact()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040093
Alexandre Lision28664352015-07-29 14:47:42 -040094 [allowHistory setEnabled:!account->allowIncomingFromUnknown()];
95 [allowContacts setEnabled:!account->allowIncomingFromUnknown()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040096
Alexandre Lision28664352015-07-29 14:47:42 -040097 [upnpButton setState:account->isUpnpEnabled()];
98 [userAgentButton setState:account->hasCustomUserAgent()];
99 [userAgentTextField setEnabled:account->hasCustomUserAgent()];
100
101 [autoAnswerButton setState:account->isAutoAnswer()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400102 [userAgentTextField setStringValue:account->userAgent().toNSString()];
103
104 [bootstrapField setStringValue:account->hostname().toNSString()];
Alexandre Lision34079c22016-10-31 16:14:02 -0400105 [blockchainField setStringValue:account->nameServiceURL().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400106
Alexandre Lision34079c22016-10-31 16:14:02 -0400107 if([account->username().toNSString() isEqualToString:@""]) {
108 [ringIDField setStringValue:NSLocalizedString(@"Reopen account to see your hash",
Alexandre Lision922380d2015-09-15 10:25:17 -0400109 @"Show advice to user")];
Alexandre Lision34079c22016-10-31 16:14:02 -0400110 } else {
111 [ringIDField setStringValue:account->username().toNSString()];
112 }
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400113
Alexandre Lision34079c22016-10-31 16:14:02 -0400114 [self refreshRegisteredName:account];
115}
116
117- (void) refreshRegisteredName:(Account*) account
118{
119 [self.registerBlockchainNameButton setHidden:!account->registeredName().isEmpty()];
120 [self.registeredNameField setStringValue:account->registeredName().toNSString()];
121}
122
123- (IBAction)startNameRegistration:(id)sender
124{
125 auto registerWC = [[RegisterNameWC alloc] initWithDelegate:self];
126#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
127 [self.view.window beginSheet:registerWC.window completionHandler:nil];
128#else
129 [NSApp beginSheet: registerWC.window
130 modalForWindow: self.view.window
131 modalDelegate: self
132 didEndSelector: nil
133 contextInfo: nil];
134#endif
135 self.accountModal = registerWC;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400136}
137
Anthony Léonard1f70f722017-10-02 10:53:32 -0400138- (IBAction)changePassword:(id)sender
139{
140 auto passwordWC = [[PasswordChangeWC alloc] initWithAccount:AccountModel::instance().selectedAccount()];
141#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
142 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
143#else
144 [NSApp beginSheet: passwordWC.window
145 modalForWindow: self.view.window
146 modalDelegate: self
147 didEndSelector: nil
148 contextInfo: nil];
149#endif
150 self.passwordModal = passwordWC;
151}
152
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400153- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400154 AccountModel::instance().selectedAccount()->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400155}
156
157- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400158 AccountModel::instance().selectedAccount()->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400159}
160
161- (IBAction)toggleCustomAgent:(NSButton *)sender {
162 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400163 AccountModel::instance().selectedAccount()->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400164}
165
Alexandre Lision28664352015-07-29 14:47:42 -0400166- (IBAction)toggleAllowFromUnknown:(id)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400167 AccountModel::instance().selectedAccount()->setAllowIncomingFromUnknown([sender state] == NSOnState);
Alexandre Lision28664352015-07-29 14:47:42 -0400168 [allowHistory setEnabled:![sender state] == NSOnState];
169 [allowContacts setEnabled:![sender state] == NSOnState];
170}
171- (IBAction)toggleAllowFromHistory:(id)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400172 AccountModel::instance().selectedAccount()->setAllowIncomingFromHistory([sender state] == NSOnState);
Alexandre Lision28664352015-07-29 14:47:42 -0400173}
174- (IBAction)toggleAllowFromContacts:(id)sender {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400175 AccountModel::instance().selectedAccount()->setAllowIncomingFromContact([sender state] == NSOnState);
Alexandre Lision28664352015-07-29 14:47:42 -0400176}
177
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400178#pragma mark - NSTextFieldDelegate methods
179
180- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
181{
182 return YES;
183}
184
185-(void)controlTextDidChange:(NSNotification *)notif
186{
187 NSTextField *textField = [notif object];
188
189 switch ([textField tag]) {
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400190 case TagViews::ALIAS:
191 AccountModel::instance().selectedAccount()->setAlias([[textField stringValue] UTF8String]);
192 AccountModel::instance().selectedAccount()->setDisplayName([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400193 break;
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400194 case TagViews::HOSTNAME:
195 AccountModel::instance().selectedAccount()->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400196 break;
Alexandre Lisionf46768f2016-04-18 09:56:26 -0400197 case TagViews::USERAGENT:
198 AccountModel::instance().selectedAccount()->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400199 break;
Alexandre Lision34079c22016-10-31 16:14:02 -0400200 case TagViews::BLOCKCHAIN:
201 AccountModel::instance().selectedAccount()->setNameServiceURL([[textField stringValue] UTF8String]);
202 break;
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400203 default:
204 break;
205 }
206}
207
Alexandre Lision34079c22016-10-31 16:14:02 -0400208- (void) didRegisterNameWithSuccess
209{
210 [self.accountModal close];
211 [self refreshRegisteredName:AccountModel::instance().selectedAccount()];
212}
213
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400214@end