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 "grit/generated_resources.h"
12 const BookmarkNode
* CreateNewNode(BookmarkModel
* model
,
13 const BookmarkNode
* parent
,
14 const BookmarkEditor::EditDetails
& details
,
15 const base::string16
& new_title
,
16 const GURL
& new_url
) {
17 const BookmarkNode
* node
;
18 // When create the new one to right-clicked folder, add it to the next to the
19 // folder's position. Because |details.index| has a index of the folder when
20 // it was right-clicked, it might cause out of range exception when another
21 // bookmark manager edits contents of the folder.
22 // So we must check the range.
23 int child_count
= parent
->child_count();
24 int insert_index
= (parent
== details
.parent_node
&& details
.index
>= 0 &&
25 details
.index
<= child_count
) ?
26 details
.index
: child_count
;
27 if (details
.type
== BookmarkEditor::EditDetails::NEW_URL
) {
28 node
= model
->AddURL(parent
, insert_index
, new_title
, new_url
);
29 } else if (details
.type
== BookmarkEditor::EditDetails::NEW_FOLDER
) {
30 node
= model
->AddFolder(parent
, insert_index
, new_title
);
31 for (size_t i
= 0; i
< details
.urls
.size(); ++i
) {
32 model
->AddURL(node
, node
->child_count(), details
.urls
[i
].second
,
33 details
.urls
[i
].first
);
35 model
->SetDateFolderModified(parent
, base::Time::Now());
46 BookmarkEditor::EditDetails::EditDetails(Type node_type
)
47 : type(node_type
), existing_node(NULL
), parent_node(NULL
), index(-1) {
50 BookmarkNode::Type
BookmarkEditor::EditDetails::GetNodeType() const {
51 BookmarkNode::Type node_type
= BookmarkNode::URL
;
54 node_type
= existing_node
->type();
57 node_type
= BookmarkNode::URL
;
60 node_type
= BookmarkNode::FOLDER
;
68 int BookmarkEditor::EditDetails::GetWindowTitleId() const {
69 int dialog_title
= IDS_BOOKMARK_EDITOR_TITLE
;
71 case EditDetails::EXISTING_NODE
:
72 case EditDetails::NEW_URL
:
73 dialog_title
= (type
== EditDetails::EXISTING_NODE
&&
74 existing_node
->type() == BookmarkNode::FOLDER
) ?
75 IDS_BOOKMARK_FOLDER_EDITOR_WINDOW_TITLE
:
76 IDS_BOOKMARK_EDITOR_TITLE
;
78 case EditDetails::NEW_FOLDER
:
79 dialog_title
= urls
.empty() ?
80 IDS_BOOKMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW
:
81 IDS_BOOKMARK_ALL_TABS_DIALOG_TITLE
;
89 BookmarkEditor::EditDetails
BookmarkEditor::EditDetails::EditNode(
90 const BookmarkNode
* node
) {
91 EditDetails
details(EXISTING_NODE
);
92 details
.existing_node
= node
;
94 details
.parent_node
= node
->parent();
98 BookmarkEditor::EditDetails
BookmarkEditor::EditDetails::AddNodeInFolder(
99 const BookmarkNode
* parent_node
,
102 const string16
& title
) {
103 EditDetails
details(NEW_URL
);
104 details
.parent_node
= parent_node
;
105 details
.index
= index
;
107 details
.title
= title
;
111 BookmarkEditor::EditDetails
BookmarkEditor::EditDetails::AddFolder(
112 const BookmarkNode
* parent_node
,
114 EditDetails
details(NEW_FOLDER
);
115 details
.parent_node
= parent_node
;
116 details
.index
= index
;
120 BookmarkEditor::EditDetails::~EditDetails() {}
123 const BookmarkNode
* BookmarkEditor::ApplyEditsWithNoFolderChange(
124 BookmarkModel
* model
,
125 const BookmarkNode
* parent
,
126 const EditDetails
& details
,
127 const base::string16
& new_title
,
128 const GURL
& new_url
) {
129 if (details
.type
== EditDetails::NEW_URL
||
130 details
.type
== EditDetails::NEW_FOLDER
) {
131 return CreateNewNode(model
, parent
, details
, new_title
, new_url
);
134 const BookmarkNode
* node
= details
.existing_node
;
138 model
->SetURL(node
, new_url
);
139 model
->SetTitle(node
, new_title
);
145 const BookmarkNode
* BookmarkEditor::ApplyEditsWithPossibleFolderChange(
146 BookmarkModel
* model
,
147 const BookmarkNode
* new_parent
,
148 const EditDetails
& details
,
149 const base::string16
& new_title
,
150 const GURL
& new_url
) {
151 if (details
.type
== EditDetails::NEW_URL
||
152 details
.type
== EditDetails::NEW_FOLDER
) {
153 return CreateNewNode(model
, new_parent
, details
, new_title
, new_url
);
156 const BookmarkNode
* node
= details
.existing_node
;
159 if (new_parent
!= node
->parent())
160 model
->Move(node
, new_parent
, new_parent
->child_count());
162 model
->SetURL(node
, new_url
);
163 model
->SetTitle(node
, new_title
);