[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / bookmarks / bookmark_name_folder_controller.mm
blobbffa889634b7a60c419fa5ead1bfc0678c4c8cbf
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/mac/mac_util.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_model_observer_for_cocoa.h"
13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/l10n/l10n_util_mac.h"
17 @implementation BookmarkNameFolderController
19 // Common initializer (private).
20 - (id)initWithParentWindow:(NSWindow*)window
21                    profile:(Profile*)profile
22                       node:(const BookmarkNode*)node
23                     parent:(const BookmarkNode*)parent
24                   newIndex:(int)newIndex {
25   NSString* nibpath = [base::mac::FrameworkBundle()
26                         pathForResource:@"BookmarkNameFolder"
27                         ofType:@"nib"];
28   if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
29     parentWindow_ = window;
30     profile_ = profile;
31     node_ = node;
32     parent_ = parent;
33     newIndex_ = newIndex;
34     if (parent) {
35       DCHECK_LE(newIndex, parent->child_count());
36     }
37     if (node_) {
38       initialName_.reset([base::SysUTF16ToNSString(node_->GetTitle()) retain]);
39     } else {
40       NSString* newString =
41         l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME);
42       initialName_.reset([newString retain]);
43     }
44   }
45   return self;
48 - (id)initWithParentWindow:(NSWindow*)window
49                    profile:(Profile*)profile
50                       node:(const BookmarkNode*)node {
51   DCHECK(node);
52   return [self initWithParentWindow:window
53                             profile:profile
54                                node:node
55                              parent:nil
56                            newIndex:0];
59 - (id)initWithParentWindow:(NSWindow*)window
60                    profile:(Profile*)profile
61                     parent:(const BookmarkNode*)parent
62                   newIndex:(int)newIndex {
63   DCHECK(parent);
64   return [self initWithParentWindow:window
65                             profile:profile
66                                node:nil
67                              parent:parent
68                            newIndex:newIndex];
71 - (void)awakeFromNib {
72   [nameField_ setStringValue:initialName_.get()];
73   [[nameField_ cell] setUsesSingleLineMode:YES];
75   [okButton_ setTitle:l10n_util::GetNSStringWithFixup(node_ ? IDS_SAVE :
76                                                               IDS_ADD)];
79 - (void)runAsModalSheet {
80   // Ping me when things change out from under us.
81   observer_.reset(new BookmarkModelObserverForCocoa(
82                     BookmarkModelFactory::GetForProfile(profile_),
83                     ^(BOOL nodeWasDeleted) {
84                         [self cancel:nil];
85                     }));
86   observer_->StartObservingNode(node_);
87   [NSApp beginSheet:[self window]
88      modalForWindow:parentWindow_
89       modalDelegate:self
90      didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
91         contextInfo:nil];
94 - (IBAction)cancel:(id)sender {
95   [NSApp endSheet:[self window]];
98 - (IBAction)ok:(id)sender {
99   NSString* name = [nameField_ stringValue];
100   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
101   if (node_) {
102     model->SetTitle(node_, base::SysNSStringToUTF16(name));
103   } else {
104     model->AddFolder(parent_,
105                      newIndex_,
106                      base::SysNSStringToUTF16(name));
107   }
108   [NSApp endSheet:[self window]];
111 - (void)didEndSheet:(NSWindow*)sheet
112          returnCode:(int)returnCode
113         contextInfo:(void*)contextInfo {
114   [[self window] orderOut:self];
115   observer_.reset(NULL);
116   [self autorelease];
119 - (NSString*)folderName {
120   return [nameField_ stringValue];
123 - (void)setFolderName:(NSString*)name {
124   [nameField_ setStringValue:name];
127 - (NSButton*)okButton {
128   return okButton_;
131 @end  // BookmarkNameFolderController