| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/bookmark_editor_view.h" | 5 #include "chrome/browser/views/bookmark_editor_view.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 // Preferred width of the tree. | 42 // Preferred width of the tree. |
| 43 static const int kTreeWidth = 300; | 43 static const int kTreeWidth = 300; |
| 44 | 44 |
| 45 // ID for various children. | 45 // ID for various children. |
| 46 static const int kNewGroupButtonID = 1002; | 46 static const int kNewGroupButtonID = 1002; |
| 47 | 47 |
| 48 // static | 48 // static |
| 49 void BookmarkEditor::Show(HWND parent_hwnd, | 49 void BookmarkEditor::Show(HWND parent_hwnd, |
| 50 Profile* profile, | 50 Profile* profile, |
| 51 const BookmarkNode* parent, | 51 const BookmarkNode* parent, |
| 52 const BookmarkNode* node, | 52 const EditDetails& details, |
| 53 Configuration configuration, | 53 Configuration configuration, |
| 54 Handler* handler) { | 54 Handler* handler) { |
| 55 DCHECK(profile); | 55 DCHECK(profile); |
| 56 BookmarkEditorView* editor = | 56 BookmarkEditorView* editor = |
| 57 new BookmarkEditorView(profile, parent, node, configuration, handler); | 57 new BookmarkEditorView(profile, parent, details, configuration, handler); |
| 58 editor->Show(parent_hwnd); | 58 editor->Show(parent_hwnd); |
| 59 } | 59 } |
| 60 | 60 |
| 61 BookmarkEditorView::BookmarkEditorView( | 61 BookmarkEditorView::BookmarkEditorView( |
| 62 Profile* profile, | 62 Profile* profile, |
| 63 const BookmarkNode* parent, | 63 const BookmarkNode* parent, |
| 64 const BookmarkNode* node, | 64 const EditDetails& details, |
| 65 BookmarkEditor::Configuration configuration, | 65 BookmarkEditor::Configuration configuration, |
| 66 BookmarkEditor::Handler* handler) | 66 BookmarkEditor::Handler* handler) |
| 67 : profile_(profile), | 67 : profile_(profile), |
| 68 tree_view_(NULL), | 68 tree_view_(NULL), |
| 69 new_group_button_(NULL), | 69 new_group_button_(NULL), |
| 70 parent_(parent), | 70 parent_(parent), |
| 71 node_(node), | 71 details_(details), |
| 72 running_menu_for_root_(false), | 72 running_menu_for_root_(false), |
| 73 show_tree_(configuration == SHOW_TREE), | 73 show_tree_(configuration == SHOW_TREE), |
| 74 handler_(handler) { | 74 handler_(handler) { |
| 75 DCHECK(profile); | 75 DCHECK(profile); |
| 76 Init(); | 76 Init(); |
| 77 } | 77 } |
| 78 | 78 |
| 79 BookmarkEditorView::~BookmarkEditorView() { | 79 BookmarkEditorView::~BookmarkEditorView() { |
| 80 // The tree model is deleted before the view. Reset the model otherwise the | 80 // The tree model is deleted before the view. Reset the model otherwise the |
| 81 // tree will reference a deleted model. | 81 // tree will reference a deleted model. |
| 82 if (tree_view_) | 82 if (tree_view_) |
| 83 tree_view_->SetModel(NULL); | 83 tree_view_->SetModel(NULL); |
| 84 bb_model_->RemoveObserver(this); | 84 bb_model_->RemoveObserver(this); |
| 85 } | 85 } |
| 86 | 86 |
| 87 bool BookmarkEditorView::IsDialogButtonEnabled( | 87 bool BookmarkEditorView::IsDialogButtonEnabled( |
| 88 MessageBoxFlags::DialogButton button) const { | 88 MessageBoxFlags::DialogButton button) const { |
| 89 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { | 89 if (button == MessageBoxFlags::DIALOGBUTTON_OK) { |
| 90 if (IsEditingFolder()) | 90 if (details_.type == EditDetails::NEW_FOLDER) |
| 91 return !title_tf_.text().empty(); | 91 return !title_tf_.text().empty(); |
| 92 | 92 |
| 93 const GURL url(GetInputURL()); | 93 const GURL url(GetInputURL()); |
| 94 return bb_model_->IsLoaded() && url.is_valid(); | 94 return bb_model_->IsLoaded() && url.is_valid(); |
| 95 } | 95 } |
| 96 return true; | 96 return true; |
| 97 } | 97 } |
| 98 | 98 |
| 99 bool BookmarkEditorView::IsModal() const { | 99 bool BookmarkEditorView::IsModal() const { |
| 100 return true; | 100 return true; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 } | 256 } |
| 257 | 257 |
| 258 void BookmarkEditorView::Init() { | 258 void BookmarkEditorView::Init() { |
| 259 bb_model_ = profile_->GetBookmarkModel(); | 259 bb_model_ = profile_->GetBookmarkModel(); |
| 260 DCHECK(bb_model_); | 260 DCHECK(bb_model_); |
| 261 bb_model_->AddObserver(this); | 261 bb_model_->AddObserver(this); |
| 262 | 262 |
| 263 url_tf_.SetParentOwned(false); | 263 url_tf_.SetParentOwned(false); |
| 264 title_tf_.SetParentOwned(false); | 264 title_tf_.SetParentOwned(false); |
| 265 | 265 |
| 266 title_tf_.SetText(node_ ? node_->GetTitle() : std::wstring()); | 266 std::wstring title; |
| 267 if (details_.type == EditDetails::EXISTING_NODE) |
| 268 title = details_.existing_node->GetTitle(); |
| 269 else if (details_.type == EditDetails::NEW_FOLDER) |
| 270 title = l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME); |
| 271 title_tf_.SetText(title); |
| 267 title_tf_.SetController(this); | 272 title_tf_.SetController(this); |
| 268 | 273 |
| 269 std::wstring url_text; | 274 std::wstring url_text; |
| 270 if (node_ && !IsEditingFolder()) { | 275 if (details_.type == EditDetails::EXISTING_NODE) { |
| 271 std::wstring languages = profile_ | 276 std::wstring languages = profile_ |
| 272 ? profile_->GetPrefs()->GetString(prefs::kAcceptLanguages) | 277 ? profile_->GetPrefs()->GetString(prefs::kAcceptLanguages) |
| 273 : std::wstring(); | 278 : std::wstring(); |
| 274 // The following URL is user-editable. We specify omit_username_password= | 279 // The following URL is user-editable. We specify omit_username_password= |
| 275 // false and unescape=false to show the original URL except IDN. | 280 // false and unescape=false to show the original URL except IDN. |
| 276 url_text = | 281 url_text = |
| 277 net::FormatUrl(node_->GetURL(), languages, false, UnescapeRule::NONE, | 282 net::FormatUrl(details_.existing_node->GetURL(), languages, false, |
| 278 NULL, NULL); | 283 UnescapeRule::NONE, NULL, NULL); |
| 279 } | 284 } |
| 280 url_tf_.SetText(url_text); | 285 url_tf_.SetText(url_text); |
| 281 url_tf_.SetController(this); | 286 url_tf_.SetController(this); |
| 282 | 287 |
| 283 if (show_tree_) { | 288 if (show_tree_) { |
| 284 tree_view_ = new views::TreeView(); | 289 tree_view_ = new views::TreeView(); |
| 285 new_group_button_.reset(new views::NativeButton( | 290 new_group_button_.reset(new views::NativeButton( |
| 286 this, l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_BUTTON))); | 291 this, l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_BUTTON))); |
| 287 new_group_button_->SetParentOwned(false); | 292 new_group_button_->SetParentOwned(false); |
| 288 tree_view_->SetContextMenuController(this); | 293 tree_view_->SetContextMenuController(this); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); | 325 column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing); |
| 321 column_set->AddColumn(GridLayout::FILL, GridLayout::LEADING, 0, | 326 column_set->AddColumn(GridLayout::FILL, GridLayout::LEADING, 0, |
| 322 GridLayout::USE_PREF, 0, 0); | 327 GridLayout::USE_PREF, 0, 0); |
| 323 column_set->LinkColumnSizes(0, 2, 4, -1); | 328 column_set->LinkColumnSizes(0, 2, 4, -1); |
| 324 | 329 |
| 325 layout->StartRow(0, labels_column_set_id); | 330 layout->StartRow(0, labels_column_set_id); |
| 326 layout->AddView( | 331 layout->AddView( |
| 327 new Label(l10n_util::GetString(IDS_BOOMARK_EDITOR_NAME_LABEL))); | 332 new Label(l10n_util::GetString(IDS_BOOMARK_EDITOR_NAME_LABEL))); |
| 328 layout->AddView(&title_tf_); | 333 layout->AddView(&title_tf_); |
| 329 | 334 |
| 330 if (!IsEditingFolder()) { | 335 if (details_.type != EditDetails::NEW_FOLDER) { |
| 331 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | 336 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); |
| 332 | 337 |
| 333 layout->StartRow(0, labels_column_set_id); | 338 layout->StartRow(0, labels_column_set_id); |
| 334 layout->AddView( | 339 layout->AddView( |
| 335 new Label(l10n_util::GetString(IDS_BOOMARK_EDITOR_URL_LABEL))); | 340 new Label(l10n_util::GetString(IDS_BOOMARK_EDITOR_URL_LABEL))); |
| 336 layout->AddView(&url_tf_); | 341 layout->AddView(&url_tf_); |
| 337 } | 342 } |
| 338 | 343 |
| 339 if (show_tree_) { | 344 if (show_tree_) { |
| 340 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); | 345 layout->AddPaddingRow(0, kRelatedControlVerticalSpacing); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 359 void BookmarkEditorView::BookmarkNodeAdded(BookmarkModel* model, | 364 void BookmarkEditorView::BookmarkNodeAdded(BookmarkModel* model, |
| 360 const BookmarkNode* parent, | 365 const BookmarkNode* parent, |
| 361 int index) { | 366 int index) { |
| 362 Reset(); | 367 Reset(); |
| 363 } | 368 } |
| 364 | 369 |
| 365 void BookmarkEditorView::BookmarkNodeRemoved(BookmarkModel* model, | 370 void BookmarkEditorView::BookmarkNodeRemoved(BookmarkModel* model, |
| 366 const BookmarkNode* parent, | 371 const BookmarkNode* parent, |
| 367 int index, | 372 int index, |
| 368 const BookmarkNode* node) { | 373 const BookmarkNode* node) { |
| 369 if ((node_ && node_->HasAncestor(node)) || | 374 if ((details_.type == EditDetails::EXISTING_NODE && |
| 375 details_.existing_node->HasAncestor(node)) || |
| 370 (parent_ && parent_->HasAncestor(node))) { | 376 (parent_ && parent_->HasAncestor(node))) { |
| 371 // The node, or its parent was removed. Close the dialog. | 377 // The node, or its parent was removed. Close the dialog. |
| 372 window()->Close(); | 378 window()->Close(); |
| 373 } else { | 379 } else { |
| 374 Reset(); | 380 Reset(); |
| 375 } | 381 } |
| 376 } | 382 } |
| 377 | 383 |
| 378 void BookmarkEditorView::BookmarkNodeChildrenReordered( | 384 void BookmarkEditorView::BookmarkNodeChildrenReordered( |
| 379 BookmarkModel* model, const BookmarkNode* node) { | 385 BookmarkModel* model, const BookmarkNode* node) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 BookmarkEditorView::EditorNode* BookmarkEditorView::AddNewGroup( | 446 BookmarkEditorView::EditorNode* BookmarkEditorView::AddNewGroup( |
| 441 EditorNode* parent) { | 447 EditorNode* parent) { |
| 442 EditorNode* new_node = new EditorNode(); | 448 EditorNode* new_node = new EditorNode(); |
| 443 new_node->SetTitle(l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME)); | 449 new_node->SetTitle(l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME)); |
| 444 new_node->value = 0; | 450 new_node->value = 0; |
| 445 // new_node is now owned by parent. | 451 // new_node is now owned by parent. |
| 446 tree_model_->Add(parent, parent->GetChildCount(), new_node); | 452 tree_model_->Add(parent, parent->GetChildCount(), new_node); |
| 447 return new_node; | 453 return new_node; |
| 448 } | 454 } |
| 449 | 455 |
| 450 bool BookmarkEditorView::IsEditingFolder() const { | |
| 451 return node_ && node_->is_folder(); | |
| 452 } | |
| 453 | |
| 454 void BookmarkEditorView::ExpandAndSelect() { | 456 void BookmarkEditorView::ExpandAndSelect() { |
| 455 tree_view_->ExpandAll(); | 457 tree_view_->ExpandAll(); |
| 456 | 458 |
| 457 const BookmarkNode* to_select = node_ ? node_->GetParent() : parent_; | 459 const BookmarkNode* to_select = parent_; |
| 460 if (details_.type == EditDetails::EXISTING_NODE) |
| 461 to_select = details_.existing_node->GetParent(); |
| 458 int64 group_id_to_select = to_select->id(); | 462 int64 group_id_to_select = to_select->id(); |
| 459 EditorNode* b_node = | 463 EditorNode* b_node = |
| 460 FindNodeWithID(tree_model_->GetRoot(), group_id_to_select); | 464 FindNodeWithID(tree_model_->GetRoot(), group_id_to_select); |
| 461 if (!b_node) | 465 if (!b_node) |
| 462 b_node = tree_model_->GetRoot()->GetChild(0); // Bookmark bar node. | 466 b_node = tree_model_->GetRoot()->GetChild(0); // Bookmark bar node. |
| 463 | 467 |
| 464 tree_view_->SetSelectedNode(b_node); | 468 tree_view_->SetSelectedNode(b_node); |
| 465 } | 469 } |
| 466 | 470 |
| 467 BookmarkEditorView::EditorNode* BookmarkEditorView::CreateRootNode() { | 471 BookmarkEditorView::EditorNode* BookmarkEditorView::CreateRootNode() { |
| 468 EditorNode* root_node = new EditorNode(std::wstring(), 0); | 472 EditorNode* root_node = new EditorNode(std::wstring(), 0); |
| 469 const BookmarkNode* bb_root_node = bb_model_->root_node(); | 473 const BookmarkNode* bb_root_node = bb_model_->root_node(); |
| 470 CreateNodes(bb_root_node, root_node); | 474 CreateNodes(bb_root_node, root_node); |
| 471 DCHECK(root_node->GetChildCount() == 2); | 475 DCHECK(root_node->GetChildCount() == 2); |
| 472 DCHECK(bb_root_node->GetChild(0)->GetType() == BookmarkNode::BOOKMARK_BAR); | 476 DCHECK(bb_root_node->GetChild(0)->GetType() == BookmarkNode::BOOKMARK_BAR); |
| 473 DCHECK(bb_root_node->GetChild(1)->GetType() == BookmarkNode::OTHER_NODE); | 477 DCHECK(bb_root_node->GetChild(1)->GetType() == BookmarkNode::OTHER_NODE); |
| 474 return root_node; | 478 return root_node; |
| 475 } | 479 } |
| 476 | 480 |
| 477 void BookmarkEditorView::CreateNodes(const BookmarkNode* bb_node, | 481 void BookmarkEditorView::CreateNodes(const BookmarkNode* bb_node, |
| 478 BookmarkEditorView::EditorNode* b_node) { | 482 BookmarkEditorView::EditorNode* b_node) { |
| 479 for (int i = 0; i < bb_node->GetChildCount(); ++i) { | 483 for (int i = 0; i < bb_node->GetChildCount(); ++i) { |
| 480 const BookmarkNode* child_bb_node = bb_node->GetChild(i); | 484 const BookmarkNode* child_bb_node = bb_node->GetChild(i); |
| 481 if (child_bb_node->is_folder() && | 485 if (child_bb_node->is_folder()) { |
| 482 (!IsEditingFolder() || child_bb_node != node_)) { | |
| 483 EditorNode* new_b_node = new EditorNode(child_bb_node->GetTitle(), | 486 EditorNode* new_b_node = new EditorNode(child_bb_node->GetTitle(), |
| 484 child_bb_node->id()); | 487 child_bb_node->id()); |
| 485 b_node->Add(b_node->GetChildCount(), new_b_node); | 488 b_node->Add(b_node->GetChildCount(), new_b_node); |
| 486 CreateNodes(child_bb_node, new_b_node); | 489 CreateNodes(child_bb_node, new_b_node); |
| 487 } | 490 } |
| 488 } | 491 } |
| 489 } | 492 } |
| 490 | 493 |
| 491 BookmarkEditorView::EditorNode* BookmarkEditorView::FindNodeWithID( | 494 BookmarkEditorView::EditorNode* BookmarkEditorView::FindNodeWithID( |
| 492 BookmarkEditorView::EditorNode* node, | 495 BookmarkEditorView::EditorNode* node, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 519 // We're going to apply edits to the bookmark bar model, which will call us | 522 // We're going to apply edits to the bookmark bar model, which will call us |
| 520 // back. Normally when a structural edit occurs we reset the tree model. | 523 // back. Normally when a structural edit occurs we reset the tree model. |
| 521 // We don't want to do that here, so we remove ourselves as an observer. | 524 // We don't want to do that here, so we remove ourselves as an observer. |
| 522 bb_model_->RemoveObserver(this); | 525 bb_model_->RemoveObserver(this); |
| 523 | 526 |
| 524 GURL new_url(GetInputURL()); | 527 GURL new_url(GetInputURL()); |
| 525 std::wstring new_title(GetInputTitle()); | 528 std::wstring new_title(GetInputTitle()); |
| 526 | 529 |
| 527 if (!show_tree_) { | 530 if (!show_tree_) { |
| 528 bookmark_utils::ApplyEditsWithNoGroupChange( | 531 bookmark_utils::ApplyEditsWithNoGroupChange( |
| 529 bb_model_, parent_, node_, new_title, new_url, handler_.get()); | 532 bb_model_, parent_, details_, new_title, new_url, handler_.get()); |
| 530 return; | 533 return; |
| 531 } | 534 } |
| 532 | 535 |
| 533 // Create the new groups and update the titles. | 536 // Create the new groups and update the titles. |
| 534 const BookmarkNode* new_parent = NULL; | 537 const BookmarkNode* new_parent = NULL; |
| 535 ApplyNameChangesAndCreateNewGroups( | 538 ApplyNameChangesAndCreateNewGroups( |
| 536 bb_model_->root_node(), tree_model_->GetRoot(), parent, &new_parent); | 539 bb_model_->root_node(), tree_model_->GetRoot(), parent, &new_parent); |
| 537 | 540 |
| 538 bookmark_utils::ApplyEditsWithPossibleGroupChange( | 541 bookmark_utils::ApplyEditsWithPossibleGroupChange( |
| 539 bb_model_, new_parent, node_, new_title, new_url, handler_.get()); | 542 bb_model_, new_parent, details_, new_title, new_url, handler_.get()); |
| 540 } | 543 } |
| 541 | 544 |
| 542 void BookmarkEditorView::ApplyNameChangesAndCreateNewGroups( | 545 void BookmarkEditorView::ApplyNameChangesAndCreateNewGroups( |
| 543 const BookmarkNode* bb_node, | 546 const BookmarkNode* bb_node, |
| 544 BookmarkEditorView::EditorNode* b_node, | 547 BookmarkEditorView::EditorNode* b_node, |
| 545 BookmarkEditorView::EditorNode* parent_b_node, | 548 BookmarkEditorView::EditorNode* parent_b_node, |
| 546 const BookmarkNode** parent_bb_node) { | 549 const BookmarkNode** parent_bb_node) { |
| 547 if (parent_b_node == b_node) | 550 if (parent_b_node == b_node) |
| 548 *parent_bb_node = bb_node; | 551 *parent_bb_node = bb_node; |
| 549 for (int i = 0; i < b_node->GetChildCount(); ++i) { | 552 for (int i = 0; i < b_node->GetChildCount(); ++i) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 563 break; | 566 break; |
| 564 } | 567 } |
| 565 } | 568 } |
| 566 DCHECK(child_bb_node); | 569 DCHECK(child_bb_node); |
| 567 bb_model_->SetTitle(child_bb_node, child_b_node->GetTitle()); | 570 bb_model_->SetTitle(child_bb_node, child_b_node->GetTitle()); |
| 568 } | 571 } |
| 569 ApplyNameChangesAndCreateNewGroups(child_bb_node, child_b_node, | 572 ApplyNameChangesAndCreateNewGroups(child_bb_node, child_b_node, |
| 570 parent_b_node, parent_bb_node); | 573 parent_b_node, parent_bb_node); |
| 571 } | 574 } |
| 572 } | 575 } |
| OLD | NEW |