summaryrefslogtreecommitdiffstats
blob: 1cc1f1fb9db65dd7c5f0c9d75b5872fc3c62359d (plain)
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
#import "Menulet.h"

@implementation Menulet

- (id) init {
	if ([super init]) {
		statusMenu = [[NSMenu alloc] init];

		statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength: NSVariableStatusItemLength] retain];
		[statusItem setHighlightMode: YES];
		[statusItem setEnabled: YES];
		[statusItem setToolTip: @"Slimber"];	
		[statusItem setMenu: statusMenu];

		userNames = [[NSArray alloc] init];
		selfOnline = NO;

		[self updateMenu];
	}
	return self;
}

- (void) dealloc {
	[statusItem release];
	[menuIcon release];
	[super dealloc];
}

- (void) updateIcon: (BOOL) online {
	NSBundle* bundle = [NSBundle bundleForClass: [self class]];
	NSString* path;
	if (online) {
		path = [bundle pathForResource: @"UsersOnline" ofType:@"png"];
	}
	else {
		path = [bundle pathForResource: @"UsersOffline" ofType:@"png"];
	}
	[statusItem setImage: [[NSImage alloc] initWithContentsOfFile: path]];
}

- (void) updateMenu {
	// Clear the menu
	while ([statusMenu numberOfItems] > 0) {
		[statusMenu removeItemAtIndex: 0];
	}

	// User items
	if ([userNames count] > 0) {
		[statusMenu addItem: [[NSMenuItem alloc] initWithTitle: @"Online users:" action: NULL keyEquivalent: @""]];
		int i;
		for (i = 0; i < [userNames count]; ++i) {
			NSString* text = [NSString stringWithFormat: @"  %@", [userNames objectAtIndex: i]]; 
			NSMenuItem* userItem = [[NSMenuItem alloc] initWithTitle: text action: NULL keyEquivalent: @""];
			[statusMenu addItem: userItem];
			[userItem release];
		}
	}
	else {
		[statusMenu addItem: [[NSMenuItem alloc] initWithTitle: @"No online users" action: NULL keyEquivalent: @""]];
	}
	[self updateIcon: [userNames count] > 0];
	[statusMenu addItem: [NSMenuItem separatorItem]];

	// Self item
	NSMenuItem* loggedInItem;
	NSBundle* bundle = [NSBundle bundleForClass: [self class]];
	NSString* path;
	if (selfOnline) {
		loggedInItem = [[NSMenuItem alloc] initWithTitle: @"You are logged in" action: NULL keyEquivalent: @""];
		path = [bundle pathForResource: @"Online" ofType:@"png"];
	}
	else {
		loggedInItem = [[NSMenuItem alloc] initWithTitle: @"You are not logged in" action: NULL keyEquivalent: @""];
		path = [bundle pathForResource: @"Offline" ofType:@"png"];
	}
	[loggedInItem setImage: [[NSImage alloc] initWithContentsOfFile: path]];
	[statusMenu addItem: loggedInItem];
	[statusMenu addItem: [NSMenuItem separatorItem]];

	// About menu
	NSMenuItem* aboutMenuItem = [[NSMenuItem alloc] initWithTitle: @"About Slimber" action: @selector(orderFrontStandardAboutPanel:) keyEquivalent: @""];
	[aboutMenuItem setTarget: [NSApplication sharedApplication]];
	[statusMenu addItem: aboutMenuItem];
	[statusMenu addItem: [NSMenuItem separatorItem]];

	// Exit item
	NSMenuItem* exitMenuItem = [[NSMenuItem alloc] initWithTitle: @"Exit" action: @selector(terminate:) keyEquivalent: @""];
	[exitMenuItem setTarget: [NSApplication sharedApplication]];
	[statusMenu addItem: exitMenuItem];
}

- (void) setSelfConnected: (BOOL) online {
	selfOnline = online;
	[self updateMenu];
}

- (void) setUserNames: (NSArray*) names {
	[names retain];
	[userNames release];
	userNames = names;
	[self updateMenu];
}

@end