initial import
[vuplus_webkit] / Source / WebCore / platform / gtk / DataObjectGtk.cpp
1 /*
2  * Copyright (C) 2009, Martin Robinson
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "DataObjectGtk.h"
21
22 #include "markup.h"
23 #include <gtk/gtk.h>
24 #include <wtf/gobject/GOwnPtr.h>
25 #include <wtf/text/StringBuilder.h>
26
27 namespace WebCore {
28
29 static void replaceNonBreakingSpaceWithSpace(String& str)
30 {
31     static const UChar NonBreakingSpaceCharacter = 0xA0;
32     static const UChar SpaceCharacter = ' ';
33     str.replace(NonBreakingSpaceCharacter, SpaceCharacter);
34 }
35
36 String DataObjectGtk::text()
37 {
38     if (m_range)
39         return m_range->text();
40     return m_text;
41 }
42
43 String DataObjectGtk::markup()
44 {
45     if (m_range)
46         return createMarkup(m_range.get(), 0, AnnotateForInterchange, false, ResolveNonLocalURLs);
47     return m_markup;
48 }
49
50 void DataObjectGtk::setText(const String& newText)
51 {
52     m_range = 0;
53     m_text = newText;
54     replaceNonBreakingSpaceWithSpace(m_text);
55 }
56
57 void DataObjectGtk::setMarkup(const String& newMarkup)
58 {
59     m_range = 0;
60     m_markup = newMarkup;
61 }
62
63 void DataObjectGtk::setURIList(const String& uriListString)
64 {
65     m_uriList = uriListString;
66
67     // This code is originally from: platform/chromium/ChromiumDataObject.cpp.
68     // FIXME: We should make this code cross-platform eventually.
69
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);
74
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
79     // from the URI list.
80     bool setURL = false;
81     for (size_t i = 0; i < uriList.size(); ++i) {
82         String& line = uriList[i];
83         line = line.stripWhiteSpace();
84         if (line.isEmpty())
85             continue;
86         if (line[0] == '#')
87             continue;
88
89         KURL url = KURL(KURL(), line);
90         if (url.isValid()) {
91             if (!setURL) {
92                 m_url = url;
93                 setURL = true;
94             }
95
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()));
100         }
101     }
102 }
103
104 void DataObjectGtk::setURL(const KURL& url, const String& label)
105 {
106     m_url = url;
107     m_uriList = url;
108     setText(url.string());
109
110     String actualLabel(label);
111     if (actualLabel.isEmpty())
112         actualLabel = url;
113
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());
122 }
123
124 void DataObjectGtk::clearText()
125 {
126     m_range = 0;
127     m_text = "";
128 }
129
130 void DataObjectGtk::clearMarkup()
131 {
132     m_range = 0;
133     m_markup = "";
134 }
135
136 String DataObjectGtk::urlLabel()
137 {
138     if (hasText())
139         return text();
140
141     if (hasURL())
142         return url();
143
144     return String();
145 }
146
147 void DataObjectGtk::clear()
148 {
149     m_text = "";
150     m_markup = "";
151     m_uriList = "";
152     m_url = KURL();
153     m_image = 0;
154     m_range = 0;
155
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)."
160 }
161
162 DataObjectGtk* DataObjectGtk::forClipboard(GtkClipboard* clipboard)
163 {
164     static HashMap<GtkClipboard*, RefPtr<DataObjectGtk> > objectMap;
165
166     if (!objectMap.contains(clipboard)) {
167         RefPtr<DataObjectGtk> dataObject = DataObjectGtk::create();
168         objectMap.set(clipboard, dataObject);
169         return dataObject.get();
170     }
171
172     HashMap<GtkClipboard*, RefPtr<DataObjectGtk> >::iterator it = objectMap.find(clipboard);
173     return it->second.get();
174 }
175
176 }