blob: 30c72bc78debd8db9b233405c069594f0917ddb8 [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
Alexandre Lisione4041492015-03-20 18:20:43 -040021#import <categorizedhistorymodel.h>
22
Alexandre Lision3d4143a2015-06-10 14:27:49 -040023#if ENABLE_SPARKLE
24#import <Sparkle/Sparkle.h>
25#endif
26
Alexandre Lisionc65310c2015-04-23 16:44:23 -040027#import "Constants.h"
28
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050029@interface GeneralPrefsVC ()
Alexandre Lision81c97212015-06-17 15:51:53 -040030@property (unsafe_unretained) IBOutlet NSTextField *historyChangedLabel;
31@property (unsafe_unretained) IBOutlet NSView *advancedGeneralSettings;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020032@property (unsafe_unretained) IBOutlet NSButton *startUpButton;
Alexandre Lision3d4143a2015-06-10 14:27:49 -040033@property (unsafe_unretained) IBOutlet NSButton *toggleAutomaticUpdateCheck;
34@property (unsafe_unretained) IBOutlet NSPopUpButton *checkIntervalPopUp;
35@property (unsafe_unretained) IBOutlet NSView *sparkleContainer;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050036
37@end
38
Alexandre Lision4de68ce2015-04-24 18:22:49 -040039@implementation GeneralPrefsVC
Alexandre Lisione4041492015-03-20 18:20:43 -040040@synthesize historyChangedLabel;
Alexandre Lision4de68ce2015-04-24 18:22:49 -040041@synthesize advancedGeneralSettings;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020042@synthesize startUpButton;
Alexandre Lision3d4143a2015-06-10 14:27:49 -040043@synthesize toggleAutomaticUpdateCheck;
44@synthesize checkIntervalPopUp;
45@synthesize sparkleContainer;
Alexandre Lisione4041492015-03-20 18:20:43 -040046
47- (void)loadView
48{
49 [super loadView];
Alexandre Lisionc65310c2015-04-23 16:44:23 -040050 [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040051 [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::ShowAdvanced options:NSKeyValueObservingOptionNew context:NULL];
52
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020053 [startUpButton setState:[self isLaunchAtStartup]];
54
Alexandre Lision3d4143a2015-06-10 14:27:49 -040055#if ENABLE_SPARKLE
56 [sparkleContainer setHidden:NO];
57 SUUpdater *updater = [SUUpdater sharedUpdater];
58 [toggleAutomaticUpdateCheck bind:@"value" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
59
60 [checkIntervalPopUp bind:@"enabled" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
61 [checkIntervalPopUp bind:@"selectedTag" toObject:updater withKeyPath:@"updateCheckInterval" options:nil];
62#else
63 [sparkleContainer setHidden:YES];
64#endif
65
Alexandre Lisiond0e628d2015-06-23 13:54:29 -040066 //[advancedGeneralSettings setHidden:![[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]];
Alexandre Lisione4041492015-03-20 18:20:43 -040067}
68
Alexandre Lision81c97212015-06-17 15:51:53 -040069- (void) dealloc
70{
71 [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit];
72 [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::ShowAdvanced];
73}
74
Alexandre Lisione4041492015-03-20 18:20:43 -040075- (IBAction)clearHistory:(id)sender {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040076 CategorizedHistoryModel::instance().clearAllCollections();
Alexandre Lisione4041492015-03-20 18:20:43 -040077 [historyChangedLabel setHidden:NO];
78}
79
80// KVO handler
81-(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject
82 change:(NSDictionary *)aChange context:(void *)aContext
83{
Alexandre Lision4de68ce2015-04-24 18:22:49 -040084 if (aKeyPath == Preferences::HistoryLimit) {
85 [historyChangedLabel setHidden:NO];
86 } else if (aKeyPath == Preferences::ShowAdvanced) {
Alexandre Lisiond0e628d2015-06-23 13:54:29 -040087 //[advancedGeneralSettings setHidden:[[aChange objectForKey: NSKeyValueChangeNewKey] boolValue]];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040088 }
Alexandre Lisione4041492015-03-20 18:20:43 -040089}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050090
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020091#pragma mark - Startup API
92
93// MIT license by Brian Dunagan
94- (BOOL)isLaunchAtStartup {
95 // See if the app is currently in LoginItems.
96 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
97 // Store away that boolean.
98 BOOL isInList = itemRef != nil;
99 // Release the reference if it exists.
100 if (itemRef != nil) CFRelease(itemRef);
101
102 return isInList;
103}
104
105- (IBAction)toggleLaunchAtStartup:(id)sender {
106 // Toggle the state.
107 BOOL shouldBeToggled = ![self isLaunchAtStartup];
108 // Get the LoginItems list.
109 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
110 if (loginItemsRef == nil) return;
111 if (shouldBeToggled) {
112 // Add the app to the LoginItems list.
Alexandre Lision81c97212015-06-17 15:51:53 -0400113 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200114 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
115 if (itemRef) CFRelease(itemRef);
116 }
117 else {
118 // Remove the app from the LoginItems list.
119 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
120 LSSharedFileListItemRemove(loginItemsRef,itemRef);
121 if (itemRef != nil) CFRelease(itemRef);
122 }
123 CFRelease(loginItemsRef);
124}
125
126- (LSSharedFileListItemRef)itemRefInLoginItems {
127 LSSharedFileListItemRef itemRef = nil;
Alexandre Lision81c97212015-06-17 15:51:53 -0400128 CFURLRef itemUrl = nil;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200129
130 // Get the app's URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400131 auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200132 // Get the LoginItems list.
133 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
134 if (loginItemsRef == nil) return nil;
135 // Iterate over the LoginItems.
Alexandre Lision81c97212015-06-17 15:51:53 -0400136 NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200137 for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) {
138 // Get the current LoginItem and resolve its URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400139 LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex];
140 if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200141 // Compare the URLs for the current LoginItem and the app.
Alexandre Lision81c97212015-06-17 15:51:53 -0400142 if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200143 // Save the LoginItem reference.
144 itemRef = currentItemRef;
145 }
146 }
147 }
148 // Retain the LoginItem reference.
149 if (itemRef != nil) CFRetain(itemRef);
150 // Release the LoginItems lists.
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200151 CFRelease(loginItemsRef);
152
153 return itemRef;
154}
155
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500156@end