From 6589ee7063fe1806d1e1870fbe3d70715bb64138 Mon Sep 17 00:00:00 2001 From: Akira Ohgaki <akiraohgaki@gmail.com> Date: Wed, 17 Aug 2016 12:44:08 +0900 Subject: [PATCH] Add utility/file class --- src/utility/file.cpp | 193 +++++++++++++++++++++++++++++++++++++++++++ src/utility/file.h | 37 +++++++++ 2 files changed, 230 insertions(+) create mode 100644 src/utility/file.cpp create mode 100644 src/utility/file.h diff --git a/src/utility/file.cpp b/src/utility/file.cpp new file mode 100644 index 0000000..1ef3d41 --- /dev/null +++ b/src/utility/file.cpp @@ -0,0 +1,193 @@ +#include <QIODevice> +#include <QDir> +#include <QFile> +#include <QFileInfo> +#include <QTextStream> + +#include "file.h" + +namespace Utility { + +File::File(QObject *parent) : QObject(parent) +{} + +QString File::rootPath() +{ + return QDir::rootPath(); +} + +QString File::tempPath() +{ + return QDir::tempPath(); +} + +QString File::homePath() +{ + return QDir::homePath(); +} + +/** + * XDG Base Directory Specification + * http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html + */ + +QString File::xdgDataHomePath() +{ + QString path = QString::fromLocal8Bit(qgetenv("XDG_DATA_HOME").constData()); + if (path.isEmpty()) { + path = homePath() + "/.local/share"; + } + return path; +} + +QString File::xdgConfigHomePath() +{ + QString path = QString::fromLocal8Bit(qgetenv("XDG_CONFIG_HOME").constData()); + if (path.isEmpty()) { + path = homePath() + "/.config"; + } + return path; +} + +QString File::xdgCacheHomePath() +{ + QString path = QString::fromLocal8Bit(qgetenv("XDG_CACHE_HOME").constData()); + if (path.isEmpty()) { + path = homePath() + "/.cache"; + } + return path; +} + +QFileInfoList File::readDir(const QString &path) +{ + QDir dir(path); + dir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot); + //dir.setSorting(QDir::DirsFirst | QDir::Name); + return dir.entryInfoList(); +} + +bool File::makeDir(const QString &path) +{ + // This function will create all parent directories + QDir dir(path); + if (!dir.exists() && dir.mkpath(path)) { + return true; + } + return false; +} + +QString File::readText(const QString &path) +{ + QString data; + QFile file(path); + if (file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QTextStream in(&file); + in.setCodec("UTF-8"); + data = in.readAll(); + file.close(); + } + return data; +} + +bool File::writeText(const QString &path, const QString &data) +{ + QFile file(path); + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) { + QTextStream out(&file); + out.setCodec("UTF-8"); + out << data; + file.close(); + return true; + } + return false; +} + +QByteArray File::readBinary(const QString &path) +{ + QByteArray data; + QFile file(path); + if (file.exists() && file.open(QIODevice::ReadOnly)) { + data = file.readAll(); + file.close(); + } + return data; +} + +bool File::writeBinary(const QString &path, const QByteArray &data) +{ + QFile file(path); + if (file.open(QIODevice::WriteOnly)) { + file.write(data); + file.close(); + return true; + } + return false; +} + +bool File::copy(const QString &path, const QString &targetPath) +{ + // This function will copy files recursively + QFileInfo fileInfo(path); + if (fileInfo.isFile()) { + QFile file(path); + if (file.copy(targetPath)) { + return true; + } + } + else if (fileInfo.isDir()) { + QDir targetDir(targetPath); + QString targetDirName = targetDir.dirName(); + targetDir.cdUp(); + if (targetDir.mkdir(targetDirName)) { + QDir dir(path); + dir.setFilter(QDir::AllEntries | QDir::Hidden | QDir::System | QDir::NoDotAndDotDot); + QStringList entries = dir.entryList(); + foreach (const QString &entry, entries) { + if (!copy(path + "/" + entry, targetPath + "/" + entry)) { + return false; + } + } + return true; + } + } + return false; +} + +bool File::move(const QString &path, const QString &targetPath) +{ + QFileInfo fileInfo(path); + if (fileInfo.isFile()) { + QFile file(path); + if (file.rename(targetPath)) { + return true; + } + } + else if (fileInfo.isDir()) { + QDir dir(path); + if (dir.rename(path, targetPath)) { + return true; + } + } + return false; +} + +bool File::remove(const QString &path) +{ + // This function will remove files recursively + QFileInfo fileInfo(path); + if (fileInfo.isFile()) { + QFile file(path); + if (file.remove()) { + return true; + } + } + else if (fileInfo.isDir()) { + QDir dir(path); + if (dir.removeRecursively()) { + return true; + } + } + return false; +} + +} // namespace Utility diff --git a/src/utility/file.h b/src/utility/file.h new file mode 100644 index 0000000..24edd09 --- /dev/null +++ b/src/utility/file.h @@ -0,0 +1,37 @@ +#ifndef UTILITY_FILE_H +#define UTILITY_FILE_H + +#include <QObject> + +class QFileInfo; +typedef QList<QFileInfo> QFileInfoList; + +namespace Utility { + +class File : public QObject +{ + Q_OBJECT + +public: + explicit File(QObject *parent = 0); + + static QString rootPath(); + static QString tempPath(); + static QString homePath(); + static QString xdgDataHomePath(); + static QString xdgConfigHomePath(); + static QString xdgCacheHomePath(); + static QFileInfoList readDir(const QString &path); + static bool makeDir(const QString &path); + static QString readText(const QString &path); + static bool writeText(const QString &path, const QString &data); + static QByteArray readBinary(const QString &path); + static bool writeBinary(const QString &path, const QByteArray &data); + static bool copy(const QString &path, const QString &targetPath); + static bool move(const QString &path, const QString &targetPath); + static bool remove(const QString &path); +}; + +} // namespace Utility + +#endif // UTILITY_FILE_H -- GitLab