controlpanelplugins/themeplugin/src/cpthemelistmodel.cpp
changeset 22 a5692c68d772
parent 21 2883a5458389
child 26 808caa51b78b
child 31 e79ce701c376
child 40 593f946f4fec
--- a/controlpanelplugins/themeplugin/src/cpthemelistmodel.cpp	Thu Jun 10 16:07:41 2010 +0800
+++ b/controlpanelplugins/themeplugin/src/cpthemelistmodel.cpp	Fri Jun 25 17:12:20 2010 +0800
@@ -1,39 +1,69 @@
 /*
- * ============================================================================
- *  Name        : cpthemelistmodel_p.cpp
- *  Part of     : LibHb / theme
- *  Description : Private implementation of the theme listmodel.
- *  Version     : %version: 1 % << Don't touch! Updated by Synergy at check-out.
+ * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of "Eclipse Public License v1.0""
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  *
- *  Copyright (c) 2008-2009 Nokia.  All rights reserved.
- *  This material, including documentation and any related computer
- *  programs, is protected by copyright controlled by Nokia.  All
- *  rights are reserved.  Copying, including reproducing, storing,
- *  adapting or translating, any or all of this material requires the
- *  prior written consent of Nokia.  This material also contains
- *  confidential information which may not be disclosed to others
- *  without the prior written consent of Nokia.
- * ============================================================================
+ * Initial Contributors:
+ * Nokia Corporation - initial contribution.
+ *
+ * Contributors:
+ *
+ * Description:  
+ *   
  */
 
+#include <QDir>
+#include <QStringList>
+#include <QFileSystemWatcher>
+
+#include <HbIcon>
+
 #include "cpthemelistmodel.h"
-#include "cpthemechanger_p.h"
-#include <QFileSystemWatcher>
+#include "cpthemeinfo.h"
+#include "cpthemeutil.h"
 
 /*
     CpThemeChangerModel provides an interface to the data contained in the
-    CpThemeChanger using QAbstractListModel.
+    theme list using QAbstractListModel.
 */
 
 /*
     Constructor
 */
-CpThemeListModel::CpThemeListModel(CpThemeChangerPrivate *dd, QObject* parent)
+CpThemeListModel::CpThemeListModel(QObject* parent)
     : QAbstractListModel(parent)
-    , mThemeChangerPrivate(dd)
+    , mTopThemeList()
+    , mThemeList()
+    , mBottomThemeList()
+    , mFileWatcher(new QFileSystemWatcher(this))
 {
-    connect(dd->mFileWatcher, SIGNAL(directoryChanged(const QString&)),
-        this, SLOT(themeListChanged()));
+    //Build theme list
+    mThemeList = CpThemeUtil::buildThemeList();
+    
+    //Look into theme paths and add a file watcher for it
+    //to get notified when themes are added.
+    QStringList themesPathList = CpThemeUtil::themePathList();
+    foreach (const QString &path, themesPathList) {
+        QDir themeDir;
+        themeDir.setPath( path ) ;
+        QStringList list = themeDir.entryList(QDir::AllDirs|QDir::NoDotAndDotDot,QDir::Name);
+        if(list.contains("themes", Qt::CaseSensitive )) {
+            mFileWatcher->addPath(themeDir.path() + "/themes/");
+        }
+    }
+    connect(mFileWatcher, SIGNAL(directoryChanged(const QString&)),
+           this, SLOT(themeListChanged()));
+       
+    // data for the list which appears after the themes:
+    CpThemeInfo fetchFromStore;
+    fetchFromStore.setName(hbTrId("txt_cp_list_get_more_tones"));
+    fetchFromStore.setItemType(CpThemeInfo::ThemeListItemType_URL);
+    fetchFromStore.setItemData(QString("http://lr.ovi.mobi/store/themes"));
+    fetchFromStore.setIcon(HbIcon("qtg_large_ovistore"));
+    mBottomThemeList.append(fetchFromStore);
 }
 
 /*
@@ -41,6 +71,8 @@
 */
 CpThemeListModel::~CpThemeListModel()
 {
+    delete mFileWatcher;
+    mFileWatcher = 0;
 
 }
 
@@ -49,42 +81,70 @@
 */
 int CpThemeListModel::rowCount(const QModelIndex&) const
 {
-    return mThemeChangerPrivate->mThemeList.size();
+    return mTopThemeList.size() +
+        mThemeList.size() +
+        mBottomThemeList.size();
 }
 
 /*
-    Reimplemented from QAbstractListModel.  Provides the data for Qt::DisplayRole and
-    Qt::DecorationRole.
+    Reimplemented from QAbstractListModel. 
 */
 QVariant CpThemeListModel::data(const QModelIndex& index, int role) const
 {
     QVariant retVal = QVariant();
 
     if (index.isValid()) {
-        switch (role) {
-            case Qt::DisplayRole:
-                retVal = mThemeChangerPrivate->mThemeList.at(index.row()).name;
-                break;
+        // figure out which list we're in and do the appropriate mapping
+        int row = index.row();
+        const QList<CpThemeInfo> *list = 0;
+        if (row < mTopThemeList.size()) {
+            list = &mTopThemeList;
+        } else {
+            row -= mTopThemeList.size();
+            if ( row < mThemeList.size() ) {
+                list = &mThemeList;
+            } else {
+                row -= mThemeList.size();
+                if ( row < mBottomThemeList.size() ) {
+                    list = &mBottomThemeList;
+                }
+            }
+        }
 
-            case Qt::DecorationRole:
-                retVal = mThemeChangerPrivate->mThemeList.at(index.row()).icon;
-                break;
+        if (list) {
+            switch (role) {
+                case Qt::DisplayRole:
+                    retVal = list->at(row).name();
+                    break;
 
-            case Qt::SizeHintRole:
-                retVal = mThemeChangerPrivate->mThemeList.at(index.row()).icon.size();
-                break;
-                
-        	  case CpThemeChanger::PortraitPreviewRole:
-          	    retVal = mThemeChangerPrivate->mThemeList.at(index.row()).portraitPreviewIcon;
-                break;
-                
-            case CpThemeChanger::LandscapePreviewRole:
-                retVal = mThemeChangerPrivate->mThemeList.at(index.row()).landscapePreviewIcon;
-                break;
+                case Qt::DecorationRole:
+                    retVal = list->at(row).icon();
+                    break;
 
-            default:
-                // do nothing
-                qt_noop();
+                case Qt::SizeHintRole:
+                    retVal = list->at(row).icon().size();
+                    break;
+                    
+                case PortraitPreviewRole:
+          	        retVal = list->at(row).portraitPreviewIcon();
+                    break;
+                    
+                case LandscapePreviewRole:
+                    retVal = list->at(row).landscapePreviewIcon();
+                    break;
+                    
+                case ItemDataRole:
+                    retVal = list->at(row).itemData();
+                    break;
+
+                case ItemTypeRole:
+                    retVal = QVariant::fromValue<CpThemeInfo::ThemeListItemType>(list->at(row).itemType());
+                    break;
+
+                default:
+                    // do nothing
+                    qt_noop();
+            }
         }
     }
 
@@ -101,8 +161,17 @@
 void CpThemeListModel::themeListChanged()
 {
     beginResetModel();
-
-    mThemeChangerPrivate->themes();
-
+    if(!mThemeList.empty()) {
+        mThemeList.clear();
+    }
+    mThemeList = CpThemeUtil::buildThemeList();
+  
     endResetModel();
 }
+/*!
+ * Returns index of theme infor within the theme list.
+ */
+int CpThemeListModel::indexOf(const CpThemeInfo& theme) const
+{
+    return mThemeList.indexOf(theme);
+}