[50265] | 1 | /* $Id: UIDnDMIMEData.cpp 103496 2024-02-21 14:32:19Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[52727] | 3 | * VBox Qt GUI - UIDnDMIMEData class implementation.
|
---|
[50265] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2011-2023 Oracle and/or its affiliates.
|
---|
[50265] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[50265] | 26 | */
|
---|
| 27 |
|
---|
[76606] | 28 | #define LOG_GROUP LOG_GROUP_GUEST_DND
|
---|
[52730] | 29 |
|
---|
[50265] | 30 | /* Qt includes: */
|
---|
[76606] | 31 | #include <QFileInfo>
|
---|
| 32 | #include <QMimeData>
|
---|
| 33 | #include <QStringList>
|
---|
| 34 | #include <QUrl>
|
---|
[50265] | 35 |
|
---|
[52730] | 36 | /* GUI includes: */
|
---|
[76606] | 37 | #include "UIDnDMIMEData.h"
|
---|
[52730] | 38 |
|
---|
[76606] | 39 | /* Other VBox includes: */
|
---|
[50265] | 40 | #include <VBox/log.h>
|
---|
[76423] | 41 | #include <iprt/errcore.h>
|
---|
[50265] | 42 |
|
---|
[85477] | 43 | #include <VBox/GuestHost/DragAndDrop.h>
|
---|
[50265] | 44 |
|
---|
[85477] | 45 |
|
---|
[55821] | 46 | UIDnDMIMEData::UIDnDMIMEData(UIDnDHandler *pDnDHandler,
|
---|
| 47 | QStringList lstFormats, Qt::DropAction defAction, Qt::DropActions actions)
|
---|
| 48 | : m_pDnDHandler(pDnDHandler)
|
---|
| 49 | , m_lstFormats(lstFormats)
|
---|
[50265] | 50 | , m_defAction(defAction)
|
---|
[56502] | 51 | , m_curAction(Qt::IgnoreAction)
|
---|
[50265] | 52 | , m_actions(actions)
|
---|
| 53 | , m_enmState(Dragging)
|
---|
| 54 | {
|
---|
| 55 | LogFlowThisFuncEnter();
|
---|
[55821] | 56 | #ifdef DEBUG
|
---|
[56865] | 57 | LogFlowFunc(("Number of formats: %d\n", m_lstFormats.size()));
|
---|
| 58 | for (int i = 0; i < m_lstFormats.size(); i++)
|
---|
[58866] | 59 | LogFlowFunc(("\tFormat %d: %s\n", i, m_lstFormats.at(i).toUtf8().constData()));
|
---|
[55821] | 60 | #endif
|
---|
[50265] | 61 | }
|
---|
| 62 |
|
---|
[55821] | 63 | QStringList UIDnDMIMEData::formats(void) const
|
---|
[50265] | 64 | {
|
---|
[56865] | 65 | LogFlowFuncEnter();
|
---|
[57221] | 66 | #ifdef DEBUG
|
---|
| 67 | for (int i = 0; i < m_lstFormats.size(); i++)
|
---|
[58866] | 68 | LogFlowFunc(("\tFormat %d: %s\n", i, m_lstFormats.at(i).toUtf8().constData()));
|
---|
[57221] | 69 | #endif
|
---|
[50265] | 70 | return m_lstFormats;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[55821] | 73 | bool UIDnDMIMEData::hasFormat(const QString &strMIMEType) const
|
---|
[50265] | 74 | {
|
---|
[63327] | 75 | RT_NOREF(strMIMEType);
|
---|
| 76 |
|
---|
[57221] | 77 | bool fRc;
|
---|
[56865] | 78 | #ifdef RT_OS_DARWIN
|
---|
[57221] | 79 | fRc = m_lstFormats.contains(strMIMEType);
|
---|
| 80 | #else
|
---|
| 81 | fRc = m_curAction != Qt::IgnoreAction;
|
---|
[56865] | 82 | #endif
|
---|
| 83 |
|
---|
[56497] | 84 | LogFlowFunc(("%s: %RTbool (QtMimeData: %RTbool, curAction=0x%x)\n",
|
---|
[103496] | 85 | strMIMEType.toUtf8().constData(), fRc, QMimeData::hasFormat(strMIMEType), m_curAction));
|
---|
[56865] | 86 |
|
---|
[50265] | 87 | return fRc;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[55821] | 90 | /**
|
---|
| 91 | * Called by Qt's drag'n drop operation (QDrag) for retrieving the actual drag'n drop
|
---|
| 92 | * data in case of a successful drag'n drop operation.
|
---|
| 93 | *
|
---|
| 94 | * @param strMIMEType MIME type string.
|
---|
[64528] | 95 | * @param vaType Variant containing the actual data based on the MIME type.
|
---|
[55821] | 96 | *
|
---|
| 97 | * @return QVariant
|
---|
| 98 | */
|
---|
[100035] | 99 | QVariant UIDnDMIMEData::retrieveData(const QString &strMIMEType, QMetaType metaType) const
|
---|
| 100 | {
|
---|
[100347] | 101 | /* Acquire QMetaType::Type: */
|
---|
| 102 | const QMetaType::Type vaType = (QMetaType::Type)metaType.id();
|
---|
[100035] | 103 |
|
---|
[56497] | 104 | LogFlowFunc(("state=%RU32, curAction=0x%x, defAction=0x%x, mimeType=%s, type=%d (%s)\n",
|
---|
[103496] | 105 | m_enmState, m_curAction, m_defAction, strMIMEType.toUtf8().constData(), vaType, metaType.name()));
|
---|
[50265] | 106 |
|
---|
[57221] | 107 | int rc = VINF_SUCCESS;
|
---|
[50265] | 108 |
|
---|
| 109 | #ifdef RT_OS_WINDOWS
|
---|
[57221] | 110 | /*
|
---|
[56865] | 111 | * On Windows this function will be called several times by Qt's
|
---|
[50460] | 112 | * OLE-specific internals to figure out which data formats we have
|
---|
[56865] | 113 | * to offer. So just assume we can drop data here for a start.
|
---|
[57221] | 114 | */
|
---|
[56865] | 115 | #elif defined(RT_OS_DARWIN)
|
---|
[57221] | 116 | # ifndef VBOX_WITH_DRAG_AND_DROP_PROMISES
|
---|
[56865] | 117 | /*
|
---|
[57221] | 118 | * Without VBOX_WITH_DRAG_AND_DROP_PROMISES being set in VBox *and* in our (patched) Qt
|
---|
| 119 | * libraries there's no reliable way to get this working on OS X. So just deny any dropping.
|
---|
[56865] | 120 | */
|
---|
[57221] | 121 | rc = VERR_NOT_IMPLEMENTED;
|
---|
| 122 |
|
---|
| 123 | /* Let the user know. */
|
---|
[58212] | 124 | LogRel(("DnD: Drag and drop support for OS X is not available in this version\n"));
|
---|
[57221] | 125 | # endif /* VBOX_WITH_DRAG_AND_DROP_PROMISES */
|
---|
| 126 | #else /* !RT_OS_DARWIN */
|
---|
| 127 | /*
|
---|
| 128 | * On Linux/Solaris our state gets updated if the drop target has been
|
---|
| 129 | * changed. So query the current status if we're at the moment are able
|
---|
| 130 | * to drop something on the current target.
|
---|
| 131 | */
|
---|
[56502] | 132 | if (m_curAction == Qt::IgnoreAction)
|
---|
[56138] | 133 | {
|
---|
[56497] | 134 | LogFlowFunc(("Current drop action is 0x%x, so can't drop yet\n", m_curAction));
|
---|
[58212] | 135 | rc = VERR_NOT_FOUND;
|
---|
[56138] | 136 | }
|
---|
[50265] | 137 | #endif
|
---|
| 138 |
|
---|
[57221] | 139 | if (RT_SUCCESS(rc))
|
---|
[50265] | 140 | {
|
---|
[57221] | 141 | /* Silently ignore internal Qt types / converters. */
|
---|
| 142 | if (!strMIMEType.compare("application/x-qt-mime-type-name", Qt::CaseInsensitive))
|
---|
| 143 | {
|
---|
| 144 | rc = VERR_NOT_FOUND;
|
---|
| 145 | }
|
---|
[56497] | 146 | /* Do we support the requested MIME type? */
|
---|
[57221] | 147 | else if (!m_lstFormats.contains(strMIMEType))
|
---|
[56497] | 148 | {
|
---|
[103496] | 149 | LogRel(("DnD: Unsupported MIME type '%s'\n", strMIMEType.toUtf8().constData()));
|
---|
[57221] | 150 | rc = VERR_NOT_SUPPORTED;
|
---|
[56497] | 151 | }
|
---|
[100347] | 152 | #ifndef RT_OS_DARWIN /* On OS X QMetaType::UnknownType can happen for drag and drop "promises" for "lazy requests". */
|
---|
[57221] | 153 | /* Check supported variant types. */
|
---|
| 154 | else if (!(
|
---|
| 155 | /* Plain text. */
|
---|
[100347] | 156 | vaType == QMetaType::QString
|
---|
[57221] | 157 | /* Binary data. */
|
---|
[100347] | 158 | || vaType == QMetaType::QByteArray
|
---|
[57221] | 159 | /* URI list. */
|
---|
[100347] | 160 | || vaType == QMetaType::QVariantList
|
---|
| 161 | || vaType == QMetaType::QStringList))
|
---|
[56497] | 162 | {
|
---|
[100347] | 163 | LogRel(("DnD: Unsupported data type '%s'\n", metaType.name()));
|
---|
[57221] | 164 | rc = VERR_NOT_SUPPORTED;
|
---|
[56497] | 165 | }
|
---|
[57221] | 166 | #endif
|
---|
[50265] | 167 | }
|
---|
| 168 |
|
---|
[57221] | 169 | LogRel3(("DnD: Retrieved data state is %ld (action=0x%x), rc=%Rrc\n", m_enmState, m_curAction, rc));
|
---|
[55821] | 170 |
|
---|
[57221] | 171 | if (RT_SUCCESS(rc))
|
---|
[50265] | 172 | {
|
---|
[56555] | 173 | QVariant vaData;
|
---|
[57288] | 174 | rc = emit sigGetData(Qt::CopyAction, strMIMEType, vaType, vaData);
|
---|
[57221] | 175 | if (RT_SUCCESS(rc))
|
---|
| 176 | {
|
---|
| 177 | LogRel3(("DnD: Returning data for MIME type=%s, variant type=%s, rc=%Rrc\n",
|
---|
[103496] | 178 | strMIMEType.toUtf8().constData(), metaType.name(), rc));
|
---|
[56555] | 179 |
|
---|
| 180 | return vaData;
|
---|
[57221] | 181 | }
|
---|
[50265] | 182 | }
|
---|
[57221] | 183 | else if (rc == VERR_NOT_FOUND) /* Silently skip internal entries. */
|
---|
| 184 | rc = VINF_SUCCESS;
|
---|
[50265] | 185 |
|
---|
[57221] | 186 | if (RT_FAILURE(rc))
|
---|
[58212] | 187 | LogRel2(("DnD: Retrieving data failed with %Rrc\n", rc));
|
---|
[57221] | 188 |
|
---|
[100347] | 189 | return QVariant();
|
---|
[50265] | 190 | }
|
---|
| 191 |
|
---|
[57221] | 192 | /* static */
|
---|
[100347] | 193 | QMetaType::Type UIDnDMIMEData::getMetaType(const QString &strMIMEType)
|
---|
[50265] | 194 | {
|
---|
[100347] | 195 | QMetaType::Type vaType;
|
---|
[50305] | 196 |
|
---|
[57221] | 197 | if ( !strMIMEType.compare("text/html")
|
---|
| 198 | || !strMIMEType.compare("text/plain;charset=utf-8")
|
---|
| 199 | || !strMIMEType.compare("text/plain;charset=utf-16")
|
---|
| 200 | || !strMIMEType.compare("text/plain")
|
---|
| 201 | || !strMIMEType.compare("text/richtext")
|
---|
| 202 | || !strMIMEType.compare("UTF8_STRING")
|
---|
| 203 | || !strMIMEType.compare("TEXT")
|
---|
| 204 | || !strMIMEType.compare("STRING"))
|
---|
| 205 | {
|
---|
[100347] | 206 | vaType = QMetaType::QString;
|
---|
[57221] | 207 | }
|
---|
| 208 | else if (!strMIMEType.compare("text/uri-list", Qt::CaseInsensitive))
|
---|
[100347] | 209 | vaType = QMetaType::QVariantList;
|
---|
[57221] | 210 | else
|
---|
[100347] | 211 | vaType = QMetaType::UnknownType;
|
---|
[57221] | 212 |
|
---|
[100347] | 213 | LogFlowFunc(("strMIMEType=%s -> vaType=%s\n", qPrintable(strMIMEType), QMetaType(vaType).name()));
|
---|
[57221] | 214 | return vaType;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /* static */
|
---|
| 218 | int UIDnDMIMEData::getDataAsVariant(const QVector<uint8_t> &vecData,
|
---|
| 219 | const QString &strMIMEType,
|
---|
[100347] | 220 | QMetaType::Type vaType,
|
---|
[63327] | 221 | QVariant &vaData)
|
---|
[57221] | 222 | {
|
---|
[63327] | 223 | RT_NOREF(strMIMEType);
|
---|
[57221] | 224 | LogFlowFunc(("vecDataSize=%d, strMIMEType=%s vaType=%s\n",
|
---|
[100347] | 225 | vecData.size(), qPrintable(strMIMEType), QMetaType(vaType).name()));
|
---|
[57221] | 226 |
|
---|
[50305] | 227 | int rc = VINF_SUCCESS;
|
---|
| 228 |
|
---|
[57221] | 229 | switch (vaType)
|
---|
[50305] | 230 | {
|
---|
[100347] | 231 | case QMetaType::QString:
|
---|
[50305] | 232 | {
|
---|
[57221] | 233 | vaData = QVariant::fromValue(QString(reinterpret_cast<const char *>(vecData.constData())));
|
---|
[100347] | 234 | Assert(vaData.typeId() == QMetaType::QString);
|
---|
| 235 |
|
---|
[50305] | 236 | break;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[100347] | 239 | case QMetaType::QByteArray:
|
---|
[50305] | 240 | {
|
---|
[57221] | 241 | QByteArray ba(reinterpret_cast<const char*>(vecData.constData()), vecData.size());
|
---|
| 242 |
|
---|
| 243 | vaData = QVariant::fromValue(ba);
|
---|
[100347] | 244 | Assert(vaData.typeId() == QMetaType::QByteArray);
|
---|
[50305] | 245 | break;
|
---|
| 246 | }
|
---|
| 247 |
|
---|
[57221] | 248 | /* See: https://developer.apple.com/library/ios/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html */
|
---|
[100347] | 249 | case QMetaType::QVariantList: /* Used on OS X for representing URI lists. */
|
---|
[50305] | 250 | {
|
---|
[57221] | 251 | QString strData = QString(reinterpret_cast<const char*>(vecData.constData()));
|
---|
[93982] | 252 | #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
---|
| 253 | QStringList lstString = strData.split(DND_PATH_SEPARATOR_STR, Qt::SkipEmptyParts);
|
---|
| 254 | #else
|
---|
[85746] | 255 | QStringList lstString = strData.split(DND_PATH_SEPARATOR_STR, QString::SkipEmptyParts);
|
---|
[93982] | 256 | #endif
|
---|
[55821] | 257 |
|
---|
[57221] | 258 | QVariantList lstVariant;
|
---|
[55821] | 259 |
|
---|
[57221] | 260 | Q_FOREACH(const QString& strCur, lstString)
|
---|
| 261 | {
|
---|
| 262 | QVariant vaURL = QVariant::fromValue(QUrl(strCur));
|
---|
[100347] | 263 | Assert(vaURL.typeId() == QMetaType::QUrl);
|
---|
[57221] | 264 | lstVariant.append(vaURL);
|
---|
[50305] | 265 | }
|
---|
| 266 |
|
---|
[57221] | 267 | vaData = QVariant::fromValue(lstVariant);
|
---|
[100347] | 268 | Assert(vaData.typeId() == QMetaType::QVariantList);
|
---|
[50305] | 269 | break;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[100347] | 272 | case QMetaType::QStringList:
|
---|
[57221] | 273 | {
|
---|
| 274 | QString strData = QString(reinterpret_cast<const char*>(vecData.constData()));
|
---|
[93982] | 275 | #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
|
---|
| 276 | QStringList lstString = strData.split(DND_PATH_SEPARATOR_STR, Qt::SkipEmptyParts);
|
---|
| 277 | #else
|
---|
[85746] | 278 | QStringList lstString = strData.split(DND_PATH_SEPARATOR_STR, QString::SkipEmptyParts);
|
---|
[93982] | 279 | #endif
|
---|
[57221] | 280 |
|
---|
| 281 | LogFlowFunc(("\tStringList has %d entries\n", lstString.size()));
|
---|
[93982] | 282 | #ifdef DEBUG
|
---|
[57221] | 283 | Q_FOREACH(const QString& strCur, lstString)
|
---|
| 284 | LogFlowFunc(("\t\tString: %s\n", qPrintable(strCur)));
|
---|
[93982] | 285 | #endif
|
---|
[57221] | 286 | vaData = QVariant::fromValue(lstString);
|
---|
[100347] | 287 | Assert(vaData.typeId() == QMetaType::QStringList);
|
---|
[57221] | 288 | break;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[50305] | 291 | default:
|
---|
[57221] | 292 | {
|
---|
| 293 | LogRel2(("DnD: Converting data (%d bytes) from guest to variant type '%s' not supported\n",
|
---|
[100347] | 294 | vecData.size(), QMetaType(vaType).name() ? QMetaType(vaType).name() : "<Invalid>"));
|
---|
[57221] | 295 |
|
---|
[50305] | 296 | rc = VERR_NOT_SUPPORTED;
|
---|
| 297 | break;
|
---|
[57221] | 298 | }
|
---|
[50305] | 299 | }
|
---|
| 300 |
|
---|
| 301 | LogFlowFuncLeaveRC(rc);
|
---|
| 302 | return rc;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[55821] | 305 | /**
|
---|
| 306 | * Issued by the QDrag object as soon as the current drop action has changed.
|
---|
| 307 | *
|
---|
| 308 | * @param dropAction New drop action to use.
|
---|
| 309 | */
|
---|
| 310 | void UIDnDMIMEData::sltDropActionChanged(Qt::DropAction dropAction)
|
---|
| 311 | {
|
---|
| 312 | LogFlowFunc(("dropAction=0x%x\n", dropAction));
|
---|
[56497] | 313 | m_curAction = dropAction;
|
---|
[55821] | 314 | }
|
---|
[57221] | 315 |
|
---|