AccGeneralVC.mm 7.38 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
 *  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 ALIAS_TAG 0
#define HOSTNAME_TAG 1
#define USERNAME_TAG 2
#define PASSWORD_TAG 3
#define USERAGENT_TAG 4


#import "AccGeneralVC.h"

28
#import <accountmodel.h>
29
#import <protocolmodel.h>
30
#import <qitemselectionmodel.h>
31 32 33 34 35 36 37 38 39 40 41 42 43

@interface AccGeneralVC ()

@property (assign) IBOutlet NSView *boxingAccount;
@property (assign) IBOutlet NSView *boxingParameters;
@property (assign) IBOutlet NSView *boxingCommon;

@property (assign) IBOutlet NSTextField *aliasTextField;
@property (assign) IBOutlet NSTextField *typeLabel;

@property (assign) IBOutlet NSTextField *serverHostTextField;
@property (assign) IBOutlet NSTextField *usernameTextField;
@property (assign) IBOutlet NSSecureTextField *passwordTextField;
44
@property (strong) NSTextField *clearTextField;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

@property (assign) IBOutlet NSButton *upnpButton;
@property (assign) IBOutlet NSButton *autoAnswerButton;
@property (assign) IBOutlet NSButton *userAgentButton;

@property (assign) IBOutlet NSTextField *userAgentTextField;

@end

@implementation AccGeneralVC
@synthesize typeLabel;
@synthesize boxingAccount;
@synthesize boxingParameters;
@synthesize boxingCommon;
@synthesize aliasTextField;
@synthesize serverHostTextField;
@synthesize usernameTextField;
@synthesize passwordTextField;
63
@synthesize clearTextField;
64 65 66 67 68 69 70 71 72 73 74 75 76
@synthesize upnpButton;
@synthesize autoAnswerButton;
@synthesize userAgentButton;
@synthesize userAgentTextField;

- (void)awakeFromNib
{
    NSLog(@"INIT General VC");
    [aliasTextField setTag:ALIAS_TAG];
    [serverHostTextField setTag:HOSTNAME_TAG];
    [usernameTextField setTag:USERNAME_TAG];
    [passwordTextField setTag:PASSWORD_TAG];
    [userAgentTextField setTag:USERAGENT_TAG];
77

78
    QObject::connect(AccountModel::instance().selectionModel(),
79 80 81 82 83 84 85 86 87 88
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid())
                             return;
                         [self loadAccount];
                     });
}

- (Account*) currentAccount
{
89 90
    auto accIdx = AccountModel::instance().selectionModel()->currentIndex();
    return AccountModel::instance().getAccountByModelIndex(accIdx);
91 92 93
}

- (IBAction)toggleUpnp:(NSButton *)sender {
94
    [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
95 96 97
}

- (IBAction)toggleAutoAnswer:(NSButton *)sender {
98
    [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
99 100 101 102
}

- (IBAction)toggleCustomAgent:(NSButton *)sender {
    [self.userAgentTextField setEnabled:[sender state] == NSOnState];
103
    [self currentAccount]->setHasCustomUserAgent([sender state] == NSOnState);
104 105
}

106
- (void)loadAccount
107
{
108
    auto account = [self currentAccount];
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    if([account->alias().toNSString() isEqualToString:@"IP2IP"]) {
        [boxingAccount.subviews setValue:@YES forKeyPath:@"hidden"];
        [boxingParameters.subviews setValue:@YES forKeyPath:@"hidden"];

        NSLog(@"IP@IP");
        // Put visible items at top of the frame
        [boxingCommon setFrameOrigin:NSMakePoint(boxingAccount.frame.origin.x,
                                                boxingAccount.frame.origin.y - 40)];
        [boxingCommon setNeedsDisplay:YES];

    } else {
        [boxingAccount.subviews setValue:@NO forKeyPath:@"hidden"];
        [boxingParameters.subviews setValue:@NO forKeyPath:@"hidden"];

        [self.aliasTextField setStringValue:account->alias().toNSString()];
        [self.serverHostTextField setStringValue:account->hostname().toNSString()];
        [self.usernameTextField setStringValue:account->username().toNSString()];
        [self.passwordTextField setStringValue:account->password().toNSString()];
128
        [self.clearTextField setStringValue:account->password().toNSString()];
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    }

    switch (account->protocol()) {
        case Account::Protocol::SIP:
            [self.typeLabel setStringValue:@"SIP"];
            break;
        case Account::Protocol::IAX:
            [self.typeLabel setStringValue:@"IAX"];
            break;
        case Account::Protocol::RING:
            [self.typeLabel setStringValue:@"RING"];
            break;

        default:
            break;
    }

146 147 148 149
    [upnpButton setState:[self currentAccount]->isUpnpEnabled()];
    [userAgentButton setState:[self currentAccount]->hasCustomUserAgent()];
    [userAgentTextField setEnabled:[self currentAccount]->hasCustomUserAgent()];
    [self.autoAnswerButton setState:[self currentAccount]->isAutoAnswer()];
150 151 152
    [self.userAgentTextField setStringValue:account->userAgent().toNSString()];
}

153
- (IBAction)tryRegistration:(id)sender {
154
    [self currentAccount] << Account::EditAction::SAVE;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

- (IBAction)showPassword:(NSButton *)sender {
    if (sender.state == NSOnState) {
        clearTextField = [[NSTextField alloc] initWithFrame:passwordTextField.frame];
        [clearTextField setTag:passwordTextField.tag];
        [clearTextField setDelegate:self];
        [clearTextField setBounds:passwordTextField.bounds];
        [clearTextField setStringValue:passwordTextField.stringValue];
        [clearTextField becomeFirstResponder];
        [boxingParameters addSubview:clearTextField];
        [passwordTextField setHidden:YES];
    } else {
        [passwordTextField setStringValue:clearTextField.stringValue];
        [passwordTextField setHidden:NO];
        [clearTextField removeFromSuperview];
        clearTextField = nil;
    }
}

/**
 *  Debug purpose
 */
-(void) dumpFrame:(CGRect) frame WithName:(NSString*) name
{
    NSLog(@"frame %@ : %f %f %f %f \n\n",name ,frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
}

183 184 185 186 187 188 189 190 191 192 193 194 195
#pragma mark - NSTextFieldDelegate methods

- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
{
    return YES;
}

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

    switch ([textField tag]) {
        case ALIAS_TAG:
196
            [self currentAccount]->setAlias([[textField stringValue] UTF8String]);
197
            [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]);
198 199
            break;
        case HOSTNAME_TAG:
200
            [self currentAccount]->setHostname([[textField stringValue] UTF8String]);
201 202
            break;
        case USERNAME_TAG:
203
            [self currentAccount]->setUsername([[textField stringValue] UTF8String]);
204 205
            break;
        case PASSWORD_TAG:
206
            [self currentAccount]->setPassword([[textField stringValue] UTF8String]);
207 208
            break;
        case USERAGENT_TAG:
209
            [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]);
210 211 212 213 214 215
            break;
        default:
            break;
    }
}
@end