summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Swift/Controllers/ContactSuggester.cpp14
-rw-r--r--Swift/QtUI/UserSearch/QtUserSearchWindow.cpp2
2 files changed, 10 insertions, 6 deletions
diff --git a/Swift/Controllers/ContactSuggester.cpp b/Swift/Controllers/ContactSuggester.cpp
index 09a1567..42e8308 100644
--- a/Swift/Controllers/ContactSuggester.cpp
+++ b/Swift/Controllers/ContactSuggester.cpp
@@ -31,47 +31,51 @@
namespace lambda = boost::lambda;
namespace Swift {
ContactSuggester::ContactSuggester() {
}
ContactSuggester::~ContactSuggester() {
}
void ContactSuggester::addContactProvider(ContactProvider* provider) {
contactProviders_.push_back(provider);
}
bool ContactSuggester::matchContact(const std::string& search, const Contact::ref& c) {
return fuzzyMatch(c->name, search) || fuzzyMatch(c->jid.toString(), search);
}
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(), Contact::lexicographicalSortPredicate);
results.erase(std::unique(results.begin(), results.end(), Contact::equalityPredicate), results.end());
results.erase(std::remove_if(results.begin(), results.end(), !lambda::bind(&matchContact, search, lambda::_1)),
results.end());
std::sort(results.begin(), results.end(), boost::bind(&Contact::sortPredicate, _1, _2, search));
return results;
}
bool ContactSuggester::fuzzyMatch(std::string text, std::string match) {
- for (std::string::iterator currentQueryChar = match.begin(); currentQueryChar != match.end(); currentQueryChar++) {
- //size_t result = text.find(*currentQueryChar);
- std::string::iterator result = boost::algorithm::ifind_first(text, std::string(currentQueryChar, currentQueryChar+1)).begin();
- if (result == text.end()) {
+ std::string lowerText = text;
+ boost::algorithm::to_lower(lowerText);
+ std::string lowerMatch = match;
+ boost::algorithm::to_lower(lowerMatch);
+ size_t lastMatch = 0;
+ for (size_t i = 0; i < lowerMatch.length(); ++i) {
+ size_t where = lowerText.find_first_of(lowerMatch[i], lastMatch);
+ if (where == std::string::npos) {
return false;
}
- text.erase(result);
+ lastMatch = where + 1;
}
return true;
}
}
diff --git a/Swift/QtUI/UserSearch/QtUserSearchWindow.cpp b/Swift/QtUI/UserSearch/QtUserSearchWindow.cpp
index ca84759..e309265 100644
--- a/Swift/QtUI/UserSearch/QtUserSearchWindow.cpp
+++ b/Swift/QtUI/UserSearch/QtUserSearchWindow.cpp
@@ -213,71 +213,71 @@ void QtUserSearchWindow::handleSearch() {
if (fieldsPage_->getFormWidget()) {
search->setForm(fieldsPage_->getFormWidget()->getCompletedForm());
} else {
if (fieldsPage_->nickInput_->isEnabled()) {
search->setNick(Q2PSTRING(fieldsPage_->nickInput_->text()));
}
if (fieldsPage_->firstInput_->isEnabled()) {
search->setFirst(Q2PSTRING(fieldsPage_->firstInput_->text()));
}
if (fieldsPage_->lastInput_->isEnabled()) {
search->setLast(Q2PSTRING(fieldsPage_->lastInput_->text()));
}
if (fieldsPage_->emailInput_->isEnabled()) {
search->setEMail(Q2PSTRING(fieldsPage_->emailInput_->text()));
}
}
onSearchRequested(search, getServerToSearch());
}
JID QtUserSearchWindow::getContactJID() const {
JID jid;
bool useSearchResult;
if (type_ == AddContact) {
useSearchResult = !firstPage_->byJID_->isChecked();
} else {
useSearchResult = true;
}
if (useSearchResult) {
if (dynamic_cast<UserSearchModel*>(model_)) {
UserSearchResult* userItem = static_cast<UserSearchResult*>(resultsPage_->results_->currentIndex().internalPointer());
if (userItem) { /* Remember to leave this if we change to dynamic cast */
jid = userItem->getJID();
}
- } else {
+ } else if (dynamic_cast<QtFormResultItemModel*>(model_)) {
int row = resultsPage_->results_->currentIndex().row();
Form::FormItem item = dynamic_cast<QtFormResultItemModel*>(model_)->getForm()->getItems().at(row);
JID fallbackJid;
foreach(FormField::ref field, item) {
if (field->getType() == FormField::JIDSingleType) {
jid = JID(field->getJIDSingleValue());
break;
}
if (field->getName() == "jid") {
fallbackJid = field->getValues()[0];
}
}
if (!jid.isValid()) {
jid = fallbackJid;
}
}
}
else {
jid = JID(Q2PSTRING(firstPage_->jid_->text().trimmed()));
}
return jid;
}
void QtUserSearchWindow::addSearchedJIDToList(const JID& jid) {
std::vector<JID> jids;
jids.push_back(jid);
handleJIDsAdded(jids);
firstMultiJIDPage_->jid_->clear();
}
void QtUserSearchWindow::show() {
clear();
QWidget::show();
}