src/hbservers/hbthemeserver/hbthemeserverutils.cpp
changeset 5 627c4a0fd0e7
parent 3 11d3954df52a
child 21 4633027730f5
child 34 ed14f46c0e55
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
    25 
    25 
    26 #include "hbthemeserverutils_p.h"
    26 #include "hbthemeserverutils_p.h"
    27 
    27 
    28 #include <QHash>
    28 #include <QHash>
    29 #include <QString>
    29 #include <QString>
       
    30 #include <QStringList>
    30 #include <QFile>
    31 #include <QFile>
    31 #include <QDebug>
    32 #include <QDebug>
    32 
    33 
    33 #include "hbmemoryutils_p.h"
    34 #include "hbmemoryutils_p.h"
    34 #include "hbcssparser_p.h"
    35 #include "hbcssparser_p.h"
    38 #include "hbthemeperf_p.h"
    39 #include "hbthemeperf_p.h"
    39 #include "hbcache_p.h"
    40 #include "hbcache_p.h"
    40 #include "hbiconsource_p.h"
    41 #include "hbiconsource_p.h"
    41 #include "hbwidgetloader_p.h"
    42 #include "hbwidgetloader_p.h"
    42 #include "hbwidgetloaderactions_p.h"
    43 #include "hbwidgetloaderactions_p.h"
       
    44 #include "hbthemeindex_p.h"
    43 
    45 
    44 //Hash of fileName-offset
    46 //Hash of fileName-offset
    45 typedef QHash<QString, int> HbServerCache;
    47 typedef QHash<QString, int> HbServerCache;
    46 
    48 
    47 Q_GLOBAL_STATIC(HbServerCache, effCache)
    49 Q_GLOBAL_STATIC(HbServerCache, effCache)
   184 
   186 
   185 /*
   187 /*
   186   Returns of the offset for the given filename,layout and section name.
   188   Returns of the offset for the given filename,layout and section name.
   187  */
   189  */
   188 
   190 
   189 int HbThemeServerUtils::getSharedLayoutDefinition(const QString & fileName, const QString &layout,
   191 int HbThemeServerUtils::getSharedLayoutDefinition(const QString &fileName, const QString &layout,
   190                                                   const QString &section)
   192                                                   const QString &section)
   191 {
   193 {
   192     int layoutDefOffset = -1;
   194     int layoutDefOffset = -1;
       
   195     QStringRef nameKey(&fileName);
       
   196     if (nameKey.at(0) == ':') {
       
   197         //use only filename as a key.
       
   198         int index = fileName.lastIndexOf('/');
       
   199         if (index >= 0) {
       
   200             nameKey = fileName.rightRef((fileName.size() - 1) - index);
       
   201         }
       
   202     }
   193     // check in the cache.
   203     // check in the cache.
   194     QString key(fileName + layout + section);
   204     QString key;
       
   205     QChar separator('\0');
       
   206     key.reserve(nameKey.length() + 2 //separators
       
   207                 + layout.length() + section.length());
       
   208     key.append(nameKey)
       
   209        .append(separator)
       
   210        .append(layout)
       
   211        .append(separator)
       
   212        .append(section);
       
   213 
   195     if (layoutDefsCache()->contains(key)) {
   214     if (layoutDefsCache()->contains(key)) {
   196         layoutDefOffset = layoutDefsCache()->value(key);
   215         layoutDefOffset = layoutDefsCache()->value(key);
   197         return layoutDefOffset;
   216         return layoutDefOffset;
   198     }
   217     }
   199     HbWidgetLoader loader;
   218     HbWidgetLoader loader;
   226         layoutDefsCache()->insert(key, layoutDefOffset);
   245         layoutDefsCache()->insert(key, layoutDefOffset);
   227         // add the filename and css offset to the shared cache.
   246         // add the filename and css offset to the shared cache.
   228         // no need to check if this item is already present in the
   247         // no need to check if this item is already present in the
   229         // cache as the parsing of the file happens only once
   248         // cache as the parsing of the file happens only once
   230         // in the server side.
   249         // in the server side.
   231         HbSharedCache::instance()->add(HbSharedCache::LayoutDefinition, key, layoutDefOffset);
   250         HbSharedCache::instance()->addLayoutDefinition(fileName, layout, section,
       
   251                                                        layoutDefOffset);
   232     } else {
   252     } else {
   233         // load failed
   253         // load failed
   234         layoutDef->~LayoutDefinition();
   254         layoutDef->~LayoutDefinition();
   235         manager->free(layoutDefOffset);
   255         manager->free(layoutDefOffset);
   236         layoutDefOffset = -1;
   256         layoutDefOffset = -1;
   385         // removed from actual cache.
   405         // removed from actual cache.
   386         delete cache->cacheHandle().take(itemToRemove->fileName);
   406         delete cache->cacheHandle().take(itemToRemove->fileName);
   387     }
   407     }
   388 }
   408 }
   389 
   409 
       
   410 void HbThemeServerUtils::createThemeIndex(const QString &themePath, const HbThemeType &themetype)
       
   411 {
       
   412     #ifdef THEME_INDEX_TRACES
       
   413     qDebug() <<  "ThemeIndex: createThemeIndex" << themePath.toUtf8();
       
   414     #endif
       
   415     QDir themeBasePath(themePath);
       
   416     // Path is like "C:/resource/hb/themes/icons/sfwhitetheme"
       
   417     QString themeName = themeBasePath.dirName();
       
   418     
       
   419     QString filename(themeBasePath.absolutePath());
       
   420     int cutindex = filename.lastIndexOf("/",filename.lastIndexOf("/")-1)+1;
       
   421     filename = filename.left(cutindex);
       
   422     themeBasePath.setPath(filename);
       
   423     filename.append(themeName);
       
   424     filename.append(".themeindex");
       
   425 
       
   426     if (themePath == ":\\themes\\icons\\hbdefault") {
       
   427         themeBasePath.setPath(":/themes");
       
   428         filename = ":/themes/hbdefault.themeindex";
       
   429         themeName = "hbdefault";
       
   430     }
       
   431     
       
   432     QFile indexFile(filename);
       
   433 
       
   434     bool indexOK = false;
       
   435 
       
   436     if (indexFile.open(QIODevice::ReadOnly)) {
       
   437 
       
   438         indexOK = true;
       
   439 
       
   440         GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
       
   441 
       
   442         HbSharedChunkHeader *chunkHeader = (HbSharedChunkHeader*) manager->base();
       
   443         
       
   444         qint64 byteSize = indexFile.size();
       
   445 
       
   446         #ifdef THEME_INDEX_TRACES
       
   447         qDebug() <<  "ThemeIndex: " << themeName.toUtf8() << " index file size:" << byteSize;
       
   448         #endif
       
   449 
       
   450         int offset = manager->alloc(byteSize);
       
   451         if (offset >= 0) {
       
   452             #ifdef THEME_INDEX_TRACES
       
   453             qDebug() <<  "ThemeIndex: memory allocated for theme: " << themeName.toUtf8();
       
   454             #endif
       
   455 
       
   456             // Read the theme index in the shared chunk
       
   457             char *address = HbMemoryUtils::getAddress<char>(HbMemoryManager::SharedMemory, offset);
       
   458 
       
   459             indexFile.read(address, byteSize);
       
   460             indexFile.close();
       
   461 
       
   462             #ifdef THEME_INDEX_TRACES
       
   463             qDebug() <<  "ThemeIndex: Reading themeindex for theme" << themeName.toUtf8() << "... Done!";
       
   464             #endif
       
   465 
       
   466             // Verify theme index contents if it is not located in ROM,
       
   467             // so that it does not have over-indexing offsets which might
       
   468             // crash all the clients trying to read from it.
       
   469 
       
   470 #ifdef Q_OS_SYMBIAN // ROM check only for Symbian - verify always in other platforms.
       
   471             if (themePath[0] != 'z' && themePath[0] != 'Z') {
       
   472 #endif
       
   473                 #ifdef THEME_INDEX_TRACES
       
   474                 qDebug() <<  "ThemeIndex: Validating themeindex for theme" << themeName.toUtf8();
       
   475                 #endif
       
   476 
       
   477                 HbThemeIndex index(address);
       
   478                 indexOK = index.validateItems(byteSize);
       
   479 
       
   480                 #ifdef THEME_INDEX_TRACES
       
   481                 qDebug() <<  "ThemeIndex: Validating themeindex for theme" << themeName.toUtf8() << " done! Result: " << indexOK;
       
   482                 #endif
       
   483 #ifdef Q_OS_SYMBIAN
       
   484             }
       
   485 #endif
       
   486             if (indexOK) {
       
   487                 // Allocate theme path string from shared memory
       
   488                 QByteArray themePathArray = themeBasePath.absolutePath().toLatin1();
       
   489                 quint32 themePathOffset = manager->alloc(themePathArray.size()+1);
       
   490                 memcpy(HbMemoryUtils::getAddress<char>(
       
   491                            HbMemoryManager::SharedMemory,
       
   492                            themePathOffset),
       
   493                            themePathArray.data(),
       
   494                            themePathArray.size()+1); // +1 for '/0'
       
   495 
       
   496                 // Allocate theme name string from shared memory
       
   497                 QByteArray themeNameArray = themeName.toLatin1();
       
   498                 quint32 themeNameOffset = manager->alloc(themeNameArray.size()+1);
       
   499                 memcpy(HbMemoryUtils::getAddress<char>(
       
   500                            HbMemoryManager::SharedMemory,
       
   501                            themeNameOffset),
       
   502                            themeNameArray.data(),
       
   503                            themeNameArray.size()+1); // +1 for '/0'
       
   504 
       
   505                 // Store offset to the index in chunk
       
   506                 switch (themetype) {
       
   507                 case BaseTheme:
       
   508                     // Release previously allocated content
       
   509                     manager->free(chunkHeader->baseThemePathOffset);
       
   510                     manager->free(chunkHeader->baseThemeNameOffset);
       
   511                     manager->free(chunkHeader->baseThemeIndexOffset);
       
   512                     // Base theme offsets
       
   513                     chunkHeader->baseThemePathOffset = themePathOffset;
       
   514                     chunkHeader->baseThemeNameOffset = themeNameOffset;
       
   515                     chunkHeader->baseThemeIndexOffset = offset;
       
   516                     break;
       
   517                 case OperatorC:
       
   518                     // Release previously allocated content
       
   519                     manager->free(chunkHeader->operatorThemeDriveCPathOffset);
       
   520                     manager->free(chunkHeader->operatorThemeDriveCNameOffset);
       
   521                     manager->free(chunkHeader->operatorThemeDriveCIndexOffset);
       
   522                     // Operator theme in C-drive offsets
       
   523                     chunkHeader->operatorThemeDriveCPathOffset = themePathOffset;
       
   524                     chunkHeader->operatorThemeDriveCNameOffset = themeNameOffset;
       
   525                     chunkHeader->operatorThemeDriveCIndexOffset = offset;
       
   526                     break;
       
   527                 case OperatorROM:
       
   528                     // Release previously allocated content
       
   529                     manager->free(chunkHeader->operatorThemeRomPathOffset);
       
   530                     manager->free(chunkHeader->operatorThemeRomNameOffset);
       
   531                     manager->free(chunkHeader->operatorThemeRomIndexOffset);
       
   532                     // Operator theme in ROM offsets
       
   533                     chunkHeader->operatorThemeRomPathOffset = themePathOffset;
       
   534                     chunkHeader->operatorThemeRomNameOffset = themeNameOffset;
       
   535                     chunkHeader->operatorThemeRomIndexOffset = offset;
       
   536                     break;
       
   537                 case ActiveTheme:
       
   538                     // Release previously allocated content
       
   539                     manager->free(chunkHeader->activeThemePathOffset);
       
   540                     manager->free(chunkHeader->activeThemeNameOffset);
       
   541                     manager->free(chunkHeader->activeThemeIndexOffset);
       
   542                     // Active theme offsets
       
   543                     chunkHeader->activeThemePathOffset = themePathOffset;
       
   544                     chunkHeader->activeThemeNameOffset = themeNameOffset;
       
   545                     chunkHeader->activeThemeIndexOffset = offset;
       
   546                     break;
       
   547                 default:
       
   548                     break;
       
   549                 }
       
   550             } else {
       
   551                 // If the index contents were not OK, remove the index from the chunk
       
   552                 manager->free(offset);
       
   553             }
       
   554         }
       
   555     }
       
   556 }
       
   557 
       
   558