GeneralPrefsVC.mm 6.7 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
19 20
#import "GeneralPrefsVC.h"

21 22
#import <categorizedhistorymodel.h>

23 24 25 26
#if ENABLE_SPARKLE
#import <Sparkle/Sparkle.h>
#endif

27 28
#import "Constants.h"

29
@interface GeneralPrefsVC ()
30 31 32 33 34 35 36 37
@property (unsafe_unretained) IBOutlet NSTextField* historyChangedLabel;
@property (unsafe_unretained) IBOutlet NSButton* startUpButton;
@property (unsafe_unretained) IBOutlet NSButton* toggleAutomaticUpdateCheck;
@property (unsafe_unretained) IBOutlet NSPopUpButton* checkIntervalPopUp;
@property (unsafe_unretained) IBOutlet NSView* sparkleContainer;
@property (unsafe_unretained) IBOutlet NSTextField* historyTextField;
@property (unsafe_unretained) IBOutlet NSStepper* historyStepper;
@property (unsafe_unretained) IBOutlet NSButton* historySwitch;
38 39 40

@end

41
@implementation GeneralPrefsVC
42
@synthesize historyChangedLabel;
43
@synthesize startUpButton;
44 45 46
@synthesize toggleAutomaticUpdateCheck;
@synthesize checkIntervalPopUp;
@synthesize sparkleContainer;
47 48 49
@synthesize historyTextField;
@synthesize historyStepper;
@synthesize historySwitch;
50 51 52 53

- (void)loadView
{
    [super loadView];
54
    [[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:Preferences::HistoryLimit options:NSKeyValueObservingOptionNew context:NULL];
55

56 57
    [startUpButton setState:[self isLaunchAtStartup]];

58 59 60 61 62 63 64 65
    int historyLimit = CategorizedHistoryModel::instance().historyLimit();
    [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]];
    [historyStepper setIntValue:historyLimit];

    BOOL limited = CategorizedHistoryModel::instance().isHistoryLimited();
    [historySwitch setState:limited];
    [historyStepper setEnabled:limited];
    [historyTextField setEnabled:limited];
66 67 68 69 70 71 72 73 74 75 76
#if ENABLE_SPARKLE
    [sparkleContainer setHidden:NO];
    SUUpdater *updater = [SUUpdater sharedUpdater];
    [toggleAutomaticUpdateCheck bind:@"value" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];

    [checkIntervalPopUp bind:@"enabled" toObject:updater withKeyPath:@"automaticallyChecksForUpdates" options:nil];
    [checkIntervalPopUp bind:@"selectedTag" toObject:updater withKeyPath:@"updateCheckInterval" options:nil];
#else
    [sparkleContainer setHidden:YES];
#endif

77 78
}

79 80 81 82 83
- (void) dealloc
{
    [[NSUserDefaults standardUserDefaults] removeObserver:self forKeyPath:Preferences::HistoryLimit];
}

84
- (IBAction)clearHistory:(id)sender {
85
    CategorizedHistoryModel::instance().clearAllCollections();
86 87 88
    [historyChangedLabel setHidden:NO];
}

89 90 91 92 93 94 95 96 97 98
- (IBAction)toggleHistory:(id)sender {
    CategorizedHistoryModel::instance().setHistoryLimited([sender state]);
    int historyLimit = CategorizedHistoryModel::instance().historyLimit();
    [historyTextField setStringValue:[NSString stringWithFormat:@"%d", historyLimit]];
    [historyStepper setIntValue:historyLimit];
    [historyChangedLabel setHidden:NO];
    [historyStepper setEnabled:[sender state]];
    [historyTextField setEnabled:[sender state]];
}

99 100 101 102
// KVO handler
-(void)observeValueForKeyPath:(NSString *)aKeyPath ofObject:(id)anObject
                       change:(NSDictionary *)aChange context:(void *)aContext
{
103 104
    if ([aKeyPath isEqualToString:Preferences::HistoryLimit]) {
        CategorizedHistoryModel::instance().setHistoryLimit([[aChange objectForKey: NSKeyValueChangeNewKey] integerValue]);
105 106
        [historyChangedLabel setHidden:NO];
    }
107
}
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#pragma mark - Startup API

// MIT license by Brian Dunagan
- (BOOL)isLaunchAtStartup {
    // See if the app is currently in LoginItems.
    LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
    // Store away that boolean.
    BOOL isInList = itemRef != nil;
    // Release the reference if it exists.
    if (itemRef != nil) CFRelease(itemRef);

    return isInList;
}

- (IBAction)toggleLaunchAtStartup:(id)sender {
    // Toggle the state.
    BOOL shouldBeToggled = ![self isLaunchAtStartup];
    // Get the LoginItems list.
    LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    if (loginItemsRef == nil) return;
    if (shouldBeToggled) {
        // Add the app to the LoginItems list.
131
        CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
132 133 134 135 136 137 138 139 140 141 142 143 144 145
        LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
        if (itemRef) CFRelease(itemRef);
    }
    else {
        // Remove the app from the LoginItems list.
        LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
        LSSharedFileListItemRemove(loginItemsRef,itemRef);
        if (itemRef != nil) CFRelease(itemRef);
    }
    CFRelease(loginItemsRef);
}

- (LSSharedFileListItemRef)itemRefInLoginItems {
    LSSharedFileListItemRef itemRef = nil;
146
    CFURLRef itemUrl = nil;
147 148

    // Get the app's URL.
149
    auto appUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
150 151 152 153
    // Get the LoginItems list.
    LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
    if (loginItemsRef == nil) return nil;
    // Iterate over the LoginItems.
154
    NSArray *loginItems = (__bridge_transfer NSArray *)LSSharedFileListCopySnapshot(loginItemsRef, nil);
155 156
    for (int currentIndex = 0; currentIndex < [loginItems count]; currentIndex++) {
        // Get the current LoginItem and resolve its URL.
157 158
        LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItems objectAtIndex:currentIndex];
        if (LSSharedFileListItemResolve(currentItemRef, 0, &itemUrl, NULL) == noErr) {
159
            // Compare the URLs for the current LoginItem and the app.
160
            if ([(__bridge NSURL *)itemUrl isEqual:appUrl]) {
161 162 163 164 165 166 167 168 169 170 171 172 173
                // Save the LoginItem reference.
                itemRef = currentItemRef;
            }
        }
    }
    // Retain the LoginItem reference.
    if (itemRef != nil) CFRetain(itemRef);
    // Release the LoginItems lists.
    CFRelease(loginItemsRef);

    return itemRef;
}

174
@end