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 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_EDITOR_BASE_CONTROLLER_H_
      6 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_EDITOR_BASE_CONTROLLER_H_
      7 #pragma once
      8 
      9 #import <Cocoa/Cocoa.h>
     10 
     11 #import "base/mac/cocoa_protocols.h"
     12 #include "base/memory/scoped_nsobject.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "chrome/browser/bookmarks/bookmark_editor.h"
     15 
     16 class BookmarkEditorBaseControllerBridge;
     17 class BookmarkModel;
     18 @class BookmarkTreeBrowserCell;
     19 
     20 // A base controller class for bookmark creation and editing dialogs which
     21 // present the current bookmark folder structure in a tree view.  Do not
     22 // instantiate this controller directly -- use one of its derived classes.
     23 // NOTE: If a derived class is intended to be dispatched via the
     24 // BookmarkEditor::Show static function found in the accompanying
     25 // implementation, that function will need to be update.
     26 @interface BookmarkEditorBaseController : NSWindowController {
     27  @private
     28   IBOutlet NSButton* newFolderButton_;
     29   IBOutlet NSButton* okButton_;  // Used for unit testing only.
     30   IBOutlet NSTreeController* folderTreeController_;
     31   IBOutlet NSOutlineView* folderTreeView_;
     32 
     33   NSWindow* parentWindow_;  // weak
     34   Profile* profile_;  // weak
     35   const BookmarkNode* parentNode_;  // weak; owned by the model
     36   BookmarkEditor::Configuration configuration_;
     37   NSString* initialName_;
     38   NSString* displayName_;  // Bound to a text field in the dialog.
     39   BOOL okEnabled_;  // Bound to the OK button.
     40   // An array of BookmarkFolderInfo where each item describes a folder in the
     41   // BookmarkNode structure.
     42   scoped_nsobject<NSArray> folderTreeArray_;
     43   // Bound to the table view giving a path to the current selections, of which
     44   // there should only ever be one.
     45   scoped_nsobject<NSArray> tableSelectionPaths_;
     46   // C++ bridge object that observes the BookmarkModel for me.
     47   scoped_ptr<BookmarkEditorBaseControllerBridge> observer_;
     48 }
     49 
     50 @property(nonatomic, copy) NSString* initialName;
     51 @property(nonatomic, copy) NSString* displayName;
     52 @property(nonatomic, assign) BOOL okEnabled;
     53 @property(nonatomic, retain, readonly) NSArray* folderTreeArray;
     54 @property(nonatomic, copy) NSArray* tableSelectionPaths;
     55 
     56 // Designated initializer.  Derived classes should call through to this init.
     57 - (id)initWithParentWindow:(NSWindow*)parentWindow
     58                    nibName:(NSString*)nibName
     59                    profile:(Profile*)profile
     60                     parent:(const BookmarkNode*)parent
     61              configuration:(BookmarkEditor::Configuration)configuration;
     62 
     63 // Run the bookmark editor as a modal sheet.  Does not block.
     64 - (void)runAsModalSheet;
     65 
     66 // Create a new folder at the end of the selected parent folder, give it
     67 // an untitled name, and put it into editing mode.
     68 - (IBAction)newFolder:(id)sender;
     69 
     70 // The cancel action will dismiss the dialog.  Derived classes which
     71 // override cancel:, must call this after accessing any dialog-related
     72 // data.
     73 - (IBAction)cancel:(id)sender;
     74 
     75 // The OK action will dismiss the dialog.  This action is bound
     76 // to the OK button of a dialog which presents a tree view of a profile's
     77 // folder hierarchy and allows the creation of new folders within that tree.
     78 // When the OK button is pressed, this function will: 1) call the derived
     79 // class's -[willCommit] function, 2) create any new folders created by
     80 // the user while the dialog is presented, 3) call the derived class's
     81 // -[didCommit] function, and then 4) dismiss the dialog.  At least one
     82 // of -[willCommit] and -[didCommit] must be provided by the derived class
     83 // and should return a NSNumber containing a BOOL or nil ('nil' means YES)
     84 // indicating if the operation should be allowed to continue.
     85 // Note: A derived class should not override the ok: action.
     86 - (IBAction)ok:(id)sender;
     87 
     88 // Methods for use by derived classes only.
     89 
     90 // Determine and returns the rightmost selected/highlighted element (node)
     91 // in the bookmark tree view if the tree view is showing, otherwise returns
     92 // the original |parentNode_|.  If the tree view is showing but nothing is
     93 // selected then the root node is returned.
     94 - (const BookmarkNode*)selectedNode;
     95 
     96 // Select/highlight the given node within the browser tree view.  If the
     97 // node is nil then select the bookmark bar node.  Exposed for unit test.
     98 - (void)selectNodeInBrowser:(const BookmarkNode*)node;
     99 
    100 // Notifications called when the BookmarkModel changes out from under me.
    101 - (void)nodeRemoved:(const BookmarkNode*)node
    102          fromParent:(const BookmarkNode*)parent;
    103 - (void)modelChangedPreserveSelection:(BOOL)preserve;
    104 
    105 // Accessors
    106 - (BookmarkModel*)bookmarkModel;
    107 - (const BookmarkNode*)parentNode;
    108 
    109 @end
    110 
    111 // Describes the profile's bookmark folder structure: the folder name, the
    112 // original BookmarkNode pointer (if the folder already exists), a BOOL
    113 // indicating if the folder is new (meaning: created during this session
    114 // but not yet committed to the bookmark structure), and an NSArray of
    115 // child folder BookmarkFolderInfo's following this same structure.
    116 @interface BookmarkFolderInfo : NSObject {
    117  @private
    118   NSString* folderName_;
    119   const BookmarkNode* folderNode_;  // weak
    120   NSMutableArray* children_;
    121   BOOL newFolder_;
    122 }
    123 
    124 @property(nonatomic, copy) NSString* folderName;
    125 @property(nonatomic, assign) const BookmarkNode* folderNode;
    126 @property(nonatomic, retain) NSMutableArray* children;
    127 @property(nonatomic, assign) BOOL newFolder;
    128 
    129 // Convenience creator for adding a new folder to the editor's bookmark
    130 // structure.  This folder will be added to the bookmark model when the
    131 // user accepts the dialog. |folderName| must be provided.
    132 + (id)bookmarkFolderInfoWithFolderName:(NSString*)folderName;
    133 
    134 // Designated initializer.  |folderName| must be provided.  For folders which
    135 // already exist in the bookmark model, |folderNode| and |children| (if any
    136 // children are already attached to this folder) must be provided and
    137 // |newFolder| should be NO.  For folders which the user has added during
    138 // this session and which have not been committed yet, |newFolder| should be
    139 // YES and |folderNode| and |children| should be NULL/nil.
    140 - (id)initWithFolderName:(NSString*)folderName
    141               folderNode:(const BookmarkNode*)folderNode
    142                 children:(NSMutableArray*)children
    143                newFolder:(BOOL)newFolder;
    144 
    145 // Convenience creator used during construction of the editor's bookmark
    146 // structure.  |folderName| and |folderNode| must be provided. |children|
    147 // is optional.  Private: exposed here for unit testing purposes.
    148 + (id)bookmarkFolderInfoWithFolderName:(NSString*)folderName
    149                             folderNode:(const BookmarkNode*)folderNode
    150                               children:(NSMutableArray*)children;
    151 
    152 @end
    153 
    154 @interface BookmarkEditorBaseController(TestingAPI)
    155 
    156 @property(nonatomic, readonly) BOOL okButtonEnabled;
    157 
    158 // Create any newly added folders.  New folders are nodes in folderTreeArray
    159 // which are marked as being new (i.e. their kFolderTreeNewFolderKey
    160 // dictionary item is YES).  This is called by -[ok:].
    161 - (void)createNewFolders;
    162 
    163 // Select the given bookmark node within the tree view.
    164 - (void)selectTestNodeInBrowser:(const BookmarkNode*)node;
    165 
    166 // Return the dictionary for the folder selected in the tree.
    167 - (BookmarkFolderInfo*)selectedFolder;
    168 
    169 @end
    170 
    171 #endif  // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_EDITOR_BASE_CONTROLLER_H_
    172