Home | History | Annotate | Download | only in bookmarks
      1 // Copyright (c) 2011 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   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   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 
     91