BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / chrome / browser / ui / bookmarks / bookmark_editor.cc
blob5ce450ff3aaf767ee468423b65924ca7dd4c8dca
1 // Copyright 2013 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.
5 #include "chrome/browser/ui/bookmarks/bookmark_editor.h"
7 #include "base/logging.h"
8 #include "chrome/grit/generated_resources.h"
9 #include "components/bookmarks/browser/bookmark_model.h"
11 using bookmarks::BookmarkModel;
12 using bookmarks::BookmarkNode;
14 namespace {
16 const BookmarkNode* CreateNewNode(BookmarkModel* model,
17 const BookmarkNode* parent,
18 const BookmarkEditor::EditDetails& details,
19 const base::string16& new_title,
20 const GURL& new_url) {
21 const BookmarkNode* node;
22 // When create the new one to right-clicked folder, add it to the next to the
23 // folder's position. Because |details.index| has a index of the folder when
24 // it was right-clicked, it might cause out of range exception when another
25 // bookmark manager edits contents of the folder.
26 // So we must check the range.
27 int child_count = parent->child_count();
28 int insert_index = (parent == details.parent_node && details.index >= 0 &&
29 details.index <= child_count) ?
30 details.index : child_count;
31 if (details.type == BookmarkEditor::EditDetails::NEW_URL) {
32 node = model->AddURL(parent, insert_index, new_title, new_url);
33 } else if (details.type == BookmarkEditor::EditDetails::NEW_FOLDER) {
34 node = model->AddFolder(parent, insert_index, new_title);
35 for (size_t i = 0; i < details.urls.size(); ++i) {
36 model->AddURL(node, node->child_count(), details.urls[i].second,
37 details.urls[i].first);
39 model->SetDateFolderModified(parent, base::Time::Now());
40 } else {
41 NOTREACHED();
42 return NULL;
45 return node;
48 } // namespace
50 BookmarkEditor::EditDetails::EditDetails(Type node_type)
51 : type(node_type), existing_node(NULL), parent_node(NULL), index(-1) {
54 BookmarkNode::Type BookmarkEditor::EditDetails::GetNodeType() const {
55 BookmarkNode::Type node_type = BookmarkNode::URL;
56 switch (type) {
57 case EXISTING_NODE:
58 node_type = existing_node->type();
59 break;
60 case NEW_URL:
61 node_type = BookmarkNode::URL;
62 break;
63 case NEW_FOLDER:
64 node_type = BookmarkNode::FOLDER;
65 break;
66 default:
67 NOTREACHED();
69 return node_type;
72 int BookmarkEditor::EditDetails::GetWindowTitleId() const {
73 int dialog_title = IDS_BOOKMARK_EDITOR_TITLE;
74 switch (type) {
75 case EditDetails::EXISTING_NODE:
76 case EditDetails::NEW_URL:
77 dialog_title = (type == EditDetails::EXISTING_NODE &&
78 existing_node->type() == BookmarkNode::FOLDER) ?
79 IDS_BOOKMARK_FOLDER_EDITOR_WINDOW_TITLE :
80 IDS_BOOKMARK_EDITOR_TITLE;
81 break;
82 case EditDetails::NEW_FOLDER:
83 dialog_title = urls.empty() ?
84 IDS_BOOKMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW :
85 IDS_BOOKMARK_ALL_TABS_DIALOG_TITLE;
86 break;
87 default:
88 NOTREACHED();
90 return dialog_title;
93 BookmarkEditor::EditDetails BookmarkEditor::EditDetails::EditNode(
94 const BookmarkNode* node) {
95 EditDetails details(EXISTING_NODE);
96 details.existing_node = node;
97 if (node)
98 details.parent_node = node->parent();
99 return details;
102 BookmarkEditor::EditDetails BookmarkEditor::EditDetails::AddNodeInFolder(
103 const BookmarkNode* parent_node,
104 int index,
105 const GURL& url,
106 const base::string16& title) {
107 EditDetails details(NEW_URL);
108 details.parent_node = parent_node;
109 details.index = index;
110 details.url = url;
111 details.title = title;
112 return details;
115 BookmarkEditor::EditDetails BookmarkEditor::EditDetails::AddFolder(
116 const BookmarkNode* parent_node,
117 int index) {
118 EditDetails details(NEW_FOLDER);
119 details.parent_node = parent_node;
120 details.index = index;
121 return details;
124 BookmarkEditor::EditDetails::~EditDetails() {}
126 // static
127 const BookmarkNode* BookmarkEditor::ApplyEditsWithNoFolderChange(
128 BookmarkModel* model,
129 const BookmarkNode* parent,
130 const EditDetails& details,
131 const base::string16& new_title,
132 const GURL& new_url) {
133 if (details.type == EditDetails::NEW_URL ||
134 details.type == EditDetails::NEW_FOLDER) {
135 return CreateNewNode(model, parent, details, new_title, new_url);
138 const BookmarkNode* node = details.existing_node;
139 DCHECK(node);
141 if (node->is_url())
142 model->SetURL(node, new_url);
143 model->SetTitle(node, new_title);
145 return node;
148 // static
149 const BookmarkNode* BookmarkEditor::ApplyEditsWithPossibleFolderChange(
150 BookmarkModel* model,
151 const BookmarkNode* new_parent,
152 const EditDetails& details,
153 const base::string16& new_title,
154 const GURL& new_url) {
155 if (details.type == EditDetails::NEW_URL ||
156 details.type == EditDetails::NEW_FOLDER) {
157 return CreateNewNode(model, new_parent, details, new_title, new_url);
160 const BookmarkNode* node = details.existing_node;
161 DCHECK(node);
163 if (new_parent != node->parent())
164 model->Move(node, new_parent, new_parent->child_count());
165 if (node->is_url())
166 model->SetURL(node, new_url);
167 model->SetTitle(node, new_title);
169 return node;