-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdndwebengineview.cpp
More file actions
80 lines (73 loc) · 2.7 KB
/
Copy pathdndwebengineview.cpp
File metadata and controls
80 lines (73 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "dndwebengineview.h"
#include "utils.h"
#include <QDebug>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QMimeData>
#include <regex>
DNDWebEngineView::DNDWebEngineView(QWidget *parent)
: QWebEngineView(parent)
{
setAcceptDrops(true);
}
// Needed to allow browser tab drop
void DNDWebEngineView::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
void DNDWebEngineView::dropEvent(QDropEvent *event)
{
const QMimeData *mimeData = event->mimeData();
// TODO: Chrome tab?
// Firefox tab dropped
if (mimeData->hasFormat("application/x-moz-tabbrowser-tab")) {
// data("text/x-moz-text-internal") contains URL string interspaced with " and \x00 sequences
std::regex pattern("[\"\\x00]");
QString url = QString::fromStdString(
std::regex_replace(mimeData->data("text/x-moz-text-internal").toStdString(),
pattern,
""));
qDebug() << "application/x-moz-tabbrowser-tab" << url;
this->setUrl(url);
emit updateCommandStatusText(url);
emit clearStatus();
}
// Dropping a file from explorer lands us here
// If multiple, only take first dropped
else if (event->mimeData()->hasUrls()) {
qDebug() << "text/uri-list" << mimeData->urls()[0].toString();
QString url = mimeData->urls()[0].toString();
if (!Utils::isUnsupportedFile(url)) {
this->setUrl(url);
emit updateCommandStatusText(url);
emit clearStatus();
} else {
qDebug() << "From DND unsupported filetype";
emit pushStatus(QString::fromStdString(
"Unsupported filetype: "
+ url.toStdString().substr(url.toStdString().find_last_of("."))),
QString("Error"));
}
}
// Plaintext
else if (mimeData->hasText()) {
// set plaintext
qDebug() << "plaintext" << mimeData->text();
QByteArray data = mimeData->text().toUtf8();
// Set the content as "text/plain"
// The empty QUrl() means external objects (like images) won't be resolved relative to a base URL,
// which is fine for plain text.
this->setContent(data, "text/plain", QUrl());
emit updateCommandStatusText(QString("Anonymous Plaintext"));
emit clearStatus();
}
// HTML
else if (mimeData->hasHtml()) {
qDebug() << "html" << mimeData->html();
this->setHtml(mimeData->html());
emit updateCommandStatusText(QString("Anonymous HTML"));
emit clearStatus();
} else {
qDebug() << "Unknown type" << mimeData->formats();
}
}