-
Notifications
You must be signed in to change notification settings - Fork 2
/
NotesManager.cpp
219 lines (182 loc) · 5.9 KB
/
NotesManager.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
#include "NotesManager.h"
#include "Note.h"
#include "NoteFactory.h"
#include "ExportStrategy.h"
#include "NotesException.h"
#include <QString>
#include <QSettings>
#include <QMap>
#include <QFile>
#include <QDir>
#include <QTextStream>
NoteType DetectType(const QString& fileName){
if(fileName.endsWith(".art")){
return article;
}
else if(fileName.endsWith(".doc")){
return document;
}
else if(fileName.endsWith(".img")){
return imageNote;
}
else if(fileName.endsWith(".aud")){
return audioNote;
}
else if(fileName.endsWith(".vid")){
return videoNote;
}
else
return unknownType;
}
void NotesManager::addNote(Note* a){
rootDocument->addNote(a);
if(a->isDocument())
DocumentsContainer << static_cast<Document*>(a);
}
void NotesManager::removeNote(Note *a)
{
rootDocument->removeNote(a);
if(a->isDocument())
DocumentsContainer.remove(static_cast<Document*>(a));
}
Note& NotesManager::getNote(const QString& fileName){
Note* res = rootDocument->find(fileName);
if(res){
qDebug()<<"gotcha";
return *res;
}
// sinon, il faut le loader
NoteType type = DetectType(fileName);
if(type == unknownType)
throw NotesException("File type not supported now! Please send an email to [email protected] for support! :P");
NoteFactory* f = NotesManager::factories->value(type);
Note* n = f->buildNote(fileName);
addNote(n);
qDebug()<<"New NOTE:::::::::::::::::"<<n->getFilePath();
n->setModified(false);
return *n;
}
Note &NotesManager::getNoteClone(const Note& note)
{
// if(!mapper)
// OldPath, NewPath mapper
// mapper = new QMultiMap<QString, QString>();
if(mapper->value(note.getFilePath()).isNull()){
NoteFactory* f = NotesManager::factories->value(note.type);
Note * n = f->buildNoteCopy(note);
addNote(n);
mapper->insert(note.getFilePath(), n->getFilePath());
return *n;
} else {
return *rootDocument->find(mapper->value(note.getFilePath()));
}
}
Note& NotesManager::getNewNote(NoteType type){
if(type == unknownType)
throw NotesException("File type not supported now! Please send an email to [email protected] for support! :P");
NoteFactory* f = NotesManager::factories->value(type);
Note* n = f->buildNewNote();
addNote(n);
return *n;
}
/**
* @brief NotesManager::instance SINGLETON!
*/
NotesManager* NotesManager::instance=0; // pointeur sur l'unique instance
NotesManager& NotesManager::getInstance(){
if (!instance)
{
instance=new NotesManager;
// TODO, populates this by file stored on disk.
QSettings settings;
QVariant rootPath = settings.value("rootDocument");
qDebug()<<"rootPath = " << rootPath << "During NotesManager construction";
if(!rootPath.isNull()){
Document* old = instance->getRootDocument();
instance->setRootDocument(static_cast<Document *>(instance->factories->value(document)->buildNote(rootPath.toString())));
if(!instance->getRootDocument()){
qDebug()<<"Init loading failed: "<<rootPath;
instance->setRootDocument(old);
} else {
delete old;
}
instance->getRootDocument()->setModified(false);
}
settings.setValue("workspaceChanged", false);
}
return *instance;
}
void NotesManager::libererInstance(){
if (instance) delete instance;
instance=0;
}
NotesManager::NotesManager()
:mapper(0)
{
mapper = new QMultiMap<QString, QString>();
factories = NoteFactory::getFactories();
strategies = ExportStrategy::getStrategies();
rootDocument = static_cast<Document *>(factories->value(document)->buildNewNote());
rootDocument->setTitle("~");
}
NotesManager::~NotesManager(){
// for(nListIt it = begin(); it!= end(); it++) {
// saveNote((**it));
// delete *it;
// }
delete mapper;
}
void NotesManager::saveDocument(Document& d){
for(nListIt it = d.begin(); it!= d.end(); ++it){
if(*it != &d)
saveNote(**it);
}
// Saving in reverse order. So that all path saved are valid
// Prepare parent directories
QString fp = d.getFilePath();
fp.truncate(fp.lastIndexOf('/'));
QDir().mkpath(fp);
// Creation d'un objet QFile
QFile file(d.getFilePath());
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
throw NotesException("Failed to save your note, please check if I have the permission to write on your harddisk and stop hacking the software!");
ExportStrategy* es = NotesManager::strategies->value(saveText);
QString q = d.exportNote(es, 0);
QTextStream flux(&file);
flux<<q;
file.close();
d.setModified(false);
}
void NotesManager::saveNote(Note& a){
if (a.isModified()) {
qDebug()<<"saving Note:"<<a.getTitle();
if(!a.isDocument()){
// Prepare parent directories
QString fp = a.getFilePath();
fp.truncate(fp.lastIndexOf('/'));
QDir().mkpath(fp);
// Creation d'un objet QFile
QFile file(a.getFilePath());
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
throw NotesException("Failed to save your note, please check if I have the permission to write on your harddisk and stop hacking the software!");
ExportStrategy* es = NotesManager::strategies->value(saveText);
QString q = a.exportNote(es);
QTextStream flux(&file);
flux<<q;
file.close();
a.setModified(false);
}
else{
saveDocument(static_cast<Document&>(a));
}
}
a.setModified(false);
}
Document *NotesManager::getRootDocument() const
{
return rootDocument;
}
void NotesManager::setRootDocument(Document *value)
{
rootDocument = value;
}