Home | History | Annotate | Download | only in bookmarks
      1 // Copyright (c) 2011 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.
      4 
      5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_name_folder_controller.h"
      6 
      7 #include "ui/base/l10n/l10n_util.h"
      8 #include "base/mac/mac_util.h"
      9 #include "base/sys_string_conversions.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/ui/cocoa/bookmarks/bookmark_model_observer_for_cocoa.h"
     12 #include "grit/generated_resources.h"
     13 #include "ui/base/l10n/l10n_util_mac.h"
     14 
     15 @implementation BookmarkNameFolderController
     16 
     17 // Common initializer (private).
     18 - (id)initWithParentWindow:(NSWindow*)window
     19                    profile:(Profile*)profile
     20                       node:(const BookmarkNode*)node
     21                     parent:(const BookmarkNode*)parent
     22                   newIndex:(int)newIndex {
     23   NSString* nibpath = [base::mac::MainAppBundle()
     24                         pathForResource:@"BookmarkNameFolder"
     25                         ofType:@"nib"];
     26   if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
     27     parentWindow_ = window;
     28     profile_ = profile;
     29     node_ = node;
     30     parent_ = parent;
     31     newIndex_ = newIndex;
     32     if (parent) {
     33       DCHECK_LE(newIndex, parent->child_count());
     34     }
     35     if (node_) {
     36       initialName_.reset([base::SysUTF16ToNSString(node_->GetTitle()) retain]);
     37     } else {
     38       NSString* newString =
     39         l10n_util::GetNSStringWithFixup(IDS_BOOMARK_EDITOR_NEW_FOLDER_NAME);
     40       initialName_.reset([newString retain]);
     41     }
     42   }
     43   return self;
     44 }
     45 
     46 - (id)initWithParentWindow:(NSWindow*)window
     47                    profile:(Profile*)profile
     48                       node:(const BookmarkNode*)node {
     49   DCHECK(node);
     50   return [self initWithParentWindow:window
     51                             profile:profile
     52                                node:node
     53                              parent:nil
     54                            newIndex:0];
     55 }
     56 
     57 - (id)initWithParentWindow:(NSWindow*)window
     58                    profile:(Profile*)profile
     59                     parent:(const BookmarkNode*)parent
     60                   newIndex:(int)newIndex {
     61   DCHECK(parent);
     62   return [self initWithParentWindow:window
     63                             profile:profile
     64                                node:nil
     65                              parent:parent
     66                            newIndex:newIndex];
     67 }
     68 
     69 - (void)awakeFromNib {
     70   [nameField_ setStringValue:initialName_.get()];
     71 }
     72 
     73 - (void)runAsModalSheet {
     74   // Ping me when things change out from under us.
     75   observer_.reset(new BookmarkModelObserverForCocoa(
     76                     node_, profile_->GetBookmarkModel(),
     77                     self,
     78                     @selector(cancel:)));
     79   [NSApp beginSheet:[self window]
     80      modalForWindow:parentWindow_
     81       modalDelegate:self
     82      didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
     83         contextInfo:nil];
     84 }
     85 
     86 - (IBAction)cancel:(id)sender {
     87   [NSApp endSheet:[self window]];
     88 }
     89 
     90 - (IBAction)ok:(id)sender {
     91   NSString* name = [nameField_ stringValue];
     92   BookmarkModel* model = profile_->GetBookmarkModel();
     93   if (node_) {
     94     model->SetTitle(node_, base::SysNSStringToUTF16(name));
     95   } else {
     96     model->AddFolder(parent_,
     97                      newIndex_,
     98                      base::SysNSStringToUTF16(name));
     99   }
    100   [NSApp endSheet:[self window]];
    101 }
    102 
    103 - (void)didEndSheet:(NSWindow*)sheet
    104          returnCode:(int)returnCode
    105         contextInfo:(void*)contextInfo {
    106   [[self window] orderOut:self];
    107   observer_.reset(NULL);
    108   [self autorelease];
    109 }
    110 
    111 - (NSString*)folderName {
    112   return [nameField_ stringValue];
    113 }
    114 
    115 - (void)setFolderName:(NSString*)name {
    116   [nameField_ setStringValue:name];
    117 }
    118 
    119 - (NSButton*)okButton {
    120   return okButton_;
    121 }
    122 
    123 @end  // BookmarkNameFolderController
    124