blob: f83f5bc0f90ebb19436e7ebaff8f3b9c57fc351d [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>
27
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050028@interface AudioPrefsVC ()
29
Alexandre Lision611b8c12015-03-20 18:07:17 -040030@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 Lision4a7b95e2015-02-20 10:06:43 -050036@end
37
38@implementation AudioPrefsVC
Alexandre Lision611b8c12015-03-20 18:07:17 -040039@synthesize recordingsPathControl;
40@synthesize outputDeviceList;
41@synthesize inputDeviceList;
42@synthesize alwaysRecordingButton;
43@synthesize muteDTMFButton;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050044
Alexandre Lision611b8c12015-03-20 18:07:17 -040045- (void)loadView
46{
47 [super loadView];
48
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040049 QModelIndex qInputIdx = Audio::Settings::instance().inputDeviceModel()->selectionModel()->currentIndex();
50 QModelIndex qOutputIdx = Audio::Settings::instance().outputDeviceModel()->selectionModel()->currentIndex();
Alexandre Lision611b8c12015-03-20 18:07:17 -040051
52 [self.outputDeviceList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040053 Audio::Settings::instance().outputDeviceModel()->data(qOutputIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision611b8c12015-03-20 18:07:17 -040054
55 [self.inputDeviceList addItemWithTitle:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040056 Audio::Settings::instance().inputDeviceModel()->data(qInputIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision611b8c12015-03-20 18:07:17 -040057 [self.alwaysRecordingButton setState:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040058 Media::RecordingModel::instance().isAlwaysRecording() ? NSOnState:NSOffState];
Alexandre Lision611b8c12015-03-20 18:07:17 -040059
60 [self.muteDTMFButton setState:
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040061 Audio::Settings::instance().areDTMFMuted()?NSOnState:NSOffState];
Alexandre Lision611b8c12015-03-20 18:07:17 -040062
Alexandre Lisione4666d12016-02-05 17:12:23 -050063 if([Media::RecordingModel::instance().recordPath().toNSString() isEqualToString:@""]) {
64 NSArray* pathComponentArray = [self pathComponentArray];
Alexandre Lision611b8c12015-03-20 18:07:17 -040065 [recordingsPathControl setPathComponentCells:pathComponentArray];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040066 } else {
Alexandre Lisione4666d12016-02-05 17:12:23 -050067 [recordingsPathControl setURL:
68 [NSURL URLWithString:Media::RecordingModel::instance().recordPath().toNSString()]];
Alexandre Lision611b8c12015-03-20 18:07:17 -040069 }
70}
71
72- (IBAction)toggleMuteDTMF:(NSButton *)sender
73{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040074 Audio::Settings::instance().setDTMFMuted([sender state] == NSOnState);
Alexandre Lision611b8c12015-03-20 18:07:17 -040075}
76
77- (IBAction)toggleAlwaysRecording:(NSButton *)sender
78{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040079 Media::RecordingModel::instance().setAlwaysRecording([sender state] == NSOnState);
Alexandre Lision611b8c12015-03-20 18:07:17 -040080}
81
82- (IBAction)pathControlSingleClick:(id)sender {
83 // Select that chosen component of the path.
84 [self.recordingsPathControl setURL:[[self.recordingsPathControl clickedPathComponentCell] URL]];
Alexandre Lisione4666d12016-02-05 17:12:23 -050085 Media::RecordingModel::instance().setRecordPath(QString::fromNSString([self.recordingsPathControl.URL path]));
Alexandre Lision611b8c12015-03-20 18:07:17 -040086}
87
88- (IBAction)chooseOutput:(id)sender {
89 int index = [sender indexOfSelectedItem];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040090 QModelIndex qIdx = Audio::Settings::instance().outputDeviceModel()->index(index, 0);
91 Audio::Settings::instance().outputDeviceModel()->selectionModel()->setCurrentIndex(
Alexandre Lision611b8c12015-03-20 18:07:17 -040092 qIdx, QItemSelectionModel::ClearAndSelect);
93}
94
95- (IBAction)chooseInput:(id)sender {
96 int index = [sender indexOfSelectedItem];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040097 QModelIndex qIdx = Audio::Settings::instance().inputDeviceModel()->index(index, 0);
98 Audio::Settings::instance().inputDeviceModel()->selectionModel()->setCurrentIndex(
Alexandre Lision611b8c12015-03-20 18:07:17 -040099 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 Lisionc8180112016-01-27 11:27:50 -0500158 [openPanel setTitle:NSLocalizedString(@"Choose a directory", @"Open panel title")];
159 [openPanel setPrompt:NSLocalizedString(@"Choose directory", @"Open panel prompt for 'Choose a directory'")];
Alexandre Lision611b8c12015-03-20 18:07:17 -0400160 [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 Lision51ff1492016-04-15 11:38:37 -0400182 if (inputDeviceList.menu == menu) {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400183 qIdx = Audio::Settings::instance().inputDeviceModel()->index(index);
184 [item setTitle:Audio::Settings::instance().inputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision51ff1492016-04-15 11:38:37 -0400185 } else {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400186 qIdx = Audio::Settings::instance().outputDeviceModel()->index(index);
187 [item setTitle:Audio::Settings::instance().outputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision611b8c12015-03-20 18:07:17 -0400188 }
189
190 return YES;
191}
192
193- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
194{
Alexandre Lision51ff1492016-04-15 11:38:37 -0400195 if (inputDeviceList.menu == menu)
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400196 return Audio::Settings::instance().inputDeviceModel()->rowCount();
Alexandre Lision611b8c12015-03-20 18:07:17 -0400197 else
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400198 return Audio::Settings::instance().outputDeviceModel()->rowCount();
Alexandre Lision611b8c12015-03-20 18:07:17 -0400199}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500200
201@end