controlpanelplugins/themeplugin/src/cpthemeutil.cpp
changeset 31 e79ce701c376
parent 25 19394c261aa5
child 33 0cfa53de576f
equal deleted inserted replaced
25:19394c261aa5 31:e79ce701c376
    24 #include <QDir>
    24 #include <QDir>
    25 #include <QList>
    25 #include <QList>
    26 
    26 
    27 #include <hbicon.h>
    27 #include <hbicon.h>
    28 #include <hbinstance.h>
    28 #include <hbinstance.h>
       
    29 #include <restricted/hbthemeservices_r.h>
    29 
    30 
    30 /*!
    31 /*!
    31  * This class provides utility function to get Theme information.  
    32  * This class provides utility function to get Theme information.  
    32  */
    33  */
    33 
    34 
    34 #if defined(Q_OS_SYMBIAN)
    35 #if defined(Q_OS_SYMBIAN)
    35 #include <e32std.h>
    36 #include <e32std.h>
    36 #include <centralrepository.h>
    37 #include <centralrepository.h>
    37         static const TUid KServerUid3={0x20022E82};
    38         static const TUid KServerUid3={0x20022E82};
    38         static const TUint32 KDefaultThemeNameKey = 0x2;
    39         static const TUint32 KDefaultThemeNameKey = 0x2;
    39         static const TUint32 KDefaultThemeRootPathKey = 0x3;
    40   
    40 
       
    41 #endif
    41 #endif
    42 
    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 
    43 
    70     //Location of theme preview and background icons.
    44     //Location of theme preview and background icons.
    71     static const QString KPreviewThumbnailNVG = "/scalable/qtg_graf_theme_preview_thumbnail.nvg";
    45     static const QString KPreviewThumbnailNVG = "/scalable/qtg_graf_theme_preview_thumbnail.nvg";
    72     static const QString KPreviewThumbnailSVG = "/scalable/qtg_graf_theme_preview_thumbnail.svg";
    46     static const QString KPreviewThumbnailSVG = "/scalable/qtg_graf_theme_preview_thumbnail.svg";
    73     
    47     
    82     static const QString KBackgroundLscSVG    = "/scalable/qtg_graf_screen_bg_lsc.svg";
    56     static const QString KBackgroundLscSVG    = "/scalable/qtg_graf_screen_bg_lsc.svg";
    83     
    57     
    84     static const QString KBackgroundPrtPNG    = "/pixmap/qtg_graf_screen_bg_prt.png";                     
    58     static const QString KBackgroundPrtPNG    = "/pixmap/qtg_graf_screen_bg_prt.png";                     
    85     static const QString KBackgroundLscPNG    = "/pixmap/qtg_graf_screen_bg_lsc.png";
    59     static const QString KBackgroundLscPNG    = "/pixmap/qtg_graf_screen_bg_lsc.png";
    86     
    60     
    87 /*!
    61 
    88  * Returns a list of paths where themes folder reside.  
    62 
    89  */
    63 
    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 /*
    64 /*
   128  * Builds a CpThemeInfo object given theme path and theme name.  It creates the name and 
    65  * 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.
    66  * preview icons for the object.  Creates a new CpThemeInfo objects. Caller takes ownership.
   130  * Returns NULL if can't build the object.
    67  * Returns NULL if can't build the object.
   131  */
    68  */
   157         return NULL;
    94         return NULL;
   158     }
    95     }
   159    
    96    
   160     themeInfo->setName(name);
    97     themeInfo->setName(name);
   161     themeInfo->setItemType(CpThemeInfo::ThemeListItemType_default);
    98     themeInfo->setItemType(CpThemeInfo::ThemeListItemType_default);
       
    99     themeInfo->setItemData(themePath);
   162           
   100           
   163     //Get the icons. Logic is as follow:
   101     //Get the icons. Logic is as follow:
   164     /* 1. If the icon path is in index.theme and the icon exist, use it.
   102     /* 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.
   103      * 2. Otherwise look for the icon in the theme folder.
   166      * 2. If preview icon doesn't exist, use background icon.
   104      * 2. If preview icon doesn't exist, use background icon.
   231     return themeInfo;
   169     return themeInfo;
   232 
   170 
   233 }
   171 }
   234 
   172 
   235 /*! Creates a list of CpThemeInfo objects representing theme information.
   173 /*! Creates a list of CpThemeInfo objects representing theme information.
   236  *  Caller should take ownership of the list.
   174  *  
   237  */
   175  */
   238 QList<CpThemeInfo> CpThemeUtil::buildThemeList()
   176 QList<CpThemeInfo> CpThemeUtil::buildThemeList()
   239 {
   177 {
   240     QList<CpThemeInfo> themeList; 
   178     QList<CpThemeInfo> themeList; 
   241   
   179   
   242     QStringList mThemesPathList = themePathList();
   180     QList<QPair<QString, QString> > mThemesPathList = availableThemes();
   243     
   181     QPair<QString, QString>pair;
   244     foreach (const QString &path, mThemesPathList) {
   182     foreach (pair, mThemesPathList) {
   245         QDir themeDir;
   183         QDir themeDir;
       
   184         QString name = pair.first;
       
   185         QString path = pair.second;
   246         themeDir.setPath( path ) ;
   186         themeDir.setPath( path ) ;
   247         QStringList iconthemeslist;
       
   248         QStringList list = themeDir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot,QDir::Name);
       
   249         CpThemeInfo* themeInfo;
   187         CpThemeInfo* themeInfo;
   250 
   188         if(themeDir.exists("index.theme") &&
   251         if(list.contains("themes", Qt::CaseSensitive )) {
   189           (themeDir.exists("scalable") || themeDir.exists("pixmap") )) {
   252             QDir root(themeDir.path());
   190             themeInfo = buildThemeInfo(path, name);
   253             themeDir.setPath(root.path() + "/themes/icons/") ;
   191             if(themeInfo && !themeInfo->name().isEmpty()) {
   254             iconthemeslist = themeDir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot,QDir::Name);
   192                 themeList.append(*themeInfo);
   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             }
   193             }
   266         }
   194         }
   267     }
   195     }
   268     qSort( themeList );
   196     qSort( themeList );
   269     return themeList;
   197     return themeList;
   273  * Returns the default theme information. 
   201  * Returns the default theme information. 
   274  */
   202  */
   275 CpThemeInfo* CpThemeUtil::defaultTheme()
   203 CpThemeInfo* CpThemeUtil::defaultTheme()
   276 {
   204 {
   277     //static because default theme doesn't change.
   205     //static because default theme doesn't change.
   278     static CpThemeInfo *defaultTheme = new CpThemeInfo();
   206     static CpThemeInfo *defaultTheme = 0;
   279     QString defaultThemeName;
   207     if(!defaultTheme) {
   280     QString defaultThemeRootDir;
   208         defaultTheme = new CpThemeInfo();
       
   209     }
       
   210     QString defaultThemePath;
   281     if(defaultTheme->name().isEmpty()) {
   211     if(defaultTheme->name().isEmpty()) {
   282        
   212        
   283 
   213 
   284 #ifdef Q_OS_SYMBIAN
   214 #ifdef Q_OS_SYMBIAN
   285         CRepository *repository = 0;
   215         CRepository *repository = 0;
   286         TRAP_IGNORE(repository = CRepository::NewL(KServerUid3));
   216         TRAP_IGNORE(repository = CRepository::NewL(KServerUid3));
   287         if (repository) {
   217         if (repository) {
   288             TBuf<256> value;
   218             TBuf<256> value;
   289             if (KErrNone == repository->Get((TUint32)KDefaultThemeNameKey, value)) {
   219             if (KErrNone == repository->Get((TUint32)KDefaultThemeNameKey, value)) {
   290                 QString qvalue((QChar*)value.Ptr(), value.Length());
   220                 QString qvalue((QChar*)value.Ptr(), value.Length());
   291                 defaultThemeName = qvalue.trimmed();
   221                 defaultThemePath = qvalue.trimmed();
   292             }
   222             }
   293             value.Zero();
   223             value.Zero();
   294             if (KErrNone == repository->Get((TUint32)KDefaultThemeRootPathKey, value)) {
   224            delete repository;
   295                 QString qvalue((QChar*)value.Ptr(), value.Length());
   225         }
   296                 defaultThemeRootDir = qvalue.trimmed();
   226           
       
   227 #endif
       
   228         defaultTheme = buildThemeInfo(defaultThemePath);
       
   229 
       
   230     }
       
   231     return defaultTheme;
       
   232 }
       
   233 
       
   234 
       
   235 const QStringList CpThemeUtil::themeDirectories(const QList<CpThemeInfo>& themeInfoList)
       
   236 {
       
   237     QStringList themeDirs;
       
   238     
       
   239     foreach(const CpThemeInfo& themeInfo, themeInfoList) {
       
   240         QDir themePath(themeInfo.itemData());
       
   241         QString topDir;
       
   242         if(themePath.cdUp()) {
       
   243             topDir = themePath.path();
       
   244             if(!themeDirs.contains(topDir)) {
       
   245                 themeDirs.append(topDir);
   297             }
   246             }
   298             else {
   247         }
   299                 defaultThemeRootDir = themePath(defaultThemeName);
   248     }
   300             }
   249     return themeDirs;
   301             value.Zero();
   250 }
   302             delete repository;
   251 
   303         }
   252 const QList< QPair< QString, QString > > CpThemeUtil::availableThemes( )
   304           
   253 {
   305 #endif
   254     
   306         defaultTheme = buildThemeInfo(defaultThemeRootDir, defaultThemeName);
   255     QList<QPair<QString, QString> > result = HbThemeServices::availableThemes();
   307 
   256     return result;
   308     }
   257     
   309     return defaultTheme;
   258 }
   310 }
   259 
   311 
   260