Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 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. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 18 | */ |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 19 | #import "GeneralPrefsVC.h" |
| 20 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 21 | #import <categorizedhistorymodel.h> |
| 22 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 23 | #if ENABLE_SPARKLE |
| 24 | #import <Sparkle/Sparkle.h> |
| 25 | #endif |
| 26 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 27 | #import "Constants.h" |
| 28 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 29 | @interface GeneralPrefsVC () |
Alexandre Lision | aa03df4 | 2016-01-26 09:39:25 -0500 | [diff] [blame] | 30 | @property (unsafe_unretained) IBOutlet NSTextField* historyChangedLabel; |
| 31 | @property (unsafe_unretained) IBOutlet NSButton* startUpButton; |
| 32 | @property (unsafe_unretained) IBOutlet NSButton* toggleAutomaticUpdateCheck; |
| 33 | @property (unsafe_unretained) IBOutlet NSPopUpButton* checkIntervalPopUp; |
| 34 | @property (unsafe_unretained) IBOutlet NSView* sparkleContainer; |
| 35 | @property (unsafe_unretained) IBOutlet NSTextField* historyTextField; |
| 36 | @property (unsafe_unretained) IBOutlet NSStepper* historyStepper; |
| 37 | @property (unsafe_unretained) IBOutlet NSButton* historySwitch; |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 38 | |
| 39 | @end |
| 40 | |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 41 | @implementation GeneralPrefsVC |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 42 | @synthesize historyChangedLabel; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 43 | @synthesize startUpButton; |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 44 | @synthesize toggleAutomaticUpdateCheck; |
| 45 | @synthesize checkIntervalPopUp; |
| 46 | @synthesize sparkleContainer; |
Alexandre Lision | aa03df4 | 2016-01-26 09:39:25 -0500 | [diff] [blame] | 47 | @synthesize historyTextField; |
| 48 | @synthesize historyStepper; |
| 49 | @synthesize historySwitch; |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 50 | |
| 51 | - (void)loadView |
| 52 | { |
| 53 | [super loadView]; |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 54 | [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 55 | |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 56 | [startUpButton setState:[self isLaunchAtStartup]]; |
| 57 | |
Alexandre Lision | aa03df4 | 2016-01-26 09:39:25 -0500 | [diff] [blame] | 58 | int historyLimit = CategorizedHistoryModel::instance().historyLimit(); |
| 59 | [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]]; |
| 60 | [historyStepper setIntValue:historyLimit]; |
| 61 | |
| 62 | BOOL limited = CategorizedHistoryModel::instance().isHistoryLimited(); |
| 63 | [historySwitch setState:limited]; |
| 64 | [historyStepper setEnabled:limited]; |
| 65 | [historyTextField setEnabled:limited]; |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 66 | #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 Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 79 | - (void) dealloc |
| 80 | { |
| 81 | [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit]; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 84 | - (IBAction)clearHistory:(id)sender { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 85 | CategorizedHistoryModel::instance().clearAllCollections(); |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 86 | [historyChangedLabel setHidden:NO]; |
| 87 | } |
| 88 | |
Alexandre Lision | aa03df4 | 2016-01-26 09:39:25 -0500 | [diff] [blame] | 89 | - (IBAction)toggleHistory:(id)sender { |
| 90 | CategorizedHistoryModel::instance().setHistoryLimited([sender state]); |
| 91 | int historyLimit = CategorizedHistoryModel::instance().historyLimit(); |
| 92 | [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]]; |
| 93 | [historyStepper setIntValue:historyLimit]; |
| 94 | [historyChangedLabel setHidden:NO]; |
| 95 | [historyStepper setEnabled:[sender state]]; |
| 96 | [historyTextField setEnabled:[sender state]]; |
| 97 | } |
| 98 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 99 | // KVO handler |
| 100 | -(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject |
| 101 | change:(NSDictionary *)aChange context:(void *)aContext |
| 102 | { |
Alexandre Lision | aa03df4 | 2016-01-26 09:39:25 -0500 | [diff] [blame] | 103 | if ([aKeyPath isEqualToString:Preferences::HistoryLimit]) { |
| 104 | CategorizedHistoryModel::instance().setHistoryLimit([[aChange objectForKey: NSKeyValueChangeNewKey] integerValue]); |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 105 | [historyChangedLabel setHidden:NO]; |
Alexandre Lision | 4de68ce | 2015-04-24 18:22:49 -0400 | [diff] [blame] | 106 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 107 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 108 | |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 109 | #pragma mark - Startup API |
| 110 | |
| 111 | // MIT license by Brian Dunagan |
| 112 | - (BOOL)isLaunchAtStartup { |
| 113 | // See if the app is currently in LoginItems. |
| 114 | LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; |
| 115 | // Store away that boolean. |
| 116 | BOOL isInList = itemRef != nil; |
| 117 | // Release the reference if it exists. |
| 118 | if (itemRef != nil) CFRelease(itemRef); |
| 119 | |
| 120 | return isInList; |
| 121 | } |
| 122 | |
| 123 | - (IBAction)toggleLaunchAtStartup:(id)sender { |
| 124 | // Toggle the state. |
| 125 | BOOL shouldBeToggled = ![self isLaunchAtStartup]; |
| 126 | // Get the LoginItems list. |
| 127 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 128 | if (loginItemsRef == nil) return; |
| 129 | if (shouldBeToggled) { |
| 130 | // Add the app to the LoginItems list. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 131 | CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 132 | LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL); |
| 133 | if (itemRef) CFRelease(itemRef); |
| 134 | } |
| 135 | else { |
| 136 | // Remove the app from the LoginItems list. |
| 137 | LSSharedFileListItemRef itemRef = [self itemRefInLoginItems]; |
| 138 | LSSharedFileListItemRemove(loginItemsRef,itemRef); |
| 139 | if (itemRef != nil) CFRelease(itemRef); |
| 140 | } |
| 141 | CFRelease(loginItemsRef); |
| 142 | } |
| 143 | |
| 144 | - (LSSharedFileListItemRef)itemRefInLoginItems { |
| 145 | LSSharedFileListItemRef itemRef = nil; |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 146 | CFURLRef itemUrl = nil; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 147 | |
| 148 | // Get the app's URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 149 | auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 150 | // Get the LoginItems list. |
| 151 | LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 152 | if (loginItemsRef == nil) return nil; |
| 153 | // Iterate over the LoginItems. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 154 | NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil); |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 155 | for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) { |
| 156 | // Get the current LoginItem and resolve its URL. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 157 | LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex]; |
| 158 | if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 159 | // Compare the URLs for the current LoginItem and the app. |
Alexandre Lision | 81c9721 | 2015-06-17 15:51:53 -0400 | [diff] [blame] | 160 | if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) { |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 161 | // Save the LoginItem reference. |
| 162 | itemRef = currentItemRef; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | // Retain the LoginItem reference. |
| 167 | if (itemRef != nil) CFRetain(itemRef); |
| 168 | // Release the LoginItems lists. |
Alexandre Lision | 1ad5a2f | 2015-05-27 14:21:45 +0200 | [diff] [blame] | 169 | CFRelease(loginItemsRef); |
| 170 | |
| 171 | return itemRef; |
| 172 | } |
| 173 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 174 | @end |