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 "chrome/browser/cocoa/download_item_mac.h" | 5 #include "chrome/browser/cocoa/download_item_mac.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #import "chrome/browser/cocoa/download_item_controller.h" |
8 #include "base/string_util.h" | |
9 #include "base/sys_string_conversions.h" | |
10 #import "chrome/browser/cocoa/download_shelf_controller.h" | |
11 #import "chrome/browser/cocoa/download_shelf_mac.h" | |
12 #import "chrome/browser/cocoa/download_shelf_view.h" | |
13 #include "chrome/browser/download/download_item_model.h" | 8 #include "chrome/browser/download/download_item_model.h" |
14 #include "chrome/browser/download/download_manager.h" | |
15 #include "chrome/browser/download/download_shelf.h" | |
16 | |
17 // A class for the chromium-side part of the download shelf context menu. | |
18 | |
19 class DownloadShelfContextMenuMac : public DownloadShelfContextMenu { | |
20 public: | |
21 DownloadShelfContextMenuMac(BaseDownloadItemModel* model, | |
22 DownloadShelfContextMenuBridge* bridge) | |
23 : DownloadShelfContextMenu(model), bridge_(bridge) { | |
24 } | |
25 | |
26 NSMenu* GetCocoaMenu(); | |
27 | |
28 using DownloadShelfContextMenu::ExecuteItemCommand; | |
29 | |
30 private: | |
31 DownloadShelfContextMenuBridge* bridge_; // weak, owns us | |
32 }; | |
33 | |
34 NSMenu* DownloadShelfContextMenuMac::GetCocoaMenu() { | |
35 // TODO(thakis): win/gtk show slightly different menus when the download is | |
36 // in progress/done (mainly the first item) | |
37 // TODO(thakis): this probably wants to be in a xib file or at least use | |
38 // localized strings | |
39 | |
40 NSMenuItem* item; | |
41 NSMenu* menu = | |
42 [[[NSMenu alloc] initWithTitle:@"DownloadItemPopup"] autorelease]; | |
43 SEL action = @selector(performAction:); | |
44 | |
45 item = [menu addItemWithTitle:@"Open" action:action keyEquivalent:@""]; | |
46 // [item addItemWithTitle:@"Open when complete" ...]; // In-progress text. | |
47 [item setTag:OPEN_WHEN_COMPLETE]; | |
48 [item setTarget:bridge_]; | |
49 | |
50 // TODO(thakis): Set correct checkbox state, make this a checkbox item | |
51 item = [menu addItemWithTitle:@"Always open type" | |
52 action:action | |
53 keyEquivalent:@""]; | |
54 [item setTag:ALWAYS_OPEN_TYPE]; | |
55 [item setTarget:bridge_]; | |
56 | |
57 [menu addItem:[NSMenuItem separatorItem]]; | |
58 | |
59 item = [menu addItemWithTitle:@"Reveal in Finder" | |
60 action:action | |
61 keyEquivalent:@""]; | |
62 [item setTag:SHOW_IN_FOLDER]; | |
63 [item setTarget:bridge_]; | |
64 | |
65 [menu addItem:[NSMenuItem separatorItem]]; | |
66 | |
67 item = [menu addItemWithTitle:@"Cancel" action:action keyEquivalent:@""]; | |
68 [item setTag:CANCEL]; | |
69 [item setTarget:bridge_]; | |
70 | |
71 return menu; | |
72 } | |
73 | |
74 | |
75 // A class for the cocoa side of the download shelf context menu. | |
76 | |
77 @interface DownloadShelfContextMenuBridge : NSObject { | |
78 @private | |
79 scoped_ptr<DownloadShelfContextMenuMac> contextMenu_; | |
80 } | |
81 | |
82 - (DownloadShelfContextMenuBridge*)initWithModel:(BaseDownloadItemModel*)model; | |
83 @end | |
84 | |
85 @interface DownloadShelfContextMenuBridge(Private) | |
86 - (void)performAction:(id)sender; | |
87 - (NSMenu*)menu; | |
88 @end | |
89 | |
90 @implementation DownloadShelfContextMenuBridge | |
91 | |
92 - (DownloadShelfContextMenuBridge*)initWithModel:(BaseDownloadItemModel*)model { | |
93 if ((self = [super init]) == nil) { | |
94 return nil; | |
95 } | |
96 contextMenu_.reset(new DownloadShelfContextMenuMac(model, self)); | |
97 return self; | |
98 } | |
99 | |
100 @end | |
101 | |
102 @implementation DownloadShelfContextMenuBridge(Private) | |
103 | |
104 - (void)performAction:(id)sender { | |
105 contextMenu_->ExecuteItemCommand([sender tag]); | |
106 } | |
107 | |
108 - (NSMenu*)menu { | |
109 return contextMenu_->GetCocoaMenu(); | |
110 } | |
111 | |
112 @end | |
113 | |
114 | 9 |
115 // DownloadItemMac ------------------------------------------------------------- | 10 // DownloadItemMac ------------------------------------------------------------- |
116 | 11 |
117 DownloadItemMac::DownloadItemMac(BaseDownloadItemModel* download_model, | 12 DownloadItemMac::DownloadItemMac(BaseDownloadItemModel* download_model, |
118 NSRect frame, | 13 DownloadItemController* controller) |
119 DownloadShelfController* parent) | 14 : download_model_(download_model), item_controller_(controller) { |
120 : download_model_(download_model), parent_(parent) { | |
121 download_model_->download()->AddObserver(this); | 15 download_model_->download()->AddObserver(this); |
122 | |
123 // TODO(thakis): The windows version of this does all kinds of things | |
124 // (gratituous use of animation, special handling of dangerous downloads) | |
125 // that we don't currently do. | |
126 | |
127 scoped_nsobject<NSPopUpButton> view( | |
128 [[NSPopUpButton alloc] initWithFrame:frame pullsDown:YES]); | |
129 [parent_ addDownloadItem:view.get()]; | |
130 | |
131 FilePath download_path = download_model->download()->GetFileName(); | |
132 | |
133 // TODO(thakis): use filename eliding like gtk/windows versions | |
134 NSString* titleString = base::SysWideToNSString( | |
135 download_path.ToWStringHack()); | |
136 | |
137 menu_.reset([[DownloadShelfContextMenuBridge alloc] | |
138 initWithModel:download_model_.get()]); | |
139 [view.get() setMenu:[menu_.get() menu]]; | |
140 | |
141 [view.get() insertItemWithTitle:titleString atIndex:0]; | |
142 | |
143 NSString* extension = base::SysUTF8ToNSString(download_path.Extension()); | |
144 [[view.get() itemAtIndex:0] setImage: | |
145 [[NSWorkspace sharedWorkspace] iconForFileType:extension]]; | |
146 } | 16 } |
147 | 17 |
148 DownloadItemMac::~DownloadItemMac() { | 18 DownloadItemMac::~DownloadItemMac() { |
149 download_model_->download()->RemoveObserver(this); | 19 download_model_->download()->RemoveObserver(this); |
150 } | 20 } |
151 | 21 |
152 void DownloadItemMac::OnDownloadUpdated(DownloadItem* download) { | 22 void DownloadItemMac::OnDownloadUpdated(DownloadItem* download) { |
153 DCHECK_EQ(download, download_model_->download()); | 23 DCHECK_EQ(download, download_model_->download()); |
154 | 24 |
155 std::wstring status_text = download_model_->GetStatusText(); | 25 [item_controller_ setStateFromDownload:download_model_.get()]; |
156 // Remove the status text label. | |
157 if (status_text.empty()) { | |
158 // TODO(thakis): Once there is a status label, hide it here | |
159 return; | |
160 } | |
161 | |
162 // TODO(thakis): Set status_text as status label | |
163 } | 26 } |
OLD | NEW |