| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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/cocoa/cookies_window_controller.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "app/l10n_util_mac.h" |
| 10 #include "app/resource_bundle.h" |
| 11 #import "base/i18n/time_formatting.h" |
| 12 #import "base/mac_util.h" |
| 13 #include "base/sys_string_conversions.h" |
| 14 #include "chrome/browser/profile.h" |
| 15 #include "grit/generated_resources.h" |
| 16 #include "grit/theme_resources.h" |
| 17 #include "skia/ext/skia_utils_mac.h" |
| 18 #include "third_party/apple/ImageAndTextCell.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 |
| 21 // Key path used for notifying KVO. |
| 22 static NSString* const kCocoaTreeModel = @"cocoaTreeModel"; |
| 23 |
| 24 CookiesTreeModelObserverBridge::CookiesTreeModelObserverBridge( |
| 25 CookiesWindowController* controller) |
| 26 : window_controller_(controller) { |
| 27 } |
| 28 |
| 29 // Notification that nodes were added to the specified parent. |
| 30 void CookiesTreeModelObserverBridge::TreeNodesAdded(TreeModel* model, |
| 31 TreeModelNode* parent, |
| 32 int start, |
| 33 int count) { |
| 34 CocoaCookieTreeNode* cocoa_parent = FindCocoaNode(parent, nil); |
| 35 NSMutableArray* cocoa_children = [cocoa_parent children]; |
| 36 |
| 37 [window_controller_ willChangeValueForKey:kCocoaTreeModel]; |
| 38 CookieTreeNode* cookie_parent = static_cast<CookieTreeNode*>(parent); |
| 39 for (int i = 0; i < count; ++i) { |
| 40 CookieTreeNode* cookie_child = cookie_parent->GetChild(start + i); |
| 41 CocoaCookieTreeNode* new_child = CocoaNodeFromTreeNode(cookie_child, true); |
| 42 [cocoa_children addObject:new_child]; |
| 43 } |
| 44 [window_controller_ didChangeValueForKey:kCocoaTreeModel]; |
| 45 } |
| 46 |
| 47 // Notification that nodes were removed from the specified parent. |
| 48 void CookiesTreeModelObserverBridge::TreeNodesRemoved(TreeModel* model, |
| 49 TreeModelNode* parent, |
| 50 int start, |
| 51 int count) { |
| 52 CocoaCookieTreeNode* cocoa_parent = FindCocoaNode(parent, nil); |
| 53 [window_controller_ willChangeValueForKey:kCocoaTreeModel]; |
| 54 NSMutableArray* cocoa_children = [cocoa_parent children]; |
| 55 for (int i = start + count - 1; i >= start; --i) { |
| 56 [cocoa_children removeObjectAtIndex:i]; |
| 57 } |
| 58 [window_controller_ didChangeValueForKey:kCocoaTreeModel]; |
| 59 } |
| 60 |
| 61 // Notification the children of |parent| have been reordered. Note, only |
| 62 // the direct children of |parent| have been reordered, not descendants. |
| 63 void CookiesTreeModelObserverBridge::TreeNodeChildrenReordered(TreeModel* model, |
| 64 TreeModelNode* parent) { |
| 65 CocoaCookieTreeNode* cocoa_parent = FindCocoaNode(parent, nil); |
| 66 NSMutableArray* cocoa_children = [cocoa_parent children]; |
| 67 |
| 68 CookieTreeNode* cookie_parent = static_cast<CookieTreeNode*>(parent); |
| 69 const int child_count = cookie_parent->GetChildCount(); |
| 70 |
| 71 [window_controller_ willChangeValueForKey:kCocoaTreeModel]; |
| 72 for (int i = 0; i < child_count; ++i) { |
| 73 CookieTreeNode* swap_in = cookie_parent->GetChild(i); |
| 74 for (int j = i; j < child_count; ++j) { |
| 75 CocoaCookieTreeNode* child = [cocoa_children objectAtIndex:j]; |
| 76 TreeModelNode* swap_out = [child treeNode]; |
| 77 if (swap_in == swap_out) { |
| 78 [cocoa_children exchangeObjectAtIndex:j withObjectAtIndex:i]; |
| 79 break; |
| 80 } |
| 81 } |
| 82 } |
| 83 [window_controller_ didChangeValueForKey:kCocoaTreeModel]; |
| 84 } |
| 85 |
| 86 // Notification that the contents of a node has changed. |
| 87 void CookiesTreeModelObserverBridge::TreeNodeChanged(TreeModel* model, |
| 88 TreeModelNode* node) { |
| 89 [window_controller_ willChangeValueForKey:kCocoaTreeModel]; |
| 90 CocoaCookieTreeNode* changed_node = FindCocoaNode(node, nil); |
| 91 [changed_node rebuild]; |
| 92 [window_controller_ didChangeValueForKey:kCocoaTreeModel]; |
| 93 } |
| 94 |
| 95 CocoaCookieTreeNode* CookiesTreeModelObserverBridge::CocoaNodeFromTreeNode( |
| 96 TreeModelNode* node, bool recurse) { |
| 97 CookieTreeNode* cookie_node = static_cast<CookieTreeNode*>(node); |
| 98 return [[[CocoaCookieTreeNode alloc] initWithNode:cookie_node] autorelease]; |
| 99 } |
| 100 |
| 101 // Does a pre-order traversal on the tree to find |node|. |
| 102 CocoaCookieTreeNode* CookiesTreeModelObserverBridge::FindCocoaNode( |
| 103 TreeModelNode* node, CocoaCookieTreeNode* start) { |
| 104 if (!start) { |
| 105 start = [window_controller_ cocoaTreeModel]; |
| 106 } |
| 107 if ([start treeNode] == node) { |
| 108 return start; |
| 109 } |
| 110 |
| 111 NSArray* children = [start children]; |
| 112 for (CocoaCookieTreeNode* child in children) { |
| 113 if ([child treeNode] == node) { |
| 114 return child; |
| 115 } |
| 116 |
| 117 // Search the children. Return the result if we find one. |
| 118 CocoaCookieTreeNode* recurse = FindCocoaNode(node, child); |
| 119 if (recurse) |
| 120 return recurse; |
| 121 } |
| 122 return nil; // We couldn't find the node. |
| 123 } |
| 124 |
| 125 #pragma mark Window Controller |
| 126 |
| 127 @implementation CookiesWindowController |
| 128 |
| 129 @synthesize treeController = treeController_; |
| 130 |
| 131 - (id)initWithProfile:(Profile*)profile { |
| 132 DCHECK(profile); |
| 133 NSString* nibpath = [mac_util::MainAppBundle() pathForResource:@"Cookies" |
| 134 ofType:@"nib"]; |
| 135 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
| 136 profile_ = profile; |
| 137 treeModel_.reset(new CookiesTreeModel(profile_)); |
| 138 modelObserver_.reset(new CookiesTreeModelObserverBridge(self)); |
| 139 treeModel_->SetObserver(modelObserver_.get()); |
| 140 |
| 141 // Convert the model's icons from Skia to Cocoa. |
| 142 std::vector<SkBitmap> skiaIcons; |
| 143 treeModel_->GetIcons(&skiaIcons); |
| 144 icons_.reset([[NSMutableArray alloc] init]); |
| 145 for (std::vector<SkBitmap>::iterator it = skiaIcons.begin(); |
| 146 it != skiaIcons.end(); ++it) { |
| 147 [icons_ addObject:gfx::SkBitmapToNSImage(*it)]; |
| 148 } |
| 149 |
| 150 // Default icon will be the last item in the array. |
| 151 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 152 [icons_ addObject:rb.GetNSImageNamed(IDR_BOOKMARK_BAR_FOLDER)]; |
| 153 |
| 154 // Create the Cocoa model. |
| 155 CookieTreeNode* root = static_cast<CookieTreeNode*>(treeModel_->GetRoot()); |
| 156 cocoaTreeModel_.reset([[CocoaCookieTreeNode alloc] initWithNode:root]); |
| 157 } |
| 158 return self; |
| 159 } |
| 160 |
| 161 - (void)awakeFromNib { |
| 162 DCHECK([self window]); |
| 163 DCHECK_EQ(self, [[self window] delegate]); |
| 164 } |
| 165 |
| 166 - (void)windowWillClose:(NSNotification*)notif { |
| 167 [self autorelease]; |
| 168 } |
| 169 |
| 170 - (void)attachSheetTo:(NSWindow*)window { |
| 171 [NSApp beginSheet:[self window] |
| 172 modalForWindow:window |
| 173 modalDelegate:self |
| 174 didEndSelector:@selector(sheetEndSheet:returnCode:contextInfo:) |
| 175 contextInfo:nil]; |
| 176 } |
| 177 |
| 178 - (void)sheetEndSheet:(NSWindow*)sheet |
| 179 returnCode:(NSInteger)returnCode |
| 180 contextInfo:(void*)context { |
| 181 [sheet close]; |
| 182 [sheet orderOut:self]; |
| 183 } |
| 184 |
| 185 - (IBAction)deleteCookie:(id)sender { |
| 186 scoped_nsobject<NSArray> selection( |
| 187 [[treeController_ selectedObjects] retain]); |
| 188 for (CocoaCookieTreeNode* node in selection.get()) { |
| 189 CookieTreeNode* cookie = static_cast<CookieTreeNode*>([node treeNode]); |
| 190 treeModel_->DeleteCookieNode(cookie); |
| 191 } |
| 192 } |
| 193 |
| 194 - (IBAction)deleteAllCookies:(id)sender { |
| 195 treeModel_->DeleteAllCookies(); |
| 196 } |
| 197 |
| 198 - (IBAction)closeSheet:(id)sender { |
| 199 [NSApp endSheet:[self window]]; |
| 200 } |
| 201 |
| 202 #pragma mark Getters and Setters |
| 203 |
| 204 - (CocoaCookieTreeNode*)cocoaTreeModel { |
| 205 return cocoaTreeModel_.get(); |
| 206 } |
| 207 - (void)setCocoaTreeModel:(CocoaCookieTreeNode*)model { |
| 208 return cocoaTreeModel_.reset([model retain]); |
| 209 } |
| 210 |
| 211 - (CookiesTreeModel*)treeModel { |
| 212 return treeModel_.get(); |
| 213 } |
| 214 |
| 215 #pragma mark Outline View Delegate |
| 216 |
| 217 - (void)outlineView:(NSOutlineView*)outlineView |
| 218 willDisplayCell:(id)cell |
| 219 forTableColumn:(NSTableColumn*)tableColumn |
| 220 item:(id)item { |
| 221 CocoaCookieTreeNode* node = [item representedObject]; |
| 222 int index = treeModel_->GetIconIndex([node treeNode]); |
| 223 NSImage* icon = nil; |
| 224 if (index >= 0) |
| 225 icon = [icons_ objectAtIndex:index]; |
| 226 else |
| 227 icon = [icons_ lastObject]; |
| 228 [(ImageAndTextCell*)cell setImage:icon]; |
| 229 } |
| 230 |
| 231 #pragma mark Unit Testing |
| 232 |
| 233 - (CookiesTreeModelObserverBridge*)modelObserver { |
| 234 return modelObserver_.get(); |
| 235 } |
| 236 |
| 237 - (NSArray*)icons { |
| 238 return icons_.get(); |
| 239 } |
| 240 |
| 241 @end |
| OLD | NEW |