1 // Copyright (c) 2012 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 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_all_tabs_controller.h"
7 #include "base/strings/string16.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_tabstrip.h"
11 #include "chrome/browser/ui/cocoa/last_active_browser_cocoa.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "components/bookmarks/browser/bookmark_model.h"
15 #include "content/public/browser/web_contents.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
18 using bookmarks::BookmarkModel;
19 using bookmarks::BookmarkNode;
20 using content::WebContents;
22 @implementation BookmarkAllTabsController
24 - (id)initWithParentWindow:(NSWindow*)parentWindow
25 profile:(Profile*)profile
26 parent:(const BookmarkNode*)parent
28 title:(const base::string16&)title
29 configuration:(BookmarkEditor::Configuration)configuration {
30 NSString* nibName = @"BookmarkAllTabs";
31 if ((self = [super initWithParentWindow:parentWindow
37 configuration:configuration])) {
42 - (void)awakeFromNib {
44 l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME)];
48 #pragma mark Bookmark Editing
50 - (void)UpdateActiveTabPairs {
51 activeTabPairsVector_.clear();
52 Browser* browser = chrome::GetLastActiveBrowser();
53 const int tabCount = browser->tab_strip_model()->count();
54 for (int i = 0; i < tabCount; ++i) {
55 WebContents* contents = browser->tab_strip_model()->GetWebContentsAt(i);
56 ActiveTabNameURLPair tabPair(contents->GetTitle(), contents->GetURL());
57 activeTabPairsVector_.push_back(tabPair);
61 // Called by -[BookmarkEditorBaseController ok:]. Creates the container
62 // folder for the tabs and then the bookmarks in that new folder.
63 // Returns a BOOL as an NSNumber indicating that the commit may proceed.
64 - (NSNumber*)didCommit {
65 const BookmarkNode* newParentNode = [self selectedNode];
67 return [NSNumber numberWithBool:NO];
68 int newIndex = newParentNode->child_count();
69 // Create the new folder which will contain all of the tab URLs.
70 NSString* newFolderName = [self displayName];
71 base::string16 newFolderString = base::SysNSStringToUTF16(newFolderName);
72 BookmarkModel* model = [self bookmarkModel];
73 const BookmarkNode* newFolder = model->AddFolder(newParentNode, newIndex,
75 // Get a list of all open tabs, create nodes for them, and add
76 // to the new folder node.
77 [self UpdateActiveTabPairs];
79 for (ActiveTabsNameURLPairVector::const_iterator it =
80 activeTabPairsVector_.begin();
81 it != activeTabPairsVector_.end(); ++it, ++i) {
82 model->AddURL(newFolder, i, it->first, it->second);
84 return [NSNumber numberWithBool:YES];
87 - (ActiveTabsNameURLPairVector*)activeTabPairsVector {
88 return &activeTabPairsVector_;
91 @end // BookmarkAllTabsController