Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 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. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 18 | */ |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 19 | #import "AudioPrefsVC.h" |
| 20 | |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 21 | #import <audio/settings.h> |
Alexandre Lision | 6664343 | 2015-06-04 11:59:36 -0400 | [diff] [blame] | 22 | #import <media/recordingmodel.h> |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 23 | #import <QUrl> |
| 24 | #import <audio/inputdevicemodel.h> |
| 25 | #import <audio/outputdevicemodel.h> |
| 26 | #import <qitemselectionmodel.h> |
| 27 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 28 | @interface AudioPrefsVC () |
| 29 | |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 30 | @property (assign) IBOutlet NSPathControl *recordingsPathControl; |
| 31 | @property (assign) IBOutlet NSPopUpButton *outputDeviceList; |
| 32 | @property (assign) IBOutlet NSPopUpButton *inputDeviceList; |
| 33 | @property (assign) IBOutlet NSButton *alwaysRecordingButton; |
| 34 | @property (assign) IBOutlet NSButton *muteDTMFButton; |
| 35 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 36 | @end |
| 37 | |
| 38 | @implementation AudioPrefsVC |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 39 | @synthesize recordingsPathControl; |
| 40 | @synthesize outputDeviceList; |
| 41 | @synthesize inputDeviceList; |
| 42 | @synthesize alwaysRecordingButton; |
| 43 | @synthesize muteDTMFButton; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 44 | |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 45 | - (void)loadView |
| 46 | { |
| 47 | [super loadView]; |
| 48 | |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 49 | QModelIndex qInputIdx = Audio::Settings::instance().inputDeviceModel()->selectionModel()->currentIndex(); |
| 50 | QModelIndex qOutputIdx = Audio::Settings::instance().outputDeviceModel()->selectionModel()->currentIndex(); |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 51 | |
| 52 | [self.outputDeviceList addItemWithTitle: |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 53 | Audio::Settings::instance().outputDeviceModel()->data(qOutputIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 54 | |
| 55 | [self.inputDeviceList addItemWithTitle: |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 56 | Audio::Settings::instance().inputDeviceModel()->data(qInputIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 57 | [self.alwaysRecordingButton setState: |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 58 | Media::RecordingModel::instance().isAlwaysRecording() ? NSOnState:NSOffState]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 59 | |
| 60 | [self.muteDTMFButton setState: |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 61 | Audio::Settings::instance().areDTMFMuted()?NSOnState:NSOffState]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 62 | |
Alexandre Lision | e4666d1 | 2016-02-05 17:12:23 -0500 | [diff] [blame] | 63 | if([Media::RecordingModel::instance().recordPath().toNSString() isEqualToString:@""]) { |
| 64 | NSArray* pathComponentArray = [self pathComponentArray]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 65 | [recordingsPathControl setPathComponentCells:pathComponentArray]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 66 | } else { |
Alexandre Lision | e4666d1 | 2016-02-05 17:12:23 -0500 | [diff] [blame] | 67 | [recordingsPathControl setURL: |
| 68 | [NSURL URLWithString:Media::RecordingModel::instance().recordPath().toNSString()]]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | - (IBAction)toggleMuteDTMF:(NSButton *)sender |
| 73 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 74 | Audio::Settings::instance().setDTMFMuted([sender state] == NSOnState); |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | - (IBAction)toggleAlwaysRecording:(NSButton *)sender |
| 78 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 79 | Media::RecordingModel::instance().setAlwaysRecording([sender state] == NSOnState); |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | - (IBAction)pathControlSingleClick:(id)sender { |
| 83 | // Select that chosen component of the path. |
| 84 | [self.recordingsPathControl setURL:[[self.recordingsPathControl clickedPathComponentCell] URL]]; |
Alexandre Lision | e4666d1 | 2016-02-05 17:12:23 -0500 | [diff] [blame] | 85 | Media::RecordingModel::instance().setRecordPath(QString::fromNSString([self.recordingsPathControl.URL path])); |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | - (IBAction)chooseOutput:(id)sender { |
| 89 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 90 | QModelIndex qIdx = Audio::Settings::instance().outputDeviceModel()->index(index, 0); |
| 91 | Audio::Settings::instance().outputDeviceModel()->selectionModel()->setCurrentIndex( |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 92 | qIdx, QItemSelectionModel::ClearAndSelect); |
| 93 | } |
| 94 | |
| 95 | - (IBAction)chooseInput:(id)sender { |
| 96 | int index = [sender indexOfSelectedItem]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 97 | QModelIndex qIdx = Audio::Settings::instance().inputDeviceModel()->index(index, 0); |
| 98 | Audio::Settings::instance().inputDeviceModel()->selectionModel()->setCurrentIndex( |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 99 | qIdx, QItemSelectionModel::ClearAndSelect); |
| 100 | } |
| 101 | |
| 102 | #pragma mark - NSPathControl delegate methods |
| 103 | |
| 104 | /* |
| 105 | Assemble a set of custom cells to display into an array to pass to the path control. |
| 106 | */ |
| 107 | - (NSArray *)pathComponentArray |
| 108 | { |
| 109 | NSMutableArray *pathComponentArray = [[NSMutableArray alloc] init]; |
| 110 | |
| 111 | NSFileManager *fileManager = [[NSFileManager alloc] init]; |
| 112 | |
| 113 | NSURL* desktopURL = [fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 114 | NSURL* documentsURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 115 | NSURL* userURL = [fileManager URLForDirectory:NSUserDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; |
| 116 | |
| 117 | NSPathComponentCell *componentCell; |
| 118 | |
| 119 | // Use utility method to obtain a NSPathComponentCell based on icon, title and URL. |
| 120 | componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Desktop" URL:desktopURL]; |
| 121 | [pathComponentArray addObject:componentCell]; |
| 122 | |
| 123 | componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Documents" URL:documentsURL]; |
| 124 | [pathComponentArray addObject:componentCell]; |
| 125 | |
| 126 | componentCell = [self componentCellForType:kUserFolderIcon withTitle:NSUserName() URL:userURL]; |
| 127 | [pathComponentArray addObject:componentCell]; |
| 128 | |
| 129 | return pathComponentArray; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | This method is used by pathComponentArray to create a NSPathComponent cell based on icon, title and URL information. |
| 134 | Each path component needs an icon, URL and title. |
| 135 | */ |
| 136 | - (NSPathComponentCell *)componentCellForType:(OSType)withIconType withTitle:(NSString *)title URL:(NSURL *)url |
| 137 | { |
| 138 | NSPathComponentCell *componentCell = [[NSPathComponentCell alloc] init]; |
| 139 | |
| 140 | NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(withIconType)]; |
| 141 | [componentCell setImage:iconImage]; |
| 142 | [componentCell setURL:url]; |
| 143 | [componentCell setTitle:title]; |
| 144 | |
| 145 | return componentCell; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | Delegate method of NSPathControl to determine how the NSOpenPanel will look/behave. |
| 150 | */ |
| 151 | - (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel |
| 152 | { |
| 153 | NSLog(@"willDisplayOpenPanel"); |
| 154 | [openPanel setAllowsMultipleSelection:NO]; |
| 155 | [openPanel setCanChooseDirectories:YES]; |
| 156 | [openPanel setCanChooseFiles:NO]; |
| 157 | [openPanel setResolvesAliases:YES]; |
Alexandre Lision | c818011 | 2016-01-27 11:27:50 -0500 | [diff] [blame] | 158 | [openPanel setTitle:NSLocalizedString(@"Choose a directory", @"Open panel title")]; |
| 159 | [openPanel setPrompt:NSLocalizedString(@"Choose directory", @"Open panel prompt for 'Choose a directory'")]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 160 | [openPanel setDelegate:self]; |
| 161 | } |
| 162 | |
| 163 | - (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu |
| 164 | { |
| 165 | |
| 166 | } |
| 167 | |
| 168 | #pragma mark - NSOpenSavePanelDelegate delegate methods |
| 169 | |
| 170 | - (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError |
| 171 | { |
| 172 | [recordingsPathControl setURL:url]; |
| 173 | return YES; |
| 174 | } |
| 175 | |
| 176 | #pragma mark - NSMenuDelegate methods |
| 177 | |
| 178 | - (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel |
| 179 | { |
| 180 | QModelIndex qIdx; |
| 181 | |
Alexandre Lision | 51ff149 | 2016-04-15 11:38:37 -0400 | [diff] [blame] | 182 | if (inputDeviceList.menu == menu) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 183 | qIdx = Audio::Settings::instance().inputDeviceModel()->index(index); |
| 184 | [item setTitle:Audio::Settings::instance().inputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 51ff149 | 2016-04-15 11:38:37 -0400 | [diff] [blame] | 185 | } else { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 186 | qIdx = Audio::Settings::instance().outputDeviceModel()->index(index); |
| 187 | [item setTitle:Audio::Settings::instance().outputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()]; |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return YES; |
| 191 | } |
| 192 | |
| 193 | - (NSInteger)numberOfItemsInMenu:(NSMenu *)menu |
| 194 | { |
Alexandre Lision | 51ff149 | 2016-04-15 11:38:37 -0400 | [diff] [blame] | 195 | if (inputDeviceList.menu == menu) |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 196 | return Audio::Settings::instance().inputDeviceModel()->rowCount(); |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 197 | else |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 198 | return Audio::Settings::instance().outputDeviceModel()->rowCount(); |
Alexandre Lision | 611b8c1 | 2015-03-20 18:07:17 -0400 | [diff] [blame] | 199 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 200 | |
| 201 | @end |