controlpanelplugins/themeplugin/src/cpthemecontrol.cpp
changeset 22 a5692c68d772
parent 21 2883a5458389
child 26 808caa51b78b
child 40 593f946f4fec
--- a/controlpanelplugins/themeplugin/src/cpthemecontrol.cpp	Thu Jun 10 16:07:41 2010 +0800
+++ b/controlpanelplugins/themeplugin/src/cpthemecontrol.cpp	Fri Jun 25 17:12:20 2010 +0800
@@ -34,6 +34,8 @@
 #include <QSortFilterProxyModel>
 #include <QThread>
 #include <QTimer>
+#include <QDesktopServices>
+#include <QUrl>
 
 #include <hbmainwindow.h>
 #include <hbinstance.h>
@@ -42,6 +44,8 @@
 #include "cpthemecontrol.h"
 #include "cpthemelistview.h"
 #include "cpthemepreview.h"
+#include "cpthemeinfo.h"
+#include "cpthemelistmodel.h"
 
 #include <hbdialog.h>
 #include <hblabel.h>
@@ -67,7 +71,6 @@
     mThemePreview(0), 
     mThemeChanger(0),
     mListModel(0),
-    mSortModel(0),
     mThemeChangeFinished(false),
     mWaitDialog(0)
 {
@@ -79,7 +82,7 @@
     translator->load("control_panel_" + lang, path);
     qApp->installTranslator(translator);
     
-    connect(hbInstance->theme(),SIGNAL(changeFinished()), this, SLOT(themeChangeFinished()));
+    connect(mThemeChanger,SIGNAL(themeChangeFinished()), this, SLOT(themeChangeFinished()));
    
 }
 
@@ -111,16 +114,12 @@
    
     mThemeListView = new CpThemeListView();
     
-    mListModel = &mThemeChanger->model();
+    if(!mListModel) {
+        mListModel = new CpThemeListModel(this);
+    }
     
-    mSortModel = new QSortFilterProxyModel(this);
-    mSortModel->setDynamicSortFilter(true);
-    mSortModel->setSortCaseSensitivity(Qt::CaseInsensitive);
-    mSortModel->sort(0);
-    mSortModel->setSourceModel(mListModel);
-
     // Set the model for theme list.
-    mThemeListView->setModel(mSortModel);
+    mThemeListView->setModel(mListModel);
     mThemeListView->themeList()->setSelectionMode(HbAbstractItemView::SingleSelection);
     
     setActiveThemeIndex();
@@ -154,7 +153,11 @@
 */
 QString CpThemeControl::currentThemeName() const
 {
-    return mThemeChanger->currentTheme().name;
+    QString name = "";
+    if(mThemeChanger->currentTheme()) {
+        name = mThemeChanger->currentTheme()->name();
+    }
+    return name;
 }
 
 /*!
@@ -162,7 +165,11 @@
 */
 HbIcon CpThemeControl::currentThemeIcon() const
 {
-    return mThemeChanger->currentTheme().icon;
+    HbIcon icon;
+    if(mThemeChanger->currentTheme()) {
+        icon = mThemeChanger->currentTheme()->icon();
+    }
+    return icon;
 }
 
 /*!
@@ -173,38 +180,66 @@
     if(!index.isValid()) {
         return;
     }
-
     
-    CpThemeChanger::ThemeInfo themeInfo;
+    CpThemeInfo themeInfo;
     QVariant data;
 
     //reset the current index to active theme, so that the selection remains on current
     //theme even though another list item is selected.
     setActiveThemeIndex();
     
+    // Figure out whether this is a URI and appropriately delegate
+    data = index.data(CpThemeListModel::ItemTypeRole);
+    if(data.isValid() && data.canConvert<CpThemeInfo::ThemeListItemType>()) {
+
+        CpThemeInfo::ThemeListItemType type = data.value<CpThemeInfo::ThemeListItemType>();
+
+        switch (type) {
+            case CpThemeInfo::ThemeListItemType_URL:
+                //get the URL
+                data = index.data(CpThemeListModel::ItemDataRole);
+                if(data.isValid()) {
+                    QString url = data.toString();
+                    // Launch the URL in the browser and 
+                    // continue to Preview if not successful
+                    if (QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode))) {
+                        return;
+                    }
+                }
+                break;
+    
+            case CpThemeInfo::ThemeListItemType_APP:
+                break;
+
+            default:
+                // do nothing
+                qt_noop();
+        }
+    }
+    
     //get the theme name.
     data = index.data(Qt::DisplayRole);
     if(data.isValid()) {
-        themeInfo.name = data.toString();
+        themeInfo.setName(data.toString());
     }
+    
     //get theme icon.
     data = index.data(Qt::DecorationRole);
     if(data.isValid()) {
-        themeInfo.icon = data.value<HbIcon>();
-    }
-    
-    data = index.data(CpThemeChanger::PortraitPreviewRole);
-    if(data.isValid()) {
-        themeInfo.portraitPreviewIcon = data.value<HbIcon>();
+        themeInfo.setIcon(data.value<HbIcon>());
     }
     
-    data = index.data(CpThemeChanger::LandscapePreviewRole);
+    data = index.data(CpThemeListModel::PortraitPreviewRole);
     if(data.isValid()) {
-        themeInfo.landscapePreviewIcon = data.value<HbIcon>();
+        themeInfo.setPortraitPreviewIcon(data.value<HbIcon>());
     }
-        
-        
-    //Set up the theme preview and set it to
+    
+    data = index.data(CpThemeListModel::LandscapePreviewRole);
+    if(data.isValid()) {
+        themeInfo.setLandscapePreviewIcon(data.value<HbIcon>());
+    }
+    
+   //Set up the theme preview and set it to
     //the current view of main window.
     HbMainWindow*  mWindow = ::mainWindow();
    
@@ -221,9 +256,9 @@
         mThemePreview->setThemeInfo(themeInfo);
     }
     mThemePreview->setTitle(hbTrId("txt_cp_title_control_panel"));
-	  	
+
     mWindow->setCurrentView(mThemePreview);
-	
+
 }
 
 /*!
@@ -231,22 +266,21 @@
 */
 void CpThemeControl::themeApplied(const QString& theme)
 {
-    bool success = false;
-
-    success = mThemeChanger->connectToServer();
+    QThread::currentThread()->setPriority(QThread::HighPriority);  
+    
+    if(mThemeChanger->changeTheme(theme)) {
     
-    if (success) {
-        QThread::currentThread()->setPriority(QThread::HighPriority);  
-        mThemeChanger->changeTheme(theme);
-        emit themeUpdated(mThemeChanger->currentTheme().name, mThemeChanger->currentTheme().icon);
+        //Start a timer. If theme change takes more than 1 seconds,
+        //we will show a dialog (mWaitDialog) until theme change
+        //is done (themeChangeFinished is called).
+        QTimer::singleShot(1000, this, SLOT(themeWaitTimeout()));
+        
+        mThemeChangeFinished = false;
+    } else {
+        //theme change failed, go back to control panel.
+        previewClosed();
+        triggerThemeListClose();
     }
-    
-    //Start a timer. If theme change takes more than 1 seconds,
-    //we will show a dialog (mWaitDialog) until theme change
-    //is done (themeChangeFinished is called).
-    QTimer::singleShot(1000, this, SLOT(themeWaitTimeout()));
-        
-    mThemeChangeFinished = false;
    
 }
 
@@ -314,8 +348,9 @@
     {
         if(!mWaitDialog) {
             mWaitDialog = new HbDialog();
+            mWaitDialog->setDismissPolicy(HbPopup::NoDismiss);
             mWaitDialog->setModal(false);
-            mWaitDialog->setDismissPolicy(HbPopup::NoDismiss);
+            mWaitDialog->setTimeout(HbPopup::NoTimeout);
             //TODO: need localized text for Hb Dialog
             // Create and set HbLabel as content widget.
             HbLabel *label = new HbLabel("Processing ...");
@@ -334,6 +369,10 @@
     QTimer::singleShot(0, this, SLOT(themeChangeTimeout()));
     mThemeChangeFinished = true;
     
+    if(mThemeChanger->currentTheme()) {
+        emit themeUpdated(mThemeChanger->currentTheme()->name(), mThemeChanger->currentTheme()->icon());
+    }
+    
 }
 
 /*!
@@ -343,11 +382,13 @@
 void CpThemeControl::setActiveThemeIndex()
 {
     //Get the index of current theme.
-    QModelIndex sourceIndex = mListModel->index(mThemeChanger->indexOf(mThemeChanger->currentTheme()),0);
-    //Map it to the sort model index.
-    QModelIndex sortedIndex = mSortModel->mapFromSource(sourceIndex);
-    //set current index.
-    mThemeListView->themeList()->setCurrentIndex(sortedIndex, QItemSelectionModel::SelectCurrent);
+    CpThemeListModel* themeListModel = dynamic_cast<CpThemeListModel*>(mListModel);
+    const CpThemeInfo* currentTheme = mThemeChanger->currentTheme();
+    if(themeListModel && currentTheme) {
+        QModelIndex sourceIndex = mListModel->index(themeListModel->indexOf(*currentTheme),0);
+        //set current index.
+        mThemeListView->themeList()->setCurrentIndex(sourceIndex, QItemSelectionModel::SelectCurrent);
+    }
 }