-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPMenulet.m
More file actions
83 lines (62 loc) · 2.13 KB
/
IPMenulet.m
File metadata and controls
83 lines (62 loc) · 2.13 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
//
// IPMenulet.m
// IPMenulet
//
// Created by Andrew Pennebaker on 13 Dec 2007.
// Copyright 2007 YelloSoft. All rights reserved.
//
#import "IPMenulet.h"
#import "GetIP.h"
@implementation IPMenulet
-(void) dealloc {
[timer release];
[refreshMenuItem release];
[aboutMenuItem release];
[quitMenuItem release];
[statusItem release];
[menu release];
[super dealloc];
}
-(void) awakeFromNib {
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setMenu:menu];
[statusItem setHighlightMode:YES];
[statusItem setTitle:@"IP"];
[statusItem setEnabled:YES];
[menu insertItem:[NSMenuItem separatorItem] atIndex:0];
timer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(update) userInfo:nil repeats:YES];
[timer fire];
}
-(void) copy:(NSMenuItem*)target {
NSArray *ip = [target.title componentsSeparatedByString:@" "];
[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] setString:[ip objectAtIndex:0] forType:NSStringPboardType];
}
-(void) update {
[self refresh:nil];
}
-(IBAction) refresh: (id) sender {
int originalItemCount = 5;
int itemCount = [menu numberOfItems] - originalItemCount;
for (int j = itemCount; j > 0; j--) {
[menu removeItemAtIndex:0];
}
NSString *externalIp = [GetIP getExternalIP];
NSMutableArray *ips = [[NSMutableArray alloc] initWithObjects:externalIp, nil];
[ips addObjectsFromArray:[GetIP getLocalIPs]];
for(int i = 0; i < [ips count]; i++) {
NSMenuItem *testItem = [[NSMenuItem alloc] initWithTitle:[ips objectAtIndex:i]
action:@selector(copy:)
keyEquivalent:@""];
[testItem setTarget:self];
[testItem setEnabled:YES];
[menu insertItem: testItem atIndex:i];
[testItem release];
}
}
-(IBAction) about: (id) sender {
NSApplication *app = [NSApplication sharedApplication];
[app activateIgnoringOtherApps:YES];
[app orderFrontStandardAboutPanel:NULL];
}
@end