Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_name_folder_controller.mm
blob52f7aae29e38048460e4388c52e787a937adc2b6
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_name_folder_controller.h"
7 #include "base/mac/bundle_locations.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_model_observer_for_cocoa.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/base/l10n/l10n_util_mac.h"
16 using bookmarks::BookmarkModel;
17 using bookmarks::BookmarkNode;
19 @implementation BookmarkNameFolderController
21 // Common initializer (private).
22 - (id)initWithParentWindow:(NSWindow*)window
23                    profile:(Profile*)profile
24                       node:(const BookmarkNode*)node
25                     parent:(const BookmarkNode*)parent
26                   newIndex:(int)newIndex {
27   NSString* nibpath = [base::mac::FrameworkBundle()
28                         pathForResource:@"BookmarkNameFolder"
29                         ofType:@"nib"];
30   if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
31     parentWindow_ = window;
32     profile_ = profile;
33     node_ = node;
34     parent_ = parent;
35     newIndex_ = newIndex;
36     if (parent) {
37       DCHECK_LE(newIndex, parent->child_count());
38     }
39     if (node_) {
40       initialName_.reset([base::SysUTF16ToNSString(node_->GetTitle()) retain]);
41     } else {
42       NSString* newString =
43         l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME);
44       initialName_.reset([newString retain]);
45     }
46   }
47   return self;
50 - (id)initWithParentWindow:(NSWindow*)window
51                    profile:(Profile*)profile
52                       node:(const BookmarkNode*)node {
53   DCHECK(node);
54   return [self initWithParentWindow:window
55                             profile:profile
56                                node:node
57                              parent:nil
58                            newIndex:0];
61 - (id)initWithParentWindow:(NSWindow*)window
62                    profile:(Profile*)profile
63                     parent:(const BookmarkNode*)parent
64                   newIndex:(int)newIndex {
65   DCHECK(parent);
66   return [self initWithParentWindow:window
67                             profile:profile
68                                node:nil
69                              parent:parent
70                            newIndex:newIndex];
73 - (void)awakeFromNib {
74   [nameField_ setStringValue:initialName_.get()];
75   [[nameField_ cell] setUsesSingleLineMode:YES];
77   [okButton_ setTitle:l10n_util::GetNSStringWithFixup(node_ ? IDS_SAVE :
78                                                               IDS_ADD)];
81 - (void)runAsModalSheet {
82   // Ping me when things change out from under us.
83   observer_.reset(new BookmarkModelObserverForCocoa(
84       BookmarkModelFactory::GetForProfile(profile_), ^() {
85         [self cancel:nil];
86       }));
87   observer_->StartObservingNode(node_);
88   [NSApp beginSheet:[self window]
89      modalForWindow:parentWindow_
90       modalDelegate:self
91      didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
92         contextInfo:nil];
95 - (IBAction)cancel:(id)sender {
96   [NSApp endSheet:[self window]];
99 - (IBAction)ok:(id)sender {
100   NSString* name = [nameField_ stringValue];
101   if ([name length] == 0)
102     name = l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME);
103   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
104   if (node_) {
105     model->SetTitle(node_, base::SysNSStringToUTF16(name));
106   } else {
107     model->AddFolder(parent_,
108                      newIndex_,
109                      base::SysNSStringToUTF16(name));
110   }
111   [NSApp endSheet:[self window]];
114 - (void)didEndSheet:(NSWindow*)sheet
115          returnCode:(int)returnCode
116         contextInfo:(void*)contextInfo {
117   [[self window] orderOut:self];
118   observer_.reset(NULL);
119   [self autorelease];
122 - (NSString*)folderName {
123   return [nameField_ stringValue];
126 - (void)setFolderName:(NSString*)name {
127   [nameField_ setStringValue:name];
130 - (NSButton*)okButton {
131   return okButton_;
134 @end  // BookmarkNameFolderController