|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU Lesser General Public License as published by |
|
7 * the Free Software Foundation, version 2.1 of the License. |
|
8 * |
|
9 * This program is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 * GNU Lesser General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Lesser General Public License |
|
15 * along with this program. If not, |
|
16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/". |
|
17 * |
|
18 * Description: This implements the bookmarks API's. |
|
19 * |
|
20 */ |
|
21 |
|
22 #include<QString> |
|
23 #include<QFile> |
|
24 #include<QFileInfo> |
|
25 #include<QDebug> |
|
26 #include<QSqlDatabase> |
|
27 #include<QSqlQuery> |
|
28 #include<QSqlError> |
|
29 #include<QWidget> |
|
30 #include<QtGui> |
|
31 |
|
32 #include "bookmarksapi.h" |
|
33 #include "BookmarksManager.h" |
|
34 #include "BookmarkFav.h" |
|
35 #include "BookmarkResults.h" |
|
36 #include "TagResults.h" |
|
37 #include "xbelreader.h" |
|
38 #include "xbelwriter.h" |
|
39 #include "defaultBookmarks.xml.h" |
|
40 |
|
41 BookmarksManager::BookmarksManager(QWidget *parent) : |
|
42 QObject(parent) |
|
43 { |
|
44 setObjectName("bookmarksManager"); |
|
45 |
|
46 m_db = QSqlDatabase::database(BOOKMARKS_DB_NAME); |
|
47 if (!m_db.isValid()) { |
|
48 m_db = QSqlDatabase::addDatabase("QSQLITE", BOOKMARKS_DB_NAME); |
|
49 m_db.setDatabaseName(BOOKMARKS_DB_FILE); |
|
50 } |
|
51 if (m_db.open()) { |
|
52 // TODO: Do we need a flag so that this gets called only once OR when |
|
53 // creating a database tables "IF NOT EXISTS" is good enough |
|
54 createBookmarksSchema(); |
|
55 } |
|
56 } |
|
57 |
|
58 BookmarksManager::~BookmarksManager() { |
|
59 m_db.close(); |
|
60 QSqlDatabase::removeDatabase(BOOKMARKS_DB_NAME); |
|
61 } |
|
62 |
|
63 /* |
|
64 * +-------------+ +----------+ |
|
65 * |Bookmarks(ID)|* <-> *|Tags(BMID)| |
|
66 * +-------------+ +----------+ |
|
67 */ |
|
68 void BookmarksManager::createBookmarksSchema() { |
|
69 // Bookmarks |
|
70 if (!doQuery("CREATE TABLE IF NOT EXISTS bookmarks(" |
|
71 "id INTEGER PRIMARY KEY," |
|
72 "title text, " |
|
73 "url text," |
|
74 "sortIndex int DEFAULT 0)")) { |
|
75 // TODO: do some error handling here! |
|
76 return; |
|
77 } |
|
78 // Make sorting faster |
|
79 if (!doQuery("CREATE INDEX IF NOT EXISTS bm_sort_idx ON bookmarks(sortIndex ASC)")) { |
|
80 // TODO: do some error handling here! |
|
81 return; |
|
82 } |
|
83 // We do a lot of lookups by id |
|
84 if (!doQuery("CREATE INDEX IF NOT EXISTS bm_id_idx ON bookmarks(id ASC)")) { |
|
85 // TODO: do some error handling here! |
|
86 return; |
|
87 } |
|
88 |
|
89 // Tags |
|
90 // Note: foreign key constraints are not enforced in the current version of sqlite |
|
91 // that we are using. |
|
92 if (!doQuery("CREATE TABLE IF NOT EXISTS tags(" |
|
93 "bmid INTEGER," |
|
94 "tag text," |
|
95 "FOREIGN KEY(bmid) REFERENCES bookmarks(id))")) { |
|
96 // TODO: do some error handling here! |
|
97 return; |
|
98 } |
|
99 // We do a lot of lookups, both by bookmark id and by tag |
|
100 if (!doQuery("CREATE INDEX IF NOT EXISTS tags_bmid_idx ON tags(bmid ASC)")) { |
|
101 // TODO: do some error handling here! |
|
102 return; |
|
103 } |
|
104 if (!doQuery("CREATE INDEX IF NOT EXISTS tags_tag_idx ON tags(tag ASC)")) { |
|
105 // TODO: do some error handling here! |
|
106 return; |
|
107 } |
|
108 } |
|
109 |
|
110 // TODO refactor this - nothing except the schema creation can use it as is |
|
111 bool BookmarksManager::doQuery(QString query) { |
|
112 #ifdef ENABLE_PERF_TRACE |
|
113 PERF_DEBUG() << __PRETTY_FUNCTION__ << query << "\n"; |
|
114 unsigned int st = WrtPerfTracer::tracer()->startTimer(); |
|
115 #endif |
|
116 QSqlQuery db_query(m_db); |
|
117 bool ok = db_query.exec(query); |
|
118 if (!ok) { |
|
119 qDebug() << "BookmarksManager::doQuery" << QString("ERR: %1 %2").arg(db_query.lastError().type()).arg(db_query.lastError().text()) << " Query: " << db_query.lastQuery(); |
|
120 } |
|
121 #ifdef ENABLE_PERF_TRACE |
|
122 PERF_DEBUG() << __PRETTY_FUNCTION__ << WrtPerfTracer::tracer()->elapsedTime(st) << "\n"; |
|
123 #endif |
|
124 return ok; |
|
125 } |
|
126 |
|
127 |
|
128 /**============================================================== |
|
129 * Description: Normalize a given url, if needed. |
|
130 * It adds http:// in front, if the url is relative. |
|
131 ================================================================*/ |
|
132 QString BookmarksManager::normalizeUrl(const QString& url) |
|
133 { |
|
134 // If the URL is relative, add http in front |
|
135 QString updatedUrl = url; |
|
136 |
|
137 if (!url.contains("://")) { |
|
138 updatedUrl.prepend("http://"); |
|
139 } |
|
140 return updatedUrl; |
|
141 } |
|
142 |
|
143 /**============================================================== |
|
144 * Description: Adds the bookmark to the database, given title and |
|
145 * url. |
|
146 * Returns: Returns bookmarkID ( >0)else Failure (-1 or -2) |
|
147 ================================================================*/ |
|
148 int BookmarksManager::addBookmark(QString title, QString URL) |
|
149 { |
|
150 int bookmarkId = 0; |
|
151 |
|
152 if(URL.isEmpty()) { |
|
153 bookmarkId = FAILURE; |
|
154 } |
|
155 |
|
156 if(bookmarkId != FAILURE) { |
|
157 // do some checking on parameters |
|
158 QString updatedTitle = title; |
|
159 QString updatedUrl = normalizeUrl(URL); |
|
160 int soIndex = 1; |
|
161 if (title.isEmpty()) { |
|
162 updatedTitle = "Untitled"; |
|
163 } |
|
164 if (m_db.isOpen()) { |
|
165 QSqlQuery query(m_db); |
|
166 m_db.transaction(); |
|
167 if (!query.exec("SELECT count(*) from bookmarks")) { |
|
168 lastErrMsg(query); |
|
169 m_db.rollback(); |
|
170 return DATABASEERROR; |
|
171 } |
|
172 if(query.next()) { |
|
173 query.prepare("UPDATE bookmarks SET sortIndex=sortIndex+1 WHERE sortIndex >= :sIndex"); |
|
174 query.bindValue(":sIndex", soIndex); |
|
175 if (!query.exec()) { |
|
176 lastErrMsg(query); |
|
177 m_db.rollback(); |
|
178 return DATABASEERROR; |
|
179 } |
|
180 } |
|
181 query.prepare("INSERT INTO bookmarks (title, url, sortIndex) " |
|
182 "VALUES (:title, :url, :sIndex)"); |
|
183 query.bindValue(":title", QVariant(updatedTitle)); |
|
184 query.bindValue(":url", QVariant(updatedUrl)); |
|
185 query.bindValue(":sIndex", QVariant(soIndex)); |
|
186 if (!query.exec()) { |
|
187 lastErrMsg(query); |
|
188 m_db.rollback(); |
|
189 return DATABASEERROR; |
|
190 } |
|
191 // Note: lastInsertId() is not thread-safe |
|
192 bookmarkId = query.lastInsertId().toInt(); |
|
193 if (!m_db.commit()) { |
|
194 qDebug() << m_db.lastError().text(); |
|
195 m_db.rollback(); |
|
196 return DATABASEERROR; |
|
197 } |
|
198 } else { |
|
199 bookmarkId = FAILURE; |
|
200 } |
|
201 } |
|
202 return bookmarkId; |
|
203 } |
|
204 |
|
205 /**============================================================== |
|
206 * Import bookmarks from an XBEL file. If no filename is |
|
207 * passed in a set of default bookmarks will be imported. |
|
208 * The default bookmarks used can be changed by updating |
|
209 * the file "defaultBookmarks.xml.cpp". |
|
210 * @param xbelFilePath - String containing the path to the |
|
211 * file to import. |
|
212 * @return SUCCESS or FAILURE depending on whether the |
|
213 * import process was successful. |
|
214 ==============================================================*/ |
|
215 int BookmarksManager::importBookmarks(QString xbelFilePath) |
|
216 { |
|
217 XbelReader *reader = new XbelReader(this); |
|
218 bool retVal = false; |
|
219 |
|
220 if(xbelFilePath.isEmpty() || !QFile::exists(xbelFilePath)) { |
|
221 xbelFilePath = "c:\\data\\temp.xml"; |
|
222 QFile file(xbelFilePath); |
|
223 if(file.exists()) |
|
224 file.remove(); |
|
225 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) |
|
226 return false; |
|
227 QTextStream out(&file); |
|
228 out << defaultBookmarksList; |
|
229 out.flush(); |
|
230 file.close(); |
|
231 } |
|
232 QFile file(xbelFilePath); |
|
233 if(file.exists()) { |
|
234 file.open(QIODevice::ReadOnly | QIODevice::Text); |
|
235 retVal = reader->read(&file); |
|
236 file.close(); |
|
237 } |
|
238 if(reader) |
|
239 delete reader; |
|
240 return retVal ? SUCCESS : FAILURE; |
|
241 } |
|
242 |
|
243 /**============================================================== |
|
244 * Exports all bookmarks in the database to an XBEL file. This function |
|
245 * expects a valid file path to be passed in. If the path is not valid |
|
246 * a default file path will be used. If a file with that name already |
|
247 * exists at that location, |
|
248 * @param xbelFilePath String containing the path of the file to |
|
249 * export to. |
|
250 * @return SUCCESS or FAILURE depending on whether the |
|
251 * export process was successful. |
|
252 ==============================================================*/ |
|
253 int BookmarksManager::exportBookmarks(QString xbelFilePath) |
|
254 { |
|
255 XbelWriter *writer = new XbelWriter(this); |
|
256 bool retVal = false; |
|
257 |
|
258 if(xbelFilePath.isEmpty()) { |
|
259 xbelFilePath = "c:\\data\\myBookmarks.xml"; |
|
260 } |
|
261 |
|
262 QFile file(xbelFilePath); |
|
263 if(file.exists()) { |
|
264 file.remove(xbelFilePath); |
|
265 } |
|
266 file.open(QIODevice::WriteOnly | QIODevice::Text); |
|
267 retVal = writer->writeFile(&file); |
|
268 file.flush(); |
|
269 file.close(); |
|
270 |
|
271 if(writer) |
|
272 delete writer; |
|
273 return retVal ? SUCCESS : FAILURE; |
|
274 |
|
275 } |
|
276 |
|
277 /**============================================================== |
|
278 * Description: Modify the existing bookmark. Given id of the bookmark |
|
279 * and new title or url, it modifies the existing bookmark. |
|
280 * Returns : Success(0) or Failure(-1 or -2). |
|
281 ================================================================*/ |
|
282 int BookmarksManager::modifyBookmark(int origBookmarkId, QString newTitle, QString newURL) |
|
283 { |
|
284 int retVal = SUCCESS; |
|
285 |
|
286 // Client has to at least pass title or url |
|
287 if(newTitle.isEmpty() && newURL.isEmpty()) |
|
288 retVal = FAILURE; |
|
289 |
|
290 if(retVal == SUCCESS) { |
|
291 |
|
292 if (m_db.isOpen()) { |
|
293 QSqlQuery query(m_db); |
|
294 |
|
295 if(newTitle.isEmpty()) { |
|
296 query.prepare("UPDATE bookmarks SET url=:newurl WHERE id=:bmId"); |
|
297 query.bindValue(":newurl", normalizeUrl(newURL)); |
|
298 query.bindValue(":bmId", origBookmarkId); |
|
299 } else if(newURL.isEmpty()) { |
|
300 query.prepare("UPDATE bookmarks SET title=:newTitle WHERE id=:bmId"); |
|
301 query.bindValue(":newTitle", newTitle); |
|
302 query.bindValue(":bmId", origBookmarkId); |
|
303 } else { |
|
304 query.prepare("UPDATE bookmarks SET url=:newurl, title=:newTitle WHERE id=:bmId"); |
|
305 query.bindValue(":newurl", normalizeUrl(newURL)); |
|
306 query.bindValue(":newTitle", newTitle); |
|
307 query.bindValue(":bmId", origBookmarkId); |
|
308 } |
|
309 if (!query.exec()) { |
|
310 lastErrMsg(query); |
|
311 return DATABASEERROR; |
|
312 } |
|
313 if (query.numRowsAffected() == 0) { |
|
314 // No update happened - must be an invalid id |
|
315 // TODO: shall we return some other status |
|
316 retVal = FAILURE; |
|
317 } |
|
318 } else |
|
319 retVal = FAILURE; |
|
320 } |
|
321 return retVal; |
|
322 } |
|
323 |
|
324 /**=================================================================================== |
|
325 * Description: Delete the bookmark item, also delete all the tags associated with it. |
|
326 * Returns: SUCCESS(0) or Failure(-1 or -2) |
|
327 ======================================================================================*/ |
|
328 int BookmarksManager::deleteBookmark(int bookmarkId) |
|
329 { |
|
330 int retVal = SUCCESS; |
|
331 |
|
332 if (m_db.isOpen()) { |
|
333 |
|
334 QSqlQuery query(m_db); |
|
335 |
|
336 // TODO: Need to think about whether we need to get sortIndex and update all the |
|
337 // rows after the deletion or not |
|
338 |
|
339 // TODO: check if transaction() has been supported |
|
340 // by calling hasfeature() function first |
|
341 m_db.transaction(); |
|
342 |
|
343 query.prepare("DELETE FROM bookmarks WHERE id=:bmId"); |
|
344 query.bindValue(":bmId", bookmarkId); |
|
345 if (!query.exec()) { |
|
346 lastErrMsg(query); |
|
347 m_db.rollback(); |
|
348 return DATABASEERROR; |
|
349 } |
|
350 |
|
351 query.prepare("DELETE FROM tags WHERE bmid=:bmId"); |
|
352 query.bindValue(":bmId", bookmarkId); |
|
353 if (!query.exec()) { |
|
354 lastErrMsg(query); |
|
355 m_db.rollback(); |
|
356 return DATABASEERROR; |
|
357 } |
|
358 if (!m_db.commit()) { |
|
359 qDebug() << m_db.lastError().text(); |
|
360 m_db.rollback(); |
|
361 return DATABASEERROR; |
|
362 } |
|
363 } else |
|
364 retVal = FAILURE; |
|
365 |
|
366 return retVal; |
|
367 } |
|
368 |
|
369 /**=================================================================================== |
|
370 * Description: Delete all records from the bookmarks table as well as all the tags. |
|
371 * Returns: SUCCESS(0) or FAILURE(-1 or -2) |
|
372 ======================================================================================*/ |
|
373 int BookmarksManager::clearAll() |
|
374 { |
|
375 int retVal = SUCCESS; |
|
376 |
|
377 if (m_db.isOpen()) { |
|
378 QSqlQuery query(m_db); |
|
379 |
|
380 // TODO: check if transaction() has been supported |
|
381 // by calling hasfeature() function first |
|
382 m_db.transaction(); |
|
383 |
|
384 if(!query.exec("DELETE FROM bookmarks")) { |
|
385 lastErrMsg(query); |
|
386 m_db.rollback(); |
|
387 retVal = DATABASEERROR; |
|
388 } |
|
389 if (retVal == SUCCESS && !query.exec("DELETE FROM tags")) { |
|
390 lastErrMsg(query); |
|
391 m_db.rollback(); |
|
392 retVal = DATABASEERROR; |
|
393 } |
|
394 if (retVal == SUCCESS && !m_db.commit()) { |
|
395 qDebug() << m_db.lastError().text(); |
|
396 m_db.rollback(); |
|
397 retVal = DATABASEERROR; |
|
398 } |
|
399 } else |
|
400 retVal = FAILURE; |
|
401 |
|
402 return retVal; |
|
403 } |
|
404 |
|
405 /**============================================================== |
|
406 * Description: Deletes a single tag associated with the bookmark |
|
407 * Returns: SUCCESS(0) or FAILURE (-1 or -2) |
|
408 ===============================================================*/ |
|
409 int BookmarksManager::deleteTag(int bookmarkId, QString tagToDelete) |
|
410 { |
|
411 int retVal = SUCCESS; |
|
412 |
|
413 if(tagToDelete.isEmpty()|| bookmarkId < 0) |
|
414 retVal = FAILURE; |
|
415 |
|
416 if (retVal == SUCCESS) { |
|
417 if (m_db.isOpen()) { |
|
418 QSqlQuery query(m_db); |
|
419 |
|
420 query.prepare("DELETE FROM tags WHERE bmid=:bmId AND tag=:tag"); |
|
421 query.bindValue(":bmId", bookmarkId); |
|
422 query.bindValue(":tag", tagToDelete); |
|
423 if (!query.exec()) { |
|
424 lastErrMsg(query); |
|
425 retVal = DATABASEERROR; |
|
426 } |
|
427 } else |
|
428 retVal = FAILURE; |
|
429 } |
|
430 |
|
431 return retVal; |
|
432 } |
|
433 |
|
434 /**================================================================ |
|
435 * Description: Adds a single tag associated given the bookmark id |
|
436 * Returns: SUCCESS(0) or FAILURE (-1 or -2) |
|
437 ==================================================================*/ |
|
438 int BookmarksManager::addTag(int bookmarkId, QString tagToAdd) |
|
439 { |
|
440 int retVal = SUCCESS; |
|
441 |
|
442 if(tagToAdd.isEmpty()|| bookmarkId < 0) |
|
443 retVal = FAILURE; |
|
444 |
|
445 if(retVal == SUCCESS) { |
|
446 if (m_db.isOpen()) { |
|
447 QSqlQuery query(m_db); |
|
448 |
|
449 query.prepare("INSERT INTO tags (bmid, tag) " |
|
450 "VALUES (:id, :tag)"); |
|
451 query.bindValue(":id", QVariant(bookmarkId)); |
|
452 query.bindValue(":tag", QVariant(tagToAdd)); |
|
453 |
|
454 if (!query.exec()) { |
|
455 lastErrMsg(query); |
|
456 retVal = DATABASEERROR; |
|
457 } |
|
458 } else |
|
459 retVal = FAILURE; |
|
460 } |
|
461 |
|
462 return retVal; |
|
463 } |
|
464 |
|
465 /**============================================================== |
|
466 * Description: Finds all the bookmarks weather they have tags |
|
467 * or not. |
|
468 * Returns: A pointer to BookmarkResults object or NULL. |
|
469 ===============================================================*/ |
|
470 BookmarkResults *BookmarksManager::findAllBookmarks() |
|
471 { |
|
472 BookmarkResults * results = NULL; |
|
473 |
|
474 QString queryStr = QString("SELECT " |
|
475 " id, title, url, sortIndex " |
|
476 " FROM bookmarks ORDER BY sortIndex"); |
|
477 if (m_db.isOpen()) { |
|
478 QSqlQuery *query = new QSqlQuery(m_db); |
|
479 if (query->exec(queryStr)) { |
|
480 results = new BookmarkResults(query); |
|
481 } else { |
|
482 qDebug() << query->lastError().text() << " Query: " << query->lastQuery(); |
|
483 results = NULL; |
|
484 } |
|
485 } |
|
486 return results; |
|
487 } |
|
488 |
|
489 /**============================================================== |
|
490 * Description: Finds all the distinct tags. |
|
491 * Returns: A pointer to TagResults object or NULL. |
|
492 ===============================================================*/ |
|
493 TagResults *BookmarksManager::findAllTags() |
|
494 { |
|
495 TagResults * results = NULL; |
|
496 |
|
497 if (m_db.isOpen()) { |
|
498 QSqlQuery *query = new QSqlQuery(m_db); |
|
499 if (query->exec("SELECT DISTINCT tag FROM tags")) { |
|
500 // TODO: do we need javascript hack here like in findAllBookmarks API. |
|
501 results = new TagResults(query); |
|
502 } else { |
|
503 qDebug() << query->lastError().text() << " Query: " << query->lastQuery(); |
|
504 results = NULL; |
|
505 } |
|
506 } |
|
507 return results; |
|
508 } |
|
509 |
|
510 |
|
511 /**============================================================== |
|
512 * Description: Finds all the bookmarks associated with a given |
|
513 * tag. |
|
514 * Returns: A pointer to BookmarkResults object or NULL. |
|
515 ===============================================================*/ |
|
516 BookmarkResults *BookmarksManager::findBookmarksByTag(QString tag) |
|
517 { |
|
518 BookmarkResults * results = NULL; |
|
519 |
|
520 QString queryStr = QString("SELECT " |
|
521 " id, title, url, sortIndex " |
|
522 " FROM bookmarks b JOIN" |
|
523 " tags t ON b.id=t.bmid WHERE" |
|
524 " t.tag=:tag"); |
|
525 if (m_db.isOpen()) { |
|
526 QSqlQuery *query = new QSqlQuery(m_db); |
|
527 query->prepare(queryStr); |
|
528 query->bindValue(":tag", tag); |
|
529 if (query->exec()) { |
|
530 // TODO: do we need javascript hack here like in findAllBookmarks API. |
|
531 results = new BookmarkResults(query); |
|
532 } else { |
|
533 qDebug() << query->lastError().text() << " Query: " << query->lastQuery(); |
|
534 results = NULL; |
|
535 } |
|
536 } |
|
537 return results; |
|
538 } |
|
539 |
|
540 |
|
541 /**============================================================== |
|
542 * Description: Finds all the Tags associated with a given |
|
543 * bookmarkID. |
|
544 * Returns: A pointer to TagResults object or NULL. |
|
545 ===============================================================*/ |
|
546 TagResults *BookmarksManager::findTagsByBookmark(int bookmarkID) |
|
547 { |
|
548 TagResults * results = NULL; |
|
549 |
|
550 QString queryStr = QString("SELECT DISTINCT tag " |
|
551 " FROM tags t JOIN" |
|
552 " bookmarks b ON t.bmid=b.id WHERE" |
|
553 " t.bmid=:id"); |
|
554 if (m_db.isOpen()) { |
|
555 QSqlQuery *query = new QSqlQuery(m_db); |
|
556 query->prepare(queryStr); |
|
557 query->bindValue(":id", bookmarkID); |
|
558 if (query->exec()) { |
|
559 // TODO: do we need javascript hack here like in findAllBookmarks API. |
|
560 results = new TagResults(query); |
|
561 } else { |
|
562 qDebug() << query->lastError().text() << " Query: " << query->lastQuery(); |
|
563 results = NULL; |
|
564 } |
|
565 } |
|
566 return results; |
|
567 } |
|
568 |
|
569 /**============================================================== |
|
570 * Description: Finds all the Bookmarks that doesn't have any |
|
571 * tags associated with it. It is needed by export API. |
|
572 * Returns: A pointer to BookmarkResults object or NULL. |
|
573 ===============================================================*/ |
|
574 BookmarkResults *BookmarksManager::findUntaggedBookmarks() |
|
575 { |
|
576 BookmarkResults * results = NULL; |
|
577 |
|
578 QString queryStr = QString("SELECT " |
|
579 " id, title, url, sortIndex " |
|
580 " FROM bookmarks b LEFT OUTER JOIN" |
|
581 " tags t ON b.id=t.bmid WHERE" |
|
582 " t.bmid IS NULL ORDER BY sortIndex"); |
|
583 |
|
584 if (m_db.isOpen()) { |
|
585 QSqlQuery *query = new QSqlQuery(m_db); |
|
586 if (query->exec(queryStr)) { |
|
587 // TODO: do we need javascript hack here like in findAllBookmarks API. |
|
588 results = new BookmarkResults(query); |
|
589 } else { |
|
590 qDebug() << query->lastError().text() << " Query: " << query->lastQuery(); |
|
591 results = NULL; |
|
592 } |
|
593 } |
|
594 return results; |
|
595 } |
|
596 |
|
597 |
|
598 /**============================================================== |
|
599 * Description: Reorder bookmarks. Moves a given bookmark to a |
|
600 * passed new index. |
|
601 * Returns: SUCCESS(0) or FAILURE (-1 or -2) |
|
602 ===============================================================*/ |
|
603 int BookmarksManager::reorderBookmark(int bookmarkID, int newIndex) |
|
604 { |
|
605 if (newIndex <= 0) |
|
606 return FAILURE; |
|
607 |
|
608 if (!m_db.isOpen()) |
|
609 return DATABASEERROR; |
|
610 |
|
611 QSqlQuery query(m_db); |
|
612 |
|
613 // Make sure the bookmark exists |
|
614 BookmarkFav *bm = findBookmark(bookmarkID); |
|
615 if (!bm) |
|
616 return FAILURE; |
|
617 |
|
618 // Next, help stamp out and abolish redundancy. If the bookmark is already at this sortIndex, do nothing. |
|
619 if (bm->sortIndex() == newIndex) |
|
620 return SUCCESS; |
|
621 |
|
622 /* |
|
623 * Ok, the way the sortIndex works is that you can specify any sortIndex you want (it doesn't have to be consecutive). |
|
624 * This means there can be holes in the list of sortIndexes. Whenever someone wants to move a bookmark to a new sort |
|
625 * position, we just bump everything that was in that position and further down the list by 1 and then set the sortIndex |
|
626 * of the bookmark to what they want. This should work whether there is more than one bookmark or not. |
|
627 */ |
|
628 |
|
629 // Bump all the bookmarks from this sortIndex down by 1 first |
|
630 m_db.transaction(); |
|
631 query.prepare("UPDATE bookmarks SET sortIndex=sortIndex+1 WHERE sortIndex >= :newIndex"); |
|
632 query.bindValue(":newIndex", newIndex); |
|
633 if (!query.exec()) { |
|
634 lastErrMsg(query); |
|
635 return DATABASEERROR; |
|
636 } |
|
637 // Then just set this bookmark's sortIndex to the new value |
|
638 query.prepare("UPDATE bookmarks SET sortIndex=:newIndex WHERE id=:id"); |
|
639 query.bindValue(":id", bookmarkID); |
|
640 query.bindValue(":newIndex", newIndex); |
|
641 if (!query.exec()) { |
|
642 lastErrMsg(query); |
|
643 m_db.rollback(); |
|
644 return DATABASEERROR; |
|
645 } |
|
646 if (!m_db.commit()){ |
|
647 qDebug() << m_db.lastError().text(); |
|
648 m_db.rollback(); |
|
649 return DATABASEERROR; |
|
650 } |
|
651 return SUCCESS; |
|
652 } |
|
653 |
|
654 /**============================================================== |
|
655 * Description: Finds a bookmark based on a given bookmarkID. |
|
656 * Returns: A pointer to BookmarkFav object or NULL. |
|
657 ===============================================================*/ |
|
658 BookmarkFav* BookmarksManager::findBookmark(int bookmarkId) |
|
659 { |
|
660 BookmarkFav * results = NULL; |
|
661 |
|
662 |
|
663 if (m_db.isOpen()) { |
|
664 QSqlQuery query(m_db); |
|
665 query.prepare("SELECT title, url, sortIndex FROM bookmarks WHERE id=:id"); |
|
666 query.bindValue(":id", bookmarkId); |
|
667 if (query.exec()) { |
|
668 if (query.next()) |
|
669 results = new BookmarkFav(bookmarkId, query.value(0).toString(), |
|
670 query.value(1).toString(), query.value(2).toInt()); |
|
671 } else { |
|
672 lastErrMsg(query); |
|
673 } |
|
674 } |
|
675 return results; |
|
676 } |
|
677 |
|
678 /**============================================================== |
|
679 * Description: Finds a bookmark based on a given bookmarkID. |
|
680 * Returns: A pointer to BookmarkFav object or NULL. |
|
681 ===============================================================*/ |
|
682 QMap<QString, QString> BookmarksManager::findBookmarks(QString atitle) |
|
683 { |
|
684 QMap<QString, QString> map; |
|
685 |
|
686 if (m_db.isOpen()) { |
|
687 QSqlQuery query(m_db); |
|
688 QString queryStatement = "SELECT url, title FROM bookmarks WHERE title LIKE '%"+atitle+"%' OR url LIKE '%" + atitle + "%'"; |
|
689 query.prepare(queryStatement); |
|
690 if(query.exec()) { |
|
691 while (query.next()){ |
|
692 QString bookmarkUrl = query.value(0).toString(); |
|
693 QString bookmarkTitle = query.value(1).toString(); |
|
694 map.insert( bookmarkUrl, bookmarkTitle ); |
|
695 } |
|
696 } else { |
|
697 lastErrMsg(query); |
|
698 } |
|
699 } |
|
700 return map; |
|
701 } |
|
702 |
|
703 /**============================================================== |
|
704 * Description: Prints a last error message from the query. |
|
705 * Returns: Nothing. |
|
706 ===============================================================*/ |
|
707 void BookmarksManager::lastErrMsg(QSqlQuery& query) |
|
708 { |
|
709 qDebug() << "BookmarksManager::lastErrMsg" << QString("ERR: %1 %2").arg(query.lastError().type()).arg(query.lastError().text()) << " Query: " << query.lastQuery(); |
|
710 } |
|
711 |