From cf664dd8baa1accc355ec4a727899a9f68f68ed5 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Mon, 29 Mar 2010 16:03:13 +0100
Subject: Request bookmarks at login.


diff --git a/Swift/Controllers/MainController.cpp b/Swift/Controllers/MainController.cpp
index ee11b70..d042115 100644
--- a/Swift/Controllers/MainController.cpp
+++ b/Swift/Controllers/MainController.cpp
@@ -135,8 +135,6 @@ void MainController::handleConnected() {
 		presenceOracle_ = new PresenceOracle(client_);
 		nickResolver_ = new NickResolver(xmppRoster_);		
 		lastSentPresence_ = boost::shared_ptr<Presence>();
-		
-		
 
 		avatarManager_ = new AvatarManager(client_, client_, avatarStorage_);
 
diff --git a/Swift/QtUI/QtMainWindowFactory.cpp b/Swift/QtUI/QtMainWindowFactory.cpp
index dc1949e..10b122a 100644
--- a/Swift/QtUI/QtMainWindowFactory.cpp
+++ b/Swift/QtUI/QtMainWindowFactory.cpp
@@ -9,13 +9,15 @@ QtMainWindowFactory::QtMainWindowFactory(QtTreeWidgetFactory *treeWidgetFactory)
 }
 
 MainWindow* QtMainWindowFactory::createMainWindow(UIEventStream* eventStream) {
-	QtMainWindow* window = new QtMainWindow(eventStream, treeWidgetFactory_);
-	lastWindow_ = window;
-	return window;
+	if (!lastWindow_) {
+		lastWindow_  = new QtMainWindow(eventStream, treeWidgetFactory_);
+	}
+	return lastWindow_;
 }
 
 MainWindow* QtMainWindowFactory::getLastCreatedWindow() {
-	return lastWindow_;
+	assert(lastWindow_);
+	return lastWindow_;;
 }
 
 }
diff --git a/Swiften/MUC/MUCBookmarkManager.cpp b/Swiften/MUC/MUCBookmarkManager.cpp
index f9295e2..c91612d 100644
--- a/Swiften/MUC/MUCBookmarkManager.cpp
+++ b/Swiften/MUC/MUCBookmarkManager.cpp
@@ -1,11 +1,47 @@
 #include "MUCBookmarkManager.h"
 
+#include <boost/bind.hpp>
+
 #include "Swiften/Queries/IQRouter.h"
 
+
 namespace Swift {
 
 MUCBookmarkManager::MUCBookmarkManager(IQRouter* iqRouter) {
 	iqRouter_ = iqRouter;
+	boost::shared_ptr<GetPrivateStorageRequest<Storage> > request(new GetPrivateStorageRequest<Storage>(iqRouter_));
+	request->onResponse.connect(boost::bind(&MUCBookmarkManager::handleBookmarksReceived, this, _1, _2));
+	request->send();
+}
+
+void MUCBookmarkManager::handleBookmarksReceived(boost::shared_ptr<Storage> payload, const boost::optional<ErrorPayload>& error) {
+	if (error) {
+		return;
+	}
+	std::vector<boost::shared_ptr<MUCBookmark> > newBookmarks;
+	foreach (Storage::Conference conference, payload->getConferences()) {
+		String name = (!conference.name.isEmpty()) ? conference.name : conference.jid.getNode();
+		boost::shared_ptr<MUCBookmark> bookmark(new MUCBookmark(conference.jid, name));
+		bookmark->setAutojoin(conference.autoJoin);
+		if (!conference.nick.isEmpty()) {
+			bookmark->setNick(conference.nick);
+		}
+		if (!conference.password.isEmpty()) {
+			bookmark->setPassword(conference.password);
+		}
+		newBookmarks.push_back(bookmark);
+	}
+
+	//FIXME: This needs to be fixed before we start doing anything supporting updates
+	foreach (boost::shared_ptr<MUCBookmark> oldBookmark, bookmarks_) {
+		onBookmarkRemoved(oldBookmark);
+	}
+
+	foreach (boost::shared_ptr<MUCBookmark> newBookmark, newBookmarks) {
+		onBookmarkAdded(newBookmark);
+	}
+
+
 }
 
 void MUCBookmarkManager::addBookmark(boost::shared_ptr<MUCBookmark> bookmark) {
diff --git a/Swiften/MUC/MUCBookmarkManager.h b/Swiften/MUC/MUCBookmarkManager.h
index ade2e3e..a74bb1e 100644
--- a/Swiften/MUC/MUCBookmarkManager.h
+++ b/Swiften/MUC/MUCBookmarkManager.h
@@ -4,8 +4,11 @@
 
 #include <boost/shared_ptr.hpp>
 #include <boost/signals.hpp>
+#include <boost/optional.hpp>
 
 #include "Swiften/MUC/MUCBookmark.h"
+#include "Swiften/Elements/Storage.h"
+#include "Swiften/Queries/Requests/GetPrivateStorageRequest.h"
 
 namespace Swift {
 	class IQRouter;
@@ -21,7 +24,7 @@ namespace Swift {
 			boost::signal<void (boost::shared_ptr<MUCBookmark>)> onBookmarkAdded;
 			boost::signal<void (boost::shared_ptr<MUCBookmark>)> onBookmarkRemoved;
 		private:
-			
+			void handleBookmarksReceived(boost::shared_ptr<Storage> payload, const boost::optional<ErrorPayload>& error);
 			std::vector<boost::shared_ptr<MUCBookmark> > bookmarks_;
 			IQRouter* iqRouter_;
 	};
-- 
cgit v0.10.2-6-g49f6