qtinternetradio/ui/src/ircategoryview.cpp
changeset 0 09774dfdd46b
child 2 2e1adbfc62af
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     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 <hbaction.h>
       
    19 #include <hblistview.h>
       
    20 
       
    21 #include "irapplication.h"
       
    22 #include "irabstractviewmanager.h"
       
    23 #include "ircategoryview.h"
       
    24 #include "irstationsview.h"
       
    25 #include "irqnetworkcontroller.h"
       
    26 #include "ircategorymodel.h"
       
    27 
       
    28 //                                    public functions             
       
    29 
       
    30 /*
       
    31  * Description : constructor
       
    32  */
       
    33 IRCategoryView::IRCategoryView(IRApplication* aApplication, TIRViewId aViewId) 
       
    34                                : IrAbstractListViewBase(aApplication, aViewId),
       
    35                                  iWaitDialog(NULL),
       
    36                                  iViewParameter(EIRViewPara_InvalidId),
       
    37                                  iLastSelectItem(0)
       
    38 {	
       
    39     iModel = new IRCategoryModel(this);
       
    40     iListView->setModel(iModel);
       
    41     
       
    42     connect(iModel, SIGNAL(dataChanged()), this, SLOT(dataChanged()));
       
    43     
       
    44     connect(iNetworkController, SIGNAL(networkRequestNotified(IRQNetworkEvent)),
       
    45             this, SLOT(networkRequestNotified(IRQNetworkEvent)));
       
    46 }
       
    47 
       
    48 /*
       
    49  * Description : destructor
       
    50  */
       
    51 IRCategoryView::~IRCategoryView()
       
    52 {
       
    53     delete iWaitDialog;
       
    54     iWaitDialog = NULL;
       
    55 }
       
    56 
       
    57 /*
       
    58  * Description : virtual functions from base class IRBaseView. 
       
    59  *               handle view commands
       
    60  * 
       
    61  * see also    : IRBaseView::handleCommand
       
    62  */
       
    63 TIRHandleResult IRCategoryView::handleCommand(TIRViewCommand aCommand, TIRViewCommandReason aReason)
       
    64 {
       
    65     Q_UNUSED(aReason);
       
    66     TIRHandleResult ret = IrAbstractListViewBase::handleCommand(aCommand, aReason);
       
    67     
       
    68     switch (aCommand)
       
    69     {
       
    70         case EIR_ViewCommand_ACTIVATED:
       
    71         break;
       
    72         
       
    73         case EIR_ViewCommand_DEACTIVATE:
       
    74             if (iWaitDialog)
       
    75             {
       
    76                 iWaitDialog->close();
       
    77                 ret = EIR_NoDefault;
       
    78             }
       
    79             break;
       
    80             
       
    81         default:
       
    82             break;
       
    83     }
       
    84     
       
    85     return ret;
       
    86 }
       
    87 
       
    88 void IRCategoryView::launchAction()
       
    89 {    
       
    90     bool ret = false;
       
    91     switch(iViewParameter)
       
    92     {
       
    93         case EIRViewPara_Genre:
       
    94             ret = iIsdsClient->isdsIsCategoryCached(IRQIsdsClient::EGenre);
       
    95             break;
       
    96         case EIRViewPara_Language:
       
    97             ret = iIsdsClient->isdsIsCategoryCached(IRQIsdsClient::ELanguages);
       
    98             break;
       
    99         case EIRViewPara_Country:
       
   100             ret = iIsdsClient->isdsIsCategoryCached(IRQIsdsClient::ECountries);
       
   101             break;
       
   102 
       
   103         default:
       
   104             break;
       
   105     }
       
   106     
       
   107     if ( !ret )
       
   108     {
       
   109         setUseNetworkReason(EIR_UseNetwork_StartingView);
       
   110         iApplication->verifyNetworkConnectivity();
       
   111     }   
       
   112     else
       
   113     {
       
   114         IRQIsdsClient::IRQIsdsClientInterfaceIDs request =
       
   115                 IRQIsdsClient::EGenre;
       
   116 
       
   117         switch (iViewParameter)
       
   118         {
       
   119             case EIRViewPara_Genre:
       
   120                 request = IRQIsdsClient::EGenre;
       
   121                 break;
       
   122 
       
   123             case EIRViewPara_Language:
       
   124                 request = IRQIsdsClient::ELanguages;
       
   125                 break;
       
   126 
       
   127             case EIRViewPara_Country:
       
   128                 request = IRQIsdsClient::ECountries;
       
   129                 break;
       
   130 
       
   131             default:
       
   132                 break;
       
   133         }
       
   134 
       
   135         connectToIsdsClient();
       
   136         bool cache = false;
       
   137         iIsdsClient->isdsCategoryRequest(request, cache);
       
   138     }
       
   139 }
       
   140 
       
   141 void IRCategoryView::setViewParameter(TIRViewParameter aParameter)
       
   142 {
       
   143     iViewParameter = aParameter;
       
   144 }
       
   145 
       
   146 TIRViewParameter IRCategoryView::getViewParameter() const
       
   147 {
       
   148     return iViewParameter;
       
   149 }
       
   150 
       
   151 void IRCategoryView::loadCategory(IRQIsdsClient::IRQIsdsClientInterfaceIDs aCategory)
       
   152 {
       
   153     connectToIsdsClient();
       
   154     
       
   155     bool cache = false;
       
   156     switch (aCategory)
       
   157     {
       
   158     case IRQIsdsClient::EGenre : 
       
   159         setHeadingText(tr("Genre"));
       
   160         if (EIRViewPara_Genre != getViewParameter())
       
   161         {
       
   162             //the view is used for other categories other than Genre
       
   163             resetCurrentItem();
       
   164         }
       
   165         setViewParameter(EIRViewPara_Genre);
       
   166         iIsdsClient->isdsCategoryRequest(IRQIsdsClient::EGenre, cache);
       
   167         break;
       
   168         
       
   169     case IRQIsdsClient::ELanguages : 
       
   170         setHeadingText(hbTrId("txt_irad_list_stations_by_language"));
       
   171         if (EIRViewPara_Language != getViewParameter())
       
   172         {
       
   173             //the view is used for other categories other than Language
       
   174             resetCurrentItem();
       
   175         }
       
   176         setViewParameter(EIRViewPara_Language);
       
   177         iIsdsClient->isdsCategoryRequest(IRQIsdsClient::ELanguages, cache);
       
   178         break;
       
   179         
       
   180     case IRQIsdsClient::ECountries :
       
   181         setHeadingText(hbTrId("txt_irad_list_stations_by_country_region"));
       
   182         if (EIRViewPara_Country != getViewParameter())
       
   183         {
       
   184             //the view is used for other categories other than Country
       
   185             resetCurrentItem();
       
   186         }
       
   187         setViewParameter(EIRViewPara_Country);
       
   188         iIsdsClient->isdsCategoryRequest(IRQIsdsClient::ECountries, cache);
       
   189         break;
       
   190         
       
   191     default:
       
   192         Q_ASSERT(false);
       
   193         break;
       
   194     }
       
   195     
       
   196     if (!cache)
       
   197     {
       
   198         createWaitDialog(hbTrId("txt_common_info_loading"));
       
   199     }
       
   200 }
       
   201 
       
   202 void IRCategoryView::storeCurrentItem()
       
   203 {
       
   204     iLastSelectItem = iListView->currentIndex().row();
       
   205 }
       
   206 
       
   207 void IRCategoryView::resetCurrentItem()
       
   208 {
       
   209     iLastSelectItem = 0;
       
   210     IRBaseView *stationView = getViewManager()->getView(EIRView_StationsView);
       
   211     if( stationView )
       
   212     {
       
   213         static_cast<IrAbstractListViewBase*>(stationView)->resetCurrentItem();
       
   214     }
       
   215 }
       
   216 
       
   217 /*
       
   218  * Description : slot function for active network event
       
   219  * Parameters  : aEvent, see the definition of IRQNetworkEvent
       
   220  */
       
   221 void IRCategoryView::networkRequestNotified(IRQNetworkEvent aEvent)
       
   222 {
       
   223     Q_ASSERT(iViewParameter != EIRViewPara_InvalidId);
       
   224     
       
   225     if (getViewManager()->currentView() != this)
       
   226     {
       
   227         return;
       
   228     }
       
   229     
       
   230     switch (aEvent)
       
   231     {
       
   232     case EIRQNetworkConnectionEstablished:
       
   233         if (EIR_UseNetwork_StartingView == getUseNetworkReason())
       
   234         {
       
   235             IRQIsdsClient::IRQIsdsClientInterfaceIDs request = IRQIsdsClient::EGenre;
       
   236         
       
   237             switch (iViewParameter)
       
   238             {
       
   239             case EIRViewPara_Genre:
       
   240                 request = IRQIsdsClient::EGenre;
       
   241                 break;
       
   242 
       
   243             case EIRViewPara_Language:
       
   244                 request = IRQIsdsClient::ELanguages;
       
   245                 break;
       
   246 
       
   247             case EIRViewPara_Country:
       
   248                 request = IRQIsdsClient::ECountries;
       
   249                 break;
       
   250 
       
   251             default:
       
   252                 break;
       
   253             }
       
   254 
       
   255             connectToIsdsClient();
       
   256             bool cache = false;
       
   257             iIsdsClient->isdsCategoryRequest(request, cache);
       
   258         }
       
   259         //for there may be some cache, and when we click, we need to handle here        
       
   260         else if ( EIR_UseNetwork_SelectItem == getUseNetworkReason())
       
   261         {
       
   262             iApplication->closeConnectingDialog();
       
   263             handleItemSelected();               
       
   264         }        
       
   265         
       
   266         setUseNetworkReason(EIR_UseNetwork_NoReason);
       
   267         break;
       
   268        
       
   269     case EIRQConnectingCancelled:
       
   270     case EIRQDisplayNetworkMessageNoConnectivity:
       
   271         if (iListView->model()->rowCount() == 0)
       
   272         {
       
   273             getViewManager()->activateView(EIRView_MainView);
       
   274         }
       
   275         else
       
   276         {
       
   277             setCheckedAction();
       
   278         }
       
   279         
       
   280         break;
       
   281         
       
   282     default:
       
   283         setCheckedAction();
       
   284         break;
       
   285     }
       
   286 }
       
   287 
       
   288 
       
   289 //                                 slots functions 
       
   290 
       
   291 void IRCategoryView::itemAboutToBeSelected(bool &aNeedNetwork)
       
   292 {
       
   293  
       
   294     if (iIsdsClient && iListView)
       
   295     {
       
   296         int index = iListView->currentIndex().row();         
       
   297         if (iIsdsClient->isdsIsCategoryBanner())
       
   298         {
       
   299             ++index;
       
   300         }       
       
   301          
       
   302         aNeedNetwork = !iIsdsClient->isdsIsChannelCached(index);              
       
   303     }       
       
   304 }
       
   305 /*
       
   306  * Description  : slot function when an item in a list is clicked. call isds client to get 
       
   307  *                channel list under the clicked category.
       
   308  * Parameters   : aItem  : pointer to the clicked item
       
   309  * Return       : None
       
   310  */
       
   311 void IRCategoryView::handleItemSelected()
       
   312 {
       
   313     if (iIsdsClient && iListView)
       
   314     {
       
   315         int index = iListView->currentIndex().row();
       
   316         if ( iLastSelectItem != index )
       
   317         {
       
   318             IRBaseView *stationView = getViewManager()->getView(EIRView_StationsView);
       
   319             if( stationView )
       
   320             {
       
   321                 static_cast<IrAbstractListViewBase*>(stationView)->resetCurrentItem();
       
   322             }
       
   323         }
       
   324         iLastSelectItem = index;
       
   325         if (iIsdsClient->isdsIsCategoryBanner())
       
   326         {
       
   327             ++index;
       
   328         }
       
   329         
       
   330         const QString& headingText = iModel->categoryName(iListView->currentIndex());
       
   331         IRStationsView *stationsView =
       
   332         static_cast<IRStationsView*>(getViewManager()->getView(EIRView_StationsView, true));
       
   333         stationsView->loadCategoryStations(index, headingText);
       
   334     }
       
   335 }
       
   336 
       
   337 /*
       
   338  * Description : data is received from isds client. Load the data to the list widget and display
       
   339  *               the category view
       
   340  * 
       
   341  */
       
   342 void IRCategoryView::dataChanged()
       
   343 {
       
   344     disconnectIsdsClient();
       
   345     iApplication->closeConnectingDialog();
       
   346     if (iWaitDialog)
       
   347     {
       
   348         iWaitDialog->close();
       
   349     }
       
   350 
       
   351     iListView->reset();
       
   352     iListView->setCurrentIndex(iModel->index(iLastSelectItem));
       
   353     iListView->scrollTo(iModel->index(iLastSelectItem));
       
   354     getViewManager()->activateView(this);
       
   355 }
       
   356 
       
   357 /*
       
   358  * Description : slot function for canceling request.
       
   359  *               Before data is received, cancel requests through isds client
       
   360  */
       
   361 void IRCategoryView::cancelRequest()
       
   362 {
       
   363     iIsdsClient->isdsCancelRequest();
       
   364     disconnectIsdsClient();
       
   365 }
       
   366 
       
   367 /*
       
   368  * Description  : slot function for operation failure. The signal is emitted by isds client
       
   369  * Parameters   : aError : see the definition of IRQError
       
   370  */
       
   371 void IRCategoryView::operationException(IRQError aError)
       
   372 {
       
   373     Q_UNUSED(aError);
       
   374     iApplication->closeConnectingDialog();
       
   375     
       
   376     if (iWaitDialog)
       
   377     {
       
   378         iWaitDialog->close();
       
   379     }
       
   380     
       
   381     disconnectIsdsClient();
       
   382     
       
   383     popupNote(hbTrId("txt_irad_info_failed_to_connect"), HbMessageBox::MessageTypeWarning);
       
   384     
       
   385     //if category view is starting view and failed to get data from ISDS server, back to main view
       
   386     if (getViewManager()->currentView() == this &&
       
   387         iListView->model()->rowCount() == 0)
       
   388     {
       
   389         getViewManager()->activateView(EIRView_MainView);
       
   390     }
       
   391 }
       
   392 
       
   393 //                                        private functions
       
   394 
       
   395 /*
       
   396  * Description : Show a wait dialog to inform user that data is being loaded. If wait dialog doesn't
       
   397  *               exist yet, create first
       
   398  */
       
   399 void IRCategoryView::createWaitDialog(const QString &aText)
       
   400 {
       
   401     if (!iWaitDialog)
       
   402     {
       
   403         iWaitDialog = new HbMessageBox(tr(""), HbMessageBox::MessageTypeInformation);
       
   404         //iWaitDialog->setTimeout(HbPopupBase::NoTimeout);
       
   405         iWaitDialog->setTimeout(HbPopup::NoTimeout); // JM: changed in w47 Orbit
       
   406         iWaitDialog->setModal(true);
       
   407         //iWaitDialog->setDismissPolicy(HbPopupBase::NoDismiss);
       
   408         iWaitDialog->setDismissPolicy(HbPopup::NoDismiss); // JM: changed in w47 Orbit
       
   409         HbAction *cancelAction = new HbAction(hbTrId("txt_common_button_cancel"), iWaitDialog);
       
   410         iWaitDialog->setPrimaryAction(cancelAction);
       
   411         connect(cancelAction, SIGNAL(triggered()), this, SLOT(cancelRequest()));
       
   412     }
       
   413     
       
   414     iWaitDialog->setText(aText);
       
   415     iWaitDialog->exec();
       
   416 }
       
   417 
       
   418 void IRCategoryView::connectToIsdsClient()
       
   419 {
       
   420     connect(iIsdsClient, SIGNAL(categoryItemsChanged(QList<IRQBrowseCategoryItem *> *)),
       
   421             iModel, SLOT(categoryReceived(QList<IRQBrowseCategoryItem *> *)));
       
   422     connect(iIsdsClient, SIGNAL(operationException(IRQError)),
       
   423             this, SLOT(operationException(IRQError)));
       
   424 }
       
   425 
       
   426 void IRCategoryView::disconnectIsdsClient()
       
   427 {
       
   428     disconnect(iIsdsClient, SIGNAL(categoryItemsChanged(QList<IRQBrowseCategoryItem *> *)),
       
   429                iModel, SLOT(categoryReceived(QList<IRQBrowseCategoryItem *> *)));
       
   430     disconnect(iIsdsClient, SIGNAL(operationException(IRQError)),
       
   431                this, SLOT(operationException(IRQError)));
       
   432 }
       
   433 
       
   434 /*
       
   435  * Description : set the checked action in toolbar. For categoryview, iGenresAction should be
       
   436  *               checked if the view is for genre display. For other categories, iGenresAction
       
   437  *               should not be checked
       
   438  */
       
   439 void IRCategoryView::setCheckedAction()
       
   440 {
       
   441     if (EIRViewPara_Genre == getViewParameter())
       
   442     {
       
   443         iGenresAction->setChecked(true);
       
   444     }
       
   445     else
       
   446     {
       
   447         QList<QAction*> actionList = iCollectionsAction->actionGroup()->actions();
       
   448         for (QList<QAction*>::const_iterator it = actionList.begin(); it != actionList.end(); ++it)
       
   449         {
       
   450             (*it)->setChecked(false);
       
   451         }
       
   452     }
       
   453 }