Debian Codegraph @

src/librssguard/miscellaneous/iofactory.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "miscellaneous/iofactory.h"
4
5 #include "definitions/definitions.h"
6 #include "exceptions/ioexception.h"
7 #include "exceptions/processexception.h"
8
9 #include <QDataStream>
10 #include <QDir>
11 #include <QFile>
12 #include <QFileInfo>
13 #include <QObject>
14 #include <QProcess>
15 #include <QTemporaryFile>
16
17 IOFactory::IOFactory() = default;
18
19 bool IOFactory::isFolderWritable(const QString& folder) {
20 QString real_file = folder;
21
22 if (!real_file.endsWith(QDir::separator())) {
23 real_file += QDir::separator();
24 }
25
26 real_file += QSL("test-permissions-file");
27 return QTemporaryFile(real_file).open();
28 }
29
30 QString IOFactory::getSystemFolder(QStandardPaths::StandardLocation location) {
31 QStringList locations = QStandardPaths::standardLocations(location);
32
33 return locations.isEmpty() ? QString() : locations.at(0);
34 }
35
36 QString IOFactory::ensureUniqueFilename(const QString& name, const QString& append_format) {
37 if (!QFile::exists(name)) {
38 return name;
39 }
40
41 QString tmp_filename = name;
42 int i = 1;
43
44 while (QFile::exists(tmp_filename)) {
45 tmp_filename = name;
46 const int index = tmp_filename.lastIndexOf(QL1C('.'));
47 const QString append_string = append_format.arg(i++);
48
49 if (index < 0) {
50 tmp_filename.append(append_string);
51 }
52 else {
53 tmp_filename = tmp_filename.left(index) + append_string + tmp_filename.mid(index);
54 }
55 }
56
57 return tmp_filename;
58 }
59
60 QString IOFactory::filterBadCharsFromFilename(const QString& name) {
61 QString value = name;
62
63 value.replace(QL1C('/'), QL1C('-'));
64 value.remove(QL1C('\\'));
65 value.remove(QL1C(':'));
66 value.remove(QL1C('*'));
67 value.remove(QL1C('?'));
68 value.remove(QL1C('"'));
69 value.remove(QL1C('<'));
70 value.remove(QL1C('>'));
71 value.remove(QL1C('|'));
72 return value;
73 }
74
75 bool IOFactory::startProcessDetached(const QString& executable,
76 const QStringList& arguments,
77 const QString& working_directory) {
78 QProcess process;
79
80 process.setProgram(executable);
81 process.setArguments(arguments);
82 process.setWorkingDirectory(working_directory);
83
84 return process.startDetached(nullptr);
85 }
86
87 void IOFactory::startProcess(QProcess* const proc,
88 const QString& executable,
89 const QStringList& arguments,
90 const QProcessEnvironment& pe,
91 const QString& working_directory) {
92 proc->setProgram(executable);
93 proc->setArguments(arguments);
94
95 QProcessEnvironment system_pe = QProcessEnvironment::systemEnvironment();
96
97 system_pe.insert(pe);
98 proc->setProcessEnvironment(system_pe);
99
100 if (!working_directory.isEmpty()) {
101 proc->setWorkingDirectory(working_directory);
102 }
103
104 proc->start();
105 }
106
107 QString IOFactory::startProcessGetOutput(const QString& executable,
108 const QStringList& arguments,
109 const QProcessEnvironment& pe,
110 const QString& working_directory) {
111 QProcess proc;
112
113 proc.setProgram(executable);
114 proc.setArguments(arguments);
115
116 QProcessEnvironment system_pe = QProcessEnvironment::systemEnvironment();
117
118 system_pe.insert(pe);
119 proc.setProcessEnvironment(system_pe);
120
121 if (!working_directory.isEmpty()) {
122 proc.setWorkingDirectory(working_directory);
123 }
124
125 proc.start();
126
127 if (proc.waitForFinished() && proc.exitStatus() == QProcess::ExitStatus::NormalExit &&
128 proc.exitCode() == EXIT_SUCCESS) {
129 return proc.readAllStandardOutput();
130 }
131 else {
132 QString err_output = proc.readAllStandardError().simplified();
133
134 throw ProcessException(proc.exitCode(),
135 proc.exitStatus(),
136 proc.error(),
137 err_output.isEmpty() ? proc.errorString() : err_output);
138 }
139 }
140
141 QByteArray IOFactory::readFile(const QString& file_path) {
142 QFile input_file(file_path);
143 QByteArray input_data;
144
145 if (input_file.open(QIODevice::Text | QIODevice::Unbuffered | QIODevice::ReadOnly)) {
146 input_data = input_file.readAll();
147 input_file.close();
148 return input_data;
149 }
150 else {
151 throw IOException(tr("Cannot open file '%1' for reading.").arg(QDir::toNativeSeparators(file_path)));
152 }
153 }
154
155 void IOFactory::writeFile(const QString& file_path, const QByteArray& data) {
156 QFile input_file(file_path);
157
158 if (input_file.open(QIODevice::WriteOnly)) {
159 input_file.write(data);
160 input_file.close();
161 }
162 else {
163 throw IOException(tr("Cannot open file '%1' for writting.").arg(QDir::toNativeSeparators(file_path)));
164 }
165 }
166
167 bool IOFactory::copyFile(const QString& source, const QString& destination) {
168 if (QFile::exists(destination)) {
169 QFile file(destination);
170
171 file.setPermissions(file.permissions() | QFileDevice::WriteOwner | QFileDevice::WriteUser |
172 QFileDevice::WriteGroup | QFileDevice::WriteOther);
173
174 if (!QFile::remove(destination)) {
175 return false;
176 }
177 }
178
179 return QFile::copy(source, destination);
180 }
181
182 void IOFactory::removeFolder(const QString& path) {
183 QDir dir(path);
184 dir.removeRecursively();
185 }