Plugins/Displays/MailMe/GrowlMailMeDisplay.m
author Rudy Richter
Sat Aug 01 20:43:39 2009 -0400 (2009-08-01)
changeset 4259 0e9b6b0b1e25
parent 4246 4f52d1d98978
child 4311 5cea9bbdea33
permissions -rw-r--r--
Plugins: clang warnings
     1 //
     2 //  GrowlMailMeDisplay.m
     3 //  Growl Display Plugins
     4 //
     5 //  Copyright 2004 Mac-arena the Bored Zo. All rights reserved.
     6 //
     7 #import "GrowlMailMeDisplay.h"
     8 #import "GrowlMailMePrefs.h"
     9 #import "GrowlDefinesInternal.h"
    10 #import "GrowlApplicationNotification.h"
    11 #ifdef __LP64__
    12 #import "Mail.h"	// this file is automatically generated by a shell script; see the target build rules for details
    13 #else
    14 #import <Message/NSMailDelivery.h>
    15 #endif
    16 
    17 #define destAddressKey @"MailMe - Recipient address"
    18 
    19 /* for when there is no icon */
    20 #define plainTextMessageFormat @"%@\r\n"\
    21 	@"-- This message was automatically generated by MailMe, a Growl plug-in, --\r\n"\
    22 	@"-- in response to a Growl notification --\r\n"\
    23 	@"-- http://growl.info/ --\r\n"
    24 
    25 @implementation GrowlMailMeDisplay
    26 
    27 - (void) dealloc {
    28 	[preferencePane release];
    29 	[super dealloc];
    30 }
    31 
    32 - (NSPreferencePane *) preferencePane {
    33 	if (!preferencePane)
    34 		preferencePane = [[GrowlMailMePrefs alloc] initWithBundle:[NSBundle bundleWithIdentifier:@"com.Growl.MailMe"]];
    35 	return preferencePane;
    36 }
    37 
    38 - (void) displayNotification:(GrowlApplicationNotification *)notification {
    39 	NSString *destAddress = nil;
    40 	READ_GROWL_PREF_VALUE(destAddressKey, @"com.Growl.MailMe", NSString *, &destAddress);
    41 	NSDictionary *noteDict = [notification dictionaryRepresentation];
    42 
    43 	if (destAddress) {
    44 		CFMakeCollectable(destAddress);
    45 		if([destAddress length]) {
    46 			NSString *title = [noteDict objectForKey:GROWL_NOTIFICATION_TITLE];
    47 			NSString *desc = [noteDict objectForKey:GROWL_NOTIFICATION_DESCRIPTION];
    48 			//hopefully something can be worked out to use the imageData.
    49 			//documentation, Apple, documentation!
    50 			//	NSData *imageData = [noteDict objectForKey:GROWL_NOTIFICATION_ICON];
    51 			
    52 #ifdef __LP64__
    53 			// On 64-bit, the Message framework is not available. Instead, use the Scripting Bridge.
    54 			MailApplication *Mail = [SBApplication applicationWithBundleIdentifier:@"com.apple.Mail"];
    55 			MailOutgoingMessage *message = [[[[Mail classForScriptingClass:@"outgoing message"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:title, @"subject", [NSString stringWithFormat:plainTextMessageFormat, desc], @"content", nil]] autorelease];
    56 			MailToRecipient *recipient = [[[[Mail classForScriptingClass:@"to recipient"] alloc] initWithProperties:[NSDictionary dictionaryWithObjectsAndKeys:destAddress, @"address", nil]] autorelease];
    57 			BOOL success;
    58 			
    59 			[[Mail outgoingMessages] addObject:message];
    60 			[message.toRecipients addObject:recipient];	// don't ask me, I'm only following the sample code's example
    61 			success = [message send];
    62 #else
    63 			BOOL success = [NSMailDelivery deliverMessage:[NSString stringWithFormat:plainTextMessageFormat, desc]
    64 												  subject:title
    65 													   to:destAddress];
    66 #endif
    67 			
    68 			if (!success) {
    69 				NSLog(@"(MailMe) WARNING: Could not send email message \"%@\" to address %@", title, destAddress);
    70 				NSLog(@"(MailMe) description of notification:\n%@", desc);
    71 			} else
    72 				NSLog(@"(MailMe) Successfully sent message \"%@\" to address %@", title, destAddress);
    73 		} else {
    74 			NSLog(@"(MailMe) WARNING: No destination address set");
    75 		}
    76 	} else {
    77 		NSLog(@"(MailMe) WARNING: No destination address set");
    78 	}
    79 	[destAddress release];
    80 
    81 	id clickContext = [noteDict objectForKey:GROWL_NOTIFICATION_CLICK_CONTEXT];
    82 	if (clickContext) {
    83 		NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:
    84 			[noteDict objectForKey:@"ClickHandlerEnabled"], @"ClickHandlerEnabled",
    85 			clickContext,                                   GROWL_KEY_CLICKED_CONTEXT,
    86 			[noteDict objectForKey:GROWL_APP_PID],          GROWL_APP_PID,
    87 			nil];
    88 		[[NSNotificationCenter defaultCenter] postNotificationName:GROWL_NOTIFICATION_TIMED_OUT
    89 															object:[notification applicationName]
    90 														  userInfo:userInfo];
    91 		[userInfo release];
    92 	}
    93 }
    94 
    95 - (BOOL) requiresPositioning {
    96 	return NO;
    97 }
    98 
    99 @end