Commit 39c10221 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇
Browse files

[Previewer] Use file name in archive for window caption

Instead of showing "file:///tmp/ark-blabla.txt" it will show the path inside
the archive, e.g. "foo/bar.txt"
parent 519d8587
Pipeline #127985 passed with stage
in 2 minutes and 59 seconds
......@@ -552,6 +552,11 @@ TempExtractJob::TempExtractJob(Archive::Entry *entry, bool passwordProtectedHint
m_tmpExtractDir = new QTemporaryDir();
}
Archive::Entry *TempExtractJob::entry() const
{
return m_entry;
}
QString TempExtractJob::validatedFilePath() const
{
QString path = extractionDir() + QLatin1Char('/') + m_entry->fullPath();
......
......@@ -235,6 +235,8 @@ class KERFUFFLE_EXPORT TempExtractJob : public Job
public:
TempExtractJob(Archive::Entry *entry, bool passwordProtectedHint, ReadOnlyArchiveInterface *interface);
Archive::Entry *entry() const;
/**
* @return The absolute path of the extracted file.
* The path is validated in order to prevent directory traversal attacks.
......
......@@ -82,13 +82,13 @@ void ArkViewer::openExternalViewer(const KService::Ptr viewer, const QString& fi
job->start();
}
void ArkViewer::openInternalViewer(const KService::Ptr viewer, const QString& fileName, const QMimeType& mimeType)
void ArkViewer::openInternalViewer(const KService::Ptr viewer, const QString& fileName, const QString& entryPath, const QMimeType& mimeType)
{
qCDebug(ARK) << "Opening internal viewer";
ArkViewer *internalViewer = new ArkViewer();
internalViewer->show();
if (internalViewer->viewInInternalViewer(viewer, fileName, mimeType)) {
if (internalViewer->viewInInternalViewer(viewer, fileName, entryPath, mimeType)) {
// The internal viewer is showing the file, and will
// remove the temporary file in its destructor. So there
// is no more to do here.
......@@ -134,16 +134,16 @@ bool ArkViewer::askViewAsPlainText(const QMimeType& mimeType)
return response != KMessageBox::Cancel;
}
void ArkViewer::view(const QString& fileName)
void ArkViewer::view(const QString& fileName, const QString& entryPath)
{
QMimeDatabase db;
QMimeType mimeType = db.mimeTypeForFile(fileName);
qCDebug(ARK) << "viewing" << fileName << "with mime type:" << mimeType.name();
qCDebug(ARK) << "viewing" << fileName << "from" << entryPath << "with mime type:" << mimeType.name();
const KService::Ptr internalViewer = ArkViewer::getInternalViewer(mimeType.name());
if (internalViewer) {
openInternalViewer(internalViewer, fileName, mimeType);
openInternalViewer(internalViewer, fileName, entryPath, mimeType);
return;
}
......@@ -158,17 +158,15 @@ void ArkViewer::view(const QString& fileName)
// should be previewed as text/plain.
if (askViewAsPlainText(mimeType)) {
const KService::Ptr textViewer = ArkViewer::getInternalViewer(QStringLiteral("text/plain"));
openInternalViewer(textViewer, fileName, db.mimeTypeForName(QStringLiteral("text/plain")));
openInternalViewer(textViewer, fileName, entryPath, db.mimeTypeForName(QStringLiteral("text/plain")));
} else {
qCDebug(ARK) << "Removing temporary file:" << fileName;
QFile::remove(fileName);
}
}
bool ArkViewer::viewInInternalViewer(const KService::Ptr viewer, const QString& fileName, const QMimeType &mimeType)
bool ArkViewer::viewInInternalViewer(const KService::Ptr viewer, const QString& fileName, const QString& entryPath, const QMimeType &mimeType)
{
setWindowFilePath(fileName);
// Set icon and comment for the mimetype.
m_iconLabel->setPixmap(QIcon::fromTheme(mimeType.iconName()).pixmap(style()->pixelMetric(QStyle::PixelMetric::PM_SmallIconSize)));
m_commentLabel->setText(mimeType.comment());
......@@ -199,6 +197,10 @@ bool ArkViewer::viewInInternalViewer(const KService::Ptr viewer, const QString&
m_part.data()->widget()->setFocus();
m_fileName = fileName;
// Needs to come after openUrl to override the part-provided caption
setWindowTitle(entryPath);
setWindowFilePath(fileName);
return true;
}
......
......@@ -25,7 +25,7 @@ class ArkViewer : public KParts::MainWindow, public Ui::ArkViewer
public:
~ArkViewer() override;
static void view(const QString& fileName);
static void view(const QString& fileName, const QString& entryPath = QString());
private:
explicit ArkViewer();
......@@ -34,11 +34,11 @@ private:
static KService::Ptr getInternalViewer(const QString& mimeType);
static void openExternalViewer(const KService::Ptr viewer, const QString& fileName);
static void openInternalViewer(const KService::Ptr viewer, const QString& fileName, const QMimeType& mimeType);
static void openInternalViewer(const KService::Ptr viewer, const QString& fileName, const QString& entryPath, const QMimeType& mimeType);
static bool askViewAsPlainText(const QMimeType& mimeType);
bool viewInInternalViewer(const KService::Ptr viewer, const QString& fileName, const QMimeType& mimeType);
bool viewInInternalViewer(const KService::Ptr viewer, const QString& fileName, const QString &entryPath, const QMimeType& mimeType);
private Q_SLOTS:
void aboutKPart();
......
......@@ -1063,7 +1063,7 @@ void Part::slotPreviewExtractedEntry(KJob *job)
Q_ASSERT(previewJob);
m_tmpExtractDirList << previewJob->tempDir();
ArkViewer::view(previewJob->validatedFilePath());
ArkViewer::view(previewJob->validatedFilePath(), previewJob->entry()->fullPath(PathFormat::NoTrailingSlash));
} else if (job->error() != KJob::KilledJobError) {
KMessageBox::error(widget(), job->errorString());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment