summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Maudsley <richard.maudsley@isode.com>2014-04-09 07:53:12 (GMT)
committerSwift Review <review@swift.im>2014-04-22 21:12:21 (GMT)
commita7fb55381ab4a5c470bf891b31ac6e201611a2c1 (patch)
tree475b3b7fc0142946863580506b00aa250ef8ca14 /Swift/Controllers/ContactSuggester.cpp
parent4444aeb255f8d1712b794c31166f362bb3ec335a (diff)
downloadswift-a7fb55381ab4a5c470bf891b31ac6e201611a2c1.zip
swift-a7fb55381ab4a5c470bf891b31ac6e201611a2c1.tar.bz2
Fix crash in QtUserSearchWindow.
Avoid storing pointers to items in vectors. Using shared_ptr for Contact items. Change-Id: I3baa05fc058011b2beca14dc620ab794988a2b37
Diffstat (limited to 'Swift/Controllers/ContactSuggester.cpp')
-rw-r--r--Swift/Controllers/ContactSuggester.cpp39
1 files changed, 25 insertions, 14 deletions
diff --git a/Swift/Controllers/ContactSuggester.cpp b/Swift/Controllers/ContactSuggester.cpp
index f1104b0..41e0261 100644
--- a/Swift/Controllers/ContactSuggester.cpp
+++ b/Swift/Controllers/ContactSuggester.cpp
@@ -4,6 +4,12 @@
* See Documentation/Licenses/BSD-simplified.txt for more information.
*/
+/*
+ * Copyright (c) 2014 Kevin Smith and Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
#include <Swift/Controllers/ContactSuggester.h>
#include <boost/algorithm/string/find.hpp>
@@ -34,25 +40,22 @@ void ContactSuggester::addContactProvider(ContactProvider* provider) {
contactProviders_.push_back(provider);
}
-bool ContactSuggester::matchContact(const std::string& search, const Contact& c) {
- return fuzzyMatch(c.name, search) || fuzzyMatch(c.jid.toString(), search);
+bool ContactSuggester::matchContact(const std::string& search, const Contact::ref& c) {
+ return fuzzyMatch(c->name, search) || fuzzyMatch(c->jid.toString(), search);
}
-std::vector<Contact> ContactSuggester::getSuggestions(const std::string& search) const {
- std::vector<Contact> results;
+std::vector<Contact::ref> ContactSuggester::getSuggestions(const std::string& search) const {
+ std::vector<Contact::ref> results;
foreach(ContactProvider* provider, contactProviders_) {
append(results, provider->getContacts());
}
- std::sort(results.begin(), results.end(),
- lambda::bind(&Contact::jid, lambda::_1) < lambda::bind(&Contact::jid, lambda::_2));
- results.erase(std::unique(results.begin(), results.end(),
- lambda::bind(&Contact::jid, lambda::_1) == lambda::bind(&Contact::jid, lambda::_2)),
- results.end());
+ std::sort(results.begin(), results.end(), ContactSuggester::contactLexicographicalSortPredicate);
+ results.erase(std::unique(results.begin(), results.end(), ContactSuggester::contactEqualityPredicate), results.end());
results.erase(std::remove_if(results.begin(), results.end(), !lambda::bind(&ContactSuggester::matchContact, search, lambda::_1)),
results.end());
- std::sort(results.begin(), results.end(), ContactSuggester::chatSortPredicate);
+ std::sort(results.begin(), results.end(), ContactSuggester::contactSmartSortPredicate);
return results;
}
@@ -69,11 +72,19 @@ bool ContactSuggester::fuzzyMatch(std::string text, std::string match) {
return true;
}
-bool ContactSuggester::chatSortPredicate(const Contact& a, const Contact& b) {
- if (a.statusType == b.statusType) {
- return a.name.compare(b.name) < 0;
+bool ContactSuggester::contactLexicographicalSortPredicate(const Contact::ref& a, const Contact::ref& b) {
+ return a->jid < b->jid;
+}
+
+bool ContactSuggester::contactEqualityPredicate(const Contact::ref& a, const Contact::ref& b) {
+ return a->jid == b->jid;
+}
+
+bool ContactSuggester::contactSmartSortPredicate(const Contact::ref& a, const Contact::ref& b) {
+ if (a->statusType == b->statusType) {
+ return a->name.compare(b->name) < 0;
} else {
- return a.statusType < b.statusType;
+ return a->statusType < b->statusType;
}
}