From 73ad73d896664a75e30063a6b96ed18a3b856786 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Sat, 8 May 2010 19:36:16 +0200
Subject: Flush bookmarks.

Resolves: #335

diff --git a/Swiften/Elements/Storage.h b/Swiften/Elements/Storage.h
index 7d978d1..447ca2e 100644
--- a/Swiften/Elements/Storage.h
+++ b/Swiften/Elements/Storage.h
@@ -35,12 +35,17 @@ namespace Swift {
 			Storage() {
 			}
 
+			
+			void clearRooms() {
+				rooms.clear();
+			}
+
 			const std::vector<Room>& getRooms() const {
-				return conferences;
+				return rooms;
 			}
 
-			void addRoom(const Room& conference) {
-				conferences.push_back(conference);
+			void addRoom(const Room& room) {
+				rooms.push_back(room);
 			}
 
 			const std::vector<URL>& getURLs() const {
@@ -52,7 +57,7 @@ namespace Swift {
 			}
 
 		private:
-			std::vector<Room> conferences;
+			std::vector<Room> rooms;
 			std::vector<URL> urls;
 	};
 }
diff --git a/Swiften/MUC/MUCBookmark.h b/Swiften/MUC/MUCBookmark.h
index 7afbe76..c6c22bf 100644
--- a/Swiften/MUC/MUCBookmark.h
+++ b/Swiften/MUC/MUCBookmark.h
@@ -10,21 +10,70 @@
 
 #include "Swiften/Base/String.h"
 #include "Swiften/JID/JID.h"
+#include "Swiften/Elements/Storage.h"
 
 namespace Swift {
 	class MUCBookmark {
 		public:
-			MUCBookmark(const JID& room, const String& bookmarkName) : room_(room), name_(bookmarkName){};
-			void setAutojoin(bool enabled) {autojoin_ = enabled;};
-			void setNick(const boost::optional<String>& nick) {nick_ = nick;};
-			void setPassword(const boost::optional<String>& password) {password_ = password;};
-			bool getAutojoin() const {return autojoin_;};
-			const boost::optional<String>& getNick() const {return nick_;};
-			const boost::optional<String>& getPassword() const {return password_;};
-			const String& getName() const {return name_;};
-			const JID& getRoom() const {return room_;};
-
-			bool operator==(const MUCBookmark& rhs) const {return rhs.room_ == room_ && rhs.name_ == name_ && rhs.nick_ == nick_ && rhs.password_ == password_ && rhs.autojoin_ == autojoin_;};
+			MUCBookmark(const Storage::Room& room) {
+				name_ = room.name;
+				room_ = room.jid;
+				nick_ = room.nick;
+				password_ = room.password;
+			}
+
+			MUCBookmark(const JID& room, const String& bookmarkName) : room_(room), name_(bookmarkName) {
+			}
+
+			void setAutojoin(bool enabled) {
+				autojoin_ = enabled; 
+			}
+
+			bool getAutojoin() const {
+				return autojoin_;
+			}
+
+			void setNick(const boost::optional<String>& nick) {
+				nick_ = nick;
+			}
+
+			void setPassword(const boost::optional<String>& password) {
+				password_ = password;
+			}
+
+			const boost::optional<String>& getNick() const {
+				return nick_;
+			}
+
+			const boost::optional<String>& getPassword() const {
+				return password_;
+			}
+
+			const String& getName() const {
+				return name_;
+			}
+
+			const JID& getRoom() const {
+				return room_;
+			}
+
+			bool operator==(const MUCBookmark& rhs) const {
+				return rhs.room_ == room_ && rhs.name_ == name_ && rhs.nick_ == nick_ && rhs.password_ == password_ && rhs.autojoin_ == autojoin_;
+			}
+
+			Storage::Room toStorage() const {
+				Storage::Room room;
+				room.name = name_;
+				room.jid = room_;
+				if (nick_) {
+					room.nick = *nick_;
+				}
+				if (password_) {
+					room.password = *password_;
+				}
+				return room;
+			}
+
 		private:
 			JID room_;
 			String name_;
diff --git a/Swiften/MUC/MUCBookmarkManager.cpp b/Swiften/MUC/MUCBookmarkManager.cpp
index 3675635..cc1c8d6 100644
--- a/Swiften/MUC/MUCBookmarkManager.cpp
+++ b/Swiften/MUC/MUCBookmarkManager.cpp
@@ -10,6 +10,8 @@
 #include <iostream>
 
 #include "Swiften/Queries/IQRouter.h"
+#include "Swiften/Queries/Requests/GetPrivateStorageRequest.h"
+#include "Swiften/Queries/Requests/SetPrivateStorageRequest.h"
 
 
 namespace Swift {
@@ -29,16 +31,7 @@ void MUCBookmarkManager::handleBookmarksReceived(boost::shared_ptr<Storage> payl
 
 	std::vector<MUCBookmark> receivedBookmarks;
 	foreach (Storage::Room room, payload->getRooms()) {
-		String name = (!room.name.isEmpty()) ? room.name : room.jid.getNode();
-		MUCBookmark bookmark(room.jid, name);
-		bookmark.setAutojoin(room.autoJoin);
-		if (!room.nick.isEmpty()) {
-			bookmark.setNick(room.nick);
-		}
-		if (!room.password.isEmpty()) {
-			bookmark.setPassword(room.password);
-		}
-		receivedBookmarks.push_back(bookmark);
+		receivedBookmarks.push_back(MUCBookmark(room));
 	}
 
 	std::vector<MUCBookmark> newBookmarks;
@@ -95,7 +88,17 @@ void MUCBookmarkManager::removeBookmark(const MUCBookmark& bookmark) {
 }
 
 void MUCBookmarkManager::flush() {
-	//FIXME: some code may be useful
+	// Update the storage element
+	storage->clearRooms();
+	foreach(const MUCBookmark& bookmark, bookmarks_) {
+		storage->addRoom(bookmark.toStorage());
+	}
+
+	// Send an iq to save the storage element
+	boost::shared_ptr<SetPrivateStorageRequest<Storage> > request(new SetPrivateStorageRequest<Storage>(storage, iqRouter_));
+	// FIXME: We should care about the result
+	//request->onResponse.connect(boost::bind(&MUCBookmarkManager::handleBookmarksSet, this, _1, _2));
+	request->send();
 }
 
 const std::vector<MUCBookmark>& MUCBookmarkManager::getBookmarks() const {
diff --git a/Swiften/MUC/MUCBookmarkManager.h b/Swiften/MUC/MUCBookmarkManager.h
index 594a805..136c76f 100644
--- a/Swiften/MUC/MUCBookmarkManager.h
+++ b/Swiften/MUC/MUCBookmarkManager.h
@@ -14,7 +14,7 @@
 
 #include "Swiften/MUC/MUCBookmark.h"
 #include "Swiften/Elements/Storage.h"
-#include "Swiften/Queries/Requests/GetPrivateStorageRequest.h"
+#include "Swiften/Elements/ErrorPayload.h"
 
 namespace Swift {
 	class IQRouter;
diff --git a/Swiften/Queries/Requests/SetPrivateStorageRequest.h b/Swiften/Queries/Requests/SetPrivateStorageRequest.h
index 997d434..2bdb133 100644
--- a/Swiften/Queries/Requests/SetPrivateStorageRequest.h
+++ b/Swiften/Queries/Requests/SetPrivateStorageRequest.h
@@ -20,7 +20,7 @@ namespace Swift {
 			SetPrivateStorageRequest(boost::shared_ptr<PAYLOAD_TYPE> payload, IQRouter* router) : Request(IQ::Set, JID(), boost::shared_ptr<PrivateStorage>(new PrivateStorage(payload)), router) {
 			}
 
-			virtual void handleResponse(boost::shared_ptr<Payload> payload, boost::optional<ErrorPayload> error) {
+			virtual void handleResponse(boost::shared_ptr<Payload>, boost::optional<ErrorPayload> error) {
 				onResponse(error);
 			}
 
-- 
cgit v0.10.2-6-g49f6