blob: da785440ae1c279933e37c3a637e58e08e449638 [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 "GeneralPrefsVC.h"
20
Kateryna Kostiuke3503842018-12-12 16:39:45 -050021//lrc
Kateryna Kostiuk67735232018-05-10 15:05:32 -040022#import <api/datatransfermodel.h>
Alexandre Lisione4041492015-03-20 18:20:43 -040023
Alexandre Lision3d4143a2015-06-10 14:27:49 -040024#if ENABLE_SPARKLE
25#import <Sparkle/Sparkle.h>
26#endif
27
Alexandre Lisionc65310c2015-04-23 16:44:23 -040028#import "Constants.h"
Kateryna Kostiuk4138db12018-06-08 15:52:18 -040029#import "utils.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040030
Alexandre Lision261f1b92016-04-04 12:35:34 -040031@interface GeneralPrefsVC () {
Alexandre Lision261f1b92016-04-04 12:35:34 -040032 __unsafe_unretained IBOutlet NSButton* startUpButton;
33 __unsafe_unretained IBOutlet NSButton* toggleAutomaticUpdateCheck;
34 __unsafe_unretained IBOutlet NSPopUpButton* checkIntervalPopUp;
35 __unsafe_unretained IBOutlet NSView* sparkleContainer;
Kateryna Kostiuk67735232018-05-10 15:05:32 -040036 __unsafe_unretained IBOutlet NSButton *downloadFolder;
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -040037 __unsafe_unretained IBOutlet NSTextField *downloadFolderLabel;
Alexandre Lision261f1b92016-04-04 12:35:34 -040038}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050039@end
40
Alexandre Lision4de68ce2015-04-24 18:22:49 -040041@implementation GeneralPrefsVC
Alexandre Lisione4041492015-03-20 18:20:43 -040042
Kateryna Kostiuk67735232018-05-10 15:05:32 -040043@synthesize dataTransferModel;
44
45
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040046-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dataTransferModel:(lrc::api::DataTransferModel*) dataTransferModel
Kateryna Kostiuk67735232018-05-10 15:05:32 -040047{
48 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
49 {
50 self.dataTransferModel = dataTransferModel;
51 }
52 return self;
53}
54
Alexandre Lisione4041492015-03-20 18:20:43 -040055- (void)loadView
56{
57 [super loadView];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020058 [startUpButton setState:[self isLaunchAtStartup]];
Alexandre Lision3d4143a2015-06-10 14:27:49 -040059#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 Kostiuk74fe20c2018-06-14 12:05:53 -040069 if (appSandboxed()) {
70 [downloadFolder setHidden:YES];
71 [downloadFolder setEnabled:NO];
72 [downloadFolderLabel setHidden: YES];
73 return;
74 }
Kateryna Kostiuk67735232018-05-10 15:05:32 -040075 if (dataTransferModel) {
76 downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent];
77 }
Alexandre Lisione4041492015-03-20 18:20:43 -040078}
79
Kateryna Kostiuk67735232018-05-10 15:05:32 -040080- (IBAction)changeDownloadFolder:(id)sender {
81
82 NSOpenPanel *panel = [NSOpenPanel openPanel];
83 [panel setAllowsMultipleSelection:NO];
84 [panel setCanChooseDirectories:YES];
85 [panel setCanChooseFiles:NO];
Kateryna Kostiuk4138db12018-06-08 15:52:18 -040086 panel.delegate = self;
Kateryna Kostiuk67735232018-05-10 15:05:32 -040087 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 Lision1ad5a2f2015-05-27 14:21:45 +020095#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 Lision81c97212015-06-17 15:51:53 -0400117 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200118 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 Lision81c97212015-06-17 15:51:53 -0400132 CFURLRef itemUrl = nil;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200133
134 // Get the app's URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400135 auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200136 // Get the LoginItems list.
137 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
138 if (loginItemsRef == nil) return nil;
139 // Iterate over the LoginItems.
Alexandre Lision81c97212015-06-17 15:51:53 -0400140 NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200141 for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) {
142 // Get the current LoginItem and resolve its URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400143 LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex];
144 if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200145 // Compare the URLs for the current LoginItem and the app.
Alexandre Lision81c97212015-06-17 15:51:53 -0400146 if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200147 // 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 Lision1ad5a2f2015-05-27 14:21:45 +0200155 CFRelease(loginItemsRef);
156
157 return itemRef;
158}
159
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400160#pragma mark - NSOpenSavePanelDelegate delegate methods
161
162- (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url {
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -0400163 return YES;
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400164}
165
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500166@end