controlpanelplugins/themeplugin/src/cpthemelistmodel.cpp
branchGCC_SURGE
changeset 27 848a3adde87f
parent 25 19394c261aa5
child 26 808caa51b78b
child 31 e79ce701c376
child 40 593f946f4fec
equal deleted inserted replaced
16:8c9427577f2a 27:848a3adde87f
       
     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 <QDir>
       
    19 #include <QStringList>
       
    20 #include <QFileSystemWatcher>
       
    21 
       
    22 #include <HbIcon>
       
    23 
       
    24 #include "cpthemelistmodel.h"
       
    25 #include "cpthemeinfo.h"
       
    26 #include "cpthemeutil.h"
       
    27 
       
    28 /*
       
    29     CpThemeChangerModel provides an interface to the data contained in the
       
    30     theme list using QAbstractListModel.
       
    31 */
       
    32 
       
    33 /*
       
    34     Constructor
       
    35 */
       
    36 CpThemeListModel::CpThemeListModel(QObject* parent)
       
    37     : QAbstractListModel(parent)
       
    38     , mTopThemeList()
       
    39     , mThemeList()
       
    40     , mBottomThemeList()
       
    41     , mFileWatcher(new QFileSystemWatcher(this))
       
    42 {
       
    43     //Build theme list
       
    44     mThemeList = CpThemeUtil::buildThemeList();
       
    45     
       
    46     //Look into theme paths and add a file watcher for it
       
    47     //to get notified when themes are added.
       
    48     QStringList themesPathList = CpThemeUtil::themePathList();
       
    49     foreach (const QString &path, themesPathList) {
       
    50         QDir themeDir;
       
    51         themeDir.setPath( path ) ;
       
    52         QStringList list = themeDir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot,QDir::Name);
       
    53         if(list.contains("themes", Qt::CaseSensitive )) {
       
    54             mFileWatcher->addPath(themeDir.path() + "/themes/");
       
    55         }
       
    56     }
       
    57     connect(mFileWatcher, SIGNAL(directoryChanged(const QString&)),
       
    58            this, SLOT(themeListChanged()));
       
    59        
       
    60     // data for the list which appears after the themes:
       
    61     CpThemeInfo fetchFromStore;
       
    62     fetchFromStore.setName(hbTrId("txt_cp_list_get_more_tones"));
       
    63     fetchFromStore.setItemType(CpThemeInfo::ThemeListItemType_URL);
       
    64     fetchFromStore.setItemData(QString("http://lr.ovi.mobi/store/themes"));
       
    65     fetchFromStore.setIcon(HbIcon("qtg_large_ovistore"));
       
    66     mBottomThemeList.append(fetchFromStore);
       
    67 }
       
    68 
       
    69 /*
       
    70     Destructor
       
    71 */
       
    72 CpThemeListModel::~CpThemeListModel()
       
    73 {
       
    74     delete mFileWatcher;
       
    75     mFileWatcher = 0;
       
    76 
       
    77 }
       
    78 
       
    79 /*
       
    80     Reimplemented from QAbstractListModel.
       
    81 */
       
    82 int CpThemeListModel::rowCount(const QModelIndex&) const
       
    83 {
       
    84     return mTopThemeList.size() +
       
    85         mThemeList.size() +
       
    86         mBottomThemeList.size();
       
    87 }
       
    88 
       
    89 /*
       
    90     Reimplemented from QAbstractListModel. 
       
    91 */
       
    92 QVariant CpThemeListModel::data(const QModelIndex& index, int role) const
       
    93 {
       
    94     QVariant retVal = QVariant();
       
    95 
       
    96     if (index.isValid()) {
       
    97         // figure out which list we're in and do the appropriate mapping
       
    98         int row = index.row();
       
    99         const QList<CpThemeInfo> *list = 0;
       
   100         if (row < mTopThemeList.size()) {
       
   101             list = &mTopThemeList;
       
   102         } else {
       
   103             row -= mTopThemeList.size();
       
   104             if ( row < mThemeList.size() ) {
       
   105                 list = &mThemeList;
       
   106             } else {
       
   107                 row -= mThemeList.size();
       
   108                 if ( row < mBottomThemeList.size() ) {
       
   109                     list = &mBottomThemeList;
       
   110                 }
       
   111             }
       
   112         }
       
   113 
       
   114         if (list) {
       
   115             switch (role) {
       
   116                 case Qt::DisplayRole:
       
   117                     retVal = list->at(row).name();
       
   118                     break;
       
   119 
       
   120                 case Qt::DecorationRole:
       
   121                     retVal = list->at(row).icon();
       
   122                     break;
       
   123 
       
   124                 case Qt::SizeHintRole:
       
   125                     retVal = list->at(row).icon().size();
       
   126                     break;
       
   127                     
       
   128                 case PortraitPreviewRole:
       
   129           	        retVal = list->at(row).portraitPreviewIcon();
       
   130                     break;
       
   131                     
       
   132                 case LandscapePreviewRole:
       
   133                     retVal = list->at(row).landscapePreviewIcon();
       
   134                     break;
       
   135                     
       
   136                 case ItemDataRole:
       
   137                     retVal = list->at(row).itemData();
       
   138                     break;
       
   139 
       
   140                 case ItemTypeRole:
       
   141                     retVal = QVariant::fromValue<CpThemeInfo::ThemeListItemType>(list->at(row).itemType());
       
   142                     break;
       
   143 
       
   144                 default:
       
   145                     // do nothing
       
   146                     qt_noop();
       
   147             }
       
   148         }
       
   149     }
       
   150 
       
   151     return retVal;
       
   152 }
       
   153 
       
   154 /*
       
   155     Responds appropriately when the underlying data in the theme changer is modified.
       
   156 
       
   157     Unfortunately the directory watcher from QFileWatcher only says when something changed
       
   158     not what changed.  Therefore the model is considered reset rather than having rows
       
   159     with dataChanged.
       
   160 */
       
   161 void CpThemeListModel::themeListChanged()
       
   162 {
       
   163     beginResetModel();
       
   164     if(!mThemeList.empty()) {
       
   165         mThemeList.clear();
       
   166     }
       
   167     mThemeList = CpThemeUtil::buildThemeList();
       
   168   
       
   169     endResetModel();
       
   170 }
       
   171 /*!
       
   172  * Returns index of theme infor within the theme list.
       
   173  */
       
   174 int CpThemeListModel::indexOf(const CpThemeInfo& theme) const
       
   175 {
       
   176     return mThemeList.indexOf(theme);
       
   177 }