| 2 |      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 "PodcastSearchView.h"
 | 
|  |     20 | #include "PodcastAppUi.h"
 | 
|  |     21 | #include "FeedEngine.h"
 | 
|  |     22 | #include "ShowEngine.h"
 | 
|  |     23 | #include "SettingsEngine.h"
 | 
|  |     24 | #include "PodcastApp.h"
 | 
|  |     25 | #include "PodcastUtils.h"
 | 
|  |     26 | #include <caknfileselectiondialog.h> 
 | 
|  |     27 | #include <podcast.rsg>
 | 
|  |     28 | #include <podcast.mbg>
 | 
|  |     29 | #include <gulicon.h>
 | 
|  |     30 | #include <eikenv.h>
 | 
|  |     31 | #include <e32const.h>
 | 
|  |     32 | #include <eikdialg.h>
 | 
|  |     33 | #include <aknquerydialog.h>
 | 
|  |     34 | #include <caknmemoryselectiondialog.h> 
 | 
|  |     35 | #include <caknfilenamepromptdialog.h> 
 | 
|  |     36 | #include <BAUTILS.H> 
 | 
|  |     37 | #include <pathinfo.h> 
 | 
|  |     38 | #include <f32file.h>
 | 
|  |     39 | #include <akntoolbarextension.h>
 | 
|  |     40 | #include <akntitle.h>
 | 
|  |     41 | 
 | 
|  |     42 | const TInt KMaxFeedNameLength = 100;
 | 
|  |     43 | const TInt KDefaultGran = 5;
 | 
|  |     44 | const TInt KNumberOfFilesMaxLength = 4;
 | 
|  |     45 | #define KMaxMessageLength 200
 | 
|  |     46 | #define KMaxTitleLength 100
 | 
|  |     47 | const TInt KMimeBufLength = 100;
 | 
|  |     48 | 
 | 
|  |     49 | _LIT(KUnknownUpdateDateString, "?/?");
 | 
|  |     50 | _LIT(KSearchResultFormat, "%d\t%S\t%S");
 | 
|  |     51 | 
 | 
|  |     52 | CPodcastSearchView* CPodcastSearchView::NewL(CPodcastModel& aPodcastModel)
 | 
|  |     53 |     {
 | 
|  |     54 |     CPodcastSearchView* self = CPodcastSearchView::NewLC(aPodcastModel);
 | 
|  |     55 |     CleanupStack::Pop( self );
 | 
|  |     56 |     return self;
 | 
|  |     57 |     }
 | 
|  |     58 | 
 | 
|  |     59 | CPodcastSearchView* CPodcastSearchView::NewLC(CPodcastModel& aPodcastModel)
 | 
|  |     60 |     {
 | 
|  |     61 |     CPodcastSearchView* self = new ( ELeave ) CPodcastSearchView(aPodcastModel);
 | 
|  |     62 |     CleanupStack::PushL( self );
 | 
|  |     63 |     self->ConstructL();
 | 
|  |     64 |     return self;
 | 
|  |     65 |     }
 | 
|  |     66 | 
 | 
|  |     67 | CPodcastSearchView::CPodcastSearchView(CPodcastModel& aPodcastModel):iPodcastModel(aPodcastModel)
 | 
|  |     68 | {
 | 
|  |     69 | }
 | 
|  |     70 | 
 | 
|  |     71 | void CPodcastSearchView::ConstructL()
 | 
|  |     72 | {
 | 
|  |     73 | 	BaseConstructL(R_PODCAST_SEARCHVIEW);
 | 
|  |     74 | 	CPodcastListView::ConstructL();
 | 
|  |     75 | 	iPodcastModel.FeedEngine().AddObserver(this);
 | 
|  |     76 | 	CArrayPtr< CGulIcon >* icons = new(ELeave) CArrayPtrFlat< CGulIcon >(1);
 | 
|  |     77 | 	CleanupStack::PushL( icons );
 | 
|  |     78 | 	
 | 
|  |     79 | 	// Load the bitmap for empty icon	
 | 
|  |     80 | 	CFbsBitmap* bitmap = NULL;
 | 
|  |     81 | 	CFbsBitmap* mask = NULL;//	
 | 
|  |     82 | 	
 | 
|  |     83 | 	// Load the bitmap for feed icon	
 | 
|  |     84 | 
 | 
|  |     85 | 	// Load svg.-image and mask with a single call
 | 
|  |     86 | 		AknIconUtils::CreateIconL(bitmap,
 | 
|  |     87 | 		                          mask,
 | 
|  |     88 | 		                          iEikonEnv->EikAppUi()->Application()->BitmapStoreName(),
 | 
|  |     89 | 		                          EMbmPodcastFeed,
 | 
|  |     90 | 		                          EMbmPodcastFeed_mask);
 | 
|  |     91 | 		/*
 | 
|  |     92 | 	bitmap = iEikonEnv->CreateBitmapL( _L("*"),EMbmPodcastFeed_40x40);*/
 | 
|  |     93 | 	CleanupStack::PushL( bitmap );		
 | 
|  |     94 | 	// Load the mask for feed icon	
 | 
|  |     95 | 	/*mask = iEikonEnv->CreateBitmapL( _L("*"),EMbmPodcastFeed_40x40m );	
 | 
|  |     96 | 	*/
 | 
|  |     97 | 	CleanupStack::PushL( mask );		
 | 
|  |     98 | 	// Append the feed icon to icon array
 | 
|  |     99 | 	icons->AppendL( CGulIcon::NewL( bitmap, mask ) );
 | 
|  |    100 | 	CleanupStack::Pop(2); // bitmap, mask
 | 
|  |    101 | 	
 | 
|  |    102 | 	iListContainer->Listbox()->ItemDrawer()->FormattedCellData()->SetIconArrayL( icons );
 | 
|  |    103 | 	CleanupStack::Pop(icons); // icons
 | 
|  |    104 | 
 | 
|  |    105 | 	iListContainer->Listbox()->SetListBoxObserver(this);
 | 
|  |    106 | 	
 | 
|  |    107 | 	SetEmptyTextL(R_PODCAST_EMPTY_SEARCH);
 | 
|  |    108 | }
 | 
|  |    109 |     
 | 
|  |    110 | CPodcastSearchView::~CPodcastSearchView()
 | 
|  |    111 |     {
 | 
|  |    112 | 	iPodcastModel.FeedEngine().RemoveObserver(this);
 | 
|  |    113 |  
 | 
|  |    114 |     if(iLongTapDetector)
 | 
|  |    115 |         delete iLongTapDetector, iLongTapDetector = NULL;
 | 
|  |    116 | 
 | 
|  |    117 |     if(iStylusPopupMenu)
 | 
|  |    118 |         delete iStylusPopupMenu, iStylusPopupMenu = NULL;
 | 
|  |    119 | 
 | 
|  |    120 |     }
 | 
|  |    121 | 
 | 
|  |    122 | TUid CPodcastSearchView::Id() const
 | 
|  |    123 | {
 | 
|  |    124 | 	return KUidPodcastSearchViewID;
 | 
|  |    125 | }
 | 
|  |    126 | 		
 | 
|  |    127 | void CPodcastSearchView::DoActivateL(const TVwsViewId& aPrevViewId,
 | 
|  |    128 | 	                                  TUid aCustomMessageId,
 | 
|  |    129 | 	                                  const TDesC8& aCustomMessage)
 | 
|  |    130 | {
 | 
|  |    131 | 	UpdateToolbar();
 | 
|  |    132 | 	
 | 
|  |    133 | 	 CAknTitlePane* titlePane = static_cast<CAknTitlePane*>
 | 
|  |    134 | 		      ( StatusPane()->ControlL( TUid::Uid( EEikStatusPaneUidTitle ) ) );
 | 
|  |    135 | 	
 | 
|  |    136 | 	HBufC* title = iEikonEnv->AllocReadResourceLC(R_SEARCH_RESULTS);
 | 
|  |    137 | 	titlePane->SetTextL( *title, ETrue );
 | 
|  |    138 | 	CleanupStack::PopAndDestroy(title);
 | 
|  |    139 |     
 | 
|  |    140 | 	CPodcastListView::DoActivateL(aPrevViewId, aCustomMessageId, aCustomMessage);
 | 
|  |    141 | 	iPreviousView = TVwsViewId(KUidPodcast, KUidPodcastFeedViewID);
 | 
|  |    142 | }
 | 
|  |    143 | 
 | 
|  |    144 | void CPodcastSearchView::DoDeactivate()
 | 
|  |    145 | {
 | 
|  |    146 | 	CPodcastListView::DoDeactivate();
 | 
|  |    147 | 	CAknTitlePane* titlePane = static_cast<CAknTitlePane*>
 | 
|  |    148 | 		     ( StatusPane()->ControlL( TUid::Uid( EEikStatusPaneUidTitle ) ) );
 | 
|  |    149 | 	titlePane->SetTextToDefaultL();
 | 
|  |    150 | }
 | 
|  |    151 | 
 | 
|  |    152 | 
 | 
|  |    153 | void CPodcastSearchView::HandleListBoxEventL(CEikListBox* /* aListBox */, TListBoxEvent aEventType)
 | 
|  |    154 | {
 | 
|  |    155 | 	DP("CPodcastSearchView::HandleListBoxEventL BEGIN");
 | 
|  |    156 | 	switch(aEventType)
 | 
|  |    157 | 	{
 | 
|  |    158 | 	case EEventEnterKeyPressed:
 | 
|  |    159 | 	case EEventItemDoubleClicked:
 | 
|  |    160 | 	case EEventItemActioned:
 | 
|  |    161 | 		{
 | 
|  |    162 | 		HandleCommandL(EPodcastAddSearchResult);
 | 
|  |    163 | 		}
 | 
|  |    164 | 		break;
 | 
|  |    165 | 	default:
 | 
|  |    166 | 		break;
 | 
|  |    167 | 	}
 | 
|  |    168 | 	DP("CPodcastSearchView::HandleListBoxEventL END");
 | 
|  |    169 | }
 | 
|  |    170 | 
 | 
|  |    171 | void CPodcastSearchView::UpdateListboxItemsL()
 | 
|  |    172 | 	{
 | 
|  |    173 | 	DP("CPodcastSearchView::UpdateListboxItemsL BEGIN");
 | 
|  |    174 | 	if(!iListContainer->IsVisible()) {
 | 
|  |    175 | 		DP("CPodcastSearchView::UpdateListboxItemsL END (not visible)");
 | 
|  |    176 | 		return;
 | 
|  |    177 | 	}
 | 
|  |    178 | 	const RFeedInfoArray* searchItems = NULL;
 | 
|  |    179 | 	searchItems = &iPodcastModel.FeedEngine().GetSearchResults();
 | 
|  |    180 | 	TInt len = searchItems->Count();
 | 
|  |    181 | 	TListItemProperties itemProps;
 | 
|  |    182 | 	iListContainer->Listbox()->Reset();
 | 
|  |    183 | 	iListContainer->Listbox()->ItemDrawer()->ClearAllPropertiesL();
 | 
|  |    184 | 	iItemIdArray.Reset();
 | 
|  |    185 | 	iItemArray->Reset();
 | 
|  |    186 | 		
 | 
|  |    187 | 	if (len > 0) 
 | 
|  |    188 | 		{
 | 
|  |    189 | 		for (int i=0;i<len;i++) 
 | 
|  |    190 | 			{				
 | 
|  |    191 | 			CFeedInfo *fi = (*searchItems)[i];
 | 
|  |    192 | 			iItemIdArray.Append(fi->Uid());
 | 
|  |    193 | 			TInt iconIndex = 0;
 | 
|  |    194 | 			
 | 
|  |    195 | 			TBuf<512> descr;
 | 
|  |    196 | 			descr.Copy(fi->Description().Left(512));
 | 
|  |    197 | 			
 | 
|  |    198 | 			iListboxFormatbuffer.Format(KSearchResultFormat(), iconIndex, &fi->Title(), &descr);
 | 
|  |    199 | 			iItemArray->AppendL(iListboxFormatbuffer);
 | 
|  |    200 | 			iListContainer->Listbox()->ItemDrawer()->SetPropertiesL(i, itemProps);
 | 
|  |    201 | 			}
 | 
|  |    202 | 		} 
 | 
|  |    203 | 	else 
 | 
|  |    204 | 		{					
 | 
|  |    205 | 		TBuf<KMaxFeedNameLength> itemName;
 | 
|  |    206 | 		iEikonEnv->ReadResourceL(itemName, R_PODCAST_NO_SEARCH_RESULTS);
 | 
|  |    207 | 		iItemArray->Reset();
 | 
|  |    208 | 		iItemIdArray.Reset();
 | 
|  |    209 | 
 | 
|  |    210 | 		TListItemProperties itemProps;
 | 
|  |    211 | 		itemProps.SetDimmed(ETrue);
 | 
|  |    212 | 		itemProps.SetHiddenSelection(ETrue);								
 | 
|  |    213 | 		iListContainer->Listbox()->ItemDrawer()->SetPropertiesL(0, itemProps);
 | 
|  |    214 | 		}
 | 
|  |    215 | 	iListContainer->Listbox()->HandleItemAdditionL();
 | 
|  |    216 | 	DP("CPodcastSearchView::UpdateListboxItemsL END");
 | 
|  |    217 | 	}
 | 
|  |    218 | 
 | 
|  |    219 | /** 
 | 
|  |    220 |  * Command handling function intended for overriding by sub classes. 
 | 
|  |    221 |  * Default implementation is empty.  
 | 
|  |    222 |  * @param aCommand ID of the command to respond to. 
 | 
|  |    223 |  */
 | 
|  |    224 | void CPodcastSearchView::HandleCommandL(TInt aCommand)
 | 
|  |    225 | 	{
 | 
|  |    226 | 	//CloseToolbarExtension();
 | 
|  |    227 | 	switch(aCommand)
 | 
|  |    228 | 		{
 | 
|  |    229 | 		case EPodcastAddSearchResult:
 | 
|  |    230 | 			{
 | 
|  |    231 | 			TInt index = iListContainer->Listbox()->CurrentItemIndex();
 | 
|  |    232 | 		
 | 
|  |    233 | 			if(index < iItemArray->MdcaCount() && index >= 0)
 | 
|  |    234 | 				{
 | 
|  |    235 | 				CFeedInfo *newInfo = iPodcastModel.FeedEngine().GetSearchResults()[index];
 | 
|  |    236 | 				
 | 
|  |    237 | 				// ask if user wants to add the feed
 | 
|  |    238 | 				TBuf<KMaxMessageLength> templ;
 | 
|  |    239 | 				TBuf<KMaxMessageLength> message;
 | 
|  |    240 | 								
 | 
|  |    241 | 				iEikonEnv->ReadResourceL(templ, R_ADD_FEED_QUERY);
 | 
|  |    242 | 				message.Format(templ, &newInfo->Title());
 | 
|  |    243 | 				if(ShowQueryMessage(message)) {
 | 
|  |    244 | 					TBool added = iPodcastModel.FeedEngine().AddFeedL(*newInfo);
 | 
|  |    245 | 					
 | 
|  |    246 | 					if (added)
 | 
|  |    247 | 						{					
 | 
|  |    248 | 						// ask if user wants to update it now
 | 
|  |    249 | 						TBuf<KMaxMessageLength> message;
 | 
|  |    250 | 						iEikonEnv->ReadResourceL(message, R_ADD_FEED_SUCCESS);
 | 
|  |    251 | 						if(ShowQueryMessage(message))
 | 
|  |    252 | 							{
 | 
|  |    253 | 							CFeedInfo *info = iPodcastModel.FeedEngine().GetFeedInfoByUid(newInfo->Uid());
 | 
|  |    254 | 							
 | 
|  |    255 | 							iPodcastModel.ActiveShowList().Reset();
 | 
|  |    256 | 							iPodcastModel.SetActiveFeedInfo(info);			
 | 
|  |    257 | 							AppUi()->ActivateLocalViewL(KUidPodcastShowsViewID,  TUid::Uid(0), KNullDesC8());
 | 
|  |    258 | 							iPodcastModel.FeedEngine().UpdateFeedL(info->Uid());
 | 
|  |    259 | 							}
 | 
|  |    260 | 						}
 | 
|  |    261 | 					else
 | 
|  |    262 | 						{
 | 
|  |    263 | 						TBuf<KMaxMessageLength> message;
 | 
|  |    264 | 						iEikonEnv->ReadResourceL(message, R_ADD_FEED_EXISTS);
 | 
|  |    265 | 						ShowErrorMessage(message);
 | 
|  |    266 | 						}		
 | 
|  |    267 | 				}
 | 
|  |    268 | 				}
 | 
|  |    269 | 			}
 | 
|  |    270 | 			break;
 | 
|  |    271 | 		default:
 | 
|  |    272 | 			CPodcastListView::HandleCommandL(aCommand);
 | 
|  |    273 | 			break;
 | 
|  |    274 | 		}
 | 
|  |    275 | 		UpdateToolbar();
 | 
|  |    276 | 	}
 | 
|  |    277 | 
 | 
|  |    278 | void CPodcastSearchView::OpmlParsingComplete(TUint /*aNumFeedsImported*/)
 | 
|  |    279 | 	{
 | 
|  |    280 | 	DP("CPodcastSearchView::OpmlParsingComplete BEGIN");
 | 
|  |    281 | 	UpdateListboxItemsL();
 | 
|  |    282 | 	UpdateToolbar();
 | 
|  |    283 | 	DP("CPodcastSearchView::OpmlParsingComplete END");
 | 
|  |    284 | 	}
 | 
|  |    285 | 
 | 
|  |    286 | void CPodcastSearchView::UpdateToolbar()
 | 
|  |    287 | {
 | 
|  |    288 | 	TBool disableAdd = iItemArray->MdcaCount() == 0 || iSearchRunning;
 | 
|  |    289 | 	
 | 
|  |    290 | 	CAknToolbar* toolbar = Toolbar();
 | 
|  |    291 | 	
 | 
|  |    292 | 	if (toolbar)
 | 
|  |    293 | 		{
 | 
|  |    294 | 		toolbar->SetItemDimmed(EPodcastAddSearchResult, disableAdd, ETrue );
 | 
|  |    295 | 		toolbar->HideItem(EPodcastSearch, iSearchRunning, ETrue );
 | 
|  |    296 | 		toolbar->HideItem(EPodcastCancelUpdateAllFeeds, !iSearchRunning, ETrue);
 | 
|  |    297 | 		}
 | 
|  |    298 | }
 |