1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/bookmarks/bookmark_name_folder_controller.h"
#include "base/mac/bundle_locations.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/cocoa/bookmarks/bookmark_model_observer_for_cocoa.h"
#include "chrome/grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/l10n/l10n_util_mac.h"
@implementation BookmarkNameFolderController
// Common initializer (private).
- (id)initWithParentWindow:(NSWindow*)window
profile:(Profile*)profile
node:(const BookmarkNode*)node
parent:(const BookmarkNode*)parent
newIndex:(int)newIndex {
NSString* nibpath = [base::mac::FrameworkBundle()
pathForResource:@"BookmarkNameFolder"
ofType:@"nib"];
if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
parentWindow_ = window;
profile_ = profile;
node_ = node;
parent_ = parent;
newIndex_ = newIndex;
if (parent) {
DCHECK_LE(newIndex, parent->child_count());
}
if (node_) {
initialName_.reset([base::SysUTF16ToNSString(node_->GetTitle()) retain]);
} else {
NSString* newString =
l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME);
initialName_.reset([newString retain]);
}
}
return self;
}
- (id)initWithParentWindow:(NSWindow*)window
profile:(Profile*)profile
node:(const BookmarkNode*)node {
DCHECK(node);
return [self initWithParentWindow:window
profile:profile
node:node
parent:nil
newIndex:0];
}
- (id)initWithParentWindow:(NSWindow*)window
profile:(Profile*)profile
parent:(const BookmarkNode*)parent
newIndex:(int)newIndex {
DCHECK(parent);
return [self initWithParentWindow:window
profile:profile
node:nil
parent:parent
newIndex:newIndex];
}
- (void)awakeFromNib {
[nameField_ setStringValue:initialName_.get()];
[[nameField_ cell] setUsesSingleLineMode:YES];
[okButton_ setTitle:l10n_util::GetNSStringWithFixup(node_ ? IDS_SAVE :
IDS_ADD)];
}
- (void)runAsModalSheet {
// Ping me when things change out from under us.
observer_.reset(new BookmarkModelObserverForCocoa(
BookmarkModelFactory::GetForProfile(profile_),
^(BOOL nodeWasDeleted) {
[self cancel:nil];
}));
observer_->StartObservingNode(node_);
[NSApp beginSheet:[self window]
modalForWindow:parentWindow_
modalDelegate:self
didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
contextInfo:nil];
}
- (IBAction)cancel:(id)sender {
[NSApp endSheet:[self window]];
}
- (IBAction)ok:(id)sender {
NSString* name = [nameField_ stringValue];
if ([name length] == 0)
name = l10n_util::GetNSStringWithFixup(IDS_BOOKMARK_EDITOR_NEW_FOLDER_NAME);
BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
if (node_) {
model->SetTitle(node_, base::SysNSStringToUTF16(name));
} else {
model->AddFolder(parent_,
newIndex_,
base::SysNSStringToUTF16(name));
}
[NSApp endSheet:[self window]];
}
- (void)didEndSheet:(NSWindow*)sheet
returnCode:(int)returnCode
contextInfo:(void*)contextInfo {
[[self window] orderOut:self];
observer_.reset(NULL);
[self autorelease];
}
- (NSString*)folderName {
return [nameField_ stringValue];
}
- (void)setFolderName:(NSString*)name {
[nameField_ setStringValue:name];
}
- (NSButton*)okButton {
return okButton_;
}
@end // BookmarkNameFolderController
|