contentstorage/caclient/stub/src/caclientproxy.cpp
changeset 85 7feec50967db
child 87 9d806967057c
equal deleted inserted replaced
4:1a2a00e78665 85:7feec50967db
       
     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     }
       
   642     else {
       
   643         QString
       
   644             queryString(
       
   645                 "SELECT ENTRY_ID FROM CA_ENTRY \
       
   646         LEFT JOIN CA_GROUP_ENTRY ON GE_ENTRY_ID = ENTRY_ID WHERE GE_GROUP_ID  = ? ");
       
   647         queryString.append(whereStatement);
       
   648         modifyQueryForSortOrder(queryString, query, true);
       
   649         if (query.count() > 0)
       
   650             queryString.append(" LIMIT ").append(query.count());
       
   651         qDebug() << "CaServicePrivate::getEntryIds query text: "
       
   652             << queryString;
       
   653         sqlquery.prepare(queryString);
       
   654         sqlquery.addBindValue(query.parentId());
       
   655         success = sqlquery.exec();
       
   656         if (success) {
       
   657             while (sqlquery.next()) {
       
   658                 sourceIdList << sqlquery.value(sqlquery.record().indexOf(
       
   659                     "ENTRY_ID")).toInt();
       
   660             }
       
   661         }
       
   662     }
       
   663     ErrorCode error = UnknownErrorCode;
       
   664     if (success) {
       
   665             error = NoErrorCode;
       
   666     }
       
   667     return error;
       
   668 }
       
   669 
       
   670 //----------------------------------------------------------------------------
       
   671 //
       
   672 //----------------------------------------------------------------------------
       
   673 ErrorCode CaClientProxy::executeCommand(const CaEntry &entry,
       
   674     const QString &command)
       
   675 {
       
   676     qDebug() << "CaClientProxy::executeCommand" << "entry id: "
       
   677         << entry.id() << "command: " << command;
       
   678     return touch(entry);
       
   679 }
       
   680 
       
   681 //----------------------------------------------------------------------------
       
   682 //
       
   683 //----------------------------------------------------------------------------
       
   684 ErrorCode CaClientProxy::touch(const CaEntry &entry)
       
   685 {
       
   686     const int id = entry.id();
       
   687 
       
   688     qDebug() << "CaClientProxy::touch" << "id: " << id;
       
   689 
       
   690     QSqlDatabase db = dbConnection();
       
   691     QSqlQuery query(db);
       
   692     query.exec("begin");
       
   693 
       
   694     query.prepare(
       
   695         "INSERT INTO CA_LAUNCH (LA_ENTRY_ID,LA_LAUNCH_TIME) VALUES ( ?,? )");
       
   696     query.addBindValue(id);
       
   697     query.addBindValue(QDateTime::currentDateTime().toTime_t());
       
   698     bool success = query.exec();
       
   699 
       
   700     if (success) {
       
   701         query.prepare(
       
   702             "UPDATE CA_ENTRY SET EN_FLAGS = EN_FLAGS | ? WHERE ENTRY_ID = ?");
       
   703         query.addBindValue((int) UsedEntryFlag);
       
   704         query.addBindValue(id);
       
   705         success = query.exec();
       
   706     }
       
   707 
       
   708     ErrorCode error = NoErrorCode;
       
   709     if (success) {
       
   710         query.exec("commit");
       
   711         QList<CaEntry *> entryList;
       
   712         if (getData(QList<int>() << id, entryList) == NoErrorCode) {
       
   713             QList<int> parentIds;
       
   714             GetParentsIds(QList<int>() << id, parentIds);
       
   715             CaNotifiers::Notify(*entryList[0], UpdateChangeType, parentIds);
       
   716         }
       
   717     } else {
       
   718         query.exec("rollback");
       
   719         error = UnknownErrorCode;
       
   720     }
       
   721     
       
   722     return error;
       
   723 }
       
   724 
       
   725 //----------------------------------------------------------------------------
       
   726 //
       
   727 //----------------------------------------------------------------------------
       
   728 ErrorCode CaClientProxy::customSort(const QList<int> &entryIdList,
       
   729         int groupId)
       
   730 {
       
   731     bool success = true;
       
   732     QSqlDatabase db = dbConnection();
       
   733     QSqlQuery query(db);
       
   734     query.exec("begin");
       
   735     for (int i = 0; i < entryIdList.count(); i++) {
       
   736         int position = i+1;
       
   737         query.prepare(
       
   738             "UPDATE CA_GROUP_ENTRY SET GE_POSITION = ? WHERE GE_ENTRY_ID = ? AND GE_GROUP_ID = ?" );
       
   739         query.addBindValue(position);
       
   740         query.addBindValue(entryIdList.at(i));
       
   741         query.addBindValue(groupId);
       
   742         success = query.exec();
       
   743         if (!success) {
       
   744             break;
       
   745         }
       
   746     }
       
   747 
       
   748     ErrorCode error(NoErrorCode);
       
   749     if (success) {
       
   750         query.exec("commit");
       
   751         CaNotifiers::Notify(groupId);
       
   752     } else {
       
   753         query.exec("rollback");
       
   754         error = UnknownErrorCode;
       
   755     }
       
   756     return error;
       
   757 }
       
   758 
       
   759 /*!
       
   760  //TODO:
       
   761  */
       
   762 void CaClientProxy::modifyQueryForSortOrder(QString& queryString,
       
   763     const CaQuery &query, bool parent) const
       
   764 {
       
   765     SortAttribute sortAttribute;
       
   766     Qt::SortOrder sortOrder;
       
   767     query.getSort(sortAttribute, sortOrder);
       
   768     QString oldQueryString(queryString);
       
   769     queryString.clear();
       
   770 
       
   771     if (sortAttribute == NameSortAttribute) {
       
   772         queryString.append(oldQueryString).append(" ORDER BY EN_TEXT ");
       
   773     }
       
   774     else if (sortAttribute == CreatedTimestampSortAttribute) {
       
   775         queryString.append(oldQueryString).append(
       
   776             " ORDER BY EN_CREATION_TIME ");
       
   777     }
       
   778     else if (sortAttribute == MostUsedSortAttribute) {
       
   779         queryString.append("SELECT ENTRY_ID FROM (").append(oldQueryString).append(
       
   780             " \
       
   781                 ) LEFT JOIN \
       
   782                 (SELECT LA_ENTRY_ID, COUNT(*) AS USAGE_DATA FROM CA_LAUNCH GROUP BY LA_ENTRY_ID) \
       
   783                 ON ENTRY_ID = LA_ENTRY_ID ORDER BY USAGE_DATA ");
       
   784     }
       
   785     else if (sortAttribute == LastUsedSortAttribute) {
       
   786         queryString.append("SELECT ENTRY_ID FROM (").append(oldQueryString).append(
       
   787             " \
       
   788                 ) LEFT JOIN \
       
   789                 (SELECT LA_ENTRY_ID, MAX(LA_LAUNCH_TIME) AS USAGE_DATA FROM CA_LAUNCH GROUP BY LA_ENTRY_ID) \
       
   790                 ON ENTRY_ID = LA_ENTRY_ID ORDER BY USAGE_DATA ");
       
   791     }
       
   792     else if (parent && sortAttribute == DefaultSortAttribute) {
       
   793         queryString.append(oldQueryString).append(
       
   794             " ORDER BY GE_GROUP_ID, GE_POSITION ");
       
   795     }
       
   796     else if (!parent && sortAttribute == DefaultSortAttribute) {
       
   797         queryString.append(oldQueryString).append(" ORDER BY ENTRY_ID ");
       
   798     }
       
   799     else {
       
   800         queryString.append(oldQueryString);
       
   801     }
       
   802 
       
   803     if (sortAttribute == NameSortAttribute || sortAttribute
       
   804         == CreatedTimestampSortAttribute || sortAttribute
       
   805         == MostUsedSortAttribute || sortAttribute == LastUsedSortAttribute
       
   806         || (sortAttribute == DefaultSortAttribute && parent)) {
       
   807         if (sortOrder == Qt::AscendingOrder)
       
   808             queryString.append(" ASC ");
       
   809         else
       
   810             queryString.append(" DESC ");
       
   811     }
       
   812 
       
   813 }
       
   814 
       
   815 /*!
       
   816  //TODO:
       
   817  */
       
   818 bool CaClientProxy::setIconInDb(CaEntry *entryClone) const
       
   819 {
       
   820     //set icon information into db
       
   821     QSqlQuery query(dbConnection());
       
   822     query.prepare(
       
   823         "SELECT ICON_ID FROM CA_ICON WHERE IC_FILENAME = :IC_FILENAME \
       
   824             AND IC_BITMAP_ID = :IC_BITMAP_ID \
       
   825             AND IC_MASK_ID = :IC_MASK_ID \
       
   826             AND IC_SKINMAJOR_ID = :IC_SKINMAJOR_ID \
       
   827             AND IC_SKINMINOR_ID = :IC_SKINMINOR_ID");
       
   828     query.bindValue(":IC_FILENAME",
       
   829         entryClone->iconDescription().filename());
       
   830     query.bindValue(":IC_BITMAP_ID",
       
   831         entryClone->iconDescription().bitmapId());
       
   832     query.bindValue(":IC_MASK_ID", entryClone->iconDescription().maskId());
       
   833     query.bindValue(":IC_SKINMAJOR_ID",
       
   834         entryClone->iconDescription().skinMajorId());
       
   835     query.bindValue(":IC_SKINMINOR_ID",
       
   836         entryClone->iconDescription().skinMinorId());
       
   837 
       
   838     bool success = query.exec();
       
   839     if (success && query.next()) {
       
   840         qDebug() << "query.executedQuery() : " << query.executedQuery();
       
   841         int iconId = query.value(query.record().indexOf("ICON_ID")).toInt();
       
   842         qDebug() << "iconId = " << iconId;
       
   843         CaIconDescription iconDescription = entryClone->iconDescription();
       
   844         if (iconId <= 0 && (iconDescription.filename() != ""
       
   845             || iconDescription.bitmapId() != 0 || iconDescription.maskId() != 0
       
   846             || iconDescription.skinMajorId() != 0
       
   847             || iconDescription.skinMinorId() != 0)) {
       
   848             query.prepare(
       
   849                 "INSERT INTO CA_ICON \
       
   850                            (IC_FILENAME,IC_BITMAP_ID,IC_MASK_ID,IC_SKINMAJOR_ID,IC_SKINMINOR_ID) \
       
   851                             VALUES ( ? , ? , ? , ? , ? )");
       
   852             query.addBindValue(iconDescription.filename());
       
   853             query.addBindValue(iconDescription.bitmapId());
       
   854             query.addBindValue(iconDescription.maskId());
       
   855             query.addBindValue(iconDescription.skinMajorId());
       
   856             query.addBindValue(iconDescription.skinMinorId());
       
   857             success = query.exec();
       
   858             qDebug() << query.executedQuery();
       
   859             iconId = query.lastInsertId().toInt();
       
   860         }
       
   861         CaObjectAdapter::setId(iconDescription, iconId);
       
   862         entryClone->setIconDescription(iconDescription);
       
   863     }
       
   864     return success;
       
   865 }
       
   866 
       
   867 /*!
       
   868  //TODO:
       
   869  */
       
   870 bool CaClientProxy::setEntryInDb(CaEntry *entryClone) const
       
   871 {
       
   872     QSqlQuery query(dbConnection());
       
   873     bool isNewEntry(entryClone->id() <= 0);
       
   874     QString
       
   875         queryText(
       
   876             "INSERT INTO CA_ENTRY \
       
   877         (EN_TEXT,EN_ROLE,EN_TYPE_NAME,EN_FLAGS,EN_ICON_ID ) VALUES ( ?, ?, ?, ?, ");
       
   878     if (!isNewEntry)
       
   879         queryText
       
   880             = "REPLACE INTO CA_ENTRY \
       
   881         (ENTRY_ID,EN_TEXT,EN_ROLE,EN_TYPE_NAME,EN_FLAGS,EN_ICON_ID ) VALUES ( ?, ?, ?, ?, ?, ";
       
   882     if (entryClone->iconDescription().id() > 0) {
       
   883         queryText.append("?");
       
   884     }
       
   885     else {
       
   886         queryText.append("NULL");
       
   887     }
       
   888     queryText.append(")");
       
   889     query.prepare(queryText);
       
   890     if (!isNewEntry)
       
   891         query.addBindValue(entryClone->id());
       
   892     query.addBindValue(entryClone->text());
       
   893     query.addBindValue((int) entryClone->role());
       
   894     query.addBindValue(entryClone->entryTypeName());
       
   895     query.addBindValue((int) entryClone->flags());
       
   896     if (entryClone->iconDescription().id() > 0) {
       
   897         query.addBindValue(entryClone->iconDescription().id());
       
   898     }
       
   899 
       
   900     bool success = query.exec();
       
   901     if (success) {
       
   902         qDebug() << query.executedQuery();
       
   903         int newEntryId(0);
       
   904         //set entry creation time if new entry
       
   905         if (isNewEntry) {
       
   906             newEntryId = query.lastInsertId().toInt();
       
   907 
       
   908             CaObjectAdapter::setId(*entryClone, newEntryId);
       
   909             uint timestamp = QDateTime::currentDateTime().toTime_t();
       
   910             query.prepare(
       
   911                 "UPDATE CA_ENTRY SET EN_CREATION_TIME = ? WHERE ENTRY_ID = ?");
       
   912             query.addBindValue(timestamp);
       
   913             query.addBindValue(newEntryId);
       
   914             success = query.exec();
       
   915             qDebug() << "CaServicePrivate::setEntryInDb"
       
   916                 << query.executedQuery();
       
   917         }
       
   918     }
       
   919     return success;
       
   920 }
       
   921 
       
   922 /*!
       
   923  //TODO:
       
   924  */
       
   925 bool CaClientProxy::setAttributesInDb(CaEntry *entryClone) const
       
   926 {
       
   927     bool success = true;
       
   928     QSqlQuery query(dbConnection());
       
   929     if (entryClone->attributes().count() > 0) {
       
   930         QMap<QString, QString> attributesMap = entryClone->attributes();
       
   931         foreach (QString key, attributesMap.keys()) {
       
   932                 query.prepare(
       
   933                     "REPLACE INTO CA_ATTRIBUTE (AT_ENTRY_ID,AT_NAME,AT_VALUE) VALUES ( \
       
   934                 :AT_ENTRY_ID,\
       
   935                 :AT_NAME,\
       
   936                 :AT_VALUE )");
       
   937                 query.bindValue(":AT_ENTRY_ID", entryClone->id());
       
   938                 query.bindValue(":AT_NAME", key);
       
   939                 query.bindValue(":AT_VALUE", attributesMap.value(key));
       
   940                 success = query.exec();
       
   941                 if (!success) {
       
   942                     break;
       
   943                 }
       
   944                 qDebug() << "CaServicePrivate::setAttributesInDb"
       
   945                     << query.boundValues();
       
   946             }
       
   947     }
       
   948      return success;
       
   949 }
       
   950 
       
   951 
       
   952 void CaClientProxy::CreateGetParentsIdsQuery(
       
   953     const QList<int> &entryIds,
       
   954     const QList<int> &parentIds,
       
   955     QString &query)
       
   956 {
       
   957     QString entryIdList;
       
   958     int lastItemIndex = entryIds.count()-1;
       
   959     for (int i = 0; i < lastItemIndex; i++) {
       
   960         entryIdList.append(QString::number(entryIds[i]));
       
   961         entryIdList.append(",");
       
   962     }
       
   963     if (lastItemIndex >= 0) {
       
   964         entryIdList.append(QString::number(entryIds[lastItemIndex]));
       
   965     }
       
   966     query = QString("SELECT GE_GROUP_ID FROM CA_GROUP_ENTRY "
       
   967         "WHERE GE_ENTRY_ID IN ( %1 )").arg(entryIdList);
       
   968 
       
   969     int lastParentIndex = parentIds.count()-1;
       
   970     if (lastParentIndex >= 0) {
       
   971         QString parentIdList;
       
   972         for (int i = 0; i < lastParentIndex; i++) {
       
   973             parentIdList.append(QString::number(parentIds[i]));
       
   974             parentIdList.append(",");
       
   975         }
       
   976         parentIdList.append(QString::number(parentIds[lastParentIndex]));
       
   977         query.append(QString(" AND GE_GROUP_ID NOT IN( %1 )").arg(parentIdList));
       
   978     }
       
   979 }
       
   980 
       
   981 
       
   982 bool CaClientProxy::GetParentsIds( const QList<int> &entryIds,
       
   983     QList<int> &parentIds)
       
   984 {
       
   985     QString getParentIdsQuery;
       
   986     CreateGetParentsIdsQuery(entryIds, parentIds, getParentIdsQuery);
       
   987     QSqlQuery query(dbConnection());
       
   988     bool success = query.exec(getParentIdsQuery);
       
   989 
       
   990     if (success && query.next()) {
       
   991         QList<int> newParentIds;
       
   992         do {
       
   993             newParentIds << query.value(query.record().indexOf("GE_GROUP_ID")).toInt();
       
   994         } while (query.next());
       
   995         parentIds.append(newParentIds);
       
   996         GetParentsIds(newParentIds, parentIds);
       
   997     }
       
   998     return success;
       
   999 }