| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/search_engines/template_url_table_model.h" | 5 #include "chrome/browser/ui/search_engines/template_url_table_model.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | 21 #include "third_party/skia/include/core/SkBitmap.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/base/models/table_model_observer.h" | 23 #include "ui/base/models/table_model_observer.h" |
| 24 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
| 25 #include "ui/gfx/favicon_size.h" | 25 #include "ui/gfx/favicon_size.h" |
| 26 #include "ui/gfx/image/image_skia.h" | 26 #include "ui/gfx/image/image_skia.h" |
| 27 | 27 |
| 28 // Group IDs used by TemplateURLTableModel. | 28 // Group IDs used by TemplateURLTableModel. |
| 29 static const int kMainGroupID = 0; | 29 static const int kMainGroupID = 0; |
| 30 static const int kOtherGroupID = 1; | 30 static const int kOtherGroupID = 1; |
| 31 static const int kExtensionGroupID = 2; |
| 31 | 32 |
| 32 // ModelEntry ---------------------------------------------------- | 33 // ModelEntry ---------------------------------------------------- |
| 33 | 34 |
| 34 // ModelEntry wraps a TemplateURL as returned from the TemplateURL. | 35 // ModelEntry wraps a TemplateURL as returned from the TemplateURL. |
| 35 // ModelEntry also tracks state information about the URL. | 36 // ModelEntry also tracks state information about the URL. |
| 36 | 37 |
| 37 // Icon used while loading, or if a specific favicon can't be found. | 38 // Icon used while loading, or if a specific favicon can't be found. |
| 38 static gfx::ImageSkia* default_icon = NULL; | 39 static gfx::ImageSkia* default_icon = NULL; |
| 39 | 40 |
| 40 class ModelEntry { | 41 class ModelEntry { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 entries_.clear(); | 137 entries_.clear(); |
| 137 } | 138 } |
| 138 | 139 |
| 139 void TemplateURLTableModel::Reload() { | 140 void TemplateURLTableModel::Reload() { |
| 140 STLDeleteElements(&entries_); | 141 STLDeleteElements(&entries_); |
| 141 entries_.clear(); | 142 entries_.clear(); |
| 142 | 143 |
| 143 TemplateURLService::TemplateURLVector urls = | 144 TemplateURLService::TemplateURLVector urls = |
| 144 template_url_service_->GetTemplateURLs(); | 145 template_url_service_->GetTemplateURLs(); |
| 145 | 146 |
| 147 std::vector<ModelEntry*> default_entries, other_entries, extension_entries; |
| 146 // Keywords that can be made the default first. | 148 // Keywords that can be made the default first. |
| 147 for (TemplateURLService::TemplateURLVector::iterator i = urls.begin(); | 149 for (TemplateURLService::TemplateURLVector::iterator i = urls.begin(); |
| 148 i != urls.end(); ++i) { | 150 i != urls.end(); ++i) { |
| 149 TemplateURL* template_url = *i; | 151 TemplateURL* template_url = *i; |
| 150 // NOTE: we don't use ShowInDefaultList here to avoid items bouncing around | 152 // NOTE: we don't use ShowInDefaultList here to avoid items bouncing around |
| 151 // the lists while editing. | 153 // the lists while editing. |
| 152 if (template_url->show_in_default_list()) | 154 if (template_url->show_in_default_list()) |
| 153 entries_.push_back(new ModelEntry(this, template_url)); | 155 default_entries.push_back(new ModelEntry(this, template_url)); |
| 156 else if (template_url->IsExtensionKeyword()) |
| 157 extension_entries.push_back(new ModelEntry(this, template_url)); |
| 158 else |
| 159 other_entries.push_back(new ModelEntry(this, template_url)); |
| 154 } | 160 } |
| 155 | 161 |
| 156 last_search_engine_index_ = static_cast<int>(entries_.size()); | 162 last_search_engine_index_ = static_cast<int>(default_entries.size()); |
| 163 last_other_engine_index_ = last_search_engine_index_ + |
| 164 static_cast<int>(other_entries.size()); |
| 157 | 165 |
| 158 // Then the rest. | 166 entries_.insert(entries_.end(), |
| 159 for (TemplateURLService::TemplateURLVector::iterator i = urls.begin(); | 167 default_entries.begin(), |
| 160 i != urls.end(); ++i) { | 168 default_entries.end()); |
| 161 TemplateURL* template_url = *i; | 169 |
| 162 // NOTE: we don't use ShowInDefaultList here to avoid things bouncing | 170 entries_.insert(entries_.end(), |
| 163 // the lists while editing. | 171 other_entries.begin(), |
| 164 if (!template_url->show_in_default_list() && | 172 other_entries.end()); |
| 165 !template_url->IsExtensionKeyword()) { | 173 |
| 166 entries_.push_back(new ModelEntry(this, template_url)); | 174 entries_.insert(entries_.end(), |
| 167 } | 175 extension_entries.begin(), |
| 168 } | 176 extension_entries.end()); |
| 169 | 177 |
| 170 if (observer_) | 178 if (observer_) |
| 171 observer_->OnModelChanged(); | 179 observer_->OnModelChanged(); |
| 172 } | 180 } |
| 173 | 181 |
| 174 int TemplateURLTableModel::RowCount() { | 182 int TemplateURLTableModel::RowCount() { |
| 175 return static_cast<int>(entries_.size()); | 183 return static_cast<int>(entries_.size()); |
| 176 } | 184 } |
| 177 | 185 |
| 178 string16 TemplateURLTableModel::GetText(int row, int col_id) { | 186 string16 TemplateURLTableModel::GetText(int row, int col_id) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_MAIN_SEPARATOR); | 223 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_MAIN_SEPARATOR); |
| 216 search_engine_group.id = kMainGroupID; | 224 search_engine_group.id = kMainGroupID; |
| 217 groups.push_back(search_engine_group); | 225 groups.push_back(search_engine_group); |
| 218 | 226 |
| 219 Group other_group; | 227 Group other_group; |
| 220 other_group.title = | 228 other_group.title = |
| 221 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_OTHER_SEPARATOR); | 229 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_OTHER_SEPARATOR); |
| 222 other_group.id = kOtherGroupID; | 230 other_group.id = kOtherGroupID; |
| 223 groups.push_back(other_group); | 231 groups.push_back(other_group); |
| 224 | 232 |
| 233 Group extension_group; |
| 234 extension_group.title = |
| 235 l10n_util::GetStringUTF16(IDS_SEARCH_ENGINES_EDITOR_EXTENSIONS_SEPARATOR); |
| 236 extension_group.id = kExtensionGroupID; |
| 237 groups.push_back(extension_group); |
| 238 |
| 225 return groups; | 239 return groups; |
| 226 } | 240 } |
| 227 | 241 |
| 228 int TemplateURLTableModel::GetGroupID(int row) { | 242 int TemplateURLTableModel::GetGroupID(int row) { |
| 229 DCHECK(row >= 0 && row < RowCount()); | 243 DCHECK(row >= 0 && row < RowCount()); |
| 230 return row < last_search_engine_index_ ? kMainGroupID : kOtherGroupID; | 244 if (row < last_search_engine_index_) |
| 245 return kMainGroupID; |
| 246 return row < last_other_engine_index_ ? kOtherGroupID : kExtensionGroupID; |
| 231 } | 247 } |
| 232 | 248 |
| 233 void TemplateURLTableModel::Remove(int index) { | 249 void TemplateURLTableModel::Remove(int index) { |
| 234 // Remove the observer while we modify the model, that way we don't need to | 250 // Remove the observer while we modify the model, that way we don't need to |
| 235 // worry about the model calling us back when we mutate it. | 251 // worry about the model calling us back when we mutate it. |
| 236 template_url_service_->RemoveObserver(this); | 252 template_url_service_->RemoveObserver(this); |
| 237 TemplateURL* template_url = GetTemplateURL(index); | 253 TemplateURL* template_url = GetTemplateURL(index); |
| 238 | 254 |
| 239 scoped_ptr<ModelEntry> entry(entries_[index]); | 255 scoped_ptr<ModelEntry> entry(entries_[index]); |
| 240 entries_.erase(entries_.begin() + index); | 256 entries_.erase(entries_.begin() + index); |
| 241 if (index < last_search_engine_index_) | 257 if (index < last_search_engine_index_) |
| 242 last_search_engine_index_--; | 258 --last_search_engine_index_; |
| 259 if (index < last_other_engine_index_) |
| 260 --last_other_engine_index_; |
| 243 if (observer_) | 261 if (observer_) |
| 244 observer_->OnItemsRemoved(index, 1); | 262 observer_->OnItemsRemoved(index, 1); |
| 245 | 263 |
| 246 // Make sure to remove from the table model first, otherwise the | 264 // Make sure to remove from the table model first, otherwise the |
| 247 // TemplateURL would be freed. | 265 // TemplateURL would be freed. |
| 248 template_url_service_->Remove(template_url); | 266 template_url_service_->Remove(template_url); |
| 249 template_url_service_->AddObserver(this); | 267 template_url_service_->AddObserver(this); |
| 250 } | 268 } |
| 251 | 269 |
| 252 void TemplateURLTableModel::Add(int index, | 270 void TemplateURLTableModel::Add(int index, |
| 253 const string16& short_name, | 271 const string16& short_name, |
| 254 const string16& keyword, | 272 const string16& keyword, |
| 255 const std::string& url) { | 273 const std::string& url) { |
| 256 DCHECK(index >= 0 && index <= RowCount()); | 274 DCHECK(index >= 0 && index <= RowCount()); |
| 257 DCHECK(!url.empty()); | 275 DCHECK(!url.empty()); |
| 258 template_url_service_->RemoveObserver(this); | 276 template_url_service_->RemoveObserver(this); |
| 259 TemplateURLData data; | 277 TemplateURLData data; |
| 260 data.short_name = short_name; | 278 data.short_name = short_name; |
| 261 data.SetKeyword(keyword); | 279 data.SetKeyword(keyword); |
| 262 data.SetURL(url); | 280 data.SetURL(url); |
| 263 TemplateURL* turl = new TemplateURL(template_url_service_->profile(), data); | 281 TemplateURL* turl = new TemplateURL(template_url_service_->profile(), data); |
| 264 template_url_service_->Add(turl); | 282 template_url_service_->Add(turl); |
| 265 ModelEntry* entry = new ModelEntry(this, turl); | 283 ModelEntry* entry = new ModelEntry(this, turl); |
| 266 template_url_service_->AddObserver(this); | 284 template_url_service_->AddObserver(this); |
| 267 entries_.insert(entries_.begin() + index, entry); | 285 entries_.insert(entries_.begin() + index, entry); |
| 286 if (index <= last_other_engine_index_) |
| 287 ++last_other_engine_index_; |
| 268 if (observer_) | 288 if (observer_) |
| 269 observer_->OnItemsAdded(index, 1); | 289 observer_->OnItemsAdded(index, 1); |
| 270 } | 290 } |
| 271 | 291 |
| 272 void TemplateURLTableModel::ModifyTemplateURL(int index, | 292 void TemplateURLTableModel::ModifyTemplateURL(int index, |
| 273 const string16& title, | 293 const string16& title, |
| 274 const string16& keyword, | 294 const string16& keyword, |
| 275 const std::string& url) { | 295 const std::string& url) { |
| 276 DCHECK(index >= 0 && index <= RowCount()); | 296 DCHECK(index >= 0 && index <= RowCount()); |
| 277 DCHECK(!url.empty()); | 297 DCHECK(!url.empty()); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 void TemplateURLTableModel::FaviconAvailable(ModelEntry* entry) { | 386 void TemplateURLTableModel::FaviconAvailable(ModelEntry* entry) { |
| 367 std::vector<ModelEntry*>::iterator i = | 387 std::vector<ModelEntry*>::iterator i = |
| 368 std::find(entries_.begin(), entries_.end(), entry); | 388 std::find(entries_.begin(), entries_.end(), entry); |
| 369 DCHECK(i != entries_.end()); | 389 DCHECK(i != entries_.end()); |
| 370 NotifyChanged(static_cast<int>(i - entries_.begin())); | 390 NotifyChanged(static_cast<int>(i - entries_.begin())); |
| 371 } | 391 } |
| 372 | 392 |
| 373 void TemplateURLTableModel::OnTemplateURLServiceChanged() { | 393 void TemplateURLTableModel::OnTemplateURLServiceChanged() { |
| 374 Reload(); | 394 Reload(); |
| 375 } | 395 } |
| OLD | NEW |