AccRingVC.mm 5.73 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
 *  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 "AccRingVC.h"

27 28
#import <accountmodel.h>
#import <qitemselectionmodel.h>
29

30
@interface AccRingVC ()
31 32 33 34 35 36 37 38 39 40

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

@property (assign) IBOutlet NSButton *upnpButton;
@property (assign) IBOutlet NSButton *autoAnswerButton;
@property (assign) IBOutlet NSButton *userAgentButton;
@property (assign) IBOutlet NSTextField *userAgentTextField;
41 42 43
@property (unsafe_unretained) IBOutlet NSButton *allowUnknown;
@property (unsafe_unretained) IBOutlet NSButton *allowHistory;
@property (unsafe_unretained) IBOutlet NSButton *allowContacts;
44 45 46 47 48 49 50 51 52 53 54 55

@end

@implementation AccRingVC
@synthesize typeLabel;
@synthesize bootstrapField;
@synthesize hashField;
@synthesize aliasTextField;
@synthesize upnpButton;
@synthesize autoAnswerButton;
@synthesize userAgentButton;
@synthesize userAgentTextField;
56
@synthesize allowContacts, allowHistory, allowUnknown;
57 58 59 60 61 62 63

- (void)awakeFromNib
{
    NSLog(@"INIT Ring VC");
    [aliasTextField setTag:ALIAS_TAG];
    [userAgentTextField setTag:USERAGENT_TAG];
    [bootstrapField setTag:HOSTNAME_TAG];
64

65
    QObject::connect(AccountModel::instance().selectionModel(),
66 67 68 69 70 71
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid())
                             return;
                         [self loadAccount];
                     });
72 73
}

74
- (Account*) currentAccount
75
{
76 77
    auto accIdx = AccountModel::instance().selectionModel()->currentIndex();
    return AccountModel::instance().getAccountByModelIndex(accIdx);
78
}
79

80 81 82
- (void)loadAccount
{
    auto account = [self currentAccount];
83 84 85

    [self.aliasTextField setStringValue:account->alias().toNSString()];

86
    [typeLabel setStringValue:@"RING"];
87

88 89 90
    [allowUnknown setState:account->allowIncomingFromUnknown()];
    [allowHistory setState:account->allowIncomingFromHistory()];
    [allowContacts setState:account->allowIncomingFromContact()];
91

92 93
    [allowHistory setEnabled:!account->allowIncomingFromUnknown()];
    [allowContacts setEnabled:!account->allowIncomingFromUnknown()];
94

95 96 97 98 99
    [upnpButton setState:account->isUpnpEnabled()];
    [userAgentButton setState:account->hasCustomUserAgent()];
    [userAgentTextField setEnabled:account->hasCustomUserAgent()];

    [autoAnswerButton setState:account->isAutoAnswer()];
100 101 102 103
    [userAgentTextField setStringValue:account->userAgent().toNSString()];

    [bootstrapField setStringValue:account->hostname().toNSString()];

104
    if([account->username().toNSString() isEqualToString:@""])
105 106
        [hashField setStringValue:NSLocalizedString(@"Reopen account to see your hash",
                                                    @"Show advice to user")];
107
    else
108
        [hashField setStringValue:account->username().toNSString()];
109 110 111 112

}

- (IBAction)toggleUpnp:(NSButton *)sender {
113
    [self currentAccount]->setUpnpEnabled([sender state] == NSOnState);
114 115 116
}

- (IBAction)toggleAutoAnswer:(NSButton *)sender {
117
    [self currentAccount]->setAutoAnswer([sender state] == NSOnState);
118 119 120 121
}

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

125 126 127 128 129 130 131 132 133 134 135 136
- (IBAction)toggleAllowFromUnknown:(id)sender {
    [self currentAccount]->setAllowIncomingFromUnknown([sender state] == NSOnState);
    [allowHistory setEnabled:![sender state] == NSOnState];
    [allowContacts setEnabled:![sender state] == NSOnState];
}
- (IBAction)toggleAllowFromHistory:(id)sender {
    [self currentAccount]->setAllowIncomingFromHistory([sender state] == NSOnState);
}
- (IBAction)toggleAllowFromContacts:(id)sender {
    [self currentAccount]->setAllowIncomingFromContact([sender state] == NSOnState);
}

137 138 139 140 141 142 143 144 145 146 147 148 149
#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:
150
            [self currentAccount]->setAlias([[textField stringValue] UTF8String]);
151
            [self currentAccount]->setDisplayName([[textField stringValue] UTF8String]);
152 153
            break;
        case HOSTNAME_TAG:
154
            [self currentAccount]->setHostname([[textField stringValue] UTF8String]);
155 156
            break;
        case PASSWORD_TAG:
157
            [self currentAccount]->setPassword([[textField stringValue] UTF8String]);
158 159
            break;
        case USERAGENT_TAG:
160
            [self currentAccount]->setUserAgent([[textField stringValue] UTF8String]);
161 162 163 164 165 166 167
            break;
        default:
            break;
    }
}

@end