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