diff --git a/importer/fileutils.cpp b/importer/fileutils.cpp --- a/importer/fileutils.cpp +++ b/importer/fileutils.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include // KDE #include @@ -42,49 +44,78 @@ bool contentsAreIdentical(const QUrl& url1, const QUrl& url2, QWidget* authWindow) { - // FIXME: Support remote urls - KIO::StatJob *statJob = KIO::mostLocalUrl(url1); - KJobWidgets::setWindow(statJob, authWindow); - if (!statJob->exec()) { - qWarning() << "Unable to stat" << url1; - return false; + KIO::StatJob *statJob; + KIO::StoredTransferJob *fileJob; + QScopedPointer file1, file2; + QByteArray file1Bytes, file2Bytes; + + if (url1.isLocalFile()) { + //qDebug() << "file1 is local"; + statJob = KIO::mostLocalUrl(url1); + KJobWidgets::setWindow(statJob, authWindow); + if (!statJob->exec()) { + qWarning() << "Unable to stat" << url1; + return false; + } + file1.reset(new QFile(statJob->mostLocalUrl().toLocalFile())); + } else { + //qDebug() << "file1 is remote"; + fileJob = KIO::storedGet(url1, KIO::NoReload ,KIO::HideProgressInfo); + KJobWidgets::setWindow(fileJob, authWindow); + if (!fileJob->exec()) { + qWarning() << "Can't read" << url1; + return false; + } + file1Bytes = QByteArray(fileJob->data()); + file1.reset(new QBuffer(&file1Bytes)); } - QFile file1(statJob->mostLocalUrl().toLocalFile()); - if (!file1.open(QIODevice::ReadOnly)) { + if (!file1->open(QIODevice::ReadOnly)) { // Can't read url1, assume it's different from url2 qWarning() << "Can't read" << url1; return false; } - statJob = KIO::mostLocalUrl(url2); - KJobWidgets::setWindow(statJob, authWindow); - if (!statJob->exec()) { - qWarning() << "Unable to stat" << url2; - return false; + if (url2.isLocalFile()) { + //qDebug() << "file2 is local"; + statJob = KIO::mostLocalUrl(url2); + KJobWidgets::setWindow(statJob, authWindow); + if (!statJob->exec()) { + qWarning() << "Unable to stat" << url2; + return false; + } + file2.reset(new QFile(statJob->mostLocalUrl().toLocalFile())); + } else { + //qDebug() << "file2 is remote"; + fileJob = KIO::storedGet(url2, KIO::NoReload ,KIO::HideProgressInfo); + KJobWidgets::setWindow(fileJob, authWindow); + if (!fileJob->exec()) { + qWarning() << "Can't read" << url2; + return false; + } + file2Bytes = QByteArray(fileJob->data()); + file2.reset(new QBuffer(&file2Bytes)); } - - QFile file2(statJob->mostLocalUrl().toLocalFile()); - if (!file2.open(QIODevice::ReadOnly)) { + if (!file2->open(QIODevice::ReadOnly)) { // Can't read url2, assume it's different from url1 qWarning() << "Can't read" << url2; return false; } const int CHUNK_SIZE = 4096; - while (!file1.atEnd() && !file2.atEnd()) { - QByteArray url1Array = file1.read(CHUNK_SIZE); - QByteArray url2Array = file2.read(CHUNK_SIZE); - + while (!file1->atEnd() && !file2->atEnd()) { + QByteArray url1Array = file1->read(CHUNK_SIZE); + QByteArray url2Array = file2->read(CHUNK_SIZE); + if (url1Array != url2Array) { return false; } } - if (file1.atEnd() && file2.atEnd()) { + if (file1->atEnd() && file2->atEnd()) { return true; } else { qWarning() << "One file ended before the other"; return false; - } + } } RenameResult rename(const QUrl& src, const QUrl& dst_, QWidget* authWindow) @@ -131,7 +162,7 @@ } // Rename - KIO::Job* job = KIO::rename(src, dst, KIO::HideProgressInfo); + KIO::Job* job = KIO::moveAs(src, dst, KIO::HideProgressInfo); KJobWidgets::setWindow(job, authWindow); if (!job->exec()) { result = RenameFailed; diff --git a/importer/importer.cpp b/importer/importer.cpp --- a/importer/importer.cpp +++ b/importer/importer.cpp @@ -31,7 +31,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -55,6 +57,7 @@ std::unique_ptr mFileNameFormater; QUrl mTempImportDirUrl; QTemporaryDir* mTempImportDir; + QUrl mDestinationDirUrl; /* @defgroup reset Should be reset in start() * @{ */ @@ -70,28 +73,31 @@ bool createImportDir(const QUrl& url) { - Q_ASSERT(url.isLocalFile()); - // FIXME: Support remote urls - - if (!QDir().mkpath(url.toLocalFile())) { + KIO::Job* job = KIO::mkpath(url, QUrl(), KIO::HideProgressInfo); + KJobWidgets::setWindow(job,mAuthWindow); + if (!job->exec()) { emit q->error(i18n("Could not create destination folder.")); return false; } - - QString tempDirPath = url.toLocalFile() + "/.gwenview_importer-XXXXXX"; - mTempImportDir = new QTemporaryDir(tempDirPath); - + + if (url.isLocalFile()) { + QString tempDirPath = url.toLocalFile() + "/.gwenview_importer-XXXXXX"; + mTempImportDir = new QTemporaryDir(tempDirPath); + } else { + mTempImportDir = new QTemporaryDir(); + } + if (!mTempImportDir->isValid()) { emit q->error(i18n("Could not create temporary upload folder.")); return false; } - + mTempImportDirUrl = QUrl::fromLocalFile(mTempImportDir->path() + '/'); if (!mTempImportDirUrl.isValid()) { emit q->error(i18n("Could not create temporary upload folder.")); return false; - } - + } + return true; } @@ -114,7 +120,7 @@ void renameImportedUrl(const QUrl& src) { - QUrl dst = src.resolved(QUrl("..")); + QUrl dst = mDestinationDirUrl; QString fileName; if (mFileNameFormater.get()) { KFileItem item(src); @@ -128,7 +134,7 @@ } else { fileName = src.fileName(); } - dst.setPath(dst.path() + fileName); + dst.setPath(dst.path() + '/' + fileName); FileUtils::RenameResult result = FileUtils::rename(src, dst, mAuthWindow); switch (result) { @@ -174,6 +180,7 @@ void Importer::start(const QList& list, const QUrl& destination) { + d->mDestinationDirUrl = destination; d->mUrlList = list; d->mImportedUrlList.clear(); d->mSkippedUrlList.clear();