aboutsummaryrefslogtreecommitdiff
path: root/launcher/updater/MacSparkleUpdater.mm
blob: 1c1f5cd27a3e2849167ef19682da0aacf97b29c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-License-Identifier: GPL-3.0-only
/*
 *  PolyMC - Minecraft Launcher
 *  Copyright (C) 2022 Kenneth Chew <kenneth.c0@protonmail.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, version 3.
 *
 *  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, see <https://www.gnu.org/licenses/>.
 */

#include "MacSparkleUpdater.h"

#include "Application.h"

#include <Cocoa/Cocoa.h>
#include <Sparkle/Sparkle.h>

@interface UpdaterObserver : NSObject

@property(nonatomic, readonly) SPUUpdater* updater;

/// A callback to run when the state of `canCheckForUpdates` for the `updater` changes.
@property(nonatomic, copy) void (^callback)(bool);

- (id)initWithUpdater:(SPUUpdater*)updater;

@end

@implementation UpdaterObserver

- (id)initWithUpdater:(SPUUpdater*)updater {
    self = [super init];
    _updater = updater;
    [self addObserver:self forKeyPath:@"updater.canCheckForUpdates" options:NSKeyValueObservingOptionNew context:nil];

    return self;
}

- (void)observeValueForKeyPath:(NSString*)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey, id>*)change
                       context:(void*)context {
    if ([keyPath isEqualToString:@"updater.canCheckForUpdates"]) {
        bool canCheck = [change[NSKeyValueChangeNewKey] boolValue];
        self.callback(canCheck);
    }
}

@end

@interface UpdaterDelegate : NSObject <SPUUpdaterDelegate>

@property(nonatomic, copy) NSSet<NSString*>* allowedChannels;

@end

@implementation UpdaterDelegate

- (NSSet<NSString*>*)allowedChannelsForUpdater:(SPUUpdater*)updater {
    return _allowedChannels;
}

@end

class MacSparkleUpdater::Private {
   public:
    SPUStandardUpdaterController* updaterController;
    UpdaterObserver* updaterObserver;
    UpdaterDelegate* updaterDelegate;
    NSAutoreleasePool* autoReleasePool;
};

MacSparkleUpdater::MacSparkleUpdater() {
    priv = new MacSparkleUpdater::Private();

    // Enable Cocoa's memory management.
    NSApplicationLoad();
    priv->autoReleasePool = [[NSAutoreleasePool alloc] init];

    // Delegate is used for setting/getting allowed update channels.
    priv->updaterDelegate = [[UpdaterDelegate alloc] init];

    // Controller is the interface for actually doing the updates.
    priv->updaterController = [[SPUStandardUpdaterController alloc] initWithStartingUpdater:true
                                                                            updaterDelegate:priv->updaterDelegate
                                                                         userDriverDelegate:nil];

    priv->updaterObserver = [[UpdaterObserver alloc] initWithUpdater:priv->updaterController.updater];
    // Use KVO to run a callback that emits a Qt signal when `canCheckForUpdates` changes, so the UI can respond accordingly.
    priv->updaterObserver.callback = ^(bool canCheck) {
      emit canCheckForUpdatesChanged(canCheck);
    };
}

MacSparkleUpdater::~MacSparkleUpdater() {
    [priv->updaterObserver removeObserver:priv->updaterObserver forKeyPath:@"updater.canCheckForUpdates"];

    [priv->updaterController release];
    [priv->updaterObserver release];
    [priv->updaterDelegate release];
    [priv->autoReleasePool release];
    delete priv;
}

void MacSparkleUpdater::checkForUpdates() {
    [priv->updaterController checkForUpdates:nil];
}

bool MacSparkleUpdater::getAutomaticallyChecksForUpdates() {
    return priv->updaterController.updater.automaticallyChecksForUpdates;
}

double MacSparkleUpdater::getUpdateCheckInterval() {
    return priv->updaterController.updater.updateCheckInterval;
}

QSet<QString> MacSparkleUpdater::getAllowedChannels() {
    // Convert NSSet<NSString> -> QSet<QString>
    __block QSet<QString> channels;
    [priv->updaterDelegate.allowedChannels enumerateObjectsUsingBlock:^(NSString* channel, BOOL* stop) {
      channels.insert(QString::fromNSString(channel));
    }];
    return channels;
}

bool MacSparkleUpdater::getBetaAllowed() {
    return getAllowedChannels().contains("beta");
}

void MacSparkleUpdater::setAutomaticallyChecksForUpdates(bool check) {
    priv->updaterController.updater.automaticallyChecksForUpdates = check ? YES : NO;  // make clang-tidy happy
}

void MacSparkleUpdater::setUpdateCheckInterval(double seconds) {
    priv->updaterController.updater.updateCheckInterval = seconds;
}

void MacSparkleUpdater::clearAllowedChannels() {
    priv->updaterDelegate.allowedChannels = [NSSet set];
}

void MacSparkleUpdater::setAllowedChannel(const QString& channel) {
    if (channel.isEmpty()) {
        clearAllowedChannels();
        return;
    }

    NSSet<NSString*>* nsChannels = [NSSet setWithObject:channel.toNSString()];
    priv->updaterDelegate.allowedChannels = nsChannels;
}

void MacSparkleUpdater::setAllowedChannels(const QSet<QString>& channels) {
    if (channels.isEmpty()) {
        clearAllowedChannels();
        return;
    }

    QString channelsConfig = "";
    // Convert QSet<QString> -> NSSet<NSString>
    NSMutableSet<NSString*>* nsChannels = [NSMutableSet setWithCapacity:channels.count()];
    foreach (const QString channel, channels) {
        [nsChannels addObject:channel.toNSString()];
        channelsConfig += channel + " ";
    }

    priv->updaterDelegate.allowedChannels = nsChannels;
}

void MacSparkleUpdater::setBetaAllowed(bool allowed) {
    if (allowed) {
        setAllowedChannel("beta");
    } else {
        clearAllowedChannels();
    }
}