Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'jb50567' into 'master'
[filemanager] Add a FileInfo QML type. Contributes to JB#50567

See merge request mer-core/nemo-qml-plugin-filemanager!36
  • Loading branch information
adenexter committed Jan 8, 2021
2 parents 013dd9e + e68ca2b commit 54b962c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/plugin/fileengine.cpp
Expand Up @@ -215,3 +215,8 @@ bool FileEngine::chmod(QString path,
}
return true;
}

QString FileEngine::extensionForFileName(const QString &fileName) const
{
return QMimeDatabase().suffixForFileName(fileName);
}
1 change: 1 addition & 0 deletions src/plugin/fileengine.h
Expand Up @@ -106,6 +106,7 @@ class FileEngine : public QObject
bool groupRead, bool groupWrite, bool groupExecute,
bool othersRead, bool othersWrite, bool othersExecute, bool nonprivileged = false);

Q_INVOKABLE QString extensionForFileName(const QString &fileName) const;

signals:
void clipboardCountChanged();
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/filemodel.cpp
Expand Up @@ -131,7 +131,7 @@ QVariant FileModel::data(const QModelIndex &index, int role) const
return info.fileName();

case MimeTypeRole:
return m_mimeDatabase.mimeTypeForFile(info.absoluteFilePath()).name();
return info.mimeType();

case SizeRole:
return info.size();
Expand Down
2 changes: 0 additions & 2 deletions src/plugin/filemodel.h
Expand Up @@ -43,7 +43,6 @@
#include <QBasicTimer>
#include <QDir>
#include <QFileSystemWatcher>
#include <QMimeDatabase>
#include <QVector>

/**
Expand Down Expand Up @@ -242,7 +241,6 @@ private slots:
QStringList m_nameFilters;
QVector<StatFileInfo> m_files;
QFileSystemWatcher *m_watcher;
QMimeDatabase m_mimeDatabase;
QBasicTimer m_timer;
ChangedFlags m_changedFlags;
};
Expand Down
1 change: 1 addition & 0 deletions src/plugin/plugin.cpp
Expand Up @@ -63,6 +63,7 @@ class Q_DECL_EXPORT NemoFileManagerPlugin : public QQmlExtensionPlugin
void registerTypes(const char *uri)
{
Q_ASSERT(uri == QLatin1String("Nemo.FileManager"));
qmlRegisterType<FileInfo>(uri, 1, 0, "FileInfo");
qmlRegisterType<FileModel>(uri, 1, 0, "FileModel");
qmlRegisterType<Sailfish::ArchiveModel>(uri, 1, 0, "ArchiveModel");
qmlRegisterType<FileWatcher>(uri, 1, 0, "FileWatcher");
Expand Down
67 changes: 44 additions & 23 deletions src/plugin/statfileinfo.cpp
Expand Up @@ -32,6 +32,8 @@

#include "statfileinfo.h"

#include <QMimeDatabase>

StatFileInfo::StatFileInfo() :
m_selected(false)
{
Expand All @@ -50,27 +52,17 @@ StatFileInfo::~StatFileInfo()

void StatFileInfo::setFile(QString fileName)
{
m_fileName = fileName;
refresh();
if (m_fileName != fileName) {
m_fileName = fileName;
refresh();
}
}

bool StatFileInfo::exists() const
{
return m_fileInfo.exists();
}

QString StatFileInfo::extension() const
{
// If there is only one token following a dot, prefer it to be the baseName
return m_fileInfo.completeBaseName().isEmpty() ? QString() : m_fileInfo.suffix();
}

QString StatFileInfo::baseName() const
{
QString rv(m_fileInfo.completeBaseName());
return rv.isEmpty() ? m_fileInfo.fileName() : rv;
}

bool StatFileInfo::isSafeToRead() const
{
// it is safe to read non-existing files
Expand Down Expand Up @@ -100,8 +92,29 @@ void StatFileInfo::refresh()
memset(&m_lstat, 0, sizeof(m_lstat));

m_fileInfo = QFileInfo(m_fileName);
if (m_fileName.isEmpty())
if (m_fileName.isEmpty()) {
m_mimeType = QString();
m_baseName = QString();
m_extension = QString();

fileChanged();

return;
}

// QMimeDatabase is just a pointer to a global static instance of the actual database so there's
// no real cost to constructing one when needed.
QMimeDatabase mimeDatabase;

m_mimeType = mimeDatabase.mimeTypeForFile(m_fileInfo).name();
m_extension = mimeDatabase.suffixForFileName(m_fileName);
m_baseName = m_fileInfo.fileName();

if (m_baseName.lastIndexOf(m_extension) == 1) {
m_extension.clear();
} else {
m_baseName.chop(m_extension.length() + 1);
}

m_archiveInfo.setFile(m_fileName);

Expand All @@ -115,16 +128,16 @@ void StatFileInfo::refresh()
}
// if not symlink, then just copy lstat data to stat
if (!S_ISLNK(m_lstat.st_mode)) {
memcpy(&m_stat, &m_lstat, sizeof(m_stat));
return;
}

// check the file after following possible symlinks
res = stat64(fn, &m_stat);
if (res != 0) { // if error, then set to undefined
m_stat.st_mode = 0;
memcpy(&m_stat, &m_lstat, sizeof(m_stat));
} else {
// check the file after following possible symlinks
res = stat64(fn, &m_stat);
if (res != 0) { // if error, then set to undefined
m_stat.st_mode = 0;
}
}

fileChanged();
}

bool operator==(const StatFileInfo &lhs, const StatFileInfo &rhs)
Expand All @@ -143,3 +156,11 @@ bool operator!=(const StatFileInfo &lhs, const StatFileInfo &rhs)
return !operator==(lhs, rhs);
}

FileInfo::FileInfo(QObject *parent)
: QObject(parent)
{
}

FileInfo::~FileInfo()
{
}
40 changes: 38 additions & 2 deletions src/plugin/statfileinfo.h
Expand Up @@ -50,9 +50,12 @@ class StatFileInfo
explicit StatFileInfo(QString fileName);
~StatFileInfo();

QString file() const { return m_fileName; }
void setFile(QString fileName);
QString fileName() const { return m_fileInfo.fileName(); }

QString mimeType() const { return m_mimeType; }

// these inspect the file itself without following symlinks

// directory
Expand Down Expand Up @@ -105,8 +108,8 @@ class StatFileInfo
QDateTime lastModified() const { return m_fileInfo.lastModified(); }
QDateTime lastAccessed() const { return m_fileInfo.lastRead(); }
QDateTime created() const { return m_fileInfo.created(); }
QString extension() const;
QString baseName() const;
QString extension() const { return m_extension; }
QString baseName() const { return m_baseName; }
bool exists() const;
bool isSafeToRead() const;

Expand All @@ -125,8 +128,14 @@ class StatFileInfo

void refresh();

protected:
virtual void fileChanged() {}

private:
QString m_fileName;
QString m_baseName;
QString m_extension;
QString m_mimeType;
QFileInfo m_fileInfo;
Sailfish::ArchiveInfo m_archiveInfo;
struct stat64 m_stat; // after following possible symlinks
Expand All @@ -137,4 +146,31 @@ class StatFileInfo
bool operator==(const StatFileInfo &lhs, const StatFileInfo &rhs);
bool operator!=(const StatFileInfo &lhs, const StatFileInfo &rhs);

class FileInfo : public QObject, protected StatFileInfo
{
Q_OBJECT
Q_PROPERTY(QString file READ file WRITE setFile NOTIFY fileChanged)
Q_PROPERTY(QString fileName READ fileName NOTIFY fileChanged)
Q_PROPERTY(QString mimeType READ mimeType NOTIFY fileChanged)
Q_PROPERTY(qint64 size READ size NOTIFY fileChanged)
Q_PROPERTY(QDateTime lastModified READ lastModified NOTIFY fileChanged)
Q_PROPERTY(bool isDir READ isDirAtEnd NOTIFY fileChanged)
Q_PROPERTY(bool isArchive READ isArchive NOTIFY fileChanged)
Q_PROPERTY(bool isLink READ isSymLink NOTIFY fileChanged)
Q_PROPERTY(QString symLinkTarget READ symLinkTarget NOTIFY fileChanged)
Q_PROPERTY(QString extension READ extension NOTIFY fileChanged)
Q_PROPERTY(QString absolutePath READ absoluteFilePath NOTIFY fileChanged)
Q_PROPERTY(QDateTime accessed READ lastAccessed NOTIFY fileChanged)
Q_PROPERTY(QString baseName READ baseName NOTIFY fileChanged)
Q_PROPERTY(QString directoryPath READ absolutePath NOTIFY fileChanged)
public:
explicit FileInfo(QObject *parent = nullptr);
~FileInfo() override;

Q_INVOKABLE void refresh() { StatFileInfo::refresh(); }

signals:
void fileChanged() override;
};

#endif // STATFILEINFO_H

0 comments on commit 54b962c

Please sign in to comment.