Home | History | Annotate | Download | only in download
      1 // Copyright (c) 2011 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/ui/cocoa/download/download_util_mac.h"
      8 
      9 #include "base/sys_string_conversions.h"
     10 #include "chrome/browser/download/download_item.h"
     11 #include "chrome/browser/download/download_manager.h"
     12 #import "chrome/browser/ui/cocoa/dock_icon.h"
     13 #include "ui/gfx/image.h"
     14 #include "ui/gfx/native_widget_types.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.
     20   NSString* file = base::SysUTF8ToNSString(path.value());
     21   NSArray* fileList = [NSArray arrayWithObject:file];
     22   [pasteboard declareTypes:[NSArray arrayWithObject:NSFilenamesPboardType]
     23                      owner:nil];
     24   [pasteboard setPropertyList:fileList forType:NSFilenamesPboardType];
     25 }
     26 
     27 void NotifySystemOfDownloadComplete(const FilePath& path) {
     28   NSString* filePath = base::SysUTF8ToNSString(path.value());
     29   [[NSDistributedNotificationCenter defaultCenter]
     30       postNotificationName:@"com.apple.DownloadFileFinished"
     31                     object:filePath];
     32 
     33   NSString* parentPath = [filePath stringByDeletingLastPathComponent];
     34   FNNotifyByPath(
     35       reinterpret_cast<const UInt8*>([parentPath fileSystemRepresentation]),
     36       kFNDirectoryModifiedMessage,
     37       kNilOptions);
     38 }
     39 
     40 void DragDownload(const DownloadItem* download,
     41                   gfx::Image* icon,
     42                   gfx::NativeView view) {
     43   NSPasteboard* pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
     44   AddFileToPasteboard(pasteboard, download->full_path());
     45 
     46   // Synthesize a drag event, since we don't have access to the actual event
     47   // that initiated a drag (possibly consumed by the Web UI, for example).
     48   NSPoint position = [[view window] mouseLocationOutsideOfEventStream];
     49   NSTimeInterval eventTime = [[NSApp currentEvent] timestamp];
     50   NSEvent* dragEvent = [NSEvent mouseEventWithType:NSLeftMouseDragged
     51                                           location:position
     52                                      modifierFlags:NSLeftMouseDraggedMask
     53                                          timestamp:eventTime
     54                                       windowNumber:[[view window] windowNumber]
     55                                            context:nil
     56                                        eventNumber:0
     57                                         clickCount:1
     58                                           pressure:1.0];
     59 
     60   // Run the drag operation.
     61   [[view window] dragImage:*icon
     62                         at:position
     63                     offset:NSZeroSize
     64                      event:dragEvent
     65                 pasteboard:pasteboard
     66                     source:view
     67                  slideBack:YES];
     68 }
     69 
     70 void UpdateAppIconDownloadProgress(int download_count,
     71                                    bool progress_known,
     72                                    float progress) {
     73   DockIcon* dock_icon = [DockIcon sharedDockIcon];
     74   [dock_icon setDownloads:download_count];
     75   [dock_icon setIndeterminate:!progress_known];
     76   [dock_icon setProgress:progress];
     77   [dock_icon updateIcon];
     78 }
     79 
     80 }  // namespace download_util
     81