diff --git a/part/part.h b/part/part.h --- a/part/part.h +++ b/part/part.h @@ -76,6 +76,13 @@ KConfigSkeleton *config() const Q_DECL_OVERRIDE; QList settingsPages(QWidget *parent) const Q_DECL_OVERRIDE; + /** + * Validate the localFilePath() associated to this part. + * If the file is not valid, an error message is displayed to the user. + * @return Whether the localFilePath() can be loaded by the part. + */ + bool isLocalFileValid(); + public slots: void extractSelectedFilesTo(const QString& localPath); diff --git a/part/part.cpp b/part/part.cpp --- a/part/part.cpp +++ b/part/part.cpp @@ -514,56 +514,24 @@ { qCDebug(ARK) << "Attempting to open archive" << localFilePath(); - const QString localFile(localFilePath()); - const QFileInfo localFileInfo(localFile); - const bool creatingNewArchive = - arguments().metaData()[QStringLiteral("createNewArchive")] == QLatin1String("true"); - - if (localFileInfo.isDir()) { - KMessageBox::error(widget(), xi18nc("@info", - "%1 is a directory.", - localFile)); + if (!isLocalFileValid()) { return false; } - if (creatingNewArchive) { - if (localFileInfo.exists()) { - int overwrite = KMessageBox::questionYesNo(widget(), xi18nc("@info", "The archive %1 already exists. Would you like to open it instead?", localFile), i18nc("@title:window", "File Exists"), KGuiItem(i18n("Open File")), KStandardGuiItem::cancel()); - - if (overwrite == KMessageBox::No) { - return false; - } - } - displayMsgWidget(KMessageWidget::Information, xi18nc("@info", "The archive %1 will be created as soon as you add a file.", localFile)); - } else { - if (!localFileInfo.exists()) { - KMessageBox::sorry(widget(), xi18nc("@info", "The archive %1 was not found.", localFile), i18nc("@title:window", "Error Opening Archive")); - return false; - } - - if (!localFileInfo.isReadable()) { - displayMsgWidget(KMessageWidget::Warning, xi18nc("@info", "The archive %1 could not be loaded, as it was not possible to read from it.", localFile)); - return false; - } - } - - QScopedPointer archive(Kerfuffle::Archive::create(localFile, m_model)); - + QScopedPointer archive(Kerfuffle::Archive::create(localFilePath(), m_model)); Q_ASSERT(archive); if (archive->error() == NoPlugin) { - KMessageBox::sorry(widget(), - xi18nc("@info", "Ark was not able to open %1. No suitable plugin found." - "Ark does not seem to support this file type.", - localFile.section(QLatin1Char('/'), -1)), - i18nc("@title:window", "Error Opening Archive")); + displayMsgWidget(KMessageWidget::Error, xi18nc("@info", "Ark was not able to open %1. No suitable plugin found." + "Ark does not seem to support this file type.", + QFileInfo(localFilePath()).fileName())); return false; - } else if (archive->error() == FailedPlugin) { - KMessageBox::sorry(widget(), - xi18nc("@info", "Ark was not able to open %1. Failed to load a suitable plugin." - "Make sure any executables needed to handle the archive type are installed.", - localFile.section(QLatin1Char('/'), -1)), - i18nc("@title:window", "Error Opening Archive")); + } + + if (archive->error() == FailedPlugin) { + displayMsgWidget(KMessageWidget::Error, xi18nc("@info", "Ark was not able to open %1. Failed to load a suitable plugin." + "Make sure any executables needed to handle the archive type are installed.", + QFileInfo(localFilePath()).fileName())); return false; } @@ -617,6 +585,43 @@ return pages; } +bool Part::isLocalFileValid() +{ + const QString localFile = localFilePath(); + const QFileInfo localFileInfo(localFile); + const bool creatingNewArchive = arguments().metaData()[QStringLiteral("createNewArchive")] == QLatin1String("true"); + + if (localFileInfo.isDir()) { + displayMsgWidget(KMessageWidget::Error, xi18nc("@info", + "%1 is a directory.", + localFile)); + return false; + } + + if (creatingNewArchive) { + if (localFileInfo.exists()) { + int overwrite = KMessageBox::questionYesNo(widget(), xi18nc("@info", "The archive %1 already exists. Would you like to open it instead?", localFile), i18nc("@title:window", "File Exists"), KGuiItem(i18n("Open File")), KStandardGuiItem::cancel()); + + if (overwrite == KMessageBox::No) { + return false; + } + } + displayMsgWidget(KMessageWidget::Information, xi18nc("@info", "The archive %1 will be created as soon as you add a file.", localFile)); + } else { + if (!localFileInfo.exists()) { + displayMsgWidget(KMessageWidget::Error, xi18nc("@info", "The archive %1 was not found.", localFile)); + return false; + } + + if (!localFileInfo.isReadable()) { + displayMsgWidget(KMessageWidget::Error, xi18nc("@info", "The archive %1 could not be loaded, as it was not possible to read from it.", localFile)); + return false; + } + } + + return true; +} + void Part::slotLoadingStarted() { } @@ -626,11 +631,9 @@ if (job->error()) { if (arguments().metaData()[QStringLiteral("createNewArchive")] != QLatin1String("true")) { if (job->error() != KJob::KilledJobError) { - KMessageBox::error(widget(), - xi18nc("@info", "Loading the archive %1 failed with the following error:%2", - localFilePath(), - job->errorText()), - i18nc("@title:window", "Error Opening Archive")); + displayMsgWidget(KMessageWidget::Error, xi18nc("@info", "Loading the archive %1 failed with the following error:%2", + localFilePath(), + job->errorText())); } // The file failed to open, so reset the open archive, info panel and caption.