bookmark_all_tabs_controller.mm revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2010 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/string16.h"
8#include "base/sys_string_conversions.h"
9#include "chrome/browser/bookmarks/bookmark_model.h"
10#include "chrome/browser/tabs/tab_strip_model.h"
11#include "chrome/browser/ui/browser.h"
12#include "chrome/browser/ui/browser_list.h"
13#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
14#include "content/browser/tab_contents/tab_contents.h"
15#include "grit/generated_resources.h"
16#include "ui/base/l10n/l10n_util_mac.h"
17
18@implementation BookmarkAllTabsController
19
20- (id)initWithParentWindow:(NSWindow*)parentWindow
21                   profile:(Profile*)profile
22                    parent:(const BookmarkNode*)parent
23             configuration:(BookmarkEditor::Configuration)configuration {
24  NSString* nibName = @"BookmarkAllTabs";
25  if ((self = [super initWithParentWindow:parentWindow
26                                  nibName:nibName
27                                  profile:profile
28                                   parent:parent
29                            configuration:configuration])) {
30  }
31  return self;
32}
33
34- (void)awakeFromNib {
35  [self setInitialName:
36      l10n_util::GetNSStringWithFixup(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME)];
37  [super awakeFromNib];
38}
39
40#pragma mark Bookmark Editing
41
42- (void)UpdateActiveTabPairs {
43  activeTabPairsVector_.clear();
44  Browser* browser = BrowserList::GetLastActive();
45  TabStripModel* tabstrip_model = browser->tabstrip_model();
46  const int tabCount = tabstrip_model->count();
47  for (int i = 0; i < tabCount; ++i) {
48    TabContents* tc = tabstrip_model->GetTabContentsAt(i)->tab_contents();
49    const string16 tabTitle = tc->GetTitle();
50    const GURL& tabURL(tc->GetURL());
51    ActiveTabNameURLPair tabPair(tabTitle, tabURL);
52    activeTabPairsVector_.push_back(tabPair);
53  }
54}
55
56// Called by -[BookmarkEditorBaseController ok:].  Creates the container
57// folder for the tabs and then the bookmarks in that new folder.
58// Returns a BOOL as an NSNumber indicating that the commit may proceed.
59- (NSNumber*)didCommit {
60  NSString* name = [[self displayName] stringByTrimmingCharactersInSet:
61                    [NSCharacterSet newlineCharacterSet]];
62  std::wstring newTitle = base::SysNSStringToWide(name);
63  const BookmarkNode* newParentNode = [self selectedNode];
64  int newIndex = newParentNode->GetChildCount();
65  // Create the new folder which will contain all of the tab URLs.
66  NSString* newFolderName = [self displayName];
67  string16 newFolderString = base::SysNSStringToUTF16(newFolderName);
68  BookmarkModel* model = [self bookmarkModel];
69  const BookmarkNode* newFolder = model->AddGroup(newParentNode, newIndex,
70                                                  newFolderString);
71  // Get a list of all open tabs, create nodes for them, and add
72  // to the new folder node.
73  [self UpdateActiveTabPairs];
74  int i = 0;
75  for (ActiveTabsNameURLPairVector::const_iterator it =
76           activeTabPairsVector_.begin();
77       it != activeTabPairsVector_.end(); ++it, ++i) {
78    model->AddURL(newFolder, i, it->first, it->second);
79  }
80  return [NSNumber numberWithBool:YES];
81}
82
83- (ActiveTabsNameURLPairVector*)activeTabPairsVector {
84  return &activeTabPairsVector_;
85}
86
87@end  // BookmarkAllTabsController
88
89