-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabStatusController.m
executable file
·426 lines (317 loc) · 11.3 KB
/
LabStatusController.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//
// LabStatusController.m
// LabStatus
//
// Created by Christian Pape on 07.10.08.
// Copyright 2008 cc-productions. All rights reserved.
//
#import "LabStatusController.h"
#import "Keychain.h"
@implementation LabStatusController
- (id)init
{
if (![super init])
return nil;
return self;
}
- (void)awakeFromNib
{
if (!preferences) {
preferences = [[Preferences alloc] init];
}
hosts = [preferences hosts];
indicators = [preferences indicators];
actions = [preferences actions];
progress = [preferences progress];
NSEnumerator * enumeratorHosts = [hosts objectEnumerator];
Host* host;
int h = 0;
while(host = [enumeratorHosts nextObject])
{
[host loadPassword];
h++;
}
[self createControls];
}
- (void)taskFinished:(NSNotification *)aNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSTaskDidTerminateNotification
object:task];
Host* host = [hosts objectAtIndex:hostIndex];
Indicator* indicator = [indicators objectAtIndex:indicatorIndex];
int terminationStatus = [task terminationStatus];
int oldTerminationStatus = [host setIndicatorStatus:indicatorIndex status:terminationStatus];
if (terminationStatus!=oldTerminationStatus) {
// values changed -> invoke popup
if (oldTerminationStatus != -1) {
if (!popUpController)
popUpController = [[PopUpController alloc] init];
[popUpController showWindow:self];
[popUpController addMessage:[NSString stringWithFormat:NSLocalizedString(@"LABSTATUS_INFORMATION",@""),[indicator name], [host hostname], oldTerminationStatus, terminationStatus]];
}
}
if (progress == NSOnState) {
[progressIndicator removeFromSuperview];
[[host indicatorControl:indicatorIndex] setHidden:NO];
}
indicatorIndex++;
if (indicatorIndex>=[indicators count]) {
indicatorIndex = 0;
hostIndex++;
}
if (hostIndex>=[hosts count])
hostIndex = 0;
timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(fireTimer:)
userInfo:nil
repeats:NO];
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
if (flag) {
return NO;
} else {
[labStatusPanel makeKeyAndOrderFront:nil];
return YES;
}
}
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
[task terminate];
task = nil;
[timer invalidate];
timer = nil;
return NSTerminateNow;
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
return NO;
}
- (IBAction)showPreferencesPanel:(id)sender
{
if (!preferencesController)
preferencesController = [[PreferencesController alloc] init];
// bugfix for version 0.92
if (timer) {
[timer invalidate];
timer = nil;
}
// bugfix for version 0.92
if (progressIndicator) {
[progressIndicator removeFromSuperview];
progressIndicator = nil;
}
[NSApp runModalForWindow:[preferencesController window]];
[self removeControls];
hosts = [preferences hosts];
indicators = [preferences indicators];
actions = [preferences actions];
progress = [preferences progress];
Keychain *keychain = [[Keychain alloc] init];
[keychain removeKeyChainEntries];
NSEnumerator * enumeratorHosts = [hosts objectEnumerator];
Host* host;
int h = 0;
while(host = [enumeratorHosts nextObject])
{
[host savePassword];
h++;
}
[self createControls];
}
- (void) resizePanelWithWidth:(int)width height:(int)height
{
NSRect rect = NSMakeRect([labStatusPanel frame].origin.x,
[labStatusPanel frame].origin.y,
width,
height);
// NSLog(@"%d", [labStatusPanel frame].origin.y);
[labStatusPanel setFrame:[labStatusPanel frameRectForContentRect:rect]
display:YES
animate:YES];
}
- (void) createControls
{
if ([hosts count]==0) {
// No hosts
[self resizePanelWithWidth:200
height:100];
if (!infoTextField) {
NSRect rectInfoTextField = NSMakeRect(10,10,200,80);
infoTextField = [[NSTextField alloc] initWithFrame:rectInfoTextField];
[infoTextField setEditable:false];
[infoTextField setSelectable:false];
[infoTextField setBezeled:NO];
[infoTextField setDrawsBackground:NO];
[infoTextField setTextColor:[NSColor whiteColor]];
[infoTextField setStringValue:NSLocalizedString(@"LABSTATUS_UNCONFIGURED",@"")];
[infoTextField setFont:[NSFont userFontOfSize:12.0]];
[labStatusView addSubview: infoTextField];
}
return ;
} else {
if (infoTextField) {
[infoTextField removeFromSuperview];
infoTextField = nil;
}
}
// initializing array
headerControls = [[NSMutableArray alloc] init];
// enumerating hosts
NSEnumerator * enumeratorHosts = [hosts objectEnumerator];
Host* host;
int h = 0;
hostnameWidth = 20;
while(host = [enumeratorHosts nextObject])
{
int width = [host createTextField:h
hosts:hosts
view:labStatusView];
if (width>hostnameWidth)
hostnameWidth = width;
h++;
}
hostnameWidth= hostnameWidth+10;
headerWidth = 20;
NSEnumerator * enumeratorIterators = [indicators objectEnumerator];
Indicator* indicator;
int i = 0;
while(indicator = [enumeratorIterators nextObject])
{
NSRect rectHeaderField = NSMakeRect(hostnameWidth+(i*20), ([hosts count]*20), 20, 10);
NSTextField *headerField = [[NSTextField alloc] initWithFrame:rectHeaderField];
[headerField setEditable:false];
[headerField setSelectable:false];
[headerField setBezeled:NO];
[headerField setDrawsBackground:NO];
[headerField setTextColor:[NSColor whiteColor]];
[headerField setStringValue:[indicator name]];
[headerField setFont:[NSFont userFontOfSize:7.0]];
[headerField sizeToFit];
//[labStatusView addSubview: headerField];
[headerControls addObject: headerField];
NSRect r = [headerField frame];
if (r.size.width>headerWidth)
headerWidth = r.size.width;
i++;
}
NSEnumerator * enumeratorActions = [actions objectEnumerator];
Action* action;
int a = 0;
while(action = [enumeratorActions nextObject])
{
NSRect rectHeaderField = NSMakeRect(hostnameWidth+(20*[indicators count])+(a*20), ([hosts count]*20), 20, 10);
NSTextField *headerField = [[NSTextField alloc] initWithFrame:rectHeaderField];
[headerField setEditable:false];
[headerField setSelectable:false];
[headerField setBezeled:NO];
[headerField setDrawsBackground:NO];
[headerField setTextColor:[NSColor whiteColor]];
[headerField setStringValue:[action name]];
[headerField setFont:[NSFont userFontOfSize:7.0]];
[headerField sizeToFit];
//[labStatusView addSubview: headerField];
[headerControls addObject: headerField];
NSRect r = [headerField frame];
if (r.size.width>headerWidth)
headerWidth = r.size.width;
a++;
}
NSEnumerator * enumeratorHeaderControls = [headerControls objectEnumerator];
NSControl* control;
a = 0;
while(control = [enumeratorHeaderControls nextObject]) {
[control setFrameOrigin: NSMakePoint(hostnameWidth+(a*headerWidth),([hosts count]*20))];
[labStatusView addSubview: control];
a++;
}
// resize the panel
[self resizePanelWithWidth:hostnameWidth + ([indicators count]+[actions count]) * headerWidth
height:10 + ([hosts count] * 20)];
enumeratorHosts = [hosts objectEnumerator];
h = 0;
while(host = [enumeratorHosts nextObject])
{
[host createControls:h
offset:hostnameWidth
width:headerWidth
hosts:hosts
indicators:indicators
actions:actions
view:labStatusView];
h++;
}
indicatorIndex = 0;
hostIndex = 0;
[self startTimer];
}
- (void) startTimer
{
if (timer) {
[timer invalidate];
timer = nil;
}
timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(fireTimer:)
userInfo:nil
repeats:NO];
}
- (void) fireTimer:(NSTimer*)aTimer
{
// do check here
Host* host = [hosts objectAtIndex:hostIndex];
NSRect rectProgressIndicator = NSMakeRect(hostnameWidth+(headerWidth*indicatorIndex),
([hosts count]*20)-(hostIndex+1)*20,
20,
20);
// bugfix for version 0.92
if (progressIndicator) {
[progressIndicator removeFromSuperview];
progressIndicator = nil;
}
if (progress == NSOnState) {
progressIndicator = [[NSProgressIndicator alloc] initWithFrame:rectProgressIndicator];
[progressIndicator setIndeterminate:YES];
[progressIndicator setStyle:NSProgressIndicatorSpinningStyle];
[progressIndicator startAnimation:self];
[[host indicatorControl:indicatorIndex] setHidden:YES];
[labStatusView addSubview:progressIndicator];
}
task = [[NSTask alloc] init];
// get command
Indicator* indicator = [indicators objectAtIndex:indicatorIndex];
// insert tags
NSString *command = [indicator command];
command = [command stringByReplacingOccurrencesOfString:@"%hostname%" withString: [host hostname]];
command = [command stringByReplacingOccurrencesOfString:@"%ipaddress%" withString: [host ipAddress]];
command = [command stringByReplacingOccurrencesOfString:@"%macaddress%" withString: [host macAddress]];
command = [command stringByReplacingOccurrencesOfString:@"%username%" withString: [host username]];
command = [command stringByReplacingOccurrencesOfString:@"%password%" withString: [host password]];
command = [command stringByAppendingString:@" 1>/dev/null 2>&1"];
// execute task
[task setLaunchPath:@"/bin/sh"];
NSArray *arguments = [NSArray arrayWithObjects: @"-c", command, nil];
[task setArguments: arguments];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(taskFinished:)
name:NSTaskDidTerminateNotification
object:task];
[task launch];
}
- (void) removeControls
{
// removing host controls
NSEnumerator * enumeratorHosts = [hosts objectEnumerator];
Host* host;
while(host = [enumeratorHosts nextObject])
[host removeControls];
// removing header controls
NSEnumerator * enumeratorHeaderControls = [headerControls objectEnumerator];
NSControl* control;
while(control = [enumeratorHeaderControls nextObject])
[control removeFromSuperview];
}
@end