Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2015 Savoir-Faire Linux Inc. |
| 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Additional permission under GNU GPL version 3 section 7: |
| 20 | * |
| 21 | * If you modify this program, or any covered work, by linking or |
| 22 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 23 | * modified version of that library), containing parts covered by the |
| 24 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 25 | * grants you additional permission to convey the resulting work. |
| 26 | * Corresponding Source for a non-source form of such a combination |
| 27 | * shall include the source code for the parts of OpenSSL used as well |
| 28 | * as that of the covered work. |
| 29 | */ |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 30 | #import "AudioPrefsVC.h" |
| 31 | |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 32 | #import <audio/settings.h> |
| 33 | #import <QUrl> |
| 34 | #import <audio/inputdevicemodel.h> |
| 35 | #import <audio/outputdevicemodel.h> |
| 36 | #import <qitemselectionmodel.h> |
| 37 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 38 | @interface AudioPrefsVC () |
| 39 | |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 40 | @property (assign) IBOutlet NSPathControl *recordingsPathControl; |
| 41 | @property (assign) IBOutlet NSPopUpButton *outputDeviceList; |
| 42 | @property (assign) IBOutlet NSPopUpButton *inputDeviceList; |
| 43 | @property (assign) IBOutlet NSButton *alwaysRecordingButton; |
| 44 | @property (assign) IBOutlet NSButton *muteDTMFButton; |
| 45 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 46 | @end |
| 47 | |
| 48 | @implementation AudioPrefsVC |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 49 | @synthesize recordingsPathControl; |
| 50 | @synthesize outputDeviceList; |
| 51 | @synthesize inputDeviceList; |
| 52 | @synthesize alwaysRecordingButton; |
| 53 | @synthesize muteDTMFButton; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 54 | |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 55 | - (void)loadView |
| 56 | { |
| 57 | [super loadView]; |
| 58 | |
| 59 | QModelIndex qInputIdx = Audio::Settings::instance()->inputDeviceModel()->selectionModel()->currentIndex(); |
| 60 | QModelIndex qOutputIdx = Audio::Settings::instance()->outputDeviceModel()->selectionModel()->currentIndex(); |
| 61 | |
| 62 | [self.outputDeviceList addItemWithTitle: |
| 63 | Audio::Settings::instance()->outputDeviceModel()->data(qOutputIdx, Qt::DisplayRole).toString().toNSString()]; |
| 64 | |
| 65 | [self.inputDeviceList addItemWithTitle: |
| 66 | Audio::Settings::instance()->inputDeviceModel()->data(qInputIdx, Qt::DisplayRole).toString().toNSString()]; |
| 67 | [self.alwaysRecordingButton setState: |
| 68 | Audio::Settings::instance()->isAlwaysRecording()?NSOnState:NSOffState]; |
| 69 | |
| 70 | [self.muteDTMFButton setState: |
| 71 | Audio::Settings::instance()->areDTMFMuted()?NSOnState:NSOffState]; |
| 72 | |
| 73 | if([[Audio::Settings::instance()->recordPath().toNSURL() absoluteString] isEqualToString:@""]) { |
| 74 | NSArray * pathComponentArray = [self pathComponentArray]; |
| 75 | [recordingsPathControl setPathComponentCells:pathComponentArray]; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | - (IBAction)toggleMuteDTMF:(NSButton *)sender |
| 80 | { |
| 81 | Audio::Settings::instance()->setDTMFMuted([sender state] == NSOnState); |
| 82 | } |
| 83 | |
| 84 | - (IBAction)toggleAlwaysRecording:(NSButton *)sender |
| 85 | { |
| 86 | Audio::Settings::instance()->setAlwaysRecording([sender state] == NSOnState); |
| 87 | } |
| 88 | |
| 89 | - (IBAction)pathControlSingleClick:(id)sender { |
| 90 | // Select that chosen component of the path. |
| 91 | [self.recordingsPathControl setURL:[[self.recordingsPathControl clickedPathComponentCell] URL]]; |
| 92 | Audio::Settings::instance()->setRecordPath(QUrl::fromNSURL(self.recordingsPathControl.URL)); |
| 93 | } |
| 94 | |
| 95 | - (IBAction)chooseOutput:(id)sender { |
| 96 | int index = [sender indexOfSelectedItem]; |
| 97 | QModelIndex qIdx = Audio::Settings::instance()->outputDeviceModel()->index(index, 0); |
| 98 | Audio::Settings::instance()->outputDeviceModel()->selectionModel()->setCurrentIndex( |
| 99 | qIdx, QItemSelectionModel::ClearAndSelect); |
| 100 | } |
| 101 | |
| 102 | - (IBAction)chooseInput:(id)sender { |
| 103 | int index = [sender indexOfSelectedItem]; |
| 104 | QModelIndex qIdx = Audio::Settings::instance()->inputDeviceModel()->index(index, 0); |
| 105 | Audio::Settings::instance()->inputDeviceModel()->selectionModel()->setCurrentIndex( |
| 106 | qIdx, QItemSelectionModel::ClearAndSelect); |
| 107 | } |
| 108 | |
| 109 | #pragma mark - NSPathControl delegate methods |
| 110 | |
| 111 | /* |
| 112 | Assemble a set of custom cells to display into an array to pass to the path control. |
| 113 | */ |
| 114 | - (NSArray *)pathComponentArray |
| 115 | { |
| 116 | NSMutableArray *pathComponentArray = [[NSMutableArray alloc] init]; |
| 117 | |
| 118 | NSFileManager *fileManager = [[NSFileManager alloc] init]; |
| 119 | |
| 120 | NSURL* desktopURL = [fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 121 | NSURL* documentsURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 122 | NSURL* userURL = [fileManager URLForDirectory:NSUserDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 123 | |
| 124 | NSPathComponentCell *componentCell; |
| 125 | |
| 126 | // Use utility method to obtain a NSPathComponentCell based on icon, title and URL. |
| 127 | componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Desktop" URL:desktopURL]; |
| 128 | [pathComponentArray addObject:componentCell]; |
| 129 | |
| 130 | componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Documents" URL:documentsURL]; |
| 131 | [pathComponentArray addObject:componentCell]; |
| 132 | |
| 133 | componentCell = [self componentCellForType:kUserFolderIcon withTitle:NSUserName() URL:userURL]; |
| 134 | [pathComponentArray addObject:componentCell]; |
| 135 | |
| 136 | return pathComponentArray; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | This method is used by pathComponentArray to create a NSPathComponent cell based on icon, title and URL information. |
| 141 | Each path component needs an icon, URL and title. |
| 142 | */ |
| 143 | - (NSPathComponentCell *)componentCellForType:(OSType)withIconType withTitle:(NSString *)title URL:(NSURL *)url |
| 144 | { |
| 145 | NSPathComponentCell *componentCell = [[NSPathComponentCell alloc] init]; |
| 146 | |
| 147 | NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(withIconType)]; |
| 148 | [componentCell setImage:iconImage]; |
| 149 | [componentCell setURL:url]; |
| 150 | [componentCell setTitle:title]; |
| 151 | |
| 152 | return componentCell; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | Delegate method of NSPathControl to determine how the NSOpenPanel will look/behave. |
| 157 | */ |
| 158 | - (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel |
| 159 | { |
| 160 | NSLog(@"willDisplayOpenPanel"); |
| 161 | [openPanel setAllowsMultipleSelection:NO]; |
| 162 | [openPanel setCanChooseDirectories:YES]; |
| 163 | [openPanel setCanChooseFiles:NO]; |
| 164 | [openPanel setResolvesAliases:YES]; |
| 165 | [openPanel setTitle:NSLocalizedString(@"Choose a file", @"Open panel title")]; |
| 166 | [openPanel setPrompt:NSLocalizedString(@"Choose", @"Open panel prompt for 'Choose a directory'")]; |
| 167 | [openPanel setDelegate:self]; |
| 168 | } |
| 169 | |
| 170 | - (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu |
| 171 | { |
| 172 | |
| 173 | } |
| 174 | |
| 175 | #pragma mark - NSOpenSavePanelDelegate delegate methods |
| 176 | |
| 177 | - (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError |
| 178 | { |
| 179 | [recordingsPathControl setURL:url]; |
| 180 | return YES; |
| 181 | } |
| 182 | |
| 183 | #pragma mark - NSMenuDelegate methods |
| 184 | |
| 185 | - (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel |
| 186 | { |
| 187 | QModelIndex qIdx; |
| 188 | |
| 189 | if([menu.title isEqualToString:@"inputlist"]) |
| 190 | { |
| 191 | qIdx = Audio::Settings::instance()->inputDeviceModel()->index(index); |
| 192 | [item setTitle:Audio::Settings::instance()->inputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
| 193 | } else |
| 194 | { |
| 195 | qIdx = Audio::Settings::instance()->outputDeviceModel()->index(index); |
| 196 | [item setTitle:Audio::Settings::instance()->outputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
| 197 | } |
| 198 | |
| 199 | return YES; |
| 200 | } |
| 201 | |
| 202 | - (NSInteger)numberOfItemsInMenu:(NSMenu *)menu |
| 203 | { |
| 204 | if([menu.title isEqualToString:@"inputlist"]) |
| 205 | return Audio::Settings::instance()->inputDeviceModel()->rowCount(); |
| 206 | else |
| 207 | return Audio::Settings::instance()->outputDeviceModel()->rowCount(); |
| 208 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 209 | |
| 210 | @end |