qtinternetradio/ui/src/irfavoritesview.cpp
changeset 0 09774dfdd46b
child 3 ee64f059b8e1
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 <hblistview.h>
       
    18 #include <hbaction.h>
       
    19 #include <hbmenu.h>
       
    20 #include <QPixmap>
       
    21 #include <QTimer>
       
    22 
       
    23 #include "irabstractviewmanager.h"
       
    24 #include "irfavoritesview.h"
       
    25 #include "irapplication.h"
       
    26 #include "irqfavoritesdb.h"
       
    27 #include "irqisdsclient.h"
       
    28 #include "irqnetworkcontroller.h"
       
    29 #include "irplaycontroller.h"
       
    30 #include "irfavoritesmodel.h"
       
    31 #include "irqenums.h"
       
    32 #include "irqisdsdatastructure.h"
       
    33 #include "irqutility.h"
       
    34 
       
    35 const int KBitmapSize = 59;
       
    36 //                                        public functions
       
    37 
       
    38 /*
       
    39  * Description : constructor
       
    40  */
       
    41 IRFavoritesView::IRFavoritesView(IRApplication *aApplication, TIRViewId aViewId) 
       
    42                                  : IrAbstractListViewBase(aApplication, aViewId),
       
    43                                    iClearFavoriteAction(NULL),iLogoPreset(NULL)
       
    44 								  
       
    45 {    
       
    46     iClearFavoriteAction = new HbAction(hbTrId("txt_irad_opt_clear_favorites"), menu());
       
    47     connect(iClearFavoriteAction, SIGNAL(triggered()), this, SLOT(clearAllFavorites()));    
       
    48   
       
    49     iModel = new IRFavoritesModel(iFavorites, this);
       
    50     iListView->setModel(iModel);
       
    51     iListView->setCurrentIndex(iModel->index(0));
       
    52     
       
    53     iConvertTimer = new QTimer(this);
       
    54     iConvertTimer->setInterval(10);
       
    55     
       
    56     connect(iModel, SIGNAL(modelChanged()), this, SLOT(modelChanged()));
       
    57     connect(iNetworkController, SIGNAL(networkRequestNotified(IRQNetworkEvent)),
       
    58             this, SLOT(networkRequestNotified(IRQNetworkEvent))); 
       
    59     connect(iConvertTimer, SIGNAL(timeout()), this, SLOT(convertAnother()));
       
    60 }
       
    61 
       
    62 /*
       
    63  * Description : destructor
       
    64  */
       
    65 IRFavoritesView::~IRFavoritesView()
       
    66 {
       
    67     delete iLogoPreset;
       
    68     iLogoPreset = NULL;
       
    69 }
       
    70 
       
    71 TIRHandleResult IRFavoritesView::handleCommand(TIRViewCommand aCommand, TIRViewCommandReason aReason)
       
    72 {
       
    73     Q_UNUSED(aReason);
       
    74     TIRHandleResult ret = IrAbstractListViewBase::handleCommand(aCommand, aReason);
       
    75     int leftCount = 0;
       
    76     
       
    77     switch (aCommand)
       
    78     {
       
    79     case EIR_ViewCommand_ACTIVATED:        
       
    80         connect(iIsdsClient, SIGNAL(presetLogoDownloaded(IRQPreset* )),
       
    81                 this, SLOT(presetLogoDownload(IRQPreset* )));
       
    82         connect(iIsdsClient, SIGNAL(presetLogoDownloadError()),
       
    83                 this, SLOT(presetLogoDownloadError()));
       
    84         
       
    85         
       
    86         iModel->checkFavoritesUpdate();          
       
    87         ret = EIR_NoDefault;
       
    88         break;
       
    89 
       
    90     case EIR_ViewCommand_DEACTIVATE:        		 
       
    91         
       
    92         iModel->clearAndDestroyLogos();
       
    93         iConvertTimer->stop();
       
    94         iIsdsClient->isdsLogoDownCancelTransaction();
       
    95         //iIconIndexArray must be cleared, because timer call back convertAnother() might be
       
    96         //called after view is deactivated. In that case, iModel->getImgURL(aIndex); will crash
       
    97         iIconIndexArray.clear();
       
    98         
       
    99         disconnect(iIsdsClient, SIGNAL(presetLogoDownloaded(IRQPreset*)),
       
   100                    this, SLOT(presetLogoDownload(IRQPreset* )));
       
   101         disconnect(iIsdsClient, SIGNAL(presetLogoDownloadError()),
       
   102                    this, SLOT(presetLogoDownloadError()));
       
   103 
       
   104         ret = EIR_NoDefault;
       
   105         break;
       
   106      
       
   107     case EIR_ViewCommand_EffectFinished:
       
   108         
       
   109         /* when the effect is finished, we start showing the logos  */
       
   110         leftCount = iIconIndexArray.count();
       
   111         if( leftCount > 0 )
       
   112         {
       
   113             iConvertTimer->start();
       
   114         }
       
   115         break;
       
   116 
       
   117     default:
       
   118         break;
       
   119     }
       
   120 
       
   121     return ret;
       
   122 }
       
   123 
       
   124 void IRFavoritesView::handleItemSelected()
       
   125 {
       
   126     int currentIndex = iListView->currentIndex().row();
       
   127     IRQPreset * currentPreset = iModel->getPreset(currentIndex);
       
   128 
       
   129     if (currentPreset)
       
   130     {
       
   131         if(IRQPreset::EIsds == currentPreset->type)
       
   132         {
       
   133             iPlayController->connectToChannel(currentPreset,EIRQPresetIsds);
       
   134         }
       
   135         else
       
   136         {
       
   137             iPlayController->connectToChannel(currentPreset,EIRQPresetAdhoc);
       
   138         }
       
   139     }        
       
   140 }
       
   141 
       
   142 // ---------------------------------------------------------------------------
       
   143 // IRFavoritesView::clearAllList()
       
   144 //---------------------------------------------------------------------------
       
   145 void IRFavoritesView::clearAllFavorites()
       
   146 {
       
   147     iIconIndexArray.clear();
       
   148     iModel->clearFavoriteDB();   
       
   149     iIsdsClient->isdsLogoDownCancelTransaction();
       
   150     iConvertTimer->stop();
       
   151     updateView();
       
   152 }
       
   153 
       
   154 void IRFavoritesView::networkRequestNotified(IRQNetworkEvent aEvent)
       
   155 {
       
   156     if (getViewManager()->currentView() != this)
       
   157     {
       
   158         return;
       
   159     }
       
   160     
       
   161     switch (aEvent)
       
   162     {
       
   163     case EIRQNetworkConnectionEstablished:
       
   164         iApplication->closeConnectingDialog();
       
   165         if (EIR_UseNetwork_SelectItem == getUseNetworkReason())
       
   166         {
       
   167             handleItemSelected();
       
   168         }
       
   169         
       
   170         setUseNetworkReason(EIR_UseNetwork_NoReason);
       
   171         break;
       
   172 
       
   173     default:
       
   174         setCheckedAction();
       
   175         break;
       
   176     }
       
   177 }
       
   178 
       
   179 void IRFavoritesView::prepareMenu()
       
   180 {
       
   181     HbMenu *viewMenu = menu();
       
   182     
       
   183     viewMenu->removeAction(iClearFavoriteAction);
       
   184     if (iModel->rowCount() > 0)
       
   185     {
       
   186         viewMenu->insertAction(iOpenWebAddressAction, iClearFavoriteAction);
       
   187     }
       
   188 }
       
   189 
       
   190 void IRFavoritesView::startConvert(int aIndex)
       
   191 {     
       
   192     QString url = iModel->getImgUrl(aIndex);
       
   193     
       
   194     IRQPreset tempPreset;
       
   195     tempPreset.imgUrl = url;
       
   196     tempPreset.type = IRQPreset::EIsds;
       
   197 
       
   198     iIsdsClient->isdsLogoDownSendRequest(&tempPreset, 0, KBitmapSize, KBitmapSize); 
       
   199 }
       
   200 
       
   201 //if the logo is downloaded ok
       
   202 void IRFavoritesView::presetLogoDownload(IRQPreset* aPreset)
       
   203 {
       
   204     if (NULL == aPreset)
       
   205     {
       
   206         presetLogoDownloadError();
       
   207         return;
       
   208     }
       
   209 
       
   210     delete iLogoPreset;
       
   211     iLogoPreset = aPreset;
       
   212 
       
   213     if (iLogoPreset->logoData != KNullDesC8)
       
   214     {
       
   215         const unsigned char * logoData = iLogoPreset->logoData.Ptr();
       
   216         QPixmap tempMap;
       
   217         bool ret = tempMap.loadFromData(logoData,iLogoPreset->logoData.Length());
       
   218         if( ret )
       
   219         {
       
   220             QIcon convertIcon(tempMap);
       
   221             HbIcon *hbIcon = new HbIcon(convertIcon);
       
   222             int index = iIconIndexArray[0];
       
   223             iModel->setLogo(hbIcon, index);
       
   224             iIconIndexArray.removeAt(0);
       
   225             int leftCount = iIconIndexArray.count();
       
   226             if( leftCount > 0 )
       
   227             {
       
   228                 iConvertTimer->start();  
       
   229             }            
       
   230             return;             
       
   231         }        
       
   232     }    
       
   233     
       
   234     presetLogoDownloadError();
       
   235 }
       
   236 
       
   237 //if the logo download fails
       
   238 void IRFavoritesView::presetLogoDownloadError()
       
   239 {
       
   240     iIconIndexArray.removeAt(0);
       
   241     int leftCount = 0;
       
   242     leftCount = iIconIndexArray.count();
       
   243     if( leftCount > 0 )
       
   244     {
       
   245         iConvertTimer->start();
       
   246     }    
       
   247 }
       
   248 
       
   249 void IRFavoritesView::convertAnother()
       
   250 {
       
   251     iConvertTimer->stop();
       
   252     int leftCount = iIconIndexArray.count();
       
   253 
       
   254     if (0 != leftCount)
       
   255     {
       
   256         startConvert(iIconIndexArray[0]);
       
   257     }
       
   258 }
       
   259 
       
   260 void IRFavoritesView::modelChanged()
       
   261 {
       
   262     QString headingStr = tr("Favorites") + " (" + QString::number(iModel->rowCount()) + ")";
       
   263     setHeadingText(headingStr);
       
   264     iIconIndexArray.clear();
       
   265     
       
   266     for (int i = 0; i < iModel->rowCount(); ++i)
       
   267     {
       
   268         if (iModel->getImgUrl(i) != "")
       
   269         {
       
   270             iIconIndexArray.append(i);
       
   271         }
       
   272     }
       
   273     
       
   274     iListView->reset();
       
   275     iListView->setCurrentIndex(iModel->index(0));
       
   276 }
       
   277 
       
   278 /*
       
   279  * Description : set the checked action in toolbar. for favoritesview, iFavoritesAction should
       
   280  *               be checked
       
   281  */
       
   282 void IRFavoritesView::setCheckedAction()
       
   283 {
       
   284     iFavoritesAction->setChecked(true);
       
   285 }
       
   286 
       
   287 void IRFavoritesView::listViewLongPressed(HbAbstractViewItem *aItem, const QPointF& aCoords)
       
   288 {
       
   289     Q_UNUSED(aItem);             
       
   290     HbMenu *contextMenu = 0;
       
   291     HbAction *action = 0;
       
   292     
       
   293     contextMenu = new HbMenu(); 
       
   294     action = contextMenu->addAction(QString(hbTrId("txt_common_menu_delete")));
       
   295     action->setObjectName("delete");
       
   296     action = contextMenu->exec(aCoords);
       
   297     
       
   298     if( action )
       
   299     {
       
   300         QString objectName = action->objectName();      
       
   301         if( objectName == "delete")
       
   302         {
       
   303             deleteContextAction();                            
       
   304         }
       
   305     }          
       
   306 } 
       
   307 
       
   308 void IRFavoritesView::deleteContextAction()
       
   309 {
       
   310     int current = iListView->currentIndex().row();
       
   311     bool ret = iModel->deleteOneFavorite(current);   
       
   312 	  if( !ret )
       
   313 	  {
       
   314 	    popupNote(hbTrId("txt_irad_info_operation_failed"), HbMessageBox::MessageTypeWarning);
       
   315 	  }
       
   316 }
       
   317