summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2016-08-25 14:00:10 (GMT)
committerTobias Markmann <tm@ayena.de>2016-08-25 14:12:34 (GMT)
commit5a4e7d9518e91cf39b96f14b3b310fe5b9a27594 (patch)
treede15882d3bb6f7b5e8230deada40ccc705a25c9c /Swift/QtUI/MUCSearch/MUCSearchServiceItem.cpp
parent5a19f3167b640966c582e52abe4b55ef20bd79b2 (diff)
downloadswift-5a4e7d9518e91cf39b96f14b3b310fe5b9a27594.zip
swift-5a4e7d9518e91cf39b96f14b3b310fe5b9a27594.tar.bz2
Alphabetically sort MUC search result
This also changes the classes around MUCSearchModel to C++11 smart pointer based memory management. Test-Information: Verified that dtors of MUCSearch*Items are called when old search results are replaced by new search results. This was not the case previously. All unit tests and manual testing with an ASAN enabled build succeeded. Change-Id: I84d62f3b86138728401b98d3774f47c72fdf9a4c
Diffstat (limited to 'Swift/QtUI/MUCSearch/MUCSearchServiceItem.cpp')
-rw-r--r--Swift/QtUI/MUCSearch/MUCSearchServiceItem.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/Swift/QtUI/MUCSearch/MUCSearchServiceItem.cpp b/Swift/QtUI/MUCSearch/MUCSearchServiceItem.cpp
new file mode 100644
index 0000000..57d5aac
--- /dev/null
+++ b/Swift/QtUI/MUCSearch/MUCSearchServiceItem.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2010-2016 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+
+#include <Swift/QtUI/MUCSearch/MUCSearchServiceItem.h>
+
+#include <memory>
+
+#include <QString>
+#include <QVector>
+#include <QtAlgorithms>
+
+namespace Swift {
+
+MUCSearchServiceItem::MUCSearchServiceItem(const QString& jidString) : jidString_(jidString) {
+
+}
+
+void MUCSearchServiceItem::addRoom(std::shared_ptr<MUCSearchItem> room) {
+ room->setParent(shared_from_this());
+ rooms_.push_back(room);
+ if (sortOrder_) {
+ sort();
+ }
+}
+
+void MUCSearchServiceItem::addRooms(const std::vector<std::shared_ptr<MUCSearchItem> >& rooms) {
+ for (auto&& room: rooms) {
+ room->setParent(shared_from_this());
+ rooms_.push_back(room);
+ }
+ if (sortOrder_) {
+ sort();
+ }
+}
+
+int MUCSearchServiceItem::rowCount() {
+ return rooms_.count();
+}
+
+MUCSearchItem* MUCSearchServiceItem::getItem(int i) {
+ return rooms_[i].get();
+}
+
+QVariant MUCSearchServiceItem::data(int role) {
+ switch (role) {
+ case Qt::DisplayRole:
+ return QVariant(jidString_);
+ default:
+ return QVariant();
+ }
+}
+
+QString MUCSearchServiceItem::getHost() const {
+ return jidString_;
+}
+
+void MUCSearchServiceItem::setSorting(Qt::SortOrder sortOrder) {
+ sortOrder_ = sortOrder;
+ sort();
+}
+
+void MUCSearchServiceItem::sort() {
+ if (*sortOrder_ == Qt::AscendingOrder) {
+ qStableSort(rooms_.begin(), rooms_.end(), [](const std::shared_ptr<MUCSearchItem>& item1, const std::shared_ptr<MUCSearchItem>& item2) -> bool {
+ return QString::localeAwareCompare(item1->data(Qt::DisplayRole).toString(), item2->data(Qt::DisplayRole).toString()) < 0;
+ });
+ }
+ else {
+ qStableSort(rooms_.begin(), rooms_.end(), [](const std::shared_ptr<MUCSearchItem>& item1, const std::shared_ptr<MUCSearchItem>& item2) -> bool {
+ return QString::localeAwareCompare(item1->data(Qt::DisplayRole).toString(), item2->data(Qt::DisplayRole).toString()) > 0;
+ });
+ }
+}
+
+}