blob: c6ea2d97a8d80bc3cab82a9f6d0a914aef3e4a4c [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>
Kateryna Kostiuk6db52802019-08-12 17:56:38 -040023#import <api/avmodel.h>
Alexandre Lisione4041492015-03-20 18:20:43 -040024
Alexandre Lision3d4143a2015-06-10 14:27:49 -040025#if ENABLE_SPARKLE
26#import <Sparkle/Sparkle.h>
27#endif
28
Alexandre Lisionc65310c2015-04-23 16:44:23 -040029#import "Constants.h"
Kateryna Kostiuk4138db12018-06-08 15:52:18 -040030#import "utils.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040031
Alexandre Lision261f1b92016-04-04 12:35:34 -040032@interface GeneralPrefsVC () {
Alexandre Lision261f1b92016-04-04 12:35:34 -040033 __unsafe_unretained IBOutlet NSButton* startUpButton;
34 __unsafe_unretained IBOutlet NSButton* toggleAutomaticUpdateCheck;
35 __unsafe_unretained IBOutlet NSPopUpButton* checkIntervalPopUp;
36 __unsafe_unretained IBOutlet NSView* sparkleContainer;
Kateryna Kostiuk67735232018-05-10 15:05:32 -040037 __unsafe_unretained IBOutlet NSButton *downloadFolder;
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -040038 __unsafe_unretained IBOutlet NSTextField *downloadFolderLabel;
Kateryna Kostiuk6db52802019-08-12 17:56:38 -040039 __unsafe_unretained IBOutlet NSButton *recordingFolder;
40 __unsafe_unretained IBOutlet NSTextField *recordingFolderLabel;
Alexandre Lision261f1b92016-04-04 12:35:34 -040041}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050042@end
43
Alexandre Lision4de68ce2015-04-24 18:22:49 -040044@implementation GeneralPrefsVC
Alexandre Lisione4041492015-03-20 18:20:43 -040045
Kateryna Kostiuk67735232018-05-10 15:05:32 -040046@synthesize dataTransferModel;
Kateryna Kostiuk6db52802019-08-12 17:56:38 -040047@synthesize avModel;
Kateryna Kostiuk67735232018-05-10 15:05:32 -040048
49
Kateryna Kostiuk6db52802019-08-12 17:56:38 -040050-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil dataTransferModel:(lrc::api::DataTransferModel*) dataTransferModel avModel:(lrc::api::AVModel*) avModel {
Kateryna Kostiuk67735232018-05-10 15:05:32 -040051 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
52 {
53 self.dataTransferModel = dataTransferModel;
Kateryna Kostiuk6db52802019-08-12 17:56:38 -040054 self.avModel = avModel;
Kateryna Kostiuk67735232018-05-10 15:05:32 -040055 }
56 return self;
57}
58
Alexandre Lisione4041492015-03-20 18:20:43 -040059- (void)loadView
60{
61 [super loadView];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020062 [startUpButton setState:[self isLaunchAtStartup]];
Alexandre Lision3d4143a2015-06-10 14:27:49 -040063#if ENABLE_SPARKLE
64 [sparkleContainer setHidden:NO];
65 SUUpdater *updater = [SUUpdater sharedUpdater];
66 [toggleAutomaticUpdateCheck bind:@"value" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
67
68 [checkIntervalPopUp bind:@"enabled" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
69 [checkIntervalPopUp bind:@"selectedTag" toObject:updater withKeyPath:@"updateCheckInterval" options:nil];
70#else
71 [sparkleContainer setHidden:YES];
72#endif
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -040073 if (appSandboxed()) {
74 [downloadFolder setHidden:YES];
75 [downloadFolder setEnabled:NO];
76 [downloadFolderLabel setHidden: YES];
Kateryna Kostiuk6db52802019-08-12 17:56:38 -040077 [recordingFolder setHidden:YES];
78 [recordingFolder setEnabled:NO];
79 [recordingFolderLabel setHidden:YES];
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -040080 return;
81 }
Kateryna Kostiuk67735232018-05-10 15:05:32 -040082 if (dataTransferModel) {
83 downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent];
84 }
Kateryna Kostiuk6db52802019-08-12 17:56:38 -040085 if (avModel) {
86 auto name1 = avModel->getRecordPath();
87 auto name = @(avModel->getRecordPath().c_str());
88 recordingFolder.title = [@(avModel->getRecordPath().c_str()) lastPathComponent];
89 }
Alexandre Lisione4041492015-03-20 18:20:43 -040090}
91
Kateryna Kostiuk67735232018-05-10 15:05:32 -040092- (IBAction)changeDownloadFolder:(id)sender {
93
94 NSOpenPanel *panel = [NSOpenPanel openPanel];
95 [panel setAllowsMultipleSelection:NO];
96 [panel setCanChooseDirectories:YES];
97 [panel setCanChooseFiles:NO];
Kateryna Kostiuk4138db12018-06-08 15:52:18 -040098 panel.delegate = self;
Kateryna Kostiuk67735232018-05-10 15:05:32 -040099 if ([panel runModal] != NSFileHandlingPanelOKButton) return;
100 if ([[panel URLs] lastObject] == nil) return;
101 NSString * path = [[[[panel URLs] lastObject] path] stringByAppendingString:@"/"];
102 dataTransferModel->downloadDirectory = std::string([path UTF8String]);
103 downloadFolder.title = [@(dataTransferModel->downloadDirectory.c_str()) lastPathComponent];
104 [[NSUserDefaults standardUserDefaults] setObject:path forKey:Preferences::DownloadFolder];
105}
106
Kateryna Kostiuk6db52802019-08-12 17:56:38 -0400107- (IBAction)changeRecordingFolder:(id)sender {
108 NSOpenPanel *panel = [NSOpenPanel openPanel];
109 [panel setAllowsMultipleSelection:NO];
110 [panel setCanChooseDirectories:YES];
111 [panel setCanChooseFiles:NO];
112 panel.delegate = self;
113 if ([panel runModal] != NSFileHandlingPanelOKButton) return;
114 if ([[panel URLs] lastObject] == nil) return;
115 NSString * path = [[[[panel URLs] lastObject] path] stringByAppendingString:@"/"];
116 avModel->setRecordPath([path UTF8String]);
117 recordingFolder.title = [@(avModel->getRecordPath().c_str()) lastPathComponent];
118 [[NSUserDefaults standardUserDefaults] setObject:path forKey:Preferences::DownloadFolder];
119}
120
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200121#pragma mark - Startup API
122
123// MIT license by Brian Dunagan
124- (BOOL)isLaunchAtStartup {
125 // See if the app is currently in LoginItems.
126 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
127 // Store away that boolean.
128 BOOL isInList = itemRef != nil;
129 // Release the reference if it exists.
130 if (itemRef != nil) CFRelease(itemRef);
131
132 return isInList;
133}
134
135- (IBAction)toggleLaunchAtStartup:(id)sender {
136 // Toggle the state.
137 BOOL shouldBeToggled = ![self isLaunchAtStartup];
138 // Get the LoginItems list.
139 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
140 if (loginItemsRef == nil) return;
141 if (shouldBeToggled) {
142 // Add the app to the LoginItems list.
Alexandre Lision81c97212015-06-17 15:51:53 -0400143 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200144 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
145 if (itemRef) CFRelease(itemRef);
146 }
147 else {
148 // Remove the app from the LoginItems list.
149 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
150 LSSharedFileListItemRemove(loginItemsRef,itemRef);
151 if (itemRef != nil) CFRelease(itemRef);
152 }
153 CFRelease(loginItemsRef);
154}
155
156- (LSSharedFileListItemRef)itemRefInLoginItems {
157 LSSharedFileListItemRef itemRef = nil;
Alexandre Lision81c97212015-06-17 15:51:53 -0400158 CFURLRef itemUrl = nil;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200159
160 // Get the app's URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400161 auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200162 // Get the LoginItems list.
163 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
164 if (loginItemsRef == nil) return nil;
165 // Iterate over the LoginItems.
Alexandre Lision81c97212015-06-17 15:51:53 -0400166 NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200167 for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) {
168 // Get the current LoginItem and resolve its URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400169 LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex];
170 if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200171 // Compare the URLs for the current LoginItem and the app.
Alexandre Lision81c97212015-06-17 15:51:53 -0400172 if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200173 // Save the LoginItem reference.
174 itemRef = currentItemRef;
175 }
176 }
177 }
178 // Retain the LoginItem reference.
179 if (itemRef != nil) CFRetain(itemRef);
180 // Release the LoginItems lists.
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200181 CFRelease(loginItemsRef);
182
183 return itemRef;
184}
185
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400186#pragma mark - NSOpenSavePanelDelegate delegate methods
187
188- (BOOL) panel:(id)sender shouldEnableURL:(NSURL*)url {
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -0400189 return YES;
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400190}
191
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500192@end