summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmark_bar_context_menu_controller.cc
blob: 472bad253c11ed59015d4db83b07463798e434c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "chrome/browser/bookmark_bar_context_menu_controller.h"

#include "chrome/browser/bookmark_bar_model.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/page_navigator.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/tab_contents.h"
#include "chrome/browser/user_metrics.h"
#include "chrome/browser/views/bookmark_editor_view.h"
#include "chrome/browser/views/input_window.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "chrome/views/view_container.h"
#include "chrome/views/window.h"

#include "generated_resources.h"

namespace {

// Returns true if the specified node is of type URL, or has a descendant
// of type URL.
bool NodeHasURLs(BookmarkBarNode* node) {
  if (node->GetType() == history::StarredEntry::URL)
    return true;

  for (int i = 0; i < node->GetChildCount(); ++i) {
    if (NodeHasURLs(node->GetChild(i)))
      return true;
  }
  return false;
}

// Opens a tab/window for node and recursively opens all descendants.
// If open_first_in_new_window is true, the first opened node is opened
// in a new window. navigator indicates the PageNavigator to use for
// new tabs. It is reset if open_first_in_new_window is true.
// opened_url is set to true the first time a new tab is opened.
void OpenAll(BookmarkBarNode* node,
             bool open_first_in_new_window,
             PageNavigator** navigator,
             bool* opened_url) {
  if (node->GetType() == history::StarredEntry::URL) {
    WindowOpenDisposition disposition;
    if (*opened_url)
      disposition = NEW_BACKGROUND_TAB;
    else if (open_first_in_new_window)
      disposition = NEW_WINDOW;
    else  // Open in current window.
      disposition = CURRENT_TAB;
    (*navigator)->OpenURL(node->GetURL(), disposition,
                          PageTransition::AUTO_BOOKMARK);
    if (!*opened_url) {
      *opened_url = true;
      if (open_first_in_new_window || disposition == CURRENT_TAB) {
        // We opened the tab in a new window or in the current tab which
        // likely reset the navigator. Need to reset the page navigator
        // appropriately.
        Browser* new_browser = BrowserList::GetLastActive();
        if (new_browser) {
          TabContents* current_tab = new_browser->GetSelectedTabContents();
          DCHECK(new_browser && current_tab);
          if (new_browser && current_tab)
            *navigator = current_tab;
        }  // else, new_browser == NULL, which happens during testing.
      }
    }
  } else {
    // Group, recurse through children.
    for (int i = 0; i < node->GetChildCount(); ++i) {
      OpenAll(node->GetChild(i), open_first_in_new_window, navigator,
              opened_url);
    }
  }
}

// EditFolderController -------------------------------------------------------

// EditFolderController manages the editing and/or creation of a folder. If the
// user presses ok, the name change is committed to the database.
//
// EditFolderController deletes itself when the window is closed.
//
class EditFolderController : public InputWindowDelegate,
                             public BookmarkBarView::ModelChangedListener {
 public:
  EditFolderController(BookmarkBarView* view,
                       BookmarkBarNode* node,
                       int visual_order,
                       bool is_new)
      : view_(view),
        node_(node),
        visual_order_(visual_order),
        is_new_(is_new) {
    DCHECK(is_new_ || node);
    window_ = CreateInputWindow(view->GetViewContainer()->GetHWND(), this);
    view_->SetModelChangedListener(this);
  }

  void Show() {
    window_->Show();
  }

  virtual void ModelChanged() {
    window_->Close();
  }

 private:
  virtual std::wstring GetTextFieldLabel() {
    return l10n_util::GetString(IDS_BOOMARK_BAR_EDIT_FOLDER_LABEL);
  }

  virtual std::wstring GetTextFieldContents() {
    if (is_new_)
      return l10n_util::GetString(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME);
    return node_->GetTitle();
  }

  virtual bool IsValid(const std::wstring& text) {
    return !text.empty();
  }

  virtual void InputAccepted(const std::wstring& text) {
    view_->ClearModelChangedListenerIfEquals(this);
    BookmarkBarModel* model = view_->GetProfile()->GetBookmarkBarModel();
    if (is_new_)
      model->AddGroup(node_, visual_order_, text);
    else
      model->SetTitle(node_, text);
  }

  virtual void InputCanceled() {
    view_->ClearModelChangedListenerIfEquals(this);
  }

  virtual void WindowClosing() {
    view_->ClearModelChangedListenerIfEquals(this);
    delete this;
  }

  virtual std::wstring GetWindowTitle() const {
    return is_new_ ?
        l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE_NEW) :
        l10n_util::GetString(IDS_BOOMARK_FOLDER_EDITOR_WINDOW_TITLE);
  }

  virtual ChromeViews::View* GetContentsView() {
    return view_;
  }

  BookmarkBarView* view_;

  // If is_new is true, this is the parent to create the new node under.
  // Otherwise this is the node to change the title of.
  BookmarkBarNode* node_;

  int visual_order_;
  bool is_new_;
  ChromeViews::Window* window_;

  DISALLOW_EVIL_CONSTRUCTORS(EditFolderController);
};

}  // namespace

// BookmarkBarContextMenuController -------------------------------------------

const int BookmarkBarContextMenuController::always_show_command_id = 1;
const int BookmarkBarContextMenuController::open_bookmark_id = 2;
const int BookmarkBarContextMenuController::open_bookmark_in_new_window_id = 3;
const int BookmarkBarContextMenuController::open_bookmark_in_new_tab_id = 4;
const int BookmarkBarContextMenuController::open_all_bookmarks_id = 5;
const int
    BookmarkBarContextMenuController::open_all_bookmarks_in_new_window_id = 6;
const int BookmarkBarContextMenuController::edit_bookmark_id = 7;
const int BookmarkBarContextMenuController::delete_bookmark_id = 8;
const int BookmarkBarContextMenuController::add_bookmark_id = 9;
const int BookmarkBarContextMenuController::new_folder_id = 10;

BookmarkBarContextMenuController::BookmarkBarContextMenuController(
    BookmarkBarView* view,
    BookmarkBarNode* node)
    : view_(view),
      node_(node),
      menu_(this) {
  if (node->GetType() == history::StarredEntry::URL) {
    menu_.AppendMenuItemWithLabel(
        open_bookmark_id,
        l10n_util::GetString(IDS_BOOMARK_BAR_OPEN));
    menu_.AppendMenuItemWithLabel(
        open_bookmark_in_new_tab_id,
        l10n_util::GetString(IDS_BOOMARK_BAR_OPEN_IN_NEW_TAB));
    menu_.AppendMenuItemWithLabel(
        open_bookmark_in_new_window_id,
        l10n_util::GetString(IDS_BOOMARK_BAR_OPEN_IN_NEW_WINDOW));
  } else {
    menu_.AppendMenuItemWithLabel(
        open_all_bookmarks_id,
        l10n_util::GetString(IDS_BOOMARK_BAR_OPEN_ALL));
    menu_.AppendMenuItemWithLabel(
        open_all_bookmarks_in_new_window_id,
        l10n_util::GetString(IDS_BOOMARK_BAR_OPEN_ALL_NEW_WINDOW));
  }
  menu_.AppendSeparator();

  if (node->GetParent() !=
      view->GetProfile()->GetBookmarkBarModel()->root_node()) {
    menu_.AppendMenuItemWithLabel(edit_bookmark_id,
                                  l10n_util::GetString(IDS_BOOKMARK_BAR_EDIT));
    menu_.AppendMenuItemWithLabel(
        delete_bookmark_id,
        l10n_util::GetString(IDS_BOOKMARK_BAR_REMOVE));
  }

  menu_.AppendMenuItemWithLabel(
      add_bookmark_id,
      l10n_util::GetString(IDS_BOOMARK_BAR_ADD_NEW_BOOKMARK));
  menu_.AppendMenuItemWithLabel(
      new_folder_id,
      l10n_util::GetString(IDS_BOOMARK_BAR_NEW_FOLDER));
  menu_.AppendSeparator();
  menu_.AppendMenuItem(always_show_command_id,
                       l10n_util::GetString(IDS_BOOMARK_BAR_ALWAYS_SHOW),
                       ChromeViews::MenuItemView::CHECKBOX);
}

void BookmarkBarContextMenuController::RunMenuAt(int x, int y) {
  // Record the current ModelChangedListener. It will be non-null when we're
  // used as the context menu for another menu.
  ModelChangedListener* last_listener = view_->GetModelChangedListener();

  view_->SetModelChangedListener(this);

  // width/height don't matter here.
  menu_.RunMenuAt(view_->GetViewContainer()->GetHWND(), gfx::Rect(x, y, 0, 0),
                  ChromeViews::MenuItemView::TOPLEFT, false);

  if (view_->GetModelChangedListener() == this)
    view_->SetModelChangedListener(last_listener);
}

void BookmarkBarContextMenuController::ModelChanged() {
  menu_.Cancel();
}

void BookmarkBarContextMenuController::ExecuteCommand(int id) {
  Profile* profile = view_->GetProfile();

  switch (id) {
    case open_bookmark_id:
      UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Open", profile);

      view_->GetPageNavigator()->OpenURL(node_->GetURL(), CURRENT_TAB,
                                         PageTransition::AUTO_BOOKMARK);
      break;

    case open_bookmark_in_new_window_id:
      UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_OpenInNewWindow",
                                profile);

      view_->GetPageNavigator()->OpenURL(node_->GetURL(), NEW_WINDOW,
                                         PageTransition::AUTO_BOOKMARK);
      break;

    case open_bookmark_in_new_tab_id:
      UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_OpenInNewTab",
                                profile);

      view_->GetPageNavigator()->OpenURL(node_->GetURL(), NEW_FOREGROUND_TAB,
                                         PageTransition::AUTO_BOOKMARK);
      break;

    case open_all_bookmarks_id:
    case open_all_bookmarks_in_new_window_id: {
      if (id == open_all_bookmarks_id) {
        UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_OpenAll",
                                  profile);
      } else {
        UserMetrics::RecordAction(
            L"BookmarkBar_ContextMenu_OpenAllInNewWindow", profile);
      }

      BookmarkBarNode* node = node_;
      PageNavigator* navigator = view_->GetPageNavigator();
      bool opened_url = false;
      OpenAll(node, (id == open_all_bookmarks_in_new_window_id), &navigator,
              &opened_url);
      break;
    }

    case edit_bookmark_id:
      UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Edit", profile);

      if (node_->GetType() == history::StarredEntry::URL) {
        BookmarkEditorView::Show(view_->GetViewContainer()->GetHWND(),
                                 view_->GetProfile(), node_->GetURL(),
                                 node_->GetTitle());
      } else {
        // Controller deletes itself when done.
        EditFolderController* controller = new EditFolderController(
            view_, node_, -1, false);
        controller->Show();
      }
      break;

    case delete_bookmark_id: {
      UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Remove", profile);

      view_->GetModel()->Remove(node_->GetParent(),
                                node_->GetParent()->IndexOfChild(node_));
      break;
    }

    case add_bookmark_id: {
      UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_Add", profile);

      BookmarkEditorView::Show(view_->GetViewContainer()->GetHWND(),
                               view_->GetProfile(), GURL(), std::wstring());
      break;
    }

    case new_folder_id: {
      UserMetrics::RecordAction(L"BookmarkBar_ContextMenu_NewFolder",
                                profile);

      int visual_order;
      BookmarkBarNode* parent =
          GetParentAndVisualOrderForNewNode(&visual_order);
      GetParentAndVisualOrderForNewNode(&visual_order);
      // Controller deletes itself when done.
      EditFolderController* controller =
          new EditFolderController(view_, parent, visual_order, true);
      controller->Show();
      break;
    }

    case always_show_command_id:
      view_->ToggleWhenVisible();
      break;

    default:
      NOTREACHED();
  }
}

bool BookmarkBarContextMenuController::IsItemChecked(int id) const {
  DCHECK(id == always_show_command_id);
  return view_->GetProfile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
}

bool BookmarkBarContextMenuController::IsCommandEnabled(int id) const {
  if (id == open_all_bookmarks_id || id == open_all_bookmarks_in_new_window_id)
    return NodeHasURLs(node_);

  return true;
}

// Returns the parent node and visual_order to use when adding new
// bookmarks/folders.
BookmarkBarNode* BookmarkBarContextMenuController::
    GetParentAndVisualOrderForNewNode(int* visual_order) {
  if (node_->GetType() != history::StarredEntry::URL) {
    // Adding to a group always adds to the end.
    *visual_order = node_->GetChildCount();
    return node_;
  } else {
    DCHECK(node_->GetParent());
    *visual_order = node_->GetParent()->IndexOfChild(node_) + 1;
    return node_->GetParent();
  }
}