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
This commit is contained in:
Aleix Pol 2015-03-09 13:22:10 +01:00
parent 0a1128ae86
commit ede7dd0e84
4 changed files with 43 additions and 49 deletions

View file

@ -35,7 +35,6 @@ PUBLIC
PRIVATE
Qt5::DBus
Qt5::Gui
KF5::KIOWidgets
KF5::I18n
KF5::ConfigCore
KF5::Notifications

View file

@ -25,7 +25,6 @@
#include <QFileInfo>
#include <QDebug>
#include <KIO/RenameDialog>
#include <KLocalizedString>
FileTransferJob::FileTransferJob(const QSharedPointer<QIODevice>& 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<KIO::RenameDialog*>(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();
}

View file

@ -44,7 +44,6 @@ public:
public Q_SLOTS:
void doStart();
void renameDone(int result);
void readyRead();
void open(KIO::Job*);
void sourceFinished();

View file

@ -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<QString>("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*)));