qtinternetradio/ui/src/irstationsview.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 #include <QPixmap>
       
    18 #include <hblistview.h>
       
    19 #include <hbaction.h>
       
    20 #include <QTimer>
       
    21 
       
    22 #include "irabstractviewmanager.h"
       
    23 #include "irstationsview.h"
       
    24 #include "irapplication.h"
       
    25 #include "irplaycontroller.h"
       
    26 #include "irqisdsclient.h"
       
    27 #include "irqenums.h"
       
    28 #include "irqutility.h"
       
    29 #include "irchannelmodel.h"
       
    30 #include "irqisdsdatastructure.h"
       
    31 #include "irqnetworkcontroller.h"
       
    32 
       
    33 const int KBitmapSize = 59; 
       
    34 const uint KConnectTimeOut = 15000; //if the connecting take more than 15 seconds, we will show a pop up
       
    35 
       
    36 //                                      public functions
       
    37 
       
    38 /*
       
    39  * Description : constructor
       
    40  */
       
    41 IRStationsView::IRStationsView(IRApplication* aApplication, TIRViewId aViewId) 
       
    42                                : IrAbstractListViewBase(aApplication, aViewId),
       
    43                                iLogoPreset(NULL),  iPreset(NULL), 
       
    44                                iWaitDialog(NULL),
       
    45                                iViewParameter(EIRViewPara_InvalidId),iLastSelectitem(0),
       
    46                                iLastPopularItem(0)                          
       
    47 {       
       
    48     iIconIndexArray.clear();
       
    49     iChannelModel = new IrChannelModel(this);
       
    50     iListView->setModel(iChannelModel);
       
    51     
       
    52     
       
    53     iConnectTimer = new QTimer(this);
       
    54     iConnectTimer->setInterval(KConnectTimeOut);     
       
    55     
       
    56     iConvertTimer = new QTimer(this);
       
    57     iConvertTimer->setInterval(10);    
       
    58     
       
    59     connect(iChannelModel, SIGNAL(dataAvailable()), this, SLOT(dataChanged()));    
       
    60     connect(iNetworkController, SIGNAL(networkRequestNotified(IRQNetworkEvent)),
       
    61             this, SLOT(networkRequestNotified(IRQNetworkEvent)));    
       
    62     connect(iConnectTimer, SIGNAL(timeout()), this, SLOT(connectTimeOut()));
       
    63     connect(iConvertTimer, SIGNAL(timeout()), this, SLOT(convertAnother()));
       
    64 }
       
    65 
       
    66 /*
       
    67  * Description : destructor
       
    68  */
       
    69 IRStationsView::~IRStationsView()
       
    70 {
       
    71     delete iPreset;
       
    72     iPreset = NULL;
       
    73 
       
    74     delete iLogoPreset;
       
    75     iLogoPreset = NULL;
       
    76 
       
    77     delete iWaitDialog;
       
    78     iWaitDialog = NULL;
       
    79     
       
    80  
       
    81  
       
    82 }
       
    83 
       
    84 void IRStationsView::setViewParameter(TIRViewParameter aParameter)
       
    85 {
       
    86     iViewParameter = aParameter;
       
    87 }
       
    88 
       
    89 TIRViewParameter IRStationsView::getViewParameter() const
       
    90 {
       
    91     return iViewParameter;
       
    92 }
       
    93 
       
    94 void IRStationsView::loadCategoryStations(int aIndex, const QString &aHeadingText)
       
    95 {
       
    96     connectToIsdsClient();
       
    97     setHeadingText(aHeadingText);
       
    98     setViewParameter(EIRViewPara_CategoryStations);
       
    99     
       
   100     bool cache = false;
       
   101     iIsdsClient->isdsChannelRequest(aIndex, cache);
       
   102     
       
   103     if (!cache)
       
   104     {
       
   105         createWaitDialog(hbTrId("txt_common_info_loading"));
       
   106     }
       
   107 }
       
   108 
       
   109 void IRStationsView::loadPopularStations(bool aShowWaitDialog)
       
   110 {
       
   111     connectToIsdsClient();
       
   112     setViewParameter(EIRViewPara_PopularStations);
       
   113     setHeadingText(tr("Popular stations"));
       
   114     bool cache = false;
       
   115     iIsdsClient->isdsCategoryRequest(IRQIsdsClient::Ehotpicks, cache);
       
   116      
       
   117     if (!cache && aShowWaitDialog)
       
   118     {
       
   119         createWaitDialog(hbTrId("txt_common_info_loading"));
       
   120     }
       
   121 }
       
   122 
       
   123 void IRStationsView::loadSearchResult(const QString &aStr)
       
   124 {
       
   125     connectToIsdsClient();
       
   126     setViewParameter(EIRViewPara_SearchResults);
       
   127     setHeadingText(hbTrId("txt_irad_subtitle_search_result"));
       
   128     iIsdsClient->isdsSearchRequest(aStr);    
       
   129     if( iConnectTimer->isActive() )
       
   130     {
       
   131         iConnectTimer->stop();
       
   132     }
       
   133     
       
   134     iConnectTimer->start();
       
   135     createWaitDialog(hbTrId("txt_common_info_searching"));
       
   136 }
       
   137 
       
   138 void IRStationsView::storeCurrentItem()
       
   139 {
       
   140     switch (getViewParameter())
       
   141     {
       
   142     case EIRViewPara_PopularStations:
       
   143         iLastPopularItem = iListView->currentIndex().row();
       
   144         break;
       
   145     case EIRViewPara_CategoryStations:
       
   146         iLastSelectitem = iListView->currentIndex().row();
       
   147         break;
       
   148     default:
       
   149         break;
       
   150     }
       
   151 }
       
   152 
       
   153 void IRStationsView::resetCurrentItem()
       
   154 {
       
   155     iLastSelectitem = 0;
       
   156 }
       
   157 
       
   158 /*
       
   159  * Description : from base class IRBaseView.
       
   160  *               handle view commands.
       
   161  * Parameters  : aCommand : see the definition of TIRViewCommand
       
   162  * Return      : EIR_DoDefault : caller does default handling
       
   163  *               EIR_NoDefault : caller doesn't do default handling
       
   164  */
       
   165 TIRHandleResult IRStationsView::handleCommand(TIRViewCommand aCommand, TIRViewCommandReason aReason)
       
   166 {
       
   167     TIRHandleResult ret = IrAbstractListViewBase::handleCommand(aCommand, aReason);
       
   168     int leftCount = 0;
       
   169     
       
   170     switch (aCommand)
       
   171     {   
       
   172     case EIR_ViewCommand_ACTIVATED:
       
   173         connect(iIsdsClient, SIGNAL(presetResponse(IRQPreset *)),
       
   174                 this, SLOT(presetResponse(IRQPreset *)));
       
   175         connect(iIsdsClient, SIGNAL(presetLogoDownloaded(IRQPreset* )),
       
   176                 this, SLOT(presetLogoDownload(IRQPreset* )));
       
   177         connect(iIsdsClient, SIGNAL(presetLogoDownloadError()),
       
   178                 this, SLOT(presetLogoDownloadError()));
       
   179         
       
   180         ret = EIR_NoDefault;
       
   181         break;
       
   182         
       
   183     case EIR_ViewCommand_DEACTIVATE:
       
   184         if (aReason == EIR_ViewCommandReason_Back)
       
   185         {
       
   186             cleanupResource();
       
   187         }
       
   188         
       
   189         iConnectTimer->stop();
       
   190         iConvertTimer->stop();
       
   191         iIsdsClient->isdsLogoDownCancelTransaction();         
       
   192         
       
   193         disconnect(iIsdsClient, SIGNAL(presetResponse(IRQPreset *)),
       
   194                    this, SLOT(presetResponse(IRQPreset *)));
       
   195         disconnect(iIsdsClient, SIGNAL(presetLogoDownloaded(IRQPreset*)),
       
   196                    this, SLOT(presetLogoDownload(IRQPreset* )));
       
   197         disconnect(iIsdsClient, SIGNAL(presetLogoDownloadError()),
       
   198                    this, SLOT(presetLogoDownloadError()));        
       
   199         ret = EIR_NoDefault;
       
   200         break;
       
   201         
       
   202     case EIR_ViewCommand_EffectFinished:
       
   203         /* when the effect is finished, we start showing the logos  */
       
   204         leftCount = iIconIndexArray.count();
       
   205         if( leftCount > 0 )
       
   206         {
       
   207             iConvertTimer->start();
       
   208         }
       
   209         break;
       
   210  
       
   211     default:
       
   212         break;
       
   213     }
       
   214     
       
   215     return ret;
       
   216 }
       
   217 
       
   218 void IRStationsView::launchAction()
       
   219 {
       
   220     setUseNetworkReason(EIR_UseNetwork_StartingView);
       
   221     iApplication->verifyNetworkConnectivity();
       
   222 }
       
   223 
       
   224 //                                      slots functions
       
   225 
       
   226 /*
       
   227  * Description : slot function when an item in a list is clicked.
       
   228  *               issue a listen request to isds client
       
   229  * Parameters  : aItem : pointer to the clicked item.
       
   230  */
       
   231 void IRStationsView::handleItemSelected()
       
   232 {
       
   233     if (iListView)
       
   234     {
       
   235         int index = iListView->currentIndex().row();
       
   236         if (index != -1)  
       
   237         {
       
   238             if (iIsdsClient)
       
   239             {
       
   240                 connect(iIsdsClient, SIGNAL(operationException(IRQError)),
       
   241                         this, SLOT(operationException(IRQError)));
       
   242                 
       
   243                 //once an item is selected, we show a dialog to prevent user from clicking the
       
   244                 //item again
       
   245                 iPlayController->createBufferingDialog(this, SLOT(cancelRequest()));
       
   246                 
       
   247                 if (iIsdsClient->isdsIsChannelBanner())
       
   248                 {
       
   249                     iIsdsClient->isdsListenRequest(index + 1);
       
   250                 }
       
   251                 else
       
   252                 {
       
   253                     iIsdsClient->isdsListenRequest(index);
       
   254                 }
       
   255             }
       
   256         }
       
   257     }
       
   258 }
       
   259 
       
   260 /*
       
   261  * Description : data is received from isds client. Load the data to the list widget and display
       
   262  *               the stations view
       
   263  */
       
   264 void IRStationsView::dataChanged()
       
   265 {
       
   266 	
       
   267 	int currentItem = 0;
       
   268     iApplication->closeConnectingDialog();
       
   269     iConnectTimer->stop();
       
   270     if (iWaitDialog)
       
   271     {
       
   272         iWaitDialog->close();
       
   273     }
       
   274     
       
   275     disconnectIsdsClient();
       
   276     cleanupResource();
       
   277  
       
   278     
       
   279     switch (getViewParameter())
       
   280     {
       
   281     case EIRViewPara_SearchResults:
       
   282         currentItem = 0;
       
   283         break;
       
   284     case EIRViewPara_PopularStations:
       
   285         currentItem = iLastPopularItem;
       
   286         break;
       
   287     case EIRViewPara_CategoryStations:
       
   288         currentItem = iLastSelectitem;
       
   289         break;
       
   290 	  default:
       
   291         break;
       
   292     }
       
   293     iListView->reset();
       
   294     iListView->setCurrentIndex(iChannelModel->index(currentItem));
       
   295     iListView->scrollTo(iChannelModel->index(currentItem));
       
   296 
       
   297     //initialize the iconindices
       
   298     for (int i = 0; i < iChannelModel->rowCount(); ++i)
       
   299     {
       
   300         if (iChannelModel->imageUrl(i) != "")
       
   301         {
       
   302             iIconIndexArray.append(i);
       
   303         }
       
   304     }
       
   305 
       
   306     getViewManager()->activateView(this);   
       
   307 }
       
   308 
       
   309 /*
       
   310  * Description : the preset of the selected station is received from isds client.
       
   311  *               application is responsible for deleting old preset.
       
   312  *               call player controler to play the station
       
   313  * Parameters  : aPreset : the preset of the selected station            
       
   314  */
       
   315 void IRStationsView::presetResponse(IRQPreset *aPreset)
       
   316 {
       
   317     disconnect(iIsdsClient, SIGNAL(operationException(IRQError)),
       
   318                this, SLOT(operationException(IRQError)));
       
   319     
       
   320     delete iPreset;
       
   321     iPreset = aPreset;
       
   322     
       
   323     if (iPreset)
       
   324     {
       
   325         iPlayController->connectToChannel(iPreset,EIRQIsds);
       
   326     }
       
   327 }
       
   328 
       
   329 
       
   330 /*
       
   331  * Description  : slot function for operation failure. The signal is emitted by isds client
       
   332  * Parameters   : aError : see the definition of IRQError
       
   333  */
       
   334 void IRStationsView::operationException(IRQError aError)
       
   335 {
       
   336     iApplication->closeConnectingDialog();
       
   337     iPlayController->closeBufferingDialog();
       
   338     
       
   339     if (iWaitDialog)
       
   340     {
       
   341         iWaitDialog->close();
       
   342     }
       
   343     
       
   344     disconnectIsdsClient();
       
   345     QString errorString = hbTrId("txt_irad_info_failed_to_connect");
       
   346     
       
   347     switch (aError)
       
   348     {   
       
   349     case EIRQErrorNotFound:
       
   350         errorString = hbTrId("txt_irad_info_no_matching_stations_found");
       
   351         if (iConnectTimer->isActive())
       
   352         {
       
   353             iConnectTimer->stop();
       
   354         }
       
   355         break;
       
   356         default:
       
   357             break;
       
   358     }
       
   359     
       
   360     popupNote(errorString, HbMessageBox::MessageTypeWarning);
       
   361 }
       
   362 
       
   363 void IRStationsView::cancelRequest()
       
   364 {
       
   365     if( iWaitDialog )
       
   366     {
       
   367         iWaitDialog->close();
       
   368     }
       
   369     
       
   370     if( iConnectTimer->isActive())
       
   371     {
       
   372         iConnectTimer->stop();
       
   373     }    
       
   374     iIsdsClient->isdsCancelRequest();
       
   375     disconnectIsdsClient();
       
   376 }
       
   377 
       
   378 void IRStationsView::startConvert(int aIndex)
       
   379 {
       
   380     QString url = iChannelModel->imageUrl(aIndex);
       
   381  
       
   382     IRQPreset tempPreset;
       
   383     tempPreset.imgUrl = url;
       
   384     tempPreset.type = IRQPreset::EIsds;
       
   385     
       
   386     bool cached = iIsdsClient->isdsIsLogoCached(&tempPreset, KBitmapSize, KBitmapSize);
       
   387     
       
   388     if( !cached )
       
   389     {
       
   390         bool network = iApplication->verifyNetworkConnectivity(hbTrId("txt_irad_info_downloading_logos"));
       
   391         if( !network )
       
   392         {
       
   393             //if user has clicked an item and the connection is being setup, we don't change the reason
       
   394             if (EIR_UseNetwork_NoReason == getUseNetworkReason())
       
   395             {
       
   396                 setUseNetworkReason(EIR_UseNetwork_DownloadLogo);
       
   397             }
       
   398             iConvertTimer->stop();
       
   399             return;
       
   400         }        
       
   401     }
       
   402     
       
   403     iIsdsClient->isdsLogoDownSendRequest(&tempPreset, 0, KBitmapSize, KBitmapSize); 
       
   404 }
       
   405 
       
   406  
       
   407 //if the logo is downloaded ok
       
   408 void IRStationsView::presetLogoDownload(IRQPreset* aPreset)
       
   409 {
       
   410     if( NULL == aPreset )
       
   411     {
       
   412         presetLogoDownloadError();
       
   413         return;
       
   414     } 
       
   415     
       
   416  
       
   417     delete iLogoPreset;            
       
   418     iLogoPreset = aPreset;    
       
   419 
       
   420     if (iLogoPreset->logoData != KNullDesC8)
       
   421     {         
       
   422         QPixmap tempMap;  
       
   423         const unsigned char * logoData = iLogoPreset->logoData.Ptr();     
       
   424         bool ret = tempMap.loadFromData(logoData, iLogoPreset->logoData.Length());
       
   425         QIcon convertIcon(tempMap);
       
   426        
       
   427         if( ret )
       
   428         {            
       
   429             HbIcon *hbIcon = new HbIcon(convertIcon);
       
   430             int index = iIconIndexArray[0];
       
   431             iChannelModel->setLogo(hbIcon, index);
       
   432             iIconIndexArray.removeAt(0);     
       
   433             int leftCount = iIconIndexArray.count(); 
       
   434             if( leftCount > 0 )
       
   435             {
       
   436                 iConvertTimer->start();  
       
   437             }
       
   438             return;
       
   439         }     
       
   440     }    
       
   441     
       
   442     presetLogoDownloadError(); 
       
   443 }
       
   444 
       
   445 //if the logo download fails
       
   446 void IRStationsView::presetLogoDownloadError()
       
   447 {
       
   448     // if the logo download fails, try to download the next
       
   449     iIconIndexArray.removeAt(0);
       
   450     int leftCount = iIconIndexArray.count();
       
   451     if( leftCount > 0 )
       
   452     {
       
   453         iConvertTimer->start();
       
   454     }    
       
   455 }
       
   456 
       
   457 void IRStationsView::cleanupResource()
       
   458 {
       
   459     iIconIndexArray.clear();
       
   460 
       
   461     //destroy icons in time to save memory
       
   462     iChannelModel->clearAndDestroyLogos();
       
   463 }
       
   464 
       
   465 void IRStationsView::networkRequestNotified(IRQNetworkEvent aEvent)
       
   466 {
       
   467     if (getViewManager()->currentView() != this)
       
   468     {
       
   469         return;
       
   470     }
       
   471     
       
   472     switch (aEvent)
       
   473     {
       
   474     case EIRQNetworkConnectionEstablished:
       
   475         if (EIR_UseNetwork_StartingView == getUseNetworkReason())
       
   476         {
       
   477             //when stations view is starting view(used to show popular stations), don't show wait dialog 
       
   478             loadPopularStations(false);
       
   479         }
       
   480         else if(EIR_UseNetwork_DownloadLogo == getUseNetworkReason())
       
   481         {
       
   482             iApplication->closeConnectingDialog();
       
   483             int leftCount = iIconIndexArray.count();
       
   484             if(0 != leftCount)
       
   485             {
       
   486             	  iConvertTimer->start();
       
   487             }            
       
   488         }
       
   489         else if (EIR_UseNetwork_SelectItem == getUseNetworkReason())
       
   490         {
       
   491             iApplication->closeConnectingDialog();
       
   492             handleItemSelected();
       
   493             int leftCount = iIconIndexArray.count();
       
   494             if(0 != leftCount)
       
   495             {
       
   496                 iConvertTimer->start();
       
   497             }
       
   498         }
       
   499         setUseNetworkReason(EIR_UseNetwork_NoReason);
       
   500         
       
   501         break;
       
   502         
       
   503     case EIRQConnectingCancelled:
       
   504     case EIRQDisplayNetworkMessageNoConnectivity:
       
   505         if (iListView->model()->rowCount() == 0)
       
   506         {
       
   507             getViewManager()->activateView(EIRView_MainView);
       
   508         }
       
   509         else
       
   510         {
       
   511             setCheckedAction();
       
   512         }
       
   513         setUseNetworkReason(EIR_UseNetwork_NoReason);
       
   514         break;
       
   515         
       
   516     default:
       
   517         setCheckedAction();
       
   518         setUseNetworkReason(EIR_UseNetwork_NoReason);
       
   519         break;
       
   520     }
       
   521 }
       
   522 
       
   523 void IRStationsView::connectToIsdsClient()
       
   524 {
       
   525     connect(iIsdsClient, SIGNAL(channelItemsChanged(QList<IRQChannelItem *> *)),
       
   526             iChannelModel, SLOT(updateData(QList<IRQChannelItem *> *)));
       
   527     
       
   528     connect(iIsdsClient, SIGNAL(operationException(IRQError)),
       
   529             this, SLOT(operationException(IRQError)));
       
   530 }
       
   531 
       
   532 void IRStationsView::disconnectIsdsClient()
       
   533 {
       
   534     disconnect(iIsdsClient, SIGNAL(channelItemsChanged(QList<IRQChannelItem *> *)),
       
   535                iChannelModel, SLOT(updateData(QList<IRQChannelItem *> *)));
       
   536     
       
   537     disconnect(iIsdsClient, SIGNAL(operationException(IRQError)),
       
   538                this, SLOT(operationException(IRQError)));
       
   539 }
       
   540 
       
   541 void IRStationsView::createWaitDialog(QString aStr)
       
   542 {
       
   543     if (!iWaitDialog)
       
   544     {
       
   545         iWaitDialog = new HbMessageBox(aStr, HbMessageBox::MessageTypeInformation);
       
   546         //iWaitDialog->setTimeout(HbPopupBase::NoTimeout); 
       
   547         iWaitDialog->setTimeout(HbPopup::NoTimeout); // JM: changed in w47 Orbit
       
   548         iWaitDialog->setModal(true);
       
   549         //iWaitDialog->setDismissPolicy(HbPopupBase::NoDismiss); 
       
   550         iWaitDialog->setDismissPolicy(HbPopup::NoDismiss); // JM: changed in w47 Orbit
       
   551         HbAction *cancelAction = new HbAction(hbTrId("txt_common_button_cancel"), iWaitDialog);
       
   552         iWaitDialog->setPrimaryAction(cancelAction);
       
   553         connect(cancelAction, SIGNAL(triggered()), this, SLOT(cancelRequest()));
       
   554     }
       
   555 
       
   556     iWaitDialog->exec();
       
   557 }
       
   558 
       
   559 void IRStationsView::convertAnother()
       
   560 {
       
   561     iConvertTimer->stop();
       
   562     int leftCount = iIconIndexArray.count();
       
   563     
       
   564     if(0 != leftCount)
       
   565     {
       
   566         startConvert(iIconIndexArray[0]);   
       
   567     }
       
   568 }
       
   569 
       
   570 void IRStationsView::connectTimeOut()
       
   571 {    
       
   572     iConnectTimer->stop();
       
   573     cancelRequest();   
       
   574     popupNote(hbTrId("txt_irad_info_connecting_timout"), HbMessageBox::MessageTypeWarning);
       
   575 }