internetradio2.0/uisrc/irdocument.cpp
changeset 0 09774dfdd46b
equal deleted inserted replaced
-1:000000000000 0:09774dfdd46b
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <aknglobalnote.h>
       
    20 #include <apgwgnam.h>
       
    21 #include <hal.h>
       
    22 #include <internetradio.rsg>
       
    23 #include <stringloader.h>
       
    24 #include <coeaui.h>
       
    25 
       
    26 #include "ir.hrh"
       
    27 #include "irdebug.h"
       
    28 #include "irdocument.h"
       
    29 #include "irui.h"
       
    30 #include "irpubsubkeys.h"
       
    31 
       
    32 
       
    33 // At least 3 MB of free RAM is needed to run the application.
       
    34 const TInt KIRMinimumRAMNeeded = 3145728;
       
    35 // 5 seconds inverval for memory check timer.
       
    36 const TTimeIntervalMicroSeconds32 KIRMemCheckTimerInterval = 5000000;
       
    37 
       
    38 const TInt KBufMaxSize = 255;
       
    39 
       
    40 _LIT( KIRPlsFile, "c:\\private\\2000b499\\tempplsfile.pls" );
       
    41 // ----------------------------------------------------------------------------
       
    42 // CIRDocument::NewL(CEikApplication& aApp).
       
    43 // returns an instance of CIRDocument.
       
    44 // ----------------------------------------------------------------------------
       
    45 //      
       
    46 CIRDocument* CIRDocument::NewL(CEikApplication& aApp)
       
    47     {
       
    48     IRLOG_INFO( "CIRDocument::NewL - Entering" );    
       
    49     CIRDocument* self = new (ELeave) CIRDocument(aApp);
       
    50     CleanupStack::PushL(self);
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop(self);
       
    53     IRLOG_DEBUG( "CIRDocument::NewL - Exiting" );    
       
    54     return self;
       
    55     }
       
    56 
       
    57 // ----------------------------------------------------------------------------
       
    58 // CIRDocument::ConstructL().
       
    59 // ----------------------------------------------------------------------------
       
    60 //  
       
    61 void CIRDocument::ConstructL()
       
    62     {
       
    63     IRLOG_DEBUG( "CIRDocument::ConstructL - Entering" );  
       
    64 	iMemCheckTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
    65     iMemCheckTimer->Start(0, KIRMemCheckTimerInterval, 
       
    66         TCallBack( CIRDocument::MemCheckTimerCallback, this ) );
       
    67     IRLOG_DEBUG( "CIRDocument::ConstructL - Exiting" );
       
    68     }    
       
    69 
       
    70 
       
    71 // ----------------------------------------------------------------------------
       
    72 // CIRDocument::CIRDocument(CEikApplication& aApp).
       
    73 // ----------------------------------------------------------------------------
       
    74 //
       
    75 CIRDocument::CIRDocument(CEikApplication& aApp) : CAknDocument(aApp) 
       
    76     {
       
    77     IRLOG_DEBUG( "CIRDocument::CIRDocument" );
       
    78     }
       
    79 
       
    80 CIRDocument::~CIRDocument()
       
    81     {
       
    82     IRLOG_DEBUG( "CIRDocument::~CIRDocument - Entering" );
       
    83     delete iMemCheckTimer;
       
    84     IRLOG_DEBUG( "CIRDocument::~CIRDocument - Exiting" );
       
    85     }
       
    86     
       
    87 // ----------------------------------------------------------------------------
       
    88 // CIRDocument::CreateAppUiL().
       
    89 // Create the application user interface, and return a pointer to it,the 
       
    90 // framework takes ownership of this object 
       
    91 // ----------------------------------------------------------------------------
       
    92 // 
       
    93 CEikAppUi* CIRDocument::CreateAppUiL()
       
    94     {
       
    95     IRLOG_DEBUG( "CIRDocument::CreateAppUiL - Entering" );
       
    96     iAppUi = new (ELeave) CIRUi;
       
    97     IRLOG_DEBUG( "CIRDocument::CreateAppUiL - Exiting" );
       
    98     return iAppUi;
       
    99     }
       
   100 
       
   101 TInt CIRDocument::MemCheckTimerCallback(TAny* /*aSelf*/)
       
   102     {
       
   103     IRLOG_DEBUG("CIRDocument::MemCheckTimerCallback(TAny*) - Entering");
       
   104     TInt val(0);
       
   105     TInt err = HAL::Get(HALData::EMemoryRAMFree, val);
       
   106     if ( err || (val < KIRMinimumRAMNeeded) )
       
   107         {
       
   108         TRAP_IGNORE(
       
   109             HBufC* errorText = StringLoader::LoadLC( R_IRAPP_MEMLO_RAM_OUT_OF_MEM );
       
   110             CAknGlobalNote* note = CAknGlobalNote::NewLC();
       
   111             note->ShowNoteL( EAknGlobalErrorNote, *errorText );
       
   112             CleanupStack::PopAndDestroy( note );
       
   113             CleanupStack::PopAndDestroy( errorText ) )
       
   114         User::Exit( KErrNoMemory );
       
   115         }
       
   116     IRLOG_DEBUG("CIRDocument::MemCheckTimerCallback(TAny*) - Exiting");
       
   117     return KErrNone;
       
   118     }
       
   119 
       
   120 // ----------------------------------------------------------------------------
       
   121 // CIRDocument::OpenFileL( TBool aDoOpen, const TDesC& aFilename, RFs& aFs )
       
   122 // ----------------------------------------------------------------------------
       
   123 //
       
   124 CFileStore* CIRDocument::OpenFileL( TBool /*aDoOpen*/, const TDesC& aFilename, RFs& /*aFs*/ )
       
   125     {
       
   126     IRLOG_DEBUG("CIRDocument::OpenFileL(TBool, const TDesC&, RFs&)");
       
   127     _LIT(KPlsExtension, ".pls");
       
   128     
       
   129     CIRUi *appui = static_cast<CIRUi *> (iAppUi);
       
   130 
       
   131 	TParse parse;
       
   132 	parse.Set(aFilename,NULL,NULL);
       
   133 	TPtrC ext=parse.Ext(); // extract the extension from the filename  
       
   134 	CEikonEnv* env = CEikonEnv::Static();      
       
   135 	RFs& fs = env->FsSession();
       
   136 	RFile plsFile;
       
   137 	TInt status=plsFile.Open(fs, aFilename, EFileShareReadersOnly);
       
   138 	if(status)
       
   139 		{
       
   140 	    OpenFileL( ETrue, aFilename, fs );
       
   141 		return NULL;	
       
   142 		}
       
   143 	CleanupClosePushL(plsFile);
       
   144 	CFileStore* fileStore;
       
   145 	fileStore = NULL; //So the other OpenFileL version is not called
       
   146 
       
   147 	if (ext.CompareF(KPlsExtension)==0) 
       
   148 		{
       
   149 		appui->ParsePlsDataL(fileStore,plsFile);	
       
   150 		}
       
   151 	CleanupStack::PopAndDestroy( &plsFile );
       
   152 	//Deleting the local copy of the pls file
       
   153 	fs.Delete( KIRPlsFile );	
       
   154 	return NULL;
       
   155     }
       
   156 
       
   157 // ----------------------------------------------------------------------------
       
   158 // CIRDocument::OpenFileL(CFileStore*& aFileStore, RFile& aFile)
       
   159 // ----------------------------------------------------------------------------
       
   160 //
       
   161 
       
   162 void CIRDocument::OpenFileL(CFileStore*& aFileStore, RFile& aFile)
       
   163     {
       
   164 	IRLOG_DEBUG("CIRDocument::OpenFileL(CFileStore*&, RFile&) - Entering");
       
   165 	CIRUi *appui = static_cast<CIRUi *> (iAppUi);
       
   166 	aFileStore = NULL;  //So the other OpenFileL version is not called
       
   167 	
       
   168 	TBuf16<KBufMaxSize> aName;
       
   169 	aFile.FullName(aName);
       
   170 
       
   171 	CEikonEnv* env = CEikonEnv::Static();
       
   172 	TApaTaskList taskList( env->WsSession() );
       
   173 	// Find handler application by its UID
       
   174 	RWsSession& ws = env->WsSession();
       
   175 	const TInt myWgId = env->RootWin().Identifier();
       
   176 	TInt wgId = 0;
       
   177 	TUid uid(KUidActiveInternetRadioApp);
       
   178 	// Look for another instance of this app
       
   179 	while (wgId >= 0)
       
   180 		{
       
   181 		if (wgId && wgId != myWgId)
       
   182 			{
       
   183 			TApaTask FirstInstance(ws);
       
   184 			TApaTask SecondInstance(ws);
       
   185 			FirstInstance.SetWgId(wgId);
       
   186 			SecondInstance.SetWgId(myWgId);
       
   187 			SecondInstance.EndTask();  
       
   188 			
       
   189 		    //To copy the contents of .PLS file into local PLS file 
       
   190 		    
       
   191 		    TInt fileSize;
       
   192 		    User::LeaveIfError( aFile.Size( fileSize ) );
       
   193 			HBufC8* plsData = HBufC8::NewLC( fileSize );
       
   194 			TPtr8 pData8( plsData->Des() );
       
   195 			User::LeaveIfError( aFile.Read( pData8 ) );
       
   196 			RFile plsFile;
       
   197 			RFs& fs = env->FsSession();
       
   198 			TInt ret=plsFile.Open(fs, KIRPlsFile, EFileShareReadersOrWriters);
       
   199 			if(ret==KErrNotFound)
       
   200 				{
       
   201 				TInt status=plsFile.Create(fs, KIRPlsFile, EFileShareReadersOrWriters);
       
   202 				}
       
   203             plsFile.Write(pData8);
       
   204 			TBuf16<KBufMaxSize> plsName;
       
   205 			plsFile.FullName(plsName);
       
   206             
       
   207             CleanupStack::PopAndDestroy( plsData );
       
   208             
       
   209 			User::LeaveIfError( FirstInstance.SwitchOpenFile( plsName ) );
       
   210 			return;
       
   211 			}
       
   212 		CApaWindowGroupName::FindByAppUid(uid, ws, wgId);
       
   213 		}
       
   214 
       
   215 	_LIT(KPlsExtension, ".pls");
       
   216 	TParse parse;
       
   217 	parse.Set(aName,NULL,NULL);
       
   218 	TPtrC ext=parse.Ext(); // extract the extension from the filename
       
   219 	if (ext.CompareF(KPlsExtension)==0) 
       
   220 		{
       
   221 		appui->ParsePlsDataL(aFileStore,aFile);	
       
   222 		}
       
   223 	IRLOG_DEBUG("CIRDocument::OpenFileL - Exiting."); 
       
   224     }
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 // From CEikDocument; Hide the application from the tasklist
       
   228 // ---------------------------------------------------------------------------
       
   229 //    
       
   230 void CIRDocument::UpdateTaskNameL( CApaWindowGroupName* aWgName )
       
   231     {
       
   232     IRLOG_INFO("CIRDocument::UpdateTaskNameL - Hiding InternetRadio from grid - Entering");
       
   233     CAknDocument::UpdateTaskNameL( aWgName );
       
   234 #ifndef __DISABLE_RADIO_LAUNCHER
       
   235     
       
   236     aWgName->SetHidden( EFalse );
       
   237     IRLOG_DEBUG("CIRDocument::UpdateTaskNameL - Exiting."); 
       
   238 #endif
       
   239     }