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 "GeneralPrefsVC.h" |
| 20 | |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 21 | //lrc |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 22 | #import <api/datatransfermodel.h> |
Kateryna Kostiuk | 6db5280 | 2019-08-12 17:56:38 -0400 | [diff] [blame] | 23 | #import <api/avmodel.h> |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 24 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 25 | #if ENABLE_SPARKLE |
| 26 | #import <Sparkle/Sparkle.h> |
| 27 | #endif |
| 28 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 29 | #import "Constants.h" |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 30 | #import "utils.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 31 | |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 32 | @interface GeneralPrefsVC () { |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 33 | __unsafe_unretained IBOutlet NSButton* startUpButton; |
| 34 | __unsafe_unretained IBOutlet NSButton* toggleAutomaticUpdateCheck; |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 35 | __unsafe_unretained IBOutlet NSButton* alwaysRecording; |
| 36 | __unsafe_unretained IBOutlet NSButton* recordPreview; |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 37 | __unsafe_unretained IBOutlet NSPopUpButton* checkIntervalPopUp; |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 38 | __unsafe_unretained IBOutlet NSStackView* sparkleContainer; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 39 | __unsafe_unretained IBOutlet NSButton *downloadFolder; |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 40 | __unsafe_unretained IBOutlet NSTextField *downloadFolderLabel; |
Kateryna Kostiuk | 6db5280 | 2019-08-12 17:56:38 -0400 | [diff] [blame] | 41 | __unsafe_unretained IBOutlet NSButton *recordingFolder; |
| 42 | __unsafe_unretained IBOutlet NSTextField *recordingFolderLabel; |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 43 | __unsafe_unretained IBOutlet NSStackView *generalStackView; |
| 44 | __unsafe_unretained IBOutlet NSStackView *conversationStackView; |
| 45 | __unsafe_unretained IBOutlet NSStackView *recordingFolderStackView; |
| 46 | __unsafe_unretained IBOutlet NSSlider *qualitySlider; |
| 47 | __unsafe_unretained IBOutlet NSTextField *qualityLabel; |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 48 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 49 | @end |
| 50 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 51 | @implementation GeneralPrefsVC |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 52 | |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 53 | @synthesize dataTransferModel; |
Kateryna Kostiuk | 6db5280 | 2019-08-12 17:56:38 -0400 | [diff] [blame] | 54 | @synthesize avModel; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 55 | |
| 56 | |
Kateryna Kostiuk | 6db5280 | 2019-08-12 17:56:38 -0400 | [diff] [blame] | 57 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dataTransferModel:(lrc::api::DataTransferModel*) dataTransferModel avModel:(lrc::api::AVModel*) avModel { |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 58 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 59 | { |
| 60 | self.dataTransferModel = dataTransferModel; |
Kateryna Kostiuk | 6db5280 | 2019-08-12 17:56:38 -0400 | [diff] [blame] | 61 | self.avModel = avModel; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 62 | } |
| 63 | return self; |
| 64 | } |
| 65 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 66 | - (void)loadView |
| 67 | { |
| 68 | [super loadView]; |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 69 | CGFloat heightToReduice = 0; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 70 | [startUpButton setState:[self isLaunchAtStartup]]; |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 71 | #if ENABLE_SPARKLE |
| 72 | [sparkleContainer setHidden:NO]; |
| 73 | SUUpdater *updater = [SUUpdater sharedUpdater]; |
| 74 | [toggleAutomaticUpdateCheck bind:@"value" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil]; |
| 75 | |
| 76 | [checkIntervalPopUp bind:@"enabled" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil]; |
| 77 | [checkIntervalPopUp bind:@"selectedTag" toObject:updater withKeyPath:@"updateCheckInterval" options:nil]; |
| 78 | #else |
| 79 | [sparkleContainer setHidden:YES]; |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 80 | heightToReduice += (sparkleContainer.frame.size.height + 25); |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 81 | #endif |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 82 | [alwaysRecording setState: avModel->getAlwaysRecord()]; |
| 83 | [recordPreview setState: avModel->getRecordPreview()]; |
| 84 | [qualitySlider setDoubleValue: avModel->getRecordQuality()]; |
| 85 | [qualityLabel setIntValue:avModel->getRecordQuality()]; |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 86 | if (appSandboxed()) { |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 87 | [recordingFolderStackView setHidden:YES]; |
| 88 | [conversationStackView setHidden:YES]; |
| 89 | heightToReduice += (downloadFolder.frame.size.height + recordingFolder.frame.size.height + 25); |
| 90 | } else { |
| 91 | if (dataTransferModel) { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 92 | downloadFolder.title = [dataTransferModel->downloadDirectory.toNSString() lastPathComponent]; |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 93 | } |
| 94 | if (avModel) { |
| 95 | auto name1 = avModel->getRecordPath(); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 96 | auto name = avModel->getRecordPath().toNSString(); |
| 97 | recordingFolder.title = [avModel->getRecordPath().toNSString() lastPathComponent]; |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 98 | } |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 99 | } |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 100 | auto frame = self.view.frame; |
| 101 | frame.size.height -= heightToReduice; |
| 102 | self.view.frame = frame; |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 105 | - (IBAction)changeDownloadFolder:(id)sender { |
| 106 | |
| 107 | NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 108 | [panel setAllowsMultipleSelection:NO]; |
| 109 | [panel setCanChooseDirectories:YES]; |
| 110 | [panel setCanChooseFiles:NO]; |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 111 | panel.delegate = self; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 112 | if ([panel runModal] != NSFileHandlingPanelOKButton) return; |
| 113 | if ([[panel URLs] lastObject] == nil) return; |
| 114 | NSString * path = [[[[panel URLs] lastObject] path] stringByAppendingString:@"/"]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 115 | dataTransferModel->downloadDirectory = QString::fromNSString(path); |
| 116 | downloadFolder.title = [dataTransferModel->downloadDirectory.toNSString() lastPathComponent]; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 117 | [[NSUserDefaults standardUserDefaults] setObject:path forKey:Preferences::DownloadFolder]; |
| 118 | } |
| 119 | |
Kateryna Kostiuk | 6db5280 | 2019-08-12 17:56:38 -0400 | [diff] [blame] | 120 | - (IBAction)changeRecordingFolder:(id)sender { |
| 121 | NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 122 | [panel setAllowsMultipleSelection:NO]; |
| 123 | [panel setCanChooseDirectories:YES]; |
| 124 | [panel setCanChooseFiles:NO]; |
| 125 | panel.delegate = self; |
| 126 | if ([panel runModal] != NSFileHandlingPanelOKButton) return; |
| 127 | if ([[panel URLs] lastObject] == nil) return; |
| 128 | NSString * path = [[[[panel URLs] lastObject] path] stringByAppendingString:@"/"]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 129 | avModel->setRecordPath(QString::fromNSString(path)); |
| 130 | recordingFolder.title = [avModel->getRecordPath().toNSString() lastPathComponent]; |
Kateryna Kostiuk | 6db5280 | 2019-08-12 17:56:38 -0400 | [diff] [blame] | 131 | [[NSUserDefaults standardUserDefaults] setObject:path forKey:Preferences::DownloadFolder]; |
| 132 | } |
| 133 | |
Kateryna Kostiuk | beb9bae | 2019-08-21 17:59:16 -0400 | [diff] [blame] | 134 | - (IBAction)alwaysRecording:(id)sender { |
| 135 | avModel->setAlwaysRecord([sender state]); |
| 136 | } |
| 137 | |
| 138 | - (IBAction)recordPreview:(id)sender { |
| 139 | avModel->setRecordPreview([sender state]); |
| 140 | } |
| 141 | |
| 142 | - (IBAction)setRecordingQuality:(NSSlider*)sender { |
| 143 | avModel->setRecordQuality([sender intValue]); |
| 144 | } |
| 145 | |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 146 | #pragma mark - Startup API |
| 147 | |
| 148 | // MIT license by Brian Dunagan |
| 149 | - (BOOL)isLaunchAtStartup { |
| 150 | // See if the app is currently in LoginItems. |
| 151 | LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; |
| 152 | // Store away that boolean. |
| 153 | BOOL isInList = itemRef != nil; |
| 154 | // Release the reference if it exists. |
| 155 | if (itemRef != nil) CFRelease(itemRef); |
| 156 | |
| 157 | return isInList; |
| 158 | } |
| 159 | |
| 160 | - (IBAction)toggleLaunchAtStartup:(id)sender { |
| 161 | // Toggle the state. |
| 162 | BOOL shouldBeToggled = ![self isLaunchAtStartup]; |
| 163 | // Get the LoginItems list. |
| 164 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 165 | if (loginItemsRef == nil) return; |
| 166 | if (shouldBeToggled) { |
| 167 | // Add the app to the LoginItems list. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 168 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 169 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 170 | if (itemRef) CFRelease(itemRef); |
| 171 | } |
| 172 | else { |
| 173 | // Remove the app from the LoginItems list. |
| 174 | LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; |
| 175 | LSSharedFileListItemRemove(loginItemsRef,itemRef); |
| 176 | if (itemRef != nil) CFRelease(itemRef); |
| 177 | } |
| 178 | CFRelease(loginItemsRef); |
| 179 | } |
| 180 | |
| 181 | - (LSSharedFileListItemRef)itemRefInLoginItems { |
| 182 | LSSharedFileListItemRef itemRef = nil; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 183 | CFURLRef itemUrl = nil; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 184 | |
| 185 | // Get the app's URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 186 | auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 187 | // Get the LoginItems list. |
| 188 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 189 | if (loginItemsRef == nil) return nil; |
| 190 | // Iterate over the LoginItems. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 191 | NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil); |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 192 | for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) { |
| 193 | // Get the current LoginItem and resolve its URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 194 | LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex]; |
| 195 | if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 196 | // Compare the URLs for the current LoginItem and the app. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 197 | if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 198 | // Save the LoginItem reference. |
| 199 | itemRef = currentItemRef; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | // Retain the LoginItem reference. |
| 204 | if (itemRef != nil) CFRetain(itemRef); |
| 205 | // Release the LoginItems lists. |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 206 | CFRelease(loginItemsRef); |
| 207 | |
| 208 | return itemRef; |
| 209 | } |
| 210 | |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 211 | #pragma mark - NSOpenSavePanelDelegate delegate methods |
| 212 | |
| 213 | - (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url { |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 214 | return YES; |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 217 | @end |