controlpanelplugins/themeplugin/src/cpthemelistmodel.cpp
changeset 22 a5692c68d772
parent 21 2883a5458389
child 26 808caa51b78b
child 31 e79ce701c376
child 40 593f946f4fec
equal deleted inserted replaced
21:2883a5458389 22:a5692c68d772
     1 /*
     1 /*
     2  * ============================================================================
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3  *  Name        : cpthemelistmodel_p.cpp
     3  * All rights reserved.
     4  *  Part of     : LibHb / theme
     4  * This component and the accompanying materials are made available
     5  *  Description : Private implementation of the theme listmodel.
     5  * under the terms of "Eclipse Public License v1.0""
     6  *  Version     : %version: 1 % << Don't touch! Updated by Synergy at check-out.
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  *
     8  *
     8  *  Copyright (c) 2008-2009 Nokia.  All rights reserved.
     9  * Initial Contributors:
     9  *  This material, including documentation and any related computer
    10  * Nokia Corporation - initial contribution.
    10  *  programs, is protected by copyright controlled by Nokia.  All
    11  *
    11  *  rights are reserved.  Copying, including reproducing, storing,
    12  * Contributors:
    12  *  adapting or translating, any or all of this material requires the
    13  *
    13  *  prior written consent of Nokia.  This material also contains
    14  * Description:  
    14  *  confidential information which may not be disclosed to others
    15  *   
    15  *  without the prior written consent of Nokia.
       
    16  * ============================================================================
       
    17  */
    16  */
    18 
    17 
       
    18 #include <QDir>
       
    19 #include <QStringList>
       
    20 #include <QFileSystemWatcher>
       
    21 
       
    22 #include <HbIcon>
       
    23 
    19 #include "cpthemelistmodel.h"
    24 #include "cpthemelistmodel.h"
    20 #include "cpthemechanger_p.h"
    25 #include "cpthemeinfo.h"
    21 #include <QFileSystemWatcher>
    26 #include "cpthemeutil.h"
    22 
    27 
    23 /*
    28 /*
    24     CpThemeChangerModel provides an interface to the data contained in the
    29     CpThemeChangerModel provides an interface to the data contained in the
    25     CpThemeChanger using QAbstractListModel.
    30     theme list using QAbstractListModel.
    26 */
    31 */
    27 
    32 
    28 /*
    33 /*
    29     Constructor
    34     Constructor
    30 */
    35 */
    31 CpThemeListModel::CpThemeListModel(CpThemeChangerPrivate *dd, QObject* parent)
    36 CpThemeListModel::CpThemeListModel(QObject* parent)
    32     : QAbstractListModel(parent)
    37     : QAbstractListModel(parent)
    33     , mThemeChangerPrivate(dd)
    38     , mTopThemeList()
       
    39     , mThemeList()
       
    40     , mBottomThemeList()
       
    41     , mFileWatcher(new QFileSystemWatcher(this))
    34 {
    42 {
    35     connect(dd->mFileWatcher, SIGNAL(directoryChanged(const QString&)),
    43     //Build theme list
    36         this, SLOT(themeListChanged()));
    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);
    37 }
    67 }
    38 
    68 
    39 /*
    69 /*
    40     Destructor
    70     Destructor
    41 */
    71 */
    42 CpThemeListModel::~CpThemeListModel()
    72 CpThemeListModel::~CpThemeListModel()
    43 {
    73 {
       
    74     delete mFileWatcher;
       
    75     mFileWatcher = 0;
    44 
    76 
    45 }
    77 }
    46 
    78 
    47 /*
    79 /*
    48     Reimplemented from QAbstractListModel.
    80     Reimplemented from QAbstractListModel.
    49 */
    81 */
    50 int CpThemeListModel::rowCount(const QModelIndex&) const
    82 int CpThemeListModel::rowCount(const QModelIndex&) const
    51 {
    83 {
    52     return mThemeChangerPrivate->mThemeList.size();
    84     return mTopThemeList.size() +
       
    85         mThemeList.size() +
       
    86         mBottomThemeList.size();
    53 }
    87 }
    54 
    88 
    55 /*
    89 /*
    56     Reimplemented from QAbstractListModel.  Provides the data for Qt::DisplayRole and
    90     Reimplemented from QAbstractListModel. 
    57     Qt::DecorationRole.
       
    58 */
    91 */
    59 QVariant CpThemeListModel::data(const QModelIndex& index, int role) const
    92 QVariant CpThemeListModel::data(const QModelIndex& index, int role) const
    60 {
    93 {
    61     QVariant retVal = QVariant();
    94     QVariant retVal = QVariant();
    62 
    95 
    63     if (index.isValid()) {
    96     if (index.isValid()) {
    64         switch (role) {
    97         // figure out which list we're in and do the appropriate mapping
    65             case Qt::DisplayRole:
    98         int row = index.row();
    66                 retVal = mThemeChangerPrivate->mThemeList.at(index.row()).name;
    99         const QList<CpThemeInfo> *list = 0;
    67                 break;
   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         }
    68 
   113 
    69             case Qt::DecorationRole:
   114         if (list) {
    70                 retVal = mThemeChangerPrivate->mThemeList.at(index.row()).icon;
   115             switch (role) {
    71                 break;
   116                 case Qt::DisplayRole:
       
   117                     retVal = list->at(row).name();
       
   118                     break;
    72 
   119 
    73             case Qt::SizeHintRole:
   120                 case Qt::DecorationRole:
    74                 retVal = mThemeChangerPrivate->mThemeList.at(index.row()).icon.size();
   121                     retVal = list->at(row).icon();
    75                 break;
   122                     break;
    76                 
       
    77         	  case CpThemeChanger::PortraitPreviewRole:
       
    78           	    retVal = mThemeChangerPrivate->mThemeList.at(index.row()).portraitPreviewIcon;
       
    79                 break;
       
    80                 
       
    81             case CpThemeChanger::LandscapePreviewRole:
       
    82                 retVal = mThemeChangerPrivate->mThemeList.at(index.row()).landscapePreviewIcon;
       
    83                 break;
       
    84 
   123 
    85             default:
   124                 case Qt::SizeHintRole:
    86                 // do nothing
   125                     retVal = list->at(row).icon().size();
    87                 qt_noop();
   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             }
    88         }
   148         }
    89     }
   149     }
    90 
   150 
    91     return retVal;
   151     return retVal;
    92 }
   152 }
    99     with dataChanged.
   159     with dataChanged.
   100 */
   160 */
   101 void CpThemeListModel::themeListChanged()
   161 void CpThemeListModel::themeListChanged()
   102 {
   162 {
   103     beginResetModel();
   163     beginResetModel();
   104 
   164     if(!mThemeList.empty()) {
   105     mThemeChangerPrivate->themes();
   165         mThemeList.clear();
   106 
   166     }
       
   167     mThemeList = CpThemeUtil::buildThemeList();
       
   168   
   107     endResetModel();
   169     endResetModel();
   108 }
   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 }