summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften')
-rw-r--r--Swiften/MUC/MUCBookmark.h1
-rw-r--r--Swiften/MUC/MUCBookmarkManager.cpp47
-rw-r--r--Swiften/MUC/MUCBookmarkManager.h6
3 files changed, 42 insertions, 12 deletions
diff --git a/Swiften/MUC/MUCBookmark.h b/Swiften/MUC/MUCBookmark.h
index 333a253..2b31f90 100644
--- a/Swiften/MUC/MUCBookmark.h
+++ b/Swiften/MUC/MUCBookmark.h
@@ -23,6 +23,7 @@ namespace Swift {
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) {return rhs.room_ == room_ && rhs.name_ == name_ && rhs.nick_ == nick_ && rhs.password_ == password_ && rhs.autojoin_ == autojoin_;};
private:
JID room_;
String name_;
diff --git a/Swiften/MUC/MUCBookmarkManager.cpp b/Swiften/MUC/MUCBookmarkManager.cpp
index d99bdff..72e63f2 100644
--- a/Swiften/MUC/MUCBookmarkManager.cpp
+++ b/Swiften/MUC/MUCBookmarkManager.cpp
@@ -7,6 +7,7 @@
#include "MUCBookmarkManager.h"
#include <boost/bind.hpp>
+#include <iostream>
#include "Swiften/Queries/IQRouter.h"
@@ -24,7 +25,7 @@ void MUCBookmarkManager::handleBookmarksReceived(boost::shared_ptr<Storage> payl
if (error) {
return;
}
- std::vector<boost::shared_ptr<MUCBookmark> > newBookmarks;
+ std::vector<boost::shared_ptr<MUCBookmark> > receivedBookmarks;
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));
@@ -35,25 +36,54 @@ void MUCBookmarkManager::handleBookmarksReceived(boost::shared_ptr<Storage> payl
if (!conference.password.isEmpty()) {
bookmark->setPassword(conference.password);
}
- newBookmarks.push_back(bookmark);
+ receivedBookmarks.push_back(bookmark);
}
- //FIXME: This needs to be fixed before we start doing anything supporting updates
+ std::vector<boost::shared_ptr<MUCBookmark> > newBookmarks;
foreach (boost::shared_ptr<MUCBookmark> oldBookmark, bookmarks_) {
- onBookmarkRemoved(oldBookmark);
+ if (containsEquivalent(receivedBookmarks, oldBookmark)) {
+ newBookmarks.push_back(oldBookmark);
+ } else {
+ onBookmarkRemoved(oldBookmark);
+ }
}
- foreach (boost::shared_ptr<MUCBookmark> newBookmark, newBookmarks) {
- onBookmarkAdded(newBookmark);
+ foreach (boost::shared_ptr<MUCBookmark> newBookmark, receivedBookmarks) {
+ if (!containsEquivalent(bookmarks_, newBookmark)) {
+ newBookmarks.push_back(newBookmark);
+ onBookmarkAdded(newBookmark);
+ }
}
+ bookmarks_ = newBookmarks;
+}
+bool MUCBookmarkManager::containsEquivalent(std::vector<boost::shared_ptr<MUCBookmark> > list, boost::shared_ptr<MUCBookmark> bookmark) {
+ foreach (boost::shared_ptr<MUCBookmark> listBookmark, list) {
+ if (*listBookmark == *bookmark) {
+ return true;
+ }
+ }
+ return false;
+}
+void MUCBookmarkManager::replaceBookmark(boost::shared_ptr<MUCBookmark> oldBookmark, boost::shared_ptr<MUCBookmark> newBookmark) {
+ for (size_t i = 0; i < bookmarks_.size(); i++) {
+ boost::shared_ptr<MUCBookmark> bookmark(bookmarks_[i]);
+ if (bookmark.get() == oldBookmark.get()) {
+ bookmarks_[i] = newBookmark;
+ flush();
+ onBookmarkRemoved(oldBookmark);
+ onBookmarkAdded(newBookmark);
+ return;
+ }
+ }
}
void MUCBookmarkManager::addBookmark(boost::shared_ptr<MUCBookmark> bookmark) {
+ std::cout << "adding bookmark with pointer " << bookmark.get() << std::endl;
bookmarks_.push_back(bookmark);
- flush();
onBookmarkAdded(bookmark);
+ flush();
}
@@ -63,10 +93,9 @@ void MUCBookmarkManager::removeBookmark(boost::shared_ptr<MUCBookmark> bookmark)
if ((*it).get() == bookmark.get()) {
bookmarks_.erase(it);
onBookmarkRemoved(bookmark);
- return;
+ break;
}
}
- assert(false);
flush();
}
diff --git a/Swiften/MUC/MUCBookmarkManager.h b/Swiften/MUC/MUCBookmarkManager.h
index 9fe5b7d..79b6167 100644
--- a/Swiften/MUC/MUCBookmarkManager.h
+++ b/Swiften/MUC/MUCBookmarkManager.h
@@ -23,14 +23,14 @@ namespace Swift {
MUCBookmarkManager(IQRouter* iqRouter);
void addBookmark(boost::shared_ptr<MUCBookmark> bookmark);
void removeBookmark(boost::shared_ptr<MUCBookmark> bookmark);
- /** Call flush after editing an existing bookmark. */
- void flush();
- /** Returns pointers to the bookmarks. These can be edited, and then flush()ed.*/
+ void replaceBookmark(boost::shared_ptr<MUCBookmark> oldBookmark, boost::shared_ptr<MUCBookmark> newBookmark);
const std::vector<boost::shared_ptr<MUCBookmark> >& getBookmarks();
boost::signal<void (boost::shared_ptr<MUCBookmark>)> onBookmarkAdded;
boost::signal<void (boost::shared_ptr<MUCBookmark>)> onBookmarkRemoved;
private:
+ bool containsEquivalent(std::vector<boost::shared_ptr<MUCBookmark> > list, boost::shared_ptr<MUCBookmark> bookmark);
void handleBookmarksReceived(boost::shared_ptr<Storage> payload, const boost::optional<ErrorPayload>& error);
+ void flush();
std::vector<boost::shared_ptr<MUCBookmark> > bookmarks_;
IQRouter* iqRouter_;
};