From ede7dd0e843a2c83db1297383ff568a8fbe7fbe0 Mon Sep 17 00:00:00 2001 From: Aleix Pol Date: Mon, 9 Mar 2015 13:22:10 +0100 Subject: [PATCH] Drop KIOWidgets dependency from KDEConnectCore Removes the usage of RenameDialog by moving the functionality away. In fact, what it does is make the share dialog try to come up with a different name rather than asking the user. It's a quite common procedure as makes the interaction simpler (for example, Chromium and Kamoso work like that). REVIEW: 122813 --- core/CMakeLists.txt | 1 - core/filetransferjob.cpp | 49 +++-------------------------------- core/filetransferjob.h | 1 - plugins/share/shareplugin.cpp | 41 ++++++++++++++++++++++++++++- 4 files changed, 43 insertions(+), 49 deletions(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 95b8bd84f..ec7fd7fa3 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -35,7 +35,6 @@ PUBLIC PRIVATE Qt5::DBus Qt5::Gui - KF5::KIOWidgets KF5::I18n KF5::ConfigCore KF5::Notifications diff --git a/core/filetransferjob.cpp b/core/filetransferjob.cpp index 1e0dfcdec..73bad7977 100644 --- a/core/filetransferjob.cpp +++ b/core/filetransferjob.cpp @@ -25,7 +25,6 @@ #include #include -#include #include FileTransferJob::FileTransferJob(const QSharedPointer& origin, qint64 size, const QUrl& destination) @@ -66,54 +65,12 @@ void FileTransferJob::doStart() QString(mDeviceName)) ); QUrl destCheck = mDestination; - if (QFile::exists(destCheck.toLocalFile())) { - QFileInfo destInfo(destCheck.toLocalFile()); - KIO::RenameDialog *dialog = new KIO::RenameDialog(Q_NULLPTR, - i18n("Incoming file exists"), - QUrl(mDeviceName + ":/" + destCheck.fileName()), - destCheck, - KIO::RenameDialog_Overwrite, - mSize, - destInfo.size(), - QDateTime(), - destInfo.created(), - QDateTime(), - destInfo.lastModified() - ); - connect(this, SIGNAL(finished(KJob*)), dialog, SLOT(deleteLater())); - connect(dialog, SIGNAL(finished(int)), SLOT(renameDone(int))); - dialog->show(); - return; - } - - startTransfer(); -} - -void FileTransferJob::renameDone(int result) -{ - KIO::RenameDialog *renameDialog = qobject_cast(sender()); - switch (result) { - case KIO::R_CANCEL: - //The user cancelled, killing the job - emitResult(); - case KIO::R_RENAME: - mDestination = renameDialog->newDestUrl(); - break; - case KIO::R_OVERWRITE: - { - // Delete the old file if exists - QFile oldFile(mDestination.toLocalFile()); - if (oldFile.exists()) { - oldFile.remove(); - } - break; - } - default: - qCWarning(KDECONNECT_CORE()) << "Unknown Error"; + if (destCheck.isLocalFile() && QFile::exists(destCheck.toLocalFile())) { + setError(2); + setErrorText(i18n("Filename already present")); emitResult(); } - renameDialog->deleteLater(); startTransfer(); } diff --git a/core/filetransferjob.h b/core/filetransferjob.h index 8c94c9949..973bd3a87 100644 --- a/core/filetransferjob.h +++ b/core/filetransferjob.h @@ -44,7 +44,6 @@ public: public Q_SLOTS: void doStart(); - void renameDone(int result); void readyRead(); void open(KIO::Job*); void sourceFinished(); diff --git a/plugins/share/shareplugin.cpp b/plugins/share/shareplugin.cpp index 2c1566bd1..dbafb1149 100644 --- a/plugins/share/shareplugin.cpp +++ b/plugins/share/shareplugin.cpp @@ -42,6 +42,41 @@ K_PLUGIN_FACTORY( KdeConnectPluginFactory, registerPlugin< SharePlugin >(); ) Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_SHARE, "kdeconnect.plugin.share"); +static void autoincFilename(QUrl &filename) +{ + // Extract the filename from the path + QString name= filename.fileName(); + + // If the name contains a number then increment it + QRegExp numSearch( "(^|[^\\d])(\\d+)" ); // we want to match as far left as possible, and when the number is at the start of the name + + // Does it have a number? + int start = numSearch.lastIndexIn( name ); + if (start != -1) { + // It has a number, increment it + start = numSearch.pos( 2 ); // we are only interested in the second group + QString numAsStr = numSearch.cap(2); + QString number = QString::number( numAsStr.toInt() + 1 ); + number = number.rightJustified( numAsStr.length(), '0' ); + name.replace( start, numAsStr.length(), number ); + } + else { + // no number + start = name.lastIndexOf('.'); + if (start != -1) { + // has a . somewhere, e.g. it has an extension + name.insert(start, '1'); + } + else { + // no extension, just tack it on to the end + name += '1'; + } + } + + //Rebuild the path + filename.setPath( filename.adjusted(QUrl::RemoveFilename).toLocalFile() + name ); +} + SharePlugin::SharePlugin(QObject* parent, const QVariantList& args) : KdeConnectPlugin(parent, args) { @@ -92,7 +127,11 @@ bool SharePlugin::receivePackage(const NetworkPackage& np) //qCDebug(KDECONNECT_PLUGIN_SHARE) << "receiving file"; const QString filename = np.get("filename", QString::number(QDateTime::currentMSecsSinceEpoch())); const QString dir = destinationDir().adjusted(QUrl::StripTrailingSlash).toString(); - const QUrl destination(dir + '/' + filename); + QUrl destination(dir + '/' + filename); + while (destination.isLocalFile() && QFile::exists(destination.toLocalFile())) { + autoincFilename(destination); + } + FileTransferJob* job = np.createPayloadTransferJob(destination); job->setDeviceName(device()->name()); connect(job, SIGNAL(result(KJob*)), this, SLOT(finished(KJob*)));