forked from baumgarr/nixnote2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavoritestable.cpp
301 lines (262 loc) · 8.82 KB
/
favoritestable.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) 2014 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 "favoritestable.h"
#include "sql/nsqlquery.h"
#include "sql/configstore.h"
#include "sql/tagtable.h"
#include "sql/notebooktable.h"
#include "sql/notetable.h"
#include "sql/searchtable.h"
// Generic constructor
FavoritesTable::FavoritesTable(DatabaseConnection *db, QObject *parent) :
QObject(parent)
{
this->db = db;
}
// Get the LID of all favorites
void FavoritesTable::getAll(QList<qint32> &lids) {
lids.empty();
db->lockForRead();
NSqlQuery query(db);
query.prepare("Select lid from datastore where key=:key");
query.bindValue(":key", FAVORITES_TARGET);
query.exec();
while (query.next()) {
lids.append(query.value(0).toInt());
}
query.finish();
db->unlock();
}
// Fetch an individual favorite
bool FavoritesTable::get(FavoritesRecord &record, qint32 lid) {
NSqlQuery query(db);
record.parent = 0;
db->lockForRead();
query.prepare("select key,data from datastore where lid=:lid");
query.bindValue(":lid", lid);
query.exec();
int type = 0;
bool retval = false;
while (query.next()) {
retval = true;
record.lid = lid;
int key = query.value(0).toInt();
switch (key) {
case FAVORITES_ORDER :
record.order = query.value(1).toInt();
break;
case FAVORITES_TYPE :
type = query.value(1).toInt();
break;
case FAVORITES_TARGET :
record.target = query.value(1);
break;
case FAVORITES_PARENT :
record.parent = query.value(1).toInt();
}
}
query.finish();
db->unlock();
switch (type) {
case FavoritesRecord::Note :
record.type = FavoritesRecord::Note;
break;
case FavoritesRecord::LocalNotebook :
record.type = FavoritesRecord::LocalNotebook;
break;
case FavoritesRecord::SynchronizedNotebook :
record.type = FavoritesRecord::SynchronizedNotebook;
break;
case FavoritesRecord::ConflictNotebook :
record.type = FavoritesRecord::ConflictNotebook;
break;
case FavoritesRecord::NotebookStack :
record.type = FavoritesRecord::NotebookStack;
break;
case FavoritesRecord::SharedNotebook :
record.type = FavoritesRecord::SharedNotebook;
break;
case FavoritesRecord::LinkedNotebook :
record.type = FavoritesRecord::LinkedNotebook;
break;
case FavoritesRecord::LinkedStack :
record.type = FavoritesRecord::LinkedStack;
break;
case FavoritesRecord::Tag :
record.type = FavoritesRecord::Tag;
break;
case FavoritesRecord::Search :
record.type = FavoritesRecord::Search;
break;
}
record.displayName = "<missing value>";
if (record.type == FavoritesRecord::Tag) {
TagTable tagTable(db);
Tag t;
tagTable.get(t, record.target.toInt());
if (t.name.isSet())
record.displayName = t.name;
}
if (record.type == FavoritesRecord::Note) {
NoteTable table(db);
Note r;
table.get(r, record.target.toInt(), false,false);
if (r.title.isSet())
record.displayName = r.title;
}
if (record.type == FavoritesRecord::LocalNotebook||
record.type == FavoritesRecord::SynchronizedNotebook||
record.type == FavoritesRecord::ConflictNotebook ||
record.type == FavoritesRecord::SharedNotebook ||
record.type == FavoritesRecord::LinkedNotebook) {
NotebookTable table(db);
Notebook r;
table.get(r, record.target.toInt());
if (r.name.isSet())
record.displayName = r.name;
}
if (record.type == FavoritesRecord::Search) {
SearchTable table(db);
SavedSearch r;
table.get(r, record.target.toInt());
if (r.name.isSet())
record.displayName = r.name;
}
if (record.type == FavoritesRecord::NotebookStack ||
record.type == FavoritesRecord::LinkedStack) {
record.displayName = record.target.toString();
}
return retval;
}
// Change the sort order of the favorite records
void FavoritesTable::setOrder(QList< QPair< qint32, qint32 > > order) {
NSqlQuery query(db);
db->lockForWrite();
query.prepare("delete from datastore where key=:key");
query.bindValue(":key", FAVORITES_ORDER);
query.exec();
query.prepare("Insert into datastore (lid, key, data) values (:lid, :key, :value)");
for (int i=0; i<order.size(); i++) {
query.bindValue(":lid", order[i].first);
query.bindValue(":key", FAVORITES_ORDER);
query.bindValue(":value", order[i].second);
query.exec();
}
query.finish();
db->unlock();
}
// Erase a favorite record
void FavoritesTable::expunge(qint32 lid) {
NSqlQuery query(db);
db->lockForWrite();
query.prepare("delete from datastore where lid=:key");
query.bindValue(":key", lid);
query.exec();
query.finish();
db->unlock();
}
// Get a favorite's lid by the data target
qint32 FavoritesTable::getLidByTarget(const QVariant &target) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("select lid from datastore where key=:key and data=:target");
query.bindValue(":key", FAVORITES_TARGET);
query.bindValue(":target", target);
qint32 result = 0;
query.exec();
if (query.next())
result = query.value(0).toInt();
query.finish();
db->unlock();
return result;
}
// Add a new record
qint32 FavoritesTable::add(const FavoritesRecord &record) {
NSqlQuery query(db);
db->lockForWrite();
query.prepare("Insert into datastore (lid, key, data) values (:lid, :key, :data)");
qint32 lid = record.lid;
ConfigStore cs(db);
if (lid <= 0)
lid = cs.incrementLidCounter();
else
expunge(lid);
qint32 tempLid = getLidByTarget(record.target);
if (tempLid>0)
expunge(tempLid);
query.bindValue(":lid", lid);
query.bindValue(":key", FAVORITES_TYPE);
query.bindValue(":data", record.type);
query.exec();
query.bindValue(":lid", lid);
query.bindValue(":key", FAVORITES_TARGET);
query.bindValue(":data", record.target);
query.exec();
query.bindValue(":lid", lid);
query.bindValue(":key", FAVORITES_ORDER);
query.bindValue(":data", record.order);
query.exec();
query.bindValue(":lid", lid);
query.bindValue(":key", FAVORITES_PARENT);
query.bindValue(":data", record.parent);
query.exec();
query.finish();
db->unlock();
return lid;
}
// Insert a new favorite
qint32 FavoritesTable::insert(const FavoritesRecord &record) {
NSqlQuery query(db);
NSqlQuery subQuery(db);
db->lockForWrite();
query.prepare("Select lid, data from datastore where key=:key and lid in (select lid from datastore where key=:parentkey and data=0) order by data");
query.bindValue(":key", FAVORITES_ORDER);
query.bindValue(":parentkey", FAVORITES_PARENT);
query.exec();
int newOrder = 0;
while (query.next()) {
qint32 lid = query.value(0).toInt();
int order = query.value(1).toInt();
if(order == record.order)
newOrder++;
subQuery.prepare("Update datastore set data=:neworder where lid=:lid and key=:key");
subQuery.bindValue(":neworder", newOrder);
subQuery.bindValue(":lid", lid);
subQuery.bindValue(":key", FAVORITES_ORDER);
subQuery.exec();
newOrder++;
}
query.finish();
db->unlock();
return add(record);
}
// Does this favorite have any children?
bool FavoritesTable::childrenFound(qint32 lid) {
NSqlQuery query(db);
db->lockForRead();
query.prepare("select lid from datastore where key=:key and data=:lid limit 1");
query.bindValue(":key", FAVORITES_PARENT);
query.bindValue(":lid", lid);
query.exec();
if (query.next()) {
query.finish();
db->unlock();
return true;
}
query.finish();
db->unlock();
return false;
}