diff --git a/Sparkle/SUStandardUserDriver.m b/Sparkle/SUStandardUserDriver.m index 840a2674..2bb75152 100644 --- a/Sparkle/SUStandardUserDriver.m +++ b/Sparkle/SUStandardUserDriver.m @@ -116,6 +116,9 @@ - (void)setUpFocusForActiveUpdateAlert { + // Make sure the window is loaded in any case + [self.activeUpdateAlert window]; + // If the app is a menubar app or the like, we need to focus it first and alter the // update prompt to behave like a normal window. Otherwise if the window were hidden // there may be no way for the application to be activated to make it visible again. @@ -158,6 +161,21 @@ }); } +- (void)showUpdateReleaseNotes:(NSData *)releaseNotes +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.activeUpdateAlert showReleaseNotes:releaseNotes]; + }); +} + +- (void)showUpdateReleaseNotesFailedToDownloadWithError:(NSError *)error +{ + dispatch_async(dispatch_get_main_queue(), ^{ + NSLog(@"Failed to download release notes with error: %@", error); + [self.activeUpdateAlert showReleaseNotesFailedToDownload]; + }); +} + #pragma mark Install & Relaunch Update - (void)showReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 03f3c892..95786f4d 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -14,6 +14,7 @@ #import "SUUpdaterDelegate.h" #import "SUAppcastItem.h" #import "SUErrors.h" +#import "SUURLDownload.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -84,6 +85,19 @@ [self updateAlertFinishedForUpdateItem:updateItem withChoice:choice]; }); }]; + + if (updateItem.releaseNotesURL != nil) { + NSURLRequest *request = [NSURLRequest requestWithURL:updateItem.releaseNotesURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; + + id userDriver = self.userDriver; + SUDownloadURLWithRequest(request, ^(NSData * _Nullable data, NSError * _Nullable error) { + if (data != nil) { + [userDriver showUpdateReleaseNotes:(NSData * _Nonnull)data]; + } else { + [userDriver showUpdateReleaseNotesFailedToDownloadWithError:(NSError * _Nonnull)error]; + } + }); + } } - (void)updateAlertFinishedForUpdateItem:(SUAppcastItem *)updateItem withChoice:(SUUpdateAlertChoice)choice diff --git a/Sparkle/SUURLDownload.h b/Sparkle/SUURLDownload.h index 1b0e92f3..61e52498 100644 --- a/Sparkle/SUURLDownload.h +++ b/Sparkle/SUURLDownload.h @@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN -// From the completion block, either data or error can be nil (or both can be nil if an unknown error occured) +// From the completion block, either data or error can be nil, but not both // The completion block returns on the main queue void SUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(NSData * _Nullable data, NSError * _Nullable error)); diff --git a/Sparkle/SUURLDownload.m b/Sparkle/SUURLDownload.m index 44798736..29fe5a7d 100644 --- a/Sparkle/SUURLDownload.m +++ b/Sparkle/SUURLDownload.m @@ -10,6 +10,7 @@ #import "SUXPCServiceInfo.h" #import "SUAppcastDownloader.h" #import "SUAppcastDownloaderProtocol.h" +#import "SUErrors.h" void SUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(NSData * _Nullable, NSError * _Nullable)) { @@ -35,7 +36,7 @@ void SUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(NS connection.invalidationHandler = ^{ dispatch_async(dispatch_get_main_queue(), ^{ if (!retrievedDownloadResult) { - completionBlock(nil, nil); + completionBlock(nil, [NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:nil]); } }); }; diff --git a/Sparkle/SUUpdateAlert.h b/Sparkle/SUUpdateAlert.h index 14f57459..d87aedcf 100644 --- a/Sparkle/SUUpdateAlert.h +++ b/Sparkle/SUUpdateAlert.h @@ -22,6 +22,9 @@ - (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)aHost versionDisplayer:(id )aVersionDisplayer allowsAutomaticUpdates:(BOOL)allowsAutomaticUpdates alreadyDownloaded:(BOOL)alreadyDownloaded completionBlock:(void (^)(SUUpdateAlertChoice))block; +- (void)showReleaseNotes:(NSData *)releaseNotes; +- (void)showReleaseNotesFailedToDownload; + - (IBAction)installUpdate:sender; - (IBAction)skipThisVersion:sender; - (IBAction)remindMeLater:sender; diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index d2842e6d..6d6f336a 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -143,7 +143,7 @@ // "-apple-system-font" is a reference to the system UI font on OS X. "-apple-system" is the new recommended token, but for backward compatibility we can't use it. prefs.standardFontFamily = @"-apple-system-font"; prefs.defaultFontSize = (int)[NSFont systemFontSize]; - + // Stick a nice big spinner in the middle of the web view until the page is loaded. NSRect frame = [[self.releaseNotesView superview] frame]; self.releaseNotesSpinner = [[NSProgressIndicator alloc] initWithFrame:NSMakeRect(NSMidX(frame) - 16, NSMidY(frame) - 16, 32, 32)]; @@ -151,18 +151,34 @@ [self.releaseNotesSpinner startAnimation:self]; self.webViewFinishedLoading = NO; [[self.releaseNotesView superview] addSubview:self.releaseNotesSpinner]; - - // If there's a release notes URL, load it; otherwise, just stick the contents of the description into the web view. - if ([self.updateItem releaseNotesURL]) - { - [[self.releaseNotesView mainFrame] loadRequest:[NSURLRequest requestWithURL:[self.updateItem releaseNotesURL] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]]; - } - else + + // If there's no release notes URL, just stick the contents of the description into the web view + // Otherwise we'll wait until the client wants us to show release notes + if (self.updateItem.releaseNotesURL == nil) { [[self.releaseNotesView mainFrame] loadHTMLString:[self.updateItem itemDescription] baseURL:nil]; } } +- (void)showReleaseNotes:(NSData *)releaseNotes +{ + if (!self.webViewFinishedLoading) { + [[self.releaseNotesView mainFrame] loadData:releaseNotes MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:nil]; + } +} + +- (void)showReleaseNotesFailedToDownload +{ + [self stopReleaseNotesSpinner]; + self.webViewFinishedLoading = YES; +} + +- (void)stopReleaseNotesSpinner +{ + [self.releaseNotesSpinner stopAnimation:self]; + [self.releaseNotesSpinner setHidden:YES]; +} + - (BOOL)showsReleaseNotes { NSNumber *shouldShowReleaseNotes = [self.host objectForInfoDictionaryKey:SUShowReleaseNotesKey]; @@ -287,8 +303,8 @@ - (void)webView:(WebView *)sender didFinishLoadForFrame:frame { if ([frame parentFrame] == nil) { + [self stopReleaseNotesSpinner]; self.webViewFinishedLoading = YES; - [self.releaseNotesSpinner setHidden:YES]; [sender display]; // necessary to prevent weird scroll bar artifacting } } diff --git a/Sparkle/SUUserDriver.h b/Sparkle/SUUserDriver.h index 1d941d8f..2e366c2b 100644 --- a/Sparkle/SUUserDriver.h +++ b/Sparkle/SUUserDriver.h @@ -137,6 +137,32 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)showUpdateFoundWithAppcastItem:(SUAppcastItem *)appcastItem allowsAutomaticUpdates:(BOOL)allowsAutomaticUpdates alreadyDownloaded:(BOOL)alreadyDownloaded reply:(void (^)(SUUpdateAlertChoice))reply; +/*! + * Show the user the release notes for the new update + * + * Display the release notes to the user. This will be called after showing the new update. + * This is only applicable if the release notes are linked from the appcast, and are not directly embedded inside of the appcast file. + * That is, this may be invoked if the releaseNotesURL from the appcast item is non-nil. + * + * @param releaseNotes The data for the release notes that was downloaded from the new update's appcast. + * + * This can be called from any thread + */ +- (void)showUpdateReleaseNotes:(NSData *)releaseNotes; + +/*! + * Show the user that the new update's release notes could not be downloaded + * + * This will be called after showing the new update. + * This is only applicable if the release notes are linked from the appcast, and are not directly embedded inside of the appcast file. + * That is, this may be invoked if the releaseNotesURL from the appcast item is non-nil. + * + * @param error The error associated with why the new update's release notes could not be downloaded. + * + * This can be called from any thread + */ +- (void)showUpdateReleaseNotesFailedToDownloadWithError:(NSError *)error; + /*! * Show the user a new update was not found * diff --git a/TestApplication/SUInstallUpdateViewController.m b/TestApplication/SUInstallUpdateViewController.m index 96ece2f4..c9a0c7bb 100644 --- a/TestApplication/SUInstallUpdateViewController.m +++ b/TestApplication/SUInstallUpdateViewController.m @@ -47,10 +47,14 @@ [[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]; + if (self.appcastItem.releaseNotesURL == nil) { + NSString *descriptionHTML = self.appcastItem.itemDescription; + if (descriptionHTML == nil) { + NSData *htmlData = [descriptionHTML dataUsingEncoding:NSUTF8StringEncoding]; + NSAttributedString *attributedString = [[NSAttributedString alloc] initWithHTML:htmlData documentAttributes:NULL]; + [self.textView.textStorage setAttributedString:attributedString]; + } + } } - (IBAction)installUpdate:(id)__unused sender diff --git a/TestApplication/SUPopUpTitlebarUserDriver.m b/TestApplication/SUPopUpTitlebarUserDriver.m index b5947be4..e603cbb9 100644 --- a/TestApplication/SUPopUpTitlebarUserDriver.m +++ b/TestApplication/SUPopUpTitlebarUserDriver.m @@ -186,6 +186,14 @@ }); } +- (void)showUpdateReleaseNotes:(NSData *)__unused releaseNotes +{ +} + +- (void)showUpdateReleaseNotesFailedToDownloadWithError:(NSError *)__unused error +{ +} + #pragma mark Install & Relaunch Update - (void)showReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler diff --git a/Tests/SUUpdaterTest.m b/Tests/SUUpdaterTest.m index eab911d0..d4e3d49e 100644 --- a/Tests/SUUpdaterTest.m +++ b/Tests/SUUpdaterTest.m @@ -35,6 +35,10 @@ - (void)showUpdateFoundWithAppcastItem:(SUAppcastItem *)__unused appcastItem allowsAutomaticUpdates:(BOOL)__unused allowsAutomaticUpdates alreadyDownloaded:(BOOL)__unused alreadyDownloaded reply:(void (^)(SUUpdateAlertChoice))__unused reply {} +- (void)showUpdateReleaseNotes:(NSData *)__unused releaseNotes {} + +- (void)showUpdateReleaseNotesFailedToDownloadWithError:(NSError *)__unused error {} + - (void)showUpdateNotFoundWithAcknowledgement:(void (^)(void))__unused acknowledgement {} - (void)showUpdaterError:(NSError *)__unused error acknowledgement:(void (^)(void))__unused acknowledgement {} diff --git a/sparkle-cli/SUCommandLineUserDriver.m b/sparkle-cli/SUCommandLineUserDriver.m index ea6f43bb..4b4f8ac0 100644 --- a/sparkle-cli/SUCommandLineUserDriver.m +++ b/sparkle-cli/SUCommandLineUserDriver.m @@ -129,6 +129,14 @@ }); } +- (void)showUpdateReleaseNotes:(NSData *)__unused releaseNotes +{ +} + +- (void)showUpdateReleaseNotesFailedToDownloadWithError:(NSError *)__unused error +{ +} + - (void)showUpdateNotFoundWithAcknowledgement:(void (^)(void))__unused acknowledgement { dispatch_async(dispatch_get_main_queue(), ^{