OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "app/l10n_util_mac.h" | 5 #include "app/l10n_util_mac.h" |
6 #include "base/mac_util.h" | 6 #include "base/mac_util.h" |
7 #include "base/sys_string_conversions.h" | 7 #include "base/sys_string_conversions.h" |
8 #include "chrome/browser/bookmarks/bookmark_model.h" | 8 #include "chrome/browser/bookmarks/bookmark_model.h" |
9 #import "chrome/browser/cocoa/bookmark_bubble_controller.h" | 9 #import "chrome/browser/cocoa/bookmark_bubble_controller.h" |
10 #import "chrome/browser/cocoa/bookmark_bubble_window.h" | 10 #import "chrome/browser/cocoa/bookmark_bubble_window.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 [mac_util::MainAppBundle() pathForResource:@"BookmarkBubble" | 23 [mac_util::MainAppBundle() pathForResource:@"BookmarkBubble" |
24 ofType:@"nib"]; | 24 ofType:@"nib"]; |
25 if ((self = [super initWithWindowNibPath:nibPath owner:self])) { | 25 if ((self = [super initWithWindowNibPath:nibPath owner:self])) { |
26 delegate_ = delegate; | 26 delegate_ = delegate; |
27 parentWindow_ = parentWindow; | 27 parentWindow_ = parentWindow; |
28 topLeftForBubble_ = topLeftForBubble; | 28 topLeftForBubble_ = topLeftForBubble; |
29 model_ = model; | 29 model_ = model; |
30 node_ = node; | 30 node_ = node; |
31 alreadyBookmarked_ = alreadyBookmarked; | 31 alreadyBookmarked_ = alreadyBookmarked; |
32 // But this is strong. | 32 // But this is strong. |
33 titleMapping_.reset([[NSMutableDictionary alloc] init]); | 33 parentMapping_.reset([[NSMutableArray alloc] init]); |
34 } | 34 } |
35 return self; | 35 return self; |
36 } | 36 } |
37 | 37 |
38 - (void)windowWillClose:(NSNotification *)notification { | 38 - (void)windowWillClose:(NSNotification *)notification { |
39 [self autorelease]; | 39 [self autorelease]; |
40 } | 40 } |
41 | 41 |
42 - (void)windowDidLoad { | 42 - (void)windowDidLoad { |
43 NSWindow* window = [self window]; | 43 NSWindow* window = [self window]; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 [self performSelector:@selector(ok:) withObject:self afterDelay:0]; | 119 [self performSelector:@selector(ok:) withObject:self afterDelay:0]; |
120 } | 120 } |
121 | 121 |
122 @end // BookmarkBubbleController | 122 @end // BookmarkBubbleController |
123 | 123 |
124 | 124 |
125 @implementation BookmarkBubbleController(ExposedForUnitTesting) | 125 @implementation BookmarkBubbleController(ExposedForUnitTesting) |
126 | 126 |
127 | 127 |
128 // Fill in all information related to the folder combo box. | 128 // Fill in all information related to the folder combo box. |
129 // | |
130 // TODO(jrg): make sure nested folders that have the same name are | |
131 // handled properly. | |
132 // http://crbug.com/19408 | |
133 - (void)fillInFolderList { | 129 - (void)fillInFolderList { |
134 [nameTextField_ setStringValue:base::SysWideToNSString(node_->GetTitle())]; | 130 [nameTextField_ setStringValue:base::SysWideToNSString(node_->GetTitle())]; |
| 131 DCHECK([parentMapping_ count] == 0); |
| 132 DCHECK([folderComboBox_ numberOfItems] == 0); |
135 [self addFolderNodes:model_->root_node() toComboBox:folderComboBox_]; | 133 [self addFolderNodes:model_->root_node() toComboBox:folderComboBox_]; |
136 | 134 |
137 // Add "Choose another folder...". Remember it for later to compare against. | 135 // Add "Choose another folder...". Remember it for later to compare against. |
138 chooseAnotherFolder_.reset( | 136 chooseAnotherFolder_.reset( |
139 [l10n_util::GetNSStringWithFixup(IDS_BOOMARK_BUBBLE_CHOOSER_ANOTHER_FOLDER) | 137 [l10n_util::GetNSStringWithFixup(IDS_BOOMARK_BUBBLE_CHOOSER_ANOTHER_FOLDER) |
140 retain]); | 138 retain]); |
141 [folderComboBox_ addItemWithObjectValue:chooseAnotherFolder_.get()]; | 139 [folderComboBox_ addItemWithObjectValue:chooseAnotherFolder_.get()]; |
142 | 140 |
143 // Finally, select the current parent. | 141 // Finally, select the current parent. |
144 NSString* parentTitle = base::SysWideToNSString( | 142 NSString* parentTitle = base::SysWideToNSString( |
145 node_->GetParent()->GetTitle()); | 143 node_->GetParent()->GetTitle()); |
146 [folderComboBox_ selectItemWithObjectValue:parentTitle]; | 144 [folderComboBox_ selectItemWithObjectValue:parentTitle]; |
147 } | 145 } |
148 | 146 |
149 // For the given folder node, walk the tree and add folder names to | 147 // For the given folder node, walk the tree and add folder names to |
150 // the given combo box. | 148 // the given combo box. |
151 // | |
152 // TODO(jrg): no distinction is made among folders with the same name. | |
153 - (void)addFolderNodes:(const BookmarkNode*)parent toComboBox:(NSComboBox*)box { | 149 - (void)addFolderNodes:(const BookmarkNode*)parent toComboBox:(NSComboBox*)box { |
154 NSString* title = base::SysWideToNSString(parent->GetTitle()); | 150 NSString* title = base::SysWideToNSString(parent->GetTitle()); |
155 if ([title length]) { // no title if root | 151 if ([title length]) { // no title if root |
156 [box addItemWithObjectValue:title]; | 152 [box addItemWithObjectValue:title]; |
157 [titleMapping_ setValue:[NSValue valueWithPointer:parent] forKey:title]; | 153 [parentMapping_ insertObject:[NSValue valueWithPointer:parent] |
| 154 atIndex:[box numberOfItems]-1]; |
158 } | 155 } |
159 for (int i = 0; i < parent->GetChildCount(); i++) { | 156 for (int i = 0; i < parent->GetChildCount(); i++) { |
160 const BookmarkNode* child = parent->GetChild(i); | 157 const BookmarkNode* child = parent->GetChild(i); |
161 if (child->is_folder()) | 158 if (child->is_folder()) |
162 [self addFolderNodes:child toComboBox:box]; | 159 [self addFolderNodes:child toComboBox:box]; |
163 } | 160 } |
164 } | 161 } |
165 | 162 |
166 // Look at the dialog; if the user has changed anything, update the | 163 // Look at the dialog; if the user has changed anything, update the |
167 // bookmark node to reflect this. | 164 // bookmark node to reflect this. |
168 - (void)updateBookmarkNode { | 165 - (void)updateBookmarkNode { |
169 if (!node_) return; | 166 if (!node_) return; |
170 | 167 |
171 // First the title... | 168 // First the title... |
172 NSString* oldTitle = base::SysWideToNSString(node_->GetTitle()); | 169 NSString* oldTitle = base::SysWideToNSString(node_->GetTitle()); |
173 NSString* newTitle = [nameTextField_ stringValue]; | 170 NSString* newTitle = [nameTextField_ stringValue]; |
174 if (![oldTitle isEqual:newTitle]) { | 171 if (![oldTitle isEqual:newTitle]) { |
175 model_->SetTitle(node_, base::SysNSStringToWide(newTitle)); | 172 model_->SetTitle(node_, base::SysNSStringToWide(newTitle)); |
176 UserMetrics::RecordAction(L"BookmarkBubble_ChangeTitleInBubble", | 173 UserMetrics::RecordAction(L"BookmarkBubble_ChangeTitleInBubble", |
177 model_->profile()); | 174 model_->profile()); |
178 } | 175 } |
179 // Then the parent folder. | 176 // Then the parent folder. |
180 NSString* oldParentTitle = base::SysWideToNSString( | 177 const BookmarkNode* oldParent = node_->GetParent(); |
181 node_->GetParent()->GetTitle()); | 178 NSInteger selectedIndex = [folderComboBox_ indexOfSelectedItem]; |
182 NSString* newParentTitle = [folderComboBox_ objectValueOfSelectedItem]; | 179 if (selectedIndex == -1) // No selection ever made. |
183 if (![oldParentTitle isEqual:newParentTitle]) { | 180 return; |
184 const BookmarkNode* newParent = static_cast<const BookmarkNode*>( | 181 |
185 [[titleMapping_ objectForKey:newParentTitle] pointerValue]); | 182 if ((NSUInteger)selectedIndex == [parentMapping_ count]) { |
186 if (newParent) { | 183 // "Choose another folder..." |
187 // newParent should only ever possibly be NULL in a unit test. | 184 return; |
| 185 } |
| 186 const BookmarkNode* newParent = static_cast<const BookmarkNode*>( |
| 187 [[parentMapping_ objectAtIndex:selectedIndex] pointerValue]); |
| 188 DCHECK(newParent); |
| 189 if (oldParent != newParent) { |
188 int index = newParent->GetChildCount(); | 190 int index = newParent->GetChildCount(); |
189 model_->Move(node_, newParent, index); | 191 model_->Move(node_, newParent, index); |
190 UserMetrics::RecordAction(L"BookmarkBubble_ChangeParent", | 192 UserMetrics::RecordAction(L"BookmarkBubble_ChangeParent", |
191 model_->profile()); | 193 model_->profile()); |
192 } | |
193 } | 194 } |
194 } | 195 } |
195 | 196 |
196 - (void)setTitle:(NSString*)title parentFolder:(NSString*)folder { | 197 - (void)setTitle:(NSString*)title parentFolder:(NSString*)folder { |
197 [nameTextField_ setStringValue:title]; | 198 [nameTextField_ setStringValue:title]; |
198 [folderComboBox_ selectItemWithObjectValue:folder]; | 199 [folderComboBox_ selectItemWithObjectValue:folder]; |
199 } | 200 } |
200 | 201 |
| 202 // Pick a specific parent node in the selection by finding the right |
| 203 // combo box index. |
| 204 - (void)setParentFolderSelection:(const BookmarkNode*)parent { |
| 205 // Expectation: we have a parent mapping for all items in the |
| 206 // folderComboBox except the last one ("Choose another folder..."). |
| 207 DCHECK((NSInteger)[parentMapping_ count] == |
| 208 [folderComboBox_ numberOfItems]-1); |
| 209 for (NSUInteger i = 0; i < [parentMapping_ count]; i++) { |
| 210 const BookmarkNode* possible = static_cast<const BookmarkNode*>( |
| 211 [[parentMapping_ objectAtIndex:i] pointerValue]); |
| 212 DCHECK(possible); |
| 213 if (possible == parent) { |
| 214 [folderComboBox_ selectItemAtIndex:i]; |
| 215 return; |
| 216 } |
| 217 } |
| 218 NOTREACHED(); |
| 219 } |
| 220 |
201 - (NSString*)chooseAnotherFolderString { | 221 - (NSString*)chooseAnotherFolderString { |
202 return chooseAnotherFolder_.get(); | 222 return chooseAnotherFolder_.get(); |
203 } | 223 } |
204 | 224 |
205 - (NSComboBox*)folderComboBox { | 225 - (NSComboBox*)folderComboBox { |
206 return folderComboBox_; | 226 return folderComboBox_; |
207 } | 227 } |
208 | 228 |
209 @end // implementation BookmarkBubbleController(ExposedForUnitTesting) | 229 @end // implementation BookmarkBubbleController(ExposedForUnitTesting) |
OLD | NEW |