1 /* |
1 /* |
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
3 * All rights reserved. |
3 * All rights reserved. |
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
4 * |
9 * Initial Contributors: |
5 * This program is free software: you can redistribute it and/or modify |
10 * Nokia Corporation - initial contribution. |
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. |
11 * |
13 * |
12 * Contributors: |
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/". |
13 * |
17 * |
14 * Description: |
18 * Description: |
15 * |
19 * |
16 */ |
20 */ |
17 |
|
18 |
21 |
19 #include<QString> |
22 #include<QString> |
20 #include<browsercontentdll.h> |
23 #include<browsercontentdll.h> |
21 #include<QSqlDatabase> |
24 #include<QSqlDatabase> |
22 #include<QSqlQuery> |
25 #include<QSqlQuery> |
23 #include<QSqlError> |
26 #include<QSqlError> |
24 #include<QDebug> |
27 #include<QDebug> |
25 const QString dbLocation="browserContent.db"; |
28 const QString dbLocation="browserContent.db"; |
26 |
29 |
27 class BrowserContentPrivate |
30 class BrowserContentPrivate { |
28 { |
|
29 BOOKMARKSCLIENT_PUBLIC(BrowserContent) |
31 BOOKMARKSCLIENT_PUBLIC(BrowserContent) |
30 public: |
32 public: |
31 ~BrowserContentPrivate(); |
33 ~BrowserContentPrivate(); |
32 QString m_connectionName; |
34 QString m_connectionName; |
33 }; |
35 }; |
34 |
36 |
35 /**============================================================== |
37 /**============================================================== |
36 * Description: Constructor of BrowserContentPrivate |
38 * Description: Constructor of BrowserContentPrivate |
37 ================================================================*/ |
39 ================================================================*/ |
38 BrowserContentPrivate::~BrowserContentPrivate() |
40 BrowserContentPrivate::~BrowserContentPrivate() |
39 { |
41 { |
40 |
42 |
41 } |
43 } |
42 |
44 |
43 /**============================================================== |
45 /**============================================================== |
44 * Description: Constructor of BrowserContent |
46 * Description: Constructor of BrowserContent |
45 ================================================================*/ |
47 ================================================================*/ |
46 BrowserContent::BrowserContent(QString aClientName) |
48 BrowserContent::BrowserContent(QString aClientName) |
47 { |
49 { |
48 BOOKMARKSCLIENT_INITIALIZE(BrowserContent); |
50 BOOKMARKSCLIENT_INITIALIZE(BrowserContent); |
49 |
51 |
50 priv->m_connectionName=aClientName; |
52 priv->m_connectionName=aClientName; |
51 createDatabase(); |
53 createDatabase(); |
52 } |
54 } |
53 |
55 |
54 /**============================================================== |
56 /**============================================================== |
55 * Description: creates the database |
57 * Description: creates the database |
56 ================================================================*/ |
58 ================================================================*/ |
57 int BrowserContent::createDatabase() |
59 int BrowserContent::createDatabase() |
58 { |
60 { |
59 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
61 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
60 |
62 |
61 QSqlDatabase sqlDB = QSqlDatabase::addDatabase("QSQLITE", priv->m_connectionName); |
63 QSqlDatabase sqlDB = QSqlDatabase::addDatabase("QSQLITE", priv->m_connectionName); |
62 sqlDB.setHostName("Simulator"); |
64 sqlDB.setHostName("Simulator"); |
63 sqlDB.setDatabaseName(dbLocation); |
65 sqlDB.setDatabaseName(dbLocation); |
64 |
66 |
65 |
67 |
66 if (!sqlDB.open()) |
68 if (!sqlDB.open()) |
67 return -1; |
69 return -1; |
68 QSqlError error; |
70 QSqlError error; |
69 int err = ErrGeneral; |
71 int err = ErrGeneral; |
70 |
72 |
71 QFileInfo dbFile(dbLocation); |
73 //Check if the table exists, create table only when it's empty |
72 if (dbFile.exists() && dbFile.size() == 0) |
74 QStringList tablelist = sqlDB.tables(QSql::Tables); |
73 { |
75 if (tablelist.count() == 0) { |
74 QSqlQuery query(sqlDB); |
76 QSqlQuery query(sqlDB); |
75 query.exec( |
77 query.exec( |
76 "CREATE TABLE BookMarkTable (title VARCHAR(40) NOT NULL,url VARCHAR(100),adate VARCHAR(40),tags VARCHAR(40),rowindex INTEGER,CONSTRAINT pk_BookMarkTable PRIMARY KEY(title))"); |
78 "CREATE TABLE BookMarkTable (title VARCHAR(40) NOT NULL,url VARCHAR(100),adate VARCHAR(40),tags VARCHAR(40),rowindex INTEGER, domain VARCHAR(100),CONSTRAINT pk_BookMarkTable PRIMARY KEY(title))"); |
77 error=query.lastError(); |
79 error=query.lastError(); |
78 |
80 |
79 if (error.type() == QSqlError::NoError) { |
81 if (error.type() == QSqlError::NoError) { |
80 query.exec("CREATE TABLE HistoryTable (rowindex INTEGER PRIMARY KEY, title VARCHAR(40) NOT NULL,url VARCHAR(100),adate VARCHAR(40),atime VARCHAR(40))"); |
82 query.exec("CREATE TABLE HistoryTable (rowindex INTEGER PRIMARY KEY, title VARCHAR(40) NOT NULL,url VARCHAR(100),adate VARCHAR(40),atime VARCHAR(40), domain VARCHAR(100))"); |
81 error=query.lastError(); |
83 error=query.lastError(); |
82 } |
84 } |
83 } |
85 } |
84 |
86 |
85 if (error.type() == QSqlError::NoError) { |
87 if (error.type() == QSqlError::NoError) { |
86 return ErrNone; |
88 return ErrNone; |
87 } |
89 } else { |
88 else { |
90 return ErrGeneral; |
89 return ErrGeneral; |
91 } |
90 } |
92 } |
91 |
|
92 } |
|
93 |
93 |
94 |
94 |
95 /**============================================================== |
95 /**============================================================== |
96 * Description: Destrcutor of the BookmarksClientSide |
96 * Description: Destrcutor of the BookmarksClientSide |
97 ================================================================*/ |
97 ================================================================*/ |
102 /**============================================================== |
102 /**============================================================== |
103 * Description: adds the bookmark to the database |
103 * Description: adds the bookmark to the database |
104 ================================================================*/ |
104 ================================================================*/ |
105 int BrowserContent::AddBookmark( |
105 int BrowserContent::AddBookmark( |
106 BookmarkLeaf* BookmarkContent) |
106 BookmarkLeaf* BookmarkContent) |
107 { |
107 { |
108 |
|
109 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
108 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
110 QSqlError error; |
109 QSqlError error; |
111 QString title =BookmarkContent->getTitle(); |
110 QString title =BookmarkContent->getTitle(); |
112 QString url = BookmarkContent->getUrl(); |
111 QString url = BookmarkContent->getUrl(); |
113 QString adate =BookmarkContent->getDate().toString("dd.MM.yyyy"); |
112 QString adate =BookmarkContent->getDate().toString("dd.MM.yyyy"); |
114 QString tags = BookmarkContent->getTag(); |
113 QString tags = BookmarkContent->getTag(); |
115 int aIndex=BookmarkContent->getIndex(); |
114 int aIndex=BookmarkContent->getIndex(); |
116 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
115 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
117 |
116 |
118 if (db.isOpen()) |
117 if (db.isOpen()) { |
119 { |
|
120 QSqlQuery query(db); |
118 QSqlQuery query(db); |
121 query.prepare("UPDATE BookMarkTable SET rowindex=rowindex+1 WHERE rowindex>=:aIndex"); |
119 query.prepare("UPDATE BookMarkTable SET rowindex=rowindex+1 WHERE rowindex>=:aIndex"); |
122 query.bindValue(":aIndex", aIndex); |
120 query.bindValue(":aIndex", aIndex); |
123 query.exec(); |
121 query.exec(); |
124 error = query.lastError(); |
122 error = query.lastError(); |
125 |
123 |
126 query.prepare("INSERT INTO BookMarkTable (title, url, adate, tags, rowindex) " |
124 query.prepare("INSERT INTO BookMarkTable (title, url, adate, tags, rowindex, domain) " |
127 "VALUES (:title, :url, :adate, :tags, :aIndex)"); |
125 "VALUES (:title, :url, :adate, :tags, :aIndex, :domain)"); |
128 |
126 |
129 query.bindValue(":title", QVariant(title)); |
127 query.bindValue(":title", QVariant(title)); |
130 query.bindValue(":url", QVariant(url)); |
128 query.bindValue(":url", QVariant(url)); |
131 query.bindValue(":adate", QVariant(adate)); |
129 query.bindValue(":adate", QVariant(adate)); |
132 query.bindValue(":tags", QVariant(tags)); |
130 query.bindValue(":tags", QVariant(tags)); |
133 query.bindValue(":rowindex", QVariant(aIndex)); |
131 query.bindValue(":rowindex",QVariant(aIndex)); |
134 |
132 query.bindValue(":domain", QVariant(filterUrl(url))); |
135 query.exec(); |
133 |
136 error = query.lastError(); |
134 query.exec(); |
137 } |
135 error = query.lastError(); |
138 |
136 } |
139 if (error.type() == QSqlError::NoError) |
137 |
140 { |
138 if (error.type() == QSqlError::NoError) { |
141 return ErrNone; |
139 return ErrNone; |
142 } |
140 } else { |
143 else |
|
144 { |
|
145 return ErrGeneral; |
141 return ErrGeneral; |
146 } |
142 } |
147 } |
143 } |
148 |
|
149 |
144 |
150 /**============================================================== |
145 /**============================================================== |
151 * Description: deletes the requested bookmark |
146 * Description: deletes the requested bookmark |
152 ================================================================*/ |
147 ================================================================*/ |
153 int BrowserContent::DeleteBookmark( |
148 int BrowserContent::DeleteBookmark(QString atitle) |
154 QString atitle) |
149 { |
155 { |
|
156 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
150 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
157 QSqlError error; |
151 QSqlError error; |
158 QString title = atitle; |
152 QString title = atitle; |
159 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
153 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
160 bool ok; |
154 bool ok; |
161 if (db.isOpen()) |
155 if (db.isOpen()) { |
162 { |
156 QSqlQuery query(db); |
163 QSqlQuery query(db); |
157 |
|
158 if(atitle.contains("'", Qt::CaseInsensitive)) |
|
159 atitle.replace(QString("'"), QString("''")); |
|
160 |
164 query.prepare("SELECT title,url,adate,tags,rowindex FROM BookMarkTable WHERE title=:title"); |
161 query.prepare("SELECT title,url,adate,tags,rowindex FROM BookMarkTable WHERE title=:title"); |
165 query.bindValue(":title", title); |
162 query.bindValue(":title", title); |
166 query.exec(); |
163 query.exec(); |
167 error = query.lastError(); |
164 error = query.lastError(); |
168 query.next(); |
165 query.next(); |
227 node->setUrl(url); |
224 node->setUrl(url); |
228 node->setDate(adate); |
225 node->setDate(adate); |
229 node->setTag(tag); |
226 node->setTag(tag); |
230 node->setIndex(aIndex); |
227 node->setIndex(aIndex); |
231 nodeslist.append(node); |
228 nodeslist.append(node); |
232 } |
229 } |
233 } |
230 } |
234 return nodeslist; |
231 return nodeslist; |
235 |
232 } |
236 } |
233 |
|
234 /**============================================================== |
|
235 * Description: fetches suggested bookmarks from database |
|
236 ================================================================*/ |
|
237 QList<BookmarkLeaf*> BrowserContent::suggestBookMarks(QString atitle) |
|
238 { |
|
239 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
|
240 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
241 QList<BookmarkLeaf*> nodeslist; |
|
242 |
|
243 if (db.isOpen()) { |
|
244 QSqlQuery query(db); |
|
245 QString queryStatement = "SELECT title,url FROM BookMarkTable WHERE title LIKE '%" + atitle + "%' OR url LIKE '%" + atitle + "%' LIMIT 3"; |
|
246 query.prepare(queryStatement); |
|
247 query.exec(); |
|
248 QSqlError error = query.lastError(); |
|
249 |
|
250 while (query.next()) { |
|
251 QString title = query.value(0).toString(); |
|
252 QString url = query.value(1).toString(); |
|
253 BookmarkLeaf* node = new BookmarkLeaf(); |
|
254 node->setTitle(title); |
|
255 node->setUrl(url); |
|
256 nodeslist.append(node); |
|
257 } |
|
258 } |
|
259 return nodeslist; |
|
260 } |
237 |
261 |
238 /**============================================================== |
262 /**============================================================== |
239 * Description: Reoders the Bokmarks based on index |
263 * Description: Reoders the Bokmarks based on index |
240 ================================================================*/ |
264 ================================================================*/ |
241 int BrowserContent::reorderBokmarks(QString title,int new_index) |
265 int BrowserContent::reorderBokmarks(QString title,int new_index) |
242 { |
266 { |
243 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
267 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
244 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
268 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
245 QSqlQuery query(db); |
269 QSqlQuery query(db); |
246 QSqlError error; |
270 QSqlError error; |
247 bool ok; |
271 bool ok; |
248 query.prepare("SELECT title,url,adate,tags,rowindex FROM BookMarkTable WHERE title=:title"); |
272 |
249 query.bindValue(":title", title); |
273 if(title.contains("'", Qt::CaseInsensitive)) |
250 query.exec(); |
274 title.replace(QString("'"), QString("''")); |
251 error = query.lastError(); |
275 |
252 query.next(); |
276 query.prepare("SELECT title,url,adate,tags,rowindex FROM BookMarkTable WHERE title=:title"); |
253 int old_index=query.value(4).toInt(&ok); |
277 query.bindValue(":title", title); |
254 |
278 query.exec(); |
255 if(old_index>new_index) |
279 error = query.lastError(); |
256 { |
280 query.next(); |
|
281 int old_index=query.value(4).toInt(&ok); |
|
282 |
|
283 if(old_index>new_index) { |
257 //Moving the item UP |
284 //Moving the item UP |
258 query.prepare("UPDATE BookMarkTable set rowindex=rowindex+1 WHERE rowindex BETWEEN :new_index AND :old_index-1"); |
285 query.prepare("UPDATE BookMarkTable set rowindex=rowindex+1 WHERE rowindex BETWEEN :new_index AND :old_index-1"); |
259 } |
286 } else if(old_index<new_index) { |
260 else if(old_index<new_index) |
287 //Moving items Down |
261 { |
288 query.prepare("UPDATE BookMarkTable set rowindex=rowindex-1 WHERE rowindex BETWEEN :old_index+1 AND :new_index"); |
262 //Moving items Down |
289 } |
263 query.prepare("UPDATE BookMarkTable set rowindex=rowindex-1 WHERE rowindex BETWEEN :old_index+1 AND :new_index"); |
290 |
264 } |
291 query.bindValue(":old_index", old_index); |
265 query.bindValue(":old_index", old_index); |
292 query.bindValue(":new_index", new_index); |
266 query.bindValue(":new_index", new_index); |
293 //Moving items Down |
267 //Moving items Down |
294 query.exec(); |
268 query.exec(); |
295 error= query.lastError(); |
269 error= query.lastError(); |
296 |
270 |
297 if (error.type() == QSqlError::NoError) { |
271 if (error.type() == QSqlError::NoError) |
298 query.prepare("UPDATE BookMarkTable set rowindex=:new_index WHERE title=:title"); |
272 { |
299 query.bindValue(":title", title); |
273 query.prepare("UPDATE BookMarkTable set rowindex=:new_index WHERE title=:title"); |
300 query.bindValue(":new_index", new_index); |
274 query.bindValue(":title", title); |
301 query.exec(); |
275 query.bindValue(":new_index", new_index); |
302 error = query.lastError(); |
276 query.exec(); |
303 } |
277 error = query.lastError(); |
304 |
278 } |
305 if (error.type() == QSqlError::NoError) { |
279 |
306 //No error |
280 if (error.type() == QSqlError::NoError) |
307 return ErrNone; |
281 { |
308 } else { |
282 return ErrNone; |
309 return ErrGeneral; |
283 } |
310 } |
284 else |
311 } |
285 { |
312 |
286 return ErrGeneral; |
313 |
287 } |
314 /**============================================================== |
288 } |
315 * Description: modify the requested bookmark |
289 |
316 ================================================================*/ |
290 |
317 int BrowserContent::modifyBookmark(QString aOrgTitle, QString aNewTitle, QString aNewUrl) |
291 ////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
318 { |
|
319 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
|
320 QSqlError error; |
|
321 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
322 |
|
323 if (db.isOpen()) { |
|
324 QSqlQuery query(db); |
|
325 query.prepare("UPDATE BookMarkTable set title=:aNewTitle, url=:aNewUrl, domain=:aNewDomain WHERE title=:aOrgTitle"); |
|
326 query.bindValue(":aNewTitle", aNewTitle); |
|
327 query.bindValue(":aNewUrl", aNewUrl); |
|
328 query.bindValue(":aNewDomain", filterUrl(aNewUrl)); |
|
329 query.bindValue(":aOrgTitle", aOrgTitle); |
|
330 query.exec(); |
|
331 error = query.lastError(); |
|
332 } |
|
333 |
|
334 if (error.type() == QSqlError::NoError) { |
|
335 return ErrNone; |
|
336 } else { |
|
337 return ErrGeneral; |
|
338 } |
|
339 } |
|
340 |
292 |
341 |
293 /**============================================================== |
342 /**============================================================== |
294 * Description: adds the bookmark to the database |
343 * Description: adds the bookmark to the database |
295 ================================================================*/ |
344 ================================================================*/ |
296 int BrowserContent::AddHistory( |
345 int BrowserContent::AddHistory( |
297 HistoryLeaf* HistoryContent) |
346 HistoryLeaf* HistoryContent) |
298 { |
347 { |
299 |
|
300 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
348 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
301 QSqlError error; |
349 QSqlError error; |
302 QString title =HistoryContent->getTitle(); |
350 QString title =HistoryContent->getTitle(); |
303 QString url = HistoryContent->getUrl(); |
351 QString url = HistoryContent->getUrl(); |
304 QString adate =HistoryContent->getDate().toString("dd.MM.yyyy"); |
352 QString adate =HistoryContent->getDate().toString("dd.MM.yyyy"); |
305 QString atime = HistoryContent->getLastVisited().toString("h:mm ap"); |
353 QString atime = HistoryContent->getLastVisited().toString("h:mm ap"); |
306 // int aIndex=HistoryContent->getIndex(); |
354 |
307 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
355 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
308 |
356 |
309 if (db.isOpen()) |
357 if (db.isOpen()) { |
310 { |
358 QSqlQuery query(db); |
311 QSqlQuery query(db); |
359 query.prepare("INSERT INTO HistoryTable (rowindex,title, url, adate, atime, domain) " |
312 // query.prepare("UPDATE HistoryTable SET rowindex=rowindex+1 WHERE rowindex>=:aIndex"); |
360 "VALUES (NULL,:title, :url, :adate, :atime, :domain)"); |
313 //query.bindValue(":aIndex", aIndex); |
361 |
314 //query.exec(); |
362 query.bindValue(":title", QVariant(title)); |
315 //error = query.lastError(); |
363 query.bindValue(":url", QVariant(url)); |
316 |
364 query.bindValue(":adate", QVariant(adate)); |
317 //"CREATE TABLE HistoryTable (rowindex INTEGER PRIMARY KEY, title VARCHAR(40) NOT NULL,url VARCHAR(100),adate VARCHAR(40),atime VARCHAR(40))"); |
365 query.bindValue(":atime", QVariant(atime)); |
318 |
366 query.bindValue(":domain", QVariant(filterUrl(url))); |
319 query.prepare("INSERT INTO HistoryTable (rowindex,title, url, adate, atime) " |
367 query.exec(); |
320 "VALUES (NULL,:title, :url, :adate, :atime)"); |
368 error = query.lastError(); |
321 |
369 } |
322 //query.bindValue(":rowindex", QVariant(aIndex)); |
370 |
323 query.bindValue(":title", QVariant(title)); |
371 if (error.type() == QSqlError::NoError) { |
324 query.bindValue(":url", QVariant(url)); |
372 return ErrNone; |
325 query.bindValue(":adate", QVariant(adate)); |
373 } else { |
326 query.bindValue(":atime", QVariant(atime)); |
374 return ErrGeneral; |
327 query.exec(); |
375 } |
328 error = query.lastError(); |
376 } |
329 } |
|
330 |
|
331 if (error.type() == QSqlError::NoError) |
|
332 { |
|
333 return ErrNone; |
|
334 } |
|
335 else |
|
336 { |
|
337 return ErrGeneral; |
|
338 } |
|
339 } |
|
340 |
377 |
341 |
378 |
342 /**============================================================== |
379 /**============================================================== |
343 * Description: fetches History From database |
380 * Description: fetches History From database |
344 ================================================================*/ |
381 ================================================================*/ |
345 QList<HistoryLeaf*> BrowserContent::FetchHistory() |
382 QList<HistoryLeaf*> BrowserContent::FetchHistory() |
346 { |
383 { |
347 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
384 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
348 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
385 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
349 QList<HistoryLeaf*> nodeslist; |
386 QList<HistoryLeaf*> nodeslist; |
350 |
387 |
351 bool dbopen = db.isOpen(); |
388 bool dbopen = db.isOpen(); |
352 bool ok; |
389 bool ok; |
353 |
390 |
354 if (dbopen) |
391 if (dbopen) { |
355 { |
|
356 |
|
357 QSqlQuery query(db); |
392 QSqlQuery query(db); |
358 |
393 |
359 query.prepare("SELECT title,url,adate,atime,rowindex FROM HistoryTable ORDER BY rowindex"); |
394 query.prepare("SELECT title,url,adate,atime,rowindex FROM HistoryTable ORDER BY rowindex"); |
360 query.exec(); |
395 query.exec(); |
361 QSqlError error = query.lastError(); |
396 QSqlError error = query.lastError(); |
362 |
397 |
363 while (query.next()) |
398 while (query.next()) { |
364 { |
|
365 |
|
366 QString title = query.value(0).toString(); |
399 QString title = query.value(0).toString(); |
|
400 if(title.contains("\"", Qt::CaseInsensitive)) |
|
401 title.replace(QString("\""), QString(""")); |
367 QString url = query.value(1).toString(); |
402 QString url = query.value(1).toString(); |
368 QString date = query.value(2).toString(); |
403 QString date = query.value(2).toString(); |
369 QString time = query.value(3).toString(); |
404 QString time = query.value(3).toString(); |
370 int aIndex=query.value(4).toInt(&ok); |
405 int aIndex=query.value(4).toInt(&ok); |
371 HistoryLeaf* node = new HistoryLeaf(); |
406 HistoryLeaf* node = new HistoryLeaf(); |
372 QDate adate = QDate::fromString(date, "dd.MM.yyyy"); |
407 QDate adate = QDate::fromString(date, "dd.MM.yyyy"); |
373 QTime atime = QTime::fromString(time, "h:mm ap"); |
408 QTime atime = QTime::fromString(time, "h:mm ap"); |
374 node->setTitle(title); |
409 node->setTitle(title); |
375 node->setUrl(url); |
410 node->setUrl(url); |
376 node->setDate(adate); |
411 node->setDate(adate); |
377 node->setLastVisited(atime); |
412 node->setLastVisited(atime); |
378 // node->setTag(tag); |
413 // node->setTag(tag); |
379 node->setIndex(aIndex); |
414 node->setIndex(aIndex); |
380 nodeslist.append(node); |
415 nodeslist.append(node); |
381 } |
416 } |
382 } |
417 } |
383 return nodeslist; |
418 return nodeslist; |
384 |
419 } |
385 } |
|
386 |
420 |
387 /**============================================================== |
421 /**============================================================== |
388 * Description: Clear all rows From History database |
422 * Description: Clear all rows From History database |
389 ================================================================*/ |
423 ================================================================*/ |
390 int BrowserContent::clearHistory() |
424 int BrowserContent::clearHistory() |
391 { |
425 { |
392 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
426 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
393 QSqlError error; |
427 QSqlError error; |
394 |
428 |
395 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
429 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
430 |
|
431 if (db.isOpen()) { |
|
432 QSqlQuery query(db); |
|
433 query.prepare("DELETE FROM HistoryTable" ); |
|
434 |
|
435 query.exec(); |
|
436 error = query.lastError(); |
|
437 |
|
438 if (error.type() == QSqlError::NoError) { |
|
439 return ErrNone; |
|
440 } else { |
|
441 return ErrGeneral; |
|
442 } |
|
443 } |
|
444 |
|
445 return ErrGeneral; |
|
446 } |
|
447 |
|
448 /**============================================================== |
|
449 * Description: Clear all rows From Bookmarks database |
|
450 ================================================================*/ |
|
451 int BrowserContent::clearBookmarks() |
|
452 { |
|
453 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
|
454 QSqlError error; |
|
455 |
|
456 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
457 |
|
458 if (db.isOpen()){ |
|
459 QSqlQuery query(db); |
|
460 query.prepare("DELETE FROM BookMarkTable" ); |
|
461 |
|
462 query.exec(); |
|
463 error = query.lastError(); |
|
464 |
|
465 if (error.type() == QSqlError::NoError) { |
|
466 return ErrNone; |
|
467 } |
|
468 else{ |
|
469 return ErrGeneral; |
|
470 } |
|
471 } |
|
472 |
|
473 return ErrGeneral; |
|
474 } |
|
475 |
|
476 /**============================================================== |
|
477 * Description: fetches suggested History From database |
|
478 ================================================================*/ |
|
479 QList<HistoryLeaf*> BrowserContent::suggestHistory(QString atitle){ |
|
480 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
|
481 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
482 QList<HistoryLeaf*> nodeslist; |
|
483 |
|
484 bool dbopen = db.isOpen(); |
|
485 if (dbopen){ |
|
486 QSqlQuery query(db); |
|
487 QString queryStatement = "SELECT title,url FROM HistoryTable WHERE title LIKE '%" + atitle + "%' OR url LIKE '%" + atitle + "%'"; |
|
488 query.prepare(queryStatement); |
|
489 query.exec(); |
|
490 QSqlError error = query.lastError(); |
|
491 |
|
492 while (query.next()){ |
|
493 QString title = query.value(0).toString(); |
|
494 QString url = query.value(1).toString(); |
|
495 HistoryLeaf* node = new HistoryLeaf(); |
|
496 node->setTitle(title); |
|
497 node->setUrl(url); |
|
498 nodeslist.append(node); |
|
499 } |
|
500 } |
|
501 return nodeslist; |
|
502 } |
|
503 /**============================================================== |
|
504 * Description: fetches suggested History and Bookmarks From database |
|
505 ================================================================*/ |
|
506 QObjectList BrowserContent::suggestContent(QString atitle){ |
|
507 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
|
508 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
509 |
|
510 while(!suggestedList.isEmpty()) { |
|
511 delete suggestedList.takeFirst(); |
|
512 } |
|
513 |
|
514 bool dbopen = db.isOpen(); |
|
515 if (dbopen){ |
|
516 QSqlQuery query(db); |
|
517 |
|
518 if(atitle.contains("'", Qt::CaseInsensitive)) |
|
519 atitle.replace(QString("'"), QString("''")); |
|
520 |
|
521 QString queryStatement = "SELECT title,url,1 FROM HistoryTable WHERE title LIKE '%" + atitle + "%' OR url LIKE '%" + atitle + "%'" + |
|
522 " UNION " + |
|
523 "SELECT title,url,2 FROM BookMarkTable WHERE title LIKE '%" + atitle + "%' OR url LIKE '%" + atitle + "%'" + |
|
524 "ORDER BY 3"; |
|
525 |
|
526 |
|
527 query.prepare(queryStatement); |
|
528 query.exec(); |
|
529 QSqlError error = query.lastError(); |
|
530 |
|
531 while (query.next()){ |
|
532 QString bookmarkTitle = query.value(0).toString(); |
|
533 QString bookmarkUrl = query.value(1).toString(); |
|
534 SuggestData* node = new SuggestData(bookmarkTitle, bookmarkUrl); |
|
535 suggestedList.append(node); |
|
536 } |
|
537 } |
|
538 |
|
539 return suggestedList; |
|
540 } |
|
541 /**============================================================== |
|
542 * Description: removes common strings from URLs |
|
543 ================================================================*/ |
|
544 QString BrowserContent::filterUrl(QString atitle){ |
|
545 QString https = "https://"; |
|
546 QString http = "http://"; |
|
547 QString www = "www"; |
|
548 QString com = "com"; |
|
549 QString org = "org"; |
|
550 QString htm = "htm"; |
|
551 QString html = "html"; |
|
552 |
|
553 if(atitle.contains(https, Qt::CaseInsensitive)) |
|
554 atitle = atitle.remove(https); |
|
555 if(atitle.contains(http, Qt::CaseInsensitive)) |
|
556 atitle = atitle.remove(http); |
|
557 if(atitle.contains(www, Qt::CaseInsensitive)) |
|
558 atitle = atitle.remove(www); |
|
559 if(atitle.contains(com, Qt::CaseInsensitive)) |
|
560 atitle = atitle.remove(com); |
|
561 if(atitle.contains(org, Qt::CaseInsensitive)) |
|
562 atitle = atitle.remove(org); |
|
563 if(atitle.contains(html, Qt::CaseInsensitive)) |
|
564 atitle = atitle.remove(html); |
|
565 if(atitle.contains(htm, Qt::CaseInsensitive)) |
|
566 atitle = atitle.remove(htm); |
|
567 |
|
568 |
|
569 return atitle; |
|
570 } |
|
571 /**============================================================== |
|
572 * Description: Retrieves the bookmarks and sends it in serialized fashion |
|
573 ================================================================*/ |
|
574 QString BrowserContent::FetchSerializedBookmarks() |
|
575 { |
|
576 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
|
577 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
578 |
|
579 bool dbopen = db.isOpen(); |
396 bool ok; |
580 bool ok; |
397 if (db.isOpen()){ |
581 QString bookmakrData = "["; |
398 QSqlQuery query(db); |
582 if(dbopen) |
399 query.prepare("DELETE FROM HistoryTable" ); |
583 { |
400 |
584 |
401 query.exec(); |
585 QSqlQuery query(db); |
402 error = query.lastError(); |
586 |
403 |
587 query.prepare("SELECT title,url,adate,tags,rowindex FROM BookMarkTable ORDER BY rowindex"); |
404 if (error.type() == QSqlError::NoError) { |
588 query.exec(); |
405 return ErrNone; |
589 QSqlError error = query.lastError(); |
406 } |
590 |
407 else{ |
591 while (query.next()) |
408 return ErrGeneral; |
592 { |
409 } |
593 |
410 } |
594 QString title = query.value(0).toString(); |
411 } |
595 QString url = query.value(1).toString(); |
|
596 QString date = query.value(2).toString(); |
|
597 QString tag = query.value(3).toString(); |
|
598 int aIndex=query.value(4).toInt(&ok); |
|
599 bookmakrData.append("{"); |
|
600 bookmakrData.append("\"title\": \""); |
|
601 bookmakrData.append(title); |
|
602 bookmakrData.append("\", \"urlvalue\": \""); |
|
603 bookmakrData.append(url); |
|
604 bookmakrData.append("\"},"); |
|
605 } |
|
606 |
|
607 bookmakrData.remove(bookmakrData.length(),1); |
|
608 bookmakrData.append("]"); |
|
609 |
|
610 |
|
611 } |
|
612 return bookmakrData; |
|
613 } |
|
614 |
|
615 |
|
616 /**============================================================== |
|
617 * Description: Retrieves the History and sends it in serialized fashion |
|
618 ================================================================*/ |
|
619 void BrowserContent::FetchSerializedHistory(QVector<QString> &folderVector,QMap<QString,QString> &mymap) |
|
620 { |
|
621 |
|
622 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
|
623 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
624 QList<HistoryLeaf*> nodeslist; |
|
625 int i=0; |
|
626 bool dbopen = db.isOpen(); |
|
627 bool ok; |
|
628 QString history = ""; |
|
629 |
|
630 if (dbopen) |
|
631 { |
|
632 |
|
633 QSqlQuery query(db); |
|
634 |
|
635 query.prepare("SELECT title,url,adate,atime,rowindex FROM HistoryTable ORDER BY rowindex"); |
|
636 query.exec(); |
|
637 QSqlError error = query.lastError(); |
|
638 QString prevtitle=""; |
|
639 int len=query.numRowsAffected(); |
|
640 static int count=1; |
|
641 while (query.next()) |
|
642 { |
|
643 |
|
644 QString title = query.value(0).toString(); |
|
645 QString url = query.value(1).toString(); |
|
646 QString date = query.value(2).toString(); |
|
647 QString time = query.value(3).toString(); |
|
648 int aIndex=query.value(4).toInt(&ok); |
|
649 HistoryLeaf* node = new HistoryLeaf(); |
|
650 QDate adate = QDate::fromString(date, "dd.MM.yyyy"); |
|
651 QTime atime = QTime::fromString(time, "h:mm ap"); |
|
652 |
|
653 QString foldertitle=findFolderForDate(adate); |
|
654 |
|
655 |
|
656 if(folderVector.contains(foldertitle)) |
|
657 { |
|
658 mymap[prevtitle].append("\"},"); |
|
659 } |
|
660 else |
|
661 { |
|
662 folderVector.append(foldertitle); |
|
663 if(count>1) |
|
664 { |
|
665 mymap[prevtitle].append("\"}"); |
|
666 mymap[prevtitle].append ("]"); |
|
667 mymap[foldertitle].append("["); |
|
668 } |
|
669 else |
|
670 { |
|
671 mymap[foldertitle].append("["); |
|
672 } |
|
673 count++; |
|
674 |
|
675 |
|
676 } |
|
677 |
|
678 prevtitle=foldertitle; |
|
679 |
|
680 mymap[foldertitle].append("{"); |
|
681 mymap[foldertitle].append("\"titleVal\": \""); |
|
682 mymap[foldertitle].append(title); |
|
683 mymap[foldertitle].append("\", \"dateVal\": \""); |
|
684 mymap[foldertitle].append(adate.toString("dd.MM.yyyy")); |
|
685 mymap[foldertitle].append("\", \"urlVal\": \""); |
|
686 mymap[foldertitle].append(url); |
|
687 mymap[foldertitle].append("\", \"timeVal\": \""); |
|
688 mymap[foldertitle].append(atime.toString("h:mm ap")); |
|
689 } |
|
690 mymap[prevtitle].append("\"}"); |
|
691 mymap[prevtitle].append ("]"); |
|
692 } |
|
693 |
|
694 } |
|
695 /**============================================================== |
|
696 * Description: Retrieves the bookmark titles and sends it in serialized fashion |
|
697 ================================================================*/ |
|
698 void BrowserContent::FetchAllBookmarkTitles(QVector<QString> &title) |
|
699 { |
|
700 BOOKMARKSCLIENT_PRIVATEPTR(BrowserContent); |
|
701 QSqlDatabase db = QSqlDatabase::database(priv->m_connectionName); |
|
702 |
|
703 bool dbopen = db.isOpen(); |
|
704 bool ok; |
|
705 int i=0; |
|
706 |
|
707 if(dbopen) |
|
708 { |
|
709 |
|
710 QSqlQuery query(db); |
|
711 |
|
712 query.prepare("SELECT title FROM BookMarkTable"); |
|
713 query.exec(); |
|
714 QSqlError error = query.lastError(); |
|
715 |
|
716 while (query.next()) |
|
717 { |
|
718 |
|
719 title.append(query.value(0).toString()); |
|
720 } |
|
721 } |
|
722 } |
|
723 |
|
724 /**============================================================== |
|
725 * Description: calculate the folder title |
|
726 ================================================================*/ |
|
727 QString BrowserContent::findFolderForDate( QDate& nodeDate) |
|
728 { |
|
729 QDateTime currentDateTime = QDateTime::currentDateTime(); |
|
730 int currentDayOfWeek = currentDateTime.date().dayOfWeek(); |
|
731 |
|
732 int nodeDayOfWeek = nodeDate.dayOfWeek(); |
|
733 |
|
734 int daysToCurrentDate = nodeDate.daysTo(currentDateTime.date()); |
|
735 |
|
736 //Check if date to belongs to "ToDay" Folder |
|
737 if(nodeDate == currentDateTime.date()){ |
|
738 QString folder = qtTrId("txt_browser_history_today"); |
|
739 return folder; |
|
740 } |
|
741 //Check if date to belongs to "YesterDay" Folder |
|
742 if(nodeDate.addDays(1) == currentDateTime.date() ){ |
|
743 QString folder = qtTrId("txt_browser_history_yesterday"); |
|
744 return folder; |
|
745 } |
|
746 |
|
747 //Check if date to belongs to current week folder |
|
748 //Should disply the day for the current week |
|
749 if(daysToCurrentDate < 7 && currentDayOfWeek > nodeDayOfWeek ){ |
|
750 |
|
751 QString folder = qtTrId("txt_browser_history_this_week"); |
|
752 return folder; |
|
753 } |
|
754 |
|
755 if(dateInThisMonth(nodeDate)){ |
|
756 QString folder = qtTrId("txt_browser_history_this_month"); |
|
757 return folder; |
|
758 } |
|
759 |
|
760 QString folder = nodeDate.toString("dd.MM.yyyy"); |
|
761 |
|
762 return folder; |
|
763 |
|
764 } |
|
765 /**============================================================== |
|
766 * Description: calculate whether the date falls with in this month |
|
767 ================================================================*/ |
|
768 bool BrowserContent::dateInThisMonth(QDate &date) |
|
769 { |
|
770 QDate currentDate = QDateTime::currentDateTime().date(); |
|
771 int daysToCurrentDate = currentDate.daysTo(date); |
|
772 |
|
773 int currentMonth = currentDate.month(); |
|
774 int nodeMonth = date.month(); |
|
775 |
|
776 if(daysToCurrentDate <= 31 && currentMonth == nodeMonth) { |
|
777 return true; |
|
778 } |
|
779 return false; |
|
780 |
|
781 } |