forked from baumgarr/nixnote2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilemanager.cpp
301 lines (237 loc) · 10.2 KB
/
filemanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*********************************************************************************
NixNote - An open-source client for the Evernote service.
Copyright (C) 2013 Randy Baumgarte
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************************************/
#include "filemanager.h"
#include "global.h"
#include <iostream>
#include <cstdlib>
//*******************************************
//* This class is used to find the location
//* of various files & directories
//*******************************************
using namespace std;
FileManager::FileManager() {
}
void FileManager::setup(QString homeDirPath, QString programDirPath, int id) {
homeDir.setPath(toPlatformPathSeparator(homeDirPath));
programDir.setPath(toPlatformPathSeparator(programDirPath));
createDirOrCheckWriteable(homeDir);
this->homeDirPath = slashTerminatePath(homeDir.path());
this->programDirPath = slashTerminatePath(programDir.path());
// Read only files that everyone uses
imagesDir.setPath(programDirPath+"images");
checkExistingReadableDir(imagesDir);
imagesDirPath = slashTerminatePath(imagesDir.path());
javaDir.setPath(programDirPath+"java");
checkExistingReadableDir(javaDir);
javaDirPath = slashTerminatePath(javaDir.path());
// spellDir.setPath(programDirPath+"spell");
// checkExistingReadableDir(spellDir);
// spellDirPath = slashTerminatePath(spellDir.path());
spellDirUser.setPath(homeDirPath+"spell");
createDirOrCheckWriteable(spellDirUser);
spellDirPathUser = slashTerminatePath(spellDirUser.path());
//xmlDir.setPath(programDirPath+"xml");
//checkExistingReadableDir(xmlDir);
translateDir.setPath(programDirPath+"translations");
checkExistingReadableDir(translateDir);
translateDirPath= slashTerminatePath(translateDir.path());
qssDir.setPath(programDirPath+"qss");
checkExistingReadableDir(qssDir);
qssDirPath = slashTerminatePath(qssDir.path());
// Read/write directories that only we use
QString settingsFile = getHomeDirPath("") + "nixnote.conf";
QSettings globalSettings(settingsFile, QSettings::IniFormat);
if (id <=0) {
globalSettings.beginGroup("SaveState");
int accountId = globalSettings.value("lastAccessedAccount", 1).toInt();
globalSettings.endGroup();
id = accountId;
}
qssDirUser.setPath(homeDirPath+"qss");
createDirOrCheckWriteable(qssDirUser);
qssDirPathUser = slashTerminatePath(qssDirUser.path());
logsDir.setPath(homeDirPath+"logs-" +QString::number(id));
createDirOrCheckWriteable(logsDir);
logsDirPath = slashTerminatePath(logsDir.path());
tmpDir.setPath(homeDirPath+"tmp-" +QString::number(id));
createDirOrCheckWriteable(tmpDir);
tmpDirPath = slashTerminatePath(tmpDir.path());
dbDir.setPath(homeDirPath+"db-" +QString::number(id));
createDirOrCheckWriteable(dbDir);
dbDirPath = slashTerminatePath(dbDir.path());
dbaDir.setPath(dbDirPath+"dba");
createDirOrCheckWriteable(dbaDir);
dbaDirPath = slashTerminatePath(dbaDir.path());
thumbnailDir.setPath(dbDirPath+"tdba");
createDirOrCheckWriteable(thumbnailDir);
thumbnailDirPath = slashTerminatePath(thumbnailDir.path());
}
QString FileManager::toPlatformPathSeparator(QString relativePath) {
return relativePath;
}
/*************************************************/
/* Given a path, append either a / or a \ to */
/* form a fully qualified path */
/*************************************************/
QString FileManager::slashTerminatePath(QString path) {
if (!path.endsWith(QDir::separator())) {
return path + QDir::separator();
}
return path;
}
/*************************************************/
/* Delete files in a directory. This is used */
/* to cleanup temporary files. */
/*************************************************/
void FileManager::deleteTopLevelFiles(QDir dir, bool exitOnFail) {
dir.setFilter(QDir::Files);
QStringList list = dir.entryList();
for (qint32 i=0; i<list.size(); i++) {
QFile f(list.at(i));
if (!f.remove() && exitOnFail) {
cout << "Error deleting file '" +f.fileName().toStdString() <<
"'. Aborting program";
exit(16);
}
}
}
/*************************************************/
/* Create a directory if it doesn't exist. */
/*************************************************/
void FileManager::createDirOrCheckWriteable(QDir dir) {
if (!dir.exists()) {
if (!dir.mkdir(dir.path())) {
QLOG_FATAL() << "Failed to create directory '" << dir.path() << "'. Aborting program.";
exit(16);
}
} else {
if (!dir.isReadable()) {
QLOG_FATAL() << "Directory '" + dir.path() + "' does not have read permission. Aborting program.";
exit(16);
}
}
}
/**************************************************/
/* Check that an existing directory is readable. */
/**************************************************/
void FileManager::checkExistingReadableDir(QDir dir) {
if (!dir.isReadable()) {
QLOG_FATAL() << "Directory '" + dir.path() + "' does not have read permission. Aborting program.";
exit(16);
}
if (!dir.exists()) {
QLOG_FATAL() << "Directory '" + dir.path() + "' does not exist. Aborting program";
exit(16);
}
}
/**************************************************/
/* Check that an existing directory is writable. */
/**************************************************/
void FileManager::checkExistingWriteableDir(QDir dir) {
this->checkExistingReadableDir(dir);
}
QDir FileManager::getProgramDirFile(QString relativePath) {
return QDir(programDir.dirName() + toPlatformPathSeparator(relativePath));
}
QString FileManager::getProgramDirPath(QString relativePath) {
return programDirPath + toPlatformPathSeparator(relativePath);
}
QDir FileManager::getHomeDirFile(QString relativePath) {
return QDir(homeDir.dirName() + toPlatformPathSeparator(relativePath));
}
QString FileManager::getHomeDirPath(QString relativePath) {
return homeDirPath + toPlatformPathSeparator(relativePath);
}
//QString FileManager::getSpellDirPath(QString relativePath) {
// return spellDirPath + toPlatformPathSeparator(relativePath);
//}
//QDir FileManager::getSpellDirFile(QString relativePath) {
// return QDir(spellDir.dirName() + toPlatformPathSeparator(relativePath));
//}
//QString FileManager::getSpellDirPath() {
// return spellDirPath;
//}
QDir FileManager::getSpellDirFileUser(QString relativePath) {
return spellDirPathUser + toPlatformPathSeparator(relativePath);
}
QString FileManager::getSpellDirPathUser() {
return spellDirPathUser;
}
QString FileManager::getDbDirPath(QString relativePath) {
return dbDirPath + toPlatformPathSeparator(relativePath);
}
QDir FileManager::getImageDirFile(QString relativePath) {
return QDir(imagesDir.dirName()+ toPlatformPathSeparator(relativePath));
}
QString FileManager::getImageDirPath(QString relativePath) {
return imagesDirPath + toPlatformPathSeparator(relativePath);
}
QDir FileManager::getJavaDirFile(QString relativePath) {
return QDir(javaDir.dirName()+ toPlatformPathSeparator(relativePath));
}
QString FileManager::getJavaDirPath(QString relativePath) {
return javaDirPath + toPlatformPathSeparator(relativePath);
}
QDir FileManager::getLogsDirFile(QString relativePath) {
return QDir(logsDir.dirName() + toPlatformPathSeparator(relativePath));
}
QString FileManager::getLogsDirPath(QString relativePath) {
return logsDirPath + toPlatformPathSeparator(relativePath);
}
QString FileManager::getQssDirPath(QString relativePath) {
return qssDirPath + toPlatformPathSeparator(relativePath);
}
QString FileManager::getQssDirPathUser(QString relativePath) {
return qssDirPath + toPlatformPathSeparator(relativePath);
}
QString FileManager::getTmpDirPath() {
return tmpDirPath;
}
QString FileManager::getTmpDirPath(QString relativePath) {
return tmpDirPath + toPlatformPathSeparator(relativePath);
}
QString FileManager::getTmpDirPathSpecialChar(QString relativePath) {
return tmpDirPath + toPlatformPathSeparator(relativePath).replace("#", "%23");
}
QString FileManager::getDbaDirPath() {
return dbaDirPath;
}
QString FileManager::getDbaDirPath(QString relativePath) {
return dbaDirPath + toPlatformPathSeparator(relativePath);
}
QString FileManager::getDbaDirPathSpecialChar(QString relativePath) {
return dbaDirPath + toPlatformPathSeparator(relativePath).replace("#", "%23");
}
QString FileManager::getThumbnailDirPath() {
return thumbnailDirPath;
}
QString FileManager::getThumbnailDirPath(QString relativePath) {
return thumbnailDirPath + toPlatformPathSeparator(relativePath);
}
QString FileManager::getThumbnailDirPathSpecialChar(QString relativePath) {
return thumbnailDirPath + toPlatformPathSeparator(relativePath).replace("#", "%23");
}
/*
QDir FileManager::getXMLDirFile(QString relativePath) {
return QDir(xmlDir.dirName() + toPlatformPathSeparator(relativePath));
}
*/
QString FileManager::getTranslateFilePath(QString relativePath) {
return translateDirPath + toPlatformPathSeparator(relativePath);
}
void FileManager::purgeResDirectory(bool exitOnFail) {
this->deleteTopLevelFiles(tmpDir.dirName(), exitOnFail);
}