Skip to content

Commit

Permalink
Archive preview support (#153)
Browse files Browse the repository at this point in the history
* Add archive/data preview interfaces
* Add raw data text parser
  • Loading branch information
Silarn authored Jul 11, 2024
1 parent ef56b9e commit bd5b856
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/ipluginpreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ class IPluginPreview : public QObject, public IPlugin
*/
virtual std::set<QString> supportedExtensions() const = 0;

/**
* @return return whether or not the preview supports raw data previews (likely
* sourced from an archive)
*/
virtual bool supportsArchives() const { return false; }

/**
* @brief generate a preview widget for the specified file
* @param fileName name of the file to preview
Expand All @@ -44,6 +50,18 @@ class IPluginPreview : public QObject, public IPlugin
virtual QWidget* genFilePreview(const QString& fileName,
const QSize& maxSize) const = 0;

/**
* @brief generate a preview widget from an archive file loaded into memory
* @param fileData data of file to preview
* @param fileName path of file in virtual filesystem
* @param maxSize maxiumum size of the generated widget
*/
virtual QWidget* genDataPreview(const QByteArray& fileData, const QString& fileName,
const QSize& maxSize) const
{
return nullptr;
}

private:
};

Expand Down
19 changes: 12 additions & 7 deletions src/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,30 +849,35 @@ bool shellDeleteQuiet(const QString& fileName, QWidget* dialog)

QString readFileText(const QString& fileName, QString* encoding)
{
QStringConverter::Encoding codec = QStringConverter::Encoding::Utf8;
QStringEncoder encoder(codec);
QStringDecoder decoder(codec);

QFile textFile(fileName);
if (!textFile.open(QIODevice::ReadOnly)) {
return QString();
}

QByteArray buffer = textFile.readAll();
QString text = decoder.decode(buffer);
return decodeTextData(buffer);
}

QString decodeTextData(const QByteArray& fileData, QString* encoding)
{
QStringConverter::Encoding codec = QStringConverter::Encoding::Utf8;
QStringEncoder encoder(codec);
QStringDecoder decoder(codec);
QString text = decoder.decode(fileData);

// check reverse conversion. If this was unicode text there can't be data loss
// this assumes QString doesn't normalize the data in any way so this is a bit unsafe
if (encoder.encode(text) != buffer) {
if (encoder.encode(text) != fileData) {
log::debug("conversion failed assuming local encoding");
auto codecSearch = QStringConverter::encodingForData(buffer);
auto codecSearch = QStringConverter::encodingForData(fileData);
if (codecSearch.has_value()) {
codec = codecSearch.value();
decoder = QStringDecoder(codec);
} else {
decoder = QStringDecoder(QStringConverter::Encoding::System);
}
text = decoder.decode(buffer);
text = decoder.decode(fileData);
}

if (encoding != nullptr) {
Expand Down
10 changes: 10 additions & 0 deletions src/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,16 @@ QDLLEXPORT QString getStartMenuDirectory();
**/
QDLLEXPORT QString readFileText(const QString& fileName, QString* encoding = nullptr);

/**
* @brief decode raw text data. This tries to guess the encoding used in the file
* @param fileData raw data to decode
* @param encoding (optional) if this is set, the target variable received the name of
*the encoding used
* @return the textual content of the file or an empty string if the file doesn't exist
**/
QDLLEXPORT QString decodeTextData(const QByteArray& fileData,
QString* encoding = nullptr);

/**
* @brief delete files matching a pattern
* @param directory in which to delete files
Expand Down

0 comments on commit bd5b856

Please sign in to comment.