blob: 936452049f978a241f9c045439c97b29256097cb [file] [log] [blame]
[email protected]af066a6a2009-08-17 18:01:551// 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]af066a6a2009-08-17 18:01:559#include "base/sys_string_conversions.h"
[email protected]6a7fb042010-02-01 16:30:4710#import "chrome/browser/cocoa/dock_icon.h"
[email protected]6c69796d2010-07-16 21:41:1611#include "chrome/browser/download/download_item.h"
[email protected]af066a6a2009-08-17 18:01:5512#include "chrome/browser/download/download_manager.h"
[email protected]5c7293a2010-03-17 06:40:5713#include "gfx/native_widget_types.h"
[email protected]af066a6a2009-08-17 18:01:5514#include "skia/ext/skia_utils_mac.h"
15
16namespace download_util {
17
18void AddFileToPasteboard(NSPasteboard* pasteboard, const FilePath& path) {
19 // Write information about the file being dragged to the pasteboard.
[email protected]74cda952009-11-19 04:48:5720 NSString* file = base::SysUTF8ToNSString(path.value());
[email protected]af066a6a2009-08-17 18:01:5521 NSArray* fileList = [NSArray arrayWithObject:file];
22 [pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
23 owner:nil];
24 [pasteboard setPropertyList:fileList forType:NSFilenamesPboardType];
25}
26
[email protected]74cda952009-11-19 04:48:5727void NotifySystemOfDownloadComplete(const FilePath& path) {
[email protected]5968af32009-11-23 17:57:5428 NSString* filePath = base::SysUTF8ToNSString(path.value());
[email protected]74cda952009-11-19 04:48:5729 [[NSDistributedNotificationCenter defaultCenter]
30 postNotificationName:@"com.apple.DownloadFileFinished"
[email protected]5968af32009-11-23 17:57:5431 object:filePath];
32
33 NSString* parentPath = [filePath stringByDeletingLastPathComponent];
34 FNNotifyByPath(
35 reinterpret_cast<const UInt8*>([parentPath fileSystemRepresentation]),
36 kFNDirectoryModifiedMessage,
37 kNilOptions);
[email protected]74cda952009-11-19 04:48:5738}
39
[email protected]af066a6a2009-08-17 18:01:5540void 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]6a7fb042010-02-01 16:30:4773void 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]af066a6a2009-08-17 18:01:5583} // namespace download_util