Commit b73355da authored by Elvis Angelaccio's avatar Elvis Angelaccio
Browse files

Check if destination exists also when pasting binary data

Summary:
Currently we check whether the destination URL exists only if we are
pasting text (or if the mimedata has only one format).

If we are pasting e.g. images copied from Spectacle, we unconditionally
overwrite the destination URL.

This patch moves the StatJob+RenameDialog from `getNewFileName()` to a
new `getDestinationUrl()` function, which we can also call from `chooseFormatAndUrl()`.

BUG: 394318
FIXED-IN: 5.47

Test Plan:
- Paste some image into an existing file and check the rename dialog.
- Check that pasting text still shows the rename dialog.

Reviewers: dfaure

Subscribers: kde-frameworks-devel

Tags: #frameworks

Differential Revision: https://phabricator.kde.org/D13145
parent 9d4ccb98
......@@ -73,22 +73,9 @@ static KIO::Job *pasteClipboardUrls(const QMimeData *mimeData, const QUrl &destD
return nullptr;
}
static QUrl getNewFileName(const QUrl &u, const QString &text, const QString &suggestedFileName, QWidget *widget)
static QUrl getDestinationUrl(const QUrl &srcUrl, const QUrl &destUrl, QWidget *widget)
{
bool ok;
QString dialogText(text);
if (dialogText.isEmpty()) {
dialogText = i18n("Filename for clipboard content:");
}
QString file = QInputDialog::getText(widget, QString(), dialogText, QLineEdit::Normal, suggestedFileName, &ok);
if (!ok) {
return QUrl();
}
QUrl myurl(u);
myurl.setPath(concatPaths(myurl.path(), file));
KIO::StatJob *job = KIO::stat(myurl, myurl.isLocalFile() ? KIO::HideProgressInfo : KIO::DefaultFlags);
KIO::StatJob *job = KIO::stat(destUrl, destUrl.isLocalFile() ? KIO::HideProgressInfo : KIO::DefaultFlags);
job->setDetails(0);
job->setSide(KIO::StatJob::DestinationSide);
KJobWidgets::setWindow(job, widget);
......@@ -98,24 +85,41 @@ static QUrl getNewFileName(const QUrl &u, const QString &text, const QString &su
// an ugly tempfile name as the source URL)
// And now we're using a put job anyway, no destination checking included.
if (job->exec()) {
//qDebug() << "Paste will overwrite file. Prompting...";
KIO::RenameDialog dlg(widget,
i18n("File Already Exists"),
u,
myurl,
srcUrl,
destUrl,
KIO::RenameDialog_Overwrite);
KIO::RenameDialog_Result res = static_cast<KIO::RenameDialog_Result>(dlg.exec());
if (res == KIO::Result_Rename) {
myurl = dlg.newDestUrl();
return dlg.newDestUrl();
} else if (res == KIO::Result_Cancel) {
return QUrl();
} else if (res == KIO::Result_Overwrite) {
// OK, proceed
return destUrl;
}
}
return myurl;
return QUrl();
}
static QUrl getNewFileName(const QUrl &u, const QString &text, const QString &suggestedFileName, QWidget *widget)
{
bool ok;
QString dialogText(text);
if (dialogText.isEmpty()) {
dialogText = i18n("Filename for clipboard content:");
}
QString file = QInputDialog::getText(widget, QString(), dialogText, QLineEdit::Normal, suggestedFileName, &ok);
if (!ok) {
return QUrl();
}
QUrl myurl(u);
myurl.setPath(concatPaths(myurl.path(), file));
return getDestinationUrl(u, myurl, widget);
}
static KIO::Job *putDataAsyncTo(const QUrl &url, const QByteArray &data, QWidget *widget, KIO::JobFlags flags)
......@@ -170,6 +174,10 @@ static QByteArray chooseFormatAndUrl(const QUrl &u, const QMimeData *mimeData,
//qDebug() << " result=" << result << " chosenFormat=" << chosenFormat;
*newUrl = u;
newUrl->setPath(concatPaths(newUrl->path(), result));
const QUrl destUrl = getDestinationUrl(u, *newUrl, widget);
*newUrl = destUrl;
// In Qt3, the result of clipboard()->mimeData() only existed until the next
// event loop run (see dlg.exec() above), so we re-fetched it.
// TODO: This should not be necessary with Qt5; remove this conditional
......@@ -232,7 +240,7 @@ KIO::Job *pasteMimeDataImpl(const QMimeData *mimeData, const QUrl &destUrl,
} else if (formats.size() > 1) {
QUrl newUrl;
ba = chooseFormatAndUrl(destUrl, mimeData, formats, dialogText, suggestedFilename, widget, clipboard, &newUrl);
if (ba.isEmpty()) {
if (ba.isEmpty() || newUrl.isEmpty()) {
return nullptr;
}
return putDataAsyncTo(newUrl, ba, widget, KIO::Overwrite);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment