blob: 17c3a1f6956d8fa368b62454a65b8bca09c16771 [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 Lisiond3aa3ad2015-10-23 14:28:41 -040063 if([[Media::RecordingModel::instance().recordPath().toNSURL() absoluteString] isEqualToString:@""]) {
Alexandre Lision611b8c12015-03-20 18:07:17 -040064 NSArray * pathComponentArray = [self pathComponentArray];
65 [recordingsPathControl setPathComponentCells:pathComponentArray];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040066 } else {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040067 [recordingsPathControl setURL:Media::RecordingModel::instance().recordPath().toNSURL()];
Alexandre Lision611b8c12015-03-20 18:07:17 -040068 }
69}
70
71- (IBAction)toggleMuteDTMF:(NSButton *)sender
72{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040073 Audio::Settings::instance().setDTMFMuted([sender state] == NSOnState);
Alexandre Lision611b8c12015-03-20 18:07:17 -040074}
75
76- (IBAction)toggleAlwaysRecording:(NSButton *)sender
77{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040078 Media::RecordingModel::instance().setAlwaysRecording([sender state] == NSOnState);
Alexandre Lision611b8c12015-03-20 18:07:17 -040079}
80
81- (IBAction)pathControlSingleClick:(id)sender {
82 // Select that chosen component of the path.
83 [self.recordingsPathControl setURL:[[self.recordingsPathControl clickedPathComponentCell] URL]];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040084 Media::RecordingModel::instance().setRecordPath(QUrl::fromNSURL(self.recordingsPathControl.URL));
Alexandre Lision611b8c12015-03-20 18:07:17 -040085}
86
87- (IBAction)chooseOutput:(id)sender {
88 int index = [sender indexOfSelectedItem];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040089 QModelIndex qIdx = Audio::Settings::instance().outputDeviceModel()->index(index, 0);
90 Audio::Settings::instance().outputDeviceModel()->selectionModel()->setCurrentIndex(
Alexandre Lision611b8c12015-03-20 18:07:17 -040091 qIdx, QItemSelectionModel::ClearAndSelect);
92}
93
94- (IBAction)chooseInput:(id)sender {
95 int index = [sender indexOfSelectedItem];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040096 QModelIndex qIdx = Audio::Settings::instance().inputDeviceModel()->index(index, 0);
97 Audio::Settings::instance().inputDeviceModel()->selectionModel()->setCurrentIndex(
Alexandre Lision611b8c12015-03-20 18:07:17 -040098 qIdx, QItemSelectionModel::ClearAndSelect);
99}
100
101#pragma mark - NSPathControl delegate methods
102
103/*
104 Assemble a set of custom cells to display into an array to pass to the path control.
105 */
106- (NSArray *)pathComponentArray
107{
108 NSMutableArray *pathComponentArray = [[NSMutableArray alloc] init];
109
110 NSFileManager *fileManager = [[NSFileManager alloc] init];
111
112 NSURL* desktopURL = [fileManager URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
113 NSURL* documentsURL = [fileManager URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
114 NSURL* userURL = [fileManager URLForDirectory:NSUserDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
115
116 NSPathComponentCell *componentCell;
117
118 // Use utility method to obtain a NSPathComponentCell based on icon, title and URL.
119 componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Desktop" URL:desktopURL];
120 [pathComponentArray addObject:componentCell];
121
122 componentCell = [self componentCellForType:kGenericFolderIcon withTitle:@"Documents" URL:documentsURL];
123 [pathComponentArray addObject:componentCell];
124
125 componentCell = [self componentCellForType:kUserFolderIcon withTitle:NSUserName() URL:userURL];
126 [pathComponentArray addObject:componentCell];
127
128 return pathComponentArray;
129}
130
131/*
132 This method is used by pathComponentArray to create a NSPathComponent cell based on icon, title and URL information.
133 Each path component needs an icon, URL and title.
134 */
135- (NSPathComponentCell *)componentCellForType:(OSType)withIconType withTitle:(NSString *)title URL:(NSURL *)url
136{
137 NSPathComponentCell *componentCell = [[NSPathComponentCell alloc] init];
138
139 NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(withIconType)];
140 [componentCell setImage:iconImage];
141 [componentCell setURL:url];
142 [componentCell setTitle:title];
143
144 return componentCell;
145}
146
147/*
148 Delegate method of NSPathControl to determine how the NSOpenPanel will look/behave.
149 */
150- (void)pathControl:(NSPathControl *)pathControl willDisplayOpenPanel:(NSOpenPanel *)openPanel
151{
152 NSLog(@"willDisplayOpenPanel");
153 [openPanel setAllowsMultipleSelection:NO];
154 [openPanel setCanChooseDirectories:YES];
155 [openPanel setCanChooseFiles:NO];
156 [openPanel setResolvesAliases:YES];
157 [openPanel setTitle:NSLocalizedString(@"Choose a file", @"Open panel title")];
Alexandre Lision922380d2015-09-15 10:25:17 -0400158 [openPanel setPrompt:NSLocalizedString(@"Choose file", @"Open panel prompt for 'Choose a directory'")];
Alexandre Lision611b8c12015-03-20 18:07:17 -0400159 [openPanel setDelegate:self];
160}
161
162- (void)pathControl:(NSPathControl *)pathControl willPopUpMenu:(NSMenu *)menu
163{
164
165}
166
167#pragma mark - NSOpenSavePanelDelegate delegate methods
168
169- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError **)outError
170{
171 [recordingsPathControl setURL:url];
172 return YES;
173}
174
175#pragma mark - NSMenuDelegate methods
176
177- (BOOL)menu:(NSMenu *)menu updateItem:(NSMenuItem *)item atIndex:(NSInteger)index shouldCancel:(BOOL)shouldCancel
178{
179 QModelIndex qIdx;
180
181 if([menu.title isEqualToString:@"inputlist"])
182 {
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 Lision611b8c12015-03-20 18:07:17 -0400185 } else
186 {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400187 qIdx = Audio::Settings::instance().outputDeviceModel()->index(index);
188 [item setTitle:Audio::Settings::instance().outputDeviceModel()->data(qIdx, Qt::DisplayRole).toString().toNSString()];
Alexandre Lision611b8c12015-03-20 18:07:17 -0400189 }
190
191 return YES;
192}
193
194- (NSInteger)numberOfItemsInMenu:(NSMenu *)menu
195{
196 if([menu.title isEqualToString:@"inputlist"])
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400197 return Audio::Settings::instance().inputDeviceModel()->rowCount();
Alexandre Lision611b8c12015-03-20 18:07:17 -0400198 else
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400199 return Audio::Settings::instance().outputDeviceModel()->rowCount();
Alexandre Lision611b8c12015-03-20 18:07:17 -0400200}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500201
202@end