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