blob: 866972ba01c31247d83898012db01dd3ad1fee9c [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 Lisionc65310c2015-04-23 16:44:23 -040034#import "Constants.h"
35
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050036@interface GeneralPrefsVC ()
Alexandre Lision81c97212015-06-17 15:51:53 -040037@property (unsafe_unretained) IBOutlet NSTextField *historyChangedLabel;
38@property (unsafe_unretained) IBOutlet NSView *advancedGeneralSettings;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020039@property (unsafe_unretained) IBOutlet NSButton *startUpButton;
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050040
41@end
42
Alexandre Lision4de68ce2015-04-24 18:22:49 -040043@implementation GeneralPrefsVC
Alexandre Lisione4041492015-03-20 18:20:43 -040044@synthesize historyChangedLabel;
Alexandre Lision4de68ce2015-04-24 18:22:49 -040045@synthesize advancedGeneralSettings;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020046@synthesize startUpButton;
Alexandre Lisione4041492015-03-20 18:20:43 -040047
48- (void)loadView
49{
50 [super loadView];
Alexandre Lisionc65310c2015-04-23 16:44:23 -040051 [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040052 [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::ShowAdvanced options:NSKeyValueObservingOptionNew context:NULL];
53
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020054 [startUpButton setState:[self isLaunchAtStartup]];
55
Alexandre Lisiond0e628d2015-06-23 13:54:29 -040056 //[advancedGeneralSettings setHidden:![[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]];
Alexandre Lisione4041492015-03-20 18:20:43 -040057}
58
Alexandre Lision81c97212015-06-17 15:51:53 -040059- (void) dealloc
60{
61 [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit];
62 [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::ShowAdvanced];
63}
64
Alexandre Lisione4041492015-03-20 18:20:43 -040065- (IBAction)clearHistory:(id)sender {
66 CategorizedHistoryModel::instance()->clearAllCollections();
67 [historyChangedLabel setHidden:NO];
68}
69
70// KVO handler
71-(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject
72 change:(NSDictionary *)aChange context:(void *)aContext
73{
Alexandre Lision4de68ce2015-04-24 18:22:49 -040074 if (aKeyPath == Preferences::HistoryLimit) {
75 [historyChangedLabel setHidden:NO];
76 } else if (aKeyPath == Preferences::ShowAdvanced) {
Alexandre Lisiond0e628d2015-06-23 13:54:29 -040077 //[advancedGeneralSettings setHidden:[[aChange objectForKey: NSKeyValueChangeNewKey] boolValue]];
Alexandre Lision4de68ce2015-04-24 18:22:49 -040078 }
Alexandre Lisione4041492015-03-20 18:20:43 -040079}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050080
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +020081#pragma mark - Startup API
82
83// MIT license by Brian Dunagan
84- (BOOL)isLaunchAtStartup {
85 // See if the app is currently in LoginItems.
86 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
87 // Store away that boolean.
88 BOOL isInList = itemRef != nil;
89 // Release the reference if it exists.
90 if (itemRef != nil) CFRelease(itemRef);
91
92 return isInList;
93}
94
95- (IBAction)toggleLaunchAtStartup:(id)sender {
96 // Toggle the state.
97 BOOL shouldBeToggled = ![self isLaunchAtStartup];
98 // Get the LoginItems list.
99 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
100 if (loginItemsRef == nil) return;
101 if (shouldBeToggled) {
102 // Add the app to the LoginItems list.
Alexandre Lision81c97212015-06-17 15:51:53 -0400103 CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200104 LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
105 if (itemRef) CFRelease(itemRef);
106 }
107 else {
108 // Remove the app from the LoginItems list.
109 LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
110 LSSharedFileListItemRemove(loginItemsRef,itemRef);
111 if (itemRef != nil) CFRelease(itemRef);
112 }
113 CFRelease(loginItemsRef);
114}
115
116- (LSSharedFileListItemRef)itemRefInLoginItems {
117 LSSharedFileListItemRef itemRef = nil;
Alexandre Lision81c97212015-06-17 15:51:53 -0400118 CFURLRef itemUrl = nil;
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200119
120 // Get the app's URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400121 auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200122 // Get the LoginItems list.
123 LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
124 if (loginItemsRef == nil) return nil;
125 // Iterate over the LoginItems.
Alexandre Lision81c97212015-06-17 15:51:53 -0400126 NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200127 for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) {
128 // Get the current LoginItem and resolve its URL.
Alexandre Lision81c97212015-06-17 15:51:53 -0400129 LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex];
130 if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200131 // Compare the URLs for the current LoginItem and the app.
Alexandre Lision81c97212015-06-17 15:51:53 -0400132 if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) {
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200133 // Save the LoginItem reference.
134 itemRef = currentItemRef;
135 }
136 }
137 }
138 // Retain the LoginItem reference.
139 if (itemRef != nil) CFRetain(itemRef);
140 // Release the LoginItems lists.
Alexandre Lision1ad5a2f2015-05-27 14:21:45 +0200141 CFRelease(loginItemsRef);
142
143 return itemRef;
144}
145
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500146@end