src/hbservers/hbthemeserver/hbthemeserverutils.cpp
changeset 21 4633027730f5
parent 5 627c4a0fd0e7
child 28 b7da29130b0e
--- a/src/hbservers/hbthemeserver/hbthemeserverutils.cpp	Tue Jul 06 14:36:53 2010 +0300
+++ b/src/hbservers/hbthemeserver/hbthemeserverutils.cpp	Wed Aug 18 10:05:37 2010 +0300
@@ -37,7 +37,6 @@
 #include "hbeffectxmlparser_p.h"
 #include "hbdeviceprofiledatabase_p.h"
 #include "hbthemeperf_p.h"
-#include "hbcache_p.h"
 #include "hbiconsource_p.h"
 #include "hbwidgetloader_p.h"
 #include "hbwidgetloaderactions_p.h"
@@ -53,6 +52,10 @@
 typedef QHash<QString, int> ServerHashForLayoutDefs;
 Q_GLOBAL_STATIC(ServerHashForLayoutDefs, layoutDefsCache)
 
+// Offset for the cache of the missed CSS files for classes starting 'hb'
+static int missedHbCssFilesOffset = -1;
+static const QString HB_CLASSNAME_PREFIX("hb");
+
 static const int ICON_SOURCES_MAX_SIZE = 8;
 static QList<HbIconSource *> iconSources; // cache of recently used icon sources
 
@@ -107,9 +110,10 @@
  */
 int HbThemeServerUtils::getSharedStylesheet(const QString &fileName,
                                             HbLayeredStyleLoader::LayerPriority priority,
+                                            bool &fileExists,
                                             bool *inSharedCache)
 {
-    int cssOffset = -1;
+    qptrdiff cssOffset = -1;
     HbSharedCache *sharedCache = 0;
     if (priority == HbLayeredStyleLoader::Priority_Core) {
         sharedCache = HbSharedCache::instance();
@@ -125,12 +129,10 @@
     qDebug() << "In " << Q_FUNC_INFO;
 #endif // THEME_SERVER_TRACES
     if (cssOffset < 0) {
-        if (QFile::exists(fileName)) {
-            HbCss::Parser parser;
-            if (!parseCssFile(parser, fileName, cssOffset)) {
-                if (parser.errorCode == HbCss::Parser::OutOfMemoryError) {
-                    return OUT_OF_MEMORY_ERROR;
-                }
+        HbCss::Parser parser;
+        if (!parseCssFile(parser, fileName, cssOffset, fileExists)) {
+            if (parser.errorCode == HbCss::Parser::OutOfMemoryError) {
+                return OUT_OF_MEMORY_ERROR;
             }
         }
         if (sharedCache) { //sharedCache valid only when priority is Priority_Core
@@ -140,13 +142,41 @@
     return cssOffset;
 }
 
+/*
+ * Returns the offset of the missed CSS files for widgets starting with 'hb'
+ */
+int HbThemeServerUtils::getMissedHbCssFilesOffset()
+{
+    if (missedHbCssFilesOffset < 0) {
+        initMissedHbCssFilesList();
+    }
+    return missedHbCssFilesOffset;
+}
+
+void HbThemeServerUtils::initMissedHbCssFilesList()
+{
+    GET_MEMORY_MANAGER(HbMemoryManager::SharedMemory);
+    int offset = -1;
+    try {
+        offset = manager->alloc(sizeof(HbVector<uint>));
+        HbVector<uint> *list = new((char*)manager->base() + offset)
+                HbVector<uint>(HbMemoryManager::SharedMemory);
+    } catch (std::bad_alloc &) {
+        if (offset >= 0) {
+            manager->free(offset);
+            offset = -1;
+        }
+    }
+    missedHbCssFilesOffset = offset;
+}
+
 /**
  * HbThemeServerUtils::parseCssFile()
  *
  * Returns false in case Css file has some error or there is not enough memory
  */
 bool HbThemeServerUtils::parseCssFile(HbCss::Parser &parser, const QString &fileName,
-                                      int &cssOffset)
+                                      qptrdiff &cssOffset, bool &fileExists)
 {
     bool retVal = false;
     // 1. Create a styleSheet in shared memory
@@ -168,7 +198,21 @@
         return retVal;
     }
     // 2. Parse the required file into styleSheet.
-    parser.init(fileName, true);
+    fileExists = parser.init(fileName, true);
+    if (!fileExists) {
+        if (QFileInfo(fileName).fileName().startsWith(HB_CLASSNAME_PREFIX)) {
+            HbVector<uint> *list = HbMemoryUtils::getAddress<HbVector<uint> >(
+                HbMemoryManager::SharedMemory, getMissedHbCssFilesOffset());
+            if (list) {
+                list->append(qHash(fileName));
+            }
+        }
+        if (cssOffset != -1) {
+            manager->free(cssOffset);
+            cssOffset = -1;
+        }
+        return retVal;
+    }
     
     retVal = parser.parse(styleSheet);
     if (!retVal) {
@@ -189,9 +233,9 @@
  */
 
 int HbThemeServerUtils::getSharedLayoutDefinition(const QString &fileName, const QString &layout,
-                                                  const QString &section)
+                                                  const QString &section, bool &fileExists)
 {
-    int layoutDefOffset = -1;
+    qptrdiff layoutDefOffset = -1;
     QStringRef nameKey(&fileName);
     if (nameKey.at(0) == ':') {
         //use only filename as a key.
@@ -218,8 +262,11 @@
     HbWidgetLoader loader;
 
     QFile file(fileName);
-    if (!file.open(QFile::ReadOnly | QFile::Text)) {
+    fileExists = file.open(QFile::ReadOnly | QFile::Text);
+    if (!fileExists) {
+#ifdef THEME_SERVER_TRACES
         qWarning("Unable to open file");
+#endif
         return -1;
     }
 #ifdef THEME_SERVER_TRACES
@@ -311,7 +358,7 @@
     qDebug() << "In " << Q_FUNC_INFO << fileName;
 #endif // THEME_SERVER_TRACES
 
-    int effOffset = -1;
+    qptrdiff effOffset = -1;
     if (effCache()->contains(fileName)) {
         effOffset = effCache()->value(fileName);
 
@@ -383,30 +430,6 @@
     return effOffset;
 }
 
-/**
- * cleanupUnusedCss  function removes css-resources (stylesheets), whose reference count
- * is zero, it also releases the shared memory occupied by those resources.
- * \param cache server css-cache
- *
- */
-void HbThemeServerUtils::cleanupUnusedCss(HbCache *cache)
-{
-    QList<HbCacheItem*> list = cache->lruList();
-    while (!list.isEmpty()) {
-        HbCacheItem* itemToRemove = list.takeFirst();
-        if (itemToRemove->offset != -1) {
-            HbCss::StyleSheet *styleSheet =
-                HbMemoryUtils::getAddress<HbCss::StyleSheet>(HbMemoryManager::SharedMemory,
-                        itemToRemove->offset);
-            HbMemoryUtils::release<HbCss::StyleSheet>(styleSheet);
-            itemToRemove->offset = -1;
-        }
-        //Since we are cleaning up css-resources whose ref-count is zero, these entries will be
-        // removed from actual cache.
-        delete cache->cacheHandle().take(itemToRemove->fileName);
-    }
-}
-
 void HbThemeServerUtils::createThemeIndex(const QString &themePath, const HbThemeType &themetype)
 {
     #ifdef THEME_INDEX_TRACES
@@ -467,9 +490,7 @@
             // so that it does not have over-indexing offsets which might
             // crash all the clients trying to read from it.
 
-#ifdef Q_OS_SYMBIAN // ROM check only for Symbian - verify always in other platforms.
             if (themePath[0] != 'z' && themePath[0] != 'Z') {
-#endif
                 #ifdef THEME_INDEX_TRACES
                 qDebug() <<  "ThemeIndex: Validating themeindex for theme" << themeName.toUtf8();
                 #endif
@@ -480,9 +501,8 @@
                 #ifdef THEME_INDEX_TRACES
                 qDebug() <<  "ThemeIndex: Validating themeindex for theme" << themeName.toUtf8() << " done! Result: " << indexOK;
                 #endif
-#ifdef Q_OS_SYMBIAN
             }
-#endif
+
             if (indexOK) {
                 // Allocate theme path string from shared memory
                 QByteArray themePathArray = themeBasePath.absolutePath().toLatin1();