application/src/PodcastFeedView.cpp
changeset 2 29cda98b007e
child 5 ba42cd6670b8
equal deleted inserted replaced
1:5f8e5adbbed9 2:29cda98b007e
       
     1 /*
       
     2 * Copyright (c) 2007-2010 Sebastian Brannstrom, Lars Persson, EmbedDev AB
       
     3 *
       
     4 * All rights reserved.
       
     5 * This component and the accompanying materials are made available
       
     6 * under the terms of the License "Eclipse Public License v1.0"
       
     7 * which accompanies this distribution, and is available
       
     8 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 *
       
    10 * Initial Contributors:
       
    11 * EmbedDev AB - initial contribution.
       
    12 *
       
    13 * Contributors:
       
    14 *
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 #include "PodcastFeedView.h"
       
    20 #include "PodcastAppUi.h"
       
    21 #include "ShowEngine.h"
       
    22 #include "SettingsEngine.h"
       
    23 #include "PodcastApp.h"
       
    24 #include "PodcastUtils.h"
       
    25 #include "PodcastFeedViewUpdater.h"
       
    26 #include "Podcast.hrh"
       
    27 #include <caknfileselectiondialog.h> 
       
    28 #include <podcast.rsg>
       
    29 #include <podcast.mbg>
       
    30 #include <gulicon.h>
       
    31 #include <aknquerydialog.h>
       
    32 #include <caknmemoryselectiondialog.h> 
       
    33 #include <caknfilenamepromptdialog.h> 
       
    34 #include <BAUTILS.H> 
       
    35 #include <pathinfo.h> 
       
    36 
       
    37 const TInt KMaxFeedNameLength = 100;
       
    38 const TInt KMaxUnplayedFeedsLength =64;
       
    39 const TInt KADayInHours = 24;
       
    40 const TInt KDefaultGran = 5;
       
    41 const TInt KNumberOfFilesMaxLength = 4;
       
    42 #define KMaxMessageLength 200
       
    43 #define KMaxTitleLength 100
       
    44 const TInt KMimeBufLength = 100;
       
    45 
       
    46 _LIT(KFeedFormat, "%d\t%S\t%S%S");
       
    47 enum 
       
    48 {
       
    49  EFeedIcon
       
    50 };
       
    51 
       
    52 CPodcastFeedView* CPodcastFeedView::NewL(CPodcastModel& aPodcastModel)
       
    53     {
       
    54     CPodcastFeedView* self = CPodcastFeedView::NewLC(aPodcastModel);
       
    55     CleanupStack::Pop( self );
       
    56     return self;
       
    57     }
       
    58 
       
    59 CPodcastFeedView* CPodcastFeedView::NewLC(CPodcastModel& aPodcastModel)
       
    60     {
       
    61     CPodcastFeedView* self = new ( ELeave ) CPodcastFeedView(aPodcastModel);
       
    62     CleanupStack::PushL( self );
       
    63     self->ConstructL();
       
    64     return self;
       
    65     }
       
    66 
       
    67 CPodcastFeedView::CPodcastFeedView(CPodcastModel& aPodcastModel):iPodcastModel(aPodcastModel)
       
    68 	{
       
    69 	iFirstActivateAfterLaunch = ETrue;
       
    70 	}
       
    71 #define KAsterisk iEikonEnv->EikAppUi()->Application()->BitmapStoreName()
       
    72 void CPodcastFeedView::ConstructL()
       
    73 	{
       
    74 	DP("CPodcastFeedView::ConstructL BEGIN");
       
    75 	//_LIT(KAsterisk, "*");
       
    76 	BaseConstructL(R_PODCAST_FEEDVIEW);
       
    77 	iNeverUpdated = iEikonEnv->AllocReadResourceL(R_PODCAST_FEEDS_NEVER_UPDATED);
       
    78 	iFeedsFormat = iEikonEnv->AllocReadResourceL(R_PODCAST_FEEDS_STATUS_FORMAT);
       
    79 	CPodcastListView::ConstructL();
       
    80 	iPodcastModel.FeedEngine().AddObserver(this);
       
    81 	CArrayPtr< CGulIcon >* icons = new(ELeave) CArrayPtrFlat< CGulIcon >(1);
       
    82 	CleanupStack::PushL( icons );
       
    83 	
       
    84 	CFbsBitmap* bitmap = NULL;
       
    85 	CFbsBitmap* mask = NULL;
       
    86 	// Load the bitmap for empty icon	
       
    87 	TFileName fname = KAsterisk;
       
    88 	TParsePtr parser(fname);
       
    89 
       
    90 	SetEmptyTextL(R_PODCAST_NO_FEEDS);
       
    91 	
       
    92 	// Load svg.-image and mask with a single call
       
    93 		AknIconUtils::CreateIconL(bitmap,
       
    94 		                          mask,
       
    95 		                          iEikonEnv->EikAppUi()->Application()->BitmapStoreName(),
       
    96 		                          EMbmPodcastFeed,
       
    97 		                          EMbmPodcastFeed_mask);
       
    98 	    
       
    99 	/*bitmap = iEikonEnv->CreateBitmapL(KAsterisk,EMbmPodcastFeed_40x40);
       
   100 	 * */
       
   101 	CleanupStack::PushL( bitmap );		
       
   102 	// Load the mask for feed icon	
       
   103 	//mask = iEikonEnv->CreateBitmapL(KAsterisk,EMbmPodcastFeed_40x40m );	
       
   104 	CleanupStack::PushL( mask );
       
   105 	// Append the feed icon to icon array
       
   106 	icons->AppendL( CGulIcon::NewL( bitmap, mask ) );
       
   107 	CleanupStack::Pop(2); // bitmap, mask
       
   108 	
       
   109 	iListContainer->Listbox()->ItemDrawer()->FormattedCellData()->SetIconArrayL( icons );
       
   110 	CleanupStack::Pop(icons); // icons
       
   111 
       
   112 	iListContainer->Listbox()->SetListBoxObserver(this);
       
   113 	
       
   114     iStylusPopupMenu = CAknStylusPopUpMenu::NewL( this , TPoint(0,0));
       
   115     TResourceReader reader;
       
   116     iCoeEnv->CreateResourceReaderLC(reader,R_FEEDVIEW_POPUP_MENU);
       
   117     iStylusPopupMenu->ConstructFromResourceL(reader);
       
   118     CleanupStack::PopAndDestroy();
       
   119     
       
   120     iUpdater = CPodcastFeedViewUpdater::NewL(*this);
       
   121 	DP("CPodcastFeedView::ConstructL END");
       
   122 	}
       
   123     
       
   124 CPodcastFeedView::~CPodcastFeedView()
       
   125     {
       
   126 	iPodcastModel.FeedEngine().RemoveObserver(this);
       
   127 	delete iFeedsFormat;
       
   128 	delete iNeverUpdated;
       
   129 	delete iStylusPopupMenu;
       
   130 	delete iUpdater;
       
   131     }
       
   132 
       
   133 void CPodcastFeedView::UpdateItemL(TInt aIndex)
       
   134 	{
       
   135 	_LIT(KPanicCategory, "CPodcastFeedView::UpdateItemL");
       
   136 	__ASSERT_DEBUG(iListContainer->IsVisible(), User::Panic(KPanicCategory, 0));
       
   137 	__ASSERT_ALWAYS(iItemIdArray.Count() > aIndex, User::Panic(KPanicCategory, 1));
       
   138 
       
   139 	const RFeedInfoArray& sortedItems = iPodcastModel.FeedEngine().GetSortedFeeds();
       
   140 	__ASSERT_ALWAYS(sortedItems.Count() > aIndex, User::Panic(KPanicCategory, 2));
       
   141 
       
   142 	// Update UID of for the feed at aIndex
       
   143 	iItemIdArray[aIndex] = sortedItems[aIndex]->Uid();
       
   144 	
       
   145 	// Prepare data to update the listbox item with
       
   146 	FormatFeedInfoListBoxItemL(*sortedItems[aIndex], EFalse);
       
   147 	
       
   148 	// If nothing has changed, we are done here
       
   149 	if (iListboxFormatbuffer == iItemArray->MdcaPoint(aIndex))
       
   150 		{
       
   151 		return;
       
   152 		}
       
   153 
       
   154 	// Something has changed, update the listbox item
       
   155 	TListItemProperties itemProps;			
       
   156 	itemProps.SetDimmed(EFalse);	
       
   157 	iItemArray->Delete(aIndex);	
       
   158 	iItemArray->InsertL(aIndex, iListboxFormatbuffer);
       
   159 	iListContainer->Listbox()->ItemDrawer()->SetPropertiesL(aIndex, itemProps);
       
   160 	// If item is visible, redraw it
       
   161 	if (iListContainer->Listbox()->TopItemIndex() <= aIndex
       
   162 			&& iListContainer->Listbox()->BottomItemIndex() >= aIndex)
       
   163 		{
       
   164 		iListContainer->Listbox()->DrawItem(aIndex);
       
   165 		}
       
   166 	}
       
   167 
       
   168 TUid CPodcastFeedView::Id() const
       
   169 	{
       
   170 	return KUidPodcastFeedViewID;
       
   171 	}
       
   172 		
       
   173 void CPodcastFeedView::DoActivateL(const TVwsViewId& aPrevViewId,
       
   174 	                                  TUid aCustomMessageId,
       
   175 	                                  const TDesC8& aCustomMessage)
       
   176 	{
       
   177 	UpdateToolbar();
       
   178 	
       
   179 	CPodcastListView::DoActivateL(aPrevViewId, aCustomMessageId, aCustomMessage);
       
   180 	
       
   181 	if (iFirstActivateAfterLaunch)
       
   182 		{
       
   183 		iFirstActivateAfterLaunch = EFalse;
       
   184 		}
       
   185 	}
       
   186 
       
   187 void CPodcastFeedView::DoDeactivate()
       
   188 	{
       
   189 	iUpdater->StopUpdate();
       
   190 	CPodcastListView::DoDeactivate();
       
   191 	}
       
   192 
       
   193 
       
   194 void CPodcastFeedView::HandleListBoxEventL(CEikListBox* /* aListBox */, TListBoxEvent aEventType)
       
   195 	{
       
   196 	DP("CPodcastFeedView::HandleListBoxEventL BEGIN");
       
   197 
       
   198 	switch(aEventType)
       
   199 		{
       
   200 	case EEventEnterKeyPressed:
       
   201 	case EEventItemDoubleClicked:
       
   202 	case EEventItemActioned:
       
   203 			{
       
   204 			const RFeedInfoArray* sortedItems = NULL;
       
   205 			TInt index = iListContainer->Listbox()->CurrentItemIndex();
       
   206 			sortedItems = &iPodcastModel.FeedEngine().GetSortedFeeds();
       
   207 
       
   208 			if(index >= 0 && index < sortedItems->Count())
       
   209 				{
       
   210 				iPodcastModel.ActiveShowList().Reset();
       
   211 				iPodcastModel.SetActiveFeedInfo((*sortedItems)[index]);			
       
   212 
       
   213 				AppUi()->ActivateLocalViewL(KUidPodcastShowsViewID,  TUid::Uid(0), KNullDesC8());
       
   214 				}
       
   215 			}
       
   216 		break;
       
   217 	default:
       
   218 		break;
       
   219 		}
       
   220 	DP("CPodcastFeedView::HandleListBoxEventL END");
       
   221 	}
       
   222 
       
   223 void CPodcastFeedView::FeedUpdateAllCompleteL(TFeedState /*aState*/)
       
   224 	{
       
   225 	iUpdatingAllRunning = EFalse;
       
   226 	UpdateToolbar();
       
   227 	}
       
   228 
       
   229 void CPodcastFeedView::FeedDownloadStartedL(TFeedState /*aState*/, TUint aFeedUid)
       
   230 	{
       
   231 	// Update status text
       
   232 	UpdateFeedInfoStatusL(aFeedUid, ETrue);
       
   233 	
       
   234 	UpdateToolbar();
       
   235 	}
       
   236 
       
   237 void CPodcastFeedView::FeedDownloadFinishedL(TFeedState aState,TUint aFeedUid, TInt aError)
       
   238 	{
       
   239 	switch(aError)
       
   240 		{
       
   241 		case KErrCouldNotConnect:
       
   242 			{
       
   243 			if(aState == MFeedEngineObserver::EFeedManualUpdate)
       
   244 				{
       
   245 				TBuf<KMaxMessageLength> message;
       
   246 				iEikonEnv->ReadResourceL(message, R_PODCAST_CONNECTION_ERROR);
       
   247 				ShowErrorMessage(message);
       
   248 				}
       
   249 			}
       
   250 			break;
       
   251 		default: // Do nothing
       
   252 			break;
       
   253 		}
       
   254 	UpdateFeedInfoStatusL(aFeedUid, EFalse);
       
   255 	}
       
   256 
       
   257 void CPodcastFeedView::UpdateFeedInfoStatusL(TUint aFeedUid, TBool aIsUpdating)
       
   258 	{
       
   259 	const RFeedInfoArray& feeds = iPodcastModel.FeedEngine().GetSortedFeeds();
       
   260 
       
   261 	// Find the index for the feed i both the feed-array and the listbox 
       
   262 	TInt feedsIdx = KErrNotFound;
       
   263 	TInt listboxIdx = KErrNotFound;
       
   264 	for (TInt i = 0; i < feeds.Count(); i++)
       
   265 		{
       
   266 		if (feeds[i]->Uid() == aFeedUid)
       
   267 			{
       
   268 			feedsIdx = i;
       
   269 			break;
       
   270 			}
       
   271 		}
       
   272 	for (TInt j = 0; j < iItemIdArray.Count(); j++)
       
   273 		{
       
   274 		if (iItemIdArray[j] == aFeedUid)
       
   275 			{
       
   276 			listboxIdx = j;
       
   277 			break;
       
   278 			}
       
   279 		}
       
   280 		
       
   281 	if (feedsIdx != KErrNotFound && listboxIdx != KErrNotFound)
       
   282 		{
       
   283 		// TODO In the long run we want to move the sorting resposibility from
       
   284 		// CFeedEngine to CPodcastFeedView.
       
   285 		// Hackish fix to make sure the listbox is sorted.
       
   286 		if (listboxIdx != feedsIdx)
       
   287 			{
       
   288 			iItemIdArray.Remove(listboxIdx);
       
   289 			iItemIdArray.InsertL(aFeedUid, feedsIdx);
       
   290 			iItemArray->Delete(listboxIdx);
       
   291 			iItemArray->InsertL(feedsIdx, KNullDesC);
       
   292 			iListContainer->Listbox()->HandleItemAdditionL();
       
   293 			}
       
   294 		// Update the listbox info
       
   295 		UpdateFeedInfoDataL(feeds[feedsIdx], feedsIdx, aIsUpdating);
       
   296 		//TODO sort the listbox after update
       
   297 		}
       
   298 		// Update all visible listbox items that are affected by sorting and update.
       
   299 		TInt minIdx = Max(Min(feedsIdx, listboxIdx), iListContainer->Listbox()->TopItemIndex());
       
   300 		TInt maxIdx = Min(Max(feedsIdx, listboxIdx), iListContainer->Listbox()->BottomItemIndex());
       
   301 		for (TInt k = minIdx; k <= maxIdx; k++)
       
   302 			{
       
   303 			iListContainer->Listbox()->DrawItem(k);
       
   304 			}
       
   305 	}
       
   306 
       
   307 void CPodcastFeedView::FormatFeedInfoListBoxItemL(CFeedInfo& aFeedInfo, TBool aIsUpdating)
       
   308 	{
       
   309 	TBuf<KMaxShortDateFormatSpec*2> updatedDate;
       
   310 	TBuf<KMaxUnplayedFeedsLength> unplayedShows;
       
   311 	TUint unplayedCount = 0;
       
   312 	TUint showCount = 0;
       
   313 	TInt iconIndex = EFeedIcon;
       
   314 	
       
   315 	if(aIsUpdating)
       
   316 		{
       
   317 		iEikonEnv->ReadResourceL(updatedDate, R_PODCAST_FEEDS_IS_UPDATING);
       
   318 		unplayedShows = KNullDesC();			
       
   319 		}
       
   320 	else
       
   321 		{
       
   322 		iPodcastModel.FeedEngine().GetStatsByFeed(aFeedInfo.Uid(), showCount, unplayedCount);	
       
   323 		
       
   324 		if (unplayedCount) {
       
   325 			unplayedShows.Format(*iFeedsFormat, unplayedCount);
       
   326 		} else {
       
   327 			unplayedShows.Zero();
       
   328 		}
       
   329 		
       
   330 		if (aFeedInfo.LastUpdated().Int64() == 0) 
       
   331 			{
       
   332 			updatedDate.Copy(*iNeverUpdated);					
       
   333 			unplayedShows.Zero();
       
   334 			}
       
   335 		else 
       
   336 			{
       
   337 			TTime now;
       
   338 			TTimeIntervalHours interval;
       
   339 			now.HomeTime();
       
   340 			now.HoursFrom(aFeedInfo.LastUpdated(), interval);
       
   341 			if (interval.Int() < KADayInHours) 
       
   342 				{
       
   343 				aFeedInfo.LastUpdated().FormatL(updatedDate, KTimeFormat());
       
   344 				}
       
   345 			else 
       
   346 				{
       
   347 				aFeedInfo.LastUpdated().FormatL(updatedDate, KDateFormatShort());
       
   348 				}
       
   349 			}
       
   350 		}
       
   351 	CArrayPtr<CGulIcon>* icons = iListContainer->Listbox()->ItemDrawer()->FormattedCellData()->IconArray();
       
   352 	
       
   353 	if (aFeedInfo.FeedIconIndex() != -1) {
       
   354 		iconIndex = aFeedInfo.FeedIconIndex();
       
   355 	} else {
       
   356 		if(aFeedInfo.FeedIcon() != NULL && 
       
   357 				aFeedInfo.FeedIcon()->SizeInPixels().iHeight > 0 &&
       
   358 				aFeedInfo.FeedIcon()->SizeInPixels().iWidth > 0)
       
   359 			{
       
   360 			// Hopefully temporary haxx to prevent double delete. I would prefer if
       
   361 			// this could be solved with a little better design.
       
   362 			CFbsBitmap* bmpCopy = new (ELeave) CFbsBitmap;
       
   363 			CleanupStack::PushL(bmpCopy);
       
   364 			bmpCopy->Duplicate(aFeedInfo.FeedIcon()->Handle());
       
   365 			icons->AppendL( CGulIcon::NewL(bmpCopy, NULL));
       
   366 			CleanupStack::Pop(bmpCopy);
       
   367 			iconIndex = icons->Count()-1;
       
   368 			aFeedInfo.SetFeedIconIndex(iconIndex);
       
   369 			}
       
   370 		else {
       
   371 			if(BaflUtils::FileExists(iPodcastModel.FsSession(), aFeedInfo.ImageFileName()))
       
   372 			{
       
   373 			// If this fails, no reason to worry
       
   374 			TRAP_IGNORE(iPodcastModel.ImageHandler().LoadFileAndScaleL(aFeedInfo.FeedIcon(), aFeedInfo.ImageFileName(), TSize(64,56), *this));
       
   375 			}
       
   376 		}
       
   377 	}
       
   378 	
       
   379 	if (unplayedShows.Length() > 0) {
       
   380 		unplayedShows.Insert(0,_L(", "));
       
   381 	}
       
   382 	
       
   383 	iListboxFormatbuffer.Format(KFeedFormat(), iconIndex, &(aFeedInfo.Title()), &updatedDate,  &unplayedShows);
       
   384 	}
       
   385 
       
   386 void CPodcastFeedView::ImageOperationCompleteL(TInt aError)
       
   387 	{
       
   388 	if (aError == KErrNone) {
       
   389 		UpdateListboxItemsL();
       
   390 	}
       
   391 	}
       
   392 
       
   393 void CPodcastFeedView::UpdateFeedInfoDataL(CFeedInfo* aFeedInfo, TInt aIndex, TBool aIsUpdating )
       
   394 	{			
       
   395 	TListItemProperties itemProps;			
       
   396 	itemProps.SetDimmed(aIsUpdating);	
       
   397 	FormatFeedInfoListBoxItemL(*aFeedInfo, aIsUpdating);
       
   398 	
       
   399 	TPtrC compareTo((*iItemArray)[aIndex]);
       
   400 	
       
   401 	if (iListboxFormatbuffer.Compare(compareTo) != 0) {
       
   402 		iItemArray->Delete(aIndex);
       
   403 		if(aIndex>= iItemArray->MdcaCount())
       
   404 				{
       
   405 				iItemArray->AppendL(iListboxFormatbuffer);
       
   406 				}
       
   407 			else
       
   408 				{
       
   409 				iItemArray->InsertL(aIndex, iListboxFormatbuffer);
       
   410 				}
       
   411 	}
       
   412 	iListContainer->Listbox()->ItemDrawer()->SetPropertiesL(aIndex, itemProps);
       
   413 	}
       
   414 
       
   415 
       
   416 void CPodcastFeedView::UpdateListboxItemsL()
       
   417 	{
       
   418 	// No reason to do any work if it isn't going to show..
       
   419 	if(!iListContainer->IsVisible())
       
   420 		{
       
   421 		return;
       
   422 		}
       
   423 	
       
   424 	TInt nbrItems = iPodcastModel.FeedEngine().GetSortedFeeds().Count();
       
   425 	if (nbrItems > 0)
       
   426 		{
       
   427 		// Ensure that there are as many elements in iItemIdArray as in FeedEngine
       
   428 		while (iItemIdArray.Count() < nbrItems)
       
   429 			{
       
   430 			iItemIdArray.AppendL(0);
       
   431 			}
       
   432 		while (iItemIdArray.Count() > nbrItems)
       
   433 			{
       
   434 			iItemIdArray.Remove(iItemIdArray.Count() - 1);
       
   435 			}
       
   436 		
       
   437 		// Ensure that there are as many elements in iItemArray as in FeedEngine
       
   438 		while (iItemArray->Count() < nbrItems)
       
   439 			{
       
   440 			iItemArray->AppendL(KNullDesC);
       
   441 			TListItemProperties itemProps;
       
   442 			iListContainer->Listbox()->ItemDrawer()->SetPropertiesL(iItemArray->Count() - 1, itemProps);
       
   443 			}
       
   444 		while (iItemArray->Count() > nbrItems)
       
   445 			{
       
   446 			iItemArray->Delete(iItemArray->Count() - 1);
       
   447 			}
       
   448 		iUpdater->StartUpdate(nbrItems);
       
   449 		}
       
   450 	else
       
   451 		{
       
   452 		// No feeds at all in the list , add dummy list item
       
   453 		TBuf<KMaxFeedNameLength> itemName;
       
   454 		iEikonEnv->ReadResourceL(itemName, R_PODCAST_FEEDS_NO_FEEDS);
       
   455 		iItemArray->Reset();
       
   456 		iItemIdArray.Reset();
       
   457 	
       
   458 		TListItemProperties itemProps;
       
   459 		itemProps.SetDimmed(ETrue);
       
   460 		itemProps.SetHiddenSelection(ETrue);								
       
   461 		iListContainer->Listbox()->ItemDrawer()->SetPropertiesL(0, itemProps);
       
   462 		}
       
   463 	iListContainer->Listbox()->HandleItemAdditionL();		
       
   464 	}
       
   465 
       
   466 /** 
       
   467  * Command handling function intended for overriding by sub classes. 
       
   468  * Default implementation is empty.  
       
   469  * @param aCommand ID of the command to respond to. 
       
   470  */
       
   471 void CPodcastFeedView::HandleCommandL(TInt aCommand)
       
   472 	{
       
   473 	//CloseToolbarExtension();
       
   474 	switch(aCommand)
       
   475 		{
       
   476         case EPodcastHide:
       
   477 			AppUi()->HandleCommandL(EEikCmdExit);
       
   478 			break;
       
   479 		case EPodcastAddFeed:
       
   480 			HandleAddFeedL();
       
   481 			break;
       
   482 		case EPodcastImportFeeds:
       
   483 			HandleImportFeedsL();
       
   484 			break;
       
   485 		case EPodcastExportFeeds:
       
   486 			HandleExportFeedsL();
       
   487 			break;
       
   488 		case EPodcastEditFeed:
       
   489 			HandleEditFeedL();
       
   490 			break;
       
   491 		case EPodcastDeleteFeedHardware:
       
   492 		case EPodcastDeleteFeed:
       
   493 			HandleRemoveFeedL();
       
   494 			break;
       
   495 		case EPodcastUpdateAllFeeds:
       
   496 			{
       
   497 			iUpdatingAllRunning = ETrue;			
       
   498 			iPodcastModel.FeedEngine().UpdateAllFeedsL();
       
   499 			UpdateToolbar();
       
   500 			}break;
       
   501 		case EPodcastUpdateFeed:
       
   502 			{
       
   503 			HandleUpdateFeedL();
       
   504 			}break;
       
   505 		case EPodcastCancelUpdateAllFeeds:
       
   506 			{
       
   507 			if(iUpdatingAllRunning)
       
   508 				{
       
   509 				iUpdatingAllRunning = EFalse;
       
   510 				iPodcastModel.FeedEngine().CancelUpdateAllFeeds();
       
   511 				}
       
   512 			}break;
       
   513 		case EAknSoftkeyExit:
       
   514 			{
       
   515 			RShowInfoArray dlQueue;
       
   516 			iPodcastModel.ShowEngine().GetShowsDownloadingL(dlQueue);
       
   517 			TUint queueCount = dlQueue.Count();
       
   518 			dlQueue.ResetAndDestroy();
       
   519 			dlQueue.Close();
       
   520 			
       
   521 			if (queueCount > 0 && !iPodcastModel.SettingsEngine().DownloadSuspended())
       
   522 				{
       
   523 				TBuf<KMaxMessageLength> message;
       
   524 				iEikonEnv->ReadResourceL(message, R_EXIT_SHOWS_DOWNLOADING);
       
   525 				if(ShowQueryMessage(message))
       
   526 					{
       
   527 					// pass it on to AppUi, which will exit for us
       
   528 					CPodcastListView::HandleCommandL(aCommand);
       
   529 					}
       
   530 				} 
       
   531 			else
       
   532 				{
       
   533 					// nothing in queue, or downloading suspended
       
   534 					CPodcastListView::HandleCommandL(aCommand);
       
   535 				}
       
   536 			}
       
   537 			break;
       
   538 		default:
       
   539 			CPodcastListView::HandleCommandL(aCommand);
       
   540 			break;
       
   541 		}
       
   542 	UpdateToolbar();
       
   543 	}
       
   544 
       
   545 void CPodcastFeedView::UpdateToolbar()
       
   546 {
       
   547 	CAknToolbar* toolbar = Toolbar();
       
   548 	
       
   549 	if (toolbar)
       
   550 		{
       
   551 		toolbar->HideItem(EPodcastUpdateAllFeeds, iUpdatingAllRunning, ETrue);
       
   552 		toolbar->HideItem(EPodcastCancelUpdateAllFeeds, !iUpdatingAllRunning, ETrue );
       
   553 		toolbar->SetItemDimmed(EPodcastAddFeed, iUpdatingAllRunning, ETrue );
       
   554 		toolbar->SetItemDimmed(EPodcastSettings, iUpdatingAllRunning, ETrue );
       
   555 		}
       
   556 }
       
   557 
       
   558 void CPodcastFeedView::HandleAddFeedL()
       
   559 	{
       
   560 	TBuf<KFeedUrlLength> url;
       
   561 	url.Copy(_L("http://"));
       
   562 	CAknTextQueryDialog * dlg =CAknTextQueryDialog::NewL(url);
       
   563 	dlg->PrepareLC(R_PODCAST_ADD_FEED_DLG);
       
   564 	HBufC* prompt = iEikonEnv->AllocReadResourceLC(R_PODCAST_ADDFEED_PROMPT);
       
   565 	dlg->SetPromptL(*prompt);
       
   566 	CleanupStack::PopAndDestroy(prompt);
       
   567 	
       
   568 	if(dlg->RunLD())
       
   569 		{
       
   570 		
       
   571 		// if no :// we do a search
       
   572 		if (url.Find(_L("://")) == KErrNotFound)
       
   573 			{
       
   574 			HBufC *waitText = iEikonEnv->AllocReadResourceLC(R_SEARCHING);
       
   575 			ShowWaitDialogL(*waitText);
       
   576 			CleanupStack::PopAndDestroy(waitText);	
       
   577 	
       
   578 			iOpmlState = EOpmlSearching;
       
   579 			iPodcastModel.FeedEngine().SearchForFeedL(url);
       
   580 			}
       
   581 		else
       
   582 			{
       
   583 			PodcastUtils::FixProtocolsL(url);
       
   584 	
       
   585 			CFeedInfo* newFeedInfo = CFeedInfo::NewL();
       
   586 			CleanupStack::PushL(newFeedInfo);
       
   587 			newFeedInfo->SetUrlL(url);
       
   588 			newFeedInfo->SetTitleL(newFeedInfo->Url());
       
   589 			
       
   590 			TBool added = iPodcastModel.FeedEngine().AddFeedL(*newFeedInfo);
       
   591 			
       
   592 			if (added)
       
   593 				{
       
   594 				UpdateListboxItemsL();
       
   595 				
       
   596 				// ask if users wants to update it now
       
   597 				TBuf<KMaxMessageLength> message;
       
   598 				iEikonEnv->ReadResourceL(message, R_ADD_FEED_SUCCESS);
       
   599 				if(ShowQueryMessage(message))
       
   600 					{
       
   601 					CFeedInfo *info = iPodcastModel.FeedEngine().GetFeedInfoByUid(newFeedInfo->Uid());
       
   602 					
       
   603 					iPodcastModel.ActiveShowList().Reset();
       
   604 					iPodcastModel.SetActiveFeedInfo(info);			
       
   605 					AppUi()->ActivateLocalViewL(KUidPodcastShowsViewID,  TUid::Uid(0), KNullDesC8());
       
   606 					iPodcastModel.FeedEngine().UpdateFeedL(newFeedInfo->Uid());
       
   607 					}
       
   608 				}
       
   609 			else
       
   610 				{
       
   611 				TBuf<KMaxMessageLength> message;
       
   612 				iEikonEnv->ReadResourceL(message, R_ADD_FEED_EXISTS);
       
   613 				ShowErrorMessage(message);
       
   614 				}		
       
   615 			
       
   616 			CleanupStack::PopAndDestroy(newFeedInfo);
       
   617 			}
       
   618 		}
       
   619 	}
       
   620 
       
   621 void CPodcastFeedView::HandleEditFeedL()
       
   622 	{
       
   623 	TInt index = iListContainer->Listbox()->CurrentItemIndex();
       
   624 	
       
   625 	if(index < iItemArray->MdcaCount() && index >= 0)
       
   626 		{
       
   627 		CFeedInfo *info = iPodcastModel.FeedEngine().GetSortedFeeds()[index];
       
   628 
       
   629 		TBuf<KFeedTitleLength> title;
       
   630 		title.Copy(info->Title());
       
   631 		TBuf<KFeedUrlLength> url;
       
   632 		url.Copy(info->Url());
       
   633 		CAknMultiLineDataQueryDialog  *dlg = CAknMultiLineDataQueryDialog ::NewL(title, url);
       
   634 	
       
   635 		if (dlg->ExecuteLD(R_PODCAST_EDIT_FEED_DLG)) 
       
   636 			{
       
   637 			
       
   638 			if(info->Url().Compare(url) != 0)
       
   639 				{
       
   640 				TBuf<KMaxMessageLength> dlgMessage;
       
   641 				iEikonEnv->ReadResourceL(dlgMessage, R_ADD_FEED_REPLACE);
       
   642 
       
   643 				// Ask the user if it is OK to remove all shows
       
   644 				if ( ShowQueryMessage(dlgMessage))
       
   645 					{
       
   646 					PodcastUtils::FixProtocolsL(url);
       
   647 					
       
   648 					//----- HACK ---- //
       
   649 					CFeedInfo* temp = CFeedInfo::NewLC();
       
   650 					temp->SetUrlL(url);
       
   651 					TBool added = iPodcastModel.FeedEngine().AddFeedL(*temp);
       
   652 					if (added) {
       
   653 						// The Feed URL did not exist
       
   654 						// Remove the temp entry so that the correct entry could be changed
       
   655 						iPodcastModel.FeedEngine().RemoveFeedL(temp->Uid());	
       
   656 						
       
   657 						// user has accepted that shows will be deleted
       
   658 						iPodcastModel.ShowEngine().DeleteAllShowsByFeedL(info->Uid());
       
   659 
       
   660 						// update URL
       
   661 						info->SetUrlL(url);	
       
   662 					
       
   663 						if (info->Title().Compare(title) != 0)
       
   664 							{
       
   665 							info->SetTitleL(title);
       
   666 							info->SetCustomTitle();	
       
   667 							}
       
   668 						iPodcastModel.FeedEngine().UpdateFeed(info);
       
   669 						UpdateListboxItemsL();
       
   670 					} else {
       
   671 						// the feed existed. Object deleted in AddFeed.	
       
   672 						TBuf<KMaxMessageLength> dlgMessage;
       
   673 						iEikonEnv->ReadResourceL(dlgMessage, R_ADD_FEED_EXISTS);
       
   674 						ShowErrorMessage(dlgMessage);
       
   675 					}
       
   676 					CleanupStack::PopAndDestroy(temp);
       
   677 				}
       
   678 			} else { // no url change, maybe title?
       
   679 				// Update the title
       
   680 				if (info->Title().Compare(title) != 0)
       
   681 				{
       
   682 					info->SetTitleL(title);
       
   683 					info->SetCustomTitle();	
       
   684 					iPodcastModel.FeedEngine().UpdateFeed(info);
       
   685 					UpdateListboxItemsL();
       
   686 				}
       
   687 			}
       
   688 		}
       
   689 		}
       
   690 	}
       
   691 
       
   692 void CPodcastFeedView::HandleRemoveFeedL()
       
   693 	{
       
   694 	if(iListContainer->Listbox() != NULL)
       
   695 		{
       
   696 		TInt index = iListContainer->Listbox()->CurrentItemIndex();
       
   697 	
       
   698 		if(index < iItemArray->MdcaCount() && index >= 0)
       
   699 			{
       
   700 			CFeedInfo *info = iPodcastModel.FeedEngine().GetFeedInfoByUid(iItemIdArray[index]);
       
   701 			TBuf<KMaxMessageLength> templ;
       
   702 			TBuf<KMaxMessageLength> message;
       
   703 			iEikonEnv->ReadResourceL(templ, R_PODCAST_REMOVE_FEED_PROMPT);
       
   704 			message.Format(templ, &info->Title());					
       
   705 			if(ShowQueryMessage(message))
       
   706 				{
       
   707 				iPodcastModel.FeedEngine().RemoveFeedL(iItemIdArray[index]);
       
   708 				iItemArray->Delete(index);
       
   709 				iItemIdArray.Remove(index);
       
   710 				iListContainer->Listbox()->HandleItemRemovalL();
       
   711 				iListContainer->Listbox()->DrawNow();
       
   712 				}					
       
   713 			}
       
   714 		UpdateListboxItemsL();
       
   715 		}
       
   716 	}
       
   717 
       
   718 void CPodcastFeedView::HandleUpdateFeedL()
       
   719 	{
       
   720 	TInt index = iListContainer->Listbox()->CurrentItemIndex();
       
   721 	
       
   722 	if(index < iItemArray->MdcaCount() && index >= 0)
       
   723 		{
       
   724 		CFeedInfo *info = iPodcastModel.FeedEngine().GetSortedFeeds()[index];
       
   725 		iPodcastModel.FeedEngine().UpdateFeedL(info->Uid());
       
   726 		}
       
   727 	}
       
   728 
       
   729 void CPodcastFeedView::HandleImportFeedsL()
       
   730 	{
       
   731 	CAknMemorySelectionDialog* memDlg = 
       
   732 		CAknMemorySelectionDialog::NewL(ECFDDialogTypeNormal, ETrue);
       
   733 	CleanupStack::PushL(memDlg);
       
   734 	CAknMemorySelectionDialog::TMemory memory = 
       
   735 		CAknMemorySelectionDialog::EPhoneMemory;
       
   736 
       
   737 	if (memDlg->ExecuteL(memory))
       
   738 		{
       
   739 		TFileName importName;
       
   740 	
       
   741 		if (memory==CAknMemorySelectionDialog::EMemoryCard)
       
   742 		{
       
   743 			importName = PathInfo:: MemoryCardRootPath();
       
   744 		}
       
   745 		else
       
   746 		{
       
   747 			importName = PathInfo:: PhoneMemoryRootPath();
       
   748 		}
       
   749 
       
   750 		CAknFileSelectionDialog* dlg = CAknFileSelectionDialog::NewL(ECFDDialogTypeNormal, R_PODCAST_IMPORT_PODCAST);
       
   751 		CleanupStack::PushL(dlg);
       
   752 
       
   753 		dlg->SetDefaultFolderL(importName);
       
   754 		
       
   755 		if(dlg->ExecuteL(importName))
       
   756 			{
       
   757 			if(importName.Length()>0)
       
   758 				{
       
   759 				HBufC *waitText = iEikonEnv->AllocReadResourceLC(R_IMPORTING);
       
   760 				iOpmlState = EOpmlImporting;
       
   761 				ShowWaitDialogL(*waitText);
       
   762 				CleanupStack::PopAndDestroy(waitText);	
       
   763 
       
   764 				TRAPD(err, iPodcastModel.FeedEngine().ImportFeedsL(importName));
       
   765 									
       
   766 				if (err != KErrNone) {
       
   767 					TBuf<KMaxMessageLength> message;
       
   768 					iEikonEnv->ReadResourceL(message, R_IMPORT_FEED_FAILURE);
       
   769 					ShowErrorMessage(message);
       
   770 					}
       
   771 				}
       
   772 				
       
   773 			}
       
   774 		CleanupStack::PopAndDestroy(dlg);
       
   775 		}
       
   776 	CleanupStack::PopAndDestroy(memDlg);
       
   777 	}
       
   778 
       
   779 void CPodcastFeedView::HandleExportFeedsL()
       
   780 	{
       
   781 	CAknMemorySelectionDialog* memDlg = 
       
   782 		CAknMemorySelectionDialog::NewL(ECFDDialogTypeSave, ETrue);
       
   783 	CleanupStack::PushL(memDlg);
       
   784 	CAknMemorySelectionDialog::TMemory memory = 
       
   785 		CAknMemorySelectionDialog::EPhoneMemory;
       
   786 
       
   787 	if (memDlg->ExecuteL(memory))
       
   788 		{
       
   789 		TFileName pathName;
       
   790 		
       
   791 		if (memory==CAknMemorySelectionDialog::EMemoryCard)
       
   792 		{
       
   793 			pathName = PathInfo::MemoryCardRootPath();
       
   794 		}
       
   795 		else
       
   796 		{
       
   797 			pathName = PathInfo::PhoneMemoryRootPath();
       
   798 		}
       
   799 
       
   800 		CAknFileSelectionDialog* dlg = CAknFileSelectionDialog::NewL(ECFDDialogTypeSave, R_PODCAST_EXPORT_FEEDS);
       
   801 		CleanupStack::PushL(dlg);
       
   802 								
       
   803 		if(dlg->ExecuteL(pathName))
       
   804 			{
       
   805 			CAknFileNamePromptDialog *fileDlg = CAknFileNamePromptDialog::NewL(R_PODCAST_FILENAME_PROMPT_DIALOG);
       
   806 			CleanupStack::PushL(fileDlg);
       
   807 			fileDlg->SetPathL(pathName);
       
   808 			TFileName fileName;
       
   809 			if (fileDlg->ExecuteL(fileName) && fileName.Length() > 0)
       
   810 				{
       
   811 				pathName.Append(fileName);
       
   812 				TFileName temp;
       
   813 				TRAPD(err, iPodcastModel.FeedEngine().ExportFeedsL(temp));						
       
   814 				BaflUtils::CopyFile(iEikonEnv->FsSession(), temp, pathName);
       
   815 				BaflUtils::DeleteFile(iEikonEnv->FsSession(),temp);	
       
   816 				if (err == KErrNone) 
       
   817 					{
       
   818 					UpdateListboxItemsL();
       
   819 					TInt numFeeds = iPodcastModel.FeedEngine().GetSortedFeeds().Count();
       
   820 									
       
   821 					TBuf<KMaxMessageLength> message;
       
   822 					TBuf<KMaxMessageLength> templ;
       
   823 					iEikonEnv->ReadResourceL(templ, R_EXPORT_FEED_SUCCESS);
       
   824 					message.Format(templ, numFeeds);
       
   825 					ShowOkMessage(message);
       
   826 					} 
       
   827 				else 
       
   828 					{
       
   829 					TBuf<KMaxMessageLength> message;
       
   830 					iEikonEnv->ReadResourceL(message, R_EXPORT_FEED_FAILURE);
       
   831 					ShowErrorMessage(message);
       
   832 					}
       
   833 				}
       
   834 			CleanupStack::PopAndDestroy(fileDlg);
       
   835 			}
       
   836 		CleanupStack::PopAndDestroy(dlg);
       
   837 	}
       
   838 	CleanupStack::PopAndDestroy(memDlg);									
       
   839 	}
       
   840 
       
   841 void CPodcastFeedView::CheckResumeDownload()
       
   842 	{
       
   843 	// if there are shows queued for downloading, ask if we should resume now
       
   844 	RShowInfoArray showsDownloading;
       
   845 	iPodcastModel.ShowEngine().GetShowsDownloadingL(showsDownloading);
       
   846 
       
   847 	if (showsDownloading.Count() > 0)
       
   848 		{
       
   849 		TBuf<KMaxMessageLength> msg;
       
   850 		iEikonEnv->ReadResourceL(msg, R_PODCAST_ENABLE_DOWNLOADS_PROMPT);
       
   851 	
       
   852 		if (ShowQueryMessage(msg))
       
   853 			{
       
   854 			// need to suspend downloads before ResumeDownloadL will work :)
       
   855 			iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
       
   856 			// resume downloading if user says yes
       
   857 			iPodcastModel.ShowEngine().ResumeDownloadsL();
       
   858 			}
       
   859 		else
       
   860 			{
       
   861 			// we disable downloading if user says no
       
   862 			iPodcastModel.SettingsEngine().SetDownloadSuspended(ETrue);
       
   863 			}
       
   864 		}
       
   865 	
       
   866 	// if no shows in queue, we keep whichever state suspend is in
       
   867 	showsDownloading.ResetAndDestroy();
       
   868 	}
       
   869 
       
   870 void CPodcastFeedView::OpmlParsingComplete(TUint aNumFeedsImported)
       
   871 	{
       
   872 	DP("CPodcastFeedView::OpmlParsingComplete BEGIN");
       
   873 	
       
   874 	switch (iOpmlState)
       
   875 		{
       
   876 		case EOpmlIdle:
       
   877 			break;
       
   878 		case EOpmlImporting:
       
   879 			{
       
   880 			UpdateListboxItemsL();
       
   881 			delete iWaitDialog;
       
   882 			iOpmlState = EOpmlIdle;
       
   883 				
       
   884 			TBuf<KMaxMessageLength> message;
       
   885 			TBuf<KMaxMessageLength> templ;
       
   886 			iEikonEnv->ReadResourceL(templ, R_IMPORT_FEED_SUCCESS);
       
   887 			message.Format(templ, aNumFeedsImported);
       
   888 			
       
   889 			if(ShowQueryMessage(message))
       
   890 				{
       
   891 				HandleCommandL(EPodcastUpdateAllFeeds);
       
   892 				}
       
   893 			}
       
   894 			break;
       
   895 		case EOpmlSearching:
       
   896 			delete iWaitDialog;
       
   897 			if (iPodcastModel.FeedEngine().GetSearchResults().Count() == 0)
       
   898 				{
       
   899 				TBuf<KMaxMessageLength> message;
       
   900 				iEikonEnv->ReadResourceL(message, R_SEARCH_NORESULTS);
       
   901 				ShowErrorMessage(message);
       
   902 				}
       
   903 			else
       
   904 				{
       
   905 				AppUi()->ActivateLocalViewL(KUidPodcastSearchViewID,  TUid::Uid(0), KNullDesC8());
       
   906 				}
       
   907 			iOpmlState = EOpmlIdle;
       
   908 		default:
       
   909 			break;
       
   910 		}
       
   911 	
       
   912 	DP("CPodcastFeedView::OpmlParsingComplete END");
       
   913 	}
       
   914 
       
   915 void CPodcastFeedView::DialogDismissedL(TInt /*aButtonId*/)
       
   916 	{
       
   917 	iPodcastModel.FeedEngine().CancelUpdateAllFeeds();
       
   918 	}