blob: a9f88e6b325256e2be93e650c798964f40ccd3ec [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
2 * Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
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.
18 *
19 * Additional permission under GNU GPL version 3 section 7:
20 *
21 * If you modify this program, or any covered work, by linking or
22 * combining it with the OpenSSL project's OpenSSL library (or a
23 * modified version of that library), containing parts covered by the
24 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25 * grants you additional permission to convey the resulting work.
26 * Corresponding Source for a non-source form of such a combination
27 * shall include the source code for the parts of OpenSSL used as well
28 * as that of the covered work.
29 */
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050030#import "GeneralPrefsVC.h"
31
Alexandre Lisione4041492015-03-20 18:20:43 -040032#import <categorizedhistorymodel.h>
33
Alexandre Lision3d4143a2015-06-10 14:27:49 -040034#if ENABLE_SPARKLE
35#import <Sparkle/Sparkle.h>
36#endif
37
Alexandre Lisionc65310c2015-04-23 16:44:23 -040038#import "Constants.h"
39
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050040@interface GeneralPrefsVC ()
Alexandre Lision81c97212015-06-17 15:51:53 -040041@property (unsafe_unretained) IBOutlet NSTextField *historyChangedLabel;
42@property (unsafe_unretained) IBOutlet NSView *advancedGeneralSettings;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020043@property (unsafe_unretained) IBOutlet NSButton *startUpButton;
Alexandre Lision3d4143a2015-06-10 14:27:49 -040044@property (unsafe_unretained) IBOutlet NSButton *toggleAutomaticUpdateCheck;
45@property (unsafe_unretained) IBOutlet NSPopUpButton *checkIntervalPopUp;
46@property (unsafe_unretained) IBOutlet NSView *sparkleContainer;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050047
48@end
49
Alexandre Lision4de68ce2015-04-24 18:22:49 -040050@implementation GeneralPrefsVC
Alexandre Lisione4041492015-03-20 18:20:43 -040051@synthesize historyChangedLabel;
Alexandre Lision4de68ce2015-04-24 18:22:49 -040052@synthesize advancedGeneralSettings;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020053@synthesize startUpButton;
Alexandre Lision3d4143a2015-06-10 14:27:49 -040054@synthesize toggleAutomaticUpdateCheck;
55@synthesize checkIntervalPopUp;
56@synthesize sparkleContainer;
Alexandre Lisione4041492015-03-20 18:20:43 -040057
58- (void)loadView
59{
60 [super loadView];
Alexandre Lisionc65310c2015-04-23 16:44:23 -040061 [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040062 [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::ShowAdvanced options:NSKeyValueObservingOptionNew context:NULL];
63
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020064 [startUpButton setState:[self isLaunchAtStartup]];
65
Alexandre Lision3d4143a2015-06-10 14:27:49 -040066#if ENABLE_SPARKLE
67 [sparkleContainer setHidden:NO];
68 SUUpdater *updater = [SUUpdater sharedUpdater];
69 [toggleAutomaticUpdateCheck bind:@"value" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
70
71 [checkIntervalPopUp bind:@"enabled" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
72 [checkIntervalPopUp bind:@"selectedTag" toObject:updater withKeyPath:@"updateCheckInterval" options:nil];
73#else
74 [sparkleContainer setHidden:YES];
75#endif
76
Alexandre Lisiond0e628d2015-06-23 13:54:29 -040077 //[advancedGeneralSettings setHidden:![[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]];
Alexandre Lisione4041492015-03-20 18:20:43 -040078}
79
Alexandre Lision81c97212015-06-17 15:51:53 -040080- (void) dealloc
81{
82 [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit];
83 [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::ShowAdvanced];
84}
85
Alexandre Lisione4041492015-03-20 18:20:43 -040086- (IBAction)clearHistory:(id)sender {
87 CategorizedHistoryModel::instance()->clearAllCollections();
88 [historyChangedLabel setHidden:NO];
89}
90
91// KVO handler
92-(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject
93 change:(NSDictionary *)aChange context:(void *)aContext
94{
Alexandre Lision4de68ce2015-04-24 18:22:49 -040095 if (aKeyPath == Preferences::HistoryLimit) {
96 [historyChangedLabel setHidden:NO];
97 } else if (aKeyPath == Preferences::ShowAdvanced) {
Alexandre Lisiond0e628d2015-06-23 13:54:29 -040098 //[advancedGeneralSettings setHidden:[[aChange objectForKey: NSKeyValueChangeNewKey] boolValue]];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040099 }
Alexandre Lisione4041492015-03-20 18:20:43 -0400100}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500101
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200102#pragma mark - Startup API
103
104// MIT license by Brian Dunagan
105- (BOOL)isLaunchAtStartup {
106 // See if the app is currently in LoginItems.
107 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
108 // Store away that boolean.
109 BOOL isInList = itemRef != nil;
110 // Release the reference if it exists.
111 if (itemRef != nil) CFRelease(itemRef);
112
113 return isInList;
114}
115
116- (IBAction)toggleLaunchAtStartup:(id)sender {
117 // Toggle the state.
118 BOOL shouldBeToggled = ![self isLaunchAtStartup];
119 // Get the LoginItems list.
120 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
121 if (loginItemsRef == nil) return;
122 if (shouldBeToggled) {
123 // Add the app to the LoginItems list.
Alexandre Lision81c97212015-06-17 15:51:53 -0400124 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200125 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
126 if (itemRef) CFRelease(itemRef);
127 }
128 else {
129 // Remove the app from the LoginItems list.
130 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
131 LSSharedFileListItemRemove(loginItemsRef,itemRef);
132 if (itemRef != nil) CFRelease(itemRef);
133 }
134 CFRelease(loginItemsRef);
135}
136
137- (LSSharedFileListItemRef)itemRefInLoginItems {
138 LSSharedFileListItemRef itemRef = nil;
Alexandre Lision81c97212015-06-17 15:51:53 -0400139 CFURLRef itemUrl = nil;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200140
141 // Get the app's URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400142 auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200143 // Get the LoginItems list.
144 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
145 if (loginItemsRef == nil) return nil;
146 // Iterate over the LoginItems.
Alexandre Lision81c97212015-06-17 15:51:53 -0400147 NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200148 for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) {
149 // Get the current LoginItem and resolve its URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400150 LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex];
151 if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200152 // Compare the URLs for the current LoginItem and the app.
Alexandre Lision81c97212015-06-17 15:51:53 -0400153 if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200154 // Save the LoginItem reference.
155 itemRef = currentItemRef;
156 }
157 }
158 }
159 // Retain the LoginItem reference.
160 if (itemRef != nil) CFRetain(itemRef);
161 // Release the LoginItems lists.
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200162 CFRelease(loginItemsRef);
163
164 return itemRef;
165}
166
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500167@end