[email protected] | af066a6a | 2009-08-17 18:01:55 | [diff] [blame] | 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 | // Download utility implementation for Mac OS X. |
| 6 | |
| 7 | #include "chrome/browser/cocoa/download_util_mac.h" |
| 8 | |
[email protected] | af066a6a | 2009-08-17 18:01:55 | [diff] [blame] | 9 | #include "base/sys_string_conversions.h" |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 10 | #import "chrome/browser/cocoa/dock_icon.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame^] | 11 | #include "chrome/browser/download/download_item.h" |
[email protected] | af066a6a | 2009-08-17 18:01:55 | [diff] [blame] | 12 | #include "chrome/browser/download/download_manager.h" |
[email protected] | 5c7293a | 2010-03-17 06:40:57 | [diff] [blame] | 13 | #include "gfx/native_widget_types.h" |
[email protected] | af066a6a | 2009-08-17 18:01:55 | [diff] [blame] | 14 | #include "skia/ext/skia_utils_mac.h" |
| 15 | |
| 16 | namespace download_util { |
| 17 | |
| 18 | void AddFileToPasteboard(NSPasteboard* pasteboard, const FilePath& path) { |
| 19 | // Write information about the file being dragged to the pasteboard. |
[email protected] | 74cda95 | 2009-11-19 04:48:57 | [diff] [blame] | 20 | NSString* file = base::SysUTF8ToNSString(path.value()); |
[email protected] | af066a6a | 2009-08-17 18:01:55 | [diff] [blame] | 21 | NSArray* fileList = [NSArray arrayWithObject:file]; |
| 22 | [pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType] |
| 23 | owner:nil]; |
| 24 | [pasteboard setPropertyList:fileList forType:NSFilenamesPboardType]; |
| 25 | } |
| 26 | |
[email protected] | 74cda95 | 2009-11-19 04:48:57 | [diff] [blame] | 27 | void NotifySystemOfDownloadComplete(const FilePath& path) { |
[email protected] | 5968af3 | 2009-11-23 17:57:54 | [diff] [blame] | 28 | NSString* filePath = base::SysUTF8ToNSString(path.value()); |
[email protected] | 74cda95 | 2009-11-19 04:48:57 | [diff] [blame] | 29 | [[NSDistributedNotificationCenter defaultCenter] |
| 30 | postNotificationName:@"com.apple.DownloadFileFinished" |
[email protected] | 5968af3 | 2009-11-23 17:57:54 | [diff] [blame] | 31 | object:filePath]; |
| 32 | |
| 33 | NSString* parentPath = [filePath stringByDeletingLastPathComponent]; |
| 34 | FNNotifyByPath( |
| 35 | reinterpret_cast<const UInt8*>([parentPath fileSystemRepresentation]), |
| 36 | kFNDirectoryModifiedMessage, |
| 37 | kNilOptions); |
[email protected] | 74cda95 | 2009-11-19 04:48:57 | [diff] [blame] | 38 | } |
| 39 | |
[email protected] | af066a6a | 2009-08-17 18:01:55 | [diff] [blame] | 40 | void DragDownload(const DownloadItem* download, |
| 41 | SkBitmap* icon, |
| 42 | gfx::NativeView view) { |
| 43 | NSPasteboard* pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard]; |
| 44 | AddFileToPasteboard(pasteboard, download->full_path()); |
| 45 | |
| 46 | // Convert to an NSImage. |
| 47 | NSImage* dragImage = gfx::SkBitmapToNSImage(*icon); |
| 48 | |
| 49 | // Synthesize a drag event, since we don't have access to the actual event |
| 50 | // that initiated a drag (possibly consumed by the DOM UI, for example). |
| 51 | NSPoint position = [[view window] mouseLocationOutsideOfEventStream]; |
| 52 | NSTimeInterval eventTime = [[NSApp currentEvent] timestamp]; |
| 53 | NSEvent* dragEvent = [NSEvent mouseEventWithType:NSLeftMouseDragged |
| 54 | location:position |
| 55 | modifierFlags:NSLeftMouseDraggedMask |
| 56 | timestamp:eventTime |
| 57 | windowNumber:[[view window] windowNumber] |
| 58 | context:nil |
| 59 | eventNumber:0 |
| 60 | clickCount:1 |
| 61 | pressure:1.0]; |
| 62 | |
| 63 | // Run the drag operation. |
| 64 | [[view window] dragImage:dragImage |
| 65 | at:position |
| 66 | offset:NSZeroSize |
| 67 | event:dragEvent |
| 68 | pasteboard:pasteboard |
| 69 | source:view |
| 70 | slideBack:YES]; |
| 71 | } |
| 72 | |
[email protected] | 6a7fb04 | 2010-02-01 16:30:47 | [diff] [blame] | 73 | void UpdateAppIconDownloadProgress(int download_count, |
| 74 | bool progress_known, |
| 75 | float progress) { |
| 76 | DockIcon* dock_icon = [DockIcon sharedDockIcon]; |
| 77 | [dock_icon setDownloads:download_count]; |
| 78 | [dock_icon setIndeterminate:!progress_known]; |
| 79 | [dock_icon setProgress:progress]; |
| 80 | [dock_icon updateIcon]; |
| 81 | } |
| 82 | |
[email protected] | af066a6a | 2009-08-17 18:01:55 | [diff] [blame] | 83 | } // namespace download_util |