/
JTStatusMenu.m
227 lines (194 loc) · 7.93 KB
/
JTStatusMenu.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
//
// JTStatusMenu.m
// JobTracker
//
// Created by Brad Greenlee on 10/21/12.
// Copyright (c) 2012 Etsy. All rights reserved.
//
#import "JTStatusMenu.h"
#import "JTState.h"
@implementation JTStatusMenu
@synthesize jobTrackerURL, usernames, refreshInterval, startingJobNotificationsEnabled, completedJobNotificationsEnabled,
failedJobNotificationsEnabled;
- (void)awakeFromNib {
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
statusImage = [NSImage imageNamed:@"pith helmet small.png"];
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
[statusItem setHighlightMode:YES];
[statusMenu setAutoenablesItems:NO];
[statusItem setMenu:statusMenu];
// Listen for events when the computer wakes from sleep, which otherwise
// throws off the refresh schedule.
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(receiveWakeNote:)
name:NSWorkspaceDidWakeNotification
object:nil];
[GrowlApplicationBridge setGrowlDelegate:self];
[self loadPreferences];
if ([self isConfigured]) {
jtState = [JTState sharedInstance];
jtState.url = [NSURL URLWithString:[jobTrackerURL stringByAppendingString:@"/jobtracker.jsp"]];
[jtState setUsernameString:usernames];
jtState.delegate = self;
[self refresh:nil];
[self startTimer];
} else {
[self showPreferences:nil];
}
}
- (void)loadPreferences {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// this replace just fixes the url generated by an earlier version and can eventually be removed
jobTrackerURL = [[defaults stringForKey:@"jobTrackerURL"] stringByReplacingOccurrencesOfString:@"/jobtracker.jsp" withString:@""];
if ([jobTrackerURL length] > 7 && !([[jobTrackerURL substringToIndex:7] isEqualToString:@"http://"] ||
[[jobTrackerURL substringToIndex:8] isEqualToString:@"https://"]))
{
jobTrackerURL = [@"http://" stringByAppendingString:jobTrackerURL];
}
usernames = [defaults stringForKey:@"usernames"];
refreshInterval = [defaults integerForKey:@"refreshInterval"];
if (refreshInterval == 0) {
refreshInterval = DEFAULT_REFRESH_INTERVAL;
}
startingJobNotificationsEnabled = [defaults boolForKey:@"startingJobNotificationsEnabled"];
completedJobNotificationsEnabled = [defaults boolForKey:@"completedJobNotificationsEnabled"];
failedJobNotificationsEnabled = [defaults boolForKey:@"failedJobNotificationsEnabled"];
}
- (NSWindowController *)preferencesWindowController {
if (_preferencesWindowController == nil) {
_preferencesWindowController = [[JTPreferencesWindowController alloc] init];
_preferencesWindowController.delegate = self;
}
return _preferencesWindowController;
}
- (IBAction)showPreferences:(id)sender {
[self.preferencesWindowController showWindow:nil];
}
- (BOOL)isConfigured {
if (self.jobTrackerURL == nil || [self.jobTrackerURL isEqualToString:@""]) {
return NO;
}
return YES;
}
- (void)startTimer {
[self stopTimer];
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:refreshInterval
target:self
selector:@selector(refresh:)
userInfo:nil
repeats:YES];
}
- (void)stopTimer {
if (refreshTimer != nil) {
[refreshTimer invalidate];
refreshTimer = nil;
}
}
- (void)receiveWakeNote:(NSNotification*)note {
if ([self isConfigured]) {
// Kill off the current refresh schedule.
[self stopTimer];
// Wait a bit after wake before refreshing, so we don't make wake slower.
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(refresh:)
userInfo:nil
repeats:NO];
// Reset the refresh schedule after the wake refresh.
[self startTimer];
}
}
- (IBAction)refresh:(id)sender {
[self startRefresh];
[jtState refresh];
}
- (void)stateUpdated {
[self updateMenuItemWithTag:RUNNING_JOBS_TAG withJobs:[jtState.jobs objectForKey:@"running"]];
[self updateMenuItemWithTag:COMPLETED_JOBS_TAG withJobs:[jtState.jobs objectForKey:@"completed"]];
[self updateMenuItemWithTag:FAILED_JOBS_TAG withJobs:[jtState.jobs objectForKey:@"failed"]];
[self endRefresh];
}
- (void) growlNotificationWasClicked:(id)clickContext {
NSString *jobId = [clickContext objectForKey:@"jobId"];
[self openJobInBrowser:jobId];
}
- (void)sendNotificationWithTitle:(NSString *)title withJob:(JTJob *)job {
[GrowlApplicationBridge notifyWithTitle:title
description:job.displayName
notificationName:title
iconData:nil
priority:0
isSticky:NO
clickContext:[NSDictionary dictionaryWithObject:job.jobId forKey:@"jobId"]];
}
- (void)jobStarted:(JTJob *)job {
if (startingJobNotificationsEnabled) {
[self sendNotificationWithTitle:@"Job Started" withJob:job];
}
}
- (void)jobCompleted:(JTJob *)job {
if (completedJobNotificationsEnabled) {
[self sendNotificationWithTitle:@"Job Completed" withJob:job];
}
}
- (void)jobFailed:(JTJob *)job {
if (failedJobNotificationsEnabled) {
[self sendNotificationWithTitle:@"Job Failed" withJob:job];
}
}
- (void)startRefresh {
NSMenuItem *refresh = [statusMenu itemWithTag:REFRESH_TAG];
[refresh setTitle:@"Refreshing..."];
[refresh setEnabled:NO];
}
- (void)endRefresh {
NSMenuItem *refresh = [statusMenu itemWithTag:REFRESH_TAG];
[refresh setTitle:@"Refresh"];
[refresh setEnabled:YES];
}
- (void)updateMenuItemWithTag:(NSInteger)tag withJobs:(NSArray *)jobs {
NSMenu *jobsMenu = [[NSMenu alloc] init];
NSMenuItem *jobsMenuItem = [statusMenu itemWithTag:tag];
if ([jobs count] == 0) {
NSMenuItem *noneItem = [[NSMenuItem alloc] init];
[noneItem setTitle:@"None"];
[noneItem setEnabled:NO];
[jobsMenu addItem:noneItem];
} else {
for (JTJob *job in jobs) {
NSMenuItem *jobItem = [[NSMenuItem alloc] initWithTitle:job.displayName action:@selector(jobSelected:) keyEquivalent:@""];
[jobItem setRepresentedObject:job];
[jobItem setTarget:self];
[jobItem setEnabled:YES];
[jobsMenu addItem:jobItem];
}
}
[statusMenu setSubmenu:jobsMenu forItem:jobsMenuItem];
}
- (void)jobSelected:(id)sender {
NSMenuItem *menuItem = sender;
JTJob *job = [menuItem representedObject];
[self openJobInBrowser:job.jobId];
}
- (void)openJobInBrowser:(NSString *)jobId {
NSString *jobUrl = [NSString stringWithFormat:@"%@/jobdetails.jsp?jobid=%@", jobTrackerURL, jobId];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:jobUrl]];
}
- (IBAction)openInBrowser:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[jobTrackerURL stringByAppendingString:@"/jobtracker.jsp"]]];
}
- (void)preferencesUpdated {
[self loadPreferences];
if ([self isConfigured]) {
jtState = [JTState sharedInstance];
jtState.url = [NSURL URLWithString:[jobTrackerURL stringByAppendingString:@"/jobtracker.jsp"]];
[jtState setUsernameString:usernames];
jtState.delegate = self;
[self refresh:nil];
[self startTimer];
} else {
[self showPreferences:nil];
}
}
@end