AccAdvancedVC.mm 11.5 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
#define REGISTRATION_TAG 0
#define LOCALPORT_TAG 1
#define STUNURL_TAG 2
#define PUBLICADDR_TAG 3
#define PUBLICPORT_TAG 4
#define MINAUDIO_TAG 5
#define MAXAUDIO_TAG 6
#define MINVIDEO_TAG 7
#define MAXVIDEO_TAG 8

29 30 31 32 33
#define TURN_SERVER_TAG     9
#define TURN_USERNAME_TAG   10
#define TURN_PASSWORD_TAG   11
#define TURN_REALM_TAG      12

34 35
#import "AccAdvancedVC.h"

36
///Qt
37 38
#import <qitemselectionmodel.h>

39 40 41 42 43 44
///LRC
#import <accountmodel.h>
#import <credentialmodel.h>
#import <credential.h>


45
@interface AccAdvancedVC ()
46 47
@property (unsafe_unretained) IBOutlet NSView *registrationContainer;
@property (unsafe_unretained) IBOutlet NSView *mainContainer;
48

49 50 51
@property (unsafe_unretained) IBOutlet NSTextField *registrationField;
@property (unsafe_unretained) IBOutlet NSTextField *localPortField;
@property (unsafe_unretained) IBOutlet NSButton *isUsingSTUN;
52

53 54 55 56 57
@property (unsafe_unretained) IBOutlet NSTextField *STUNserverURLField;
@property (unsafe_unretained) IBOutlet NSTextField *minAudioRTPRange;
@property (unsafe_unretained) IBOutlet NSTextField *maxAudioRTPRange;
@property (unsafe_unretained) IBOutlet NSTextField *minVideoRTPRange;
@property (unsafe_unretained) IBOutlet NSTextField *maxVideoRTPRange;
58

59 60 61 62 63
@property (unsafe_unretained) IBOutlet NSButton *isUsingTURN;
@property (unsafe_unretained) IBOutlet NSTextField *turnServerURL;
@property (unsafe_unretained) IBOutlet NSTextField *turnUsername;
@property (unsafe_unretained) IBOutlet NSSecureTextField *turnPassword;
@property (unsafe_unretained) IBOutlet NSTextField *turnRealm;
64

65 66 67 68 69 70
@property (unsafe_unretained) IBOutlet NSStepper *registrationStepper;
@property (unsafe_unretained) IBOutlet NSStepper *localPortStepper;
@property (unsafe_unretained) IBOutlet NSStepper *minAudioPortStepper;
@property (unsafe_unretained) IBOutlet NSStepper *maxAudioPortStepper;
@property (unsafe_unretained) IBOutlet NSStepper *minVideoPortStepper;
@property (unsafe_unretained) IBOutlet NSStepper *maxVideoPortStepper;
71

72 73 74
@property (unsafe_unretained) IBOutlet NSMatrix *publishAddrAndPortRadioGroup;
@property (unsafe_unretained) IBOutlet NSTextField *publishedAddrField;
@property (unsafe_unretained) IBOutlet NSTextField *publishedPortField;
75 76 77 78 79 80 81 82 83 84 85 86 87 88

@end

@implementation AccAdvancedVC
@synthesize registrationField;
@synthesize localPortField;
@synthesize isUsingSTUN;
@synthesize STUNserverURLField;
@synthesize minAudioRTPRange;
@synthesize maxAudioRTPRange;
@synthesize minVideoRTPRange;
@synthesize maxVideoRTPRange;
@synthesize registrationStepper;
@synthesize localPortStepper;
89
@synthesize turnPassword, isUsingTURN, turnRealm, turnServerURL, turnUsername;
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
@synthesize minAudioPortStepper;
@synthesize maxAudioPortStepper;
@synthesize minVideoPortStepper;
@synthesize maxVideoPortStepper;
@synthesize publishAddrAndPortRadioGroup;
@synthesize publishedAddrField;
@synthesize publishedPortField;

- (void)awakeFromNib
{
    NSLog(@"INIT Advanced VC");
    [registrationStepper setTag:REGISTRATION_TAG];
    [localPortStepper setTag:LOCALPORT_TAG];
    [minAudioPortStepper setTag:MINAUDIO_TAG];
    [maxAudioPortStepper setTag:MAXAUDIO_TAG];
    [minVideoPortStepper setTag:MINVIDEO_TAG];
    [maxVideoPortStepper setTag:MAXVIDEO_TAG];

108 109 110 111 112
    [turnServerURL setTag:TURN_SERVER_TAG];
    [turnUsername setTag:TURN_USERNAME_TAG];
    [turnPassword setTag:TURN_PASSWORD_TAG];
    [turnRealm setTag:TURN_REALM_TAG];

113 114 115 116 117 118 119 120 121 122
    [registrationField setTag:REGISTRATION_TAG];
    [localPortField setTag:LOCALPORT_TAG];
    [minAudioRTPRange setTag:MINAUDIO_TAG];
    [maxAudioRTPRange setTag:MAXAUDIO_TAG];
    [minVideoRTPRange setTag:MINVIDEO_TAG];
    [maxVideoRTPRange setTag:MAXVIDEO_TAG];

    [STUNserverURLField setTag:STUNURL_TAG];
    [publishedPortField setTag:PUBLICPORT_TAG];
    [publishedAddrField setTag:PUBLICADDR_TAG];
123

124
    QObject::connect(AccountModel::instance().selectionModel(),
125 126 127 128 129 130 131
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid())
                             return;
                         [self loadAccount];
                     });

132 133
}

134
- (Account*) currentAccount
135
{
136 137
    auto accIdx = AccountModel::instance().selectionModel()->currentIndex();
    return AccountModel::instance().getAccountByModelIndex(accIdx);
138 139 140 141 142 143
}

- (void)loadAccount
{
    auto account = [self currentAccount];

144 145 146 147 148 149 150
    [self updateControlsWithTag:REGISTRATION_TAG];
    [self updateControlsWithTag:LOCALPORT_TAG];
    [self updateControlsWithTag:MINAUDIO_TAG];
    [self updateControlsWithTag:MAXAUDIO_TAG];
    [self updateControlsWithTag:MINVIDEO_TAG];
    [self updateControlsWithTag:MAXVIDEO_TAG];

151 152 153
    [STUNserverURLField setStringValue:account->sipStunServer().toNSString()];
    [isUsingSTUN setState:account->isSipStunEnabled()?NSOnState:NSOffState];
    [STUNserverURLField setEnabled:account->isSipStunEnabled()];
154

155 156 157
    [isUsingTURN setState:account->isTurnEnabled()?NSOnState:NSOffState];
    [self toggleTURN:isUsingTURN];
    [turnServerURL setStringValue:account->turnServer().toNSString()];
158 159 160 161 162 163

    auto turnCreds = account->credentialModel()->primaryCredential(Credential::Type::TURN);

    [turnUsername setStringValue:turnCreds->username().toNSString()];
    [turnPassword setStringValue:turnCreds->password().toNSString()];
    [turnRealm setStringValue:turnCreds->realm().toNSString()];
164

165
    if(account->isPublishedSameAsLocal())
166 167 168 169 170
        [publishAddrAndPortRadioGroup selectCellAtRow:0 column:0];
    else {
        [publishAddrAndPortRadioGroup selectCellAtRow:1 column:0];
    }

171 172 173 174
    [publishedAddrField setStringValue:account->publishedAddress().toNSString()];
    [publishedPortField setIntValue:account->publishedPort()];
    [publishedAddrField setEnabled:!account->isPublishedSameAsLocal()];
    [publishedPortField setEnabled:!account->isPublishedSameAsLocal()];
175 176 177 178 179 180

    if(account->protocol() == Account::Protocol::RING) {
        [self.registrationContainer setHidden:YES];
    } else {
        [self.registrationContainer setHidden:NO];
    }
181 182 183 184 185 186 187 188 189 190
}

#pragma mark - NSTextFieldDelegate methods

-(void)controlTextDidChange:(NSNotification *)notif
{
    NSTextField *textField = [notif object];
    NSRange test = [[textField currentEditor] selectedRange];

    [self valueDidChange:textField];
191
    //FIXME: saving account lose focus because in NSTreeController we remove and reinsert row so View selection change
192 193 194 195 196 197 198 199
    [textField.window makeFirstResponder:textField];
    [[textField currentEditor] setSelectedRange:test];
}

- (IBAction) valueDidChange: (id) sender
{
    switch ([sender tag]) {
        case REGISTRATION_TAG:
200
            [self currentAccount]->setRegistrationExpire([sender integerValue]);
201 202
            break;
        case LOCALPORT_TAG:
203
            [self currentAccount]->setLocalPort([sender integerValue]);
204 205
            break;
        case STUNURL_TAG:
206
            [self currentAccount]->setSipStunServer([[sender stringValue] UTF8String]);
207 208
            break;
        case PUBLICADDR_TAG:
209
            [self currentAccount]->setPublishedAddress([[sender stringValue] UTF8String]);
210 211
            break;
        case PUBLICPORT_TAG:
212
            [self currentAccount]->setPublishedPort([sender integerValue]);
213 214
            break;
        case MINAUDIO_TAG:
215
            [self currentAccount]->setAudioPortMin([sender integerValue]);
216 217
            break;
        case MAXAUDIO_TAG:
218
            [self currentAccount]->setAudioPortMax([sender integerValue]);
219 220
            break;
        case MINVIDEO_TAG:
221
            [self currentAccount]->setVideoPortMin([sender integerValue]);
222 223
            break;
        case MAXVIDEO_TAG:
224
            [self currentAccount]->setVideoPortMax([sender integerValue]);
225
            break;
226 227 228 229
        case TURN_SERVER_TAG:
            [self currentAccount]->setTurnServer([[sender stringValue] UTF8String]);
            break;
        case TURN_USERNAME_TAG:
230
            [self currentAccount]->credentialModel()->primaryCredential(Credential::Type::TURN)->setUsername([[sender stringValue] UTF8String]);
231 232
            break;
        case TURN_PASSWORD_TAG:
233
            [self currentAccount]->credentialModel()->primaryCredential(Credential::Type::TURN)->setPassword([[sender stringValue] UTF8String]);
234 235
            break;
        case TURN_REALM_TAG:
236
            [self currentAccount]->credentialModel()->primaryCredential(Credential::Type::TURN)->setRealm([[sender stringValue] UTF8String]);
237
            break;
238 239 240 241 242 243 244 245
        default:
            break;
    }
    [self updateControlsWithTag:[sender tag]];
}

- (IBAction)toggleSTUN:(NSButton *)sender
{
246
    [self currentAccount]->setSipStunEnabled([sender state]);
247
    [STUNserverURLField setEnabled:[self currentAccount]->isSipStunEnabled()];
248 249
}

250 251 252 253 254 255 256 257
- (IBAction)toggleTURN:(id)sender {
    [self currentAccount]->setTurnEnabled([sender state]);
    [turnServerURL setEnabled:[sender state]];
    [turnUsername setEnabled:[sender state]];
    [turnPassword setEnabled:[sender state]];
    [turnRealm setEnabled:[sender state]];
}

258 259 260 261
- (IBAction)didSwitchPublishedAddress:(NSMatrix *)matrix
{
    NSInteger row = [matrix selectedRow];
    if(row == 0) {
262
        [self currentAccount]->setPublishedSameAsLocal(YES);
263
    } else {
264
        [self currentAccount]->setPublishedSameAsLocal(NO);
265
    }
266 267
    [publishedAddrField setEnabled:![self currentAccount]->isPublishedSameAsLocal()];
    [publishedPortField setEnabled:![self currentAccount]->isPublishedSameAsLocal()];
268 269 270 271 272 273 274

}

- (void) updateControlsWithTag:(NSInteger) tag
{
    switch (tag) {
        case REGISTRATION_TAG:
275 276
            [registrationStepper setIntegerValue:[self currentAccount]->registrationExpire()];
            [registrationField setIntegerValue:[self currentAccount]->registrationExpire()];
277 278
            break;
        case LOCALPORT_TAG:
279 280
            [localPortStepper setIntegerValue:[self currentAccount]->localPort()];
            [localPortField setIntegerValue:[self currentAccount]->localPort()];
281 282
            break;
        case MINAUDIO_TAG:
283 284
            [minAudioPortStepper setIntegerValue:[self currentAccount]->audioPortMin()];
            [minAudioRTPRange setIntegerValue:[self currentAccount]->audioPortMin()];
285 286
            break;
        case MAXAUDIO_TAG:
287 288
            [maxAudioPortStepper setIntegerValue:[self currentAccount]->audioPortMax()];
            [maxAudioRTPRange setIntegerValue:[self currentAccount]->audioPortMax()];
289 290
            break;
        case MINVIDEO_TAG:
291 292
            [minVideoPortStepper setIntegerValue:[self currentAccount]->videoPortMin()];
            [minVideoRTPRange setIntegerValue:[self currentAccount]->videoPortMin()];
293 294
            break;
        case MAXVIDEO_TAG:
295 296
            [maxVideoPortStepper setIntegerValue:[self currentAccount]->videoPortMax()];
            [maxVideoRTPRange setIntegerValue:[self currentAccount]->videoPortMax()];
297 298 299 300 301 302 303
            break;
        default:
            break;
    }
}

@end