-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfileutils.cpp
More file actions
165 lines (136 loc) · 4.88 KB
/
fileutils.cpp
File metadata and controls
165 lines (136 loc) · 4.88 KB
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
#include "fileutils.h"
const QString FileUtils::NOTES_FOLDER = "notes";
const QString FileUtils::APP_FOLDER = ".evernote";
const QString FileUtils::CONTENT_FILENAME = "content.html";
FileUtils::FileUtils(QObject *parent) :
QObject(parent)
{
}
void FileUtils::cacheNoteContent(NoteWrapper *note, QString content){
QDir mydocs(myDocsFolderPath());
mydocs.mkdir(APP_FOLDER);
QDir appDir(appFolderPath());
appDir.mkdir(NOTES_FOLDER);
QDir notesFolder(notesFolderPath());
QString noteFolderName = QString::fromStdString(note->getGuid());
notesFolder.mkdir(noteFolderName);
QDir noteFolder(noteFolderPath(note));
QString contentFileName = noteFolder.filePath(CONTENT_FILENAME);
qDebug() << "content file name: " << contentFileName;
QFile contentFile(contentFileName);
contentFile.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&contentFile);
out << content;
out.flush();
contentFile.flush();
contentFile.close();
}
void FileUtils::cacheResourceContent(Resource r){
QDir noteFolder(noteFolderPath(r.noteGuid));
noteFolder.mkdir(QString::fromStdString(r.guid));
QDir resourceFolder(resourceFolderPath(r));
QFile contentFile(resourceFolder.filePath(QString::fromStdString(r.guid)+"."+getExtension(r)));
contentFile.open(QIODevice::WriteOnly | QIODevice::Text);
qDebug() << "hash: " << QString::fromStdString(r.data.bodyHash);
contentFile.write(r.data.body.data(),r.data.size);
contentFile.flush();
contentFile.close();
}
QString FileUtils::myDocsFolderPath(){
return QStandardPaths::writableLocation(QStandardPaths::DataLocation);
}
QString FileUtils::appFolderPath(){
return myDocsFolderPath() + QDir::separator() + APP_FOLDER;
}
QString FileUtils::notesFolderPath(){
return appFolderPath() + QDir::separator() + NOTES_FOLDER;
}
QString FileUtils::noteFolderPath(NoteWrapper *note){
return noteFolderPath(note->getGuid());
}
QString FileUtils::noteFolderPath(std::string noteGuid){
return notesFolderPath() + QDir::separator()+QString::fromStdString(noteGuid);
}
QString FileUtils::noteContentFilePath(NoteWrapper *note){
return noteFolderPath(note) + QDir::separator() + CONTENT_FILENAME;
}
QString FileUtils::resourceFolderPath(Resource r){
return notesFolderPath() + QDir::separator() + QString::fromStdString(r.noteGuid)+ QDir::separator() + QString::fromStdString(r.guid);
}
QString FileUtils::resourceContentFilePath(Resource r){
return resourceFolderPath(r) + QDir::separator() + QString::fromStdString(r.guid)+"."+getExtension(r);
}
bool FileUtils::noteCached(NoteWrapper *note){
QFile contentFile(noteContentFilePath(note));
return contentFile.exists();
}
bool FileUtils::resourceCached(Resource r){
QFile resourceContentFile(resourceContentFilePath(r));
return resourceContentFile.exists();
}
QString FileUtils::getExtension(Resource r){
if(r.mime == "audio/mpeg"){
return "mp3";
}else if(r.mime == "image/jpeg" || r.mime == "image/pjpeg" || r.mime == "image/jpg"){
return "jpeg";
}else if(r.mime == "text/plain"){
return "txt";
}else if(r.mime == "text/html"){
return "html";
}else if(r.mime == "image/png"){
return "png";
}else if(r.mime == "image/gif"){
return "gif";
}else if(r.mime == "image/bmp"){
return "bmp";
}else if(r.mime == "audio/wav" || r.mime == "audio/vnd.wave"){
return "wav";
}else if(r.mime == "audio/amr"){
return "amr";
}else if(r.mime == "application/pdf"){
return "pdf";
}else if(r.mime == "application/msword"){
return "doc";
}else if(r.mime == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"){
return "docx";
}
return "";
}
bool FileUtils::removeDir(const QString &dirName)
{
bool result = true;
QDir dir(dirName);
if (dir.exists(dirName)) {
Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
if (info.isDir()) {
result = removeDir(info.absoluteFilePath());
}
else {
result = QFile::remove(info.absoluteFilePath());
}
if (!result) {
return result;
}
}
result = dir.rmdir(dirName);
}
return result;
}
void FileUtils::removeNoteCache(Note note){
removeDir(noteFolderPath(note.guid));
}
void FileUtils::removeNoteDir(){
removeDir(notesFolderPath());
}
QString FileUtils::readNoteContent(NoteWrapper* note){
QFile contentFile(FileUtils::noteContentFilePath(note));
contentFile.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&contentFile);
QString result = "";
while(!in.atEnd()){
QString line = in.readLine();
result += line;
}
contentFile.close();
return result;
}