Refactored GrowlMail to have a separate singleton notifier object, in order to make the mail-bundle class less dependent on being a singleton. (Assumptions are bad, especially when you're working with private/undocumented APIs.)
2 Copyright (c) The Growl Project, 2004-2005
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of Growl nor the names of its contributors
14 may be used to endorse or promote products derived from this software
15 without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26 OF THE POSSIBILITY OF SUCH DAMAGE.
29 // Message+GrowlMail.m
32 // Created by Ingmar Stein on 27.10.04.
35 #import "Message+GrowlMail.h"
36 #import "GrowlMailNotifier.h"
37 #import <AddressBook/AddressBook.h>
38 #import <Growl/Growl.h>
40 @interface NSString (GrowlMail_KeywordReplacing)
42 - (NSString *) stringByReplacingKeywords:(NSArray *)keywords
43 withValues:(NSArray *)values;
47 @interface NSMutableString (GrowlMail_LineOrientedTruncation)
49 - (void) trimStringToFirstNLines:(unsigned)n;
53 @implementation Message (GrowlMail)
55 - (void) GMShowNotificationPart1 {
56 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
58 MessageBody *messageBody = nil;
60 GrowlMailNotifier *notifier = [GrowlMailNotifier sharedNotifier];
61 NSString *titleFormat = [notifier titleFormat];
62 NSString *descriptionFormat = [notifier descriptionFormat];
64 if ([titleFormat rangeOfString:@"%body"].location != NSNotFound ||
65 [descriptionFormat rangeOfString:@"%body"].location != NSNotFound) {
66 /* We will need the body */
67 messageBody = [self messageBodyIfAvailable];
68 int nonBlockingAttempts = 0;
69 while (!messageBody && nonBlockingAttempts < 3) {
70 /* No message body available yet, but we need one */
71 nonBlockingAttempts++;
72 [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:(0.5 * nonBlockingAttempts)]];
74 /* We'd prefer to let whatever Mail process might want the message body get it on its own terms rather than blocking on this thread */
75 messageBody = [self messageBodyIfAvailable];
78 /* Already tried three times (3 seconds); this time, block this thread to get it. */
79 if (!messageBody) messageBody = [self messageBody];
82 [self performSelectorOnMainThread:@selector(GMShowNotificationPart2:)
83 withObject:messageBody
89 - (void) GMShowNotificationPart2:(MessageBody *)messageBody {
90 NSString *account = (NSString *)[[[self mailbox] account] displayName];
91 NSString *sender = [self sender];
92 NSString *senderAddress = [sender uncommentedAddress];
93 NSString *subject = (NSString *)[self subject];
95 GrowlMailNotifier *notifier = [GrowlMailNotifier sharedNotifier];
96 NSString *titleFormat = [notifier titleFormat];
97 NSString *descriptionFormat = [notifier descriptionFormat];
99 /* The fullName selector is not available in Mail.app 2.0. */
100 if ([sender respondsToSelector:@selector(fullName)])
101 sender = [sender fullName];
102 else if ([sender addressComment])
103 sender = [sender addressComment];
106 NSString *originalBody = nil;
107 /* stringForIndexing selector: Mail.app 3.0 in OS X 10.4, not in 10.5. */
108 if ([messageBody respondsToSelector:@selector(stringForIndexing)])
109 originalBody = [messageBody stringForIndexing];
110 else if ([messageBody respondsToSelector:@selector(attributedString)])
111 originalBody = [[messageBody attributedString] string];
112 else if ([messageBody respondsToSelector:@selector(stringValueForJunkEvaluation:)])
113 originalBody = [messageBody stringValueForJunkEvaluation:NO];
115 NSMutableString *transformedBody = [[[originalBody stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy] autorelease];
116 unsigned lengthWithoutWhitespace = [transformedBody length];
117 [transformedBody trimStringToFirstNLines:4U];
118 unsigned length = [transformedBody length];
120 [transformedBody deleteCharactersInRange:NSMakeRange(200U, length - 200U)];
123 if (length != lengthWithoutWhitespace)
124 [transformedBody appendString:[NSString stringWithUTF8String:"\xE2\x80\xA6"]];
125 body = (NSString *)transformedBody;
133 NSArray *keywords = [NSArray arrayWithObjects:
139 NSArray *values = [NSArray arrayWithObjects:
140 (sender ? sender : @""),
141 (subject ? subject : @""),
143 (account ? account : @""),
145 NSString *title = [titleFormat stringByReplacingKeywords:keywords withValues:values];
146 NSString *description = [descriptionFormat stringByReplacingKeywords:keywords withValues:values];
149 NSLog(@"Subject: '%@'", subject);
150 NSLog(@"Sender: '%@'", sender);
151 NSLog(@"Account: '%@'", account);
152 NSLog(@"Body: '%@'", body);
156 * MailAddressManager fetches images asynchronously so they might arrive
157 * after we have sent our notification.
160 MailAddressManager *addressManager = [MailAddressManager addressManager];
161 [addressManager fetchImageForAddress:senderAddress];
162 NSImage *image = [addressManager imageForMailAddress:senderAddress];
164 ABSearchElement *personSearch = [ABPerson searchElementForProperty:kABEmailProperty
168 comparison:kABEqualCaseInsensitive];
171 NSEnumerator *matchesEnum = [[[ABAddressBook sharedAddressBook] recordsMatchingSearchElement:personSearch] objectEnumerator];
173 while ((!image) && (person = [matchesEnum nextObject]))
174 image = [person imageData];
176 //no matches in the Address Book with an icon, so use Mail's icon instead.
178 image = [[NSImage imageNamed:@"NSApplicationIcon"] TIFFRepresentation];
180 NSString *notificationName;
181 if ([self isJunk] || ([[MailAccount junkMailboxUids] containsObject:[self mailbox]])) {
182 notificationName = NEW_JUNK_MAIL_NOTIFICATION;
184 if ([self respondsToSelector:@selector(type)] && [self type] == MESSAGE_TYPE_NOTE) {
185 notificationName = NEW_NOTE_NOTIFICATION;
187 notificationName = NEW_MAIL_NOTIFICATION;
191 NSString *clickContext = [self messageID];
193 [GrowlApplicationBridge notifyWithTitle:title
194 description:description
195 notificationName:notificationName
199 clickContext:clickContext]; // non-nil click context
201 [notifier didFinishNotificationForMessage:self];
206 @implementation NSString (GrowlMail_KeywordReplacing)
208 - (NSString *) stringByReplacingKeywords:(NSArray *)keywords
209 withValues:(NSArray *)values
211 NSParameterAssert([keywords count] == [values count]);
212 NSMutableString *str = [[self mutableCopy] autorelease];
214 NSEnumerator *keywordsEnum = [keywords objectEnumerator], *valuesEnum = [values objectEnumerator];
215 NSString *keyword, *value;
216 while ((keyword = [keywordsEnum nextObject]) && (value = [valuesEnum nextObject])) {
217 [str replaceOccurrencesOfString:keyword
220 range:NSMakeRange(0, [str length])];
227 @implementation NSMutableString (GrowlMail_LineOrientedTruncation)
229 - (void) trimStringToFirstNLines:(unsigned)n {
236 for (unsigned i=0U; i<n; ++i)
237 [self getLineStart:NULL end:&range.location contentsEnd:&end forRange:range];
239 length = [self length];
241 [self deleteCharactersInRange:NSMakeRange(end, length - end)];