controlpanelplugins/themeplugin/src/cpthemecontrol.cpp
branchRCL_3
changeset 13 90fe62538f66
equal deleted inserted replaced
12:3fec62e6e7fc 13:90fe62538f66
       
     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 /*!
       
    19     \class CpThemeControl
       
    20     \brief CpThemeControl creates and controls views for Theme Changer plugin and handles
       
    21 	user interaction to preview and change the themes.
       
    22 
       
    23 	This class also connects to the theme server using the HbThemeChanger and sets the theme
       
    24 	based on user interaction with the views.  
       
    25 
       
    26  */
       
    27 
       
    28 #include <QString>
       
    29 #include <QModelIndex>
       
    30 #include <QTranslator>
       
    31 #include <QSortFilterProxyModel>
       
    32 #include <QThread>
       
    33 #include <QTimer>
       
    34 #include <QDesktopServices>
       
    35 #include <QUrl>
       
    36 
       
    37 #include <hbmainwindow.h>
       
    38 #include <hbinstance.h>
       
    39 #include "cpthemechanger.h"
       
    40 
       
    41 #include "cpthemecontrol.h"
       
    42 #include "cpthemelistview.h"
       
    43 #include "cpthemeinfo.h"
       
    44 #include "cpthemelistmodel.h"
       
    45 
       
    46 #include <hbdialog.h>
       
    47 #include <hblabel.h>
       
    48 
       
    49 //time out time before showing a processing dialog.
       
    50 static const int KThemeChangeTimeOutMilliSeconds = 2000;  
       
    51 
       
    52 /*!
       
    53 	Helper function to fetch the main window.
       
    54 */
       
    55 static HbMainWindow *mainWindow() 
       
    56 {
       
    57     QList< HbMainWindow* > mainWindows = hbInstance->allMainWindows();
       
    58     if (!mainWindows.isEmpty()) {
       
    59         return mainWindows.front();
       
    60     }
       
    61     return 0;
       
    62 }
       
    63 
       
    64 /*!
       
    65 	constructor.
       
    66 */
       
    67 CpThemeControl::CpThemeControl(): mThemeListView(0), 
       
    68     mThemeChanger(0),
       
    69     mListModel(0),
       
    70     mThemeChangeFinished(false),
       
    71     mWaitDialog(0)
       
    72 {
       
    73     mThemeChanger = new CpThemeChanger();
       
    74        
       
    75     QTranslator *translator = new QTranslator(this);
       
    76     QString lang = QLocale::system().name();
       
    77     QString path = "Z:/resource/qt/translations/";
       
    78     translator->load("control_panel_" + lang, path);
       
    79     qApp->installTranslator(translator);
       
    80     
       
    81     connect(mThemeChanger,SIGNAL(themeChangeFinished()), this, SLOT(themeChangeFinished()));
       
    82    
       
    83 }
       
    84 
       
    85 
       
    86 /*!
       
    87 	destorys the list view and theme changer objects.
       
    88 */
       
    89 CpThemeControl::~CpThemeControl()
       
    90 {
       
    91     delete mThemeListView;
       
    92     mThemeListView = 0;
       
    93 
       
    94     delete mThemeChanger;
       
    95     mThemeChanger = 0;
       
    96 
       
    97     delete mWaitDialog;
       
    98     mWaitDialog = 0;
       
    99 }
       
   100 
       
   101 /*!
       
   102 	Creates the theme list view.  Gets the themes, creates a model  and sets the list model.
       
   103 */
       
   104 void CpThemeControl::createThemeList()
       
   105 {
       
   106    
       
   107     mThemeListView = new CpThemeListView();
       
   108     
       
   109     if(!mListModel) {
       
   110         mListModel = new CpThemeListModel(this);
       
   111     }
       
   112     
       
   113     // Set the model for theme list.
       
   114     mThemeListView->setModel(mListModel);
       
   115     
       
   116     setActiveThemeIndex();
       
   117     
       
   118     //connect to signal for selecting a list item.
       
   119     connect(mThemeListView,SIGNAL(newThemeSelected(QModelIndex)),
       
   120             this,SLOT(newThemeSelected(QModelIndex)));
       
   121     
       
   122     connect(mThemeListView, SIGNAL(oviClicked()), this, SLOT(getOviTheme()));
       
   123 
       
   124 	//handle signal for list view closing. (e.g Back softkey pressed)
       
   125     connect(mThemeListView,SIGNAL(aboutToClose()),
       
   126             this,SLOT(themeListClosed()));
       
   127 }
       
   128 
       
   129 /*!
       
   130 	returns the instance of themelist view.  Used by control panel to set
       
   131 	the view.  
       
   132 */
       
   133 CpBaseSettingView* CpThemeControl::themeListView()
       
   134 {
       
   135     //If the view was removed before by control panel app, create it again.
       
   136     if(!mThemeListView) {
       
   137         createThemeList();
       
   138     }
       
   139 
       
   140     return mThemeListView;
       
   141 }
       
   142 
       
   143 /*!
       
   144         returns the name of the current theme.
       
   145 */
       
   146 QString CpThemeControl::currentThemeName() const
       
   147 {
       
   148     QString name = "";
       
   149     if(mThemeChanger->currentTheme()) {
       
   150         name = mThemeChanger->currentTheme()->name();
       
   151     }
       
   152     return name;
       
   153 }
       
   154 
       
   155 /*!
       
   156         returns the repersenatative icon of the current theme.
       
   157 */
       
   158 HbIcon CpThemeControl::currentThemeIcon() const
       
   159 {
       
   160     HbIcon icon;
       
   161     if(mThemeChanger->currentTheme()) {
       
   162         icon = mThemeChanger->currentTheme()->icon();
       
   163     }
       
   164     return icon;
       
   165 }
       
   166 
       
   167 /*!
       
   168 	Slot called when a list item of the theme list is selected.
       
   169 */
       
   170 void CpThemeControl::newThemeSelected(const QModelIndex& index)
       
   171 {
       
   172     if(!index.isValid()) {
       
   173         return;
       
   174     }
       
   175     
       
   176     CpThemeInfo themeInfo;
       
   177     QVariant data;
       
   178 
       
   179     //reset the current index to active theme, so that the selection remains on current
       
   180     //theme even though another list item is selected.
       
   181     setActiveThemeIndex();
       
   182     
       
   183     //get the theme name.
       
   184     data = index.data(Qt::DisplayRole);
       
   185     if(data.isValid()) {
       
   186         themeInfo.setName(data.toString());
       
   187     }
       
   188     
       
   189     //get theme path
       
   190     data = index.data(CpThemeListModel::ItemDataRole);
       
   191     if(data.isValid()) {
       
   192         themeInfo.setItemData(data.toString());
       
   193     }
       
   194     
       
   195     applyTheme(themeInfo);
       
   196 
       
   197 }
       
   198 
       
   199 void CpThemeControl::getOviTheme()
       
   200 {
       
   201     QString url = QString("http://lr.ovi.mobi/store/themes");
       
   202     // Launch the URL in the browser and 
       
   203     // continue to Preview if not successful
       
   204     QDesktopServices::openUrl(QUrl(url, QUrl::TolerantMode));
       
   205                               
       
   206 }
       
   207 
       
   208 /*!
       
   209 	Slot called when a Select key is pressed in theme preview view.
       
   210 */
       
   211 void CpThemeControl::applyTheme(const CpThemeInfo& theme)
       
   212 {
       
   213     QThread::currentThread()->setPriority(QThread::HighPriority);  
       
   214     
       
   215     if(mThemeChanger->changeTheme(theme)) {
       
   216     
       
   217         //Start a timer. If theme change takes more than 1 seconds,
       
   218         //we will show a dialog (mWaitDialog) until theme change
       
   219         //is done (themeChangeFinished is called).
       
   220         QTimer::singleShot(KThemeChangeTimeOutMilliSeconds, this, SLOT(themeWaitTimeout()));
       
   221         
       
   222         mThemeChangeFinished = false;
       
   223     } else {
       
   224         //theme change failed, go back to control panel.
       
   225         setActiveThemeIndex();
       
   226     }
       
   227    
       
   228 }
       
   229 
       
   230 /*!
       
   231     Slot for when the theme list view is closed. Ownership of the theme list was given to
       
   232     control panel, so the class won't delete it.
       
   233     
       
   234 */
       
   235 void CpThemeControl::themeListClosed()
       
   236 {
       
   237     mThemeListView = 0;
       
   238 }
       
   239 
       
   240 /*!
       
   241     asks the theme list view to close.  
       
   242 */
       
   243 void CpThemeControl::triggerThemeListClose()
       
   244 {
       
   245     mThemeListView->closeView();
       
   246 }
       
   247 
       
   248 void CpThemeControl::themeChangeTimeout()
       
   249 {
       
   250     //Theme change is finished and idle timer has timed out,
       
   251     //so revert back the application priority to normal
       
   252     //and go back to control panel view.
       
   253     if(mWaitDialog && mWaitDialog->isVisible()) {
       
   254         mWaitDialog->hide();
       
   255     }
       
   256     setActiveThemeIndex();
       
   257     QThread::currentThread()->setPriority(QThread::NormalPriority); 
       
   258 }
       
   259 
       
   260 void CpThemeControl::themeWaitTimeout()
       
   261 {
       
   262     //If after this timeOut, theme change is still in progress,
       
   263     //show a processing dialog.
       
   264     if(!mThemeChangeFinished){
       
   265         if(!mWaitDialog) {
       
   266             mWaitDialog = new HbDialog();
       
   267             mWaitDialog->setDismissPolicy(HbPopup::NoDismiss);
       
   268             mWaitDialog->setModal(false);
       
   269             mWaitDialog->setTimeout(HbPopup::NoTimeout);
       
   270             // Create and set HbLabel as content widget.
       
   271             QString processingText = hbTrId("txt_common_info_processing") + QString("...");
       
   272             HbLabel *label = new HbLabel(processingText);
       
   273             label->setAlignment(Qt::AlignCenter);
       
   274             mWaitDialog->setContentWidget(label);
       
   275         }
       
   276        // as we do not need any signals, calling show() instead of open()
       
   277        mWaitDialog->show();
       
   278     }
       
   279 }
       
   280 
       
   281 void CpThemeControl::themeChangeFinished()
       
   282 {
       
   283     //Theme change is done. Start an idle timer to let the UI
       
   284     //finish remaining tasks.
       
   285     QTimer::singleShot(0, this, SLOT(themeChangeTimeout()));
       
   286     mThemeChangeFinished = true;
       
   287     
       
   288     if(mThemeChanger->currentTheme()) {
       
   289         emit themeUpdated(mThemeChanger->currentTheme()->name(), mThemeChanger->currentTheme()->icon());
       
   290     }
       
   291     
       
   292 }
       
   293 
       
   294 /*!
       
   295  * Private function that sets the current index of theme list view to indicate
       
   296  * the active theme.
       
   297  */
       
   298 void CpThemeControl::setActiveThemeIndex()
       
   299 {
       
   300     //Get the index of current theme.
       
   301     CpThemeListModel* themeListModel = dynamic_cast<CpThemeListModel*>(mListModel);
       
   302     const CpThemeInfo* currentTheme = mThemeChanger->currentTheme();
       
   303     if(themeListModel && currentTheme) {
       
   304         QModelIndex sourceIndex = mListModel->index(themeListModel->indexOf(*currentTheme),0);
       
   305         //set current index.
       
   306         mThemeListView->themeList()->setCurrentIndex(sourceIndex, QItemSelectionModel::SelectCurrent);
       
   307     }
       
   308     else {
       
   309         mThemeListView->themeList()->setCurrentIndex(QModelIndex(), QItemSelectionModel::Clear);
       
   310     }
       
   311 }
       
   312     
       
   313 
       
   314 
       
   315 
       
   316 
       
   317