controlpanelplugins/themeplugin/src/cpthemeutil.cpp
changeset 22 a5692c68d772
child 28 e0b83131558d
child 40 593f946f4fec
equal deleted inserted replaced
21:2883a5458389 22:a5692c68d772
       
     1 /*
       
     2  * Copyright (c) 2009 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 "cpthemeinfo.h"
       
    19 #include "cpthemeutil.h"
       
    20 
       
    21 #include <QStringList>
       
    22 #include <QSettings>  
       
    23 #include <QFileInfoList>
       
    24 #include <QDir>
       
    25 #include <QList>
       
    26 
       
    27 #include <hbicon.h>
       
    28 #include <hbinstance.h>
       
    29 
       
    30 /*!
       
    31  * This class provides utility function to get Theme information.  
       
    32  */
       
    33 
       
    34 #if defined(Q_OS_SYMBIAN)
       
    35 #include <e32std.h>
       
    36 #include <centralrepository.h>
       
    37         static const TUid KServerUid3={0x20022E82};
       
    38         static const TUint32 KDefaultThemeNameKey = 0x2;
       
    39         static const TUint32 KDefaultThemeRootPathKey = 0x3;
       
    40 
       
    41 #endif
       
    42 
       
    43 #if !defined(Q_OS_SYMBIAN)
       
    44     #include <stdio.h>
       
    45     static const char* KThemePathKey = "HB_THEMES_DIR";  //used for getting default theme.
       
    46 #endif
       
    47     
       
    48     
       
    49 #ifdef Q_OS_WIN
       
    50     static char* _path = NULL;
       
    51     static size_t _size = 0;
       
    52     _dupenv_s(&_path, &_size, KThemePathKey);
       
    53     static QString themeRootPath = QString(_path);
       
    54     static QString themeRootPathPostfix = QString();
       
    55     free(_path);
       
    56 #elif defined(Q_OS_SYMBIAN)
       
    57     static QString themeRootPath = "c:\\resource\\hb";
       
    58     static QString themeRootPathPostfix = "resource\\hb";
       
    59 #elif defined(Q_OS_MACX)
       
    60     static QString themeRootPath = QDir::homePath() + '/' + "Library" + QString("hb");
       
    61     static QString themeRootPathPostfix = QString();
       
    62 #elif defined(Q_OS_UNIX)
       
    63     static QString themeRootPath = QString(getenv(KThemePathKey));
       
    64     static QString themeRootPathPostfix = QString();
       
    65 #else
       
    66     static QString themeRootPath = "c:\\resource\\hb";
       
    67     static QString themeRootPathPostfix = QString();
       
    68 #endif
       
    69 
       
    70     //Location of theme preview and background icons.
       
    71     static const QString KPreviewThumbnailNVG = "/scalable/qtg_graf_theme_preview_thumbnail.nvg";
       
    72     static const QString KPreviewThumbnailSVG = "/scalable/qtg_graf_theme_preview_thumbnail.svg";
       
    73     
       
    74     static const QString KPreviewPrtNVG       = "/scalable/qtg_graf_theme_preview_prt.nvg";
       
    75     static const QString KPreviewLscNVG       = "/scalable/qtg_graf_theme_preview_lsc.nvg";
       
    76     static const QString KPreviewPrtSVG       = "/scalable/qtg_graf_theme_preview_prt.svg";
       
    77     static const QString KPreviewLscSVG       = "/scalable/qtg_graf_theme_preview_lsc.svg";
       
    78    
       
    79     static const QString KBackgroundPrtNVG    = "/scalable/qtg_graf_screen_bg_prt.nvg";
       
    80     static const QString KBackgroundLscNVG    = "/scalable/qtg_graf_screen_bg_lsc.nvg";
       
    81     static const QString KBackgroundPrtSVG    = "/scalable/qtg_graf_screen_bg_prt.svg";
       
    82     static const QString KBackgroundLscSVG    = "/scalable/qtg_graf_screen_bg_lsc.svg";
       
    83     
       
    84     static const QString KBackgroundPrtPNG    = "/pixmap/qtg_graf_screen_bg_prt.png";                     
       
    85     static const QString KBackgroundLscPNG    = "/pixmap/qtg_graf_screen_bg_lsc.png";
       
    86     
       
    87 /*!
       
    88  * Returns a list of paths where themes folder reside.  
       
    89  */
       
    90 QStringList CpThemeUtil::themePathList()
       
    91 {
       
    92     static QStringList themesPathList;
       
    93     
       
    94     if(themesPathList.isEmpty()) {
       
    95 
       
    96 #if defined(Q_OS_SYMBIAN)
       
    97         QFileInfoList driveInfoList = QDir::drives();
       
    98         foreach (const QFileInfo &driveInfo, driveInfoList) {
       
    99             const QString themePath = driveInfo.absolutePath() + themeRootPathPostfix;
       
   100             if(QDir(themePath).exists())
       
   101                 themesPathList << themePath;
       
   102         }
       
   103 #else
       
   104         themesPathList << themeRootPath;
       
   105 #endif
       
   106     }
       
   107     return themesPathList;
       
   108 }
       
   109 
       
   110 /*!
       
   111  * Given the theme name, it returns the absolute path of the theme.
       
   112  */
       
   113 QString CpThemeUtil::themePath(const QString& themeName)
       
   114 {
       
   115     QString themePath = "";
       
   116     QStringList themesPathList = themePathList();
       
   117     foreach (const QString &path, themesPathList) {
       
   118        QString tmpPath = path + "/themes/icons/" + themeName;
       
   119        if(QDir(tmpPath).exists()) {
       
   120            themePath = tmpPath;
       
   121            break;
       
   122        }
       
   123     }
       
   124     return themePath;
       
   125 }
       
   126   
       
   127 /*
       
   128  * Builds a CpThemeInfo object given theme path and theme name.  It creates the name and 
       
   129  * preview icons for the object.  Creates a new CpThemeInfo objects. Caller takes ownership.
       
   130  * Returns NULL if can't build the object.
       
   131  */
       
   132 CpThemeInfo* CpThemeUtil::buildThemeInfo(const QString& themePath, const QString& themeName)
       
   133 {
       
   134     CpThemeInfo *themeInfo = new CpThemeInfo();
       
   135     QString iconPath;
       
   136     
       
   137     QString previewPathPrt;
       
   138     QString previewPathLsc;
       
   139     QString name = themeName;
       
   140     QString hidden = "";
       
   141     
       
   142     //first look into the index.theme file to get theme information.
       
   143    
       
   144     if(QFileInfo(themePath + "/index.theme").exists()) {
       
   145         QSettings iniSetting(themePath + "/index.theme", QSettings::IniFormat);
       
   146         iniSetting.beginGroup("Icon Theme");
       
   147         name = iniSetting.value("Name").toString();
       
   148         hidden = iniSetting.value("Hidden").toString();
       
   149         iconPath = iniSetting.value("PreviewThumbnailPath").toString();
       
   150         previewPathPrt = iniSetting.value("PreviewIconPath_prt").toString();
       
   151         previewPathLsc = iniSetting.value("PreviewIconPath_lsc").toString();
       
   152         iniSetting.endGroup();
       
   153        
       
   154     }
       
   155                        
       
   156     if(name.isEmpty() || (hidden == "true") ||( hidden == "")) {
       
   157         return NULL;
       
   158     }
       
   159    
       
   160     themeInfo->setName(name);
       
   161     themeInfo->setItemType(CpThemeInfo::ThemeListItemType_default);
       
   162           
       
   163     //Get the icons. Logic is as follow:
       
   164     /* 1. If the icon path is in index.theme and the icon exist, use it.
       
   165      * 2. Otherwise look for the icon in the theme folder.
       
   166      * 2. If preview icon doesn't exist, use background icon.
       
   167      * 3. If no icon exist (background or preview),use default theme icon.
       
   168      */
       
   169     if(iconPath.isEmpty() || !QFileInfo(themePath + iconPath).exists()){
       
   170     //Set thumbnail
       
   171         if(QFileInfo(themePath + KPreviewThumbnailNVG).exists()){
       
   172             themeInfo->setIcon(HbIcon(themePath + KPreviewThumbnailNVG));
       
   173         }else if(QFileInfo(themePath + KPreviewThumbnailSVG).exists()){
       
   174             themeInfo->setIcon(HbIcon(themePath + KPreviewThumbnailSVG));
       
   175         }else if(QFileInfo(themePath + KBackgroundPrtNVG).exists()){
       
   176             themeInfo->setIcon(HbIcon(themePath + KBackgroundPrtNVG));
       
   177         } else if(QFileInfo(themePath + KBackgroundPrtSVG).exists()){
       
   178             themeInfo->setIcon(HbIcon(themePath + KBackgroundPrtSVG));
       
   179         } else if(QFileInfo(themePath + KBackgroundPrtPNG).exists()){
       
   180             themeInfo->setIcon(HbIcon(themePath + KBackgroundPrtPNG)); 
       
   181         }else{
       
   182             themeInfo->setIcon(HbIcon(defaultTheme()->icon()));
       
   183         }
       
   184 
       
   185     } else {
       
   186         themeInfo->setIcon(HbIcon(themePath + iconPath));
       
   187     }
       
   188           
       
   189     //Portrait preview
       
   190           
       
   191     if(previewPathPrt.isEmpty() || !QFileInfo(themePath + previewPathPrt).exists()) {
       
   192           
       
   193         if(QFileInfo(themePath + KPreviewPrtNVG).exists()){
       
   194             themeInfo->setPortraitPreviewIcon(HbIcon(themePath + KPreviewPrtNVG));
       
   195         }else if(QFileInfo(themePath + KPreviewPrtSVG).exists()){
       
   196             themeInfo->setPortraitPreviewIcon(HbIcon(themePath + KPreviewPrtSVG));
       
   197         }else if(QFileInfo(themePath + KBackgroundPrtNVG).exists()){
       
   198             themeInfo->setPortraitPreviewIcon(HbIcon(themePath + KBackgroundPrtNVG));
       
   199         } else if(QFileInfo(themePath + KBackgroundPrtSVG).exists()){
       
   200             themeInfo->setPortraitPreviewIcon(HbIcon(themePath + KBackgroundPrtSVG));
       
   201         } else if(QFileInfo(themePath + KBackgroundPrtPNG).exists()){
       
   202             themeInfo->setPortraitPreviewIcon(HbIcon(themePath + KBackgroundPrtPNG));
       
   203         } else{
       
   204             themeInfo->setPortraitPreviewIcon(HbIcon(defaultTheme()->icon()));
       
   205         }
       
   206     }
       
   207     else {
       
   208         themeInfo->setPortraitPreviewIcon(HbIcon(themePath + previewPathPrt));
       
   209     }
       
   210           
       
   211     //Landscape preview
       
   212           
       
   213     if(previewPathLsc.isEmpty() || !QFileInfo(themePath + previewPathLsc).exists()) {
       
   214         if(QFileInfo(themePath + KPreviewLscNVG).exists()){
       
   215             themeInfo->setLandscapePreviewIcon(HbIcon(themePath + KPreviewLscNVG));
       
   216         }else if(QFileInfo(themePath + KPreviewLscSVG).exists()){
       
   217             themeInfo->setLandscapePreviewIcon(HbIcon(themePath + KPreviewLscSVG));
       
   218         }else if(QFileInfo(themePath + KBackgroundLscNVG).exists()){
       
   219             themeInfo->setLandscapePreviewIcon(HbIcon(themePath + KBackgroundLscNVG));
       
   220         } else if(QFileInfo(themePath + KBackgroundLscSVG).exists()){
       
   221             themeInfo->setLandscapePreviewIcon(HbIcon(themePath + KBackgroundLscSVG));
       
   222         } else if(QFileInfo(themePath + KBackgroundLscPNG).exists()){
       
   223             themeInfo->setLandscapePreviewIcon(HbIcon(themePath + KBackgroundLscPNG));
       
   224         } else{
       
   225             themeInfo->setLandscapePreviewIcon(HbIcon(defaultTheme()->icon()));
       
   226         }
       
   227     }
       
   228     else {
       
   229         themeInfo->setLandscapePreviewIcon(HbIcon(themePath + previewPathLsc));
       
   230     }
       
   231     return themeInfo;
       
   232 
       
   233 }
       
   234 
       
   235 /*! Creates a list of CpThemeInfo objects representing theme information.
       
   236  *  Caller should take ownership of the list.
       
   237  */
       
   238 QList<CpThemeInfo> CpThemeUtil::buildThemeList()
       
   239 {
       
   240     QList<CpThemeInfo> themeList; 
       
   241   
       
   242     QStringList mThemesPathList = themePathList();
       
   243     
       
   244     foreach (const QString &path, mThemesPathList) {
       
   245         QDir themeDir;
       
   246         themeDir.setPath( path ) ;
       
   247         QStringList iconthemeslist;
       
   248         QStringList list = themeDir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot,QDir::Name);
       
   249         CpThemeInfo* themeInfo;
       
   250 
       
   251         if(list.contains("themes", Qt::CaseSensitive )) {
       
   252             QDir root(themeDir.path());
       
   253             themeDir.setPath(root.path() + "/themes/icons/") ;
       
   254             iconthemeslist = themeDir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot,QDir::Name);
       
   255             foreach(QString themefolder, iconthemeslist) {
       
   256                 QDir iconThemePath(root.path() + "/themes/icons/" + themefolder);
       
   257                 if(iconThemePath.exists("index.theme") &&
       
   258                    (iconThemePath.exists("scalable") || iconThemePath.exists("pixmap") )) {
       
   259 
       
   260                     themeInfo = buildThemeInfo(iconThemePath.path(), themefolder);
       
   261                     if(themeInfo && !themeInfo->name().isEmpty()) {
       
   262                         themeList.append(*themeInfo);
       
   263                     }
       
   264                 } 
       
   265             }
       
   266         }
       
   267     }
       
   268     qSort( themeList );
       
   269     return themeList;
       
   270 }
       
   271 
       
   272 /*!
       
   273  * Returns the default theme information. 
       
   274  */
       
   275 CpThemeInfo* CpThemeUtil::defaultTheme()
       
   276 {
       
   277     //static because default theme doesn't change.
       
   278     static CpThemeInfo *defaultTheme = new CpThemeInfo();
       
   279     QString defaultThemeName;
       
   280     QString defaultThemeRootDir;
       
   281     if(defaultTheme->name().isEmpty()) {
       
   282        
       
   283 
       
   284 #ifdef Q_OS_SYMBIAN
       
   285         CRepository *repository = 0;
       
   286         TRAP_IGNORE(repository = CRepository::NewL(KServerUid3));
       
   287         if (repository) {
       
   288             TBuf<256> value;
       
   289             if (KErrNone == repository->Get((TUint32)KDefaultThemeNameKey, value)) {
       
   290                 QString qvalue((QChar*)value.Ptr(), value.Length());
       
   291                 defaultThemeName = qvalue.trimmed();
       
   292             }
       
   293             value.Zero();
       
   294             if (KErrNone == repository->Get((TUint32)KDefaultThemeRootPathKey, value)) {
       
   295                 QString qvalue((QChar*)value.Ptr(), value.Length());
       
   296                 defaultThemeRootDir = qvalue.trimmed();
       
   297             }
       
   298             else {
       
   299                 defaultThemeRootDir = themePath(defaultThemeName);
       
   300             }
       
   301             value.Zero();
       
   302             delete repository;
       
   303         }
       
   304           
       
   305 #endif
       
   306         defaultTheme = buildThemeInfo(defaultThemeRootDir, defaultThemeName);
       
   307 
       
   308     }
       
   309     return defaultTheme;
       
   310 }
       
   311