blob: f262f4ee2657d45b83eb2686f85bd13b043eb0da [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;
52
53@end
54
55@implementation AccRingVC
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040056@synthesize typeLabel;
57@synthesize bootstrapField;
58@synthesize hashField;
59@synthesize aliasTextField;
60@synthesize upnpButton;
61@synthesize autoAnswerButton;
62@synthesize userAgentButton;
63@synthesize userAgentTextField;
64
65- (void)awakeFromNib
66{
67 NSLog(@"INIT Ring VC");
68 [aliasTextField setTag:ALIAS_TAG];
69 [userAgentTextField setTag:USERAGENT_TAG];
70 [bootstrapField setTag:HOSTNAME_TAG];
Alexandre Lision7f3164c2015-06-12 11:45:37 -040071
72 QObject::connect(AccountModel::instance()->selectionModel(),
73 &QItemSelectionModel::currentChanged,
74 [=](const QModelIndex &current, const QModelIndex &previous) {
75 if(!current.isValid())
76 return;
77 [self loadAccount];
78 });
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040079}
80
Alexandre Lision7f3164c2015-06-12 11:45:37 -040081- (Account*) currentAccount
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040082{
Alexandre Lision7f3164c2015-06-12 11:45:37 -040083 auto accIdx = AccountModel::instance()->selectionModel()->currentIndex();
84 return AccountModel::instance()->getAccountByModelIndex(accIdx);
85}
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040086
Alexandre Lision7f3164c2015-06-12 11:45:37 -040087- (void)loadAccount
88{
89 auto account = [self currentAccount];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -040090
91 [self.aliasTextField setStringValue:account->alias().toNSString()];
92
93 switch (account->protocol()) {
94 case Account::Protocol::SIP:
95 [typeLabel setStringValue:@"SIP"];
96 break;
97 case Account::Protocol::IAX:
98 [typeLabel setStringValue:@"IAX"];
99 break;
100 case Account::Protocol::RING:
101 [typeLabel setStringValue:@"RING"];
102 break;
103
104 default:
105 break;
106 }
107
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400108 [upnpButton setState:[self currentAccount]->isUpnpEnabled()];
109 [userAgentButton setState:[self currentAccount]->hasCustomUserAgent()];
110 [userAgentTextField setEnabled:[self currentAccount]->hasCustomUserAgent()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400111
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400112 [autoAnswerButton setState:[self currentAccount]->isAutoAnswer()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400113 [userAgentTextField setStringValue:account->userAgent().toNSString()];
114
115 [bootstrapField setStringValue:account->hostname().toNSString()];
116
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400117 if([[self currentAccount]->username().toNSString() isEqualToString:@""])
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400118 [hashField setStringValue:@"Reopen account to see your hash"];
119 else
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400120 [hashField setStringValue:[self currentAccount]->username().toNSString()];
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400121
122}
123
124- (IBAction)toggleUpnp:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400125 [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400126}
127
128- (IBAction)toggleAutoAnswer:(NSButton *)sender {
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400129 [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400130}
131
132- (IBAction)toggleCustomAgent:(NSButton *)sender {
133 [self.userAgentTextField setEnabled:[sender state] == NSOnState];
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400134 [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400135}
136
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 Lision7f3164c2015-06-12 11:45:37 -0400150 [self currentAccount]->setAlias([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400151 break;
152 case HOSTNAME_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400153 [self currentAccount]->setHostname([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400154 break;
155 case PASSWORD_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400156 [self currentAccount]->setPassword([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400157 break;
158 case USERAGENT_TAG:
Alexandre Lision7f3164c2015-06-12 11:45:37 -0400159 [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]);
Alexandre Lisionf5fc4792015-03-17 09:15:43 -0400160 break;
161 default:
162 break;
163 }
164}
165
166@end