/
MailIntervalAction.m
138 lines (108 loc) · 3.17 KB
/
MailIntervalAction.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
//
// MailIntervalAction.m
// ControlPlane
//
// Created by David Jennes on 02/09/11.
// Copyright 2011. All rights reserved.
//
#import "MailIntervalAction.h"
#import <ScriptingBridge/SBApplication.h>
#import "Mail.h"
#import "DSLogger.h"
@implementation MailIntervalAction
- (id) init {
self = [super init];
if (!self)
return nil;
time = [[NSNumber alloc] initWithInt: 0];
return self;
}
- (id) initWithDictionary: (NSDictionary *) dict {
self = [super initWithDictionary: dict];
if (!self)
return nil;
time = [[dict valueForKey: @"parameter"] copy];
return self;
}
- (id) initWithOption: (NSString *) option {
self = [super init];
if (!self)
return nil;
time = [[NSNumber alloc] initWithInt: [option intValue]];
return self;
}
- (void) dealloc {
[time release];
[super dealloc];
}
- (NSMutableDictionary *) dictionary {
NSMutableDictionary *dict = [super dictionary];
[dict setObject: [[time copy] autorelease] forKey: @"parameter"];
return dict;
}
- (NSString *) description {
int t = [time intValue];
if (t == 0)
return NSLocalizedString(@"Disabling automatic mail checking.", @"");
else if (t == 1)
return NSLocalizedString(@"Setting mail checking interval to 1 minute.", @"");
else
return [NSString stringWithFormat: NSLocalizedString(@"Setting mail checking interval to %d minutes.", @""), t];
}
- (BOOL) execute: (NSString **) errorString {
@try {
MailApplication *Mail = [SBApplication applicationWithBundleIdentifier: @"com.apple.mail"];
int t = [time intValue];
if (t == 0)
Mail.fetchesAutomatically = NO;
else {
Mail.fetchesAutomatically = YES;
Mail.fetchInterval = t;
}
} @catch (NSException *e) {
DSLog(@"Exception: %@", e);
*errorString = NSLocalizedString(@"Couldn't set mail check interval!", @"In MailIntervalAction");
return NO;
}
return YES;
}
+ (NSString *) helpText {
return NSLocalizedString(@"The parameter for MailIntervalAction actions is the time "
"(in minutes) between each check for new mails.", @"");
}
+ (NSString *) creationHelpText {
return NSLocalizedString(@"Set mail check interval to", @"");
}
+ (NSArray *) limitedOptions {
int options[] = {1, 5, 15, 30, 60, 0};
int total = sizeof(options) / sizeof(options[0]);
NSMutableArray *arr = [NSMutableArray arrayWithCapacity: total];
for (int i = 0; i < total; ++i) {
NSNumber *option = [NSNumber numberWithInt: options[i]];
NSString *description;
switch (options[i]) {
case 0:
description = NSLocalizedString(@"Manually", @"Mail check interval");
break;
case 1:
description = NSLocalizedString(@"Every minute", @"Mail check interval");
break;
case 60:
description = NSLocalizedString(@"Every hour", @"Mail check interval");
break;
default:
description = [NSString stringWithFormat: NSLocalizedString(@"Every %d minutes", @"Mail check interval"), options[i]];
}
[arr addObject: [NSDictionary dictionaryWithObjectsAndKeys:
option, @"option",
description, @"description", nil]];
}
return arr;
}
+ (NSString *) friendlyName {
return NSLocalizedString(@"Change New Mail Check Interval", @"");
}
+ (NSString *)menuCategory {
return NSLocalizedString(@"Mail", @"");
}
@end