Extras/GrowlSafari/GrowlSafari.m
changeset 4226 a6eb28fcc0b7
parent 4225 22ffbf7ae378
child 4237 d092b0dd14a5
     1.1 --- a/Extras/GrowlSafari/GrowlSafari.m	Thu Jul 02 06:30:46 2009 -0700
     1.2 +++ b/Extras/GrowlSafari/GrowlSafari.m	Thu Jul 02 06:31:09 2009 -0700
     1.3 @@ -34,8 +34,19 @@
     1.4  #import <objc/objc-runtime.h>
     1.5  
     1.6  
     1.7 -#define SAFARI_VERSION_2_0	412
     1.8 -#define SAFARI_VERSION_3_0  523
     1.9 +#define SAFARI_VERSION_4_0  5530
    1.10 +
    1.11 +enum {
    1.12 +	GrowlSafariDownloadStageActive = 1,
    1.13 +	GrowlSafariDownloadStageDecompressing = 2,
    1.14 +	GrowlSafariDownloadStageDiskImagePreparing = 4,
    1.15 +	GrowlSafariDownloadStageDiskImageVerifying = 7,
    1.16 +	GrowlSafariDownloadStageDiskImageVerified = 8,
    1.17 +	GrowlSafariDownloadStageDiskImageMounting = 9,
    1.18 +	GrowlSafariDownloadStageDiskImageCleanup = 12,
    1.19 +	GrowlSafariDownloadStageInactive = 13,
    1.20 +	GrowlSafariDownloadStageFinished = 14
    1.21 +};
    1.22  
    1.23  // How long should we wait (in seconds) before it's a long download?
    1.24  static double longDownload = 15.0;
    1.25 @@ -127,28 +138,29 @@
    1.26  		safariVersion = [[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey] intValue];
    1.27  		//  NSLog(@"%d",safariVersion);
    1.28  
    1.29 -		//	NSLog(@"Patching DownloadProgressEntry...");
    1.30 -		Class class = NSClassFromString(@"DownloadProgressEntry");
    1.31 -		PerformSwizzle(class, @selector(setDownloadStage:), @selector(mySetDownloadStage:), YES);
    1.32 -				
    1.33 -		if (safariVersion<SAFARI_VERSION_3_0) 
    1.34 -			PerformSwizzle(class, @selector(updateDiskImageStatus:), @selector(myUpdateDiskImageStatus:), YES);
    1.35 -		else
    1.36 +		if (safariVersion >= SAFARI_VERSION_4_0) {
    1.37 +			//	NSLog(@"Patching DownloadProgressEntry...");
    1.38 +			Class class = NSClassFromString(@"DownloadProgressEntry");
    1.39 +			PerformSwizzle(class, @selector(setDownloadStage:), @selector(mySetDownloadStage:), YES);
    1.40 +					
    1.41  			PerformSwizzle(class, @selector(_updateDiskImageStatus:), @selector(myUpdateDiskImageStatus:), YES);
    1.42 -		
    1.43 -		PerformSwizzle(class, @selector(initWithDownload:mayOpenWhenDone:allowOverwrite:),
    1.44 -					   @selector(myInitWithDownload:mayOpenWhenDone:allowOverwrite:),
    1.45 -					   YES);
    1.46 -		
    1.47 -		Class webBookmarkClass = NSClassFromString(@"WebBookmark");
    1.48 -		if (webBookmarkClass)
    1.49 -			[[GSWebBookmark class] poseAsClass:webBookmarkClass];
    1.50 -
    1.51 -		NSLog(@"Loaded GrowlSafari %@", [GrowlSafari bundleVersion]);
    1.52 -		NSDictionary *infoDictionary = [GrowlApplicationBridge frameworkInfoDictionary];
    1.53 -		NSLog(@"Using Growl.framework %@ (%@)",
    1.54 -			  [infoDictionary objectForKey:@"CFBundleShortVersionString"],
    1.55 -			  [infoDictionary objectForKey:(NSString *)kCFBundleVersionKey]);
    1.56 +			
    1.57 +			PerformSwizzle(class, @selector(initWithDownload:mayOpenWhenDone:allowOverwrite:),
    1.58 +						   @selector(myInitWithDownload:mayOpenWhenDone:allowOverwrite:),
    1.59 +						   YES);
    1.60 +			
    1.61 +			Class webBookmarkClass = NSClassFromString(@"WebBookmark");
    1.62 +			if (webBookmarkClass)
    1.63 +				[[GSWebBookmark class] poseAsClass:webBookmarkClass];
    1.64 +
    1.65 +			NSLog(@"Loaded GrowlSafari %@", [GrowlSafari bundleVersion]);
    1.66 +			NSDictionary *infoDictionary = [GrowlApplicationBridge frameworkInfoDictionary];
    1.67 +			NSLog(@"Using Growl.framework %@ (%@)",
    1.68 +				  [infoDictionary objectForKey:@"CFBundleShortVersionString"],
    1.69 +				  [infoDictionary objectForKey:(NSString *)kCFBundleVersionKey]);
    1.70 +		} else {
    1.71 +			NSLog(@"Safari too old (4.0 required); GrowlSafari disabled.");
    1.72 +		}
    1.73  	} else {
    1.74  		NSLog(@"Could not load Growl.framework, GrowlSafari disabled");
    1.75  	}
    1.76 @@ -212,12 +224,12 @@
    1.77  
    1.78  @implementation NSObject (GrowlSafariPatch)
    1.79  - (void) mySetDownloadStage:(int)stage {
    1.80 -	int oldStage = [self downloadStage];
    1.81 +	//int oldStage = [self downloadStage];
    1.82  	
    1.83  	//NSLog(@"mySetDownloadStage:%d -> %d", oldStage, stage);
    1.84  	[self mySetDownloadStage:stage];
    1.85  	if (dateStarted(self)) {
    1.86 -		if ( (safariVersion < SAFARI_VERSION_3_0 && stage == 2) || (safariVersion >= SAFARI_VERSION_3_0 && stage == 1) ) {
    1.87 +		if ( stage == GrowlSafariDownloadStageDecompressing ) {
    1.88  			NSBundle *bundle = [GrowlSafari bundle];
    1.89  			NSString *description = [[NSString alloc] initWithFormat:
    1.90  				NSLocalizedStringFromTableInBundle(@"%@", nil, bundle, @""),
    1.91 @@ -230,13 +242,12 @@
    1.92  										   isSticky:NO
    1.93  									   clickContext:nil];
    1.94  			[description release];
    1.95 -		} else if ( (safariVersion < SAFARI_VERSION_3_0 && stage == 9 && oldStage != 9) || 
    1.96 -			        (safariVersion >= SAFARI_VERSION_3_0 && stage == 8 && oldStage != 8) ) {
    1.97 +		} else if ( stage == GrowlSafariDownloadStageDiskImageVerifying ) {
    1.98  			NSBundle *bundle = [GrowlSafari bundle];
    1.99  			NSString *description = [[NSString alloc] initWithFormat:
   1.100 -				NSLocalizedStringFromTableInBundle(@"%@", nil, bundle, @""),
   1.101 -				[[self gsDownloadPath] lastPathComponent]];
   1.102 -			[GrowlApplicationBridge notifyWithTitle:NSLocalizedStringFromTableInBundle(@"Copying Disk Image", nil, bundle, @"")
   1.103 +									 NSLocalizedStringFromTableInBundle(@"%@", nil, bundle, @""),
   1.104 +									 [[self gsDownloadPath] lastPathComponent]];
   1.105 +			[GrowlApplicationBridge notifyWithTitle:NSLocalizedStringFromTableInBundle(@"Verifying Disk Image", nil, bundle, @"")
   1.106  										description:description
   1.107  								   notificationName:NSLocalizedStringFromTableInBundle(@"Disk Image Status", nil, bundle, @"")
   1.108  										   iconData:nil
   1.109 @@ -244,9 +255,7 @@
   1.110  										   isSticky:NO
   1.111  									   clickContext:nil];
   1.112  			[description release];
   1.113 -		} else if ( (safariVersion < SAFARI_VERSION_2_0 && stage == 13) || 
   1.114 -		            ((safariVersion >= SAFARI_VERSION_2_0 && safariVersion < SAFARI_VERSION_3_0) && stage == 15) ||
   1.115 -				    (safariVersion >= SAFARI_VERSION_3_0 && stage == 13) ) {
   1.116 +		} else if ( stage == GrowlSafariDownloadStageFinished ) {
   1.117  			NSBundle *bundle = [GrowlSafari bundle];
   1.118  			NSString *notificationName = isLongDownload(self) ? NSLocalizedStringFromTableInBundle(@"Download Complete", nil, bundle, @"") : NSLocalizedStringFromTableInBundle(@"Short Download Complete", nil, bundle, @"");
   1.119  			setDownloadFinished(self);
   1.120 @@ -262,7 +271,7 @@
   1.121  									   clickContext:nil];
   1.122  			[description release];
   1.123  		}
   1.124 -	} else if (stage == 0) {
   1.125 +	} else if (stage == GrowlSafariDownloadStageActive) {
   1.126  		setDownloadStarted(self);
   1.127  	}
   1.128  }
   1.129 @@ -273,8 +282,8 @@
   1.130  	//NSLog(@"myUpdateDiskImageStatus:%@ stage=%d -> %d", status, oldStage, [self downloadStage]);
   1.131  
   1.132  	if (dateStarted(self)
   1.133 -			&& ( (safariVersion < SAFARI_VERSION_3_0 && oldStage == 3) || (safariVersion >= SAFARI_VERSION_3_0 && oldStage == 7) )
   1.134 -			&& [self downloadStage] == 8
   1.135 +			&& oldStage == GrowlSafariDownloadStageDiskImageVerified
   1.136 +			&& [self downloadStage] == GrowlSafariDownloadStageDiskImageMounting
   1.137  			&& [[status objectForKey:@"status-stage"] isEqualToString:@"attach"]) {
   1.138  		NSBundle *bundle = [GrowlSafari bundle];
   1.139  		NSString *description = [[NSString alloc] initWithFormat:
   1.140 @@ -299,10 +308,7 @@
   1.141  }
   1.142  
   1.143  - (NSString*) gsDownloadPath {
   1.144 -	if (safariVersion<SAFARI_VERSION_3_0)
   1.145 -		return [self downloadPath];
   1.146 -	else
   1.147 -		return [self currentPath];
   1.148 +	return [self currentPath];
   1.149  }
   1.150  
   1.151  @end