From eed31ed4b074fc5ee7ef29d6a1f488d589251659 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Tue, 21 Jul 2009 16:11:16 +0200
Subject: Use VCard to set name.


diff --git a/Makefile.config.in b/Makefile.config.in
index ddb409b..f9f3252 100644
--- a/Makefile.config.in
+++ b/Makefile.config.in
@@ -22,6 +22,7 @@ HAVE_EXPAT=@HAVE_EXPAT@
 USE_BUNDLED_EXPAT=@USE_BUNDLED_EXPAT@
 HAVE_LIBXML=@CONFIG_HAVE_LIBXML@
 HAVE_OPENSSL=@CONFIG_HAVE_OPENSSL@
+HAVE_AVAHI=@CONFIG_HAVE_AVAHI@
 WIN32=@CONFIG_WIN32@
 MACOSX=@CONFIG_MACOSX@
 BUILD_SWIFT=@BUILD_SWIFT@
diff --git a/Slimber/FileVCardCollection.cpp b/Slimber/FileVCardCollection.cpp
index 60be5c0..1568b33 100644
--- a/Slimber/FileVCardCollection.cpp
+++ b/Slimber/FileVCardCollection.cpp
@@ -1,14 +1,18 @@
 #include "Slimber/FileVCardCollection.h"
+#include "Swiften/Elements/VCard.h"
 
 namespace Swift {
 
 FileVCardCollection::FileVCardCollection(boost::filesystem::path dir) : vcardsPath(dir) {
+	vcard = boost::shared_ptr<VCard>(new VCard());
 }
 
 boost::shared_ptr<VCard> FileVCardCollection::getOwnVCard() const {
+	return vcard;
 }
 
-void FileVCardCollection::setOwnVCard(boost::shared_ptr<VCard> vcard) {
+void FileVCardCollection::setOwnVCard(boost::shared_ptr<VCard> v) {
+	vcard = v;
 }
 
 }
diff --git a/Slimber/FileVCardCollection.h b/Slimber/FileVCardCollection.h
index dde47df..3d805a8 100644
--- a/Slimber/FileVCardCollection.h
+++ b/Slimber/FileVCardCollection.h
@@ -15,5 +15,6 @@ namespace Swift {
 
 		private:
 			boost::filesystem::path vcardsPath;
+			boost::shared_ptr<VCard> vcard;
 	};
 }
diff --git a/Slimber/Server.cpp b/Slimber/Server.cpp
index c331416..b7c7052 100644
--- a/Slimber/Server.cpp
+++ b/Slimber/Server.cpp
@@ -84,6 +84,7 @@ void Server::handleSessionFinished(boost::shared_ptr<ServerFromClientSession>) {
 	selfJID_ = JID();
 	rosterRequested_ = false;
 	onSelfConnected(false);
+	lastPresence_.reset();
 }
 
 void Server::handleLinkLocalSessionFinished(boost::shared_ptr<Session> session) {
@@ -129,6 +130,7 @@ void Server::handleElementReceived(boost::shared_ptr<Element> element, boost::sh
 			else {
 				dnsSDService_->updateService(getLinkLocalServiceInfo(presence));
 			}
+			lastPresence_ = presence;
 		}
 		else {
 			unregisterService();
@@ -148,14 +150,16 @@ void Server::handleElementReceived(boost::shared_ptr<Element> element, boost::sh
 					session->sendElement(IQ::createError(iq->getFrom(), iq->getID(), Error::Forbidden, Error::Cancel));
 				}
 			}
-			if (iq->getPayload<VCard>()) {
+			if (boost::shared_ptr<VCard> vcard = iq->getPayload<VCard>()) {
 				if (iq->getType() == IQ::Get) {
-					boost::shared_ptr<VCard> vcard(new VCard());
-					vcard->setNickname(iq->getFrom().getNode());
-					session->sendElement(IQ::createResult(iq->getFrom(), iq->getID(), vcard));
+					session->sendElement(IQ::createResult(iq->getFrom(), iq->getID(), vCardCollection_->getOwnVCard()));
 				}
 				else {
-					session->sendElement(IQ::createError(iq->getFrom(), iq->getID(), Error::Forbidden, Error::Cancel));
+					vCardCollection_->setOwnVCard(vcard);
+					session->sendElement(IQ::createResult(iq->getFrom(), iq->getID()));
+					if (lastPresence_) {
+						dnsSDService_->updateService(getLinkLocalServiceInfo(lastPresence_));
+					}
 				}
 			}
 			else {
@@ -259,12 +263,18 @@ void Server::handlePresenceChanged(boost::shared_ptr<Presence> presence) {
 
 LinkLocalServiceInfo Server::getLinkLocalServiceInfo(boost::shared_ptr<Presence> presence) {
 	LinkLocalServiceInfo info;
-	info.setFirstName("Remko");
-	info.setLastName("Tron\xc3\xa7on");
-	info.setEMail("email@example.com");
-	info.setJID(JID("jid@example.com"));
+	boost::shared_ptr<VCard> vcard = vCardCollection_->getOwnVCard();
+	if (!vcard->getFamilyName().isEmpty() || !vcard->getGivenName().isEmpty()) {
+		info.setFirstName(vcard->getGivenName());
+		info.setLastName(vcard->getFamilyName());
+	}
+	if (!vcard->getNickname().isEmpty()) {
+		info.setNick(vcard->getNickname());
+	}
+	if (!vcard->getEMail().isEmpty()) {
+		info.setEMail(vcard->getEMail());
+	}
 	info.setMessage(presence->getStatus());
-	info.setNick("Remko");
 	switch (presence->getShow()) {
 		case StatusShow::Online: 
 		case StatusShow::None: 
diff --git a/Slimber/Server.h b/Slimber/Server.h
index b85aec8..ac80509 100644
--- a/Slimber/Server.h
+++ b/Slimber/Server.h
@@ -69,6 +69,7 @@ namespace Swift {
 			boost::shared_ptr<LinkLocalRoster> linkLocalRoster_;
 			boost::shared_ptr<DNSSDService> dnsSDService_;
 			VCardCollection* vCardCollection_;
+			boost::shared_ptr<Presence> lastPresence_;
 			boost::shared_ptr<BoostConnectionServer> serverFromClientConnectionServer_;
 			boost::shared_ptr<ServerFromClientSession> serverFromClientSession_;
 			boost::shared_ptr<BoostConnectionServer> serverFromNetworkConnectionServer_;
diff --git a/Slimber/main.cpp b/Slimber/main.cpp
index 31ae15a..40f41c8 100644
--- a/Slimber/main.cpp
+++ b/Slimber/main.cpp
@@ -8,7 +8,10 @@
 #include "Swiften/LinkLocal/AvahiDNSSDService.h"
 #endif
 #include "Slimber/Server.h"
+#include "Slimber/FileVCardCollection.h"
+#include "Swiften/LinkLocal/LinkLocalRoster.h"
 #include "Swiften/EventLoop/SimpleEventLoop.h"
+#include "Swiften/Application/Platform/PlatformApplication.h"
 
 using namespace Swift;
 
@@ -24,7 +27,11 @@ int main() {
 			new AvahiDNSSDService());
 #endif
 
-	Server server(5222, 5562, dnsSDService);
+	boost::shared_ptr<LinkLocalRoster> linkLocalRoster = boost::shared_ptr<LinkLocalRoster>(new LinkLocalRoster(dnsSDService));
+
+	FileVCardCollection vCardCollection(PlatformApplication("Slimber").getSettingsDir());
+
+	Server server(5222, 5562, linkLocalRoster, dnsSDService, &vCardCollection);
 	eventLoop.run();
 	return 0;
 }
diff --git a/Swiften/Application/Unix/UnixApplication.h b/Swiften/Application/Unix/UnixApplication.h
index d087bd8..c2671d8 100644
--- a/Swiften/Application/Unix/UnixApplication.h
+++ b/Swiften/Application/Unix/UnixApplication.h
@@ -10,7 +10,6 @@ namespace Swift {
 			UnixApplication(const String& name) : Application(name) {
 			}
 
-		private:
 			virtual ApplicationMessageDisplay* getApplicationMessageDisplay() {
 				return  &messageDisplay_;
 			}
diff --git a/configure.in b/configure.in
index 7fa6c26..30eef85 100644
--- a/configure.in
+++ b/configure.in
@@ -13,6 +13,7 @@ AC_CANONICAL_HOST
 AH_TEMPLATE(HAVE_OPENSSL, [OpenSSL library available])
 AH_TEMPLATE(HAVE_LIBXML, [LibXML library available])
 AH_TEMPLATE(HAVE_EXPAT, [Expat library available])
+AH_TEMPLATE(HAVE_AVAHI, [Avahi library available])
 
 ################################################################################
 # Default flags
@@ -215,6 +216,7 @@ echo
 
 AC_SUBST(SET_MAKE)
 AC_SUBST(HAVE_EXPAT)
+AC_SUBST(HAVE_AVAHI)
 AC_SUBST(CONFIG_CXX)
 AC_SUBST(CONFIG_CXXFLAGS)
 AC_SUBST(CONFIG_CC)
-- 
cgit v0.10.2-6-g49f6