Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: chrome/browser/gtk/bookmark_context_menu.cc

Issue 261013: Renames browser/gtk/bookmark_context_menu to... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2009 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.
4
5 #include "chrome/browser/gtk/bookmark_context_menu.h"
6
7 #include "app/l10n_util.h"
8 #include "base/compiler_specific.h"
9 #include "chrome/browser/bookmarks/bookmark_editor.h"
10 #include "chrome/browser/bookmarks/bookmark_manager.h"
11 #include "chrome/browser/bookmarks/bookmark_model.h"
12 #include "chrome/browser/bookmarks/bookmark_utils.h"
13 #include "chrome/browser/browser.h"
14 #include "chrome/browser/browser_list.h"
15 #include "chrome/browser/input_window_dialog.h"
16 #include "chrome/browser/metrics/user_metrics.h"
17 #include "chrome/browser/profile.h"
18 #include "chrome/browser/tab_contents/page_navigator.h"
19 #include "chrome/browser/tab_contents/tab_contents.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/common/pref_service.h"
22 #include "grit/generated_resources.h"
23
24 // TODO(port): Port these files.
25 #if defined(OS_WIN)
26 #include "views/window/window.h"
27 #endif
28
29 namespace {
30
31 // Returns true if the specified node is of type URL, or has a descendant
32 // of type URL.
33 bool NodeHasURLs(const BookmarkNode* node) {
34 if (node->is_url())
35 return true;
36
37 for (int i = 0; i < node->GetChildCount(); ++i) {
38 if (NodeHasURLs(node->GetChild(i)))
39 return true;
40 }
41 return false;
42 }
43
44 // EditFolderController -------------------------------------------------------
45
46 // EditFolderController manages the editing and/or creation of a folder. If the
47 // user presses ok, the name change is committed to the model.
48 //
49 // EditFolderController deletes itself when the window is closed.
50 class EditFolderController : public InputWindowDialog::Delegate,
51 public BookmarkModelObserver {
52 public:
53 virtual ~EditFolderController() {
54 if (model_)
55 model_->RemoveObserver(this);
56 }
57
58 static void Show(Profile* profile,
59 gfx::NativeView wnd,
60 const BookmarkNode* node,
61 bool is_new,
62 bool show_in_manager) {
63 // EditFolderController deletes itself when done.
64 EditFolderController* controller =
65 new EditFolderController(profile, wnd, node, is_new, show_in_manager);
66 controller->Show();
67 }
68
69 private:
70 EditFolderController(Profile* profile,
71 gfx::NativeView wnd,
72 const BookmarkNode* node,
73 bool is_new,
74 bool show_in_manager)
75 : profile_(profile),
76 model_(profile->GetBookmarkModel()),
77 node_(node),
78 is_new_(is_new),
79 show_in_manager_(show_in_manager) {
80 DCHECK(is_new_ || node);
81
82 std::wstring title = is_new_ ?
83 l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW) :
84 l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE);
85 std::wstring label =
86 l10n_util::GetString(IDS_BOOMARK_BAR_EDIT_FOLDER_LABEL);
87 std::wstring contents = is_new_ ?
88 l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME) :
89 node_->GetTitle();
90
91 dialog_ = InputWindowDialog::Create(wnd, title, label, contents, this);
92 model_->AddObserver(this);
93 }
94
95 void Show() {
96 dialog_->Show();
97 }
98
99 // InputWindowDialog::Delegate methods.
100 virtual bool IsValid(const std::wstring& text) {
101 return !text.empty();
102 }
103
104 virtual void InputAccepted(const std::wstring& text) {
105 if (is_new_) {
106 ALLOW_UNUSED const BookmarkNode* node =
107 model_->AddGroup(node_, node_->GetChildCount(), text);
108 if (show_in_manager_) {
109 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(TOOLKIT_VIEWS))
110 BookmarkManager::SelectInTree(profile_, node);
111 #else
112 NOTIMPLEMENTED() << "BookmarkManager not yet implemented";
113 #endif
114 }
115 } else {
116 model_->SetTitle(node_, text);
117 }
118 }
119
120 virtual void InputCanceled() {
121 }
122
123 // BookmarkModelObserver methods, all invoke ModelChanged and close the
124 // dialog.
125 virtual void Loaded(BookmarkModel* model) {}
126 virtual void BookmarkModelBeingDeleted(BookmarkModel* model) {
127 model_->RemoveObserver(this);
128 model_ = NULL;
129 ModelChanged();
130 }
131
132 virtual void BookmarkNodeMoved(BookmarkModel* model,
133 const BookmarkNode* old_parent,
134 int old_index,
135 const BookmarkNode* new_parent,
136 int new_index) {
137 ModelChanged();
138 }
139
140 virtual void BookmarkNodeAdded(BookmarkModel* model,
141 const BookmarkNode* parent,
142 int index) {
143 ModelChanged();
144 }
145
146 virtual void BookmarkNodeRemoved(BookmarkModel* model,
147 const BookmarkNode* parent,
148 int index,
149 const BookmarkNode* node) {
150 ModelChanged();
151 }
152
153 virtual void BookmarkNodeChanged(BookmarkModel* model,
154 const BookmarkNode* node) {
155 ModelChanged();
156 }
157
158 virtual void BookmarkNodeFavIconLoaded(BookmarkModel* model,
159 const BookmarkNode* node) {}
160
161 virtual void BookmarkNodeChildrenReordered(BookmarkModel* model,
162 const BookmarkNode* node) {
163 ModelChanged();
164 }
165
166 void ModelChanged() {
167 dialog_->Close();
168 }
169
170 Profile* profile_;
171 BookmarkModel* model_;
172 // If is_new is true, this is the parent to create the new node under.
173 // Otherwise this is the node to change the title of.
174 const BookmarkNode* node_;
175
176 bool is_new_;
177
178 // If is_new_ is true and a new node is created, it is selected in the
179 // bookmark manager.
180 bool show_in_manager_;
181 InputWindowDialog* dialog_;
182
183 DISALLOW_COPY_AND_ASSIGN(EditFolderController);
184 };
185
186 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(TOOLKIT_VIEWS))
187 // SelectOnCreationHandler ----------------------------------------------------
188
189 // Used when adding a new bookmark. If a new bookmark is created it is selected
190 // in the bookmark manager.
191 class SelectOnCreationHandler : public BookmarkEditor::Handler {
192 public:
193 explicit SelectOnCreationHandler(Profile* profile) : profile_(profile) {
194 }
195
196 virtual void NodeCreated(const BookmarkNode* new_node) {
197 BookmarkManager::SelectInTree(profile_, new_node);
198 }
199
200 private:
201 Profile* profile_;
202
203 DISALLOW_COPY_AND_ASSIGN(SelectOnCreationHandler);
204 };
205 #endif // #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(TOOLKIT_VIEWS))
206
207 } // namespace
208
209 // BookmarkContextMenu -------------------------------------------
210
211 BookmarkContextMenu::BookmarkContextMenu(
212 gfx::NativeView wnd,
213 Profile* profile,
214 Browser* browser,
215 PageNavigator* navigator,
216 const BookmarkNode* parent,
217 const std::vector<const BookmarkNode*>& selection,
218 ConfigurationType configuration,
219 Delegate* delegate)
220 : wnd_(wnd),
221 profile_(profile),
222 browser_(browser),
223 navigator_(navigator),
224 parent_(parent),
225 selection_(selection),
226 model_(profile->GetBookmarkModel()),
227 configuration_(configuration),
228 delegate_(delegate) {
229 DCHECK(profile_);
230 DCHECK(model_->IsLoaded());
231 CreateMenuObject();
232
233 if (configuration != BOOKMARK_MANAGER_ORGANIZE_MENU) {
234 if (selection.size() == 1 && selection[0]->is_url()) {
235 AppendItem(IDS_BOOMARK_BAR_OPEN_ALL, IDS_BOOMARK_BAR_OPEN_IN_NEW_TAB);
236 AppendItem(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW,
237 IDS_BOOMARK_BAR_OPEN_IN_NEW_WINDOW);
238 AppendItem(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO,
239 IDS_BOOMARK_BAR_OPEN_INCOGNITO);
240 } else {
241 AppendItem(IDS_BOOMARK_BAR_OPEN_ALL, IDS_BOOMARK_BAR_OPEN_ALL);
242 AppendItem(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW,
243 IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW);
244 AppendItem(IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO,
245 IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO);
246 }
247 AppendSeparator();
248 }
249
250 if (selection.size() == 1 && selection[0]->is_folder()) {
251 AppendItem(IDS_BOOKMARK_BAR_RENAME_FOLDER);
252 } else {
253 AppendItem(IDS_BOOKMARK_BAR_EDIT);
254 }
255 AppendItem(IDS_BOOKMARK_BAR_REMOVE);
256
257 if (configuration == BOOKMARK_MANAGER_TABLE ||
258 configuration == BOOKMARK_MANAGER_TABLE_OTHER ||
259 configuration == BOOKMARK_MANAGER_ORGANIZE_MENU ||
260 configuration == BOOKMARK_MANAGER_ORGANIZE_MENU_OTHER) {
261 AppendItem(IDS_BOOKMARK_MANAGER_SHOW_IN_FOLDER);
262 }
263
264 if (configuration == BOOKMARK_MANAGER_TABLE ||
265 configuration == BOOKMARK_MANAGER_TABLE_OTHER ||
266 configuration == BOOKMARK_MANAGER_TREE ||
267 configuration == BOOKMARK_MANAGER_ORGANIZE_MENU ||
268 configuration == BOOKMARK_MANAGER_ORGANIZE_MENU_OTHER) {
269 AppendSeparator();
270 AppendItem(IDS_CUT);
271 AppendItem(IDS_COPY);
272 AppendItem(IDS_PASTE);
273 }
274
275 if (configuration == BOOKMARK_MANAGER_ORGANIZE_MENU) {
276 AppendSeparator();
277 AppendItem(IDS_BOOKMARK_MANAGER_SORT);
278 }
279
280 AppendSeparator();
281
282 AppendItem(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK);
283 AppendItem(IDS_BOOMARK_BAR_NEW_FOLDER);
284
285 if (configuration == BOOKMARK_BAR) {
286 AppendSeparator();
287 AppendItem(IDS_BOOKMARK_MANAGER);
288 AppendCheckboxItem(IDS_BOOMARK_BAR_ALWAYS_SHOW);
289 }
290
291 model_->AddObserver(this);
292 }
293
294 BookmarkContextMenu::~BookmarkContextMenu() {
295 if (model_)
296 model_->RemoveObserver(this);
297 }
298
299 void BookmarkContextMenu::DelegateDestroyed() {
300 delegate_ = NULL;
301 }
302
303 void BookmarkContextMenu::ExecuteCommand(int id) {
304 if (delegate_)
305 delegate_->WillExecuteCommand();
306
307 switch (id) {
308 case IDS_BOOMARK_BAR_OPEN_ALL:
309 case IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO:
310 case IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW: {
311 PageNavigator* navigator = browser_ ?
312 browser_->GetSelectedTabContents() : navigator_;
313 WindowOpenDisposition initial_disposition;
314 if (id == IDS_BOOMARK_BAR_OPEN_ALL) {
315 initial_disposition = NEW_FOREGROUND_TAB;
316 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_OpenAll",
317 profile_);
318 } else if (id == IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW) {
319 initial_disposition = NEW_WINDOW;
320 UserMetrics::RecordAction(
321 L"BookmarkBar_ContextMenu_OpenAllInNewWindow", profile_);
322 } else {
323 initial_disposition = OFF_THE_RECORD;
324 UserMetrics::RecordAction(
325 L"BookmarkBar_ContextMenu_OpenAllIncognito", profile_);
326 }
327
328 bookmark_utils::OpenAll(wnd_, profile_, navigator, selection_,
329 initial_disposition);
330 break;
331 }
332
333 case IDS_BOOKMARK_BAR_RENAME_FOLDER:
334 case IDS_BOOKMARK_BAR_EDIT:
335 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Edit", profile_);
336
337 if (selection_.size() != 1) {
338 NOTREACHED();
339 return;
340 }
341
342 if (selection_[0]->is_url()) {
343 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(TOOLKIT_VIEWS))
344 BookmarkEditor::Configuration editor_config;
345 if (configuration_ == BOOKMARK_BAR)
346 editor_config = BookmarkEditor::SHOW_TREE;
347 else
348 editor_config = BookmarkEditor::NO_TREE;
349 BookmarkEditor::Show(wnd_, profile_, parent_, selection_[0],
350 editor_config, NULL);
351 #else
352 NOTIMPLEMENTED();
353 #endif
354 } else {
355 EditFolderController::Show(profile_, wnd_, selection_[0], false,
356 false);
357 }
358 break;
359
360 case IDS_BOOKMARK_BAR_REMOVE: {
361 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Remove", profile_);
362 BookmarkModel* model = RemoveModelObserver();
363
364 for (size_t i = 0; i < selection_.size(); ++i) {
365 model->Remove(selection_[i]->GetParent(),
366 selection_[i]->GetParent()->IndexOfChild(selection_[i]));
367 }
368 selection_.clear();
369 break;
370 }
371
372 case IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK: {
373 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Add", profile_);
374
375 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(TOOLKIT_VIEWS))
376 BookmarkEditor::Configuration editor_config;
377 BookmarkEditor::Handler* handler = NULL;
378 if (configuration_ == BOOKMARK_BAR) {
379 editor_config = BookmarkEditor::SHOW_TREE;
380 } else {
381 editor_config = BookmarkEditor::NO_TREE;
382 // This is owned by the BookmarkEditorView.
383 handler = new SelectOnCreationHandler(profile_);
384 }
385 BookmarkEditor::Show(wnd_, profile_, GetParentForNewNodes(), NULL,
386 editor_config, handler);
387 #else
388 NOTIMPLEMENTED();
389 #endif
390 break;
391 }
392
393 case IDS_BOOMARK_BAR_NEW_FOLDER: {
394 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_NewFolder",
395 profile_);
396 EditFolderController::Show(profile_, wnd_, GetParentForNewNodes(),
397 true, (configuration_ != BOOKMARK_BAR));
398 break;
399 }
400
401 case IDS_BOOMARK_BAR_ALWAYS_SHOW:
402 bookmark_utils::ToggleWhenVisible(profile_);
403 break;
404
405 case IDS_BOOKMARK_MANAGER_SHOW_IN_FOLDER:
406 UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_ShowInFolder",
407 profile_);
408
409 if (selection_.size() != 1) {
410 NOTREACHED();
411 return;
412 }
413
414 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(TOOLKIT_VIEWS))
415 BookmarkManager::SelectInTree(profile_, selection_[0]);
416 #else
417 NOTIMPLEMENTED() << "Bookmark Manager not implemented";
418 #endif
419 break;
420
421 case IDS_BOOKMARK_MANAGER:
422 UserMetrics::RecordAction(L"ShowBookmarkManager", profile_);
423 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(TOOLKIT_VIEWS))
424 BookmarkManager::Show(profile_);
425 #else
426 NOTIMPLEMENTED() << "Bookmark Manager not implemented";
427 #endif
428 break;
429
430 case IDS_BOOKMARK_MANAGER_SORT:
431 UserMetrics::RecordAction(L"BookmarkManager_Sort", profile_);
432 model_->SortChildren(parent_);
433 break;
434
435 case IDS_COPY:
436 case IDS_CUT:
437 bookmark_utils::CopyToClipboard(profile_->GetBookmarkModel(),
438 selection_, id == IDS_CUT);
439 break;
440
441 case IDS_PASTE: {
442 // Always paste to parent.
443 if (!parent_)
444 return;
445
446 int index = (selection_.size() == 1) ?
447 parent_->IndexOfChild(selection_[0]) : -1;
448 if (index != -1)
449 index++;
450 bookmark_utils::PasteFromClipboard(profile_->GetBookmarkModel(),
451 parent_, index);
452 break;
453 }
454
455 default:
456 NOTREACHED();
457 }
458 }
459
460 bool BookmarkContextMenu::IsItemChecked(int id) const {
461 DCHECK(id == IDS_BOOMARK_BAR_ALWAYS_SHOW);
462 return profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
463 }
464
465 bool BookmarkContextMenu::IsCommandEnabled(int id) const {
466 bool is_root_node =
467 (selection_.size() == 1 &&
468 selection_[0]->GetParent() == model_->root_node());
469 switch (id) {
470 case IDS_BOOMARK_BAR_OPEN_INCOGNITO:
471 return !profile_->IsOffTheRecord();
472
473 case IDS_BOOMARK_BAR_OPEN_ALL_INCOGNITO:
474 return HasURLs() && !profile_->IsOffTheRecord();
475
476 case IDS_BOOMARK_BAR_OPEN_ALL:
477 case IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW:
478 return HasURLs();
479
480 case IDS_BOOKMARK_BAR_RENAME_FOLDER:
481 case IDS_BOOKMARK_BAR_EDIT:
482 return selection_.size() == 1 && !is_root_node;
483
484 case IDS_BOOKMARK_BAR_REMOVE:
485 return !selection_.empty() && !is_root_node;
486
487 case IDS_BOOKMARK_MANAGER_SHOW_IN_FOLDER:
488 return (configuration_ == BOOKMARK_MANAGER_TABLE_OTHER ||
489 configuration_ == BOOKMARK_MANAGER_ORGANIZE_MENU_OTHER) &&
490 selection_.size() == 1;
491
492 case IDS_BOOKMARK_MANAGER_SORT:
493 return parent_ && parent_ != model_->root_node();
494
495 case IDS_BOOMARK_BAR_NEW_FOLDER:
496 case IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK:
497 return GetParentForNewNodes() != NULL;
498
499 case IDS_COPY:
500 case IDS_CUT:
501 return selection_.size() > 0 && !is_root_node;
502
503 case IDS_PASTE:
504 // Always paste to parent.
505 return bookmark_utils::CanPasteFromClipboard(parent_);
506 }
507 return true;
508 }
509
510 void BookmarkContextMenu::BookmarkModelBeingDeleted(BookmarkModel* model) {
511 ModelChanged();
512 }
513
514 void BookmarkContextMenu::BookmarkNodeMoved(BookmarkModel* model,
515 const BookmarkNode* old_parent,
516 int old_index,
517 const BookmarkNode* new_parent,
518 int new_index) {
519 ModelChanged();
520 }
521
522 void BookmarkContextMenu::BookmarkNodeAdded(BookmarkModel* model,
523 const BookmarkNode* parent,
524 int index) {
525 ModelChanged();
526 }
527
528 void BookmarkContextMenu::BookmarkNodeRemoved(BookmarkModel* model,
529 const BookmarkNode* parent,
530 int index,
531 const BookmarkNode* node) {
532 ModelChanged();
533 }
534
535 void BookmarkContextMenu::BookmarkNodeChanged(BookmarkModel* model,
536 const BookmarkNode* node) {
537 ModelChanged();
538 }
539
540 void BookmarkContextMenu::BookmarkNodeChildrenReordered(
541 BookmarkModel* model, const BookmarkNode* node) {
542 ModelChanged();
543 }
544
545 void BookmarkContextMenu::ModelChanged() {
546 menu_->Cancel();
547 }
548
549 BookmarkModel* BookmarkContextMenu::RemoveModelObserver() {
550 BookmarkModel* model = model_;
551 model_->RemoveObserver(this);
552 model_ = NULL;
553 return model;
554 }
555
556 bool BookmarkContextMenu::HasURLs() const {
557 for (size_t i = 0; i < selection_.size(); ++i) {
558 if (NodeHasURLs(selection_[i]))
559 return true;
560 }
561 return false;
562 }
563
564 const BookmarkNode* BookmarkContextMenu::GetParentForNewNodes() const {
565 return (selection_.size() == 1 && selection_[0]->is_folder()) ?
566 selection_[0] : parent_;
567 }
OLDNEW
« no previous file with comments | « chrome/browser/gtk/bookmark_context_menu.h ('k') | chrome/browser/gtk/bookmark_context_menu_gtk.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698