AccAdvancedVC.mm 20.1 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2018 Savoir-faire Linux Inc.
3
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 *  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.
 */
#import "AccAdvancedVC.h"

22 23 24 25 26 27
//LRC
#import <api/lrc.h>
#import <api/newaccountmodel.h>
#import <api/newdevicemodel.h>
#import <api/newcodecmodel.h>
#import <account.h>
28

29
@interface AccAdvancedVC ()
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

@property (unsafe_unretained) IBOutlet NSButton* ringtoneSelectionButton;
@property (unsafe_unretained) IBOutlet NSButton* enableRingtone;
@property (unsafe_unretained) IBOutlet NSButton* autoAnswer;
@property (unsafe_unretained) IBOutlet NSButton* toggleUPnPButton;
@property (unsafe_unretained) IBOutlet NSButton* useTURNButton;
@property (unsafe_unretained) IBOutlet NSButton* useSTUNButton;
@property (unsafe_unretained) IBOutlet NSButton *toggleVideoButton;
@property (unsafe_unretained) IBOutlet NSTableView* audioCodecView;
@property (unsafe_unretained) IBOutlet NSTableView* videoCodecView;
@property (unsafe_unretained) IBOutlet NSTextField *turnAddressField;
@property (unsafe_unretained) IBOutlet NSTextField *turnUsernameField;
@property (unsafe_unretained) IBOutlet NSSecureTextField *turnPasswordField;
@property (unsafe_unretained) IBOutlet NSTextField *turnRealmField;
@property (unsafe_unretained) IBOutlet NSTextField *stunAddressField;
@property (unsafe_unretained) IBOutlet NSButton *disableVideoButton;
46 47 48 49 50

@end

@implementation AccAdvancedVC

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
@synthesize audioCodecView, videoCodecView;
@synthesize privateKeyPaswordField, turnAddressField, turnUsernameField, turnPasswordField, turnRealmField, stunAddressField;
@synthesize ringtoneSelectionButton, selectCACertificateButton, selectUserCertificateButton, selectPrivateKeyButton;
@synthesize enableRingtone, toggleVideoButton, autoAnswer, toggleUPnPButton, useTURNButton, useSTUNButton, disableVideoButton;
@synthesize accountModel;

NS_ENUM(NSInteger, ButtonsTags) {
    TAG_RINGTONE = 1,
    TAG_CA_CERTIFICATE,
    TAG_USER_CERTIFICATE,
    TAG_PRIVATE_KEY
};

NS_ENUM(NSInteger, TextFieldsViews) {
    PRIVATE_KEY_PASSWORG_TAG = 1,
    TURN_ADDRESS_TAG,
    TURN_USERNAME_TAG,
    TURN_PASSWORD_TAG,
    TURN_REALM_TAG,
    STUN_ADDRESS_TAG
};

NS_ENUM(NSInteger, tablesViews) {
    AUDIO_CODEC_NAME_TAG = 1,
    AUDIO_CODEC_SAMPLERATE_TAG,
    AUDIO_CODEC_ENABLE_TAG,
    VIDEO_CODEC_NAME_TAG,
    VIDIO_CODEC_ENABLE_TAG
};

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
82
{
83 84 85 86 87
    if (self =  [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil])
    {
        self.accountModel= accountModel;
    }
    return self;
88 89
}

90
- (void)awakeFromNib
91
{
92 93 94 95 96 97 98 99
    [super awakeFromNib];
    audioCodecView.delegate = self;
    audioCodecView.dataSource = self;
    videoCodecView.delegate = self;
    videoCodecView.dataSource = self;
    [disableVideoButton setWantsLayer:YES];
    disableVideoButton.layer.backgroundColor = [[NSColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:0.3] CGColor];
    [self setTags];
100 101
}

102 103 104 105 106 107 108 109 110 111 112 113
-(void) setTags {
    [ringtoneSelectionButton setTag: TAG_RINGTONE];
    [selectCACertificateButton setTag: TAG_CA_CERTIFICATE];
    [selectUserCertificateButton setTag: TAG_USER_CERTIFICATE];
    [selectPrivateKeyButton setTag: TAG_PRIVATE_KEY];
    [privateKeyPaswordField setTag: PRIVATE_KEY_PASSWORG_TAG];
    [turnAddressField setTag: TURN_ADDRESS_TAG];
    [turnUsernameField setTag: TURN_USERNAME_TAG];
    [turnPasswordField setTag: TURN_PASSWORD_TAG];
    [turnRealmField setTag: TURN_REALM_TAG];
    [stunAddressField setTag: STUN_ADDRESS_TAG];
}
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
-(void) update {
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    [ringtoneSelectionButton setTitle: [@(accountProperties.Ringtone.ringtonePath.c_str()) lastPathComponent]];
    [enableRingtone setState :accountProperties.Ringtone.ringtoneEnabled];
    [autoAnswer setState: accountProperties.autoAnswer];
    [selectPrivateKeyButton setTitle: [@(accountProperties.TLS.privateKeyFile.c_str()) lastPathComponent]];
    [selectUserCertificateButton setTitle:[@(accountProperties.TLS.certificateFile.c_str()) lastPathComponent]];
    [selectCACertificateButton setTitle: [@(accountProperties.TLS.certificateListFile.c_str()) lastPathComponent]];
    [toggleUPnPButton setState:accountProperties.upnpEnabled];
    [useTURNButton setState:accountProperties.TURN.enable];
    [useSTUNButton setState:accountProperties.STUN.enable];
    [privateKeyPaswordField setStringValue:@(accountProperties.TLS.password.c_str())];
    [turnAddressField setStringValue:@(accountProperties.TURN.server.c_str())];
    [turnUsernameField setStringValue:@(accountProperties.TURN.username.c_str())];
    [turnPasswordField setStringValue:@(accountProperties.TURN.password.c_str())];
    [turnRealmField setStringValue:@(accountProperties.TURN.realm.c_str())];
    [stunAddressField setStringValue:@(accountProperties.STUN.server.c_str())];
    [stunAddressField setEditable:accountProperties.STUN.enable];
    [turnAddressField setEditable:accountProperties.TURN.enable];
    [turnUsernameField setEditable:accountProperties.TURN.enable];
    [turnPasswordField setEditable:accountProperties.TURN.enable];
    [turnRealmField setEditable:accountProperties.TURN.enable];
    [disableVideoButton setHidden:accountProperties.Video.videoEnabled];
    [toggleVideoButton setState:accountProperties.Video.videoEnabled];
    [videoCodecView setEnabled:accountProperties.Video.videoEnabled];
    [audioCodecView reloadData];
    [videoCodecView reloadData];
}
143

144 145 146 147
- (void)viewDidLoad {
    [super viewDidLoad];
    [self update];
}
148

149
#pragma mark - AccountAdvancedProtocol methods
150

151 152 153 154
- (void) setSelectedAccount:(std::string) account {
    self.selectedAccountID = account;
    [self update];
}
155

156
#pragma mark - Actions
157

158 159 160 161 162
- (IBAction)autoAnswerCall:(id)sender {
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    if( accountProperties.autoAnswer != [sender state]) {
        accountProperties.autoAnswer = [sender state];
        self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
163
    }
164
}
165

166 167 168 169 170 171
- (IBAction)autoEnableRingtone:(id)sender {
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    if(accountProperties.Ringtone.ringtoneEnabled != [sender state]) {
        accountProperties.Ringtone.ringtoneEnabled = [sender state];
        self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
        [ringtoneSelectionButton setEnabled:[sender state]];
172
    }
173 174
}

175 176 177 178 179 180 181
- (IBAction)togleUPnP:(id)sender {
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    if( accountProperties.upnpEnabled != [sender state]) {
        accountProperties.upnpEnabled = [sender state];
        self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
    }
}
182

183 184 185 186 187 188 189 190 191 192 193
- (IBAction)useTURN:(id)sender {
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    if( accountProperties.TURN.enable != [sender state]) {
        accountProperties.TURN.enable = [sender state];
        self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
        [turnAddressField setEditable:[sender state]];
        [turnUsernameField setEditable:[sender state]];
        [turnPasswordField setEditable:[sender state]];
        [turnRealmField setEditable:[sender state]];
    }
}
194

195 196 197 198 199 200 201
- (IBAction)useSTUN:(id)sender {
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    if( accountProperties.STUN.enable != [sender state]) {
        accountProperties.STUN.enable = [sender state];
        self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
        [stunAddressField setEditable:[sender state]];
    }
202 203
}

204 205 206 207 208 209 210 211 212
- (IBAction)selectFile:(id)sender {
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    [panel setAllowsMultipleSelection:NO];
    [panel setCanChooseDirectories:NO];
    [panel setCanChooseFiles:YES];
    if ([panel runModal] != NSFileHandlingPanelOKButton) return;
    if ([[panel URLs] lastObject] == nil) return;
    NSString * path = [[[panel URLs] lastObject] path];
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
213
    switch ([sender tag]) {
214 215 216 217 218 219
        case TAG_RINGTONE:
            if(accountProperties.Ringtone.ringtonePath != [path UTF8String]) {
                accountProperties.Ringtone.ringtonePath = [path UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
                [ringtoneSelectionButton setTitle:[path lastPathComponent]];
            }
220
            break;
221 222 223 224 225 226
        case TAG_CA_CERTIFICATE:
            if(accountProperties.TLS.certificateListFile != [path UTF8String] ) {
                accountProperties.TLS.certificateListFile = [path UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
                [selectCACertificateButton setTitle:[path lastPathComponent]];
            }
227
            break;
228 229 230 231 232 233
        case TAG_USER_CERTIFICATE:
            if(accountProperties.TLS.certificateFile != [path UTF8String] ) {
                accountProperties.TLS.certificateFile = [path UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
                [selectUserCertificateButton setTitle:[path lastPathComponent]];
            }
234
            break;
235 236 237 238 239 240
        case TAG_PRIVATE_KEY:
            if(accountProperties.TLS.privateKeyFile != [path UTF8String] ) {
                accountProperties.TLS.privateKeyFile = [path UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
                [selectPrivateKeyButton setTitle:[path lastPathComponent]];
            }
241
            break;
242 243 244 245 246 247 248 249 250 251 252 253
    }
}

- (IBAction)valueDidChange:(id)sender
{
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    switch ([sender tag]) {
        case PRIVATE_KEY_PASSWORG_TAG:
            if(accountProperties.TLS.password != [[sender stringValue] UTF8String]) {
                accountProperties.TLS.password = [[sender stringValue] UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
            }
254
            break;
255 256 257 258 259
        case TURN_ADDRESS_TAG:
            if(accountProperties.TURN.server != [[sender stringValue] UTF8String]) {
                accountProperties.TURN.server = [[sender stringValue] UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
            }
260 261
            break;
        case TURN_USERNAME_TAG:
262 263 264 265
            if(accountProperties.TURN.username != [[sender stringValue] UTF8String]) {
                accountProperties.TURN.username = [[sender stringValue] UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
            }
266 267
            break;
        case TURN_PASSWORD_TAG:
268 269 270 271
            if(accountProperties.TURN.password != [[sender stringValue] UTF8String]) {
                accountProperties.TURN.password = [[sender stringValue] UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
            }
272 273
            break;
        case TURN_REALM_TAG:
274 275 276 277 278 279 280 281 282 283
            if(accountProperties.TURN.realm != [[sender stringValue] UTF8String]) {
                accountProperties.TURN.realm = [[sender stringValue] UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
            }
            break;
        case STUN_ADDRESS_TAG:
            if(accountProperties.STUN.server != [[sender stringValue] UTF8String]) {
                accountProperties.STUN.server = [[sender stringValue] UTF8String];
                self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
            }
284
            break;
285 286 287 288 289
        default:
            break;
    }
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
- (IBAction)moveAudioCodecUp:(id)sender {
    NSInteger row = [audioCodecView selectedRow];
    if(row < 0) {
        return;
    }
    auto audioCodecs = self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getAudioCodecs();
    auto codec = audioCodecs.begin();
    std::advance(codec, row);
    if (codec == audioCodecs.end()) {
        return;
    }
    self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->increasePriority(codec->id, false);
    [audioCodecView reloadData];
    row = row == 0 ? row: row-1;
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex: row];
    [audioCodecView selectRowIndexes:indexSet byExtendingSelection: NO];
    [audioCodecView scrollRowToVisible:row];
307 308
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
- (IBAction)moveAudioCodecDown:(id)sender {
    NSInteger row = [audioCodecView selectedRow];
    if(row < 0) {
        return;
    }
    auto audioCodecs = self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getAudioCodecs();
    auto codec = audioCodecs.begin();
    std::advance(codec, row);
    if (codec == audioCodecs.end()) {
        return;
    }
    self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->decreasePriority(codec->id, false);
    [audioCodecView reloadData];
    row = (row == ([audioCodecView numberOfRows] - 1)) ? row: row+1;
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex: row];
    [audioCodecView selectRowIndexes:indexSet byExtendingSelection: NO];
    [audioCodecView scrollRowToVisible:row];
}

- (IBAction)moveVideoCodecUp:(id)sender {
    NSInteger row = [videoCodecView selectedRow];
    if(row < 0) {
        return;
    }
    auto videoCodecs = self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getVideoCodecs();
    auto codec = videoCodecs.begin();
    std::advance(codec, row);
    if (codec == videoCodecs.end()) {
        return;
    }
    self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->increasePriority(codec->id, YES);
    [videoCodecView reloadData];
    row = row == 0 ? row: row-1;
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex: row];
    [videoCodecView selectRowIndexes:indexSet byExtendingSelection: NO];
    [videoCodecView scrollRowToVisible:row];
}

- (IBAction)moveVideoCodecDown:(id)sender {
    NSInteger row = [videoCodecView selectedRow];
    if(row < 0) {
        return;
    }
    auto videoCodecs = self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getVideoCodecs();
    auto codec = videoCodecs.begin();
    std::advance(codec, row);
    if (codec == videoCodecs.end()) {
        return;
    }
    self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->decreasePriority(codec->id, YES);
    [videoCodecView reloadData];
    row = row == [videoCodecView numberOfRows] - 1 ? row: row+1;
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex: row];
    [videoCodecView selectRowIndexes:indexSet byExtendingSelection: NO];
    [videoCodecView scrollRowToVisible:row];
}

- (IBAction)toggleVideoEnabled:(id)sender {
    lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
    if(accountProperties.Video.videoEnabled != [sender state]) {
        accountProperties.Video.videoEnabled = [sender state];
        self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
        [videoCodecView setEnabled:[sender state]];
        [videoCodecView reloadData];
        [disableVideoButton setHidden:[sender state]];
    }
}

- (IBAction)enableAudioCodec:(id)sender
{
    NSInteger row = [audioCodecView rowForView:sender];
    if(row < 0) {
        return;
    }
    auto audioCodecs = self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getAudioCodecs();
    auto codec = audioCodecs.begin();
    std::advance(codec, row);
    if (codec != audioCodecs.end() && codec->enabled != [sender state]) {
        self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->enable(codec->id, [sender state]);
    }
389 390
}

391
- (IBAction)enableVideoCodec:(id)sender
392
{
393 394 395 396 397 398 399 400 401
    NSInteger row = [videoCodecView rowForView:sender];
    if(row < 0) {
        return;
    }
    auto videoCodecs = self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getVideoCodecs();
    auto codec = videoCodecs.begin();
    std::advance(codec, row);
    if (codec != videoCodecs.end() && codec->enabled != [sender state]) {
        self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->enable(codec->id, [sender state]);
402
    }
403
}
404

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
#pragma mark - NSTableViewDelegate methods
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    if(tableView == audioCodecView) {
        NSTableCellView* audioCodecView = [tableView makeViewWithIdentifier:@"TableCellAudioCodecItem" owner:self];
        NSTextField* nameLabel = [audioCodecView viewWithTag: AUDIO_CODEC_NAME_TAG];
        NSTextField* samplerateLabel = [audioCodecView viewWithTag: AUDIO_CODEC_SAMPLERATE_TAG];
        NSButton* codecEnableButton = [audioCodecView viewWithTag: AUDIO_CODEC_ENABLE_TAG];
        auto audioCodecs = self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getAudioCodecs();
        auto codec = audioCodecs.begin();
        std::advance(codec, row);
        if (codec != audioCodecs.end()) {
            [nameLabel setStringValue: @(codec->name.c_str())];
            [samplerateLabel setStringValue: [@(codec->samplerate.c_str()) stringByAppendingString:@" Hz"]];
            [codecEnableButton setState:codec->enabled];
            [codecEnableButton setAction:@selector(enableAudioCodec:)];
            [codecEnableButton setTarget:self];
            return audioCodecView;
        }
    } else if (tableView == videoCodecView) {
        NSTableCellView* videoCodecView = [tableView makeViewWithIdentifier:@"TableCellVideoCodecItem" owner:self];
        NSTextField* nameLabel = [videoCodecView viewWithTag: VIDEO_CODEC_NAME_TAG];
        NSButton* codecEnableButton = [audioCodecView viewWithTag: VIDIO_CODEC_ENABLE_TAG];
        nameLabel.textColor = [tableView isEnabled] ? [NSColor labelColor] : [NSColor lightGrayColor];
        [codecEnableButton setEnabled:[tableView isEnabled]];
        auto codecs = self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getVideoCodecs();
        auto codec = codecs.begin();
        std::advance(codec, row);
        if (codec != codecs.end()) {
            [nameLabel setStringValue: @(codec->name.c_str())];
            [codecEnableButton setState:codec->enabled];
            [codecEnableButton setAction:@selector(enableVideoCodec:)];
            [codecEnableButton setTarget:self];
            return videoCodecView;
        }
    }
441 442
}

443
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
444
{
445 446 447 448 449 450 451 452 453 454 455 456 457
    if(![tableView isEnabled]) {
        return nil;
    }
    return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
}

#pragma mark - NSTableViewDataSource methods

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    if(tableView == audioCodecView) {
        return self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getAudioCodecs().size();
    } else if (tableView == videoCodecView){
        return self.accountModel->getAccountInfo(self.selectedAccountID).codecModel->getVideoCodecs().size();
458
    }
459
    return 0;
460 461 462
}

@end