From 5dca54d9045aaeb060cf835c63740aba30b95576 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 5 Mar 2016 12:57:56 -0500 Subject: [PATCH] Add custom user driver in the test app --- Sparkle.xcodeproj/project.pbxproj | 24 +- Sparkle/SUStandardUpdaterController.h | 4 +- Sparkle/SUStandardUpdaterController.m | 8 +- Sparkle/SUStandardUserDriver.h | 72 +--- Sparkle/SUStandardUserDriverProtocol.h | 89 +++++ .../SUInstallUpdateViewController.h | 18 + .../SUInstallUpdateViewController.m | 73 ++++ .../SUInstallUpdateViewController.xib | 88 ++++ TestApplication/SUPopUpTitlebarUserDriver.h | 18 + TestApplication/SUPopUpTitlebarUserDriver.m | 376 ++++++++++++++++++ .../SURemoteUpdateSettingsWindowController.m | 8 +- ...SURemoteUpdateSettingsWindowController.xib | 2 +- 12 files changed, 700 insertions(+), 80 deletions(-) create mode 100644 Sparkle/SUStandardUserDriverProtocol.h create mode 100644 TestApplication/SUInstallUpdateViewController.h create mode 100644 TestApplication/SUInstallUpdateViewController.m create mode 100644 TestApplication/SUInstallUpdateViewController.xib create mode 100644 TestApplication/SUPopUpTitlebarUserDriver.h create mode 100644 TestApplication/SUPopUpTitlebarUserDriver.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index ef792228..7bbdbd7b 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -184,6 +184,10 @@ 725F977C1C8A896F00265BE4 /* SUUserDriverCoreComponent.m in Sources */ = {isa = PBXBuildFile; fileRef = 725F977A1C8A896F00265BE4 /* SUUserDriverCoreComponent.m */; }; 725F977F1C8A93D700265BE4 /* SUUserDriverUIComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = 725F977D1C8A93D700265BE4 /* SUUserDriverUIComponent.h */; }; 725F97801C8A93D700265BE4 /* SUUserDriverUIComponent.m in Sources */ = {isa = PBXBuildFile; fileRef = 725F977E1C8A93D700265BE4 /* SUUserDriverUIComponent.m */; }; + 725F97841C8AA90000265BE4 /* SUPopUpTitlebarUserDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 725F97831C8AA90000265BE4 /* SUPopUpTitlebarUserDriver.m */; }; + 725F97A51C8B304D00265BE4 /* SUInstallUpdateViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 725F97A31C8B304D00265BE4 /* SUInstallUpdateViewController.m */; }; + 725F97A61C8B304D00265BE4 /* SUInstallUpdateViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 725F97A41C8B304D00265BE4 /* SUInstallUpdateViewController.xib */; }; + 725F97A81C8B42D700265BE4 /* SUStandardUserDriverProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 725F97A71C8B42D700265BE4 /* SUStandardUserDriverProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; 7268AC631AD634C200C3E0C1 /* SUBinaryDeltaCreate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7268AC621AD634C200C3E0C1 /* SUBinaryDeltaCreate.m */; }; 726DF88E1C84277600188804 /* SUStatusCompletionResults.h in Headers */ = {isa = PBXBuildFile; fileRef = 726DF88D1C84277500188804 /* SUStatusCompletionResults.h */; settings = {ATTRIBUTES = (Public, ); }; }; 726E4A0E1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 726E4A0D1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -676,6 +680,12 @@ 725F977A1C8A896F00265BE4 /* SUUserDriverCoreComponent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUserDriverCoreComponent.m; sourceTree = ""; }; 725F977D1C8A93D700265BE4 /* SUUserDriverUIComponent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUserDriverUIComponent.h; sourceTree = ""; }; 725F977E1C8A93D700265BE4 /* SUUserDriverUIComponent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUserDriverUIComponent.m; sourceTree = ""; }; + 725F97821C8AA90000265BE4 /* SUPopUpTitlebarUserDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUPopUpTitlebarUserDriver.h; sourceTree = ""; }; + 725F97831C8AA90000265BE4 /* SUPopUpTitlebarUserDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUPopUpTitlebarUserDriver.m; sourceTree = ""; }; + 725F97A21C8B304D00265BE4 /* SUInstallUpdateViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUInstallUpdateViewController.h; sourceTree = ""; }; + 725F97A31C8B304D00265BE4 /* SUInstallUpdateViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUInstallUpdateViewController.m; sourceTree = ""; }; + 725F97A41C8B304D00265BE4 /* SUInstallUpdateViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SUInstallUpdateViewController.xib; sourceTree = ""; }; + 725F97A71C8B42D700265BE4 /* SUStandardUserDriverProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUStandardUserDriverProtocol.h; sourceTree = ""; }; 7268AC621AD634C200C3E0C1 /* SUBinaryDeltaCreate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUBinaryDeltaCreate.m; sourceTree = ""; }; 7268AC641AD634E400C3E0C1 /* SUBinaryDeltaCreate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUBinaryDeltaCreate.h; sourceTree = ""; }; 726B2B5D1C645FC900388755 /* UI Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "UI Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1138,6 +1148,11 @@ 726E4A311C87E0C700C57C6A /* SURemoteUpdateSettingsWindowController.h */, 726E4A321C87E0C700C57C6A /* SURemoteUpdateSettingsWindowController.m */, 726E4A331C87E0C700C57C6A /* SURemoteUpdateSettingsWindowController.xib */, + 725F97821C8AA90000265BE4 /* SUPopUpTitlebarUserDriver.h */, + 725F97831C8AA90000265BE4 /* SUPopUpTitlebarUserDriver.m */, + 725F97A21C8B304D00265BE4 /* SUInstallUpdateViewController.h */, + 725F97A31C8B304D00265BE4 /* SUInstallUpdateViewController.m */, + 725F97A41C8B304D00265BE4 /* SUInstallUpdateViewController.xib */, 72E45CFD1B641D0D005C701A /* test_app_only_dsa_priv_dont_ever_do_this_for_real.pem */, 14732BBF1960F0AC00593899 /* test_app_only_dsa_pub.pem */, 61B5F90409C4CEE200B25A18 /* TestApplication-Info.plist */, @@ -1161,6 +1176,8 @@ isa = PBXGroup; children = ( 725CB9561C7120410064365A /* SUUserDriver.h */, + 725F97791C8A896F00265BE4 /* SUUserDriverCoreComponent.h */, + 725F977A1C8A896F00265BE4 /* SUUserDriverCoreComponent.m */, 726DF88D1C84277500188804 /* SUStatusCompletionResults.h */, 61B93B250DD0FDD300DCD2F8 /* SUAutomaticUpdateDriver.h */, 61B93B260DD0FDD300DCD2F8 /* SUAutomaticUpdateDriver.m */, @@ -1200,10 +1217,9 @@ 726E4A361C89116000C57C6A /* SUStandardUserDriverDelegate.h */, 726E4A0D1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h */, 726E4A0F1C86B5A200C57C6A /* SUStandardUserDriverRemoteDelegate.h */, + 725F97A71C8B42D700265BE4 /* SUStandardUserDriverProtocol.h */, 725CB9581C7121830064365A /* SUStandardUserDriver.h */, 725CB9591C7121830064365A /* SUStandardUserDriver.m */, - 725F97791C8A896F00265BE4 /* SUUserDriverCoreComponent.h */, - 725F977A1C8A896F00265BE4 /* SUUserDriverCoreComponent.m */, 725F977D1C8A93D700265BE4 /* SUUserDriverUIComponent.h */, 725F977E1C8A93D700265BE4 /* SUUserDriverUIComponent.m */, ); @@ -1279,6 +1295,7 @@ files = ( 726E4A371C89116000C57C6A /* SUStandardUserDriverDelegate.h in Headers */, 726E4A0E1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h in Headers */, + 725F97A81C8B42D700265BE4 /* SUStandardUserDriverProtocol.h in Headers */, 726E4A101C86B5A200C57C6A /* SUStandardUserDriverRemoteDelegate.h in Headers */, 725CB95A1C7121830064365A /* SUStandardUserDriver.h in Headers */, 725CB9571C7120410064365A /* SUUserDriver.h in Headers */, @@ -1640,6 +1657,7 @@ buildActionMask = 2147483647; files = ( 726E4A351C87E0C700C57C6A /* SURemoteUpdateSettingsWindowController.xib in Resources */, + 725F97A61C8B304D00265BE4 /* SUInstallUpdateViewController.xib in Resources */, 1420DF50196247F900203BB0 /* Images.xcassets in Resources */, 61B5F92E09C4CFD800B25A18 /* InfoPlist.strings in Resources */, 61B5F92F09C4CFD800B25A18 /* MainMenu.xib in Resources */, @@ -1813,8 +1831,10 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 725F97841C8AA90000265BE4 /* SUPopUpTitlebarUserDriver.m in Sources */, 726E4A2B1C87D56200C57C6A /* SUTestWebServer.m in Sources */, 726E4A341C87E0C700C57C6A /* SURemoteUpdateSettingsWindowController.m in Sources */, + 725F97A51C8B304D00265BE4 /* SUInstallUpdateViewController.m in Sources */, 61B5F93009C4CFDC00B25A18 /* main.m in Sources */, 726F2CEB1BC9C733001971A4 /* SUConstants.m in Sources */, 72E45CF31B640CDD005C701A /* SUTestApplicationDelegate.m in Sources */, diff --git a/Sparkle/SUStandardUpdaterController.h b/Sparkle/SUStandardUpdaterController.h index 5c4eec12..32a3df8d 100644 --- a/Sparkle/SUStandardUpdaterController.h +++ b/Sparkle/SUStandardUpdaterController.h @@ -11,7 +11,7 @@ NS_ASSUME_NONNULL_BEGIN @class SUUpdater; -@class SUStandardUserDriver; +@protocol SUStandardUserDriver; /*! A controller class that instantiates a SUUpdater and allows binding UI to it. @@ -43,7 +43,7 @@ NS_ASSUME_NONNULL_BEGIN /*! Accessible property for the updater's user driver. */ -@property (nonatomic, readonly) SUStandardUserDriver *userDriver; +@property (nonatomic, readonly) id userDriver; /*! Explicitly checks for updates and displays a progress dialog while doing so. diff --git a/Sparkle/SUStandardUpdaterController.m b/Sparkle/SUStandardUpdaterController.m index 798d2676..bc4e704b 100644 --- a/Sparkle/SUStandardUpdaterController.m +++ b/Sparkle/SUStandardUpdaterController.m @@ -19,7 +19,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults @interface SUStandardUpdaterController () @property (nonatomic) SUUpdater *updater; -@property (nonatomic) SUStandardUserDriver *userDriver; +@property (nonatomic) id userDriver; @end @@ -35,8 +35,10 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults [self registerAsObserver]; NSBundle *hostBundle = [NSBundle mainBundle]; - _userDriver = [[SUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:nil]; - _updater = [[SUUpdater alloc] initWithHostBundle:hostBundle userDriver:_userDriver delegate:nil]; + + id userDriver = [[SUStandardUserDriver alloc] initWithHostBundle:hostBundle delegate:nil]; + _updater = [[SUUpdater alloc] initWithHostBundle:hostBundle userDriver:userDriver delegate:nil]; + _userDriver = userDriver; } return self; } diff --git a/Sparkle/SUStandardUserDriver.h b/Sparkle/SUStandardUserDriver.h index 393065b4..0edbc670 100644 --- a/Sparkle/SUStandardUserDriver.h +++ b/Sparkle/SUStandardUserDriver.h @@ -8,6 +8,7 @@ #import #import "SUUserDriver.h" +#import "SUStandardUserDriverProtocol.h" NS_ASSUME_NONNULL_BEGIN @@ -16,7 +17,7 @@ NS_ASSUME_NONNULL_BEGIN /*! Sparkle's standard built-in user driver for updater interactions */ -@interface SUStandardUserDriver : NSObject +@interface SUStandardUserDriver : NSObject /*! Initializes a Sparkle's standard user driver for user update interactions @@ -26,75 +27,6 @@ NS_ASSUME_NONNULL_BEGIN */ - (instancetype)initWithHostBundle:(NSBundle *)hostBundle delegate:(_Nullable id )delegate; -/*! - Readable and writable property for this user driver's delegate - */ -@property (nonatomic, weak, nullable) id delegate; - -/*! - Indicates whether or not an update is in progress as far as the user's perspective is concerned - - A typical application may rely on this property for its check for updates menu item validation - */ -@property (nonatomic, readonly, getter=isUpdateInProgress) BOOL updateInProgress; - -/*! - Indicates whether or not a currently pending update check will be initiated in the future - - This may be useful for deciding if the user driver should be invalidated, when the connection to the updater - has been interrupted. - */ -@property (nonatomic, readonly) BOOL willInitiateNextUpdateCheck; - -/*! - Indicates whether or not Sparkle's updater has been idling on update checks; i.e, if automatic update checks - had been disabled the last time the updater checked. - - This may be useful for deciding if a connection needs to be immediately resumed or re-created if it has been interrupted. - See also -willInitiateNextUpdateCheck - */ -@property (nonatomic, readonly) BOOL idlesOnUpdateChecks; - -/*! - Send a signal to the user driver that the user application should terminate. - - This should only be invoked if the user driver's delegate says it's responsible for signaling application termination, - and this should only be called from NSApplicationDelegate's applicationShouldTerminate: method. - - This is particularly useful when the user driver and updater live in separate processes, and the updater - wants to install an automatic update on termination. If the updater is in a separate process, that means - we may have to defer application termination a bit until the updater is ready to launch its auto updater. - - @return The NSApplication termination reply. Use this as a return value to NSApplicationDelegate's applicationShouldTerminate: - */ -- (NSApplicationTerminateReply)sendApplicationTerminationSignal; - -/*! - Terminate the current application after sending a termination signal - - This should only be invoked by the user driver's delegate if the -sendApplicationTerminationSignal message has been sent prior. - For this user driver's delegate, invoking this is appropriate when the application's termination has been delayed, and the connection - to the updater has been invalidated or interrupted -- in which case the updater can no longer tell the user driver when to finish terminating. - - This implementation will be dispatched on the main queue. - - Note this is also a part of the SUUserDriver protocol that this class implements - */ -- (void)terminateApplication; - -/*! - Invalidate the current update installation - - This stops all behavior for the current update installation. It avoids making any calls or replies to Sparkle's updater. - Note this does not change the idle state of update checks (i.e, -idlesOnUpdateChecks), but it will invalidate any pending update checks. - One may want to check if there is a pending update check (i.e, -willInitiateNextUpdateCheck) before making this call to decide if the - user driver should be invalidated or not - - This is appropriate to call when the connection to the updater has been interrupted or invalidated. Note this class is still re-usable - in the case that the connection should be later resumed. - */ -- (void)invalidate; - @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUStandardUserDriverProtocol.h b/Sparkle/SUStandardUserDriverProtocol.h new file mode 100644 index 00000000..f796dca6 --- /dev/null +++ b/Sparkle/SUStandardUserDriverProtocol.h @@ -0,0 +1,89 @@ +// +// SUStandardUserDriverProtocol.h +// Sparkle +// +// Created by Mayur Pawashe on 3/5/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +@protocol SUStandardUserDriverDelegate; + +/*! + Protocol for Sparkle's standard built-in user driver for updater interactions. + + Other user drivers may wish to implement this protocol. + */ +@protocol SUStandardUserDriver + +/*! + Readable and writable property for this user driver's delegate + */ +@property (nonatomic, weak, nullable) id delegate; + +/*! + Indicates whether or not an update is in progress as far as the user's perspective is concerned + + A typical application may rely on this property for its check for updates menu item validation + */ +@property (nonatomic, readonly, getter=isUpdateInProgress) BOOL updateInProgress; + +/*! + Indicates whether or not a currently pending update check will be initiated in the future + + This may be useful for deciding if the user driver should be invalidated, when the connection to the updater + has been interrupted. + */ +@property (nonatomic, readonly) BOOL willInitiateNextUpdateCheck; + +/*! + Indicates whether or not Sparkle's updater has been idling on update checks; i.e, if automatic update checks + had been disabled the last time the updater checked. + + This may be useful for deciding if a connection needs to be immediately resumed or re-created if it has been interrupted. + See also -willInitiateNextUpdateCheck + */ +@property (nonatomic, readonly) BOOL idlesOnUpdateChecks; + +/*! + Send a signal to the user driver that the user application should terminate. + + This should only be invoked if the user driver's delegate says it's responsible for signaling application termination, + and this should only be called from NSApplicationDelegate's applicationShouldTerminate: method. + + This is particularly useful when the user driver and updater live in separate processes, and the updater + wants to install an automatic update on termination. If the updater is in a separate process, that means + we may have to defer application termination a bit until the updater is ready to launch its auto updater. + + @return The NSApplication termination reply. Use this as a return value to NSApplicationDelegate's applicationShouldTerminate: + */ +- (NSApplicationTerminateReply)sendApplicationTerminationSignal; + +/*! + Terminate the current application after sending a termination signal + + This should only be invoked by the user driver's delegate if the -sendApplicationTerminationSignal message has been sent prior. + For this user driver's delegate, invoking this is appropriate when the application's termination has been delayed, and the connection + to the updater has been invalidated or interrupted -- in which case the updater can no longer tell the user driver when to finish terminating. + + This implementation will be dispatched on the main queue. + + Note this is also a part of the SUUserDriver protocol that this class implements + */ +- (void)terminateApplication; + +/*! + Invalidate the current update installation + + This stops all behavior for the current update installation. It avoids making any calls or replies to Sparkle's updater. + Note this does not change the idle state of update checks (i.e, -idlesOnUpdateChecks), but it will invalidate any pending update checks. + One may want to check if there is a pending update check (i.e, -willInitiateNextUpdateCheck) before making this call to decide if the + user driver should be invalidated or not + + This is appropriate to call when the connection to the updater has been interrupted or invalidated. Note this class is still re-usable + in the case that the connection should be later resumed. + */ +- (void)invalidate; + +@end diff --git a/TestApplication/SUInstallUpdateViewController.h b/TestApplication/SUInstallUpdateViewController.h new file mode 100644 index 00000000..ad9567c4 --- /dev/null +++ b/TestApplication/SUInstallUpdateViewController.h @@ -0,0 +1,18 @@ +// +// SUInstallUpdateViewController.h +// Sparkle +// +// Created by Mayur Pawashe on 3/5/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import +#import "SUStatusCompletionResults.h" + +@class SUAppcastItem; + +@interface SUInstallUpdateViewController : NSViewController + +- (instancetype)initWithAppcastItem:(SUAppcastItem *)appcastItem reply:(void (^)(SUUpdateAlertChoice))reply; + +@end diff --git a/TestApplication/SUInstallUpdateViewController.m b/TestApplication/SUInstallUpdateViewController.m new file mode 100644 index 00000000..486c1e99 --- /dev/null +++ b/TestApplication/SUInstallUpdateViewController.m @@ -0,0 +1,73 @@ +// +// SUInstallUpdateViewController.m +// Sparkle +// +// Created by Mayur Pawashe on 3/5/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUInstallUpdateViewController.h" +#import "SUAppcastItem.h" + +@interface SUInstallUpdateViewController () + +@property (nonatomic) IBOutlet NSTextView *textView; +@property (nonatomic, readonly) SUAppcastItem *appcastItem; +@property (nonatomic, copy) void (^reply)(SUUpdateAlertChoice); + +@end + +@implementation SUInstallUpdateViewController + +@synthesize textView = _textView; +@synthesize appcastItem = _appcastItem; +@synthesize reply = _reply; + +- (instancetype)initWithAppcastItem:(SUAppcastItem *)appcastItem reply:(void (^)(SUUpdateAlertChoice))reply +{ + self = [super initWithNibName:@"SUInstallUpdateViewController" bundle:nil]; + if (self != nil) { + _appcastItem = appcastItem; + self.reply = reply; + } + return self; +} + +- (void)viewDidLoad +{ + [super viewDidLoad]; + + [[self.textView enclosingScrollView] setDrawsBackground:NO]; + [self.textView setDrawsBackground:NO]; + + NSString *descriptionHTML = self.appcastItem.itemDescription; + NSData *htmlData = [descriptionHTML dataUsingEncoding:NSUTF8StringEncoding]; + NSAttributedString *attributedString = [[NSAttributedString alloc] initWithHTML:htmlData documentAttributes:NULL]; + [self.textView.textStorage setAttributedString:attributedString]; +} + +- (IBAction)installUpdate:(id)__unused sender +{ + if (self.reply != nil) { + self.reply(SUInstallUpdateChoice); + self.reply = nil; + } +} + +- (IBAction)installUpdateLater:(id)__unused sender +{ + if (self.reply != nil) { + self.reply(SUInstallLaterChoice); + self.reply = nil; + } +} + +- (IBAction)skipUpdate:(id)__unused sender +{ + if (self.reply != nil) { + self.reply(SUSkipThisVersionChoice); + self.reply = nil; + } +} + +@end diff --git a/TestApplication/SUInstallUpdateViewController.xib b/TestApplication/SUInstallUpdateViewController.xib new file mode 100644 index 00000000..847ddedc --- /dev/null +++ b/TestApplication/SUInstallUpdateViewController.xib @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/TestApplication/SUPopUpTitlebarUserDriver.h b/TestApplication/SUPopUpTitlebarUserDriver.h new file mode 100644 index 00000000..5ab56fbb --- /dev/null +++ b/TestApplication/SUPopUpTitlebarUserDriver.h @@ -0,0 +1,18 @@ +// +// SUPopUpTitlebarUserDriver.h +// Sparkle +// +// Created by Mayur Pawashe on 3/5/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import +#import + +@protocol SUStandardUserDriver; + +@interface SUPopUpTitlebarUserDriver : NSObject + +- (instancetype)initWithWindow:(NSWindow *)window delegate:(id)delegate; + +@end diff --git a/TestApplication/SUPopUpTitlebarUserDriver.m b/TestApplication/SUPopUpTitlebarUserDriver.m new file mode 100644 index 00000000..ef1263cb --- /dev/null +++ b/TestApplication/SUPopUpTitlebarUserDriver.m @@ -0,0 +1,376 @@ +// +// SUPopUpTitlebarUserDriver.m +// Sparkle +// +// Created by Mayur Pawashe on 3/5/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUPopUpTitlebarUserDriver.h" +#import "SUStandardUserDriverDelegate.h" +#import "SUUserDriverCoreComponent.h" +#import "SUUserDriverUIComponent.h" +#import "SUUserDriverCoreComponent.h" +#import "SUUserDriverUIComponent.h" +#import "SUInstallUpdateViewController.h" + +@interface SUPopUpTitlebarUserDriver() + +@property (nonatomic, readonly) NSWindow *window; +@property (nonatomic, readonly) SUUserDriverCoreComponent *coreComponent; +@property (nonatomic, readonly) SUUserDriverUIComponent *uiComponent; +@property (nonatomic) NSTitlebarAccessoryViewController *accessoryViewController; +@property (nonatomic) BOOL addedAccessory; +@property (nonatomic) NSButton *updateButton; +@property (nonatomic, copy) void (^updateButtonAction)(NSButton *); +@property (nonatomic) NSUInteger expectedContentLength; +@property (nonatomic) NSUInteger contentLengthDownloaded; + +@end + +@implementation SUPopUpTitlebarUserDriver + +@synthesize delegate = _delegate; +@synthesize window = _window; +@synthesize coreComponent = _coreComponent; +@synthesize uiComponent = _uiComponent; +@synthesize accessoryViewController = _accessoryViewController; +@synthesize addedAccessory = _addedAccessory; +@synthesize updateButton = _updateButton; +@synthesize updateButtonAction = _updateButtonAction; +@synthesize expectedContentLength = _expectedContentLength; +@synthesize contentLengthDownloaded = _contentLengthDownloaded; + +- (instancetype)initWithWindow:(NSWindow *)window delegate:(id)delegate +{ + self = [super init]; + if (self != nil) { + _window = window; + _delegate = delegate; + _coreComponent = [[SUUserDriverCoreComponent alloc] initWithUserDriver:self delegate:delegate]; + _uiComponent = [[SUUserDriverUIComponent alloc] initWithUserDriver:self delegate:delegate]; + } + return self; +} + +- (void)addUpdateButtonWithTitle:(NSString *)title +{ + [self addUpdateButtonWithTitle:title action:nil]; +} + +- (void)addUpdateButtonWithTitle:(NSString *)title action:(void (^)(NSButton *button))action +{ + if (self.updateButton == nil) { + NSButton *updateButton = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 160, 100)]; + updateButton.title = title; + updateButton.bezelStyle = NSRecessedBezelStyle; + + self.updateButton = updateButton; + } else { + self.updateButton.title = title; + } + + if (action != nil) { + self.updateButton.target = self; + self.updateButton.action = @selector(updateButtonAction:); + self.updateButtonAction = action; + self.updateButton.enabled = YES; + } else { + self.updateButton.enabled = NO; + self.updateButton.target = nil; + self.updateButtonAction = nil; + } + + if (self.accessoryViewController == nil) { + self.accessoryViewController = [[NSTitlebarAccessoryViewController alloc] init]; + self.accessoryViewController.layoutAttribute = NSLayoutAttributeRight; + self.accessoryViewController.view = self.updateButton; + } + + if (!self.addedAccessory) { + [self.window addTitlebarAccessoryViewController:self.accessoryViewController]; + self.addedAccessory = YES; + } +} + +- (void)updateButtonAction:(NSButton *)sender +{ + if (self.updateButtonAction != nil) { + self.updateButtonAction(sender); + } +} + +- (void)removeUpdateButton +{ + [self.accessoryViewController removeFromParentViewController]; + self.addedAccessory = NO; + self.updateButtonAction = nil; +} + +#pragma mark Is Update Busy? + +- (void)idleOnUpdateChecks:(BOOL)shouldIdleOnUpdateChecks +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent idleOnUpdateChecks:shouldIdleOnUpdateChecks]; + }); +} + +- (BOOL)idlesOnUpdateChecks +{ + return self.coreComponent.idlesOnUpdateChecks; +} + +- (void)showUpdateInProgress:(BOOL)isUpdateInProgress +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent showUpdateInProgress:isUpdateInProgress]; + }); +} + +- (BOOL)isUpdateInProgress +{ + return self.coreComponent.updateInProgress; +} + +#pragma mark Check Updates Timer + +- (BOOL)willInitiateNextUpdateCheck +{ + return [self.coreComponent willInitiateNextUpdateCheck]; +} + +- (void)startUpdateCheckTimerWithNextTimeInterval:(NSTimeInterval)timeInterval reply:(void (^)(SUUpdateCheckTimerStatus))reply +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent startUpdateCheckTimerWithNextTimeInterval:timeInterval reply:reply]; + }); +} + +- (void)invalidateUpdateCheckTimer +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent invalidateUpdateCheckTimer]; + }); +} + +#pragma mark Update Permission + +- (void)requestUpdatePermissionWithSystemProfile:(NSArray *)__unused systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))reply +{ + // Just make a decision.. + dispatch_async(dispatch_get_main_queue(), ^{ + reply([SUUpdatePermissionPromptResult updatePermissionPromptResultWithChoice:SUAutomaticallyCheck shouldSendProfile:NO]); + }); +} + +#pragma mark Update Found + +- (void)showUpdateWithAppcastItem:(SUAppcastItem *)appcastItem reply:(void (^)(SUUpdateAlertChoice))reply +{ + NSPopover *popover = [[NSPopover alloc] init]; + popover.behavior = NSPopoverBehaviorTransient; + + [self addUpdateButtonWithTitle:@"Update Available" action:^(NSButton *button) { + if (popover.contentViewController == nil) { + popover.contentViewController = [[SUInstallUpdateViewController alloc] initWithAppcastItem:appcastItem reply:^(SUUpdateAlertChoice choice) { + reply(choice); + + [popover close]; + button.enabled = NO; + }]; + } + + [popover showRelativeToRect:button.bounds ofView:button preferredEdge:NSMaxYEdge]; + }]; +} + +- (void)showUpdateFoundWithAppcastItem:(SUAppcastItem *)appcastItem allowsAutomaticUpdates:(BOOL)__unused allowsAutomaticUpdates reply:(void (^)(SUUpdateAlertChoice))reply +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self showUpdateWithAppcastItem:appcastItem reply:reply]; + }); +} + +- (void)showAutomaticUpdateFoundWithAppcastItem:(SUAppcastItem *)appcastItem reply:(void (^)(SUUpdateAlertChoice))reply +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self showUpdateWithAppcastItem:appcastItem reply:reply]; + }); +} + +#pragma mark Install & Relaunch Update + +- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent registerInstallUpdateHandler:installUpdateHandler]; + + [self addUpdateButtonWithTitle:@"Install & Relaunch" action:^(NSButton *__unused button) { + installUpdateHandler(SUInstallAndRelaunchUpdateNow); + }]; + }); +} + +#pragma mark Check for Updates + +- (void)showUserInitiatedUpdateCheckWithCompletion:(void (^)(SUUserInitiatedCheckStatus))updateCheckStatusCompletion +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent registerUpdateCheckStatusHandler:updateCheckStatusCompletion]; + + [self addUpdateButtonWithTitle:@"Checking for Updates…"]; + }); +} + +- (void)dismissUserInitiatedUpdateCheck +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent completeUpdateCheckStatus]; + [self removeUpdateButton]; + }); +} + +#pragma mark Update Errors + +- (void)showUpdaterError:(NSError *)error +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self addUpdateButtonWithTitle:@"Update Failed"]; + NSLog(@"Error: %@", error); + }); +} + +- (void)showUpdateNotFound +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self addUpdateButtonWithTitle:@"No Update Found"]; + }); +} + +#pragma mark Download & Install Updates + +- (void)showDownloadInitiatedWithCompletion:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent registerDownloadStatusHandler:downloadUpdateStatusCompletion]; + }); +} + +- (void)showDownloadDidReceiveResponse:(NSURLResponse *)response +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self addUpdateButtonWithTitle:@"Downloading…"]; + self.contentLengthDownloaded = 0; + self.expectedContentLength = (NSUInteger)response.expectedContentLength; + if (self.expectedContentLength == 0) { + // don't divide by zero ever + self.expectedContentLength = 1; + } + }); +} + +- (void)showDownloadDidReceiveDataOfLength:(NSUInteger)length +{ + dispatch_async(dispatch_get_main_queue(), ^{ + self.contentLengthDownloaded += length; + double progress = (double)self.contentLengthDownloaded / self.expectedContentLength; + [self addUpdateButtonWithTitle:[NSString stringWithFormat:@"Downloading (%d%%)", (int)progress] action:nil]; + }); +} + +- (void)showDownloadFinishedAndStartedExtractingUpdate +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreComponent completeDownloadStatus]; + [self addUpdateButtonWithTitle:@"Extracting…"]; + }); +} + +- (void)showExtractionReceivedProgress:(double)progress +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self addUpdateButtonWithTitle:[NSString stringWithFormat:@"Extracting (%d%%)…", (int)(progress * 100)]]; + }); +} + +- (void)showInstallingUpdate +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self addUpdateButtonWithTitle:@"Installing…"]; + }); +} + +#pragma mark Application Death + +- (void)registerApplicationTermination:(void (^)(SUApplicationTerminationStatus))applicationTerminationHandler +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.uiComponent registerApplicationTermination:applicationTerminationHandler]; + }); +} + +- (void)unregisterApplicationTermination +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.uiComponent unregisterApplicationTermination]; + }); +} + +- (NSApplicationTerminateReply)sendApplicationTerminationSignal +{ + return [self.uiComponent sendApplicationTerminationSignal]; +} + +- (void)terminateApplication +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.uiComponent terminateApplication]; + }); +} + +#pragma mark System Death + +- (void)registerSystemPowerOff:(void (^)(SUSystemPowerOffStatus))systemPowerOffHandler +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.uiComponent registerSystemPowerOff:systemPowerOffHandler]; + }); +} + +- (void)unregisterSystemPowerOff +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.uiComponent unregisterSystemPowerOff]; + }); +} + +#pragma mark Aborting Everything + +- (void)_dismissUpdateInstallation +{ + // Make sure everything we call here does not dispatch async to main queue + // because we are already on the main queue (and I've been bitten in the past by this before) + + [self.coreComponent dismissUpdateInstallation]; + [self.uiComponent dismissUpdateInstallation]; + + [self removeUpdateButton]; +} + +- (void)dismissUpdateInstallation +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self _dismissUpdateInstallation]; + }); +} + +- (void)invalidate +{ + [self.coreComponent invalidate]; + [self.uiComponent invalidate]; + + [self _dismissUpdateInstallation]; +} + +@end diff --git a/TestApplication/SURemoteUpdateSettingsWindowController.m b/TestApplication/SURemoteUpdateSettingsWindowController.m index 96f98655..80a8b218 100644 --- a/TestApplication/SURemoteUpdateSettingsWindowController.m +++ b/TestApplication/SURemoteUpdateSettingsWindowController.m @@ -8,11 +8,12 @@ #import "SURemoteUpdateSettingsWindowController.h" #import "TestAppHelperProtocol.h" +#import "SUPopUpTitlebarUserDriver.h" @interface SURemoteUpdateSettingsWindowController () @property (nonatomic) NSXPCConnection *connection; -@property (nonatomic) SUStandardUserDriver *userDriver; +@property (nonatomic) id userDriver; @property (nonatomic) BOOL isTerminating; @@ -54,7 +55,10 @@ self.connection = [[NSXPCConnection alloc] initWithServiceName:@"org.sparkle-project.TestAppHelper"]; self.connection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUUserDriver)]; - self.userDriver = [[SUStandardUserDriver alloc] initWithHostBundle:[NSBundle mainBundle] delegate:self]; + + self.userDriver = [[SUPopUpTitlebarUserDriver alloc] initWithWindow:self.window delegate:self]; + //self.userDriver = [[SUStandardUserDriver alloc] initWithHostBundle:[NSBundle mainBundle] delegate:self]; + self.connection.exportedObject = self.userDriver; self.connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(TestAppHelperProtocol)]; diff --git a/TestApplication/SURemoteUpdateSettingsWindowController.xib b/TestApplication/SURemoteUpdateSettingsWindowController.xib index 5e78998e..45e3f9f9 100644 --- a/TestApplication/SURemoteUpdateSettingsWindowController.xib +++ b/TestApplication/SURemoteUpdateSettingsWindowController.xib @@ -16,7 +16,7 @@ - +