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