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"
28 if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
29 parentWindow_ = window;
35 DCHECK_LE(newIndex, parent->child_count());
38 initialName_.reset([base::SysUTF16ToNSString(node_->GetTitle()) retain]);
41 l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME);
42 initialName_.reset([newString retain]);
48 - (id)initWithParentWindow:(NSWindow*)window
49 profile:(Profile*)profile
50 node:(const BookmarkNode*)node {
52 return [self initWithParentWindow:window
59 - (id)initWithParentWindow:(NSWindow*)window
60 profile:(Profile*)profile
61 parent:(const BookmarkNode*)parent
62 newIndex:(int)newIndex {
64 return [self initWithParentWindow:window
71 - (void)awakeFromNib {
72 [nameField_ setStringValue:initialName_.get()];
73 [[nameField_ cell] setUsesSingleLineMode:YES];
75 [okButton_ setTitle:l10n_util::GetNSStringWithFixup(node_ ? IDS_SAVE :
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) {
86 observer_->StartObservingNode(node_);
87 [NSApp beginSheet:[self window]
88 modalForWindow:parentWindow_
90 didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
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_);
102 model->SetTitle(node_, base::SysNSStringToUTF16(name));
104 model->AddFolder(parent_,
106 base::SysNSStringToUTF16(name));
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);
119 - (NSString*)folderName {
120 return [nameField_ stringValue];
123 - (void)setFolderName:(NSString*)name {
124 [nameField_ setStringValue:name];
127 - (NSButton*)okButton {
131 @end // BookmarkNameFolderController