Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_all_tabs_controller.mm
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.
4
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_all_tabs_controller.h"
6
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/core/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"
17
18 using content::WebContents;
19
20 @implementation BookmarkAllTabsController
21
22 - (id)initWithParentWindow:(NSWindow*)parentWindow
23                    profile:(Profile*)profile
24                     parent:(const BookmarkNode*)parent
25                        url:(const GURL&)url
26                      title:(const base::string16&)title
27              configuration:(BookmarkEditor::Configuration)configuration {
28   NSString* nibName = @"BookmarkAllTabs";
29   if ((self = [super initWithParentWindow:parentWindow
30                                   nibName:nibName
31                                   profile:profile
32                                    parent:parent
33                                       url:url
34                                     title:title
35                             configuration:configuration])) {
36   }
37   return self;
38 }
39
40 - (void)awakeFromNib {
41   [self setInitialName:
42       l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME)];
43   [super awakeFromNib];
44 }
45
46 #pragma mark Bookmark Editing
47
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);
56   }
57 }
58
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];
64   if (!newParentNode)
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,
72                                                    newFolderString);
73   // Get a list of all open tabs, create nodes for them, and add
74   // to the new folder node.
75   [self UpdateActiveTabPairs];
76   int i = 0;
77   for (ActiveTabsNameURLPairVector::const_iterator it =
78            activeTabPairsVector_.begin();
79        it != activeTabPairsVector_.end(); ++it, ++i) {
80     model->AddURL(newFolder, i, it->first, it->second);
81   }
82   return [NSNumber numberWithBool:YES];
83 }
84
85 - (ActiveTabsNameURLPairVector*)activeTabPairsVector {
86   return &activeTabPairsVector_;
87 }
88
89 @end  // BookmarkAllTabsController
90