1 // Copyright (c) 2010 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 "app/os_exchange_data_provider_gtk.h"
9 #include "app/gtk_dnd_util.h"
10 #include "base/file_path.h"
11 #include "base/utf_string_conversions.h"
12 #include "net/base/net_util.h"
14 OSExchangeDataProviderGtk::OSExchangeDataProviderGtk(
16 const std::set
<GdkAtom
>& known_custom_formats
)
17 : known_formats_(known_formats
),
18 known_custom_formats_(known_custom_formats
),
23 OSExchangeDataProviderGtk::OSExchangeDataProviderGtk()
29 OSExchangeDataProviderGtk::~OSExchangeDataProviderGtk() {
31 g_object_unref(drag_image_
);
34 bool OSExchangeDataProviderGtk::HasDataForAllFormats(
36 const std::set
<GdkAtom
>& custom_formats
) const {
37 if ((formats_
& formats
) != formats
)
39 for (std::set
<GdkAtom
>::iterator i
= custom_formats
.begin();
40 i
!= custom_formats
.end(); ++i
) {
41 if (pickle_data_
.find(*i
) == pickle_data_
.end())
47 GtkTargetList
* OSExchangeDataProviderGtk::GetTargetList() const {
48 GtkTargetList
* targets
= gtk_target_list_new(NULL
, 0);
50 if ((formats_
& OSExchangeData::STRING
) != 0)
51 gtk_target_list_add_text_targets(targets
, OSExchangeData::STRING
);
53 if ((formats_
& OSExchangeData::URL
) != 0) {
54 gtk_target_list_add_uri_targets(targets
, OSExchangeData::URL
);
57 gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::CHROME_NAMED_URL
),
62 if ((formats_
& OSExchangeData::FILE_NAME
) != 0)
63 gtk_target_list_add_uri_targets(targets
, OSExchangeData::FILE_NAME
);
65 for (PickleData::const_iterator i
= pickle_data_
.begin();
66 i
!= pickle_data_
.end(); ++i
) {
67 gtk_target_list_add(targets
, i
->first
, 0, OSExchangeData::PICKLED_DATA
);
73 void OSExchangeDataProviderGtk::WriteFormatToSelection(
75 GtkSelectionData
* selection
) const {
76 if ((format
& OSExchangeData::STRING
) != 0) {
77 gtk_selection_data_set_text(
79 reinterpret_cast<const gchar
*>(string_
.c_str()),
83 if ((format
& OSExchangeData::URL
) != 0) {
84 // TODO: this should be pulled out of TabContentsDragSource into a common
87 pickle
.WriteString(UTF16ToUTF8(title_
));
88 pickle
.WriteString(url_
.spec());
89 gtk_selection_data_set(
91 gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::CHROME_NAMED_URL
),
93 reinterpret_cast<const guchar
*>(pickle
.data()),
97 uri_array
[0] = strdup(url_
.spec().c_str());
99 gtk_selection_data_set_uris(selection
, uri_array
);
103 if ((format
& OSExchangeData::FILE_NAME
) != 0) {
106 strdup(net::FilePathToFileURL(FilePath(filename_
)).spec().c_str());
108 gtk_selection_data_set_uris(selection
, uri_array
);
112 if ((format
& OSExchangeData::PICKLED_DATA
) != 0) {
113 for (PickleData::const_iterator i
= pickle_data_
.begin();
114 i
!= pickle_data_
.end(); ++i
) {
115 const Pickle
& data
= i
->second
;
116 gtk_selection_data_set(
120 reinterpret_cast<const guchar
*>(data
.data()),
126 void OSExchangeDataProviderGtk::SetString(const std::wstring
& data
) {
127 string_
= WideToUTF16Hack(data
);
128 formats_
|= OSExchangeData::STRING
;
131 void OSExchangeDataProviderGtk::SetURL(const GURL
& url
,
132 const std::wstring
& title
) {
134 title_
= WideToUTF16Hack(title
);
135 formats_
|= OSExchangeData::URL
;
138 void OSExchangeDataProviderGtk::SetFilename(const std::wstring
& full_path
) {
139 filename_
= WideToUTF8(full_path
);
140 formats_
|= OSExchangeData::FILE_NAME
;
143 void OSExchangeDataProviderGtk::SetPickledData(GdkAtom format
,
144 const Pickle
& data
) {
145 pickle_data_
[format
] = data
;
146 formats_
|= OSExchangeData::PICKLED_DATA
;
149 bool OSExchangeDataProviderGtk::GetString(std::wstring
* data
) const {
150 if ((formats_
& OSExchangeData::STRING
) == 0)
152 *data
= UTF16ToWideHack(string_
);
156 bool OSExchangeDataProviderGtk::GetURLAndTitle(GURL
* url
,
157 std::wstring
* title
) const {
158 if ((formats_
& OSExchangeData::URL
) == 0) {
160 return GetPlainTextURL(url
);
163 if (!url_
.is_valid())
167 *title
= UTF16ToWideHack(title_
);
171 bool OSExchangeDataProviderGtk::GetFilename(std::wstring
* full_path
) const {
172 if ((formats_
& OSExchangeData::FILE_NAME
) == 0)
174 *full_path
= UTF8ToWide(filename_
);
178 bool OSExchangeDataProviderGtk::GetPickledData(GdkAtom format
,
179 Pickle
* data
) const {
180 PickleData::const_iterator i
= pickle_data_
.find(format
);
181 if (i
== pickle_data_
.end())
188 bool OSExchangeDataProviderGtk::HasString() const {
189 return (known_formats_
& OSExchangeData::STRING
) != 0 ||
190 (formats_
& OSExchangeData::STRING
) != 0;
193 bool OSExchangeDataProviderGtk::HasURL() const {
194 if ((known_formats_
& OSExchangeData::URL
) != 0 ||
195 (formats_
& OSExchangeData::URL
) != 0) {
198 // No URL, see if we have plain text that can be parsed as a URL.
199 return GetPlainTextURL(NULL
);
202 bool OSExchangeDataProviderGtk::HasFile() const {
203 return (known_formats_
& OSExchangeData::FILE_NAME
) != 0 ||
204 (formats_
& OSExchangeData::FILE_NAME
) != 0;
207 bool OSExchangeDataProviderGtk::HasCustomFormat(GdkAtom format
) const {
208 return known_custom_formats_
.find(format
) != known_custom_formats_
.end() ||
209 pickle_data_
.find(format
) != pickle_data_
.end();
212 bool OSExchangeDataProviderGtk::GetPlainTextURL(GURL
* url
) const {
213 if ((formats_
& OSExchangeData::STRING
) == 0)
216 GURL
test_url(string_
);
217 if (!test_url
.is_valid())
225 void OSExchangeDataProviderGtk::SetDragImage(GdkPixbuf
* drag_image
,
226 const gfx::Point
& cursor_offset
) {
228 g_object_unref(drag_image_
);
229 g_object_ref(drag_image
);
230 drag_image_
= drag_image
;
231 cursor_offset_
= cursor_offset
;
234 ///////////////////////////////////////////////////////////////////////////////
235 // OSExchangeData, public:
238 OSExchangeData::Provider
* OSExchangeData::CreateProvider() {
239 return new OSExchangeDataProviderGtk();
242 GdkAtom
OSExchangeData::RegisterCustomFormat(const std::string
& type
) {
243 return gdk_atom_intern(type
.c_str(), false);