src/hbservers/themechanger/themeselectionlist.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 #include <QSettings>
       
    26 #include <QStringList>
       
    27 #include <QDir>
       
    28 #include <hbinstance.h>
       
    29 #include <hbmenu.h>
       
    30 #include <hbaction.h>
       
    31 #include <hbicon.h>
       
    32 #include <QDebug>
       
    33 
       
    34 #include "themeselectionlist.h"
       
    35 #include "themechangerdefs.h"
       
    36 
       
    37 /**
       
    38  * Constructor
       
    39  */
       
    40 ThemeSelectionList::ThemeSelectionList(
       
    41 #ifdef Q_OS_SYMBIAN
       
    42     ThemeClientSymbian* client
       
    43 #else
       
    44     ThemeClientQt* client
       
    45 #endif
       
    46                         ): 
       
    47                         themelist(new HbListView(this)),
       
    48                         model(new QStandardItemModel(this)),
       
    49                         rightMark(new HbIcon(QString("qtg_small_tick"))),
       
    50                         noMark(new HbIcon(QString(""))),
       
    51                         client(client)
       
    52 {
       
    53     connect(themelist, SIGNAL(activated(const QModelIndex&)),this, SLOT(setChosen(const QModelIndex&)));
       
    54     setWidget(themelist);
       
    55 
       
    56     // Automatic updation of the themelist when some theme is installed or uninstalled
       
    57     // when the themechanger app is open
       
    58     watcher=new QFileSystemWatcher();
       
    59     foreach(const QString &KThemeRootPath, rootPaths()) {
       
    60         if(!KThemeRootPath.contains("/romthemes")){
       
    61         watcher->addPath(KThemeRootPath+"/themes/icons/");
       
    62         }
       
    63     }
       
    64     connect(watcher,SIGNAL(directoryChanged(const QString &)),this,SLOT(updateThemeList(const QString &)));
       
    65     QObject::connect(this,SIGNAL(newThemeSelected(QString)),this,SLOT(sendThemeName(QString)));    
       
    66 }
       
    67 
       
    68 /**
       
    69  * Destructor
       
    70  */
       
    71 ThemeSelectionList::~ThemeSelectionList()
       
    72 {
       
    73     // Set the theme to the applied theme before exiting.
       
    74     setChosen(oldItemIndex);
       
    75     delete noMark;
       
    76 
       
    77     noMark=NULL;
       
    78     delete rightMark;
       
    79     rightMark=NULL;
       
    80 
       
    81     // Remove all the items from model, then delete
       
    82     model->clear();
       
    83     delete model;
       
    84     model=NULL;
       
    85 
       
    86     // Reset the item view
       
    87     themelist->reset();
       
    88     delete themelist;
       
    89     themelist=NULL;
       
    90 }
       
    91 
       
    92 
       
    93 /**
       
    94  * displayThemes
       
    95  */
       
    96 void ThemeSelectionList::displayThemes()
       
    97 {
       
    98     bool entryAdded = false;
       
    99     bool themePresent = false;
       
   100     foreach(const QString &KThemeRootPath, rootPaths()){
       
   101         dir.setPath(KThemeRootPath) ;
       
   102         QStringList list = dir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot,QDir::Name);
       
   103         if(list.contains("themes",Qt::CaseSensitive )) {
       
   104             themePresent = true;
       
   105             QDir root = KThemeRootPath;
       
   106             dir.setPath(root.path()+"/themes/icons/") ;
       
   107             QStringList iconthemeslist=dir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot,QDir::Name);
       
   108             foreach(QString themefolder, iconthemeslist) {
       
   109                 QDir iconThemePath(root.path()+"/themes/icons/"+themefolder);
       
   110                 if(iconThemePath.exists("index.theme")) {
       
   111                     QSettings iniSetting(iconThemePath.path()+"/index.theme",QSettings::IniFormat);
       
   112                     iniSetting.beginGroup("Icon Theme");
       
   113                     QString hidden = iniSetting.value("Hidden").toString();
       
   114                     QString name = iniSetting.value("Name").toString();
       
   115                     iniSetting.endGroup();
       
   116                     if((hidden == "true") ||( hidden == "")||(name!=themefolder) ) {
       
   117                         iconthemeslist.removeOne(themefolder);
       
   118                     }
       
   119                 }
       
   120                 else {
       
   121                      iconthemeslist.removeOne(themefolder);
       
   122                 }
       
   123             
       
   124             }
       
   125             if(!entryAdded){
       
   126                 iconthemeslist.insert(0,"hbdefault"); //adding one default entry
       
   127                 entryAdded = true;
       
   128             }
       
   129             list=iconthemeslist;
       
   130             for (int i=0; i <list.count();i++) {
       
   131                 // Items are populated to create the model
       
   132                 QStandardItem *item=new QStandardItem(list.at(i));
       
   133                 model->appendRow(item);
       
   134                 if((HbInstance::instance()->theme()->name())==(list.at(i))) { 
       
   135                     item->setIcon(rightMark->qicon());
       
   136                     oldItemIndex=item->index();
       
   137                     themelist->setCurrentIndex(oldItemIndex,QItemSelectionModel::Select);
       
   138                 }
       
   139                 else {
       
   140                     item->setIcon(noMark->qicon());
       
   141                 }
       
   142             }
       
   143         }
       
   144     }
       
   145     //    else{//add a case for no theme ,make hbdefault entry
       
   146     if(!themePresent) {
       
   147             QStringList defaultList;
       
   148             defaultList.insert(0,"hbdefault"); //adding one default entry
       
   149             QStandardItem *item=new QStandardItem(defaultList.at(0));
       
   150             model->appendRow(item);
       
   151             item->setIcon(rightMark->qicon());
       
   152             QString themeName=HbInstance::instance()->theme()->name();
       
   153             if (themeName != "hbdefault")
       
   154             {
       
   155                 if (!client->isConnected()) {
       
   156                     bool success = client->connectToServer();
       
   157                     if (success) {
       
   158                         emit newThemeSelected("hbdefault");
       
   159                     }
       
   160                 }
       
   161                 else {
       
   162                     emit newThemeSelected("hbdefault");
       
   163                 }
       
   164             }
       
   165 
       
   166         }
       
   167 
       
   168     // Set the this model for the list 
       
   169     themelist->setModel(model);
       
   170 }
       
   171 
       
   172 /**
       
   173  * setChosen
       
   174  */
       
   175 void ThemeSelectionList::setChosen(const QModelIndex &index)
       
   176 {
       
   177      // Extract the string from the model index
       
   178     QVariant variant=index.data();
       
   179     QString str=variant.toString();
       
   180 
       
   181 #ifdef THEME_CHANGER_TRACES
       
   182     qDebug() << "ThemeSelectionList::Setchosen with ThemeName: "<<str;
       
   183 #endif
       
   184     if(iCurrentTheme != str ) {
       
   185         iCurrentTheme = str;
       
   186         if (!client->isConnected()) {
       
   187             bool success = client->connectToServer();
       
   188             if (success) {
       
   189                 emit newThemeSelected(str); 
       
   190             }
       
   191         }
       
   192         else {
       
   193             emit newThemeSelected(str); 
       
   194         }
       
   195     }
       
   196     else
       
   197     {
       
   198         applySelection(); //double tap //put a tick
       
   199     }
       
   200 }
       
   201 
       
   202 
       
   203 /**
       
   204  * applySelection
       
   205  */
       
   206 void ThemeSelectionList::applySelection()
       
   207 {
       
   208     QModelIndex currentItemIndex = (themelist)->currentIndex();
       
   209     if(oldItemIndex!=currentItemIndex) {
       
   210         (model->itemFromIndex(currentItemIndex))->setIcon(rightMark->qicon());
       
   211         if(oldItemIndex.isValid()) {
       
   212             (model->itemFromIndex(oldItemIndex))->setIcon(noMark->qicon());
       
   213         }
       
   214         oldItemIndex = currentItemIndex;
       
   215     }
       
   216 
       
   217 }
       
   218 
       
   219 
       
   220 /**
       
   221  * event
       
   222  */
       
   223 bool ThemeSelectionList::event(QEvent *e)
       
   224 {
       
   225     if((e->type()==QEvent::ShortcutOverride)||(e->type()==QEvent::WindowDeactivate)) {        
       
   226         themelist->setCurrentIndex(oldItemIndex,QItemSelectionModel::Select);
       
   227         themelist->setFocus();
       
   228         setChosen(oldItemIndex);
       
   229         return true;
       
   230     }
       
   231     return (HbView::event(e));
       
   232 }
       
   233 
       
   234 /**
       
   235  * updateThemeList
       
   236  */
       
   237 void ThemeSelectionList::updateThemeList(const QString &path)
       
   238 {
       
   239     Q_UNUSED(path);
       
   240     model->clear();
       
   241     this->displayThemes();
       
   242 }
       
   243 
       
   244 
       
   245 /**
       
   246  * sendThemeName
       
   247  */
       
   248 void ThemeSelectionList::sendThemeName(const QString& name)
       
   249 {
       
   250     client->changeTheme(name);
       
   251 }
       
   252 
       
   253 /**
       
   254  * \internal
       
   255  */
       
   256 QStringList ThemeSelectionList::rootPaths()
       
   257 {
       
   258     QStringList rootDirs;
       
   259 #if defined(Q_OS_SYMBIAN)
       
   260     rootDirs << "c:/resource/hb"
       
   261              << "z:/resource/hb";
       
   262 #else
       
   263     QString envDir = qgetenv("HB_THEMES_DIR");
       
   264     if (!envDir.isEmpty())
       
   265         rootDirs << envDir;
       
   266 #endif
       
   267 #if defined(Q_OS_MAC)
       
   268     rootDirs << QDir::homePath() + "/Library/UI Extensions for Mobile";
       
   269 #elif !defined(Q_OS_SYMBIAN)
       
   270     rootDirs << HB_RESOURCES_DIR;
       
   271 #endif
       
   272     return rootDirs;
       
   273 }