OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "app/os_exchange_data_provider_gtk.h" | |
6 | |
7 #include <algorithm> | |
8 | |
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" | |
13 | |
14 OSExchangeDataProviderGtk::OSExchangeDataProviderGtk( | |
15 int known_formats, | |
16 const std::set<GdkAtom>& known_custom_formats) | |
17 : known_formats_(known_formats), | |
18 known_custom_formats_(known_custom_formats), | |
19 formats_(0), | |
20 drag_image_(NULL) { | |
21 } | |
22 | |
23 OSExchangeDataProviderGtk::OSExchangeDataProviderGtk() | |
24 : known_formats_(0), | |
25 formats_(0), | |
26 drag_image_(NULL) { | |
27 } | |
28 | |
29 OSExchangeDataProviderGtk::~OSExchangeDataProviderGtk() { | |
30 if (drag_image_) | |
31 g_object_unref(drag_image_); | |
32 } | |
33 | |
34 bool OSExchangeDataProviderGtk::HasDataForAllFormats( | |
35 int formats, | |
36 const std::set<GdkAtom>& custom_formats) const { | |
37 if ((formats_ & formats) != formats) | |
38 return false; | |
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()) | |
42 return false; | |
43 } | |
44 return true; | |
45 } | |
46 | |
47 GtkTargetList* OSExchangeDataProviderGtk::GetTargetList() const { | |
48 GtkTargetList* targets = gtk_target_list_new(NULL, 0); | |
49 | |
50 if ((formats_ & OSExchangeData::STRING) != 0) | |
51 gtk_target_list_add_text_targets(targets, OSExchangeData::STRING); | |
52 | |
53 if ((formats_ & OSExchangeData::URL) != 0) { | |
54 gtk_target_list_add_uri_targets(targets, OSExchangeData::URL); | |
55 gtk_target_list_add( | |
56 targets, | |
57 gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::CHROME_NAMED_URL), | |
58 0, | |
59 OSExchangeData::URL); | |
60 } | |
61 | |
62 if ((formats_ & OSExchangeData::FILE_NAME) != 0) | |
63 gtk_target_list_add_uri_targets(targets, OSExchangeData::FILE_NAME); | |
64 | |
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); | |
68 } | |
69 | |
70 return targets; | |
71 } | |
72 | |
73 void OSExchangeDataProviderGtk::WriteFormatToSelection( | |
74 int format, | |
75 GtkSelectionData* selection) const { | |
76 if ((format & OSExchangeData::STRING) != 0) { | |
77 gtk_selection_data_set_text( | |
78 selection, | |
79 reinterpret_cast<const gchar*>(string_.c_str()), | |
80 -1); | |
81 } | |
82 | |
83 if ((format & OSExchangeData::URL) != 0) { | |
84 // TODO: this should be pulled out of TabContentsDragSource into a common | |
85 // place. | |
86 Pickle pickle; | |
87 pickle.WriteString(UTF16ToUTF8(title_)); | |
88 pickle.WriteString(url_.spec()); | |
89 gtk_selection_data_set( | |
90 selection, | |
91 gtk_dnd_util::GetAtomForTarget(gtk_dnd_util::CHROME_NAMED_URL), | |
92 8, | |
93 reinterpret_cast<const guchar*>(pickle.data()), | |
94 pickle.size()); | |
95 | |
96 gchar* uri_array[2]; | |
97 uri_array[0] = strdup(url_.spec().c_str()); | |
98 uri_array[1] = NULL; | |
99 gtk_selection_data_set_uris(selection, uri_array); | |
100 free(uri_array[0]); | |
101 } | |
102 | |
103 if ((format & OSExchangeData::FILE_NAME) != 0) { | |
104 gchar* uri_array[2]; | |
105 uri_array[0] = | |
106 strdup(net::FilePathToFileURL(FilePath(filename_)).spec().c_str()); | |
107 uri_array[1] = NULL; | |
108 gtk_selection_data_set_uris(selection, uri_array); | |
109 free(uri_array[0]); | |
110 } | |
111 | |
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( | |
117 selection, | |
118 i->first, | |
119 8, | |
120 reinterpret_cast<const guchar*>(data.data()), | |
121 data.size()); | |
122 } | |
123 } | |
124 } | |
125 | |
126 void OSExchangeDataProviderGtk::SetString(const std::wstring& data) { | |
127 string_ = WideToUTF16Hack(data); | |
128 formats_ |= OSExchangeData::STRING; | |
129 } | |
130 | |
131 void OSExchangeDataProviderGtk::SetURL(const GURL& url, | |
132 const std::wstring& title) { | |
133 url_ = url; | |
134 title_ = WideToUTF16Hack(title); | |
135 formats_ |= OSExchangeData::URL; | |
136 } | |
137 | |
138 void OSExchangeDataProviderGtk::SetFilename(const std::wstring& full_path) { | |
139 filename_ = WideToUTF8(full_path); | |
140 formats_ |= OSExchangeData::FILE_NAME; | |
141 } | |
142 | |
143 void OSExchangeDataProviderGtk::SetPickledData(GdkAtom format, | |
144 const Pickle& data) { | |
145 pickle_data_[format] = data; | |
146 formats_ |= OSExchangeData::PICKLED_DATA; | |
147 } | |
148 | |
149 bool OSExchangeDataProviderGtk::GetString(std::wstring* data) const { | |
150 if ((formats_ & OSExchangeData::STRING) == 0) | |
151 return false; | |
152 *data = UTF16ToWideHack(string_); | |
153 return true; | |
154 } | |
155 | |
156 bool OSExchangeDataProviderGtk::GetURLAndTitle(GURL* url, | |
157 std::wstring* title) const { | |
158 if ((formats_ & OSExchangeData::URL) == 0) { | |
159 title->clear(); | |
160 return GetPlainTextURL(url); | |
161 } | |
162 | |
163 if (!url_.is_valid()) | |
164 return false; | |
165 | |
166 *url = url_; | |
167 *title = UTF16ToWideHack(title_); | |
168 return true; | |
169 } | |
170 | |
171 bool OSExchangeDataProviderGtk::GetFilename(std::wstring* full_path) const { | |
172 if ((formats_ & OSExchangeData::FILE_NAME) == 0) | |
173 return false; | |
174 *full_path = UTF8ToWide(filename_); | |
175 return true; | |
176 } | |
177 | |
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()) | |
182 return false; | |
183 | |
184 *data = i->second; | |
185 return true; | |
186 } | |
187 | |
188 bool OSExchangeDataProviderGtk::HasString() const { | |
189 return (known_formats_ & OSExchangeData::STRING) != 0 || | |
190 (formats_ & OSExchangeData::STRING) != 0; | |
191 } | |
192 | |
193 bool OSExchangeDataProviderGtk::HasURL() const { | |
194 if ((known_formats_ & OSExchangeData::URL) != 0 || | |
195 (formats_ & OSExchangeData::URL) != 0) { | |
196 return true; | |
197 } | |
198 // No URL, see if we have plain text that can be parsed as a URL. | |
199 return GetPlainTextURL(NULL); | |
200 } | |
201 | |
202 bool OSExchangeDataProviderGtk::HasFile() const { | |
203 return (known_formats_ & OSExchangeData::FILE_NAME) != 0 || | |
204 (formats_ & OSExchangeData::FILE_NAME) != 0; | |
205 } | |
206 | |
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(); | |
210 } | |
211 | |
212 bool OSExchangeDataProviderGtk::GetPlainTextURL(GURL* url) const { | |
213 if ((formats_ & OSExchangeData::STRING) == 0) | |
214 return false; | |
215 | |
216 GURL test_url(string_); | |
217 if (!test_url.is_valid()) | |
218 return false; | |
219 | |
220 if (url) | |
221 *url = test_url; | |
222 return true; | |
223 } | |
224 | |
225 void OSExchangeDataProviderGtk::SetDragImage(GdkPixbuf* drag_image, | |
226 const gfx::Point& cursor_offset) { | |
227 if (drag_image_) | |
228 g_object_unref(drag_image_); | |
229 g_object_ref(drag_image); | |
230 drag_image_ = drag_image; | |
231 cursor_offset_ = cursor_offset; | |
232 } | |
233 | |
234 /////////////////////////////////////////////////////////////////////////////// | |
235 // OSExchangeData, public: | |
236 | |
237 // static | |
238 OSExchangeData::Provider* OSExchangeData::CreateProvider() { | |
239 return new OSExchangeDataProviderGtk(); | |
240 } | |
241 | |
242 GdkAtom OSExchangeData::RegisterCustomFormat(const std::string& type) { | |
243 return gdk_atom_intern(type.c_str(), false); | |
244 } | |
OLD | NEW |