Extras/GrowlMail/Message+GrowlMail.m
changeset 4172 65a0bcf23292
parent 4171 2918a61fe9b3
child 4209 f8a902d33769
     1.1 --- a/Extras/GrowlMail/Message+GrowlMail.m	Wed Feb 25 01:02:42 2009 -0800
     1.2 +++ b/Extras/GrowlMail/Message+GrowlMail.m	Wed Feb 25 01:02:44 2009 -0800
     1.3 @@ -52,22 +52,18 @@
     1.4  
     1.5  @implementation Message (GrowlMail)
     1.6  
     1.7 -- (void) GMShowNotification
     1.8 -{
     1.9 +- (void) GMShowNotificationPart1 {
    1.10  	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    1.11  
    1.12 -	NSString *account = (NSString *)[[[self mailbox] account] displayName];
    1.13 -	NSString *sender = [self sender];
    1.14 -	NSString *senderAddress = [sender uncommentedAddress];
    1.15 -	NSString *subject = (NSString *)[self subject];
    1.16 -	NSString *body;
    1.17 +	MessageBody *messageBody = nil;
    1.18 +
    1.19  	NSString *titleFormat = (NSString *)GMTitleFormatString();
    1.20  	NSString *descriptionFormat = (NSString *)GMDescriptionFormatString();
    1.21  
    1.22  	if ([titleFormat rangeOfString:@"%body"].location != NSNotFound ||
    1.23  			[descriptionFormat rangeOfString:@"%body"].location != NSNotFound) {
    1.24  		/* We will need the body */
    1.25 -		MessageBody *messageBody = [self messageBodyIfAvailable];
    1.26 +		messageBody = [self messageBodyIfAvailable];
    1.27  		int nonBlockingAttempts = 0;
    1.28  		while (!messageBody && nonBlockingAttempts < 3) {
    1.29  			/* No message body available yet, but we need one */
    1.30 @@ -80,36 +76,23 @@
    1.31  
    1.32  		/* Already tried three times (3 seconds); this time, block this thread to get it. */ 
    1.33  		if (!messageBody) messageBody = [self messageBody];
    1.34 -
    1.35 -		if (messageBody) {
    1.36 -			NSString *originalBody = nil;
    1.37 -			/* stringForIndexing selector: Mail.app 3.0 in OS X 10.4, not in 10.5. */
    1.38 -			if ([messageBody respondsToSelector:@selector(stringForIndexing)])
    1.39 -				originalBody = [messageBody stringForIndexing];
    1.40 -			else if ([messageBody respondsToSelector:@selector(attributedString)])
    1.41 -				originalBody = [[messageBody attributedString] string];
    1.42 -			else if ([messageBody respondsToSelector:@selector(stringValueForJunkEvaluation:)])
    1.43 -				originalBody = [messageBody stringValueForJunkEvaluation:NO];
    1.44 -			if (originalBody) {
    1.45 -				NSMutableString *transformedBody = [[[originalBody stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy] autorelease];
    1.46 -				unsigned lengthWithoutWhitespace = [transformedBody length];
    1.47 -				[transformedBody trimStringToFirstNLines:4U];
    1.48 -				unsigned length = [transformedBody length];
    1.49 -				if (length > 200U) {
    1.50 -					[transformedBody deleteCharactersInRange:NSMakeRange(200U, length - 200U)];
    1.51 -					length = 200U;
    1.52 -				}
    1.53 -				if (length != lengthWithoutWhitespace)
    1.54 -					[transformedBody appendString:[NSString stringWithUTF8String:"\xE2\x80\xA6"]];
    1.55 -				body = (NSString *)transformedBody;
    1.56 -			} else {
    1.57 -				body = @"";	
    1.58 -			}
    1.59 -		} else {
    1.60 -			body = @"";
    1.61 -		}
    1.62 -	} else
    1.63 -		body = @"";
    1.64 +	}
    1.65 +
    1.66 +	[self performSelectorOnMainThread:@selector(GMShowNotificationPart2:)
    1.67 +						   withObject:messageBody
    1.68 +						waitUntilDone:NO];
    1.69 +
    1.70 +	[pool release];
    1.71 +}
    1.72 +
    1.73 +- (void) GMShowNotificationPart2:(MessageBody *)messageBody {
    1.74 +	NSString *account = (NSString *)[[[self mailbox] account] displayName];
    1.75 +	NSString *sender = [self sender];
    1.76 +	NSString *senderAddress = [sender uncommentedAddress];
    1.77 +	NSString *subject = (NSString *)[self subject];
    1.78 +	NSString *body;
    1.79 +	NSString *titleFormat = (NSString *)GMTitleFormatString();
    1.80 +	NSString *descriptionFormat = (NSString *)GMDescriptionFormatString();
    1.81  
    1.82  	/* The fullName selector is not available in Mail.app 2.0. */
    1.83  	if ([sender respondsToSelector:@selector(fullName)])
    1.84 @@ -117,6 +100,34 @@
    1.85  	else if ([sender addressComment])
    1.86  		sender = [sender addressComment];
    1.87  
    1.88 +	if (messageBody) {
    1.89 +		NSString *originalBody = nil;
    1.90 +		/* stringForIndexing selector: Mail.app 3.0 in OS X 10.4, not in 10.5. */
    1.91 +		if ([messageBody respondsToSelector:@selector(stringForIndexing)])
    1.92 +			originalBody = [messageBody stringForIndexing];
    1.93 +		else if ([messageBody respondsToSelector:@selector(attributedString)])
    1.94 +			originalBody = [[messageBody attributedString] string];
    1.95 +		else if ([messageBody respondsToSelector:@selector(stringValueForJunkEvaluation:)])
    1.96 +			originalBody = [messageBody stringValueForJunkEvaluation:NO];
    1.97 +		if (originalBody) {
    1.98 +			NSMutableString *transformedBody = [[[originalBody stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy] autorelease];
    1.99 +			unsigned lengthWithoutWhitespace = [transformedBody length];
   1.100 +			[transformedBody trimStringToFirstNLines:4U];
   1.101 +			unsigned length = [transformedBody length];
   1.102 +			if (length > 200U) {
   1.103 +				[transformedBody deleteCharactersInRange:NSMakeRange(200U, length - 200U)];
   1.104 +				length = 200U;
   1.105 +			}
   1.106 +			if (length != lengthWithoutWhitespace)
   1.107 +				[transformedBody appendString:[NSString stringWithUTF8String:"\xE2\x80\xA6"]];
   1.108 +			body = (NSString *)transformedBody;
   1.109 +		} else {
   1.110 +			body = @"";	
   1.111 +		}
   1.112 +	} else {
   1.113 +		body = @"";
   1.114 +	}
   1.115 +
   1.116  	NSArray *keywords = [NSArray arrayWithObjects:
   1.117  		@"%sender",
   1.118  		@"%subject",
   1.119 @@ -186,8 +197,6 @@
   1.120  							   clickContext:clickContext];	// non-nil click context
   1.121  
   1.122  	[GrowlMail didFinishNotificationForMessage:self];
   1.123 -
   1.124 -	[pool release];
   1.125  }
   1.126  
   1.127  @end