OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/autocomplete/contact_provider_chromeos.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/i18n/string_search.h" |
| 10 #include "base/string16.h" |
| 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 14 #include "chrome/browser/chromeos/contacts/contact.h" |
| 15 #include "chrome/browser/chromeos/contacts/contact_manager.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 17 |
| 18 ContactProvider::ContactProvider( |
| 19 AutocompleteProviderListener* listener, |
| 20 Profile* profile) |
| 21 : AutocompleteProvider(listener, profile, "Contacts") { |
| 22 contacts::ContactManager* manager = contacts::ContactManager::GetInstance(); |
| 23 DCHECK(manager); |
| 24 manager->AddObserver(this); |
| 25 RefreshContacts(manager); |
| 26 } |
| 27 |
| 28 void ContactProvider::Start(const AutocompleteInput& input, |
| 29 bool minimal_changes) { |
| 30 matches_.clear(); |
| 31 |
| 32 if ((input.type() == AutocompleteInput::INVALID) || |
| 33 (input.type() == AutocompleteInput::FORCED_QUERY)) |
| 34 return; |
| 35 |
| 36 if (input.text().empty()) |
| 37 return; |
| 38 |
| 39 for (ContactDataVector::const_iterator it = contacts_.begin(); |
| 40 it != contacts_.end(); ++it) { |
| 41 const ContactData& contact = *it; |
| 42 // FIXME: do something better than this |
| 43 string16 name = UTF8ToUTF16(contact.name); |
| 44 if (base::i18n::StringSearchIgnoringCaseAndAccents(input.text(), name)) { |
| 45 AutocompleteMatch match(this, 0, false, AutocompleteMatch::CONTACT); |
| 46 match.fill_into_edit = input.text(); |
| 47 match.inline_autocomplete_offset = string16::npos; |
| 48 match.contents = AutocompleteMatch::SanitizeString(name); |
| 49 match.destination_url = GURL(contact.provider_id); // FIXME: this is bogu
s |
| 50 AutocompleteMatch::ClassifyMatchInString( |
| 51 input.text(), |
| 52 name, |
| 53 AutocompleteMatch::ACMatchClassification::MATCH, |
| 54 &match.contents_class); |
| 55 match.relevance = 1000; // FIXME: update autocomplete_provider.h |
| 56 matches_.push_back(match); |
| 57 } |
| 58 } |
| 59 } |
| 60 |
| 61 void ContactProvider::OnContactsUpdated(contacts::ContactManager* manager) { |
| 62 RefreshContacts(manager); |
| 63 } |
| 64 |
| 65 ContactProvider::ContactData::ContactData(const std::string& name, |
| 66 const std::string& provider_id) |
| 67 : name(name), |
| 68 provider_id(provider_id) { |
| 69 } |
| 70 |
| 71 ContactProvider::~ContactProvider() { |
| 72 contacts::ContactManager* manager = contacts::ContactManager::GetInstance(); |
| 73 DCHECK(manager); |
| 74 manager->RemoveObserver(this); |
| 75 } |
| 76 |
| 77 void ContactProvider::RefreshContacts(contacts::ContactManager* manager) { |
| 78 DCHECK(manager); |
| 79 typedef std::vector<const contacts::Contact*> ContactPointers; |
| 80 ContactPointers contacts; |
| 81 manager->GetAllContacts(&contacts); |
| 82 |
| 83 contacts_.clear(); |
| 84 for (ContactPointers::const_iterator it = contacts.begin(); |
| 85 it != contacts.end(); ++it) { |
| 86 const contacts::Contact& contact = **it; |
| 87 contacts_.push_back(ContactData(contact.full_name, contact.provider_id)); |
| 88 } |
| 89 } |
OLD | NEW |