AudioPrefsVC.mm 7.79 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
 *  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.
 */
19 20
#import "AudioPrefsVC.h"

21
#import <audio/settings.h>
22
#import <media/recordingmodel.h>
23 24 25 26 27
#import <QUrl>
#import <audio/inputdevicemodel.h>
#import <audio/outputdevicemodel.h>
#import <qitemselectionmodel.h>

28 29
@interface AudioPrefsVC ()

30 31 32 33 34 35
@property (assign) IBOutlet NSPathControl *recordingsPathControl;
@property (assign) IBOutlet NSPopUpButton *outputDeviceList;
@property (assign) IBOutlet NSPopUpButton *inputDeviceList;
@property (assign) IBOutlet NSButton *alwaysRecordingButton;
@property (assign) IBOutlet NSButton *muteDTMFButton;

36 37 38
@end

@implementation AudioPrefsVC
39 40 41 42 43 44 45 46 47 48
@synthesize recordingsPathControl;
@synthesize outputDeviceList;
@synthesize inputDeviceList;
@synthesize alwaysRecordingButton;
@synthesize muteDTMFButton;

- (void)loadView
{
    [super loadView];

49 50
    QModelIndex qInputIdx = Audio::Settings::instance().inputDeviceModel()->selectionModel()->currentIndex();
    QModelIndex qOutputIdx = Audio::Settings::instance().outputDeviceModel()->selectionModel()->currentIndex();
51 52
    
    [self.outputDeviceList addItemWithTitle:
53
            Audio::Settings::instance().outputDeviceModel()->data(qOutputIdx, Qt::DisplayRole).toString().toNSString()];
54 55

    [self.inputDeviceList addItemWithTitle:
56
            Audio::Settings::instance().inputDeviceModel()->data(qInputIdx, Qt::DisplayRole).toString().toNSString()];
57
    [self.alwaysRecordingButton setState:
58
            Media::RecordingModel::instance().isAlwaysRecording() ? NSOnState:NSOffState];
59 60

    [self.muteDTMFButton setState:
61
            Audio::Settings::instance().areDTMFMuted()?NSOnState:NSOffState];
62

63 64
    if([Media::RecordingModel::instance().recordPath().toNSString() isEqualToString:@""]) {
        NSArray* pathComponentArray = [self pathComponentArray];
65
        [recordingsPathControl setPathComponentCells:pathComponentArray];
66
    } else {
67 68
        [recordingsPathControl setURL:
         [NSURL URLWithString:Media::RecordingModel::instance().recordPath().toNSString()]];
69 70 71 72 73
    }
}

- (IBAction)toggleMuteDTMF:(NSButton *)sender
{
74
    Audio::Settings::instance().setDTMFMuted([sender state] == NSOnState);
75 76 77 78
}

- (IBAction)toggleAlwaysRecording:(NSButton *)sender
{
79
    Media::RecordingModel::instance().setAlwaysRecording([sender state] == NSOnState);
80 81 82 83 84
}

- (IBAction)pathControlSingleClick:(id)sender {
    // Select that chosen component of the path.
    [self.recordingsPathControl setURL:[[self.recordingsPathControl clickedPathComponentCell] URL]];
85
    Media::RecordingModel::instance().setRecordPath(QString::fromNSString([self.recordingsPathControl.URL path]));
86 87 88 89
}

- (IBAction)chooseOutput:(id)sender {
    int index = [sender indexOfSelectedItem];
90 91
    QModelIndex qIdx = Audio::Settings::instance().outputDeviceModel()->index(index, 0);
    Audio::Settings::instance().outputDeviceModel()->selectionModel()->setCurrentIndex(
92 93 94 95 96
                                                    qIdx, QItemSelectionModel::ClearAndSelect);
}

- (IBAction)chooseInput:(id)sender {
    int index = [sender indexOfSelectedItem];
97 98
    QModelIndex qIdx = Audio::Settings::instance().inputDeviceModel()->index(index, 0);
    Audio::Settings::instance().inputDeviceModel()->selectionModel()->setCurrentIndex(
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                                                    qIdx, QItemSelectionModel::ClearAndSelect);
}

#pragma mark - NSPathControl delegate methods

/*
 Assemble a set of custom cells to display into an array to pass to the path control.
 */
- (NSArray *)pathComponentArray
{
    NSMutableArray *pathComponentArray = [[NSMutableArray alloc] init];

    NSFileManager *fileManager = [[NSFileManager alloc] init];

    NSURL* desktopURL = [fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    NSURL* documentsURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
    NSURL* userURL = [fileManager URLForDirectory:NSUserDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

    NSPathComponentCell *componentCell;

    // Use utility method to obtain a NSPathComponentCell based on icon, title and URL.
    componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Desktop" URL:desktopURL];
    [pathComponentArray addObject:componentCell];

    componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Documents" URL:documentsURL];
    [pathComponentArray addObject:componentCell];

    componentCell = [self componentCellForType:kUserFolderIcon withTitle:NSUserName() URL:userURL];
    [pathComponentArray addObject:componentCell];

    return pathComponentArray;
}

/*
 This method is used by pathComponentArray to create a NSPathComponent cell based on icon, title and URL information. 
 Each path component needs an icon, URL and title.
 */
- (NSPathComponentCell *)componentCellForType:(OSType)withIconType withTitle:(NSString *)title URL:(NSURL *)url
{
    NSPathComponentCell *componentCell = [[NSPathComponentCell alloc] init];

    NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(withIconType)];
    [componentCell setImage:iconImage];
    [componentCell setURL:url];
    [componentCell setTitle:title];

    return componentCell;
}

/*
 Delegate method of NSPathControl to determine how the NSOpenPanel will look/behave.
 */
- (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel
{
    NSLog(@"willDisplayOpenPanel");
    [openPanel setAllowsMultipleSelection:NO];
    [openPanel setCanChooseDirectories:YES];
    [openPanel setCanChooseFiles:NO];
    [openPanel setResolvesAliases:YES];
158 159
    [openPanel setTitle:NSLocalizedString(@"Choose a directory", @"Open panel title")];
    [openPanel setPrompt:NSLocalizedString(@"Choose directory", @"Open panel prompt for 'Choose a directory'")];
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    [openPanel setDelegate:self];
}

- (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu
{

}

#pragma mark - NSOpenSavePanelDelegate delegate methods

- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError
{
    [recordingsPathControl setURL:url];
    return YES;
}

#pragma mark - NSMenuDelegate methods

- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
{
    QModelIndex qIdx;

    if([menu.title isEqualToString:@"inputlist"])
    {
184 185
        qIdx = Audio::Settings::instance().inputDeviceModel()->index(index);
        [item setTitle:Audio::Settings::instance().inputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()];
186 187
    } else
    {
188 189
        qIdx = Audio::Settings::instance().outputDeviceModel()->index(index);
        [item setTitle:Audio::Settings::instance().outputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()];
190 191 192 193
    }

    return YES;
}
194

195 196 197
- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
{
    if([menu.title isEqualToString:@"inputlist"])
198
        return Audio::Settings::instance().inputDeviceModel()->rowCount();
199
    else
200
        return Audio::Settings::instance().outputDeviceModel()->rowCount();
201
}
202 203

@end