initial import
[vuplus_webkit] / Source / WebCore / platform / qt / DataTransferItemQt.cpp
1 /*
2  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26
27 #include "config.h"
28 #include "DataTransferItemQt.h"
29
30 #if ENABLE(DATA_TRANSFER_ITEMS)
31
32 #include "Blob.h"
33 #include "Clipboard.h"
34 #include "NotImplemented.h"
35 #include "StringCallback.h"
36 #include <QApplication>
37 #include <QClipboard>
38 #include <QMimeData>
39 #include <QTextCodec>
40
41 namespace WebCore {
42
43 PassRefPtr<DataTransferItem> DataTransferItem::create(PassRefPtr<Clipboard> owner,
44                                                       ScriptExecutionContext* context,
45                                                       const String& data,
46                                                       const String& type)
47 {
48     return DataTransferItemQt::create(owner, context, type, data);
49 }
50
51 PassRefPtr<DataTransferItemQt> DataTransferItemQt::createFromPasteboard(PassRefPtr<Clipboard> owner,
52                                                                         ScriptExecutionContext* context,
53                                                                         const String& type)
54 {
55     if (type == "text/plain" || type == "text/html")
56         return adoptRef(new DataTransferItemQt(owner, context, PasteboardSource, DataTransferItem::kindString, type, ""));
57
58     return adoptRef(new DataTransferItemQt(owner, context, PasteboardSource, DataTransferItem::kindFile, type, ""));
59 }
60
61 PassRefPtr<DataTransferItemQt> DataTransferItemQt::create(PassRefPtr<Clipboard> owner,
62                                                           ScriptExecutionContext* context,
63                                                           const String& type,
64                                                           const String& data)
65 {
66     return adoptRef(new DataTransferItemQt(owner, context, InternalSource, DataTransferItem::kindString, type, data));
67 }
68
69 DataTransferItemQt::DataTransferItemQt(PassRefPtr<Clipboard> owner,
70                                        ScriptExecutionContext* context,
71                                        DataSource source,
72                                        const String& kind, const String& type,
73                                        const String& data)
74     : DataTransferItem(owner, kind, type)
75     , m_context(context)
76     , m_dataSource(source)
77     , m_data(data)
78 {
79 }
80
81 void DataTransferItemQt::getAsString(PassRefPtr<StringCallback> callback)
82 {
83     if ((owner()->policy() != ClipboardReadable && owner()->policy() != ClipboardWritable)
84         || kind() != kindString)
85         return;
86
87     if (m_dataSource == InternalSource) {
88         callback->scheduleCallback(m_context, m_data);
89         return;
90     }
91
92     const QMimeData* mimeData = QApplication::clipboard()->mimeData(QClipboard::Clipboard);
93     if (!mimeData)
94         return;
95
96     QString data;
97     if (type() == "text/plain")
98         data = mimeData->text();
99     else if (type() == "text/html")
100         data = mimeData->html();
101     else {
102         QByteArray rawData = mimeData->data(type());
103         data = QTextCodec::codecForName("UTF-16")->toUnicode(rawData);
104     }
105
106     callback->scheduleCallback(m_context, data);
107 }
108
109 PassRefPtr<Blob> DataTransferItemQt::getAsFile()
110 {
111     notImplemented();
112     return 0;
113 }
114
115 }
116
117 #endif // ENABLE(DATA_TRANSFER_ITEMS)