|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
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 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <QList> |
|
19 #include <QDebug> |
|
20 #include <QString> |
|
21 #include <QtSql> |
|
22 #include <QMap> |
|
23 #include <QMapIterator> |
|
24 |
|
25 #include "caclientproxy.h" |
|
26 #include "caobjectadapter.h" |
|
27 |
|
28 #include "caentry.h" |
|
29 #include "caquery.h" |
|
30 #include "cadefs.h" |
|
31 #include "canotifier.h" |
|
32 #include "canotifier_p.h" |
|
33 #include "canotifiers.h" |
|
34 |
|
35 #include "hswidgetregistryservice.h" |
|
36 |
|
37 const char *DATABASE_CONNECTION_NAME = "CaService"; |
|
38 const char *DATABASE_TYPE = "QSQLITE"; |
|
39 const char *DATABASE_NAME = "castoragedb"; |
|
40 |
|
41 static QSqlDatabase dbConnection() |
|
42 { |
|
43 return QSqlDatabase::database(DATABASE_CONNECTION_NAME, false); |
|
44 } |
|
45 |
|
46 //---------------------------------------------------------------------------- |
|
47 // |
|
48 //---------------------------------------------------------------------------- |
|
49 CaClientProxy::CaClientProxy() : |
|
50 mWidgetRegistryPath("hsresources/import/widgetregistry") |
|
51 { |
|
52 } |
|
53 |
|
54 //---------------------------------------------------------------------------- |
|
55 // |
|
56 //---------------------------------------------------------------------------- |
|
57 CaClientProxy::~CaClientProxy() |
|
58 { |
|
59 QSqlDatabase db = dbConnection(); |
|
60 if (db.isOpen()) { |
|
61 db.close(); |
|
62 } |
|
63 QSqlDatabase::removeDatabase(DATABASE_CONNECTION_NAME); |
|
64 } |
|
65 |
|
66 //---------------------------------------------------------------------------- |
|
67 // |
|
68 //---------------------------------------------------------------------------- |
|
69 ErrorCode CaClientProxy::connect() |
|
70 { |
|
71 ErrorCode errorCode = NotFoundErrorCode; |
|
72 QSqlDatabase db = QSqlDatabase::addDatabase(DATABASE_TYPE, |
|
73 DATABASE_CONNECTION_NAME); |
|
74 if (db.isValid()) { |
|
75 db.setDatabaseName(DATABASE_NAME); |
|
76 if (db.open()) { |
|
77 errorCode = NoErrorCode; |
|
78 updateWidgets(); |
|
79 } |
|
80 } |
|
81 if (errorCode) { |
|
82 qDebug("CaClientProxy::CaClientProxy FAILED"); |
|
83 } |
|
84 return errorCode; |
|
85 } |
|
86 |
|
87 /*! |
|
88 Updates widgets. |
|
89 */ |
|
90 void CaClientProxy::updateWidgets() |
|
91 { |
|
92 qDebug("CaClientProxy::updateWidgets start"); |
|
93 |
|
94 HsWidgetRegistryService *rs = |
|
95 new HsWidgetRegistryService(mWidgetRegistryPath); |
|
96 QList<HsWidgetToken> widgets = rs->widgets(); |
|
97 |
|
98 // Read widgets in order to add synchronize the content of the widgets |
|
99 // registry with Content Storage database. |
|
100 foreach(const HsWidgetToken &widgetToken, widgets) { |
|
101 int uid = widgetToken.mUid; |
|
102 |
|
103 if (!hsWidgetExists(uid)) { |
|
104 // The given widget does not have a corresonding entry |
|
105 // in the databse, so such an entry needs do be created. |
|
106 addWidgetEntry(widgetToken); |
|
107 } |
|
108 } |
|
109 |
|
110 delete rs; |
|
111 qDebug("CaClientProxy::updateWidgets end"); |
|
112 } |
|
113 |
|
114 /*! |
|
115 Returns true if a widget with the given uid exists in the database. |
|
116 */ |
|
117 void CaClientProxy::addWidgetEntry(const HsWidgetToken &widgetToken) |
|
118 { |
|
119 QString description = widgetToken.mDescription; |
|
120 QString iconUri = widgetToken.mIconUri; |
|
121 QString library = widgetToken.mLibrary; |
|
122 QString title = widgetToken.mTitle; |
|
123 int uid = widgetToken.mUid; |
|
124 QString uri = widgetToken.mUri; |
|
125 QSqlDatabase db = dbConnection(); |
|
126 QSqlQuery query(db); |
|
127 QString hexUid; |
|
128 hexUid.setNum(uid,16); |
|
129 QDir currentDir = QDir::current(); |
|
130 |
|
131 // Add icon. |
|
132 QString queryAddIcon = |
|
133 "INSERT INTO CA_ICON " \ |
|
134 "(IC_FILENAME) " \ |
|
135 "VALUES " \ |
|
136 "(?)"; |
|
137 |
|
138 query.prepare(queryAddIcon); |
|
139 query.addBindValue(iconUri); |
|
140 query.exec(); |
|
141 qDebug() << query.executedQuery(); |
|
142 |
|
143 // Add entry. |
|
144 QString queryAddEntry = |
|
145 "INSERT INTO CA_ENTRY " \ |
|
146 "(EN_TEXT, EN_DESCRIPTION, EN_ROLE, EN_TYPE_NAME, EN_ICON_ID) " \ |
|
147 "VALUES " \ |
|
148 "(?, ?, 1, 'widget', last_insert_rowid())"; |
|
149 |
|
150 query.prepare(queryAddEntry); |
|
151 query.addBindValue(title); |
|
152 query.addBindValue(description); |
|
153 query.exec(); |
|
154 qDebug() << query.executedQuery(); |
|
155 |
|
156 // Get last id |
|
157 QString queryLastId = "SELECT last_insert_rowid() AS LAST_ID"; |
|
158 query.prepare(queryLastId); |
|
159 query.exec(); |
|
160 query.next(); |
|
161 int lastId = query.value(query.record().indexOf("LAST_ID")).toInt(); |
|
162 |
|
163 // Add attribute packageuid |
|
164 QString queryAddAttribute1 = |
|
165 "INSERT INTO CA_ATTRIBUTE " \ |
|
166 "(AT_ENTRY_ID, AT_NAME, AT_VALUE) " \ |
|
167 "VALUES " \ |
|
168 "(?, 'packageuid', ?)"; |
|
169 |
|
170 query.prepare(queryAddAttribute1); |
|
171 query.addBindValue(lastId); |
|
172 query.addBindValue(hexUid); |
|
173 query.exec(); |
|
174 qDebug() << query.executedQuery(); |
|
175 |
|
176 // Add attribute widget uri |
|
177 QString queryAddAttribute2 = |
|
178 "INSERT INTO CA_ATTRIBUTE " \ |
|
179 "(AT_ENTRY_ID, AT_NAME, AT_VALUE) " \ |
|
180 "VALUES " \ |
|
181 "(?, 'widget:uri', ?)"; |
|
182 |
|
183 query.prepare(queryAddAttribute2); |
|
184 query.addBindValue(lastId); |
|
185 query.addBindValue(uri); |
|
186 query.exec(); |
|
187 qDebug() << query.executedQuery(); |
|
188 |
|
189 // Add attribute widget library |
|
190 QString queryAddAttribute3 = |
|
191 "INSERT INTO CA_ATTRIBUTE " \ |
|
192 "(AT_ENTRY_ID, AT_NAME, AT_VALUE) " \ |
|
193 "VALUES " \ |
|
194 "(?, 'widget:library', ?)"; |
|
195 |
|
196 query.prepare(queryAddAttribute3); |
|
197 query.addBindValue(lastId); |
|
198 query.addBindValue(library); |
|
199 query.exec(); |
|
200 qDebug() << query.executedQuery(); |
|
201 } |
|
202 |
|
203 /*! |
|
204 Returns true if a widget with the given uid exists in the database. |
|
205 */ |
|
206 bool CaClientProxy::hsWidgetExists(int uid) |
|
207 { |
|
208 bool exists(false); |
|
209 QSqlDatabase db = dbConnection(); |
|
210 QSqlQuery query(db); |
|
211 QString hexUid; |
|
212 hexUid.setNum(uid,16); |
|
213 |
|
214 QString queryString = |
|
215 "SELECT " \ |
|
216 "AT_ENTRY_ID " \ |
|
217 "FROM " \ |
|
218 "CA_ATTRIBUTE " \ |
|
219 "WHERE " \ |
|
220 "AT_VALUE LIKE ?"; |
|
221 |
|
222 query.prepare(queryString); |
|
223 query.addBindValue(hexUid); |
|
224 |
|
225 if (query.exec() && query.next()) { |
|
226 // Query returned a non empty result. |
|
227 exists = true; |
|
228 } else { |
|
229 // The widget with the given uid was not found. |
|
230 exists = false; |
|
231 } |
|
232 |
|
233 qDebug() << query.executedQuery(); |
|
234 |
|
235 return exists; |
|
236 } |
|
237 |
|
238 //---------------------------------------------------------------------------- |
|
239 // |
|
240 //---------------------------------------------------------------------------- |
|
241 ErrorCode CaClientProxy::addData(const CaEntry &entryToAdd, |
|
242 CaEntry &targetEntry) |
|
243 { |
|
244 qDebug() << "CaClientProxy::addData" << "entry id: " |
|
245 << entryToAdd.id(); |
|
246 |
|
247 targetEntry = entryToAdd; |
|
248 QSqlDatabase db = dbConnection(); |
|
249 QSqlQuery query(db); |
|
250 if (entryToAdd.id() == 0) { |
|
251 CaObjectAdapter::setId(targetEntry, 0); |
|
252 } |
|
253 query.exec("begin"); |
|
254 bool success = (setIconInDb(&targetEntry) |
|
255 && setEntryInDb(&targetEntry) |
|
256 && setAttributesInDb(&targetEntry)); |
|
257 if (success) { |
|
258 query.exec("commit"); |
|
259 QList<int> parentIds; |
|
260 GetParentsIds(QList<int>() << targetEntry.id(), parentIds); |
|
261 if (entryToAdd.id() == 0) { |
|
262 CaNotifiers::Notify(targetEntry, AddChangeType, parentIds); |
|
263 } else { |
|
264 CaNotifiers::Notify(targetEntry, UpdateChangeType, parentIds); |
|
265 } |
|
266 return NoErrorCode; |
|
267 } else { |
|
268 query.exec("rollback"); |
|
269 return UnknownErrorCode; |
|
270 } |
|
271 } |
|
272 |
|
273 //---------------------------------------------------------------------------- |
|
274 // |
|
275 //---------------------------------------------------------------------------- |
|
276 ErrorCode CaClientProxy::removeData(const QList<int> &entryIdList) |
|
277 { |
|
278 qDebug() << "CaClientProxy::removeData" << "entryIdList: " |
|
279 << entryIdList; |
|
280 |
|
281 QList<CaEntry *> entryList; |
|
282 getData(entryIdList, entryList); |
|
283 QList<QList<int> > parentsIds; |
|
284 foreach(CaEntry *entry, entryList) { |
|
285 QList<int> parentIds; |
|
286 GetParentsIds(QList<int>() << entry->id(), parentIds); |
|
287 parentsIds.append(parentIds); |
|
288 } |
|
289 |
|
290 QSqlDatabase db = dbConnection(); |
|
291 QSqlQuery query(db); |
|
292 //begin transaction |
|
293 bool success(false); |
|
294 query.exec("begin"); |
|
295 foreach(int entryId, entryIdList) { |
|
296 query.prepare( |
|
297 "SELECT ENTRY_ID FROM CA_ENTRY WHERE ENTRY_ID = ?"); |
|
298 query.addBindValue(entryId); |
|
299 success = query.exec(); |
|
300 if (success && query.next()) { |
|
301 success |
|
302 = query.value(query.record().indexOf("ENTRY_ID")).toInt() |
|
303 > 0; |
|
304 } |
|
305 if (!success) { |
|
306 break; |
|
307 } |
|
308 |
|
309 query.prepare( |
|
310 "SELECT EN_ICON_ID FROM CA_ENTRY WHERE ENTRY_ID = ?"); |
|
311 query.addBindValue(entryId); |
|
312 int iconId(0); |
|
313 success = query.exec(); |
|
314 if (success && query.next()) { |
|
315 iconId |
|
316 = query.value(query.record().indexOf("EN_ICON_ID")).toInt(); |
|
317 } else { |
|
318 break; |
|
319 } |
|
320 |
|
321 query.prepare("DELETE FROM CA_LAUNCH WHERE LA_ENTRY_ID = ?"); |
|
322 query.addBindValue(entryId); |
|
323 success = query.exec(); |
|
324 if (success) { |
|
325 qDebug() << query.lastQuery() << " rows deleted: " |
|
326 << query.numRowsAffected(); |
|
327 } else { |
|
328 break; |
|
329 } |
|
330 |
|
331 query.prepare( |
|
332 "DELETE FROM CA_GROUP_ENTRY WHERE GE_ENTRY_ID = ?"); |
|
333 query.addBindValue(entryId); |
|
334 success = query.exec(); |
|
335 if (success) { |
|
336 qDebug() << query.lastQuery() << " rows deleted: " |
|
337 << query.numRowsAffected(); |
|
338 } else { |
|
339 break; |
|
340 } |
|
341 |
|
342 query.prepare( |
|
343 "DELETE FROM CA_GROUP_ENTRY WHERE GE_GROUP_ID = ?"); |
|
344 query.addBindValue(entryId); |
|
345 success = query.exec(); |
|
346 if (success) { |
|
347 qDebug() << query.lastQuery() << " rows deleted: " |
|
348 << query.numRowsAffected(); |
|
349 } else { |
|
350 break; |
|
351 } |
|
352 |
|
353 query.prepare("DELETE FROM CA_ATTRIBUTE WHERE AT_ENTRY_ID = ?"); |
|
354 query.addBindValue(entryId); |
|
355 success = query.exec(); |
|
356 if (success) { |
|
357 qDebug() << query.lastQuery() << " rows deleted: " |
|
358 << query.numRowsAffected(); |
|
359 } else { |
|
360 break; |
|
361 } |
|
362 |
|
363 query.prepare("DELETE FROM CA_ENTRY WHERE ENTRY_ID = ?"); |
|
364 query.addBindValue(entryId); |
|
365 success = query.exec(); |
|
366 if (success) { |
|
367 qDebug() << query.lastQuery() << " rows deleted: " |
|
368 << query.numRowsAffected(); |
|
369 } else { |
|
370 break; |
|
371 } |
|
372 |
|
373 if (iconId != 0) { |
|
374 query.prepare("DELETE FROM CA_ICON WHERE ICON_ID = ?"); |
|
375 query.addBindValue(iconId); |
|
376 success = query.exec(); |
|
377 if (success) { |
|
378 qDebug() << query.lastQuery() << " rows deleted: " |
|
379 << query.numRowsAffected(); |
|
380 } else { |
|
381 // ignore, this means that the icon cannot be removed |
|
382 // because some other entry has the same icon. |
|
383 success = true; |
|
384 } |
|
385 } |
|
386 } |
|
387 |
|
388 ErrorCode error(NoErrorCode); |
|
389 if (success) { |
|
390 query.exec("commit"); |
|
391 if (parentsIds.count() == entryList.count()) { |
|
392 for (int i = 0; i < entryList.count(); i++) { |
|
393 GetParentsIds(QList<int>() << entryList[i]->id(), parentsIds[i]); |
|
394 CaNotifiers::Notify(*entryList[i], RemoveChangeType, parentsIds[i]); |
|
395 } |
|
396 } |
|
397 } else { |
|
398 query.exec("rollback"); |
|
399 error = UnknownErrorCode; |
|
400 } |
|
401 |
|
402 return error; |
|
403 } |
|
404 |
|
405 //---------------------------------------------------------------------------- |
|
406 // |
|
407 //---------------------------------------------------------------------------- |
|
408 ErrorCode CaClientProxy::insertEntriesIntoGroup(int groupId, |
|
409 const QList<int> &entryIdList, int beforeEntryId) |
|
410 { |
|
411 qDebug() << "CaClientProxy::insertEntriesIntoGroup" << "groupId: " |
|
412 << groupId << "beforeEntryId: " << beforeEntryId << "entryIdList: " |
|
413 << entryIdList; |
|
414 |
|
415 removeEntriesFromGroup(groupId, entryIdList, false); |
|
416 QString queryText; |
|
417 if (beforeEntryId == AfterTheLastEntry) { |
|
418 queryText = QString("INSERT INTO CA_GROUP_ENTRY (GE_GROUP_ID,GE_ENTRY_ID,GE_POSITION) " |
|
419 "VALUES ( ?, ?,(SELECT MAX(DATA) FROM ( SELECT MAX(GE_POSITION)+ 1 AS DATA FROM CA_GROUP_ENTRY " |
|
420 "WHERE GE_GROUP_ID = %1 UNION SELECT 1 AS DATA FROM CA_GROUP_ENTRY ) ) )").arg(groupId); |
|
421 } else if (beforeEntryId == BeforeTheFirstEntry) { |
|
422 queryText = "INSERT INTO CA_GROUP_ENTRY (GE_GROUP_ID,GE_ENTRY_ID,GE_POSITION) VALUES ( ?, ?, 0 ) "; |
|
423 } else { |
|
424 queryText = QString("INSERT INTO CA_GROUP_ENTRY (GE_GROUP_ID,GE_ENTRY_ID,GE_POSITION) VALUES ( " |
|
425 "?, ?, ( SELECT GE_POSITION FROM CA_GROUP_ENTRY WHERE GE_ENTRY_ID = %1 ) ) ").arg(beforeEntryId); |
|
426 } |
|
427 |
|
428 bool success = true; |
|
429 QSqlDatabase db = dbConnection(); |
|
430 QSqlQuery query(db); |
|
431 query.exec("begin"); |
|
432 for (int i = 0; i < entryIdList.count() && success; ++i) { |
|
433 query.prepare(queryText); |
|
434 query.addBindValue(groupId); |
|
435 query.addBindValue(entryIdList.at(i)); |
|
436 success = query.exec(); |
|
437 } |
|
438 |
|
439 ErrorCode error(NoErrorCode); |
|
440 if (success) { |
|
441 query.exec("commit"); |
|
442 CaNotifiers::Notify(groupId); |
|
443 } else { |
|
444 query.exec("rollback"); |
|
445 error = UnknownErrorCode; |
|
446 } |
|
447 return error; |
|
448 } |
|
449 |
|
450 //---------------------------------------------------------------------------- |
|
451 // |
|
452 //---------------------------------------------------------------------------- |
|
453 ErrorCode CaClientProxy::removeEntriesFromGroup(int groupId, |
|
454 const QList<int> &entryIdList, |
|
455 bool calledDirectly) |
|
456 { |
|
457 qDebug() << "CaClientProxy::removeEntriesFromGroup" << "groupId: " |
|
458 << groupId << "entryIdList: " << entryIdList; |
|
459 |
|
460 bool success = true; |
|
461 QSqlDatabase db = dbConnection(); |
|
462 QSqlQuery query(db); |
|
463 query.exec("begin"); |
|
464 for (int i = 0; i < entryIdList.count() && success; ++i) { |
|
465 query.prepare( |
|
466 "DELETE FROM CA_GROUP_ENTRY WHERE GE_ENTRY_ID = ? AND GE_GROUP_ID = ? "); |
|
467 query.addBindValue(entryIdList.at(i)); |
|
468 query.addBindValue(groupId); |
|
469 success = query.exec(); |
|
470 } |
|
471 |
|
472 ErrorCode error(NoErrorCode); |
|
473 if (success) { |
|
474 query.exec("commit"); |
|
475 if (calledDirectly) { |
|
476 CaNotifiers::Notify(groupId); |
|
477 } |
|
478 } else { |
|
479 query.exec("rollback"); |
|
480 error = UnknownErrorCode; |
|
481 } |
|
482 return error; |
|
483 } |
|
484 |
|
485 //---------------------------------------------------------------------------- |
|
486 // |
|
487 //---------------------------------------------------------------------------- |
|
488 ErrorCode CaClientProxy::getData(const QList<int> &entryIdList, |
|
489 QList<CaEntry *> &sourceList) |
|
490 { |
|
491 qDebug() << "CaClientProxy::getData" << "entryIdList: " |
|
492 << entryIdList; |
|
493 |
|
494 QSqlDatabase db = dbConnection(); |
|
495 |
|
496 bool success(true); |
|
497 foreach(int i, entryIdList) { |
|
498 QSqlQuery query(db); |
|
499 query.prepare( |
|
500 "SELECT ENTRY_ID, EN_TEXT, EN_DESCRIPTION, EN_TYPE_NAME, EN_FLAGS, EN_ROLE, EN_UID, \ |
|
501 ICON_ID, IC_BITMAP_ID, IC_MASK_ID, IC_SKINMAJOR_ID, IC_SKINMINOR_ID, IC_FILENAME \ |
|
502 FROM CA_ENTRY LEFT JOIN CA_ICON ON EN_ICON_ID = ICON_ID WHERE ENTRY_ID = ?"); |
|
503 query.addBindValue(i); |
|
504 |
|
505 success = query.exec(); |
|
506 if (success && query.next()) { |
|
507 qDebug() << query.executedQuery(); |
|
508 int role = |
|
509 query.value(query.record().indexOf("EN_ROLE")).toInt(); |
|
510 CaEntry *entry = new CaEntry((EntryRole) role); |
|
511 CaObjectAdapter::setId(*entry, |
|
512 query.value(query.record().indexOf("ENTRY_ID")).toInt()); |
|
513 entry->setText(query.value( |
|
514 query.record().indexOf("EN_TEXT")).toString()); |
|
515 entry->setDescription(query.value( |
|
516 query.record().indexOf("EN_DESCRIPTION")).toString()); |
|
517 entry->setEntryTypeName(query.value(query.record().indexOf( |
|
518 "EN_TYPE_NAME")).toString()); |
|
519 entry->setFlags(static_cast<EntryFlag>(query.value( |
|
520 query.record().indexOf("EN_FLAGS")).toUInt())); |
|
521 |
|
522 CaIconDescription icon; |
|
523 CaObjectAdapter::setId(icon, |
|
524 query.value(query.record().indexOf("ICON_ID")).toInt()); |
|
525 icon.setBitmapId(query.value(query.record().indexOf( |
|
526 "IC_BITMAP_ID")).toInt()); |
|
527 icon.setMaskId(query.value(query.record().indexOf( |
|
528 "IC_MASK_ID")).toInt()); |
|
529 icon.setSkinMajorId(query.value(query.record().indexOf( |
|
530 "IC_SKINMAJOR_ID")).toInt()); |
|
531 icon.setSkinMinorId(query.value(query.record().indexOf( |
|
532 "IC_SKINMINOR_ID")).toInt()); |
|
533 icon.setFilename(query.value(query.record().indexOf( |
|
534 "IC_FILENAME")).toString()); |
|
535 entry->setIconDescription(icon); |
|
536 |
|
537 // attributes |
|
538 // UID as attribute |
|
539 if (query.value(query.record().indexOf("EN_UID")).toString().length() |
|
540 > 0) { |
|
541 entry->setAttribute("application:uid", query.value( |
|
542 query.record().indexOf("EN_UID")).toString()); |
|
543 } |
|
544 |
|
545 // fetch from DB attributes |
|
546 QSqlQuery attributesQuery(db); |
|
547 attributesQuery.prepare( |
|
548 "SELECT AT_NAME, AT_VALUE FROM CA_ATTRIBUTE WHERE AT_ENTRY_ID = ?"); |
|
549 attributesQuery.addBindValue(i); |
|
550 success = attributesQuery.exec(); |
|
551 if (success) { |
|
552 while (attributesQuery.next()) { |
|
553 entry->setAttribute( |
|
554 attributesQuery.value(0).toString(), |
|
555 attributesQuery.value(1).toString()); |
|
556 } |
|
557 sourceList << entry; |
|
558 } |
|
559 } else { |
|
560 break; |
|
561 } |
|
562 } |
|
563 |
|
564 ErrorCode error = NoErrorCode; |
|
565 if (!success) { |
|
566 error = UnknownErrorCode; |
|
567 } else if ((entryIdList.count() != sourceList.count())) { |
|
568 error = NotFoundErrorCode; |
|
569 } |
|
570 return error; |
|
571 } |
|
572 |
|
573 //---------------------------------------------------------------------------- |
|
574 // |
|
575 //---------------------------------------------------------------------------- |
|
576 ErrorCode CaClientProxy::getData(const CaQuery &query, |
|
577 QList<CaEntry *> &sourceList) |
|
578 { |
|
579 QList<int> entryIdList; |
|
580 ErrorCode errorCode = getEntryIds(query, entryIdList); |
|
581 if (errorCode == NoErrorCode) { |
|
582 errorCode = getData(entryIdList, sourceList); |
|
583 } |
|
584 return errorCode; |
|
585 } |
|
586 |
|
587 //---------------------------------------------------------------------------- |
|
588 // |
|
589 //---------------------------------------------------------------------------- |
|
590 ErrorCode CaClientProxy::getEntryIds(const CaQuery &query, |
|
591 QList<int> &sourceIdList) |
|
592 { |
|
593 qDebug() << "CaClientProxy::getEntryIds"; |
|
594 |
|
595 QSqlDatabase db = dbConnection(); |
|
596 |
|
597 bool success(true); |
|
598 QString whereStatement; |
|
599 if (query.flagsOn() != 0) |
|
600 whereStatement.append(" AND ").append(QString().setNum( |
|
601 query.flagsOn())).append(" & EN_FLAGS == ").append( |
|
602 QString().setNum(query.flagsOn())); |
|
603 if (query.flagsOff() != 0) |
|
604 whereStatement.append(" AND ").append(QString().setNum( |
|
605 query.flagsOff())).append(" & (~EN_FLAGS) == ").append( |
|
606 QString().setNum(query.flagsOff())); |
|
607 if (query.entryRoles() != 0) |
|
608 whereStatement.append(" AND ").append(QString().setNum( |
|
609 query.entryRoles())) .append(" | EN_ROLE == ").append( |
|
610 QString().setNum(query.entryRoles())); |
|
611 //TODO: by uid??? |
|
612 |
|
613 if (query.entryTypeNames().count()) { |
|
614 whereStatement.append(" AND EN_TYPE_NAME IN ("); |
|
615 for (int i = 0; i < query.entryTypeNames().count(); i++) { |
|
616 whereStatement.append("\'" + query.entryTypeNames()[i] + "\'"); |
|
617 if (i < query.entryTypeNames().count() - 1) |
|
618 whereStatement.append(","); |
|
619 } |
|
620 whereStatement.append(") "); |
|
621 } |
|
622 |
|
623 QSqlQuery sqlquery(db); |
|
624 if (query.parentId() == 0) { |
|
625 // sort |
|
626 QString queryString("SELECT ENTRY_ID from CA_ENTRY where 1=1 "); |
|
627 queryString.append(whereStatement); |
|
628 modifyQueryForSortOrder(queryString, query, false); |
|
629 if (query.count() > 0) |
|
630 queryString.append(" LIMIT ").append(query.count()); |
|
631 qDebug() << "CaServicePrivate::getEntryIds query text: " |
|
632 << queryString; |
|
633 success = sqlquery.prepare(queryString); |
|
634 success = sqlquery.exec(); |
|
635 if (success) { |
|
636 while (sqlquery.next()) { |
|
637 sourceIdList << sqlquery.value(sqlquery.record().indexOf( |
|
638 "ENTRY_ID")).toInt(); |
|
639 } |
|
640 } |
|
641 } else { |
|
642 QString |
|
643 queryString( |
|
644 "SELECT ENTRY_ID FROM CA_ENTRY \ |
|
645 LEFT JOIN CA_GROUP_ENTRY ON GE_ENTRY_ID = ENTRY_ID WHERE GE_GROUP_ID = ? "); |
|
646 queryString.append(whereStatement); |
|
647 modifyQueryForSortOrder(queryString, query, true); |
|
648 if (query.count() > 0) |
|
649 queryString.append(" LIMIT ").append(query.count()); |
|
650 qDebug() << "CaServicePrivate::getEntryIds query text: " |
|
651 << queryString; |
|
652 sqlquery.prepare(queryString); |
|
653 sqlquery.addBindValue(query.parentId()); |
|
654 success = sqlquery.exec(); |
|
655 if (success) { |
|
656 while (sqlquery.next()) { |
|
657 sourceIdList << sqlquery.value(sqlquery.record().indexOf( |
|
658 "ENTRY_ID")).toInt(); |
|
659 } |
|
660 } |
|
661 } |
|
662 ErrorCode error = UnknownErrorCode; |
|
663 if (success) { |
|
664 error = NoErrorCode; |
|
665 } |
|
666 return error; |
|
667 } |
|
668 |
|
669 //---------------------------------------------------------------------------- |
|
670 // |
|
671 //---------------------------------------------------------------------------- |
|
672 ErrorCode CaClientProxy::executeCommand(const CaEntry &entry, |
|
673 const QString &command) |
|
674 { |
|
675 qDebug() << "CaClientProxy::executeCommand" << "entry id: " |
|
676 << entry.id() << "command: " << command; |
|
677 return NoErrorCode; |
|
678 } |
|
679 |
|
680 //---------------------------------------------------------------------------- |
|
681 // |
|
682 //---------------------------------------------------------------------------- |
|
683 ErrorCode CaClientProxy::touch(const CaEntry &entry) |
|
684 { |
|
685 const int id = entry.id(); |
|
686 |
|
687 qDebug() << "CaClientProxy::touch" << "id: " << id; |
|
688 |
|
689 QSqlDatabase db = dbConnection(); |
|
690 QSqlQuery query(db); |
|
691 query.exec("begin"); |
|
692 |
|
693 query.prepare( |
|
694 "INSERT INTO CA_LAUNCH (LA_ENTRY_ID,LA_LAUNCH_TIME) VALUES ( ?,? )"); |
|
695 query.addBindValue(id); |
|
696 query.addBindValue(QDateTime::currentDateTime().toTime_t()); |
|
697 bool success = query.exec(); |
|
698 |
|
699 if (success) { |
|
700 query.prepare( |
|
701 "UPDATE CA_ENTRY SET EN_FLAGS = EN_FLAGS | ? WHERE ENTRY_ID = ?"); |
|
702 query.addBindValue((int) UsedEntryFlag); |
|
703 query.addBindValue(id); |
|
704 success = query.exec(); |
|
705 } |
|
706 |
|
707 ErrorCode error = NoErrorCode; |
|
708 if (success) { |
|
709 query.exec("commit"); |
|
710 QList<CaEntry *> entryList; |
|
711 if (getData(QList<int>() << id, entryList) == NoErrorCode) { |
|
712 QList<int> parentIds; |
|
713 GetParentsIds(QList<int>() << id, parentIds); |
|
714 CaNotifiers::Notify(*entryList[0], UpdateChangeType, parentIds); |
|
715 } |
|
716 } else { |
|
717 query.exec("rollback"); |
|
718 error = UnknownErrorCode; |
|
719 } |
|
720 |
|
721 return error; |
|
722 } |
|
723 |
|
724 //---------------------------------------------------------------------------- |
|
725 // |
|
726 //---------------------------------------------------------------------------- |
|
727 ErrorCode CaClientProxy::customSort(const QList<int> &entryIdList, |
|
728 int groupId) |
|
729 { |
|
730 bool success = true; |
|
731 QSqlDatabase db = dbConnection(); |
|
732 QSqlQuery query(db); |
|
733 query.exec("begin"); |
|
734 for (int i = 0; i < entryIdList.count(); i++) { |
|
735 int position = i+1; |
|
736 query.prepare( |
|
737 "UPDATE CA_GROUP_ENTRY SET GE_POSITION = ? WHERE GE_ENTRY_ID = ? AND GE_GROUP_ID = ?"); |
|
738 query.addBindValue(position); |
|
739 query.addBindValue(entryIdList.at(i)); |
|
740 query.addBindValue(groupId); |
|
741 success = query.exec(); |
|
742 if (!success) { |
|
743 break; |
|
744 } |
|
745 } |
|
746 |
|
747 ErrorCode error(NoErrorCode); |
|
748 if (success) { |
|
749 query.exec("commit"); |
|
750 CaNotifiers::Notify(groupId); |
|
751 } else { |
|
752 query.exec("rollback"); |
|
753 error = UnknownErrorCode; |
|
754 } |
|
755 return error; |
|
756 } |
|
757 |
|
758 /*! |
|
759 //TODO: |
|
760 */ |
|
761 void CaClientProxy::modifyQueryForSortOrder(QString &queryString, |
|
762 const CaQuery &query, bool parent) const |
|
763 { |
|
764 SortAttribute sortAttribute; |
|
765 Qt::SortOrder sortOrder; |
|
766 query.getSort(sortAttribute, sortOrder); |
|
767 QString oldQueryString(queryString); |
|
768 queryString.clear(); |
|
769 |
|
770 if (sortAttribute == NameSortAttribute) { |
|
771 queryString.append(oldQueryString).append(" ORDER BY EN_TEXT "); |
|
772 } else if (sortAttribute == CreatedTimestampSortAttribute) { |
|
773 queryString.append(oldQueryString).append( |
|
774 " ORDER BY EN_CREATION_TIME "); |
|
775 } else if (sortAttribute == MostUsedSortAttribute) { |
|
776 queryString.append("SELECT ENTRY_ID FROM (").append(oldQueryString).append( |
|
777 " \ |
|
778 ) LEFT JOIN \ |
|
779 (SELECT LA_ENTRY_ID, COUNT(*) AS USAGE_DATA FROM CA_LAUNCH GROUP BY LA_ENTRY_ID) \ |
|
780 ON ENTRY_ID = LA_ENTRY_ID ORDER BY USAGE_DATA "); |
|
781 } else if (sortAttribute == LastUsedSortAttribute) { |
|
782 queryString.append("SELECT ENTRY_ID FROM (").append(oldQueryString).append( |
|
783 " \ |
|
784 ) LEFT JOIN \ |
|
785 (SELECT LA_ENTRY_ID, MAX(LA_LAUNCH_TIME) AS USAGE_DATA FROM CA_LAUNCH GROUP BY LA_ENTRY_ID) \ |
|
786 ON ENTRY_ID = LA_ENTRY_ID ORDER BY USAGE_DATA "); |
|
787 } else if (parent && sortAttribute == DefaultSortAttribute) { |
|
788 queryString.append(oldQueryString).append( |
|
789 " ORDER BY GE_GROUP_ID, GE_POSITION "); |
|
790 } else if (!parent && sortAttribute == DefaultSortAttribute) { |
|
791 queryString.append(oldQueryString).append(" ORDER BY ENTRY_ID "); |
|
792 } else { |
|
793 queryString.append(oldQueryString); |
|
794 } |
|
795 |
|
796 if (sortAttribute == NameSortAttribute || sortAttribute |
|
797 == CreatedTimestampSortAttribute || sortAttribute |
|
798 == MostUsedSortAttribute || sortAttribute == LastUsedSortAttribute |
|
799 || (sortAttribute == DefaultSortAttribute && parent)) { |
|
800 if (sortOrder == Qt::AscendingOrder) |
|
801 queryString.append(" ASC "); |
|
802 else |
|
803 queryString.append(" DESC "); |
|
804 } |
|
805 |
|
806 } |
|
807 |
|
808 /*! |
|
809 //TODO: |
|
810 */ |
|
811 bool CaClientProxy::setIconInDb(CaEntry *entryClone) const |
|
812 { |
|
813 //set icon information into db |
|
814 QSqlQuery query(dbConnection()); |
|
815 query.prepare( |
|
816 "SELECT ICON_ID FROM CA_ICON WHERE IC_FILENAME = :IC_FILENAME \ |
|
817 AND IC_BITMAP_ID = :IC_BITMAP_ID \ |
|
818 AND IC_MASK_ID = :IC_MASK_ID \ |
|
819 AND IC_SKINMAJOR_ID = :IC_SKINMAJOR_ID \ |
|
820 AND IC_SKINMINOR_ID = :IC_SKINMINOR_ID"); |
|
821 query.bindValue(":IC_FILENAME", |
|
822 entryClone->iconDescription().filename()); |
|
823 query.bindValue(":IC_BITMAP_ID", |
|
824 entryClone->iconDescription().bitmapId()); |
|
825 query.bindValue(":IC_MASK_ID", entryClone->iconDescription().maskId()); |
|
826 query.bindValue(":IC_SKINMAJOR_ID", |
|
827 entryClone->iconDescription().skinMajorId()); |
|
828 query.bindValue(":IC_SKINMINOR_ID", |
|
829 entryClone->iconDescription().skinMinorId()); |
|
830 |
|
831 bool success = query.exec(); |
|
832 if (success && query.next()) { |
|
833 qDebug() << "query.executedQuery() : " << query.executedQuery(); |
|
834 int iconId = query.value(query.record().indexOf("ICON_ID")).toInt(); |
|
835 qDebug() << "iconId = " << iconId; |
|
836 CaIconDescription iconDescription = entryClone->iconDescription(); |
|
837 if (iconId <= 0 && (iconDescription.filename() != "" |
|
838 || iconDescription.bitmapId() != 0 || iconDescription.maskId() != 0 |
|
839 || iconDescription.skinMajorId() != 0 |
|
840 || iconDescription.skinMinorId() != 0)) { |
|
841 query.prepare( |
|
842 "INSERT INTO CA_ICON \ |
|
843 (IC_FILENAME,IC_BITMAP_ID,IC_MASK_ID,IC_SKINMAJOR_ID,IC_SKINMINOR_ID) \ |
|
844 VALUES ( ? , ? , ? , ? , ? )"); |
|
845 query.addBindValue(iconDescription.filename()); |
|
846 query.addBindValue(iconDescription.bitmapId()); |
|
847 query.addBindValue(iconDescription.maskId()); |
|
848 query.addBindValue(iconDescription.skinMajorId()); |
|
849 query.addBindValue(iconDescription.skinMinorId()); |
|
850 success = query.exec(); |
|
851 qDebug() << query.executedQuery(); |
|
852 iconId = query.lastInsertId().toInt(); |
|
853 } |
|
854 CaObjectAdapter::setId(iconDescription, iconId); |
|
855 entryClone->setIconDescription(iconDescription); |
|
856 } |
|
857 return success; |
|
858 } |
|
859 |
|
860 /*! |
|
861 //TODO: |
|
862 */ |
|
863 bool CaClientProxy::setEntryInDb(CaEntry *entryClone) const |
|
864 { |
|
865 QSqlQuery query(dbConnection()); |
|
866 bool isNewEntry(entryClone->id() <= 0); |
|
867 QString |
|
868 queryText( |
|
869 "INSERT INTO CA_ENTRY \ |
|
870 (EN_TEXT,EN_ROLE,EN_TYPE_NAME,EN_FLAGS,EN_ICON_ID ) VALUES ( ?, ?, ?, ?, "); |
|
871 if (!isNewEntry) |
|
872 queryText |
|
873 = "REPLACE INTO CA_ENTRY \ |
|
874 (ENTRY_ID,EN_TEXT,EN_ROLE,EN_TYPE_NAME,EN_FLAGS,EN_ICON_ID ) VALUES ( ?, ?, ?, ?, ?, "; |
|
875 if (entryClone->iconDescription().id() > 0) { |
|
876 queryText.append("?"); |
|
877 } else { |
|
878 queryText.append("NULL"); |
|
879 } |
|
880 queryText.append(")"); |
|
881 query.prepare(queryText); |
|
882 if (!isNewEntry) |
|
883 query.addBindValue(entryClone->id()); |
|
884 query.addBindValue(entryClone->text()); |
|
885 query.addBindValue((int) entryClone->role()); |
|
886 query.addBindValue(entryClone->entryTypeName()); |
|
887 query.addBindValue((int) entryClone->flags()); |
|
888 if (entryClone->iconDescription().id() > 0) { |
|
889 query.addBindValue(entryClone->iconDescription().id()); |
|
890 } |
|
891 |
|
892 bool success = query.exec(); |
|
893 if (success) { |
|
894 qDebug() << query.executedQuery(); |
|
895 int newEntryId(0); |
|
896 //set entry creation time if new entry |
|
897 if (isNewEntry) { |
|
898 newEntryId = query.lastInsertId().toInt(); |
|
899 |
|
900 CaObjectAdapter::setId(*entryClone, newEntryId); |
|
901 uint timestamp = QDateTime::currentDateTime().toTime_t(); |
|
902 query.prepare( |
|
903 "UPDATE CA_ENTRY SET EN_CREATION_TIME = ? WHERE ENTRY_ID = ?"); |
|
904 query.addBindValue(timestamp); |
|
905 query.addBindValue(newEntryId); |
|
906 success = query.exec(); |
|
907 qDebug() << "CaServicePrivate::setEntryInDb" |
|
908 << query.executedQuery(); |
|
909 } |
|
910 } |
|
911 return success; |
|
912 } |
|
913 |
|
914 /*! |
|
915 //TODO: |
|
916 */ |
|
917 bool CaClientProxy::setAttributesInDb(CaEntry *entryClone) const |
|
918 { |
|
919 bool success = true; |
|
920 QSqlQuery query(dbConnection()); |
|
921 if (entryClone->attributes().count() > 0) { |
|
922 QMap<QString, QString> attributesMap = entryClone->attributes(); |
|
923 foreach(QString key, attributesMap.keys()) { |
|
924 query.prepare( |
|
925 "REPLACE INTO CA_ATTRIBUTE (AT_ENTRY_ID,AT_NAME,AT_VALUE) VALUES ( \ |
|
926 :AT_ENTRY_ID,\ |
|
927 :AT_NAME,\ |
|
928 :AT_VALUE )"); |
|
929 query.bindValue(":AT_ENTRY_ID", entryClone->id()); |
|
930 query.bindValue(":AT_NAME", key); |
|
931 query.bindValue(":AT_VALUE", attributesMap.value(key)); |
|
932 success = query.exec(); |
|
933 if (!success) { |
|
934 break; |
|
935 } |
|
936 qDebug() << "CaServicePrivate::setAttributesInDb" |
|
937 << query.boundValues(); |
|
938 } |
|
939 } |
|
940 return success; |
|
941 } |
|
942 |
|
943 |
|
944 void CaClientProxy::CreateGetParentsIdsQuery( |
|
945 const QList<int> &entryIds, |
|
946 const QList<int> &parentIds, |
|
947 QString &query) |
|
948 { |
|
949 QString entryIdList; |
|
950 int lastItemIndex = entryIds.count()-1; |
|
951 for (int i = 0; i < lastItemIndex; i++) { |
|
952 entryIdList.append(QString::number(entryIds[i])); |
|
953 entryIdList.append(","); |
|
954 } |
|
955 if (lastItemIndex >= 0) { |
|
956 entryIdList.append(QString::number(entryIds[lastItemIndex])); |
|
957 } |
|
958 query = QString("SELECT GE_GROUP_ID FROM CA_GROUP_ENTRY " |
|
959 "WHERE GE_ENTRY_ID IN ( %1 )").arg(entryIdList); |
|
960 |
|
961 int lastParentIndex = parentIds.count()-1; |
|
962 if (lastParentIndex >= 0) { |
|
963 QString parentIdList; |
|
964 for (int i = 0; i < lastParentIndex; i++) { |
|
965 parentIdList.append(QString::number(parentIds[i])); |
|
966 parentIdList.append(","); |
|
967 } |
|
968 parentIdList.append(QString::number(parentIds[lastParentIndex])); |
|
969 query.append(QString(" AND GE_GROUP_ID NOT IN( %1 )").arg(parentIdList)); |
|
970 } |
|
971 } |
|
972 |
|
973 |
|
974 bool CaClientProxy::GetParentsIds(const QList<int> &entryIds, |
|
975 QList<int> &parentIds) |
|
976 { |
|
977 QString getParentIdsQuery; |
|
978 CreateGetParentsIdsQuery(entryIds, parentIds, getParentIdsQuery); |
|
979 QSqlQuery query(dbConnection()); |
|
980 bool success = query.exec(getParentIdsQuery); |
|
981 |
|
982 if (success && query.next()) { |
|
983 QList<int> newParentIds; |
|
984 do { |
|
985 newParentIds << query.value(query.record().indexOf("GE_GROUP_ID")).toInt(); |
|
986 } while (query.next()); |
|
987 parentIds.append(newParentIds); |
|
988 GetParentsIds(newParentIds, parentIds); |
|
989 } |
|
990 return success; |
|
991 } |