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> |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 23 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 24 | #if ENABLE_SPARKLE |
| 25 | #import <Sparkle/Sparkle.h> |
| 26 | #endif |
| 27 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 28 | #import "Constants.h" |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 29 | #import "utils.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 30 | |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 31 | @interface GeneralPrefsVC () { |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 32 | __unsafe_unretained IBOutlet NSButton* startUpButton; |
| 33 | __unsafe_unretained IBOutlet NSButton* toggleAutomaticUpdateCheck; |
| 34 | __unsafe_unretained IBOutlet NSPopUpButton* checkIntervalPopUp; |
| 35 | __unsafe_unretained IBOutlet NSView* sparkleContainer; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 36 | __unsafe_unretained IBOutlet NSButton *downloadFolder; |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 37 | __unsafe_unretained IBOutlet NSTextField *downloadFolderLabel; |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 38 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 39 | @end |
| 40 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 41 | @implementation GeneralPrefsVC |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 42 | |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 43 | @synthesize dataTransferModel; |
| 44 | |
| 45 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 46 | -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dataTransferModel:(lrc::api::DataTransferModel*) dataTransferModel |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 47 | { |
| 48 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 49 | { |
| 50 | self.dataTransferModel = dataTransferModel; |
| 51 | } |
| 52 | return self; |
| 53 | } |
| 54 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 55 | - (void)loadView |
| 56 | { |
| 57 | [super loadView]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 58 | [startUpButton setState:[self isLaunchAtStartup]]; |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 59 | #if ENABLE_SPARKLE |
| 60 | [sparkleContainer setHidden:NO]; |
| 61 | SUUpdater *updater = [SUUpdater sharedUpdater]; |
| 62 | [toggleAutomaticUpdateCheck bind:@"value" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil]; |
| 63 | |
| 64 | [checkIntervalPopUp bind:@"enabled" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil]; |
| 65 | [checkIntervalPopUp bind:@"selectedTag" toObject:updater withKeyPath:@"updateCheckInterval" options:nil]; |
| 66 | #else |
| 67 | [sparkleContainer setHidden:YES]; |
| 68 | #endif |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 69 | if (appSandboxed()) { |
| 70 | [downloadFolder setHidden:YES]; |
| 71 | [downloadFolder setEnabled:NO]; |
| 72 | [downloadFolderLabel setHidden: YES]; |
| 73 | return; |
| 74 | } |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 75 | if (dataTransferModel) { |
| 76 | downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent]; |
| 77 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 80 | - (IBAction)changeDownloadFolder:(id)sender { |
| 81 | |
| 82 | NSOpenPanel *panel = [NSOpenPanel openPanel]; |
| 83 | [panel setAllowsMultipleSelection:NO]; |
| 84 | [panel setCanChooseDirectories:YES]; |
| 85 | [panel setCanChooseFiles:NO]; |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 86 | panel.delegate = self; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 87 | if ([panel runModal] != NSFileHandlingPanelOKButton) return; |
| 88 | if ([[panel URLs] lastObject] == nil) return; |
| 89 | NSString * path = [[[[panel URLs] lastObject] path] stringByAppendingString:@"/"]; |
| 90 | dataTransferModel->downloadDirectory = std::string([path UTF8String]); |
| 91 | downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent]; |
| 92 | [[NSUserDefaults standardUserDefaults] setObject:path forKey:Preferences::DownloadFolder]; |
| 93 | } |
| 94 | |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 95 | #pragma mark - Startup API |
| 96 | |
| 97 | // MIT license by Brian Dunagan |
| 98 | - (BOOL)isLaunchAtStartup { |
| 99 | // See if the app is currently in LoginItems. |
| 100 | LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; |
| 101 | // Store away that boolean. |
| 102 | BOOL isInList = itemRef != nil; |
| 103 | // Release the reference if it exists. |
| 104 | if (itemRef != nil) CFRelease(itemRef); |
| 105 | |
| 106 | return isInList; |
| 107 | } |
| 108 | |
| 109 | - (IBAction)toggleLaunchAtStartup:(id)sender { |
| 110 | // Toggle the state. |
| 111 | BOOL shouldBeToggled = ![self isLaunchAtStartup]; |
| 112 | // Get the LoginItems list. |
| 113 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 114 | if (loginItemsRef == nil) return; |
| 115 | if (shouldBeToggled) { |
| 116 | // Add the app to the LoginItems list. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 117 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 118 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 119 | if (itemRef) CFRelease(itemRef); |
| 120 | } |
| 121 | else { |
| 122 | // Remove the app from the LoginItems list. |
| 123 | LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; |
| 124 | LSSharedFileListItemRemove(loginItemsRef,itemRef); |
| 125 | if (itemRef != nil) CFRelease(itemRef); |
| 126 | } |
| 127 | CFRelease(loginItemsRef); |
| 128 | } |
| 129 | |
| 130 | - (LSSharedFileListItemRef)itemRefInLoginItems { |
| 131 | LSSharedFileListItemRef itemRef = nil; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 132 | CFURLRef itemUrl = nil; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 133 | |
| 134 | // Get the app's URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 135 | auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 136 | // Get the LoginItems list. |
| 137 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 138 | if (loginItemsRef == nil) return nil; |
| 139 | // Iterate over the LoginItems. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 140 | NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil); |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 141 | for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) { |
| 142 | // Get the current LoginItem and resolve its URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 143 | LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex]; |
| 144 | if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 145 | // Compare the URLs for the current LoginItem and the app. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 146 | if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 147 | // Save the LoginItem reference. |
| 148 | itemRef = currentItemRef; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | // Retain the LoginItem reference. |
| 153 | if (itemRef != nil) CFRetain(itemRef); |
| 154 | // Release the LoginItems lists. |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 155 | CFRelease(loginItemsRef); |
| 156 | |
| 157 | return itemRef; |
| 158 | } |
| 159 | |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 160 | #pragma mark - NSOpenSavePanelDelegate delegate methods |
| 161 | |
| 162 | - (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url { |
Kateryna Kostiuk | 74fe20c | 2018-06-14 12:05:53 -0400 | [diff] [blame] | 163 | return YES; |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 166 | @end |