From 8af18843ef12c3271b61df2d19d175a2e0144daa Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 28 Feb 2016 11:36:11 -0500 Subject: [PATCH] Move AppKit functionality from SUHost to SUApplicationInfo These operations also make incorrect assumptions when called from out of process, so it makes sense to separate them out. Furthermore, SUHost can now only depend on Foundation --- Sparkle.xcodeproj/project.pbxproj | 10 +++++++ Sparkle/SUApplicationInfo.h | 17 +++++++++++ Sparkle/SUApplicationInfo.m | 44 ++++++++++++++++++++++++++++ Sparkle/SUAutomaticUpdateAlert.m | 3 +- Sparkle/SUHost.h | 4 +-- Sparkle/SUHost.m | 32 ++------------------ Sparkle/SUSparkleUserUpdaterDriver.m | 9 +++--- Sparkle/SUStatusController.m | 5 ++-- Sparkle/SUUpdateAlert.m | 5 ++-- Sparkle/SUUpdatePermissionPrompt.m | 5 ++-- 10 files changed, 90 insertions(+), 44 deletions(-) create mode 100644 Sparkle/SUApplicationInfo.h create mode 100644 Sparkle/SUApplicationInfo.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index a8e09501..58ba368d 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -179,6 +179,9 @@ 721CF1AA1AD7647000D9AC09 /* libxar.1.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D1AF5890FD7678C0065DB48 /* libxar.1.dylib */; }; 721CF1AB1AD764EB00D9AC09 /* libbz2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D06E8FB0FD68D61005AE3F6 /* libbz2.dylib */; }; 7223E7631AD1AEFF008E3161 /* sais.c in Sources */ = {isa = PBXBuildFile; fileRef = 7223E7611AD1AEFF008E3161 /* sais.c */; }; + 725602D51C83551C00DAA70E /* SUApplicationInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 725602D31C83551C00DAA70E /* SUApplicationInfo.h */; }; + 725602D61C83551C00DAA70E /* SUApplicationInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 725602D41C83551C00DAA70E /* SUApplicationInfo.m */; }; + 725602D71C83576100DAA70E /* SUApplicationInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 725602D41C83551C00DAA70E /* SUApplicationInfo.m */; }; 725CB9571C7120410064365A /* SUUserUpdaterDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 725CB9561C7120410064365A /* SUUserUpdaterDriver.h */; settings = {ATTRIBUTES = (Public, ); }; }; 725CB95A1C7121830064365A /* SUSparkleUserUpdaterDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 725CB9581C7121830064365A /* SUSparkleUserUpdaterDriver.h */; settings = {ATTRIBUTES = (Public, ); }; }; 725CB95B1C7121830064365A /* SUSparkleUserUpdaterDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 725CB9591C7121830064365A /* SUSparkleUserUpdaterDriver.m */; }; @@ -639,6 +642,8 @@ 72162B071C82C9600013C1C5 /* SULocalizations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SULocalizations.h; sourceTree = ""; }; 7223E7611AD1AEFF008E3161 /* sais.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sais.c; sourceTree = ""; }; 7223E7621AD1AEFF008E3161 /* sais.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sais.h; sourceTree = ""; }; + 725602D31C83551C00DAA70E /* SUApplicationInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUApplicationInfo.h; sourceTree = ""; }; + 725602D41C83551C00DAA70E /* SUApplicationInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUApplicationInfo.m; sourceTree = ""; }; 725CB9561C7120410064365A /* SUUserUpdaterDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUserUpdaterDriver.h; sourceTree = ""; }; 725CB9581C7121830064365A /* SUSparkleUserUpdaterDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUSparkleUserUpdaterDriver.h; sourceTree = ""; }; 725CB9591C7121830064365A /* SUSparkleUserUpdaterDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUSparkleUserUpdaterDriver.m; sourceTree = ""; }; @@ -1027,6 +1032,8 @@ 72A450521C69A68900D67EEA /* SUUpdatePermissionPromptResult.m */, 612DCBAD0D488BC60015DBEA /* SUUpdatePermissionPrompt.h */, 612DCBAE0D488BC60015DBEA /* SUUpdatePermissionPrompt.m */, + 725602D31C83551C00DAA70E /* SUApplicationInfo.h */, + 725602D41C83551C00DAA70E /* SUApplicationInfo.m */, 61180BC80D64138900B4E0D1 /* SUWindowController.h */, 61180BC90D64138900B4E0D1 /* SUWindowController.m */, ); @@ -1216,6 +1223,7 @@ 55E6F33319EC9F6C00005E76 /* SUErrors.h in Headers */, 14652F8419A978C200959E44 /* SUExport.h in Headers */, 7275F9C11B5F1F2900B1D19E /* SUFileManager.h in Headers */, + 725602D51C83551C00DAA70E /* SUApplicationInfo.h in Headers */, 767B61AC1972D488004E0C3C /* SUGuidedPackageInstaller.h in Headers */, 618FA5010DAE88B40026945C /* SUInstaller.h in Headers */, 55C14F06136EF6DB00649790 /* SULog.h in Headers */, @@ -1625,6 +1633,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 725602D71C83576100DAA70E /* SUApplicationInfo.m in Sources */, 55C14BD4136EEFCE00649790 /* Autoupdate.m in Sources */, F8761EB61ADC5E7A000C9034 /* SUCodeSigningVerifier.m in Sources */, 55C14F00136EF6B700649790 /* SUConstants.m in Sources */, @@ -1721,6 +1730,7 @@ 61299A6009CA6EB100B7442F /* SUConstants.m in Sources */, 6102FE4B0E07803800F85D09 /* SUDiskImageUnarchiver.m in Sources */, 61299A3009CA2DAB00B7442F /* SUDSAVerifier.m in Sources */, + 725602D61C83551C00DAA70E /* SUApplicationInfo.m in Sources */, 72A450541C69A68900D67EEA /* SUUpdatePermissionPromptResult.m in Sources */, 7275F9C21B5F1F2900B1D19E /* SUFileManager.m in Sources */, 767B61AD1972D488004E0C3C /* SUGuidedPackageInstaller.m in Sources */, diff --git a/Sparkle/SUApplicationInfo.h b/Sparkle/SUApplicationInfo.h new file mode 100644 index 00000000..a53d57db --- /dev/null +++ b/Sparkle/SUApplicationInfo.h @@ -0,0 +1,17 @@ +// +// SUApplicationInfo.h +// Sparkle +// +// Created by Mayur Pawashe on 2/28/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +@interface SUApplicationInfo : NSObject + ++ (BOOL)isBackgroundApplication:(NSApplication * __nonnull)application; + ++ (NSImage *__nonnull)bestIconForBundle:(NSBundle * __nonnull)bundle; + +@end diff --git a/Sparkle/SUApplicationInfo.m b/Sparkle/SUApplicationInfo.m new file mode 100644 index 00000000..2515a186 --- /dev/null +++ b/Sparkle/SUApplicationInfo.m @@ -0,0 +1,44 @@ +// +// SUApplicationInfo.m +// Sparkle +// +// Created by Mayur Pawashe on 2/28/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUApplicationInfo.h" + +@implementation SUApplicationInfo + ++ (BOOL)isBackgroundApplication:(NSApplication * __nonnull)application +{ + return (application.activationPolicy == NSApplicationActivationPolicyAccessory); +} + ++ (NSImage *__nonnull)bestIconForBundle:(NSBundle * __nonnull)bundle +{ + // Cache the application icon. + NSString *iconPath = [bundle pathForResource:[bundle objectForInfoDictionaryKey:@"CFBundleIconFile"] ofType:@"icns"]; + // According to the OS X docs, "CFBundleIconFile - This key identifies the file containing + // the icon for the bundle. The filename you specify does not need to include the .icns + // extension, although it may." + // + // However, if it *does* include the '.icns' the above method fails (tested on OS X 10.3.9) so we'll also try: + if (!iconPath) { + iconPath = [bundle pathForResource:[bundle objectForInfoDictionaryKey:@"CFBundleIconFile"] ofType:nil]; + } + NSImage *icon = [[NSImage alloc] initWithContentsOfFile:iconPath]; + // Use a default icon if none is defined. + if (!icon) { + // this asumption may not be correct (eg. even though we're not the main bundle, it could be still be a regular app) + // but still better than nothing if no icon was included + BOOL isMainBundle = (bundle == [NSBundle mainBundle]); + + NSString *fileType = isMainBundle ? (__bridge NSString *)kUTTypeApplication : (__bridge NSString *)kUTTypeBundle; + icon = [[NSWorkspace sharedWorkspace] iconForFileType:fileType]; + } + return icon; +} + + +@end diff --git a/Sparkle/SUAutomaticUpdateAlert.m b/Sparkle/SUAutomaticUpdateAlert.m index c4a324b6..0cbd902c 100644 --- a/Sparkle/SUAutomaticUpdateAlert.m +++ b/Sparkle/SUAutomaticUpdateAlert.m @@ -11,6 +11,7 @@ #import "SUHost.h" #import "SULocalizations.h" #import "SUAppcastItem.h" +#import "SUApplicationInfo.h" @interface SUAutomaticUpdateAlert () @property (strong) void(^completionBlock)(SUAutomaticInstallationChoice); @@ -61,7 +62,7 @@ - (NSImage *__nonnull)applicationIcon { - return [self.host icon]; + return [SUApplicationInfo bestIconForBundle:self.host.bundle]; } - (NSString *__nonnull)titleText diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index 7d13919f..5433bad1 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -5,7 +5,7 @@ // Copyright 2008 Andy Matuschak. All rights reserved. // -#import +#import @interface SUHost : NSObject @@ -19,9 +19,7 @@ @property (readonly, copy) NSString *name; @property (readonly, copy) NSString *version; @property (readonly, copy) NSString *displayVersion; -@property (readonly, copy) NSImage *icon; @property (getter=isRunningOnReadOnlyVolume, readonly) BOOL runningOnReadOnlyVolume; -@property (getter=isBackgroundApplication, readonly) BOOL backgroundApplication; @property (readonly, copy) NSString *publicDSAKey; - (id)objectForInfoDictionaryKey:(NSString *)key; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 77b54e26..c412011a 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -7,6 +7,8 @@ #import "SUHost.h" +// This is a "core" class and thus should NOT import Cocoa/AppKit + #import "SUConstants.h" #include // For statfs for isRunningOnReadOnlyVolume #import "SULog.h" @@ -157,31 +159,6 @@ return [self version]; // Fall back on the normal version string. } -- (NSImage *__nonnull)icon -{ - // Cache the application icon. - NSString *iconPath = [self.bundle pathForResource:[self.bundle objectForInfoDictionaryKey:@"CFBundleIconFile"] ofType:@"icns"]; - // According to the OS X docs, "CFBundleIconFile - This key identifies the file containing - // the icon for the bundle. The filename you specify does not need to include the .icns - // extension, although it may." - // - // However, if it *does* include the '.icns' the above method fails (tested on OS X 10.3.9) so we'll also try: - if (!iconPath) { - iconPath = [self.bundle pathForResource:[self.bundle objectForInfoDictionaryKey:@"CFBundleIconFile"] ofType:nil]; - } - NSImage *icon = [[NSImage alloc] initWithContentsOfFile:iconPath]; - // Use a default icon if none is defined. - if (!icon) { - // this asumption may not be correct (eg. even though we're not the main bundle, it could be still be a regular app) - // but still better than nothing if no icon was included - BOOL isMainBundle = (self.bundle == [NSBundle mainBundle]); - - NSString *fileType = isMainBundle ? (__bridge NSString *)kUTTypeApplication : (__bridge NSString *)kUTTypeBundle; - icon = [[NSWorkspace sharedWorkspace] iconForFileType:fileType]; - } - return icon; -} - - (BOOL)isRunningOnReadOnlyVolume { struct statfs statfs_info; @@ -189,11 +166,6 @@ return (statfs_info.f_flags & MNT_RDONLY) != 0; } -- (BOOL)isBackgroundApplication -{ - return ([[NSApplication sharedApplication] activationPolicy] == NSApplicationActivationPolicyAccessory); -} - - (NSString *__nullable)publicDSAKey { // Maybe the key is just a string in the Info.plist. diff --git a/Sparkle/SUSparkleUserUpdaterDriver.m b/Sparkle/SUSparkleUserUpdaterDriver.m index 0698285e..e0274dfd 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.m +++ b/Sparkle/SUSparkleUserUpdaterDriver.m @@ -16,6 +16,7 @@ #import "SUAutomaticUpdateAlert.h" #import "SUOperatingSystem.h" #import "SULocalizations.h" +#import "SUApplicationInfo.h" #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1080 @interface NSByteCountFormatter : NSFormatter { @@ -163,7 +164,7 @@ // 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. - if ([self.host isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:NSApp]) { [self.activeUpdateAlert.window setHidesOnDeactivate:NO]; [NSApp activateIgnoringOtherApps:YES]; @@ -260,7 +261,7 @@ // For background applications, obtain focus. // Useful if the update check is requested from another app like System Preferences. - if ([self.host isBackgroundApplication]) + if ([SUApplicationInfo isBackgroundApplication:NSApp]) { [NSApp activateIgnoringOtherApps:YES]; } @@ -337,9 +338,9 @@ // When showing a modal alert we need to ensure that background applications // are focused to inform the user since there is no dock icon to notify them. - if ([self.host isBackgroundApplication]) { [NSApp activateIgnoringOtherApps:YES]; } + if ([SUApplicationInfo isBackgroundApplication:NSApp]) { [NSApp activateIgnoringOtherApps:YES]; } - [alert setIcon:[self.host icon]]; + [alert setIcon:[SUApplicationInfo bestIconForBundle:self.host.bundle]]; [alert runModal]; if ([delegate respondsToSelector:@selector(userUpdaterDriverDidShowModalAlert:)]) { diff --git a/Sparkle/SUStatusController.m b/Sparkle/SUStatusController.m index c0064316..68edf4d4 100644 --- a/Sparkle/SUStatusController.m +++ b/Sparkle/SUStatusController.m @@ -13,6 +13,7 @@ #import "SUVersionComparisonProtocol.h" #import "SUStatusController.h" #import "SUHost.h" +#import "SUApplicationInfo.h" #import "SULocalizations.h" @interface SUStatusController () @@ -45,7 +46,7 @@ - (void)windowDidLoad { - if ([self.host isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:NSApp]) { [[self window] setLevel:NSFloatingWindowLevel]; } @@ -61,7 +62,7 @@ - (NSImage *)applicationIcon { - return [self.host icon]; + return [SUApplicationInfo bestIconForBundle:self.host.bundle]; } - (void)beginActionWithTitle:(NSString *)aTitle maxProgressValue:(double)aMaxProgressValue statusText:(NSString *)aStatusText diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index 52089d47..d277247e 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -19,6 +19,7 @@ #import "SULog.h" #import "SULocalizations.h" #import "SUAppcastItem.h" +#import "SUApplicationInfo.h" // WebKit protocols are not explicitly declared until 10.11 SDK, so // declare dummy protocols to keep the build working on earlier SDKs. @@ -180,7 +181,7 @@ [self.window setFrameAutosaveName: showReleaseNotes ? @"SUUpdateAlert" : @"SUUpdateAlertSmall" ]; - if ([self.host isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:NSApp]) { [self.window setLevel:NSFloatingWindowLevel]; // This means the window will float over all other apps, if our app is switched out ?! } @@ -220,7 +221,7 @@ - (NSImage *)applicationIcon { - return [self.host icon]; + return [SUApplicationInfo bestIconForBundle:self.host.bundle]; } - (NSString *)titleText diff --git a/Sparkle/SUUpdatePermissionPrompt.m b/Sparkle/SUUpdatePermissionPrompt.m index 7d118417..43a1de45 100644 --- a/Sparkle/SUUpdatePermissionPrompt.m +++ b/Sparkle/SUUpdatePermissionPrompt.m @@ -12,6 +12,7 @@ #import "SUHost.h" #import "SUConstants.h" +#import "SUApplicationInfo.h" @interface SUUpdatePermissionPrompt () @@ -61,7 +62,7 @@ // If this is a background application we need to focus it in order to bring the prompt // to the user's attention. Otherwise the prompt would be hidden behind other applications and // the user would not know why the application was paused. - if ([host isBackgroundApplication]) { + if ([SUApplicationInfo isBackgroundApplication:NSApp]) { [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; } @@ -99,7 +100,7 @@ - (NSImage *)icon { - return [self.host icon]; + return [SUApplicationInfo bestIconForBundle:self.host.bundle]; } - (NSString *)promptDescription