Newer
Older
/**
* A library for Qt app
*
* LICENSE: The GNU Lesser General Public License, version 3.0
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0
* @link https://github.com/akiraohgaki/qt-libs
*/
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
File::File(QObject *parent) : QObject(parent)
{}
QString File::rootPath()
{
return QDir::rootPath();
}
QString File::tempPath()
{
return QDir::tempPath();
}
QString File::homePath()
{
return QDir::homePath();
}
return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
return QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation);
return QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
QString File::kdehomePath()
// KDE System Administration/Environment Variables
// https://userbase.kde.org/KDE_System_Administration/Environment_Variables
// KDE 4 maybe uses $KDEHOME
QString path = QString::fromLocal8Bit(qgetenv("KDEHOME").constData());
if (path.isEmpty()) {
path = homePath() + "/.kde";
}
return path;
}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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;
}