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