Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
| 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 Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 30 | #import "GeneralPrefsVC.h" |
| 31 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 32 | #import <categorizedhistorymodel.h> |
| 33 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 34 | #import "Constants.h" |
| 35 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 36 | @interface GeneralPrefsVC () |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 37 | @property (unsafe_unretained) IBOutlet NSTextField *historyChangedLabel; |
| 38 | @property (unsafe_unretained) IBOutlet NSView *advancedGeneralSettings; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 39 | @property (unsafe_unretained) IBOutlet NSButton *startUpButton; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 40 | |
| 41 | @end |
| 42 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 43 | @implementation GeneralPrefsVC |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 44 | @synthesize historyChangedLabel; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 45 | @synthesize advancedGeneralSettings; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 46 | @synthesize startUpButton; |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 47 | |
| 48 | - (void)loadView |
| 49 | { |
| 50 | [super loadView]; |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 51 | [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 52 | [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::ShowAdvanced options:NSKeyValueObservingOptionNew context:NULL]; |
| 53 | |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 54 | [startUpButton setState:[self isLaunchAtStartup]]; |
| 55 | |
Alexandre Lision | d0e628d | 2015-06-23 13:54:29 -0400 | [diff] [blame^] | 56 | //[advancedGeneralSettings setHidden:![[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ShowAdvanced]]; |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 59 | - (void) dealloc |
| 60 | { |
| 61 | [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit]; |
| 62 | [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::ShowAdvanced]; |
| 63 | } |
| 64 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 65 | - (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 Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 74 | if (aKeyPath == Preferences::HistoryLimit) { |
| 75 | [historyChangedLabel setHidden:NO]; |
| 76 | } else if (aKeyPath == Preferences::ShowAdvanced) { |
Alexandre Lision | d0e628d | 2015-06-23 13:54:29 -0400 | [diff] [blame^] | 77 | //[advancedGeneralSettings setHidden:[[aChange objectForKey: NSKeyValueChangeNewKey] boolValue]]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 78 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 79 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 80 | |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 81 | #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 Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 103 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 104 | 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 Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 118 | CFURLRef itemUrl = nil; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 119 | |
| 120 | // Get the app's URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 121 | auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 122 | // Get the LoginItems list. |
| 123 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 124 | if (loginItemsRef == nil) return nil; |
| 125 | // Iterate over the LoginItems. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 126 | NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil); |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 127 | for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) { |
| 128 | // Get the current LoginItem and resolve its URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 129 | LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex]; |
| 130 | if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 131 | // Compare the URLs for the current LoginItem and the app. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 132 | if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 133 | // 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 Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 141 | CFRelease(loginItemsRef); |
| 142 | |
| 143 | return itemRef; |
| 144 | } |
| 145 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 146 | @end |