diff --git a/krusader/Panel/panelfunc.h b/krusader/Panel/panelfunc.h --- a/krusader/Panel/panelfunc.h +++ b/krusader/Panel/panelfunc.h @@ -136,6 +136,9 @@ void clipboardChanged(QClipboard::Mode mode); // Update the directory size in view void slotSizeCalculated(const QUrl &url, KIO::filesize_t size); + // Get some information about a file that is going to be edited + // and perform some steps before the edition + void slotStatEdit(KJob* job); protected: QUrl cleanPath(const QUrl &url); diff --git a/krusader/Panel/panelfunc.cpp b/krusader/Panel/panelfunc.cpp --- a/krusader/Panel/panelfunc.cpp +++ b/krusader/Panel/panelfunc.cpp @@ -512,7 +512,7 @@ // if the file exists, edit it instead of creating a new one QFile file(filePath.toLocalFile()); if (file.exists()) { - editFile(); + editFile(filePath); return; } else { // simply create a local file @@ -526,17 +526,45 @@ slotFileCreated(nullptr, filePath); return; } + } else { + KIO::StatJob* statJob = KIO::stat(filePath, KIO::HideProgressInfo); + connect(statJob, &KIO::StatJob::result, this, &ListPanelFunc::slotStatEdit); } +} - auto *tempFile = new QTemporaryFile; - tempFile->setAutoRemove(false); // done below - tempFile->open(); // create file +void ListPanelFunc::slotStatEdit(KJob* job) +{ + if (!job) + return; + + const KIO::StatJob *statJob = dynamic_cast(job); + const QUrl &url = statJob->url(); + + if (job->error()) { + if (job->error() == KIO::ERR_DOES_NOT_EXIST) { + // create a new file + auto *tempFile = new QTemporaryFile; + tempFile->setAutoRemove(false); // done below + tempFile->open(); // create file + + KIO::CopyJob *job = KIO::copy(QUrl::fromLocalFile(tempFile->fileName()), url); + job->setUiDelegate(nullptr); + job->setDefaultPermissions(true); + connect(job, &KIO::CopyJob::result, this, [=](KJob *job) { slotFileCreated(job, url); }); + connect(job, &KIO::CopyJob::result, tempFile, &QTemporaryFile::deleteLater); + return; + } else { + KMessageBox::error(nullptr, job->errorString()); + return; + } + } + + if (statJob->statResult().isDir()) { + KMessageBox::error(nullptr, i18n("You cannot edit a folder")); + return; + } - KIO::CopyJob *job = KIO::copy(QUrl::fromLocalFile(tempFile->fileName()), filePath); - job->setUiDelegate(nullptr); - job->setDefaultPermissions(true); - connect(job, &KIO::CopyJob::result, this, [=](KJob *job) { slotFileCreated(job, filePath); }); - connect(job, &KIO::CopyJob::result, tempFile, &QTemporaryFile::deleteLater); + KrViewer::edit(url); } void ListPanelFunc::slotFileCreated(KJob *job, const QUrl filePath)