2 * Copyright (C) 2009, Martin Robinson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "DataObjectGtk.h"
24 #include <wtf/gobject/GOwnPtr.h>
25 #include <wtf/text/StringBuilder.h>
29 static void replaceNonBreakingSpaceWithSpace(String& str)
31 static const UChar NonBreakingSpaceCharacter = 0xA0;
32 static const UChar SpaceCharacter = ' ';
33 str.replace(NonBreakingSpaceCharacter, SpaceCharacter);
36 String DataObjectGtk::text()
39 return m_range->text();
43 String DataObjectGtk::markup()
46 return createMarkup(m_range.get(), 0, AnnotateForInterchange, false, ResolveNonLocalURLs);
50 void DataObjectGtk::setText(const String& newText)
54 replaceNonBreakingSpaceWithSpace(m_text);
57 void DataObjectGtk::setMarkup(const String& newMarkup)
63 void DataObjectGtk::setURIList(const String& uriListString)
65 m_uriList = uriListString;
67 // This code is originally from: platform/chromium/ChromiumDataObject.cpp.
68 // FIXME: We should make this code cross-platform eventually.
70 // Line separator is \r\n per RFC 2483 - however, for compatibility
71 // reasons we also allow just \n here.
72 Vector<String> uriList;
73 uriListString.split('\n', uriList);
75 // Process the input and copy the first valid URL into the url member.
76 // In case no URLs can be found, subsequent calls to getData("URL")
77 // will get an empty string. This is in line with the HTML5 spec (see
78 // "The DragEvent and DataTransfer interfaces"). Also extract all filenames
81 for (size_t i = 0; i < uriList.size(); ++i) {
82 String& line = uriList[i];
83 line = line.stripWhiteSpace();
89 KURL url = KURL(KURL(), line);
96 GOwnPtr<GError> error;
97 GOwnPtr<gchar> filename(g_filename_from_uri(line.utf8().data(), 0, &error.outPtr()));
98 if (!error && filename)
99 m_filenames.append(String::fromUTF8(filename.get()));
104 void DataObjectGtk::setURL(const KURL& url, const String& label)
108 setText(url.string());
110 String actualLabel(label);
111 if (actualLabel.isEmpty())
114 StringBuilder markup;
115 markup.append("<a href=\"");
116 markup.append(url.string());
117 markup.append("\">");
118 GOwnPtr<gchar> escaped(g_markup_escape_text(actualLabel.utf8().data(), -1));
119 markup.append(String::fromUTF8(escaped.get()));
120 markup.append("</a>");
121 setMarkup(markup.toString());
124 void DataObjectGtk::clearText()
130 void DataObjectGtk::clearMarkup()
136 String DataObjectGtk::urlLabel()
147 void DataObjectGtk::clear()
156 // We do not clear filenames. According to the spec: "The clearData() method
157 // does not affect whether any files were included in the drag, so the types
158 // attribute's list might still not be empty after calling clearData() (it would
159 // still contain the "Files" string if any files were included in the drag)."
162 DataObjectGtk* DataObjectGtk::forClipboard(GtkClipboard* clipboard)
164 static HashMap<GtkClipboard*, RefPtr<DataObjectGtk> > objectMap;
166 if (!objectMap.contains(clipboard)) {
167 RefPtr<DataObjectGtk> dataObject = DataObjectGtk::create();
168 objectMap.set(clipboard, dataObject);
169 return dataObject.get();
172 HashMap<GtkClipboard*, RefPtr<DataObjectGtk> >::iterator it = objectMap.find(clipboard);
173 return it->second.get();