William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againadium / Plugins / Purple Service / ESPurpleNotifyEmailController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "ESPurpleNotifyEmailController.h"
#import <AdiumLibpurple/SLPurpleCocoaAdapter.h>
#import <AdiumLibpurple/PurpleCommon.h>
#import <Adium/ESTextAndButtonsWindowController.h>
#import <Adium/AIContactAlertsControllerProtocol.h>
#import <Adium/AIAccount.h>
#import <AIUtilities/AIObjectAdditions.h>
@interface ESPurpleNotifyEmailController ()
+ (void)openURLString:(NSString *)urlString;
+ (void)startMailApplication;
+ (NSString *)mailApplicationName;
@end
@implementation ESPurpleNotifyEmailController
/*!
* @brief Handle the notification of emails
*
* This may be called from the purple thread.
*/
+ (void *)handleNotifyEmailsForAccount:(AIAccount *)account count:(size_t)count detailed:(BOOL)detailed subjects:(const char **)subjects froms:(const char **)froms tos:(const char **)tos urls:(const char **)urls
{
NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *messageFont = [NSFont messageFontOfSize:11];
NSMutableParagraphStyle *centeredParagraphStyle;
NSMutableAttributedString *message;
centeredParagraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
[centeredParagraphStyle setAlignment:NSCenterTextAlignment];
message = [[NSMutableAttributedString alloc] init];
//Title
NSString *title;
NSFont *titleFont;
NSDictionary *titleAttributes;
title = AILocalizedString(@"You have mail!\n",nil);
titleFont = [fontManager convertFont:[NSFont messageFontOfSize:12]
toHaveTrait:NSBoldFontMask];
titleAttributes = [NSDictionary dictionaryWithObjectsAndKeys:titleFont,NSFontAttributeName,
centeredParagraphStyle,NSParagraphStyleAttributeName,nil];
[message appendAttributedString:[[[NSAttributedString alloc] initWithString:title
attributes:titleAttributes] autorelease]];
//Message
NSString *numberMessage;
NSDictionary *numberMessageAttributes;
NSString *yourName;
if (account) {
yourName = account.formattedUID;
} else if (tos && *tos) {
yourName = [NSString stringWithUTF8String:*tos];
} else {
yourName = nil;
}
if (yourName && [yourName length]) {
numberMessage = ((count == 1) ?
[NSString stringWithFormat:AILocalizedString(@"%@ has 1 new message.",nil), yourName] :
[NSString stringWithFormat:AILocalizedString(@"%@ has %u new messages.",nil), yourName, count]);
} else {
numberMessage = ((count == 1) ?
AILocalizedString(@"You have 1 new message.",nil) :
[NSString stringWithFormat:AILocalizedString(@"You have %u new messages.",nil), count]);
}
numberMessageAttributes = [NSDictionary dictionaryWithObjectsAndKeys:messageFont,NSFontAttributeName,
centeredParagraphStyle,NSParagraphStyleAttributeName,nil];
[message appendAttributedString:[[[NSAttributedString alloc] initWithString:numberMessage
attributes:numberMessageAttributes] autorelease]];
if (count == 1) {
BOOL haveFroms = (froms != NULL);
BOOL haveSubjects = (subjects != NULL);
if (haveFroms || haveSubjects) {
NSFont *fieldFont;
NSDictionary *fieldAttributed, *infoAttributed;
fieldFont = [fontManager convertFont:messageFont
toHaveTrait:NSBoldFontMask];
fieldAttributed = [NSDictionary dictionaryWithObject:fieldFont forKey:NSFontAttributeName];
infoAttributed = [NSDictionary dictionaryWithObject:messageFont forKey:NSFontAttributeName];
//Skip a line
[[message mutableString] appendString:@"\n\n"];
if (haveFroms) {
NSString *fromString = [NSString stringWithUTF8String:(*froms)];
if (fromString && [fromString length]) {
[message appendAttributedString:[[[NSAttributedString alloc] initWithString:AILocalizedString(@"From: ",nil)
attributes:fieldAttributed] autorelease]];
[message appendAttributedString:[[[NSAttributedString alloc] initWithString:fromString
attributes:infoAttributed] autorelease]];
}
}
if (haveFroms && haveSubjects) {
[[message mutableString] appendString:@"\n"];
}
if (haveSubjects) {
NSString *subjectString = [NSString stringWithUTF8String:(*subjects)];
if (subjectString && [subjectString length]) {
[message appendAttributedString:[[[NSAttributedString alloc] initWithString:AILocalizedString(@"Subject: ",nil)
attributes:fieldAttributed] autorelease]];
AILog(@"%@: %@ appending %@",self,message,subjectString);
[message appendAttributedString:[[[NSAttributedString alloc] initWithString:subjectString
attributes:infoAttributed] autorelease]];
} else {
AILog(@"Got an invalid subjectString from %s",*subjects);
}
}
}
}
NSMutableDictionary *infoDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:title,@"Title",
message,@"Message",nil];
NSString *urlString = ((urls && *urls) ? [NSString stringWithUTF8String:(*urls)] : nil);
if (urlString) {
[infoDict setObject:urlString forKey:@"URL"];
}
[self mainPerformSelector:@selector(showNotifyEmailWindowForAccount:withMessage:URLString:)
withObject:account
withObject:message
withObject:(urlString ? urlString : nil)];
[centeredParagraphStyle release];
[message release];
return NULL;
}
/*!
* @brief Show the New Mail message
*
* Displays the New Mail message, optionally offerring an Open Mail button (if a URL to open the webmail is passed).
*
* @param account The account which received new mail
* @param inMessage An attributed message describing the new mail
* @param inURLString The URL to the appropriate webmail, or nil if no webmail link is available
*/
+ (void)showNotifyEmailWindowForAccount:(AIAccount *)account withMessage:(NSAttributedString *)inMessage URLString:(NSString *)inURLString
{
NSString *mailApplicationName = [self mailApplicationName];
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"AINoNewMailWindow"]) {
[ESTextAndButtonsWindowController showTextAndButtonsWindowWithTitle:AILocalizedString(@"New Mail",nil)
defaultButton:nil
alternateButton:(inURLString ?
AILocalizedString(@"Open Mail in Browser",nil) :
nil)
otherButton:((mailApplicationName && [mailApplicationName length]) ?
[NSString stringWithFormat:AILocalizedString(@"Launch %@", nil), mailApplicationName] :
nil)
onWindow:nil
withMessageHeader:nil
andMessage:inMessage
target:self
userInfo:inURLString];
}
//XXX - Hook this to the account for listobject
[adium.contactAlertsController generateEvent:ACCOUNT_RECEIVED_EMAIL
forListObject:account
userInfo:[inMessage string]
previouslyPerformedActionIDs:nil];
}
/*!
* @brief Window was closed, either by a button being clicked or the user closing it
*/
+ (BOOL)textAndButtonsWindowDidEnd:(NSWindow *)window returnCode:(AITextAndButtonsReturnCode)returnCode suppression:(BOOL)suppression userInfo:(id)userInfo
{
switch (returnCode) {
case AITextAndButtonsAlternateReturn:
if (userInfo) [self openURLString:userInfo];
break;
case AITextAndButtonsDefaultReturn:
break;
case AITextAndButtonsOtherReturn:
if (userInfo) [self startMailApplication];
break;
case AITextAndButtonsClosedWithoutResponse:
//No action needed
break;
}
return YES;
}
/*!
* @brief Start mail application from the new mail window
*
* Launch the user's mail application instead of opening the webmail-page
*/
+ (void)startMailApplication {
if ([[NSWorkspace sharedWorkspace] launchApplication:[self mailApplicationName]] == NO) {
NSLog(@"Could not launch mail application '%@'", [self mailApplicationName]);
}
}
/*!
* @brief Open a URL string from the open mail window
*
* The urlString could either be a web address or a path to a local HTML file we are supposed to load.
* The local HTML file will be in the user's temp directory, which Purple obtains with g_get_tmp_dir()...
* so we will, too.
*/
+ (void)openURLString:(NSString *)urlString
{
if ([urlString rangeOfString:[NSString stringWithUTF8String:g_get_tmp_dir()]].location != NSNotFound) {
//Local HTML file
CFURLRef appURL = NULL;
OSStatus err;
/* Obtain the default http:// handler. We don't care what would handle _this file_ (its extension doesn't matter)
* nor what normally happens when the user opens a .html file since that is, on many systems, an HTML editor.
* Instead, we want to know what application to use for viewing web pages... and then open this file in it.
*/
err = LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"http://www.adiumx.com"],
kLSRolesViewer,
/*outAppRef*/ NULL,
&appURL);
if (err == noErr) {
[[NSWorkspace sharedWorkspace] openFile:[urlString stringByExpandingTildeInPath]
withApplication:[(NSURL *)appURL path]];
} else {
NSURL *url;
//Web address
url = [NSURL URLWithString:urlString];
[[NSWorkspace sharedWorkspace] openURL:url];
}
if (appURL) {
//LSGetApplicationForURL() requires us to release the appURL when we are done with it
CFRelease(appURL);
}
} else {
NSURL *emailURL;
//Web address
emailURL = [NSURL URLWithString:urlString];
[[NSWorkspace sharedWorkspace] openURL:emailURL];
}
}
/*!
* @brief Returns the name of the user's mail application
*
* Use the LaunchServices to identify the user's mail application and return it's name
* @return NSString with the application's name
*/
+ (NSString *)mailApplicationName {
NSString *appName;
FSRef myAppRef;
LSGetApplicationForURL((CFURLRef)[NSURL URLWithString:@"mailto://"], kLSRolesAll, &myAppRef, NULL);
LSCopyDisplayNameForRef(&myAppRef, (CFStringRef *)&appName);
NSRange appRange;
if ((appRange = [appName rangeOfString:@".app" options:(NSCaseInsensitiveSearch | NSBackwardsSearch | NSAnchoredSearch)]).location != NSNotFound) {
appName = [[appName substringToIndex:appRange.location] retain];
}
return [appName autorelease];
}
@end
|