-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.cpp
300 lines (246 loc) · 8.12 KB
/
library.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
/* This file is part of Misli.
Misli 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 3 of the License, or
(at your option) any later version.
Misli 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 Misli. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include "global.h"
#include "library.h"
#include "misli_desktop/misliwindow.h"
#include "misli_desktop/mislidesktopgui.h"
Library::Library(QString storageLocation, bool enableFSWatch)
{
fsWatchIsEnabled = enableFSWatch;
//FS-watch stuff
if(fsWatchIsEnabled){
fs_watch = new QFileSystemWatcher(this);
connect(fs_watch,SIGNAL(fileChanged(QString)),this,SLOT(handleChangedFile(QString)) );
hangingNfCheck = new QTimer;
connect(hangingNfCheck,SIGNAL(timeout()),this,SLOT(checkForHangingNFs() ));
}else{
fs_watch = nullptr;
hangingNfCheck = nullptr;
}
//Connect propery changes
connect(this,SIGNAL(noteFilesChanged()),this,SLOT(reinitNotesPointingToNotefiles()));
//Setup
QDir newDir(storageLocation);
if(!newDir.exists()){
qDebug() << "[Library::setDirectoryPath]Directory missing, creating it.";
if(!newDir.mkdir(storageLocation)){
qDebug() << "[Library::setDirectoryPath]Failed making directory: " << storageLocation;
return;
}
}
folderPath = storageLocation;
loadNoteFiles();
// Load the hacky tags note if it exists somewhere
for (auto nf: noteFiles_m){
for(auto nt: nf->notes){
if(nt->text().startsWith("define_filter_menu_tags:")){
auto lines = nt->text().split("\n", QString::SkipEmptyParts);
lines.pop_front(); // The "define_filter_menu_tags:"
filter_menu_tags = lines;
}
}
}
}
Library::~Library()
{
unloadAllNoteFiles();
delete fs_watch;
delete hangingNfCheck;
}
QList<NoteFile*> Library::noteFiles()
{
return noteFiles_m;
}
void Library::checkForHangingNFs()
{
int missingNfCount=0;
for(NoteFile * nf: noteFiles_m){
if(!nf->isReadable){
//Try to init
if( nf->loadFromFilePath() == 0 ){ //File is found and initialized properly
nf->isReadable = true;
//qDebug()<<"Adding path to fs_watch: "<<nf->filePath_m;
fs_watch->addPath(nf->filePath()); //when a file is deleted it gets off the fs_watch and we need to re-add it when a unix-type file save takes place
}else{
missingNfCount++;
}
}
}
if(missingNfCount==0) hangingNfCheck->stop(); //if none are missing - stop checking
}
NoteFile * Library::noteFileByName(QString name)
{
for(NoteFile *nf: noteFiles_m){
if(nf->name()==name){
return nf;
}
}
return nullptr;
}
NoteFile * Library::defaultNoteFile()
{
if(noteFiles_m.isEmpty()){
qDebug()<<"[Library::defaultNfOnStartup] No note files.";
return nullptr;
}
for(NoteFile *nf: noteFiles_m){
if(nf->isDisplayedFirstOnStartup) return nf;
}
return noteFiles_m[0];
}
int Library::makeCanvas(QString name)
{
if(noteFileByName(name) != nullptr){ //Check if such a NF doesn't exist already
return -1;
}
QString filePath = QDir(folderPath).filePath(name + ".json");
QFile ntFile(filePath);
if(!ntFile.open(QIODevice::WriteOnly)){ //Creates the NF
qDebug()<<"Error making new notes file";
return -1;
}
ntFile.close();
loadNoteFile(filePath);
return 0;
}
void Library::unloadNoteFile(NoteFile* nf)
{
if(fsWatchIsEnabled) fs_watch->removePath(nf->filePath());
noteFiles_m.removeOne(nf);
delete nf;
emit noteFilesChanged();
}
void Library::loadNoteFiles()
{
unloadAllNoteFiles();
QDir dir(folderPath);
QStringList nfs_misl = dir.entryList(QStringList()<<"*.misl", QDir::Files);
QStringList nfs_json = dir.entryList(QStringList()<<"*.json", QDir::Files);
// First load any .misl (legacy) note files and backup + convert them
for(QString fileName: nfs_misl){
//backup
QString oldPath = dir.absoluteFilePath(fileName);
QString backupPath = oldPath + ".backup";
if (QFile::exists(backupPath))
{
QFile::remove(backupPath);
}
QFile::copy(oldPath, backupPath);
//load nf
loadNoteFile(oldPath);
}
//Rename all misl to json
for(NoteFile *nf: noteFiles()){
QString newName = nf->filePath();
newName.chop(5);
newName = newName + ".json";
renameNoteFile(nf, newName);
}
for(QString fileName: nfs_json){
loadNoteFile(dir.absoluteFilePath(fileName));
}
}
void Library::reinitNotesPointingToNotefiles()
{
for(NoteFile *nf: noteFiles_m){
for(Note *nt: nf->notes){
if(nt->type==NoteType::redirecting) nt->checkTextForNoteFileLink();
}
}
}
void Library::handleChangedFile(QString filePath)
{
int err=0;
NoteFile *nf,dummyNF;
dummyNF.filePath_m = filePath; //Get the name
nf = noteFileByName(dummyNF.name()); //Locate the nf whith that name
if(nf==nullptr) return;//avoid segfaults on a wrong name
if( nf->loadFromFilePath()==0 ){
nf->isReadable = true;
}else if(err ==-2){ //most times the file is deleted and then replaced on sync , so we need to check back for it later
nf->isReadable=false;
hangingNfCheck->start(700);
}
emit noteFilesChanged();
}
void Library::unloadAllNoteFiles()
{
while(!noteFiles_m.isEmpty()){
unloadNoteFile(noteFiles_m.first());
}
}
double Library::defaultEyeZ()
{
return settings.value("eye_z",QVariant(90)).toDouble();
}
void Library::setDefaultEyeZ(double value)
{
settings.setValue("eye_z",QVariant(value));
settings.sync();
}
void Library::loadNoteFile(QString pathToNoteFile)
{
NoteFile *nf = new NoteFile;
nf->saveWithRequest = true;
nf->eyeZ = defaultEyeZ();
nf->setPathAndLoad(pathToNoteFile);
//If the file didn't init correctly - don't add it
if(!nf->isReadable | nf->name().isEmpty() ){
qDebug()<<"[Library::addNoteFile]Note file is not readable, skipping: " << pathToNoteFile;
delete nf;
return;
}
noteFiles_m.push_back(nf);
nf->saveStateToHistory(); //should be only a virtual save for ctrl-z
if(fsWatchIsEnabled) fs_watch->addPath(nf->filePath());
connect(nf,SIGNAL(requestingSave(NoteFile*)),this,SLOT(handleSaveRequest(NoteFile*)));
emit noteFilesChanged();
}
void Library::handleSaveRequest(NoteFile *nf)
{
nf->saveWithRequest = false;
if(fsWatchIsEnabled) fs_watch->removePath(nf->filePath());
nf->saveLastInHistoryToFile();
if(fsWatchIsEnabled) fs_watch->addPath(nf->filePath());
nf->saveWithRequest = true;
}
bool Library::renameNoteFile(NoteFile *nf, QString newName)
{
if(noteFileByName(newName) != nullptr){
return false;
}
QString newFilePath = QDir(folderPath).filePath(newName + ".json");
QString oldName = nf->name();
QFile file(nf->filePath());
if( !file.copy(newFilePath) ){ //Copy to a nf with the new name
qDebug() << "Error copying " << file.fileName() << " to " << newFilePath;
return false;
}
fs_watch->removePath(nf->filePath());//deal with fs_watch
nf->filePath_m = newFilePath;
nf->save();
nf->setPathAndLoad(nf->filePath());
file.remove();
//Now change all the notes that point to this one too
for(NoteFile *nf2: noteFiles_m){
for(Note *nt: nf2->notes){
if(nt->type==NoteType::redirecting){
if(nt->textForDisplay_m==oldName){
nt->changeText("this_note_points_to:" + newName);
}
}
}
}
return true;
}