1 // Copyright 2013 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.
5 #include "chrome/browser/ui/gtk/download/download_item_drag.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/download/drag_download_item.h"
10 #include "content/public/browser/download_item.h"
11 #include "net/base/net_util.h"
12 #include "ui/base/dragdrop/gtk_dnd_util.h"
13 #include "ui/gfx/image/image.h"
16 using content::DownloadItem;
20 const int kDownloadItemCodeMask = ui::TEXT_URI_LIST | ui::CHROME_NAMED_URL;
21 const GdkDragAction kDownloadItemDragAction = GDK_ACTION_COPY;
25 // Stores metadata for a drag & drop operation.
26 class DownloadItemDrag::DragData {
28 // Constructs a DragData object based on the current state of |item|.
29 explicit DragData(const DownloadItem* item);
31 // Sets up a drag source and connects |drag_data| to 'drag-data-get' on
32 // |widget|. If |icon| is non-NULL it will be used as the drag icon. The
33 // object pointed to by |drag_data| will be deleted when the signal is
35 static void AttachToWidget(scoped_ptr<DragData> drag_data,
39 // 'drag-data-get' signal handler.
40 CHROMEGTK_CALLBACK_4(DragData, void, OnDragDataGet, GdkDragContext*,
41 GtkSelectionData*, guint, guint);
44 // GClosureNotify handler for destroying a DragData object. |data| is assumed
46 static void OnDestroy(gpointer data, GClosure* closure);
49 base::string16 display_name_;
52 DownloadItemDrag::DragData::DragData(const DownloadItem* item)
53 : url_(net::FilePathToFileURL(item->GetTargetFilePath())),
54 display_name_(item->GetFileNameToReportUser().LossyDisplayName()) {
55 DCHECK_EQ(DownloadItem::COMPLETE, item->GetState());
59 void DownloadItemDrag::DragData::AttachToWidget(scoped_ptr<DragData> drag_data,
63 widget, GDK_BUTTON1_MASK, NULL, 0, kDownloadItemDragAction);
64 ui::SetSourceTargetListFromCodeMask(widget, kDownloadItemCodeMask);
66 // Disconnect previous signal handlers, if any.
67 g_signal_handlers_disconnect_matched(
73 reinterpret_cast<gpointer>(&OnDragDataGetThunk),
76 // Connect new signal handlers.
77 g_signal_connect_data(widget,
79 G_CALLBACK(&OnDragDataGetThunk),
80 reinterpret_cast<gpointer>(drag_data.release()),
82 static_cast<GConnectFlags>(0));
85 gtk_drag_source_set_icon_pixbuf(widget, icon->ToGdkPixbuf());
88 void DownloadItemDrag::DragData::OnDragDataGet(GtkWidget* widget,
89 GdkDragContext* context,
90 GtkSelectionData* selection_data,
93 ui::WriteURLWithName(selection_data, url_, display_name_, target_type);
97 void DownloadItemDrag::DragData::OnDestroy(gpointer data, GClosure* closure) {
98 DragData* drag_data = reinterpret_cast<DragData*>(data);
102 // DownloadItemDrag ------------------------------------------------------------
105 void DownloadItemDrag::SetSource(GtkWidget* widget,
106 const DownloadItem* item,
108 scoped_ptr<DragData> drag_data(new DragData(item));
109 DragData::AttachToWidget(drag_data.Pass(), widget, icon);
112 DownloadItemDrag::DownloadItemDrag(const DownloadItem* item, gfx::Image* icon)
113 : CustomDrag(icon, kDownloadItemCodeMask, kDownloadItemDragAction),
114 drag_data_(new DragData(item)) {}
116 DownloadItemDrag::~DownloadItemDrag() {}
118 void DownloadItemDrag::OnDragDataGet(GtkWidget* widget,
119 GdkDragContext* context,
120 GtkSelectionData* selection_data,
123 drag_data_->OnDragDataGet(widget, context, selection_data, target_type, time);
126 void DragDownloadItem(const content::DownloadItem* download,
128 gfx::NativeView view) {
129 // This starts the drag process, the lifetime of this object is tied to the
131 new DownloadItemDrag(download, icon);