src/hbservers/hbthemeserver/hbthemeserver_generic.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbServers module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbthemeserver_generic_p_p.h"
       
    27 
       
    28 #include <QLabel>
       
    29 #include <QLocalServer>
       
    30 #include <QLocalSocket>
       
    31 #include <QSharedMemory>
       
    32 #include <QSettings>
       
    33 #include <QDebug>
       
    34 #include <QList>
       
    35 #include <QIcon>
       
    36 #include <QFile>
       
    37 #include <QCoreApplication>
       
    38 #include <QPainter>
       
    39 #include <QStringList>
       
    40 #include <QPixmap>
       
    41 
       
    42 #include <hbinstance.h>
       
    43 
       
    44 #include "hbthemecommon_p.h"
       
    45 #include "hbmemoryutils_p.h"
       
    46 #include "hbthemeserverutils_p.h"
       
    47 #include "hbiconcacheitemcreator_p.h"
       
    48 #include "hbiconloader_p.h"
       
    49 #include "hbcache_p.h"
       
    50 #include "hbdeviceprofiledatabase_p.h"
       
    51 #include "hbpixmapiconimpl_p.h"
       
    52 #include "hbpixmapiconprocessor_p.h"
       
    53 #include "hblayeredstyleloader_p.h"
       
    54 
       
    55 /*!
       
    56   @hbserver
       
    57   \class HbThemeServerPrivate
       
    58   \brief HbThemeServerPrivate implements the theme server
       
    59 */
       
    60 
       
    61 #ifdef THEME_SERVER_TRACES
       
    62 extern QLabel *testLabel;
       
    63 #endif
       
    64 
       
    65 // 5 MB GPU & CPU cache size
       
    66 #define GPU_CACHE_SIZE 0x500000
       
    67 #define CPU_CACHE_SIZE 0x500000
       
    68 
       
    69 /*!
       
    70   \fn HbThemeServerPrivate::HbThemeServerPrivate()
       
    71   Constructor
       
    72   \a parent
       
    73 */
       
    74 #ifdef QT_DEBUG
       
    75 HbThemeServerPrivate::HbThemeServerPrivate(QWidget *parent): QMainWindow(parent), server(new QLocalServer(this))
       
    76 #else
       
    77 HbThemeServerPrivate::HbThemeServerPrivate(): server(new QLocalServer(this))
       
    78 #endif
       
    79 {
       
    80     iThemeSelectionClient = 0;
       
    81     sessionList.clear();
       
    82 #ifdef QT_DEBUG
       
    83     setWindowTitle("Theme Server");
       
    84 #endif
       
    85     // Using QScopedPointer so that it deallocates memory
       
    86     // when std::badalloc exception occurs.
       
    87     QScopedPointer <HbIconDataCache> tempIconCache(new HbIconDataCache());
       
    88     QScopedPointer <HbCache> tempCssCache(new HbCache());
       
    89     iconCache = tempIconCache.take();
       
    90     cssCache = tempCssCache.take();
       
    91     setMaxGpuCacheSize(GPU_CACHE_SIZE);
       
    92     setMaxCpuCacheSize(CPU_CACHE_SIZE);
       
    93 }
       
    94 
       
    95 /*!
       
    96   \fn HbThemeServerPrivate::~HbThemeServerPrivate()
       
    97   Destructor
       
    98 */
       
    99 HbThemeServerPrivate::~HbThemeServerPrivate()
       
   100 {
       
   101     server->close();
       
   102     delete server; // Order of Deletion needs to be maintain ,as the QLocalServer should delete first before deleting Server data so All sessions will be cleanup first.
       
   103     delete iconCache;
       
   104     delete cssCache;
       
   105 }
       
   106 
       
   107 /*!
       
   108   \fn HbThemeServerPrivate::start()
       
   109   start the themeserver
       
   110 */
       
   111 bool HbThemeServerPrivate::start()
       
   112 {
       
   113     bool success = false;
       
   114     if (!server->listen(THEME_SERVER_NAME)) {
       
   115         qWarning("Unable to start the server: %s.", server->errorString().toLatin1().data());
       
   116 #if defined(Q_OS_LINUX) || defined(Q_OS_MAC)
       
   117         if (server->serverError() == QAbstractSocket::AddressInUseError) {
       
   118             qDebug() << "Trying to delete temporary file hbthemeserver";
       
   119             QFile::remove("/tmp/hbthemeserver");
       
   120             success = start();
       
   121         }
       
   122 #endif
       
   123         return success;
       
   124     }
       
   125 #ifdef QT_DEBUG
       
   126     statusLabel.setText("Theme Server Started");
       
   127     setCentralWidget(&statusLabel);
       
   128 #endif
       
   129     connect(server, SIGNAL(newConnection()), this, SLOT(newClientConnected()));
       
   130     success = true;
       
   131     return success;
       
   132 }
       
   133 
       
   134 /*!
       
   135   \fn HbThemeServerPrivate::insertIconCacheItem()
       
   136   Insert item into the icon cache
       
   137   \a key denotes the unique identifier for the cache item that is to be inserted into the cache.
       
   138   \a item denotes the cache item that is to be inserted
       
   139 */
       
   140 bool HbThemeServerPrivate::insertIconCacheItem(const HbIconKey &key,  HbIconCacheItem *item)
       
   141 {
       
   142     return (iconCache->insert(key, item));
       
   143 }
       
   144 
       
   145 /*!
       
   146   \fn HbThemeServerPrivate::insertCssCacheItem()
       
   147   Insert item into the css cache
       
   148   \a key denotes the unique identifier for the cache item that is to be inserted into the cache.
       
   149   \a item denotes the cache item that is to be inserted
       
   150 */
       
   151 bool HbThemeServerPrivate::insertCssCacheItem(const QString &key,  HbCacheItem *item)
       
   152 {
       
   153     return (cssCache->insert(key, item));
       
   154 }
       
   155 
       
   156 /*!
       
   157   \fn HbThemeServerPrivate::iconCacheItem()
       
   158   Find an item in the icon cache
       
   159   \a key denotes the unique identifier for the cache item that is to be found in the cache.
       
   160 */
       
   161 HbIconCacheItem *HbThemeServerPrivate::iconCacheItem(const HbIconKey &key)
       
   162 {
       
   163     return(iconCache->getCacheItem(key, false));
       
   164 }
       
   165 
       
   166 /*!
       
   167   \fn HbThemeServerPrivate::cssCacheItem()
       
   168   Find an item in the css cache
       
   169   \a key denotes the unique identifier for the cache item that is to be found in the cache.
       
   170 */
       
   171 HbCacheItem *HbThemeServerPrivate::cssCacheItem(const QString &key)
       
   172 {
       
   173     return(cssCache->cacheItem(key));
       
   174 }
       
   175 
       
   176 /*!
       
   177   \fn HbThemeServerPrivate::setMaxGpuCacheSize()
       
   178   Provides a mechanism for setting the Gpu cache limit
       
   179   \a size denotes the cache limit in bytes e.g. size = 0x500000
       
   180 */
       
   181 void HbThemeServerPrivate::setMaxGpuCacheSize(int size)
       
   182 {
       
   183     iconCache->setMaxGpuCacheSize(size);
       
   184 }
       
   185 
       
   186 /*!
       
   187   \fn HbThemeServerPrivate::setMaxCpuCacheSize()
       
   188   Provides a mechanism for setting the Cpu cache limit
       
   189   \a size denotes the cache limit in bytes e.g. size = 0x500000
       
   190 */
       
   191 void HbThemeServerPrivate::setMaxCpuCacheSize(int size)
       
   192 {
       
   193     iconCache->setMaxCpuCacheSize(size);
       
   194 }
       
   195 
       
   196 /*!
       
   197   \fn HbThemeServerPrivate::removeIconCacheItem()
       
   198   Remove an item from iconcache corresponding to key.
       
   199   \a key denotes the unique identifier for the cache item that is to be removed from the cache.
       
   200 */
       
   201 void HbThemeServerPrivate::removeIconCacheItem(const HbIconKey &key)
       
   202 {
       
   203     iconCache->remove(key);
       
   204 }
       
   205 
       
   206 /*!
       
   207   \fn HbThemeServerPrivate::removeCssCacheItem()
       
   208   Remove item from css cache corresponding to key.
       
   209   \a key denotes the unique identifier for the cache item that is to be removed from the cache.
       
   210 */
       
   211 void HbThemeServerPrivate::removeCssCacheItem(const QString &key)
       
   212 {
       
   213     cssCache->remove(key);
       
   214 }
       
   215 
       
   216 /*!
       
   217   \fn HbThemeServerPrivate::clearIconCache()
       
   218   Clears the icon cache
       
   219 */
       
   220 void HbThemeServerPrivate::clearIconCache()
       
   221 {
       
   222     iconCache->clear();
       
   223 }
       
   224 
       
   225 /*!
       
   226   \fn HbThemeServerPrivate::clearCssCache()
       
   227   Clears the css cache
       
   228 */
       
   229 void HbThemeServerPrivate::clearCssCache()
       
   230 {
       
   231     cssCache->clear();
       
   232 }
       
   233 
       
   234 
       
   235 /*!
       
   236   data.type = INVALID_FORMAT;
       
   237   \fn HbThemeServerPrivate::handleThemeSelection()
       
   238   Handle change of theme
       
   239   \a newTheme
       
   240 */
       
   241 void HbThemeServerPrivate::handleThemeSelection(const QString &newTheme)
       
   242 {
       
   243 #ifdef THEME_SERVER_TRACES
       
   244     qDebug() << Q_FUNC_INFO << "  theme=" << newTheme;
       
   245 #endif
       
   246     // Modify the QSettings to store the applied theme
       
   247     QSettings settings(QLatin1String(ORGANIZATION), QLatin1String(THEME_COMPONENT));
       
   248     QString prevTheme = settings.value("currenttheme").toString();
       
   249     if (prevTheme == newTheme) {
       
   250         // Theme did not change, return.
       
   251         return;
       
   252     }
       
   253     QString cleanThemeName = newTheme.trimmed();
       
   254     settings.setValue("currenttheme", cleanThemeName);
       
   255     settings.sync();
       
   256     HbThemeServerRequest requestType;
       
   257     requestType = EThemeSelection;
       
   258     QByteArray block;
       
   259     QDataStream out(&block, QIODevice::WriteOnly);
       
   260     out << (int)requestType;
       
   261     out << cleanThemeName;
       
   262     writeToClients(block);
       
   263 }
       
   264 
       
   265 /*!
       
   266   \fn HbThemeServerPrivate::writeToClients()
       
   267   Write to all clients
       
   268   \a block
       
   269 */
       
   270 void HbThemeServerPrivate::writeToClients(QByteArray &block)
       
   271 {
       
   272     HbThemeServerSession *session;
       
   273 #ifdef THEME_SERVER_TRACES
       
   274     qDebug() << Q_FUNC_INFO << "socketlist count: " << sessionList.count();
       
   275 #endif
       
   276     foreach(session, sessionList) {
       
   277         QLocalSocket * curSocket = session->clientConnection();
       
   278         if (iThemeSelectionClient != curSocket) {
       
   279             curSocket->write(block);
       
   280         }
       
   281     }
       
   282 }
       
   283 
       
   284 /*!
       
   285   \fn HbThemeServerPrivate::setThemeSelectionClient()
       
   286   Set the theme selection client
       
   287   \a socket
       
   288 */
       
   289 void HbThemeServerPrivate::setThemeSelectionClient(QLocalSocket *socket)
       
   290 {
       
   291     iThemeSelectionClient = socket;
       
   292 }
       
   293 
       
   294 /*!
       
   295   \fn HbThemeServerPrivate::newClientConnected()
       
   296   Creates a new session with the server.
       
   297 */
       
   298 void HbThemeServerPrivate::newClientConnected()
       
   299 {
       
   300     QLocalSocket * newClient = server->nextPendingConnection();
       
   301     if (newClient) {
       
   302         HbThemeServerSession * newSession =
       
   303             new HbThemeServerSession(newClient, this);
       
   304         // Store list of client connected to server
       
   305         sessionList.append(newSession);
       
   306 #ifdef THEME_SERVER_TRACES
       
   307         qDebug() << "Total No of Connection after addition = " << sessionList.count();
       
   308 #endif
       
   309         connect(newClient, SIGNAL(disconnected()), this, SLOT(removeFromList()));
       
   310     }
       
   311 }
       
   312 
       
   313 /*!
       
   314   \fn HbThemeServerPrivate::removeFromList()
       
   315   Remove a session from list
       
   316 */
       
   317 void HbThemeServerPrivate::removeFromList()
       
   318 {
       
   319     QList<HbThemeServerSession*>::const_iterator itEnd(sessionList.constEnd());
       
   320 
       
   321     for (QList<HbThemeServerSession*>::const_iterator iter = sessionList.constBegin();
       
   322             iter != itEnd;
       
   323             ++iter) {
       
   324         if ((*iter)->clientConnection() == (QLocalSocket *)sender()) {
       
   325             sessionList.removeOne((*iter));
       
   326             delete(*iter);
       
   327             break;
       
   328         }
       
   329     }
       
   330     qDebug() << "Total No of Connection after deletion = " << sessionList.count();
       
   331 }
       
   332 
       
   333 //Debug Code for Test Purpose
       
   334 #ifdef HB_ICON_CACHE_DEBUG
       
   335 int HbThemeServerPrivate ::cacheIconCount() const
       
   336 {
       
   337     return iconCache->count();
       
   338 }
       
   339 
       
   340 int HbThemeServerPrivate::freeVectorMemory()
       
   341 {
       
   342     return iconCache->freeVectorMemory();
       
   343 }
       
   344 
       
   345 int HbThemeServerPrivate::freeRasterMemory()
       
   346 {
       
   347     return iconCache->freeRasterMemory();
       
   348 }
       
   349 
       
   350 int HbThemeServerPrivate::lastAddedRefCount()
       
   351 {
       
   352     return iconCache->lastAddedRefCount();
       
   353 }
       
   354 
       
   355 int HbThemeServerPrivate::lastAddedItemMem()
       
   356 {
       
   357     return iconCache->lastAddedItemMem();
       
   358 }
       
   359 
       
   360 int HbThemeServerPrivate::lastRemovedItemMem()
       
   361 {
       
   362     return iconCache->lastRemovedItemMem();
       
   363 }
       
   364 
       
   365 int HbThemeServerPrivate::lastRemovedItemRfCount()
       
   366 {
       
   367     return iconCache->lastRemovedItemRfCount();
       
   368 }
       
   369 
       
   370 bool HbThemeServerPrivate::enableCache(bool cacheIt)
       
   371 {
       
   372     return iconCache->enableCache(cacheIt);
       
   373 }
       
   374 
       
   375 int HbThemeServerPrivate::cacheHitCount()
       
   376 {
       
   377     return iconCache->cacheHitCount();
       
   378 }
       
   379 
       
   380 int HbThemeServerPrivate::cacheMissCount()
       
   381 {
       
   382     return iconCache->cacheMissCount();
       
   383 }
       
   384 
       
   385 int HbThemeServerPrivate::serverHeapSize()
       
   386 {
       
   387     return 0;
       
   388 }
       
   389 
       
   390 void HbThemeServerPrivate::cleanVectorLRUList()
       
   391 {
       
   392     iconCache->cleanVectorLRUList();
       
   393 }
       
   394 
       
   395 void HbThemeServerPrivate::cleanRasterLRUList()
       
   396 {
       
   397     iconCache->cleanRasterLRUList();
       
   398 }
       
   399 
       
   400 int HbThemeServerPrivate::rasterLruCount()
       
   401 {
       
   402     return iconCache->rasterLruCount();
       
   403 }
       
   404 
       
   405 int HbThemeServerPrivate::vectorLruCount()
       
   406 {
       
   407     return iconCache->vectorLruCount();
       
   408 }
       
   409 #endif
       
   410 
       
   411 //***********************************
       
   412 //HbThemeServerSession - implementations
       
   413 //***********************************
       
   414 /*!
       
   415   @hbserver
       
   416   \class HbThemeServerSession
       
   417   \brief HbThemeServerSession implements the sessions associated with the server.
       
   418   The Theme server maintains a list of sessions, each session corresponding to a client.
       
   419   Each session in turn is responsible for keeping track of client specific resources.
       
   420   In case of Icon caching, a session maintains a list of Icon keys.
       
   421   A  key is added to the list only if the corresponding cache item has been successfully inserted into cache
       
   422   or if a previously cached icon is requested and successfully found in the cache.
       
   423   In case of client crash or graceful exit of client, the server goes through its list of sessions,
       
   424   finds the session corresponding to the client, removes the session from its session list and deletes the session.
       
   425   In the session destructor, the session specific resources are cleaned up.
       
   426   In case of icon caching, the list of icon keys is traversed and a remove operation is performed on the cache
       
   427   items corresponding to these keys. On removal, the items reference count is decremented and in case the
       
   428   reference count becomes 0, the cache item instance gets added to the back of the LRU list.
       
   429 
       
   430 */
       
   431 
       
   432 /*!
       
   433   \fn HbThemeServerSession::HbThemeServerSession()
       
   434   Constructor
       
   435   \a aClientConnection indicates the local socket that is contained within this session
       
   436   \a aServer denotes the handle to the theme server
       
   437 */
       
   438 HbThemeServerSession::HbThemeServerSession(QLocalSocket *aClientConnection, HbThemeServerPrivate *aServer)
       
   439 {
       
   440     iServer = aServer;
       
   441     iClientConnection = aClientConnection;
       
   442     sessionIconData.clear();
       
   443     sessionCssData.clear();
       
   444     connect(iClientConnection, SIGNAL(readyRead()), this, SLOT(readDataFromClient()));
       
   445     connect(iClientConnection, SIGNAL(disconnected()), iClientConnection, SLOT(deleteLater()));
       
   446 }
       
   447 
       
   448 /*!
       
   449   \fn HbThemeServerSession::~HbThemeServerSession()
       
   450   Destructor
       
   451 */
       
   452 HbThemeServerSession::~HbThemeServerSession()
       
   453 {
       
   454     //Remove icon related-session-specific data
       
   455     QList<HbIconKey>::const_iterator itEnd(sessionIconData.constEnd());
       
   456     for (QList<HbIconKey>::const_iterator iter = sessionIconData.constBegin();
       
   457             iter != itEnd;
       
   458             ++iter) {
       
   459         iServer->removeIconCacheItem(*iter);
       
   460     }
       
   461 
       
   462     //Remove css related-session-specific data
       
   463     QList<QString>::const_iterator iterEnd(sessionCssData.constEnd());
       
   464     for (QList<QString>::const_iterator iter = sessionCssData.constBegin();
       
   465             iter != iterEnd;
       
   466             ++iter) {
       
   467         iServer->removeCssCacheItem(*iter);
       
   468     }
       
   469     sessionIconData.clear();
       
   470     sessionCssData.clear();
       
   471 }
       
   472 
       
   473 /*!
       
   474   \fn HbThemeServerSession::clientConnection()
       
   475   Returns a handle to the local socket contained by the session
       
   476 */
       
   477 QLocalSocket * HbThemeServerSession::clientConnection()
       
   478 {
       
   479     return iClientConnection;
       
   480 }
       
   481 
       
   482 /*!
       
   483   \fn HbThemeServerSession::readDataFromClient()
       
   484   Slot that is reponsible for reading data from the client
       
   485   Is responsible for performing operations such as creating cache items and storing into cache
       
   486   and writing back data to the client
       
   487 */
       
   488 void HbThemeServerSession::readDataFromClient()
       
   489 {
       
   490 #ifdef THEME_SERVER_TRACES
       
   491     qDebug() << Q_FUNC_INFO;
       
   492 #endif
       
   493     HbSharedIconInfo data;
       
   494     data.type = INVALID_FORMAT;
       
   495     HbThemeServerRequest requestType;
       
   496     QByteArray inputByteArray = ((QLocalSocket *)sender())->readAll();
       
   497     if (inputByteArray.size() > 0) {
       
   498         QDataStream inputDataStream(inputByteArray);
       
   499         int clue;
       
   500         inputDataStream >> clue;
       
   501         requestType = (HbThemeServerRequest)clue;
       
   502 
       
   503 #ifdef THEME_SERVER_TRACES
       
   504         qDebug() << Q_FUNC_INFO << "recognizer: " << requestType;
       
   505 #endif
       
   506 
       
   507 //Debug Code for Test Purpose
       
   508 #ifdef HB_ICON_CACHE_DEBUG
       
   509         QByteArray outputByteArray;
       
   510         QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly);
       
   511         outputDataStream << (int)requestType;
       
   512 #endif
       
   513         switch (requestType) {
       
   514         case EStyleSheetLookup: {
       
   515             QString fileName;
       
   516             int priority;
       
   517             HbLayeredStyleLoader::LayerPriority layerdPriority;
       
   518             inputDataStream >> fileName;
       
   519             inputDataStream >> priority;
       
   520             layerdPriority = (HbLayeredStyleLoader::LayerPriority) priority;
       
   521             QByteArray output = handleStyleSheetLookup((int) requestType, fileName, layerdPriority);
       
   522             ((QLocalSocket *)sender())->write(output);
       
   523             break;
       
   524         }
       
   525         case EWidgetMLLookup: {
       
   526             QString filename;
       
   527             QString section;
       
   528             QString layout;
       
   529 
       
   530             inputDataStream >> filename;
       
   531             inputDataStream >> layout;
       
   532             inputDataStream >> section;
       
   533             // handle the shared Widgetml look up.
       
   534             QByteArray output = iServer->handleSharedWidgetMLLookup(filename, layout, section);
       
   535             ((QLocalSocket *)sender())->write(output);
       
   536             break;
       
   537         }
       
   538         case EDeviceProfileOffset: {
       
   539             int offset = -1;
       
   540             HbDeviceProfileDatabase *deviceProfileDatabase =
       
   541                 HbDeviceProfileDatabase::instance(HbMemoryManager::SharedMemory);
       
   542 
       
   543             if (deviceProfileDatabase) {
       
   544                 offset = deviceProfileDatabase->deviceProfilesOffset();
       
   545             }
       
   546             // offset will be -1 if the deviceProfileDatabase is NULL.
       
   547             QByteArray outputByteArray;
       
   548             QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly);
       
   549             outputDataStream << requestType;
       
   550             outputDataStream << offset;
       
   551             ((QLocalSocket *)sender())->write(outputByteArray);
       
   552             break;
       
   553         }
       
   554         case ESecondaryCacheOffset: {
       
   555             int offset = HbThemeServerUtils::sharedCacheOffset();
       
   556             QByteArray outputByteArray;
       
   557             QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly);
       
   558             outputDataStream << requestType;
       
   559             outputDataStream << offset;
       
   560             ((QLocalSocket *)sender())->write(outputByteArray);
       
   561             break;
       
   562         }
       
   563         case EEffectLookupFilePath:
       
   564         case EEffectAdd: {
       
   565             QString fileName;
       
   566             inputDataStream >> fileName;
       
   567             QByteArray output = iServer->handleSharedEffectAddAndFileLookup((int) requestType, fileName);
       
   568             ((QLocalSocket *)sender())->write(output);
       
   569             break;
       
   570         }
       
   571         case EThemeSelection: {
       
   572             QString themename;
       
   573             QLocalSocket *themeSelectionClient = (QLocalSocket *)sender();
       
   574             inputDataStream >> themename;
       
   575             iServer->setThemeSelectionClient(themeSelectionClient);
       
   576             iServer->handleThemeSelection(themename);
       
   577             break;
       
   578         }
       
   579         case EThemeContentUpdate: {
       
   580             iServer->clearIconCache();
       
   581             HbThemeServerRequest requestType = EThemeContentUpdate;
       
   582             QByteArray block;
       
   583             QDataStream out(&block, QIODevice::WriteOnly);
       
   584             out << (int)requestType;
       
   585             iServer->writeToClients(block);
       
   586             break;
       
   587         }
       
   588         case EThemeServerStop: {
       
   589             //This segment is used by HbThemeApiWrapperUi.
       
   590             //When no HbApplication is open and HbThemeserver has only HbThemeApiWrapperUi as client, making sessionListCount =1
       
   591             //HbThemeserver is closed otherwise warning is shown.
       
   592             //QCoreApplication::quit() removes the UI of HbThemeServer. Destructor of HbThemeServer will be called where it was instantiated.
       
   593             if (iServer->sessionListCount() == 1) {
       
   594 #ifdef QT_DEBUG
       
   595                 iServer->close();
       
   596 #endif
       
   597                 ((QLocalSocket *)sender())->disconnectFromServer();
       
   598                 QCoreApplication::quit();
       
   599             } else {
       
   600                 qWarning() << "Close all HbApplications before closing hbthemeserver!!";
       
   601             }
       
   602             break;
       
   603         }
       
   604         case EIconLookup: {
       
   605             QString filename;
       
   606             QSizeF size;
       
   607             int aspectRatioMode;
       
   608             int mode;
       
   609             bool mirrored;
       
   610             int options;
       
   611             QColor color;
       
   612             inputDataStream >> filename;
       
   613             inputDataStream >> size;
       
   614             inputDataStream >> aspectRatioMode;
       
   615             inputDataStream >> mode;
       
   616             inputDataStream >> mirrored;
       
   617             inputDataStream >> options;
       
   618             inputDataStream >> color;
       
   619 
       
   620 #ifdef THEME_SERVER_TRACES
       
   621             qDebug() << "image req at server: " << filename;
       
   622 #endif
       
   623             HbIconKey key(filename, size, (Qt::AspectRatioMode)aspectRatioMode, (QIcon::Mode)mode, mirrored, color);
       
   624             QByteArray output = handleIconLookup(key, data, options);
       
   625             ((QLocalSocket *)sender())->write(output);
       
   626             break;
       
   627         }
       
   628         case EIconDefaultSize:
       
   629             break; //todo
       
   630         case EMultiPieceIcon: {
       
   631             HbMultiIconParams frameItemParams;
       
   632             inputDataStream >> frameItemParams.multiPartIconList;
       
   633             inputDataStream >> frameItemParams.multiPartIconData.multiPartIconId;
       
   634 
       
   635             int noOfPieces = 1;
       
   636             if (frameItemParams.multiPartIconData.multiPartIconId.contains("_3PV", Qt::CaseInsensitive) ||
       
   637                     frameItemParams.multiPartIconData.multiPartIconId.contains("_3PH", Qt::CaseInsensitive)) {
       
   638                 noOfPieces = 3;
       
   639             } else if (frameItemParams.multiPartIconData.multiPartIconId.contains("_9P", Qt::CaseInsensitive)) {
       
   640                 noOfPieces = 9;
       
   641             }
       
   642 
       
   643             for (int i = 0; i < noOfPieces; i++) {
       
   644                 inputDataStream >> frameItemParams.multiPartIconData.sources[i];
       
   645             }
       
   646 
       
   647             for (int i = 0; i < noOfPieces; i++) {
       
   648                 inputDataStream >> frameItemParams.multiPartIconData.targets[i];
       
   649             }
       
   650 
       
   651             for (int i = 0; i < noOfPieces; i++) {
       
   652                 inputDataStream >> frameItemParams.multiPartIconData.pixmapSizes[i];
       
   653             }
       
   654 
       
   655             inputDataStream >> frameItemParams.size;
       
   656             inputDataStream >> frameItemParams.aspectRatioMode;
       
   657             inputDataStream >> frameItemParams.mode;
       
   658             inputDataStream >> frameItemParams.mirrored;
       
   659             inputDataStream >> frameItemParams.options;
       
   660             inputDataStream >> frameItemParams.color;
       
   661 
       
   662 #ifdef THEME_SERVER_TRACES
       
   663             qDebug() << "image req at server: " << frameItemParams.multiPartIconList;
       
   664 #endif
       
   665 
       
   666             int index = frameItemParams.multiPartIconList[0].lastIndexOf("/");
       
   667             QString iconId = frameItemParams.multiPartIconList[0].left(index + 1);
       
   668             HbSharedIconInfo stitchedData;
       
   669             QT_TRY {
       
   670                 iconId.append(frameItemParams.multiPartIconData.multiPartIconId);
       
   671                 HbIconKey finalIconKey(iconId, frameItemParams.size,
       
   672                                        (Qt::AspectRatioMode)frameItemParams.aspectRatioMode,
       
   673                                        (QIcon::Mode)frameItemParams.mode, frameItemParams.mirrored,
       
   674                                        frameItemParams.color);
       
   675 
       
   676                 stitchedData.type = INVALID_FORMAT;
       
   677 
       
   678                 if (!iconInfoFromSingleIcon(finalIconKey, stitchedData)) {
       
   679                     iconInfoFromMultiParts(frameItemParams, noOfPieces, finalIconKey, stitchedData);
       
   680                 }
       
   681             } QT_CATCH(const std::bad_alloc &) {
       
   682                 stitchedData.type = INVALID_FORMAT;
       
   683             }
       
   684 
       
   685             QByteArray outputByteArray;
       
   686             QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly);
       
   687             HbThemeServerRequest request;
       
   688             request = EMultiPieceIcon;
       
   689             fillOutPutDataStream(outputDataStream, stitchedData, request);
       
   690 #ifdef THEME_SERVER_TRACES
       
   691             qDebug() << Q_FUNC_INFO << " offset= " << stitchedData.pixmapData.offset << " format= " << stitchedData.pixmapData.format;
       
   692             testLabel->setPixmap(QPixmap::fromImage(
       
   693                                      QImage(
       
   694                                          HbMemoryUtils::getAddress<uchar>(HbMemoryManager::SharedMemory, stitchedData.pixmapData.offset),
       
   695                                          stitchedData.pixmapData.width,
       
   696                                          stitchedData.pixmapData.height,
       
   697                                          stitchedData.pixmapData.format)));
       
   698 #endif
       
   699             ((QLocalSocket *)sender())->write(outputByteArray);
       
   700             break;
       
   701         }
       
   702         case EMultiIcon: {
       
   703             QStringList fileList;
       
   704             QVector<QSizeF> sizeList;
       
   705             QSizeF size;
       
   706             int aspectRatioMode;
       
   707             int mode;
       
   708             bool mirrored;
       
   709             int options;
       
   710             QColor color;
       
   711             inputDataStream >> fileList;
       
   712             for (int i = 0; i < fileList.count(); i++) {
       
   713                 inputDataStream >> size;
       
   714                 sizeList << size;
       
   715             }
       
   716             inputDataStream >> aspectRatioMode;
       
   717             inputDataStream >> mode;
       
   718             inputDataStream >> mirrored;
       
   719             inputDataStream >> options;
       
   720             inputDataStream >> color;
       
   721 
       
   722 #ifdef THEME_SERVER_TRACES
       
   723             qDebug() << "image req at server: " << filename;
       
   724 #endif
       
   725             QByteArray output;
       
   726             for (int i = 0; i < fileList.count(); i++) {
       
   727                 HbIconKey key(fileList[i], sizeList[i], (Qt::AspectRatioMode)aspectRatioMode, (QIcon::Mode)mode, mirrored, color);
       
   728                 output.append(handleIconLookup(key, data, options));
       
   729             }
       
   730 
       
   731             ((QLocalSocket *)sender())->write(output);
       
   732 
       
   733             break;
       
   734         }
       
   735         //Debug Code for Test Purpose
       
   736 #ifdef HB_ICON_CACHE_DEBUG
       
   737         case ECacheIconCount: {
       
   738             int count = iServer->cacheIconCount();
       
   739             outputDataStream << count;
       
   740             ((QLocalSocket *)sender())->write(outputByteArray);
       
   741             break;
       
   742         }
       
   743         case ERasterMemLimit: {
       
   744             int limit;
       
   745             inputDataStream >> limit;
       
   746             iServer->setMaxGpuCacheSize(limit);
       
   747             break;
       
   748         }
       
   749         case EVectorMemLimit: {
       
   750             int limit;
       
   751             inputDataStream >> limit;
       
   752             iServer->setMaxCpuCacheSize(limit);
       
   753             break;
       
   754         }
       
   755         case EFreeRasterMem: {
       
   756             int freeRastMem = iServer->freeRasterMemory();
       
   757             outputDataStream << freeRastMem;
       
   758             ((QLocalSocket *)sender())->write(outputByteArray);
       
   759             break;
       
   760         }
       
   761         case EFreeVectorMem: {
       
   762             int freeVectMem = iServer->freeVectorMemory();
       
   763             outputDataStream << freeVectMem;
       
   764             ((QLocalSocket *)sender())->write(outputByteArray);
       
   765             break;
       
   766         }
       
   767         case ELastAddedItemMem {
       
   768                 int lAddItemMem = iServer->lastAddedItemMem();
       
   769                 outputDataStream << lAddItemMem;
       
   770                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   771                 break;
       
   772             }
       
   773         case ELastRemovedItemMem {
       
   774                 int lRemItemMem = iServer->lastRemovedItemMem();
       
   775                 outputDataStream << lRemItemMem;
       
   776                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   777                 break;
       
   778             }
       
   779         case ELastRemovedItemRefCount: {
       
   780                 int lRemItemRfCnt = iServer->lastRemovedItemRfCount();
       
   781                 outputDataStream << lRemItemRfCnt;
       
   782                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   783                 break;
       
   784             }
       
   785         case ELastAddedItemRefCount: {
       
   786                 int lAddItemRfCnt = iServer->lastAddedRefCount();
       
   787                 outputDataStream << lAddItemRfCnt;
       
   788                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   789                 break;
       
   790             }
       
   791         case EEnableCache: {
       
   792                 int enable ;
       
   793                 inputDataStream >> enable;
       
   794                 bool success = iServer->enableCache(enable);
       
   795                 outputDataStream << (int)success;
       
   796                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   797                 break;
       
   798             }
       
   799         case ECacheHit: {
       
   800                 int cacheHitCnt = iServer->cacheHitCount();
       
   801                 outputDataStream << cacheHitCnt;
       
   802                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   803                 break;
       
   804             }
       
   805         case ECacheMiss: {
       
   806                 int cacheMissCnt = iServer->cacheMissCount();
       
   807                 outputDataStream << cacheMissCnt;
       
   808                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   809                 break;
       
   810             }
       
   811         case ECleanRasterLRUList: {
       
   812                 iServer->cleanRasterLRUList();
       
   813                 break;
       
   814             }
       
   815         case ECleanVectorLRUList: {
       
   816                 iServer->cleanVectorLRUList();
       
   817                 break;
       
   818             }
       
   819         case EGpuLruCount: {
       
   820                 int rasterLruCount = iServer->rasterLruCount();
       
   821                 outputDataStream << rasterLruCount;
       
   822                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   823                 break;
       
   824             }
       
   825         case ECpuLruCount: {
       
   826                 int vectorLruCount = iServer->vectorLruCount();
       
   827                 outputDataStream << vectorLruCount;
       
   828                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   829                 break;
       
   830             }
       
   831         case EServerHeap: {
       
   832             }
       
   833 #endif
       
   834         case EUnloadIcon: {
       
   835                 QString filename;
       
   836                 QSizeF size;
       
   837                 int aspectRatioMode;
       
   838                 int mode;
       
   839                 bool mirrored;
       
   840                 int options;
       
   841                 QColor color;
       
   842                 inputDataStream >> filename;
       
   843                 inputDataStream >> size;
       
   844                 inputDataStream >> aspectRatioMode;
       
   845                 inputDataStream >> mode;
       
   846                 inputDataStream >> mirrored;
       
   847                 inputDataStream >> options;
       
   848                 inputDataStream >> color;
       
   849 
       
   850                 HbIconKey key(filename, size, (Qt::AspectRatioMode)aspectRatioMode,
       
   851                               (QIcon::Mode)mode, mirrored, color);
       
   852                 iServer->removeIconCacheItem(key);
       
   853                 sessionIconData.removeOne(key);
       
   854                 QByteArray outputByteArray;
       
   855                 QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly);
       
   856                 outputDataStream << (int)requestType;
       
   857                 ((QLocalSocket *)sender())->write(outputByteArray);
       
   858                 break;
       
   859             }
       
   860         default:
       
   861             break;
       
   862         }
       
   863     }
       
   864 }
       
   865 
       
   866 /*!
       
   867   \fn HbThemeServerSession::iconInfoFromSingleIcon()
       
   868   Checks for the cacheItem for a given key, if found gets the data relevant of the cacheItem.
       
   869 */
       
   870 
       
   871 bool HbThemeServerSession::iconInfoFromSingleIcon(HbIconKey key,
       
   872         HbSharedIconInfo &stitchedData)
       
   873 {
       
   874     stitchedData.type = INVALID_FORMAT;
       
   875     HbIconCacheItem * cacheItem = iServer->iconCacheItem(key);
       
   876     if (cacheItem) {
       
   877         getDataFromCacheItem(cacheItem, stitchedData);
       
   878         return true;
       
   879     }
       
   880     return false;
       
   881 }
       
   882 
       
   883 /*!
       
   884   \fn HbThemeServerSession::createCacheItemData()
       
   885   Creates a cacheItem of the given key and insert the item in to the list
       
   886   else free the data allocated for the cache..
       
   887 */
       
   888 bool HbThemeServerSession::createCacheItemData(HbIconKey key, int options , HbSharedIconInfo &data)
       
   889 {
       
   890     HbIconCacheItem * cacheItemOfPiece = iServer->iconCacheItem(key);
       
   891     if (cacheItemOfPiece) {
       
   892         return true;
       
   893     }
       
   894 
       
   895     QScopedPointer <HbIconCacheItem> tempIconCacheItem;
       
   896     bool insertKeyIntoSessionList = false;
       
   897     data.type = INVALID_FORMAT;
       
   898     QString format = HbThemeServerUtils::formatFromPath(key.filename);
       
   899 
       
   900     tempIconCacheItem.reset(HbIconCacheItemCreator::createCacheItem(key,
       
   901                             (HbIconLoader::IconLoaderOptions)options,
       
   902                             format,
       
   903                             false));
       
   904     cacheItemOfPiece = tempIconCacheItem.data();
       
   905     if (cacheItemOfPiece) {
       
   906         getDataFromCacheItem(cacheItemOfPiece, data);
       
   907         if (data.type != INVALID_FORMAT) {
       
   908             insertKeyIntoSessionList = iServer->insertIconCacheItem(key, cacheItemOfPiece);
       
   909             if (!insertKeyIntoSessionList) {
       
   910                 //if insertion failed free the memory
       
   911                 freeDataFromCacheItem(cacheItemOfPiece);
       
   912                 data.type = INVALID_FORMAT;
       
   913             }
       
   914         }
       
   915     }
       
   916     tempIconCacheItem.take();
       
   917     return insertKeyIntoSessionList;
       
   918 }
       
   919 
       
   920 /*!
       
   921   \fn HbThemeServerSession::createStichedIconInfoOfParts()
       
   922   Creates a consolidated icon of the available piece iconInfo.
       
   923 */
       
   924 bool HbThemeServerSession::createStichedIconInfoOfParts(QVector<HbSharedIconInfo> dataForParts, HbMultiIconParams params,
       
   925         HbIconKey &finalIconKey, HbSharedIconInfo &stitchedData)
       
   926 {
       
   927     HbIconCacheItem * cacheItem = iServer->iconCacheItem(finalIconKey);
       
   928     if (cacheItem) {
       
   929         return true;
       
   930     }
       
   931     bool insertKeyIntoSessionList = false;
       
   932     stitchedData.type = INVALID_FORMAT;
       
   933     QString format = HbThemeServerUtils::formatFromPath(params.multiPartIconList[0]);
       
   934 
       
   935     QScopedPointer <HbPixmapIconProcessor> tempIconProcessor(new HbPixmapIconProcessor(finalIconKey, (HbIconLoader::IconLoaderOptions)params.options, format));
       
   936     HbPixmapIconProcessor * rasterIcon = tempIconProcessor.data();
       
   937     rasterIcon->createMultiPieceIconData(dataForParts, params);
       
   938 
       
   939     QScopedPointer <HbIconCacheItem> tempIconCacheItem;
       
   940     tempIconCacheItem.reset(HbIconCacheItemCreator::createCacheItem(finalIconKey, (HbIconLoader::IconLoaderOptions)params.options, format, false));
       
   941     cacheItem = tempIconCacheItem.data();
       
   942 
       
   943     cacheItem->rasterIconData = rasterIcon->sharedIconData();
       
   944     cacheItem->rasterIconDataCost = rasterIcon->sharedIconDataCost();
       
   945     stitchedData = cacheItem->rasterIconData;
       
   946     if (stitchedData.type != INVALID_FORMAT) {
       
   947         insertKeyIntoSessionList = iServer->insertIconCacheItem(finalIconKey, cacheItem);
       
   948         if (!insertKeyIntoSessionList) {
       
   949             //if insertion failed free the memory
       
   950             freeDataFromCacheItem(cacheItem);
       
   951             stitchedData.type = INVALID_FORMAT;
       
   952         }
       
   953     }
       
   954     tempIconCacheItem.take();
       
   955     delete rasterIcon;
       
   956     return insertKeyIntoSessionList;
       
   957 }
       
   958 
       
   959 
       
   960 /*!
       
   961   \fn HbThemeServerSession::iconInfoFromMultiParts()
       
   962   Creates a shared IconInfo of the piece files of a frame item and
       
   963   tries to create a stiched icon of the same.
       
   964 */
       
   965 void HbThemeServerSession::iconInfoFromMultiParts(HbMultiIconParams params,
       
   966         int noOfPieces,
       
   967         HbIconKey &stichedKey,
       
   968         HbSharedIconInfo &stitchedData)
       
   969 {
       
   970     QVector<HbIconKey> keysInserted;
       
   971     QVector<HbSharedIconInfo>  dataForParts;
       
   972     bool insertKeyIntoSessionList = false;
       
   973     bool failedToCreateParts = false;
       
   974     QString format;
       
   975 
       
   976 
       
   977     for (int i = 0; i < noOfPieces; i++) {
       
   978         HbSharedIconInfo data;
       
   979         bool iconPieceMirrored = false;
       
   980         HbIconKey key(params.multiPartIconList.at(i), params.multiPartIconData.pixmapSizes[i], (Qt::AspectRatioMode)stichedKey.aspectRatioMode, (QIcon::Mode)stichedKey.mode, iconPieceMirrored, stichedKey.color);
       
   981         insertKeyIntoSessionList = iconInfoFromSingleIcon(key, data);
       
   982         if (!insertKeyIntoSessionList) {
       
   983             insertKeyIntoSessionList = createCacheItemData(key, params.options, data);
       
   984         }
       
   985         if ((data.type == INVALID_FORMAT) || (!insertKeyIntoSessionList)) {
       
   986             failedToCreateParts = true;
       
   987             break;
       
   988         } else {
       
   989             //The session will only keep track of icons that were either successfully found or were
       
   990             //successfully inserted in the cache.
       
   991             keysInserted.append(key);
       
   992             dataForParts.append(data);
       
   993             sessionIconData.append(key);
       
   994         }
       
   995     }//end of for
       
   996 
       
   997     if ((failedToCreateParts) || (dataForParts.count() != noOfPieces) || (!insertKeyIntoSessionList)) {
       
   998         //atLeast one of the icon did'nt get constructed , so move the cached piece icons to unused state and return
       
   999         for (int i = 0; i < keysInserted.count(); i++) {
       
  1000             sessionIconData.removeOne(keysInserted.at(i));
       
  1001         }
       
  1002         dataForParts.clear();
       
  1003         stitchedData.type = INVALID_FORMAT;
       
  1004         return;
       
  1005     }
       
  1006 // Create a stitched icon of the available piece shared iconinfos
       
  1007     if ((dataForParts.count() == noOfPieces) && (!failedToCreateParts)) {
       
  1008         if (createStichedIconInfoOfParts(dataForParts, params, stichedKey, stitchedData)) {
       
  1009             sessionIconData.append(stichedKey);
       
  1010         }
       
  1011     }
       
  1012 // Move the keys created for pieces to unused state*/
       
  1013     for (int i = 0; i < keysInserted.count(); i++) {
       
  1014         sessionIconData.removeOne(keysInserted.at(i));
       
  1015     }
       
  1016 }
       
  1017 
       
  1018 /*!
       
  1019   \fn HbThemeServerSession::freeDataFromCacheItem()
       
  1020   Frees data from the cached item when insertion to the list fails.
       
  1021 */
       
  1022 void HbThemeServerSession::freeDataFromCacheItem(HbIconCacheItem* cacheItem)
       
  1023 {
       
  1024     GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory)
       
  1025     if (cacheItem->rasterIconData.type != INVALID_FORMAT) {
       
  1026         switch (cacheItem->rasterIconData.type) {
       
  1027         case PIC :
       
  1028             manager->free(cacheItem->rasterIconData.picData.offset);
       
  1029             break;
       
  1030         case NVG :
       
  1031             manager->free(cacheItem->rasterIconData.nvgData.offset);
       
  1032             break;
       
  1033         case OTHER_SUPPORTED_FORMATS :
       
  1034             manager->free(cacheItem->rasterIconData.pixmapData.offset);
       
  1035             break;
       
  1036         default:
       
  1037             break;
       
  1038         }
       
  1039     }
       
  1040     if (cacheItem->vectorIconData.type != INVALID_FORMAT) {
       
  1041         switch (cacheItem->vectorIconData.type) {
       
  1042         case PIC :
       
  1043             manager->free(cacheItem->vectorIconData.picData.offset);
       
  1044             break;
       
  1045         case NVG :
       
  1046             manager->free(cacheItem->vectorIconData.nvgData.offset);
       
  1047             break;
       
  1048         case OTHER_SUPPORTED_FORMATS :
       
  1049             manager->free(cacheItem->vectorIconData.pixmapData.offset);
       
  1050             break;
       
  1051         default:
       
  1052             break;
       
  1053         }
       
  1054     }
       
  1055     if (cacheItem->blobIconData.type == BLOB) {
       
  1056         manager->free(cacheItem->blobIconData.blobData.offset);
       
  1057     }
       
  1058 }
       
  1059 
       
  1060 /*!
       
  1061   \fn HbThemeServerSession::fillOutPutDataStream()
       
  1062   Fills the Output data stream with the sharedIconInfo data.
       
  1063 */
       
  1064 void HbThemeServerSession::fillOutPutDataStream(QDataStream &outputDataStream, HbSharedIconInfo &data, HbThemeServerRequest request)
       
  1065 {
       
  1066     outputDataStream << (int)request;
       
  1067     outputDataStream << (int)data.type;
       
  1068 
       
  1069     switch (data.type) {
       
  1070     case OTHER_SUPPORTED_FORMATS:
       
  1071         outputDataStream << data.pixmapData.offset;
       
  1072         outputDataStream << data.pixmapData.width;
       
  1073         outputDataStream << data.pixmapData.height;
       
  1074         outputDataStream << data.pixmapData.defaultWidth;
       
  1075         outputDataStream << data.pixmapData.defaultHeight;
       
  1076         outputDataStream << (int)data.pixmapData.format;
       
  1077         break;
       
  1078         /*case SVG:*/
       
  1079     case PIC: {
       
  1080         outputDataStream << data.picData.offset;
       
  1081         outputDataStream << data.picData.dataSize;
       
  1082         outputDataStream << data.picData.defaultWidth;
       
  1083         outputDataStream << data.picData.defaultHeight;
       
  1084         break;
       
  1085     }
       
  1086     case NVG: {
       
  1087         outputDataStream << data.nvgData.offset;
       
  1088         outputDataStream << data.nvgData.dataSize;
       
  1089         break;
       
  1090     }
       
  1091     case BLOB: {
       
  1092         outputDataStream << data.blobData.offset;
       
  1093         outputDataStream << data.blobData.dataSize;
       
  1094         break;
       
  1095     }
       
  1096     default:
       
  1097         break;
       
  1098     }
       
  1099 }
       
  1100 
       
  1101 /*!
       
  1102   \fn HbThemeServerSession::getDataFromCacheItem()
       
  1103   Gets data from the cache Item.
       
  1104 */
       
  1105 void HbThemeServerSession::getDataFromCacheItem(HbIconCacheItem* cacheItem, HbSharedIconInfo &data) const
       
  1106 {
       
  1107     if (cacheItem) {
       
  1108         if (cacheItem->rasterIconData.type != INVALID_FORMAT) {
       
  1109             data = cacheItem->rasterIconData;
       
  1110         } else if (cacheItem->vectorIconData.type != INVALID_FORMAT) {
       
  1111             data = cacheItem->vectorIconData;
       
  1112         } else if (cacheItem->blobIconData.type != INVALID_FORMAT) {
       
  1113             data = cacheItem->blobIconData;
       
  1114         } else {
       
  1115             data.type = INVALID_FORMAT;
       
  1116         }
       
  1117     }
       
  1118 }
       
  1119 
       
  1120 /**
       
  1121  * HbThemeServerPrivate::handleSharedWidgetMLLookup()
       
  1122  */
       
  1123 QByteArray HbThemeServerPrivate::handleSharedWidgetMLLookup(const QString &fileName, const QString &layout, const QString &section)
       
  1124 {
       
  1125     int offset = HbThemeServerUtils::getSharedLayoutDefinition(fileName, layout, section);
       
  1126     QByteArray outputByteArray;
       
  1127     QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly);
       
  1128     HbThemeServerRequest request;
       
  1129     request = EWidgetMLLookup;
       
  1130     outputDataStream << (int)request;
       
  1131     outputDataStream << offset;
       
  1132     return outputByteArray;
       
  1133 }
       
  1134 
       
  1135 /**
       
  1136  * HbThemeServerPrivate::handleSharedEffectAddAndFileLookup()
       
  1137  */
       
  1138 QByteArray HbThemeServerPrivate::handleSharedEffectAddAndFileLookup(int request, const QString &fileName)
       
  1139 {
       
  1140     int offset = HbThemeServerUtils::getSharedEffect(fileName);
       
  1141     QByteArray outputByteArray;
       
  1142     QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly);
       
  1143     outputDataStream << request;
       
  1144     outputDataStream << offset;
       
  1145     return outputByteArray;
       
  1146 }
       
  1147 
       
  1148 /**
       
  1149  * HbThemeServerSession::handleStyleSheetLookup()
       
  1150  */
       
  1151 QByteArray HbThemeServerSession::handleStyleSheetLookup(int request, const QString &fileName, HbLayeredStyleLoader::LayerPriority priority)
       
  1152 {
       
  1153     int offset = -1;
       
  1154     HbCacheItem* cssItem = iServer->cssCacheItem(fileName);
       
  1155     bool insertKeyIntoSessionList = false;
       
  1156     if (cssItem) {
       
  1157         offset = cssItem->offset;
       
  1158         insertKeyIntoSessionList = true;
       
  1159     } else {
       
  1160         bool tryAgain = false;
       
  1161         do {
       
  1162             offset = HbThemeServerUtils::getSharedStylesheet(fileName, priority);
       
  1163             if (offset >= 0) {
       
  1164                 HbCacheItem *cssItem = new HbCacheItem(offset, 0, fileName);
       
  1165                 insertKeyIntoSessionList = iServer->insertCssCacheItem(fileName, cssItem);
       
  1166                 if (priority == HbLayeredStyleLoader::Priority_Core && cssItem->refCount == 1) {
       
  1167                     // This will make sure the requested stylesheet will always remain
       
  1168                     // in the primary and secondary cache.
       
  1169                     cssItem->incrementRefCount();
       
  1170                 }
       
  1171                 break;
       
  1172             } else if (offset == OUT_OF_MEMORY_ERROR && tryAgain == false) {
       
  1173                 iServer->doCleanup();
       
  1174                 tryAgain = true;
       
  1175             } else if (offset == OUT_OF_MEMORY_ERROR && tryAgain == true) {
       
  1176                 //try only once to free up memory, else offset remains -2
       
  1177                 tryAgain = false;
       
  1178             }
       
  1179         } while (tryAgain);
       
  1180     }
       
  1181 
       
  1182     if (insertKeyIntoSessionList) {
       
  1183         //The session will only keep track of cssFiles that were either successfully found or were
       
  1184         //successfully inserted in the cache.
       
  1185         sessionCssData.append(fileName);
       
  1186     }
       
  1187 
       
  1188     //write to client socket the offset of required stylesheet
       
  1189     //it can be -1 in case of error.
       
  1190     QByteArray output;
       
  1191     QDataStream outputDataStream(&output, QIODevice::WriteOnly);
       
  1192 
       
  1193     outputDataStream << request;
       
  1194     outputDataStream << offset;
       
  1195 
       
  1196     return output;
       
  1197 }
       
  1198 
       
  1199 /**
       
  1200  * HbThemeServerSession::handleIconLookup()
       
  1201  */
       
  1202 QByteArray HbThemeServerSession::handleIconLookup(const HbIconKey &key, HbSharedIconInfo &data, int options)
       
  1203 {
       
  1204     bool insertKeyIntoSessionList = false;
       
  1205     HbIconCacheItem * cacheItem = iServer->iconCacheItem(key);
       
  1206     QScopedPointer <HbIconCacheItem> tempIconCacheItem;
       
  1207 
       
  1208     if (cacheItem) {
       
  1209         insertKeyIntoSessionList = true; //The item was found in the cache and ref count was incremented
       
  1210         if (cacheItem->rasterIconData.type != INVALID_FORMAT) {
       
  1211             data = cacheItem->rasterIconData;
       
  1212         } else if (cacheItem->vectorIconData.type != INVALID_FORMAT) {
       
  1213             data = cacheItem->vectorIconData;
       
  1214         } else if (cacheItem->blobIconData.type != INVALID_FORMAT) {
       
  1215             data = cacheItem->blobIconData;
       
  1216         } else {
       
  1217             data.type = INVALID_FORMAT;
       
  1218         }
       
  1219     } else {
       
  1220         QString format = HbThemeServerUtils::formatFromPath(key.filename);
       
  1221         QT_TRY {
       
  1222             tempIconCacheItem.reset(HbIconCacheItemCreator::createCacheItem(key,
       
  1223             (HbIconLoader::IconLoaderOptions)options,
       
  1224             format,
       
  1225             false));
       
  1226             cacheItem = tempIconCacheItem.data();
       
  1227             if (cacheItem) {
       
  1228                 if (cacheItem->rasterIconData.type != INVALID_FORMAT) {
       
  1229                     data = cacheItem->rasterIconData;
       
  1230                 } else if (cacheItem->vectorIconData.type != INVALID_FORMAT) {
       
  1231                     data = cacheItem->vectorIconData;
       
  1232                 } else if (cacheItem->blobIconData.type != INVALID_FORMAT) {
       
  1233                     data = cacheItem->blobIconData;
       
  1234                 } else {
       
  1235                     data.type = INVALID_FORMAT;
       
  1236                 }
       
  1237                 if (data.type != INVALID_FORMAT) {
       
  1238                     insertKeyIntoSessionList = iServer->insertIconCacheItem(key, cacheItem);
       
  1239                 }
       
  1240             }
       
  1241         } QT_CATCH(const std::bad_alloc &) {
       
  1242             data.type = INVALID_FORMAT;
       
  1243         }
       
  1244     }
       
  1245     if (insertKeyIntoSessionList) {
       
  1246         //The session will only keep track of icons that were either successfully found or were
       
  1247         //successfully inserted in the cache.
       
  1248         sessionIconData.append(key);
       
  1249     }
       
  1250 
       
  1251     tempIconCacheItem.take();
       
  1252 
       
  1253     QByteArray outputByteArray;
       
  1254     QDataStream outputDataStream(&outputByteArray, QIODevice::WriteOnly);
       
  1255     HbThemeServerRequest request;
       
  1256     request = EIconLookup;
       
  1257     outputDataStream << (int)request;
       
  1258     outputDataStream << (int)data.type;
       
  1259 
       
  1260     if (data.type == OTHER_SUPPORTED_FORMATS) {
       
  1261         outputDataStream << data.pixmapData.offset;
       
  1262         outputDataStream << data.pixmapData.width;
       
  1263         outputDataStream << data.pixmapData.height;
       
  1264         outputDataStream << data.pixmapData.defaultWidth;
       
  1265         outputDataStream << data.pixmapData.defaultHeight;
       
  1266         outputDataStream << (int)data.pixmapData.format;
       
  1267     } else if (/*data.type == SVG || */data.type == PIC) {
       
  1268         outputDataStream << data.picData.offset;
       
  1269         outputDataStream << data.picData.dataSize;
       
  1270         outputDataStream << data.picData.defaultWidth;
       
  1271         outputDataStream << data.picData.defaultHeight;
       
  1272     } else if (data.type == NVG) {
       
  1273         outputDataStream << data.nvgData.offset;
       
  1274         outputDataStream << data.nvgData.dataSize;
       
  1275     } else if (data.type == BLOB) {
       
  1276         outputDataStream << data.blobData.offset;
       
  1277         outputDataStream << data.blobData.dataSize;
       
  1278     }
       
  1279 #ifdef THEME_SERVER_TRACES
       
  1280     qDebug() << Q_FUNC_INFO << " offset= " << data.pixmapData.offset << " format= " << data.pixmapData.format;
       
  1281     testLabel->setPixmap(QPixmap::fromImage(
       
  1282                              QImage(
       
  1283                                  HbMemoryUtils::getAddress<uchar>(HbMemoryManager::SharedMemory, data.pixmapData.offset),
       
  1284                                  data.pixmapData.width,
       
  1285                                  data.pixmapData.height,
       
  1286                                  data.pixmapData.format)));
       
  1287 #endif
       
  1288 
       
  1289     return outputByteArray;
       
  1290 }
       
  1291 
       
  1292 /**
       
  1293  * HbThemeServerPrivate::doCleanup()
       
  1294  *
       
  1295  * This function releases shared memory occupied by css-resources whose reference count is zero,
       
  1296  * so that subsequent css-requests could be fulfilled by the server. Those css-files whose reference
       
  1297  * count are zero, are already appended to the list maintained by the css-cache. Since these resources
       
  1298  * are not being referred to by any application, they can be removed from the cache and corresponding
       
  1299  * shared memory can be freed up.
       
  1300  */
       
  1301 void HbThemeServerPrivate::doCleanup()
       
  1302 {
       
  1303     HbThemeServerUtils::cleanupUnusedCss(cssCache);
       
  1304 }
       
  1305 
       
  1306 /**
       
  1307  * HbThemeServerPrivate::sessionListCount()
       
  1308  * Gives the list count of Sessions.
       
  1309  */
       
  1310 int HbThemeServerPrivate::sessionListCount() const
       
  1311 {
       
  1312     return sessionList.count();
       
  1313 }
       
  1314