blob: 543d63c94edc20fb6f735d4d22f3e1c8da057fb7 [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision8521baa2015-03-13 11:08:00 -04003 * 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 Lision8521baa2015-03-13 11:08:00 -040018 */
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050019#import "AudioPrefsVC.h"
20
Alexandre Lision611b8c12015-03-20 18:07:17 -040021#import <audio/settings.h>
Alexandre Lision66643432015-06-04 11:59:36 -040022#import <media/recordingmodel.h>
Alexandre Lision611b8c12015-03-20 18:07:17 -040023#import <QUrl>
24#import <audio/inputdevicemodel.h>
25#import <audio/outputdevicemodel.h>
26#import <qitemselectionmodel.h>
Kateryna Kostiuk4138db12018-06-08 15:52:18 -040027#import "utils.h"
Alexandre Lision611b8c12015-03-20 18:07:17 -040028
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050029@interface AudioPrefsVC ()
30
Alexandre Lision611b8c12015-03-20 18:07:17 -040031@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 Lision4a7b95e2015-02-20 10:06:43 -050037@end
38
39@implementation AudioPrefsVC
Alexandre Lision611b8c12015-03-20 18:07:17 -040040@synthesize recordingsPathControl;
41@synthesize outputDeviceList;
42@synthesize inputDeviceList;
43@synthesize alwaysRecordingButton;
44@synthesize muteDTMFButton;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050045
Alexandre Lision611b8c12015-03-20 18:07:17 -040046- (void)loadView
47{
48 [super loadView];
49
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040050 QModelIndex qInputIdx = Audio::Settings::instance().inputDeviceModel()->selectionModel()->currentIndex();
51 QModelIndex qOutputIdx = Audio::Settings::instance().outputDeviceModel()->selectionModel()->currentIndex();
Alexandre Lision611b8c12015-03-20 18:07:17 -040052
53 [self.outputDeviceList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040054 Audio::Settings::instance().outputDeviceModel()->data(qOutputIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision611b8c12015-03-20 18:07:17 -040055
56 [self.inputDeviceList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040057 Audio::Settings::instance().inputDeviceModel()->data(qInputIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision611b8c12015-03-20 18:07:17 -040058 [self.alwaysRecordingButton setState:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040059 Media::RecordingModel::instance().isAlwaysRecording() ? NSOnState:NSOffState];
Alexandre Lision611b8c12015-03-20 18:07:17 -040060
61 [self.muteDTMFButton setState:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040062 Audio::Settings::instance().areDTMFMuted()?NSOnState:NSOffState];
Kateryna Kostiuk4138db12018-06-08 15:52:18 -040063 NSArray* pathComponentArray = [self pathComponentArrayWithCurrentUrl:Media::RecordingModel::instance().recordPath().toNSString()];
64 [recordingsPathControl setPathComponentCells:pathComponentArray];
Alexandre Lision611b8c12015-03-20 18:07:17 -040065}
66
67- (IBAction)toggleMuteDTMF:(NSButton *)sender
68{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040069 Audio::Settings::instance().setDTMFMuted([sender state] == NSOnState);
Alexandre Lision611b8c12015-03-20 18:07:17 -040070}
71
72- (IBAction)toggleAlwaysRecording:(NSButton *)sender
73{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040074 Media::RecordingModel::instance().setAlwaysRecording([sender state] == NSOnState);
Alexandre Lision611b8c12015-03-20 18:07:17 -040075}
76
77- (IBAction)pathControlSingleClick:(id)sender {
78 // Select that chosen component of the path.
Kateryna Kostiuk4138db12018-06-08 15:52:18 -040079 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 Lision611b8c12015-03-20 18:07:17 -040082}
83
84- (IBAction)chooseOutput:(id)sender {
85 int index = [sender indexOfSelectedItem];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040086 QModelIndex qIdx = Audio::Settings::instance().outputDeviceModel()->index(index, 0);
87 Audio::Settings::instance().outputDeviceModel()->selectionModel()->setCurrentIndex(
Alexandre Lision611b8c12015-03-20 18:07:17 -040088 qIdx, QItemSelectionModel::ClearAndSelect);
89}
90
91- (IBAction)chooseInput:(id)sender {
92 int index = [sender indexOfSelectedItem];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040093 QModelIndex qIdx = Audio::Settings::instance().inputDeviceModel()->index(index, 0);
94 Audio::Settings::instance().inputDeviceModel()->selectionModel()->setCurrentIndex(
Alexandre Lision611b8c12015-03-20 18:07:17 -040095 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 Kostiuk4138db12018-06-08 15:52:18 -0400103- (NSArray *)pathComponentArrayWithCurrentUrl:(NSString *) url
Alexandre Lision611b8c12015-03-20 18:07:17 -0400104{
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400105
Alexandre Lision611b8c12015-03-20 18:07:17 -0400106 NSMutableArray *pathComponentArray = [[NSMutableArray alloc] init];
107
108 NSFileManager *fileManager = [[NSFileManager alloc] init];
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400109 NSURL* downloadURL = [fileManager URLForDirectory:NSDownloadsDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
Alexandre Lision611b8c12015-03-20 18:07:17 -0400110
111 NSPathComponentCell *componentCell;
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400112 componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Downloads" URL:downloadURL];
Alexandre Lision611b8c12015-03-20 18:07:17 -0400113 [pathComponentArray addObject:componentCell];
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400114 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 Kostiuk4138db12018-06-08 15:52:18 -0400125 }
Kateryna Kostiuke184f2e2018-06-11 15:31:31 -0400126 return pathComponentArray;
Alexandre Lision611b8c12015-03-20 18:07:17 -0400127}
128
129/*
Kateryna Kostiuke184f2e2018-06-11 15:31:31 -0400130 This method is used by pathComponentArray to create a NSPathComponent cell based on icon, title and URL information.
Alexandre Lision611b8c12015-03-20 18:07:17 -0400131 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 Lisionc8180112016-01-27 11:27:50 -0500155 [openPanel setTitle:NSLocalizedString(@"Choose a directory", @"Open panel title")];
156 [openPanel setPrompt:NSLocalizedString(@"Choose directory", @"Open panel prompt for 'Choose a directory'")];
Alexandre Lision611b8c12015-03-20 18:07:17 -0400157 [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 Kostiuk4138db12018-06-08 15:52:18 -0400173- (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url {
174 if(!appSandboxed()) {
175 return YES;
176 }
177 return isUrlAccessibleFromSandbox(url);
178}
179
Alexandre Lision611b8c12015-03-20 18:07:17 -0400180#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 Lision51ff1492016-04-15 11:38:37 -0400186 if (inputDeviceList.menu == menu) {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400187 qIdx = Audio::Settings::instance().inputDeviceModel()->index(index);
188 [item setTitle:Audio::Settings::instance().inputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision51ff1492016-04-15 11:38:37 -0400189 } else {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400190 qIdx = Audio::Settings::instance().outputDeviceModel()->index(index);
191 [item setTitle:Audio::Settings::instance().outputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision611b8c12015-03-20 18:07:17 -0400192 }
193
194 return YES;
195}
196
197- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
198{
Alexandre Lision51ff1492016-04-15 11:38:37 -0400199 if (inputDeviceList.menu == menu)
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400200 return Audio::Settings::instance().inputDeviceModel()->rowCount();
Alexandre Lision611b8c12015-03-20 18:07:17 -0400201 else
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400202 return Audio::Settings::instance().outputDeviceModel()->rowCount();
Alexandre Lision611b8c12015-03-20 18:07:17 -0400203}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500204
205@end