forked from ptxmac/PingBar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPingBarAppDelegate.m
More file actions
117 lines (93 loc) · 5.18 KB
/
Copy pathPingBarAppDelegate.m
File metadata and controls
117 lines (93 loc) · 5.18 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// PingBarAppDelegate.m
// PingBar
//
// Created by Peter Kristensen on 04/02/10.
// Copyright 2010 Lucky Software. All rights reserved.
//
#import "PingBarAppDelegate.h"
#import "PSYBlockTimer.h"
@implementation PingBarAppDelegate
@synthesize window,menu;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Set default preferences
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
[defs registerDefaults:[NSDictionary
dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:1.0],@"pingDelay",
@"localhost",@"pingHost",
[NSNumber numberWithInteger:10],@"maxPings",
[NSNumber numberWithBool:NO],@"doPing",
[NSNumber numberWithInteger:0],@"timePrecision",
nil]];
pingQueue = [[NSOperationQueue alloc] init];
// Listen for preferences changes.
// NSUserDefaultsDidChangeNotification
defaultsObserver = [[NSNotificationCenter defaultCenter] addObserverForName:NSUserDefaultsDidChangeNotification
object:defs
queue:nil
usingBlock:^(NSNotification *n) {
//NSLog(@"Pref changed");
// stop pinging
[self stopPinging:nil];
[self startPinging:nil];
}];
// show the menu
NSStatusBar *bar = [NSStatusBar systemStatusBar];
barItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[barItem setTitle:@"PingBar"];
[barItem setHighlightMode:YES];
[barItem setMenu:menu];
[self startPinging:nil];
}
- (IBAction)stopPinging:(id)sender {
if (pingTimer)
[pingTimer invalidate];
}
- (void)updateTime {
NSTimeInterval secs = [[NSDate date] timeIntervalSinceDate:lastReply];
int p = [[NSUserDefaults standardUserDefaults] integerForKey:@"timePrecision"];
// Evil double format :D
NSString *str = [NSString stringWithFormat:@"%%.%df",p];
[barItem setTitle:[NSString stringWithFormat:str,secs]];
}
- (IBAction)startPinging:(id)sender {
BOOL doPing = [[NSUserDefaults standardUserDefaults] boolForKey:@"doPing"];
if (!doPing)
return;
//NSLog(@"Let's ping");
//if (pinger) // end
pinger = [PBPinger pingerWithHost:[[NSUserDefaults standardUserDefaults] stringForKey:@"pingHost"]];
lastReply = [NSDate date];
[self updateTime];
// + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds repeats:(BOOL)repeats usingBlock:(void (^)(NSTimer *timer))fireBlock;
pingTimer = [NSTimer scheduledTimerWithTimeInterval:[[NSUserDefaults standardUserDefaults] floatForKey:@"pingDelay"]
repeats:YES
usingBlock:^(NSTimer *timer) {
// Set time
int maxPings = [[NSUserDefaults standardUserDefaults] integerForKey:@"maxPings"];
if ([pingQueue operationCount] < maxPings) {
[pingQueue addOperationWithBlock:^{
NSTimeInterval delay = [pinger pingOnce];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (delay >= 0.0) {
//NSLog(@"not inf: %f",delay);
lastReply = [[NSDate date] dateByAddingTimeInterval:-delay];
}
[self updateTime];
}];
}];
} else
[self updateTime];
}];
}
- (IBAction)showPreferences:(id)sender {
//[window orderFrontRegardless];
[window makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];
}
- (IBAction)showAbout:(id)sender {
[NSApp orderFrontStandardAboutPanel:sender];
[NSApp activateIgnoringOtherApps:YES];
}
@end