| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/views/keyword_editor_view.h" | 5 #include "chrome/browser/search_engines/template_url_table_model.h" |
| 6 | |
| 7 #include <vector> | |
| 8 | 6 |
| 9 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 10 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "app/table_model_observer.h" |
| 11 #include "base/gfx/png_decoder.h" | 10 #include "base/gfx/png_decoder.h" |
| 12 #include "base/stl_util-inl.h" | |
| 13 #include "base/string_util.h" | |
| 14 #include "chrome/browser/history/history.h" | 11 #include "chrome/browser/history/history.h" |
| 15 #include "chrome/browser/profile.h" | 12 #include "chrome/browser/profile.h" |
| 16 #include "chrome/browser/metrics/user_metrics.h" | |
| 17 #include "chrome/browser/search_engines/template_url.h" | 13 #include "chrome/browser/search_engines/template_url.h" |
| 18 #include "chrome/browser/search_engines/template_url_model.h" | 14 #include "chrome/browser/search_engines/template_url_model.h" |
| 19 #include "chrome/browser/views/browser_dialogs.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "chrome/common/pref_service.h" | |
| 22 #include "googleurl/src/gurl.h" | |
| 23 #include "grit/app_resources.h" | 15 #include "grit/app_resources.h" |
| 24 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 25 #include "grit/locale_settings.h" | |
| 26 #include "grit/theme_resources.h" | |
| 27 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 28 #include "views/background.h" | |
| 29 #include "views/grid_layout.h" | |
| 30 #include "views/controls/button/native_button.h" | |
| 31 #include "views/controls/table/table_view.h" | |
| 32 #include "views/controls/textfield/textfield.h" | |
| 33 #include "views/standard_layout.h" | |
| 34 #include "views/widget/widget.h" | |
| 35 #include "views/window/dialog_delegate.h" | |
| 36 #include "views/window/window.h" | |
| 37 | |
| 38 using views::GridLayout; | |
| 39 using views::NativeButton; | |
| 40 | 18 |
| 41 // Group IDs used by TemplateURLTableModel. | 19 // Group IDs used by TemplateURLTableModel. |
| 42 static const int kMainGroupID = 0; | 20 static const int kMainGroupID = 0; |
| 43 static const int kOtherGroupID = 1; | 21 static const int kOtherGroupID = 1; |
| 44 | 22 |
| 45 namespace browser { | |
| 46 | |
| 47 // Declared in browser_dialogs.h so others don't have to depend on our header. | |
| 48 void ShowKeywordEditorView(Profile* profile) { | |
| 49 KeywordEditorView::Show(profile); | |
| 50 } | |
| 51 | |
| 52 } // namespace browser | |
| 53 | |
| 54 // ModelEntry ---------------------------------------------------- | 23 // ModelEntry ---------------------------------------------------- |
| 55 | 24 |
| 56 // ModelEntry wraps a TemplateURL as returned from the TemplateURL. | 25 // ModelEntry wraps a TemplateURL as returned from the TemplateURL. |
| 57 // ModelEntry also tracks state information about the URL. | 26 // ModelEntry also tracks state information about the URL. |
| 58 | 27 |
| 59 // Icon used while loading, or if a specific favicon can't be found. | 28 // Icon used while loading, or if a specific favicon can't be found. |
| 60 static SkBitmap* default_icon = NULL; | 29 static SkBitmap* default_icon = NULL; |
| 61 | 30 |
| 62 class ModelEntry { | 31 class ModelEntry { |
| 63 public: | 32 public: |
| 64 explicit ModelEntry(TemplateURLTableModel* model, | 33 explicit ModelEntry(TemplateURLTableModel* model, |
| 65 const TemplateURL& template_url) | 34 const TemplateURL& template_url) |
| 66 : model_(model), | 35 : template_url_(template_url), |
| 67 template_url_(template_url) { | 36 load_state_(NOT_LOADED), |
| 68 load_state_ = NOT_LOADED; | 37 model_(model) { |
| 69 if (!default_icon) { | 38 if (!default_icon) { |
| 70 default_icon = ResourceBundle::GetSharedInstance(). | 39 default_icon = ResourceBundle::GetSharedInstance(). |
| 71 GetBitmapNamed(IDR_DEFAULT_FAVICON); | 40 GetBitmapNamed(IDR_DEFAULT_FAVICON); |
| 72 } | 41 } |
| 73 } | 42 } |
| 74 | 43 |
| 75 const TemplateURL& template_url() { | 44 const TemplateURL& template_url() { |
| 76 return template_url_; | 45 return template_url_; |
| 77 } | 46 } |
| 78 | 47 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 103 load_state_ = LOADED; | 72 load_state_ = LOADED; |
| 104 HistoryService* hs = | 73 HistoryService* hs = |
| 105 model_->template_url_model()->profile()->GetHistoryService( | 74 model_->template_url_model()->profile()->GetHistoryService( |
| 106 Profile::EXPLICIT_ACCESS); | 75 Profile::EXPLICIT_ACCESS); |
| 107 if (!hs) | 76 if (!hs) |
| 108 return; | 77 return; |
| 109 GURL fav_icon_url = template_url().GetFavIconURL(); | 78 GURL fav_icon_url = template_url().GetFavIconURL(); |
| 110 if (!fav_icon_url.is_valid()) { | 79 if (!fav_icon_url.is_valid()) { |
| 111 // The favicon url isn't always set. Guess at one here. | 80 // The favicon url isn't always set. Guess at one here. |
| 112 if (template_url_.url() && template_url_.url()->IsValid()) { | 81 if (template_url_.url() && template_url_.url()->IsValid()) { |
| 113 GURL url = GURL(template_url_.url()->url()); | 82 GURL url = GURL(WideToUTF16Hack(template_url_.url()->url())); |
| 114 if (url.is_valid()) | 83 if (url.is_valid()) |
| 115 fav_icon_url = TemplateURL::GenerateFaviconURL(url); | 84 fav_icon_url = TemplateURL::GenerateFaviconURL(url); |
| 116 } | 85 } |
| 117 if (!fav_icon_url.is_valid()) | 86 if (!fav_icon_url.is_valid()) |
| 118 return; | 87 return; |
| 119 } | 88 } |
| 120 load_state_ = LOADING; | 89 load_state_ = LOADING; |
| 121 hs->GetFavIcon(fav_icon_url, | 90 hs->GetFavIcon(fav_icon_url, |
| 122 &request_consumer_, | 91 &request_consumer_, |
| 123 NewCallback(this, &ModelEntry::OnFavIconDataAvailable)); | 92 NewCallback(this, &ModelEntry::OnFavIconDataAvailable)); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 145 DISALLOW_EVIL_CONSTRUCTORS(ModelEntry); | 114 DISALLOW_EVIL_CONSTRUCTORS(ModelEntry); |
| 146 }; | 115 }; |
| 147 | 116 |
| 148 // TemplateURLTableModel ----------------------------------------- | 117 // TemplateURLTableModel ----------------------------------------- |
| 149 | 118 |
| 150 TemplateURLTableModel::TemplateURLTableModel( | 119 TemplateURLTableModel::TemplateURLTableModel( |
| 151 TemplateURLModel* template_url_model) | 120 TemplateURLModel* template_url_model) |
| 152 : observer_(NULL), | 121 : observer_(NULL), |
| 153 template_url_model_(template_url_model) { | 122 template_url_model_(template_url_model) { |
| 154 DCHECK(template_url_model); | 123 DCHECK(template_url_model); |
| 124 template_url_model_->Load(); |
| 125 template_url_model_->AddObserver(this); |
| 155 Reload(); | 126 Reload(); |
| 156 } | 127 } |
| 157 | 128 |
| 158 TemplateURLTableModel::~TemplateURLTableModel() { | 129 TemplateURLTableModel::~TemplateURLTableModel() { |
| 130 template_url_model_->RemoveObserver(this); |
| 159 STLDeleteElements(&entries_); | 131 STLDeleteElements(&entries_); |
| 160 entries_.clear(); | 132 entries_.clear(); |
| 161 } | 133 } |
| 162 | 134 |
| 163 void TemplateURLTableModel::Reload() { | 135 void TemplateURLTableModel::Reload() { |
| 164 STLDeleteElements(&entries_); | 136 STLDeleteElements(&entries_); |
| 165 entries_.clear(); | 137 entries_.clear(); |
| 166 | 138 |
| 167 std::vector<const TemplateURL*> urls = template_url_model_->GetTemplateURLs(); | 139 std::vector<const TemplateURL*> urls = template_url_model_->GetTemplateURLs(); |
| 168 | 140 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 | 233 |
| 262 return groups; | 234 return groups; |
| 263 } | 235 } |
| 264 | 236 |
| 265 int TemplateURLTableModel::GetGroupID(int row) { | 237 int TemplateURLTableModel::GetGroupID(int row) { |
| 266 DCHECK(row >= 0 && row < RowCount()); | 238 DCHECK(row >= 0 && row < RowCount()); |
| 267 return row < last_search_engine_index_ ? kMainGroupID : kOtherGroupID; | 239 return row < last_search_engine_index_ ? kMainGroupID : kOtherGroupID; |
| 268 } | 240 } |
| 269 | 241 |
| 270 void TemplateURLTableModel::Remove(int index) { | 242 void TemplateURLTableModel::Remove(int index) { |
| 243 // Remove the observer while we modify the model, that way we don't need to |
| 244 // worry about the model calling us back when we mutate it. |
| 245 template_url_model_->RemoveObserver(this); |
| 246 const TemplateURL* template_url = &GetTemplateURL(index); |
| 247 |
| 271 scoped_ptr<ModelEntry> entry(entries_[static_cast<int>(index)]); | 248 scoped_ptr<ModelEntry> entry(entries_[static_cast<int>(index)]); |
| 272 entries_.erase(entries_.begin() + static_cast<int>(index)); | 249 entries_.erase(entries_.begin() + static_cast<int>(index)); |
| 273 if (index < last_search_engine_index_) | 250 if (index < last_search_engine_index_) |
| 274 last_search_engine_index_--; | 251 last_search_engine_index_--; |
| 275 if (observer_) | 252 if (observer_) |
| 276 observer_->OnItemsRemoved(index, 1); | 253 observer_->OnItemsRemoved(index, 1); |
| 254 |
| 255 // Make sure to remove from the table model first, otherwise the |
| 256 // TemplateURL would be freed. |
| 257 template_url_model_->Remove(template_url); |
| 258 template_url_model_->AddObserver(this); |
| 277 } | 259 } |
| 278 | 260 |
| 279 void TemplateURLTableModel::Add(int index, const TemplateURL* template_url) { | 261 void TemplateURLTableModel::Add(int index, TemplateURL* template_url) { |
| 280 DCHECK(index >= 0 && index <= RowCount()); | 262 DCHECK(index >= 0 && index <= RowCount()); |
| 281 ModelEntry* entry = new ModelEntry(this, *template_url); | 263 ModelEntry* entry = new ModelEntry(this, *template_url); |
| 282 entries_.insert(entries_.begin() + index, entry); | 264 entries_.insert(entries_.begin() + index, entry); |
| 283 if (observer_) | 265 if (observer_) |
| 284 observer_->OnItemsAdded(index, 1); | 266 observer_->OnItemsAdded(index, 1); |
| 267 template_url_model_->RemoveObserver(this); |
| 268 template_url_model_->Add(template_url); |
| 269 template_url_model_->AddObserver(this); |
| 270 } |
| 271 |
| 272 void TemplateURLTableModel::ModifyTemplateURL(int index, |
| 273 const std::wstring& title, |
| 274 const std::wstring& keyword, |
| 275 const std::wstring& url) { |
| 276 DCHECK(index >= 0 && index <= RowCount()); |
| 277 const TemplateURL* template_url = &GetTemplateURL(index); |
| 278 template_url_model_->RemoveObserver(this); |
| 279 template_url_model_->ResetTemplateURL(template_url, title, keyword, url); |
| 280 if (template_url_model_->GetDefaultSearchProvider() == template_url && |
| 281 !TemplateURL::SupportsReplacement(template_url)) { |
| 282 // The entry was the default search provider, but the url has been modified |
| 283 // so that it no longer supports replacement. Reset the default search |
| 284 // provider so that it doesn't point to a bogus entry. |
| 285 template_url_model_->SetDefaultSearchProvider(NULL); |
| 286 } |
| 287 template_url_model_->AddObserver(this); |
| 288 ReloadIcon(index); // Also calls NotifyChanged(). |
| 285 } | 289 } |
| 286 | 290 |
| 287 void TemplateURLTableModel::ReloadIcon(int index) { | 291 void TemplateURLTableModel::ReloadIcon(int index) { |
| 288 DCHECK(index >= 0 && index < RowCount()); | 292 DCHECK(index >= 0 && index < RowCount()); |
| 289 | 293 |
| 290 entries_[index]->ResetIcon(); | 294 entries_[index]->ResetIcon(); |
| 291 | 295 |
| 292 NotifyChanged(index); | 296 NotifyChanged(index); |
| 293 } | 297 } |
| 294 | 298 |
| 295 const TemplateURL& TemplateURLTableModel::GetTemplateURL(int index) { | 299 const TemplateURL& TemplateURLTableModel::GetTemplateURL(int index) { |
| 296 return entries_[index]->template_url(); | 300 return entries_[index]->template_url(); |
| 297 } | 301 } |
| 298 | 302 |
| 299 int TemplateURLTableModel::IndexOfTemplateURL( | 303 int TemplateURLTableModel::IndexOfTemplateURL( |
| 300 const TemplateURL* template_url) { | 304 const TemplateURL* template_url) { |
| 301 for (std::vector<ModelEntry*>::iterator i = entries_.begin(); | 305 for (std::vector<ModelEntry*>::iterator i = entries_.begin(); |
| 302 i != entries_.end(); ++i) { | 306 i != entries_.end(); ++i) { |
| 303 ModelEntry* entry = *i; | 307 ModelEntry* entry = *i; |
| 304 if (&(entry->template_url()) == template_url) | 308 if (&(entry->template_url()) == template_url) |
| 305 return static_cast<int>(i - entries_.begin()); | 309 return static_cast<int>(i - entries_.begin()); |
| 306 } | 310 } |
| 307 return -1; | 311 return -1; |
| 308 } | 312 } |
| 309 | 313 |
| 310 void TemplateURLTableModel::MoveToMainGroup(int index) { | 314 int TemplateURLTableModel::MoveToMainGroup(int index) { |
| 311 if (index < last_search_engine_index_) | 315 if (index < last_search_engine_index_) |
| 312 return; // Already in the main group. | 316 return -1; // Already in the main group. |
| 313 | 317 |
| 314 ModelEntry* current_entry = entries_[index]; | 318 ModelEntry* current_entry = entries_[index]; |
| 315 entries_.erase(index + entries_.begin()); | 319 entries_.erase(index + entries_.begin()); |
| 316 if (observer_) | 320 if (observer_) |
| 317 observer_->OnItemsRemoved(index, 1); | 321 observer_->OnItemsRemoved(index, 1); |
| 318 | 322 |
| 319 const int new_index = last_search_engine_index_++; | 323 const int new_index = last_search_engine_index_++; |
| 320 entries_.insert(entries_.begin() + new_index, current_entry); | 324 entries_.insert(entries_.begin() + new_index, current_entry); |
| 321 if (observer_) | 325 if (observer_) |
| 322 observer_->OnItemsAdded(new_index, 1); | 326 observer_->OnItemsAdded(new_index, 1); |
| 327 return new_index; |
| 328 } |
| 329 |
| 330 int TemplateURLTableModel::MakeDefaultTemplateURL(int index) { |
| 331 if (index < 0 || index >= RowCount()) { |
| 332 NOTREACHED(); |
| 333 return -1; |
| 334 } |
| 335 |
| 336 const TemplateURL* keyword = &GetTemplateURL(index); |
| 337 const TemplateURL* current_default = |
| 338 template_url_model_->GetDefaultSearchProvider(); |
| 339 if (current_default == keyword) |
| 340 return -1; |
| 341 |
| 342 template_url_model_->RemoveObserver(this); |
| 343 template_url_model_->SetDefaultSearchProvider(keyword); |
| 344 template_url_model_->AddObserver(this); |
| 345 |
| 346 // The formatting of the default engine is different; notify the table that |
| 347 // both old and new entries have changed. |
| 348 if (current_default != NULL) |
| 349 NotifyChanged(IndexOfTemplateURL(current_default)); |
| 350 const int new_index = IndexOfTemplateURL(keyword); |
| 351 NotifyChanged(new_index); |
| 352 |
| 353 // Make sure the new default is in the main group. |
| 354 return MoveToMainGroup(index); |
| 323 } | 355 } |
| 324 | 356 |
| 325 void TemplateURLTableModel::NotifyChanged(int index) { | 357 void TemplateURLTableModel::NotifyChanged(int index) { |
| 326 if (observer_) | 358 if (observer_) |
| 327 observer_->OnItemsChanged(index, 1); | 359 observer_->OnItemsChanged(index, 1); |
| 328 } | 360 } |
| 329 | 361 |
| 330 void TemplateURLTableModel::FavIconAvailable(ModelEntry* entry) { | 362 void TemplateURLTableModel::FavIconAvailable(ModelEntry* entry) { |
| 331 std::vector<ModelEntry*>::iterator i = | 363 std::vector<ModelEntry*>::iterator i = |
| 332 find(entries_.begin(), entries_.end(), entry); | 364 find(entries_.begin(), entries_.end(), entry); |
| 333 DCHECK(i != entries_.end()); | 365 DCHECK(i != entries_.end()); |
| 334 NotifyChanged(static_cast<int>(i - entries_.begin())); | 366 NotifyChanged(static_cast<int>(i - entries_.begin())); |
| 335 } | 367 } |
| 336 | 368 |
| 337 // KeywordEditorView ---------------------------------------------------------- | 369 void TemplateURLTableModel::OnTemplateURLModelChanged() { |
| 338 | 370 Reload(); |
| 339 // If non-null, there is an open editor and this is the window it is contained | |
| 340 // in. | |
| 341 static views::Window* open_window = NULL; | |
| 342 | |
| 343 // static | |
| 344 void KeywordEditorView::Show(Profile* profile) { | |
| 345 if (!profile->GetTemplateURLModel()) | |
| 346 return; | |
| 347 | |
| 348 if (open_window != NULL) | |
| 349 open_window->Close(); | |
| 350 DCHECK(!open_window); | |
| 351 | |
| 352 // Both of these will be deleted when the dialog closes. | |
| 353 KeywordEditorView* keyword_editor = new KeywordEditorView(profile); | |
| 354 | |
| 355 // Initialize the UI. By passing in an empty rect KeywordEditorView is | |
| 356 // queried for its preferred size. | |
| 357 open_window = views::Window::CreateChromeWindow(NULL, gfx::Rect(), | |
| 358 keyword_editor); | |
| 359 | |
| 360 open_window->Show(); | |
| 361 } | 371 } |
| 362 | |
| 363 KeywordEditorView::KeywordEditorView(Profile* profile) | |
| 364 : profile_(profile), | |
| 365 url_model_(profile->GetTemplateURLModel()) { | |
| 366 DCHECK(url_model_); | |
| 367 Init(); | |
| 368 } | |
| 369 | |
| 370 KeywordEditorView::~KeywordEditorView() { | |
| 371 // Only remove the listener if we installed one. | |
| 372 if (table_model_.get()) { | |
| 373 table_view_->SetModel(NULL); | |
| 374 url_model_->RemoveObserver(this); | |
| 375 } | |
| 376 } | |
| 377 | |
| 378 void KeywordEditorView::OnEditedKeyword(const TemplateURL* template_url, | |
| 379 const std::wstring& title, | |
| 380 const std::wstring& keyword, | |
| 381 const std::wstring& url) { | |
| 382 if (template_url) { | |
| 383 ModifyTemplateURL(template_url, title, keyword, url); | |
| 384 } else { | |
| 385 AddTemplateURL(title, keyword, url); | |
| 386 } | |
| 387 } | |
| 388 | |
| 389 void KeywordEditorView::AddTemplateURL(const std::wstring& title, | |
| 390 const std::wstring& keyword, | |
| 391 const std::wstring& url) { | |
| 392 DCHECK(!url.empty()); | |
| 393 | |
| 394 UserMetrics::RecordAction(L"KeywordEditor_AddKeyword", profile_); | |
| 395 | |
| 396 TemplateURL* template_url = new TemplateURL(); | |
| 397 template_url->set_short_name(title); | |
| 398 template_url->set_keyword(keyword); | |
| 399 template_url->SetURL(url, 0, 0); | |
| 400 | |
| 401 // There's a bug (1090726) in TableView with groups enabled such that newly | |
| 402 // added items in groups ALWAYS appear at the end, regardless of the index | |
| 403 // passed in. Worse yet, the selected rows get messed up when this happens | |
| 404 // causing other problems. As a work around we always add the item to the end | |
| 405 // of the list. | |
| 406 const int new_index = table_model_->RowCount(); | |
| 407 url_model_->RemoveObserver(this); | |
| 408 table_model_->Add(new_index, template_url); | |
| 409 url_model_->Add(template_url); | |
| 410 url_model_->AddObserver(this); | |
| 411 | |
| 412 table_view_->Select(new_index); | |
| 413 } | |
| 414 | |
| 415 void KeywordEditorView::ModifyTemplateURL(const TemplateURL* template_url, | |
| 416 const std::wstring& title, | |
| 417 const std::wstring& keyword, | |
| 418 const std::wstring& url) { | |
| 419 const int index = table_model_->IndexOfTemplateURL(template_url); | |
| 420 if (index == -1) { | |
| 421 // Will happen if url was deleted out from under us while the user was | |
| 422 // editing it. | |
| 423 return; | |
| 424 } | |
| 425 | |
| 426 // Don't do anything if the entry didn't change. | |
| 427 if (template_url->short_name() == title && | |
| 428 template_url->keyword() == keyword && | |
| 429 ((url.empty() && !template_url->url()) || | |
| 430 (!url.empty() && template_url->url() && | |
| 431 template_url->url()->url() == url))) { | |
| 432 return; | |
| 433 } | |
| 434 | |
| 435 url_model_->RemoveObserver(this); | |
| 436 url_model_->ResetTemplateURL(template_url, title, keyword, url); | |
| 437 if (url_model_->GetDefaultSearchProvider() == template_url && | |
| 438 !TemplateURL::SupportsReplacement(template_url)) { | |
| 439 // The entry was the default search provider, but the url has been modified | |
| 440 // so that it no longer supports replacement. Reset the default search | |
| 441 // provider so that it doesn't point to a bogus entry. | |
| 442 url_model_->SetDefaultSearchProvider(NULL); | |
| 443 } | |
| 444 url_model_->AddObserver(this); | |
| 445 table_model_->ReloadIcon(index); // Also calls NotifyChanged(). | |
| 446 | |
| 447 // Force the make default button to update. | |
| 448 OnSelectionChanged(); | |
| 449 | |
| 450 UserMetrics::RecordAction(L"KeywordEditor_ModifiedKeyword", profile_); | |
| 451 } | |
| 452 | |
| 453 gfx::Size KeywordEditorView::GetPreferredSize() { | |
| 454 return gfx::Size(views::Window::GetLocalizedContentsSize( | |
| 455 IDS_SEARCHENGINES_DIALOG_WIDTH_CHARS, | |
| 456 IDS_SEARCHENGINES_DIALOG_HEIGHT_LINES)); | |
| 457 } | |
| 458 | |
| 459 bool KeywordEditorView::CanResize() const { | |
| 460 return true; | |
| 461 } | |
| 462 | |
| 463 std::wstring KeywordEditorView::GetWindowTitle() const { | |
| 464 return l10n_util::GetString(IDS_SEARCH_ENGINES_EDITOR_WINDOW_TITLE); | |
| 465 } | |
| 466 | |
| 467 int KeywordEditorView::GetDialogButtons() const { | |
| 468 return MessageBoxFlags::DIALOGBUTTON_CANCEL; | |
| 469 } | |
| 470 | |
| 471 bool KeywordEditorView::Accept() { | |
| 472 open_window = NULL; | |
| 473 return true; | |
| 474 } | |
| 475 | |
| 476 bool KeywordEditorView::Cancel() { | |
| 477 open_window = NULL; | |
| 478 return true; | |
| 479 } | |
| 480 | |
| 481 views::View* KeywordEditorView::GetContentsView() { | |
| 482 return this; | |
| 483 } | |
| 484 | |
| 485 void KeywordEditorView::Init() { | |
| 486 DCHECK(!table_model_.get()); | |
| 487 | |
| 488 url_model_->Load(); | |
| 489 url_model_->AddObserver(this); | |
| 490 | |
| 491 table_model_.reset(new TemplateURLTableModel(url_model_)); | |
| 492 | |
| 493 std::vector<TableColumn> columns; | |
| 494 columns.push_back( | |
| 495 TableColumn(IDS_SEARCH_ENGINES_EDITOR_DESCRIPTION_COLUMN, | |
| 496 TableColumn::LEFT, -1, .75)); | |
| 497 columns.back().sortable = true; | |
| 498 columns.push_back( | |
| 499 TableColumn(IDS_SEARCH_ENGINES_EDITOR_KEYWORD_COLUMN, | |
| 500 TableColumn::LEFT, -1, .25)); | |
| 501 columns.back().sortable = true; | |
| 502 table_view_ = new views::TableView(table_model_.get(), columns, | |
| 503 views::ICON_AND_TEXT, false, true, true); | |
| 504 table_view_->SetObserver(this); | |
| 505 | |
| 506 add_button_ = new views::NativeButton( | |
| 507 this, l10n_util::GetString(IDS_SEARCH_ENGINES_EDITOR_NEW_BUTTON)); | |
| 508 add_button_->SetEnabled(url_model_->loaded()); | |
| 509 | |
| 510 edit_button_ = new views::NativeButton( | |
| 511 this, l10n_util::GetString(IDS_SEARCH_ENGINES_EDITOR_EDIT_BUTTON)); | |
| 512 edit_button_->SetEnabled(false); | |
| 513 | |
| 514 remove_button_ = new views::NativeButton( | |
| 515 this, l10n_util::GetString(IDS_SEARCH_ENGINES_EDITOR_REMOVE_BUTTON)); | |
| 516 remove_button_->SetEnabled(false); | |
| 517 | |
| 518 make_default_button_ = new views::NativeButton( | |
| 519 this, | |
| 520 l10n_util::GetString(IDS_SEARCH_ENGINES_EDITOR_MAKE_DEFAULT_BUTTON)); | |
| 521 make_default_button_->SetEnabled(false); | |
| 522 | |
| 523 InitLayoutManager(); | |
| 524 } | |
| 525 | |
| 526 void KeywordEditorView::InitLayoutManager() { | |
| 527 const int related_x = kRelatedControlHorizontalSpacing; | |
| 528 const int related_y = kRelatedControlVerticalSpacing; | |
| 529 const int unrelated_y = kUnrelatedControlVerticalSpacing; | |
| 530 | |
| 531 GridLayout* contents_layout = CreatePanelGridLayout(this); | |
| 532 SetLayoutManager(contents_layout); | |
| 533 | |
| 534 // For the table and buttons. | |
| 535 views::ColumnSet* column_set = contents_layout->AddColumnSet(0); | |
| 536 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, | |
| 537 GridLayout::USE_PREF, 0, 0); | |
| 538 column_set->AddPaddingColumn(0, related_x); | |
| 539 column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0, | |
| 540 GridLayout::USE_PREF, 0, 0); | |
| 541 | |
| 542 contents_layout->StartRow(0, 0); | |
| 543 contents_layout->AddView(table_view_, 1, 8, GridLayout::FILL, | |
| 544 GridLayout::FILL); | |
| 545 contents_layout->AddView(add_button_); | |
| 546 | |
| 547 contents_layout->StartRowWithPadding(0, 0, 0, related_y); | |
| 548 contents_layout->SkipColumns(2); | |
| 549 contents_layout->AddView(edit_button_); | |
| 550 | |
| 551 contents_layout->StartRowWithPadding(0, 0, 0, related_y); | |
| 552 contents_layout->SkipColumns(2); | |
| 553 contents_layout->AddView(remove_button_); | |
| 554 | |
| 555 contents_layout->StartRowWithPadding(0, 0, 0, related_y); | |
| 556 contents_layout->SkipColumns(2); | |
| 557 contents_layout->AddView(make_default_button_); | |
| 558 | |
| 559 contents_layout->AddPaddingRow(1, 0); | |
| 560 } | |
| 561 | |
| 562 void KeywordEditorView::OnSelectionChanged() { | |
| 563 const int selected_row_count = table_view_->SelectedRowCount(); | |
| 564 edit_button_->SetEnabled(selected_row_count == 1); | |
| 565 bool can_make_default = false; | |
| 566 bool can_remove = false; | |
| 567 if (selected_row_count == 1) { | |
| 568 const TemplateURL* selected_url = | |
| 569 &table_model_->GetTemplateURL(table_view_->FirstSelectedRow()); | |
| 570 can_make_default = | |
| 571 (selected_url != url_model_->GetDefaultSearchProvider() && | |
| 572 selected_url->url() && | |
| 573 selected_url->url()->SupportsReplacement()); | |
| 574 can_remove = (selected_url != url_model_->GetDefaultSearchProvider()); | |
| 575 } | |
| 576 remove_button_->SetEnabled(can_remove); | |
| 577 make_default_button_->SetEnabled(can_make_default); | |
| 578 } | |
| 579 | |
| 580 void KeywordEditorView::OnDoubleClick() { | |
| 581 if (edit_button_->IsEnabled()) | |
| 582 ButtonPressed(edit_button_); | |
| 583 } | |
| 584 | |
| 585 void KeywordEditorView::ButtonPressed(views::Button* sender) { | |
| 586 if (sender == add_button_) { | |
| 587 browser::EditSearchEngine(GetWindow()->GetNativeWindow(), NULL, this, | |
| 588 profile_); | |
| 589 } else if (sender == remove_button_) { | |
| 590 DCHECK(table_view_->SelectedRowCount() > 0); | |
| 591 // Remove the observer while we modify the model, that way we don't need to | |
| 592 // worry about the model calling us back when we mutate it. | |
| 593 url_model_->RemoveObserver(this); | |
| 594 int last_view_row = -1; | |
| 595 for (views::TableView::iterator i = table_view_->SelectionBegin(); | |
| 596 i != table_view_->SelectionEnd(); ++i) { | |
| 597 last_view_row = table_view_->model_to_view(*i); | |
| 598 const TemplateURL* template_url = &table_model_->GetTemplateURL(*i); | |
| 599 // Make sure to remove from the table model first, otherwise the | |
| 600 // TemplateURL would be freed. | |
| 601 table_model_->Remove(*i); | |
| 602 url_model_->Remove(template_url); | |
| 603 } | |
| 604 if (last_view_row >= table_model_->RowCount()) | |
| 605 last_view_row = table_model_->RowCount() - 1; | |
| 606 if (last_view_row >= 0) | |
| 607 table_view_->Select(table_view_->view_to_model(last_view_row)); | |
| 608 url_model_->AddObserver(this); | |
| 609 UserMetrics::RecordAction(L"KeywordEditor_RemoveKeyword", profile_); | |
| 610 } else if (sender == edit_button_) { | |
| 611 const int selected_row = table_view_->FirstSelectedRow(); | |
| 612 const TemplateURL* template_url = | |
| 613 &table_model_->GetTemplateURL(selected_row); | |
| 614 browser::EditSearchEngine(GetWindow()->GetNativeWindow(), template_url, | |
| 615 this, profile_); | |
| 616 } else if (sender == make_default_button_) { | |
| 617 MakeDefaultSearchProvider(); | |
| 618 } else { | |
| 619 NOTREACHED(); | |
| 620 } | |
| 621 } | |
| 622 | |
| 623 void KeywordEditorView::OnTemplateURLModelChanged() { | |
| 624 table_model_->Reload(); | |
| 625 add_button_->SetEnabled(url_model_->loaded()); | |
| 626 } | |
| 627 | |
| 628 void KeywordEditorView::MakeDefaultSearchProvider() { | |
| 629 MakeDefaultSearchProvider(table_view_->FirstSelectedRow()); | |
| 630 } | |
| 631 | |
| 632 void KeywordEditorView::MakeDefaultSearchProvider(int index) { | |
| 633 if (index < 0 || index >= table_model_->RowCount()) { | |
| 634 NOTREACHED(); | |
| 635 return; | |
| 636 } | |
| 637 const TemplateURL* keyword = &table_model_->GetTemplateURL(index); | |
| 638 const TemplateURL* current_default = url_model_->GetDefaultSearchProvider(); | |
| 639 if (current_default == keyword) | |
| 640 return; | |
| 641 | |
| 642 url_model_->RemoveObserver(this); | |
| 643 url_model_->SetDefaultSearchProvider(keyword); | |
| 644 url_model_->AddObserver(this); | |
| 645 | |
| 646 // The formatting of the default engine is different; notify the table that | |
| 647 // both old and new entries have changed. | |
| 648 if (current_default != NULL) { | |
| 649 table_model_->NotifyChanged(table_model_->IndexOfTemplateURL( | |
| 650 current_default)); | |
| 651 } | |
| 652 const int new_index = table_model_->IndexOfTemplateURL(keyword); | |
| 653 table_model_->NotifyChanged(new_index); | |
| 654 | |
| 655 // Make sure the new default is in the main group. | |
| 656 table_model_->MoveToMainGroup(index); | |
| 657 | |
| 658 // And select it. | |
| 659 table_view_->Select(table_model_->IndexOfTemplateURL(keyword)); | |
| 660 } | |
| OLD | NEW |