Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 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. |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 18 | */ |
| 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 Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 27 | #import <accountmodel.h> |
| 28 | #import <qitemselectionmodel.h> |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 29 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 30 | @interface AccRingVC () |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 31 | |
| 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 Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 41 | @property (unsafe_unretained) IBOutlet NSButton *allowUnknown; |
| 42 | @property (unsafe_unretained) IBOutlet NSButton *allowHistory; |
| 43 | @property (unsafe_unretained) IBOutlet NSButton *allowContacts; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 44 | |
| 45 | @end |
| 46 | |
| 47 | @implementation AccRingVC |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 48 | @synthesize typeLabel; |
| 49 | @synthesize bootstrapField; |
| 50 | @synthesize hashField; |
| 51 | @synthesize aliasTextField; |
| 52 | @synthesize upnpButton; |
| 53 | @synthesize autoAnswerButton; |
| 54 | @synthesize userAgentButton; |
| 55 | @synthesize userAgentTextField; |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 56 | @synthesize allowContacts, allowHistory, allowUnknown; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 57 | |
| 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 Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 64 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 65 | QObject::connect(AccountModel::instance().selectionModel(), |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 66 | &QItemSelectionModel::currentChanged, |
| 67 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 68 | if(!current.isValid()) |
| 69 | return; |
| 70 | [self loadAccount]; |
| 71 | }); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 74 | - (Account*) currentAccount |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 75 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 76 | auto accIdx = AccountModel::instance().selectionModel()->currentIndex(); |
| 77 | return AccountModel::instance().getAccountByModelIndex(accIdx); |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 78 | } |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 79 | |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 80 | - (void)loadAccount |
| 81 | { |
| 82 | auto account = [self currentAccount]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 83 | |
| 84 | [self.aliasTextField setStringValue:account->alias().toNSString()]; |
| 85 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 86 | [typeLabel setStringValue:@"RING"]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 87 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 88 | [allowUnknown setState:account->allowIncomingFromUnknown()]; |
| 89 | [allowHistory setState:account->allowIncomingFromHistory()]; |
| 90 | [allowContacts setState:account->allowIncomingFromContact()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 91 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 92 | [allowHistory setEnabled:!account->allowIncomingFromUnknown()]; |
| 93 | [allowContacts setEnabled:!account->allowIncomingFromUnknown()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 94 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 95 | [upnpButton setState:account->isUpnpEnabled()]; |
| 96 | [userAgentButton setState:account->hasCustomUserAgent()]; |
| 97 | [userAgentTextField setEnabled:account->hasCustomUserAgent()]; |
| 98 | |
| 99 | [autoAnswerButton setState:account->isAutoAnswer()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 100 | [userAgentTextField setStringValue:account->userAgent().toNSString()]; |
| 101 | |
| 102 | [bootstrapField setStringValue:account->hostname().toNSString()]; |
| 103 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 104 | if([account->username().toNSString() isEqualToString:@""]) |
Alexandre Lision | 922380d | 2015-09-15 10:25:17 -0400 | [diff] [blame] | 105 | [hashField setStringValue:NSLocalizedString(@"Reopen account to see your hash", |
| 106 | @"Show advice to user")]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 107 | else |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 108 | [hashField setStringValue:account->username().toNSString()]; |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 109 | |
| 110 | } |
| 111 | |
| 112 | - (IBAction)toggleUpnp:(NSButton *)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 113 | [self currentAccount]->setUpnpEnabled([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | - (IBAction)toggleAutoAnswer:(NSButton *)sender { |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 117 | [self currentAccount]->setAutoAnswer([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | - (IBAction)toggleCustomAgent:(NSButton *)sender { |
| 121 | [self.userAgentTextField setEnabled:[sender state] == NSOnState]; |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 122 | [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Alexandre Lision | 2866435 | 2015-07-29 14:47:42 -0400 | [diff] [blame] | 125 | - (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 Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 137 | #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 Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 150 | [self currentAccount]->setAlias([[textField stringValue] UTF8String]); |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 151 | [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 152 | break; |
| 153 | case HOSTNAME_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 154 | [self currentAccount]->setHostname([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 155 | break; |
| 156 | case PASSWORD_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 157 | [self currentAccount]->setPassword([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 158 | break; |
| 159 | case USERAGENT_TAG: |
Alexandre Lision | 7f3164c | 2015-06-12 11:45:37 -0400 | [diff] [blame] | 160 | [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]); |
Alexandre Lision | f5fc479 | 2015-03-17 09:15:43 -0400 | [diff] [blame] | 161 | break; |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | @end |