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
25namespace WebCore {
26
27String DataObjectGtk::text()
28{
29    if (m_range)
30        return m_range->text();
31    return m_text;
32}
33
34String DataObjectGtk::markup()
35{
36    if (m_range)
37        createMarkup(m_range.get(), 0, AnnotateForInterchange);
38    return m_markup;
39}
40
41void DataObjectGtk::setText(const String& newText)
42{
43    m_range = 0;
44    m_text = newText;
45}
46
47void DataObjectGtk::setMarkup(const String& newMarkup)
48{
49    m_range = 0;
50    m_markup = newMarkup;
51}
52
53Vector<String> DataObjectGtk::files()
54{
55    Vector<KURL> uris(uriList());
56    Vector<String> files;
57
58    for (size_t i = 0; i < uris.size(); i++) {
59        KURL& uri = uris[0];
60        if (!uri.isValid() || !uri.isLocalFile())
61            continue;
62
63        files.append(uri.string());
64    }
65
66    return files;
67}
68
69String DataObjectGtk::url()
70{
71    Vector<KURL> uris(uriList());
72    for (size_t i = 0; i < uris.size(); i++) {
73        KURL& uri = uris[0];
74        if (uri.isValid())
75            return uri;
76    }
77
78    return String();
79}
80
81String DataObjectGtk::urlLabel()
82{
83    if (hasText())
84        return text();
85
86    if (hasURL())
87        return url();
88
89    return String();
90}
91
92bool DataObjectGtk::hasURL()
93{
94    return !url().isEmpty();
95}
96
97void DataObjectGtk::clear()
98{
99    m_text = "";
100    m_markup = "";
101    m_uriList.clear();
102    m_image = 0;
103    m_range = 0;
104}
105
106DataObjectGtk* DataObjectGtk::forClipboard(GtkClipboard* clipboard)
107{
108    static HashMap<GtkClipboard*, RefPtr<DataObjectGtk> > objectMap;
109
110    if (!objectMap.contains(clipboard)) {
111        RefPtr<DataObjectGtk> dataObject = DataObjectGtk::create();
112        objectMap.set(clipboard, dataObject);
113        return dataObject.get();
114    }
115
116    HashMap<GtkClipboard*, RefPtr<DataObjectGtk> >::iterator it = objectMap.find(clipboard);
117    return it->second.get();
118}
119
120}
121