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/bookmark_bar_context_menu_controller.h" | 5 #include "chrome/browser/bookmark_bar_context_menu_controller.h" |
6 | 6 |
7 #include "chrome/browser/bookmarks/bookmark_model.h" | 7 #include "chrome/browser/bookmarks/bookmark_model.h" |
8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
10 #include "chrome/browser/page_navigator.h" | 10 #include "chrome/browser/page_navigator.h" |
11 #include "chrome/browser/profile.h" | 11 #include "chrome/browser/profile.h" |
12 #include "chrome/browser/tab_contents.h" | 12 #include "chrome/browser/tab_contents.h" |
13 #include "chrome/browser/user_metrics.h" | 13 #include "chrome/browser/user_metrics.h" |
14 #include "chrome/browser/views/bookmark_editor_view.h" | 14 #include "chrome/browser/views/bookmark_editor_view.h" |
15 #include "chrome/browser/views/input_window.h" | 15 #include "chrome/browser/views/input_window.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "chrome/common/pref_service.h" | 17 #include "chrome/common/pref_service.h" |
18 #include "chrome/views/view_container.h" | 18 #include "chrome/views/view_container.h" |
19 #include "chrome/views/window.h" | 19 #include "chrome/views/window.h" |
20 | 20 |
| 21 #include "chromium_strings.h" |
21 #include "generated_resources.h" | 22 #include "generated_resources.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
| 26 // Number of bookmarks we'll open before prompting the user to see if they |
| 27 // really want to open all. |
| 28 const int kNumURLsBeforePrompting = 15; |
| 29 |
25 // Returns true if the specified node is of type URL, or has a descendant | 30 // Returns true if the specified node is of type URL, or has a descendant |
26 // of type URL. | 31 // of type URL. |
27 bool NodeHasURLs(BookmarkNode* node) { | 32 bool NodeHasURLs(BookmarkNode* node) { |
28 if (node->GetType() == history::StarredEntry::URL) | 33 if (node->GetType() == history::StarredEntry::URL) |
29 return true; | 34 return true; |
30 | 35 |
31 for (int i = 0; i < node->GetChildCount(); ++i) { | 36 for (int i = 0; i < node->GetChildCount(); ++i) { |
32 if (NodeHasURLs(node->GetChild(i))) | 37 if (NodeHasURLs(node->GetChild(i))) |
33 return true; | 38 return true; |
34 } | 39 } |
35 return false; | 40 return false; |
36 } | 41 } |
37 | 42 |
38 // Opens a tab/window for node and recursively opens all descendants. | 43 // Implementation of OpenAll. Opens all nodes of type URL and recurses for |
39 // If open_first_in_new_window is true, the first opened node is opened | 44 // groups. |
40 // in a new window. navigator indicates the PageNavigator to use for | 45 void OpenAll0(BookmarkNode* node, |
41 // new tabs. It is reset if open_first_in_new_window is true. | 46 WindowOpenDisposition initial_disposition, |
42 // opened_url is set to true the first time a new tab is opened. | 47 PageNavigator** navigator, |
43 void OpenAll(BookmarkNode* node, | 48 bool* opened_url) { |
44 bool open_first_in_new_window, | |
45 PageNavigator** navigator, | |
46 bool* opened_url) { | |
47 if (node->GetType() == history::StarredEntry::URL) { | 49 if (node->GetType() == history::StarredEntry::URL) { |
48 WindowOpenDisposition disposition; | 50 WindowOpenDisposition disposition; |
49 if (*opened_url) | 51 if (*opened_url) |
50 disposition = NEW_BACKGROUND_TAB; | 52 disposition = NEW_BACKGROUND_TAB; |
51 else if (open_first_in_new_window) | 53 else |
52 disposition = NEW_WINDOW; | 54 disposition = initial_disposition; |
53 else // Open in current window. | |
54 disposition = CURRENT_TAB; | |
55 (*navigator)->OpenURL(node->GetURL(), disposition, | 55 (*navigator)->OpenURL(node->GetURL(), disposition, |
56 PageTransition::AUTO_BOOKMARK); | 56 PageTransition::AUTO_BOOKMARK); |
57 if (!*opened_url) { | 57 if (!*opened_url) { |
58 *opened_url = true; | 58 *opened_url = true; |
59 if (open_first_in_new_window || disposition == CURRENT_TAB) { | 59 // We opened the first URL which may have opened a new window or clobbered |
60 // We opened the tab in a new window or in the current tab which | 60 // the current page, reset the navigator just to be sure. |
61 // likely reset the navigator. Need to reset the page navigator | 61 Browser* new_browser = BrowserList::GetLastActive(); |
62 // appropriately. | 62 if (new_browser) { |
63 Browser* new_browser = BrowserList::GetLastActive(); | 63 TabContents* current_tab = new_browser->GetSelectedTabContents(); |
64 if (new_browser) { | 64 DCHECK(new_browser && current_tab); |
65 TabContents* current_tab = new_browser->GetSelectedTabContents(); | 65 if (new_browser && current_tab) |
66 DCHECK(new_browser && current_tab); | 66 *navigator = current_tab; |
67 if (new_browser && current_tab) | 67 } // else, new_browser == NULL, which happens during testing. |
68 *navigator = current_tab; | |
69 } // else, new_browser == NULL, which happens during testing. | |
70 } | |
71 } | 68 } |
72 } else { | 69 } else { |
73 // Group, recurse through children. | 70 // Group, recurse through children. |
74 for (int i = 0; i < node->GetChildCount(); ++i) { | 71 for (int i = 0; i < node->GetChildCount(); ++i) |
75 OpenAll(node->GetChild(i), open_first_in_new_window, navigator, | 72 OpenAll0(node->GetChild(i), initial_disposition, navigator, opened_url); |
76 opened_url); | |
77 } | |
78 } | 73 } |
79 } | 74 } |
80 | 75 |
| 76 // Returns the number of descendants of node that are of type url. |
| 77 int DescendantURLCount(BookmarkNode* node) { |
| 78 int result = 0; |
| 79 for (int i = 0; i < node->GetChildCount(); ++i) { |
| 80 BookmarkNode* child = node->GetChild(i); |
| 81 if (child->is_url()) |
| 82 result++; |
| 83 else |
| 84 result += DescendantURLCount(child); |
| 85 } |
| 86 return result; |
| 87 } |
| 88 |
| 89 bool ShouldOpenAll(HWND parent, BookmarkNode* node) { |
| 90 int descendant_count = DescendantURLCount(node); |
| 91 if (descendant_count < kNumURLsBeforePrompting) |
| 92 return true; |
| 93 |
| 94 std::wstring message = |
| 95 l10n_util::GetStringF(IDS_BOOKMARK_BAR_SHOULD_OPEN_ALL, |
| 96 IntToWString(descendant_count)); |
| 97 return (MessageBox(parent, message.c_str(), |
| 98 l10n_util::GetString(IDS_PRODUCT_NAME).c_str(), |
| 99 MB_YESNO | MB_ICONWARNING | MB_TOPMOST) == IDYES); |
| 100 } |
| 101 |
81 // EditFolderController ------------------------------------------------------- | 102 // EditFolderController ------------------------------------------------------- |
82 | 103 |
83 // EditFolderController manages the editing and/or creation of a folder. If the | 104 // EditFolderController manages the editing and/or creation of a folder. If the |
84 // user presses ok, the name change is committed to the database. | 105 // user presses ok, the name change is committed to the database. |
85 // | 106 // |
86 // EditFolderController deletes itself when the window is closed. | 107 // EditFolderController deletes itself when the window is closed. |
87 // | 108 // |
88 class EditFolderController : public InputWindowDelegate, | 109 class EditFolderController : public InputWindowDelegate, |
89 public BookmarkBarView::ModelChangedListener { | 110 public BookmarkBarView::ModelChangedListener { |
90 public: | 111 public: |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 const int BookmarkBarContextMenuController::open_bookmark_in_new_window_id = 3; | 195 const int BookmarkBarContextMenuController::open_bookmark_in_new_window_id = 3; |
175 const int BookmarkBarContextMenuController::open_bookmark_in_new_tab_id = 4; | 196 const int BookmarkBarContextMenuController::open_bookmark_in_new_tab_id = 4; |
176 const int BookmarkBarContextMenuController::open_all_bookmarks_id = 5; | 197 const int BookmarkBarContextMenuController::open_all_bookmarks_id = 5; |
177 const int | 198 const int |
178 BookmarkBarContextMenuController::open_all_bookmarks_in_new_window_id = 6; | 199 BookmarkBarContextMenuController::open_all_bookmarks_in_new_window_id = 6; |
179 const int BookmarkBarContextMenuController::edit_bookmark_id = 7; | 200 const int BookmarkBarContextMenuController::edit_bookmark_id = 7; |
180 const int BookmarkBarContextMenuController::delete_bookmark_id = 8; | 201 const int BookmarkBarContextMenuController::delete_bookmark_id = 8; |
181 const int BookmarkBarContextMenuController::add_bookmark_id = 9; | 202 const int BookmarkBarContextMenuController::add_bookmark_id = 9; |
182 const int BookmarkBarContextMenuController::new_folder_id = 10; | 203 const int BookmarkBarContextMenuController::new_folder_id = 10; |
183 | 204 |
| 205 // static |
| 206 void BookmarkBarContextMenuController::OpenAll( |
| 207 HWND parent, |
| 208 PageNavigator* navigator, |
| 209 BookmarkNode* node, |
| 210 WindowOpenDisposition initial_disposition) { |
| 211 if (!ShouldOpenAll(parent, node)) |
| 212 return; |
| 213 |
| 214 PageNavigator* nav = navigator; |
| 215 bool opened_url = false; |
| 216 OpenAll0(node, initial_disposition, &nav, &opened_url); |
| 217 } |
| 218 |
184 BookmarkBarContextMenuController::BookmarkBarContextMenuController( | 219 BookmarkBarContextMenuController::BookmarkBarContextMenuController( |
185 BookmarkBarView* view, | 220 BookmarkBarView* view, |
186 BookmarkNode* node) | 221 BookmarkNode* node) |
187 : view_(view), | 222 : view_(view), |
188 node_(node), | 223 node_(node), |
189 menu_(this) { | 224 menu_(this) { |
190 if (node->GetType() == history::StarredEntry::URL) { | 225 if (node->GetType() == history::StarredEntry::URL) { |
191 menu_.AppendMenuItemWithLabel( | 226 menu_.AppendMenuItemWithLabel( |
192 open_bookmark_id, | 227 open_bookmark_id, |
193 l10n_util::GetString(IDS_BOOMARK_BAR_OPEN)); | 228 l10n_util::GetString(IDS_BOOMARK_BAR_OPEN)); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 case open_all_bookmarks_id: | 312 case open_all_bookmarks_id: |
278 case open_all_bookmarks_in_new_window_id: { | 313 case open_all_bookmarks_in_new_window_id: { |
279 if (id == open_all_bookmarks_id) { | 314 if (id == open_all_bookmarks_id) { |
280 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_OpenAll", | 315 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_OpenAll", |
281 profile); | 316 profile); |
282 } else { | 317 } else { |
283 UserMetrics::RecordAction( | 318 UserMetrics::RecordAction( |
284 L"BookmarkBar_ContextMenu_OpenAllInNewWindow", profile); | 319 L"BookmarkBar_ContextMenu_OpenAllInNewWindow", profile); |
285 } | 320 } |
286 | 321 |
287 BookmarkNode* node = node_; | 322 WindowOpenDisposition initial_disposition; |
288 PageNavigator* navigator = view_->GetPageNavigator(); | 323 if (id == open_all_bookmarks_in_new_window_id) |
289 bool opened_url = false; | 324 initial_disposition = NEW_WINDOW; |
290 OpenAll(node, (id == open_all_bookmarks_in_new_window_id), &navigator, | 325 else |
291 &opened_url); | 326 initial_disposition = CURRENT_TAB; |
| 327 |
| 328 OpenAll(view_->GetViewContainer()->GetHWND(), view_->GetPageNavigator(), |
| 329 node_, initial_disposition); |
292 break; | 330 break; |
293 } | 331 } |
294 | 332 |
295 case edit_bookmark_id: | 333 case edit_bookmark_id: |
296 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Edit", profile); | 334 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Edit", profile); |
297 | 335 |
298 if (node_->GetType() == history::StarredEntry::URL) { | 336 if (node_->GetType() == history::StarredEntry::URL) { |
299 BookmarkEditorView::Show(view_->GetViewContainer()->GetHWND(), | 337 BookmarkEditorView::Show(view_->GetViewContainer()->GetHWND(), |
300 view_->GetProfile(), NULL, node_); | 338 view_->GetProfile(), NULL, node_); |
301 } else { | 339 } else { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 if (node_->GetType() != history::StarredEntry::URL) { | 403 if (node_->GetType() != history::StarredEntry::URL) { |
366 // Adding to a group always adds to the end. | 404 // Adding to a group always adds to the end. |
367 *visual_order = node_->GetChildCount(); | 405 *visual_order = node_->GetChildCount(); |
368 return node_; | 406 return node_; |
369 } else { | 407 } else { |
370 DCHECK(node_->GetParent()); | 408 DCHECK(node_->GetParent()); |
371 *visual_order = node_->GetParent()->IndexOfChild(node_) + 1; | 409 *visual_order = node_->GetParent()->IndexOfChild(node_) + 1; |
372 return node_->GetParent(); | 410 return node_->GetParent(); |
373 } | 411 } |
374 } | 412 } |
OLD | NEW |