forked from pbek/QOwnNotes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoteapi.cpp
More file actions
216 lines (184 loc) · 4.92 KB
/
noteapi.cpp
File metadata and controls
216 lines (184 loc) · 4.92 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
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
#include "noteapi.h"
#include <entities/notefolder.h>
#include <entities/tag.h>
#include <QVector>
#include <utility>
#include "tagapi.h"
NoteApi* NoteApi::fetch(int id) {
_note = Note::fetch(id);
if (_note.isFetched()) {
_id = _note.getId();
_name = _note.getName();
_fileName = _note.getFileName();
_noteText = _note.getNoteText();
_hasDirtyData = _note.getHasDirtyData();
_noteSubFolderId = _note.getNoteSubFolderId();
_fileCreated = _note.getFileCreated();
_fileLastModified = _note.getFileLastModified();
// we'll try not to fetch the decrypted note text, because it
// would be done every time the current note changes
_decryptedNoteText = _note.getDecryptedNoteText();
}
return this;
}
NoteApi* NoteApi::fromNote(const Note& note) {
auto* noteApi = new NoteApi();
noteApi->fetch(note.getId());
return noteApi;
}
// NoteApi NoteApi::fromNote(Note note) {
// NoteApi noteApi;
// noteApi.fetch(note.getId());
// return noteApi;
//}
/**
* Returns all linked tags
*/
QQmlListProperty<TagApi> NoteApi::tags() {
_tags.clear();
Note note = Note::fetch(_id);
QVector<Tag> tags = Tag::fetchAllOfNote(note);
QVectorIterator<Tag> itr(tags);
while (itr.hasNext()) {
Tag tag = itr.next();
auto* tagApi = new TagApi();
tagApi->copy(tag);
_tags.append(tagApi);
}
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
return {this, _tags};
#else
return {this, &_tags};
#endif
}
/**
* Returns the names of all linked tags
*/
QStringList NoteApi::tagNames() const {
QStringList tagNameList;
Note note = Note::fetch(_id);
QVector<Tag> tags = Tag::fetchAllOfNote(note);
QVectorIterator<Tag> itr(tags);
while (itr.hasNext()) {
Tag tag = itr.next();
tagNameList.append(tag.getName());
}
return tagNameList;
}
/**
* Adds a tag to the note
*
* @param tagName
* @return true if the note was tagged
*/
bool NoteApi::addTag(const QString& tagName) {
if (tagName.isEmpty()) {
return false;
}
Note note = Note::fetch(_id);
if (!note.isFetched()) {
return false;
}
// create a new tag if it doesn't exist
Tag tag = Tag::fetchByName(tagName);
if (!tag.isFetched()) {
tag.setName(tagName);
tag.store();
}
return tag.linkToNote(note);
}
/**
* Removes a tag from the note
*
* @param tagName
* @return true if the tag was removed from the note
*/
bool NoteApi::removeTag(QString tagName) {
Tag tag = Tag::fetchByName(std::move(tagName));
if (!tag.isFetched()) {
return false;
}
Note note = Note::fetch(_id);
if (!note.isFetched()) {
return false;
}
return tag.removeLinkToNote(note);
}
/**
* Renames a note file
*
* @param newName new file name (without file-extension)
* @return true if the note was renamed
*/
bool NoteApi::renameNoteFile(const QString &newName) {
Note note = Note::fetch(_id);
if (note.isFetched()) {
return note.renameNoteFile(newName);
}
return false;
}
/**
* Checks if it is allowed to have a different note file name than the headline
*
* @return bool
*/
bool NoteApi::allowDifferentFileName() {
return Note::allowDifferentFileName();
}
/**
* Fetches all notes
* Disclaimer: not tested, might not work yet
*
* @param limit
* @param offset
* @return
*/
QQmlListProperty<NoteApi> NoteApi::fetchAll(int limit, int offset) {
const QVector<int> noteIds = Note::fetchAllIds(limit, offset);
QList<NoteApi*> notes;
for (int noteId : noteIds) {
NoteApi* note = NoteApi::fetch(noteId);
notes.append(note);
}
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
return {this, notes};
#else
return {this, ¬es};
#endif
}
/**
* Returns the generated html for a note
*
* @param forExport if true (default) the export-html will be generated
* @return
*/
QString NoteApi::toMarkdownHtml(bool forExport) {
return _note.toMarkdownHtml(NoteFolder::currentLocalPath(), 980, forExport,
true, true);
}
/**
* Returns the absolute file url from a relative file name
*
* @param fileName
* @return
*/
QString NoteApi::getFileURLFromFileName(const QString& localFileName) {
return _note.getFileURLFromFileName(localFileName);
}
void NoteApi::copy(const Note ¬e)
{
_note = note;
if (_note.isFetched()) {
_id = note.getId();
_name = note.getName();
_fileName = note.getFileName();
_noteText = note.getNoteText();
_hasDirtyData = note.getHasDirtyData();
_noteSubFolderId = note.getNoteSubFolderId();
_fileCreated = note.getFileCreated();
_fileLastModified = note.getFileLastModified();
// we'll try not to fetch the decrypted note text, because it
// would be done every time the current note changes
_decryptedNoteText = note.getDecryptedNoteText();
}
}