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
;
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());
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
;
58 node_type
= existing_node
->type();
61 node_type
= BookmarkNode::URL
;
64 node_type
= BookmarkNode::FOLDER
;
72 int BookmarkEditor::EditDetails::GetWindowTitleId() const {
73 int dialog_title
= IDS_BOOKMARK_EDITOR_TITLE
;
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
;
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
;
93 BookmarkEditor::EditDetails
BookmarkEditor::EditDetails::EditNode(
94 const BookmarkNode
* node
) {
95 EditDetails
details(EXISTING_NODE
);
96 details
.existing_node
= node
;
98 details
.parent_node
= node
->parent();
102 BookmarkEditor::EditDetails
BookmarkEditor::EditDetails::AddNodeInFolder(
103 const BookmarkNode
* parent_node
,
106 const base::string16
& title
) {
107 EditDetails
details(NEW_URL
);
108 details
.parent_node
= parent_node
;
109 details
.index
= index
;
111 details
.title
= title
;
115 BookmarkEditor::EditDetails
BookmarkEditor::EditDetails::AddFolder(
116 const BookmarkNode
* parent_node
,
118 EditDetails
details(NEW_FOLDER
);
119 details
.parent_node
= parent_node
;
120 details
.index
= index
;
124 BookmarkEditor::EditDetails::~EditDetails() {}
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
;
142 model
->SetURL(node
, new_url
);
143 model
->SetTitle(node
, new_title
);
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
;
163 if (new_parent
!= node
->parent())
164 model
->Move(node
, new_parent
, new_parent
->child_count());
166 model
->SetURL(node
, new_url
);
167 model
->SetTitle(node
, new_title
);