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"
30 if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
31 parentWindow_ = window;
37 DCHECK_LE(newIndex, parent->child_count());
40 initialName_.reset([base::SysUTF16ToNSString(node_->GetTitle()) retain]);
43 l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME);
44 initialName_.reset([newString retain]);
50 - (id)initWithParentWindow:(NSWindow*)window
51 profile:(Profile*)profile
52 node:(const BookmarkNode*)node {
54 return [self initWithParentWindow:window
61 - (id)initWithParentWindow:(NSWindow*)window
62 profile:(Profile*)profile
63 parent:(const BookmarkNode*)parent
64 newIndex:(int)newIndex {
66 return [self initWithParentWindow:window
73 - (void)awakeFromNib {
74 [nameField_ setStringValue:initialName_.get()];
75 [[nameField_ cell] setUsesSingleLineMode:YES];
77 [okButton_ setTitle:l10n_util::GetNSStringWithFixup(node_ ? IDS_SAVE :
81 - (void)runAsModalSheet {
82 // Ping me when things change out from under us.
83 observer_.reset(new BookmarkModelObserverForCocoa(
84 BookmarkModelFactory::GetForProfile(profile_), ^() {
87 observer_->StartObservingNode(node_);
88 [NSApp beginSheet:[self window]
89 modalForWindow:parentWindow_
91 didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
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_);
105 model->SetTitle(node_, base::SysNSStringToUTF16(name));
107 model->AddFolder(parent_,
109 base::SysNSStringToUTF16(name));
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);
122 - (NSString*)folderName {
123 return [nameField_ stringValue];
126 - (void)setFolderName:(NSString*)name {
127 [nameField_ setStringValue:name];
130 - (NSButton*)okButton {
134 @end // BookmarkNameFolderController