-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQSAppleMailMediator.m
More file actions
93 lines (78 loc) · 3.09 KB
/
QSAppleMailMediator.m
File metadata and controls
93 lines (78 loc) · 3.09 KB
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
#import "QSAppleMailMediator.h"
#import "QSMailMediator.h"
#import "Mail.h"
#import <AddressBook/AddressBook.h>
@class QSCountBadgeImage;
@implementation QSAppleMailMediator
- (id)init
{
self = [super init];
if (self) {
mailScript = nil;
}
return self;
}
- (void) sendEmailTo:(NSArray *)addresses from:(NSString *)sender subject:(NSString *)subject body:(NSString *)body attachments:(NSArray *)pathArray sendNow:(BOOL)sendNow{
NSArray *accounts = [[[self mailScript] executeSubroutine:@"account_list" arguments:[NSArray arrayWithObjects:subject, body, addresses, pathArray, nil] error:nil] objectValue];
if (!sender && ![accounts count]) {
return;
}
//NSLog(@"accounts %@",accounts);
NSInteger accountIndex = -1;
for (NSUInteger i = 0; i < [accounts count]; i++) {
NSString *accountAddress = [[accounts objectAtIndex:i] objectAtIndex:0];
for (NSString *address in addresses) {
if (emailsShareDomain(address, accountAddress)){
accountIndex = i;
i = [accounts count]; // stop the outer loop
break; // stop the inner loop
}
}
}
if (accountIndex >= 0 || !sender) {
// better sender found, or default is missing
NSArray *account = [accounts objectAtIndex:(accountIndex >= 0) ? accountIndex : 0];
NSString *accountFormatted = [(NSString *)[account lastObject]length]?[NSString stringWithFormat:@"%@ <%@>", [account lastObject], [account objectAtIndex:0]]:[account objectAtIndex:0];
sender = accountFormatted;
}
//NSLog(@"accounts %@",accountFormatted);
[[QSReg getClassInstance:@"QSMailMediator"] sendEmailWithScript:[self mailScript] to:(NSArray *)addresses from:(NSString *)sender subject:(NSString *)subject body:(NSString *)body attachments:(NSArray *)pathArray sendNow:(BOOL)sendNow];
}
- (NSString *)scriptPath {
return [[NSBundle bundleForClass:[QSAppleMailMediator class]]pathForResource:@"Mail" ofType:@"scpt"];
}
- (NSAppleScript *)mailScript {
if (!mailScript){
NSString *path=[self scriptPath];
if (path) mailScript=[[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
}
return mailScript;
}
- (void)setMailScript:(NSAppleScript *)newMailScript {
mailScript = newMailScript;
}
+ (NSDictionary *)mailPreferences
{
// locate and read Mail.app's preferences
NSArray *candidates = [NSArray arrayWithObjects:@"~/Library/Mail/V2/MailData/Accounts.plist", @"~/Library/Containers/com.apple.mail/Data/Library/Preferences/com.apple.mail.plist", @"~/Library/Preferences/com.apple.mail.plist", nil];
for (NSString *prefs in candidates) {
NSDictionary *mailPrefs = [NSDictionary dictionaryWithContentsOfFile:[prefs stringByStandardizingPath]];
if (mailPrefs) {
return mailPrefs;
}
}
return nil;
}
- (NSImage *)iconForAction:(NSString *)actionID
{
if ([actionID isEqualToString:@"QSEmailItemAction"] || [actionID isEqualToString:@"QSEmailItemReverseAction"]) {
// actions that send immediately
return [QSResourceManager imageNamed:@"MailMailbox-Sent"];
}
return [QSResourceManager imageNamed:@"com.apple.Mail"];
}
- (NSDictionary *)smtpServerDetails {
// unused for apple mail - use the direct method instead
return nil;
}
@end