summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-07-21 11:24:26 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-07-21 11:30:57 (GMT)
commit052f685338462a8c0c6d9785864e7549290d7037 (patch)
treef076b9981c62dc6913badf675c22caf6cbfcd874 /Slimber/Cocoa
parent06a69020da6ffb527bb2a31f1da2158df2a4d3de (diff)
downloadswift-contrib-052f685338462a8c0c6d9785864e7549290d7037.zip
swift-contrib-052f685338462a8c0c6d9785864e7549290d7037.tar.bz2
Show list of connected users.
Diffstat (limited to 'Slimber/Cocoa')
-rw-r--r--Slimber/Cocoa/Menulet.h3
-rw-r--r--Slimber/Cocoa/Menulet.m49
-rw-r--r--Slimber/Cocoa/Slimber.h4
-rw-r--r--Slimber/Cocoa/Slimber.mm23
4 files changed, 61 insertions, 18 deletions
diff --git a/Slimber/Cocoa/Menulet.h b/Slimber/Cocoa/Menulet.h
index dfe07a0..fabaa80 100644
--- a/Slimber/Cocoa/Menulet.h
+++ b/Slimber/Cocoa/Menulet.h
@@ -4,12 +4,13 @@
NSStatusItem* statusItem;
NSMenu* statusMenu;
NSImage* menuIcon;
+ NSArray* userNames;
BOOL selfOnline;
}
- (id) init;
- (void) updateMenu;
-- (void) setUsersOnline: (BOOL) online;
+- (void) setUserNames: (NSArray*) names;
- (void) setSelfConnected: (BOOL) online;
@end
diff --git a/Slimber/Cocoa/Menulet.m b/Slimber/Cocoa/Menulet.m
index f72078e..9eda07c 100644
--- a/Slimber/Cocoa/Menulet.m
+++ b/Slimber/Cocoa/Menulet.m
@@ -12,7 +12,7 @@
[statusItem setToolTip: @"Slimber"];
[statusItem setMenu: statusMenu];
- [self setUsersOnline: NO];
+ userNames = [[NSArray alloc] init];
selfOnline = NO;
[self updateMenu];
@@ -26,13 +26,36 @@
[super dealloc];
}
+- (void) updateIcon: (BOOL) online {
+ NSBundle* bundle = [NSBundle bundleForClass: [self class]];
+ NSString* path;
+ if (online) {
+ path = [bundle pathForResource: @"Online" ofType:@"png"];
+ }
+ else {
+ path = [bundle pathForResource: @"Offline" ofType:@"png"];
+ }
+ menuIcon = [[NSImage alloc] initWithContentsOfFile: path];
+ [statusItem setImage: menuIcon];
+}
+
- (void) updateMenu {
while ([statusMenu numberOfItems] > 0) {
[statusMenu removeItemAtIndex: 0];
}
- NSMenuItem* statusMenuItem = [[NSMenuItem alloc] initWithTitle: @"Online Users" action: NULL keyEquivalent: @""];
- [statusMenu addItem: statusMenuItem];
+ if ([userNames count] > 0) {
+ [statusMenu addItem: [[NSMenuItem alloc] initWithTitle: @"Online users:" action: NULL keyEquivalent: @""]];
+ int i;
+ for (i = 0; i < [userNames count]; ++i) {
+ NSMenuItem* userItem = [[NSMenuItem alloc] initWithTitle: [@" " stringByAppendingString: [userNames objectAtIndex: i]] action: NULL keyEquivalent: @""];
+ [statusMenu addItem: userItem];
+ }
+ }
+ else {
+ [statusMenu addItem: [[NSMenuItem alloc] initWithTitle: @"No online users" action: NULL keyEquivalent: @""]];
+ }
+ [self updateIcon: [userNames count] > 0];
[statusMenu addItem: [NSMenuItem separatorItem]];
NSMenuItem* loggedInItem;
@@ -50,22 +73,16 @@
[statusMenu addItem: exitMenuItem];
}
-- (void) setUsersOnline: (BOOL) online {
- NSBundle* bundle = [NSBundle bundleForClass: [self class]];
- NSString* path;
- if (online) {
- path = [bundle pathForResource: @"Online" ofType:@"png"];
- }
- else {
- path = [bundle pathForResource: @"Offline" ofType:@"png"];
- }
- menuIcon = [[NSImage alloc] initWithContentsOfFile: path];
- [statusItem setImage: menuIcon];
-}
-
- (void) setSelfConnected: (BOOL) online {
selfOnline = online;
[self updateMenu];
}
+- (void) setUserNames: (NSArray*) names {
+ [names retain];
+ [userNames release];
+ userNames = names;
+ [self updateMenu];
+}
+
@end
diff --git a/Slimber/Cocoa/Slimber.h b/Slimber/Cocoa/Slimber.h
index f5a02d7..7964581 100644
--- a/Slimber/Cocoa/Slimber.h
+++ b/Slimber/Cocoa/Slimber.h
@@ -2,8 +2,10 @@
#include <string>
#include <boost/bind.hpp>
+#include <boost/shared_ptr.hpp>
#include "Swiften/LinkLocal/DNSSDService.h"
+#include "Swiften/LinkLocal/LinkLocalRoster.h"
@class Menulet;
namespace Swift {
@@ -17,9 +19,11 @@ class Slimber {
private:
void handleSelfConnected(bool b);
+ void handleRosterChanged();
private:
boost::shared_ptr<Swift::DNSSDService> dnsSDService;
+ boost::shared_ptr<Swift::LinkLocalRoster>linkLocalRoster;
Swift::Server* server;
Menulet* menulet;
};
diff --git a/Slimber/Cocoa/Slimber.mm b/Slimber/Cocoa/Slimber.mm
index 74d95ef..d64cd58 100644
--- a/Slimber/Cocoa/Slimber.mm
+++ b/Slimber/Cocoa/Slimber.mm
@@ -1,5 +1,7 @@
#include "Slimber/Cocoa/Slimber.h"
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Elements/RosterPayload.h"
#include "Swiften/LinkLocal/AppleDNSSDService.h"
#include "Slimber/Cocoa/Menulet.h"
#include "Slimber/Server.h"
@@ -8,9 +10,15 @@ using namespace Swift;
Slimber::Slimber() {
dnsSDService = boost::shared_ptr<AppleDNSSDService>(new AppleDNSSDService());
- server = new Server(5222, 5562, dnsSDService);
+
+ linkLocalRoster = boost::shared_ptr<LinkLocalRoster>(new LinkLocalRoster(dnsSDService));
+ linkLocalRoster->onRosterChanged.connect(boost::bind(&Slimber::handleRosterChanged, this));
+
+ server = new Server(5222, 5562, linkLocalRoster, dnsSDService);
server->onSelfConnected.connect(boost::bind(&Slimber::handleSelfConnected, this, _1));
+
menulet = [[Menulet alloc] init];
+ handleRosterChanged();
}
Slimber::~Slimber() {
@@ -21,3 +29,16 @@ Slimber::~Slimber() {
void Slimber::handleSelfConnected(bool b) {
[menulet setSelfConnected: b];
}
+
+void Slimber::handleRosterChanged() {
+ NSMutableArray* names = [[NSMutableArray alloc] init];
+ boost::shared_ptr<RosterPayload> roster = linkLocalRoster->getRoster();
+ foreach(const RosterItemPayload& item, roster->getItems()) {
+ NSString* name = [NSString stringWithUTF8String: item.getName().getUTF8Data()];
+ [names addObject: name];
+ [name release];
+ }
+
+ [menulet setUserNames: names];
+ [names release];
+}