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]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame^] | 76 | } else { |
| 77 | [recordingsPathControl setURL:Audio::Settings::instance()->recordPath().toNSURL()]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | - (IBAction)toggleMuteDTMF:(NSButton *)sender |
| 82 | { |
| 83 | Audio::Settings::instance()->setDTMFMuted([sender state] == NSOnState); |
| 84 | } |
| 85 | |
| 86 | - (IBAction)toggleAlwaysRecording:(NSButton *)sender |
| 87 | { |
| 88 | Audio::Settings::instance()->setAlwaysRecording([sender state] == NSOnState); |
| 89 | } |
| 90 | |
| 91 | - (IBAction)pathControlSingleClick:(id)sender { |
| 92 | // Select that chosen component of the path. |
| 93 | [self.recordingsPathControl setURL:[[self.recordingsPathControl clickedPathComponentCell] URL]]; |
| 94 | Audio::Settings::instance()->setRecordPath(QUrl::fromNSURL(self.recordingsPathControl.URL)); |
| 95 | } |
| 96 | |
| 97 | - (IBAction)chooseOutput:(id)sender { |
| 98 | int index = [sender indexOfSelectedItem]; |
| 99 | QModelIndex qIdx = Audio::Settings::instance()->outputDeviceModel()->index(index, 0); |
| 100 | Audio::Settings::instance()->outputDeviceModel()->selectionModel()->setCurrentIndex( |
| 101 | qIdx, QItemSelectionModel::ClearAndSelect); |
| 102 | } |
| 103 | |
| 104 | - (IBAction)chooseInput:(id)sender { |
| 105 | int index = [sender indexOfSelectedItem]; |
| 106 | QModelIndex qIdx = Audio::Settings::instance()->inputDeviceModel()->index(index, 0); |
| 107 | Audio::Settings::instance()->inputDeviceModel()->selectionModel()->setCurrentIndex( |
| 108 | qIdx, QItemSelectionModel::ClearAndSelect); |
| 109 | } |
| 110 | |
| 111 | #pragma mark - NSPathControl delegate methods |
| 112 | |
| 113 | /* |
| 114 | Assemble a set of custom cells to display into an array to pass to the path control. |
| 115 | */ |
| 116 | - (NSArray *)pathComponentArray |
| 117 | { |
| 118 | NSMutableArray *pathComponentArray = [[NSMutableArray alloc] init]; |
| 119 | |
| 120 | NSFileManager *fileManager = [[NSFileManager alloc] init]; |
| 121 | |
| 122 | NSURL* desktopURL = [fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 123 | NSURL* documentsURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 124 | NSURL* userURL = [fileManager URLForDirectory:NSUserDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 125 | |
| 126 | NSPathComponentCell *componentCell; |
| 127 | |
| 128 | // Use utility method to obtain a NSPathComponentCell based on icon, title and URL. |
| 129 | componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Desktop" URL:desktopURL]; |
| 130 | [pathComponentArray addObject:componentCell]; |
| 131 | |
| 132 | componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Documents" URL:documentsURL]; |
| 133 | [pathComponentArray addObject:componentCell]; |
| 134 | |
| 135 | componentCell = [self componentCellForType:kUserFolderIcon withTitle:NSUserName() URL:userURL]; |
| 136 | [pathComponentArray addObject:componentCell]; |
| 137 | |
| 138 | return pathComponentArray; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | This method is used by pathComponentArray to create a NSPathComponent cell based on icon, title and URL information. |
| 143 | Each path component needs an icon, URL and title. |
| 144 | */ |
| 145 | - (NSPathComponentCell *)componentCellForType:(OSType)withIconType withTitle:(NSString *)title URL:(NSURL *)url |
| 146 | { |
| 147 | NSPathComponentCell *componentCell = [[NSPathComponentCell alloc] init]; |
| 148 | |
| 149 | NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(withIconType)]; |
| 150 | [componentCell setImage:iconImage]; |
| 151 | [componentCell setURL:url]; |
| 152 | [componentCell setTitle:title]; |
| 153 | |
| 154 | return componentCell; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | Delegate method of NSPathControl to determine how the NSOpenPanel will look/behave. |
| 159 | */ |
| 160 | - (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel |
| 161 | { |
| 162 | NSLog(@"willDisplayOpenPanel"); |
| 163 | [openPanel setAllowsMultipleSelection:NO]; |
| 164 | [openPanel setCanChooseDirectories:YES]; |
| 165 | [openPanel setCanChooseFiles:NO]; |
| 166 | [openPanel setResolvesAliases:YES]; |
| 167 | [openPanel setTitle:NSLocalizedString(@"Choose a file", @"Open panel title")]; |
| 168 | [openPanel setPrompt:NSLocalizedString(@"Choose", @"Open panel prompt for 'Choose a directory'")]; |
| 169 | [openPanel setDelegate:self]; |
| 170 | } |
| 171 | |
| 172 | - (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu |
| 173 | { |
| 174 | |
| 175 | } |
| 176 | |
| 177 | #pragma mark - NSOpenSavePanelDelegate delegate methods |
| 178 | |
| 179 | - (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError |
| 180 | { |
| 181 | [recordingsPathControl setURL:url]; |
| 182 | return YES; |
| 183 | } |
| 184 | |
| 185 | #pragma mark - NSMenuDelegate methods |
| 186 | |
| 187 | - (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel |
| 188 | { |
| 189 | QModelIndex qIdx; |
| 190 | |
| 191 | if([menu.title isEqualToString:@"inputlist"]) |
| 192 | { |
| 193 | qIdx = Audio::Settings::instance()->inputDeviceModel()->index(index); |
| 194 | [item setTitle:Audio::Settings::instance()->inputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
| 195 | } else |
| 196 | { |
| 197 | qIdx = Audio::Settings::instance()->outputDeviceModel()->index(index); |
| 198 | [item setTitle:Audio::Settings::instance()->outputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
| 199 | } |
| 200 | |
| 201 | return YES; |
| 202 | } |
| 203 | |
| 204 | - (NSInteger)numberOfItemsInMenu:(NSMenu *)menu |
| 205 | { |
| 206 | if([menu.title isEqualToString:@"inputlist"]) |
| 207 | return Audio::Settings::instance()->inputDeviceModel()->rowCount(); |
| 208 | else |
| 209 | return Audio::Settings::instance()->outputDeviceModel()->rowCount(); |
| 210 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 211 | |
| 212 | @end |