From abd4d8981ebc5ad579102002483f410a5d4d79da Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Tue, 21 Jul 2009 12:07:49 +0200
Subject: Update the "You are logged in" menu.


diff --git a/Slimber/Cocoa/Makefile.inc b/Slimber/Cocoa/Makefile.inc
index e858430..be03429 100644
--- a/Slimber/Cocoa/Makefile.inc
+++ b/Slimber/Cocoa/Makefile.inc
@@ -16,7 +16,7 @@ SLIMBER_COCOA_RESOURCES = \
 SLIMBER_COCOA_NIBS = \
 	$(SLIMBER_COCOA_XIBS:.xib=.nib)
 SLIMBER_COCOA_OBJECTS = \
-	$(patsubst %.mm,%.o,$(patsubst %.cpp,%.o, $(SLIMBER_COCOA_SOURCES)))
+	$(patsubst %.m,%.o,$(patsubst %.mm,%.o,$(patsubst %.cpp,%.o, $(SLIMBER_COCOA_SOURCES))))
 CLEANFILES += \
 	Slimber/Cocoa/PkgInfo \
 	$(SLIMBER_COCOA_OBJECTS) \
diff --git a/Slimber/Cocoa/Menulet.h b/Slimber/Cocoa/Menulet.h
index 46c4faa..dfe07a0 100644
--- a/Slimber/Cocoa/Menulet.h
+++ b/Slimber/Cocoa/Menulet.h
@@ -4,9 +4,12 @@
 	NSStatusItem* statusItem;
 	NSMenu* statusMenu;
 	NSImage* menuIcon;
+	BOOL selfOnline;
 }
 
 - (id) init;
 - (void) updateMenu;
+- (void) setUsersOnline: (BOOL) online;
+- (void) setSelfConnected: (BOOL) online;
 
 @end
diff --git a/Slimber/Cocoa/Menulet.m b/Slimber/Cocoa/Menulet.m
index 613d05d..f72078e 100644
--- a/Slimber/Cocoa/Menulet.m
+++ b/Slimber/Cocoa/Menulet.m
@@ -12,10 +12,8 @@
 		[statusItem setToolTip: @"Slimber"];	
 		[statusItem setMenu: statusMenu];
 
-		NSBundle* bundle = [NSBundle bundleForClass: [self class]];
-		NSString* path = [bundle pathForResource: @"Offline" ofType:@"png"];
-		menuIcon = [[NSImage alloc] initWithContentsOfFile: path];
-		[statusItem setImage: menuIcon];
+		[self setUsersOnline: NO];
+		selfOnline = NO;
 
 		[self updateMenu];
 	}
@@ -29,11 +27,21 @@
 }
 
 - (void) updateMenu {
+	while ([statusMenu numberOfItems] > 0) {
+		[statusMenu removeItemAtIndex: 0];
+	}
+
 	NSMenuItem* statusMenuItem = [[NSMenuItem alloc] initWithTitle: @"Online Users" action: NULL keyEquivalent: @""];
 	[statusMenu addItem: statusMenuItem];
 	[statusMenu addItem: [NSMenuItem separatorItem]];
 
-	NSMenuItem* loggedInItem = [[NSMenuItem alloc] initWithTitle: @"You are not logged in" action: NULL keyEquivalent: @""];
+	NSMenuItem* loggedInItem;
+	if (selfOnline) {
+		loggedInItem = [[NSMenuItem alloc] initWithTitle: @"You are logged in" action: NULL keyEquivalent: @""];
+	}
+	else {
+		loggedInItem = [[NSMenuItem alloc] initWithTitle: @"You are not logged in" action: NULL keyEquivalent: @""];
+	}
 	[statusMenu addItem: loggedInItem];
 	[statusMenu addItem: [NSMenuItem separatorItem]];
 
@@ -42,4 +50,22 @@
 	[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];
+}
+
 @end
diff --git a/Slimber/Cocoa/Slimber.h b/Slimber/Cocoa/Slimber.h
index 1ee9040..f5a02d7 100644
--- a/Slimber/Cocoa/Slimber.h
+++ b/Slimber/Cocoa/Slimber.h
@@ -16,6 +16,9 @@ class Slimber {
 		~Slimber();
 
 	private:
+		void handleSelfConnected(bool b);
+
+	private:
 		boost::shared_ptr<Swift::DNSSDService> dnsSDService;
 		Swift::Server* server;
 		Menulet* menulet;
diff --git a/Slimber/Cocoa/Slimber.mm b/Slimber/Cocoa/Slimber.mm
index 4bf72fe..74d95ef 100644
--- a/Slimber/Cocoa/Slimber.mm
+++ b/Slimber/Cocoa/Slimber.mm
@@ -9,6 +9,7 @@ using namespace Swift;
 Slimber::Slimber() {
 	dnsSDService = boost::shared_ptr<AppleDNSSDService>(new AppleDNSSDService());
 	server = new Server(5222, 5562, dnsSDService);
+	server->onSelfConnected.connect(boost::bind(&Slimber::handleSelfConnected, this, _1));
 	menulet = [[Menulet alloc] init];
 }
 
@@ -16,3 +17,7 @@ Slimber::~Slimber() {
 	delete server;
 	[menulet release];
 }
+
+void Slimber::handleSelfConnected(bool b) {
+	[menulet setSelfConnected: b];
+}
diff --git a/Slimber/Server.cpp b/Slimber/Server.cpp
index e84659a..1922351 100644
--- a/Slimber/Server.cpp
+++ b/Slimber/Server.cpp
@@ -53,9 +53,10 @@ void Server::handleNewClientConnection(boost::shared_ptr<Connection> c) {
 		c->disconnect();
 	}
 	serverFromClientSession_ = boost::shared_ptr<ServerFromClientSession>(new ServerFromClientSession(idGenerator_.generateID(), c, &payloadParserFactories_, &payloadSerializers_, &userRegistry_));
+	serverFromClientSession_->onSessionStarted.connect(boost::bind(&Server::handleSessionStarted, this));
 	serverFromClientSession_->onElementReceived.connect(boost::bind(&Server::handleElementReceived, this, _1, serverFromClientSession_));
 	serverFromClientSession_->onSessionFinished.connect(boost::bind(&Server::handleSessionFinished, this, serverFromClientSession_));
-	tracers_.push_back(boost::shared_ptr<SessionTracer>(new SessionTracer(serverFromClientSession_)));
+	//tracers_.push_back(boost::shared_ptr<SessionTracer>(new SessionTracer(serverFromClientSession_)));
 	serverFromClientSession_->startSession();
 }
 
@@ -72,11 +73,16 @@ void Server::handleServiceRegistered(const DNSSDService::Service& service) {
 	selfJID_ = JID(service.name);
 }
 
+void Server::handleSessionStarted() {
+	onSelfConnected(true);
+}
+
 void Server::handleSessionFinished(boost::shared_ptr<ServerFromClientSession>) {
 	serverFromClientSession_.reset();
 	unregisterService();
 	selfJID_ = JID();
 	rosterRequested_ = false;
+	onSelfConnected(false);
 }
 
 void Server::handleLinkLocalSessionFinished(boost::shared_ptr<Session> session) {
@@ -214,7 +220,7 @@ void Server::registerLinkLocalSession(boost::shared_ptr<Session> session) {
 	session->onSessionFinished.connect(boost::bind(&Server::handleLinkLocalSessionFinished, this, session));
 	session->onElementReceived.connect(boost::bind(&Server::handleLinkLocalElementReceived, this, _1, session));
 	linkLocalSessions_.push_back(session);
-	tracers_.push_back(boost::shared_ptr<SessionTracer>(new SessionTracer(session)));
+	//tracers_.push_back(boost::shared_ptr<SessionTracer>(new SessionTracer(session)));
 	session->startSession();
 }
 
diff --git a/Slimber/Server.h b/Slimber/Server.h
index 573e3d9..5ce9e31 100644
--- a/Slimber/Server.h
+++ b/Slimber/Server.h
@@ -26,10 +26,13 @@ namespace Swift {
 		public:
 			Server(int clientConnectionPort, int linkLocalConnectionPort, boost::shared_ptr<DNSSDService> dnsSDService);
 
+			boost::signal<void (bool)> onSelfConnected;
+
 		private:
 			void handleNewClientConnection(boost::shared_ptr<Connection> c);
 			void handleNewLinkLocalConnection(boost::shared_ptr<Connection> connection);
 			void handleServiceRegistered(const DNSSDService::Service& service);
+			void handleSessionStarted();
 			void handleSessionFinished(boost::shared_ptr<ServerFromClientSession>);
 			void handleLinkLocalSessionFinished(boost::shared_ptr<Session> session);
 			void handleLinkLocalElementReceived(boost::shared_ptr<Element> element, boost::shared_ptr<Session> session);
-- 
cgit v0.10.2-6-g49f6