messagingappbase/msgavkon/Initialise/src/init.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2002 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:  init implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <e32base.h>
       
    23 #include <e32test.h>
       
    24 #include <eikenv.h>
       
    25 #include <eikappui.h>
       
    26 #include <msvapi.h>
       
    27 #include <msvuids.h>
       
    28 #include <mtmuibas.h>
       
    29 #include <mtuireg.h>
       
    30 #include <mtudcbas.h>
       
    31 #include <bautils.h>
       
    32 #include <mtmuidef.hrh>
       
    33 #include <eikdialg.h>
       
    34 #include <featmgr.h>
       
    35 
       
    36 #include "init.h"
       
    37 
       
    38 // CONSTANTS
       
    39 const TInt KUidMsgTypeSMSValue  = 0x1000102C;
       
    40 const TUid KUidMsgTypeSMSUid   = {KUidMsgTypeSMSValue}; 
       
    41 const TInt KUidMsgTypeMMSValue  = 0x100058E1;
       
    42 const TUid KUidMsgTypeMMSUid   = {KUidMsgTypeMMSValue}; 
       
    43 const TInt KUidMsgTypePocaValue = 0x10207245;
       
    44 const TUid KUidMsgTypePocaUid   = {KUidMsgTypePocaValue};
       
    45 const TInt KUidMsgTypeAMSValue  = 0x1020745B;
       
    46 const TUid KUidMsgTypeAMSUid    = {KUidMsgTypeAMSValue};
       
    47 const TInt KMailInitPanicStringLength = 15;
       
    48 
       
    49 _LIT(KInitSpaceBuffer, " ");
       
    50 _LIT(KInitColon, " ");
       
    51 _LIT(KThread,   "MTMInit");
       
    52 _LIT(KPanic,    "MTMInit");
       
    53 #ifdef __WINS__
       
    54 _LIT(KDllName,"\\system\\programs\\mailinit.dll");
       
    55 #endif
       
    56 #ifdef __EPOC32__
       
    57 _LIT(KProcess,  "MTMInit");
       
    58 #endif
       
    59 #ifdef _DEBUG
       
    60 const TInt KInitErrorMaxTextLength=80;
       
    61 #endif
       
    62 
       
    63 const TInt KMailInitRetryCounter = 10;
       
    64 const TInt KMailInitRetryTimeout = 2000000; // two seconds
       
    65 
       
    66 #ifdef __MARM__
       
    67 //  To stop warnings of missing .def file for a EXEDLL compilation
       
    68 //  under MARM (as suggested by Alistair Bradley)
       
    69 //
       
    70 EXPORT_C void Dummy() {};
       
    71 #endif
       
    72 
       
    73 
       
    74 
       
    75 /**
       
    76 *  CDummyObserver 
       
    77 *  This is just a temporary MMsvSessionObserver during the mail init
       
    78 */
       
    79 class CDummyObserver : public CBase, public MMsvSessionObserver
       
    80     {
       
    81     public:
       
    82         CDummyObserver() {};
       
    83         void HandleSessionEventL(
       
    84             TMsvSessionEvent /*aEvent*/, TAny* /*aArg1*/, TAny* /*aArg2*/, TAny* /*aArg3*/) {};
       
    85     };
       
    86 
       
    87 // ----------------------------------------------------
       
    88 // DeleteStoreInitFlagFileL
       
    89 // ----------------------------------------------------
       
    90 void DeleteStoreInitFlagFileL()
       
    91     {
       
    92     }
       
    93 
       
    94 // ----------------------------------------------------
       
    95 // DoInitialiseSingleMtmL
       
    96 // ----------------------------------------------------
       
    97 LOCAL_C void DoInitialiseSingleMtmL(CClientMtmRegistry& aBaseReg, CMtmUiRegistry& aUiReg, const TUid& anId)
       
    98     {
       
    99     INITLOGGER_WRITE_FORMAT2( "DoInitialiseSingleMtmL() - mtm uid: %d 0x%x", anId.iUid, anId.iUid);
       
   100 
       
   101     INITLOGGER("DoInitialiseSingleMtmL() - Creating BaseMtm");
       
   102     //create base mtm
       
   103     CBaseMtm* baseMtm=aBaseReg.NewMtmL(anId);
       
   104     CleanupStack::PushL(baseMtm);
       
   105     
       
   106     INITLOGGER("DoInitialiseSingleMtmL() - Creating Mtm UI");
       
   107     CBaseMtmUi* mtmUi=aUiReg.NewMtmUiL(*baseMtm);
       
   108     CleanupStack::PushL(mtmUi);
       
   109 
       
   110     TInt response;
       
   111     TUid isInstalled={KUidMsvMtmUiQueryMessagingInitialisation};
       
   112     TInt err=mtmUi->QueryCapability(isInstalled, response);
       
   113 
       
   114     if (err==KErrNone)
       
   115         {
       
   116         INITLOGGER("DoInitialiseSingleMtmL() - Invoking initialisation method");
       
   117         // Need to install this type
       
   118         CMsvEntrySelection* tempobject = new (ELeave) CMsvEntrySelection;
       
   119         CleanupStack::PushL(tempobject);
       
   120         TBuf8<1> tempdes;
       
   121         TInt installNow={KMtmUiMessagingInitialisation};    
       
   122         mtmUi->InvokeSyncFunctionL(installNow,*tempobject,tempdes);
       
   123         CleanupStack::PopAndDestroy( tempobject );
       
   124         }
       
   125     else if (err!=KErrNotSupported)
       
   126         {
       
   127         User::Leave(err);
       
   128         }
       
   129 
       
   130     CleanupStack::PopAndDestroy( 2, baseMtm ); // mtmUi,baseMtm
       
   131     INITLOGGER("DoInitialiseSingleMtmL() - Complete");
       
   132     }
       
   133 
       
   134 // ----------------------------------------------------------------------------
       
   135 // GetMtmName()
       
   136 // ----------------------------------------------------------------------------
       
   137 //
       
   138 LOCAL_C void GetMtmName( 
       
   139     TBuf<KMaxKernelName>& aMtmName, 
       
   140     const TUid& aUid,
       
   141     const CMtmUiRegistry& aUiRegistry )
       
   142     {    
       
   143     aMtmName = aUiRegistry.RegisteredMtmDllInfo(
       
   144         aUid ).HumanReadableName().Left( KMailInitPanicStringLength - 1 ); 
       
   145     }
       
   146     
       
   147 // ----------------------------------------------------------------------------
       
   148 // AppendPanicString()
       
   149 // ----------------------------------------------------------------------------
       
   150 //
       
   151 LOCAL_C void AppendPanicString( 
       
   152     TBuf<KMaxKernelName>& aPanicString, 
       
   153     const TUid& aUid,
       
   154     const CMtmUiRegistry& aUiRegistry )
       
   155     {    
       
   156     // Make sure the new length fits to total name
       
   157     if ( aPanicString.Length() < KMaxKernelName - KMailInitPanicStringLength )
       
   158         {
       
   159         GetMtmName( aPanicString, aUid, aUiRegistry );
       
   160         aPanicString.Append( aPanicString );
       
   161         aPanicString.Append( KInitSpaceBuffer );
       
   162         }
       
   163     }
       
   164     
       
   165 // ----------------------------------------------------
       
   166 // DoInitialiseMtmsL
       
   167 // ----------------------------------------------------
       
   168 #ifdef _DEBUG
       
   169 LOCAL_C void DoInitialiseMtmsL( TDes& aMtmBuffer )
       
   170 #else
       
   171 LOCAL_C void DoInitialiseMtmsL( )
       
   172 #endif
       
   173     {
       
   174     TBuf<KMaxKernelName> panicString;
       
   175     panicString.Copy( KThread );
       
   176     panicString.Append( KInitColon );
       
   177     
       
   178     INITLOGGER("DoInitialiseMtmsL()");
       
   179     // connect to the server
       
   180     CDummyObserver* ob = new(ELeave)CDummyObserver;
       
   181     CleanupStack::PushL(ob);
       
   182 
       
   183     INITLOGGER("DoInitialiseMtmsL() : Creating CMsvSession");
       
   184     TInt error = KErrNone;
       
   185     TInt counter = KMailInitRetryCounter;
       
   186     CMsvSession* session = NULL;
       
   187     TRAP( error, session = CMsvSession::OpenSyncL(*ob) );
       
   188     while ( counter > 0 && error != KErrNone )
       
   189         {
       
   190         INITLOGGER("DoInitialiseMtmsL() : Creating CMsvSession failed, wait and try again");
       
   191         counter--;
       
   192         User::After( KMailInitRetryTimeout );
       
   193         TRAP( error, session = CMsvSession::OpenSyncL(*ob) );
       
   194         }
       
   195     if ( error != KErrNone || session == NULL )
       
   196         {
       
   197         INITLOGGER("DoInitialiseMtmsL() : Creating CMsvSession failed, leave now");
       
   198         User::Leave( error != KErrNone ? error : KErrNotFound );
       
   199         }
       
   200     CleanupStack::PushL(session);
       
   201     
       
   202     //create ui registry
       
   203     INITLOGGER("DoInitialiseMtmsL() : Creating Registries");
       
   204     CClientMtmRegistry* basemtmregistry=CClientMtmRegistry::NewL(*session);
       
   205     CleanupStack::PushL(basemtmregistry);
       
   206     CMtmUiRegistry* uiRegistry=CMtmUiRegistry::NewL(*session);
       
   207     CleanupStack::PushL(uiRegistry);
       
   208 
       
   209     //check through mtms
       
   210     TInt ret = 0;
       
   211 
       
   212     // try to initialise SMS first
       
   213     INITLOGGER( "-----------------------< Start INIT: SMS >-----------------------" );
       
   214     if ( uiRegistry->IsPresent( KUidMsgTypeSMSUid ) )
       
   215         {
       
   216         TRAP(ret, DoInitialiseSingleMtmL(*basemtmregistry, *uiRegistry, KUidMsgTypeSMSUid));
       
   217         }
       
   218 #ifdef _DEBUG
       
   219     else
       
   220         {
       
   221         INITLOGGER("DoInitialiseMtmsL() - SMS Not present, this might be ok?");
       
   222         }
       
   223 #endif
       
   224 
       
   225     if (ret)
       
   226         {
       
   227         error = ret;
       
   228         ret = 0;
       
   229         
       
   230         // Track SMS, which can't fail of course
       
   231         AppendPanicString( panicString, KUidMsgTypeSMSUid, *uiRegistry );
       
   232         
       
   233 #ifdef _DEBUG
       
   234         INITLOGGER_WRITE_FORMAT( "***** !!! SMS INIT FAILED (%d) !!! *****", error );  
       
   235         aMtmBuffer.Append( KInitSpaceBuffer );
       
   236         aMtmBuffer.AppendNum( KUidMsgTypeSMSValue );
       
   237 #endif
       
   238         }
       
   239     INITLOGGER( "------------------------< End INIT: SMS >------------------------" );        
       
   240         
       
   241     INITLOGGER( "-----------------------< Start INIT: MMS >-----------------------" );    
       
   242     // then try to initialise MMS 
       
   243     if ( uiRegistry->IsPresent( KUidMsgTypeMMSUid ) )
       
   244         {
       
   245         TRAP(ret, DoInitialiseSingleMtmL(*basemtmregistry, *uiRegistry, KUidMsgTypeMMSUid ));
       
   246         }
       
   247 #ifdef _DEBUG
       
   248     else
       
   249         {
       
   250         INITLOGGER("DoInitialiseMtmsL() - MMS Not present, this might be ok?");
       
   251         }
       
   252 #endif
       
   253 
       
   254     if (ret)
       
   255         {         
       
   256         // Track MMS
       
   257         AppendPanicString( panicString, KUidMsgTypeMMSUid, *uiRegistry );
       
   258 
       
   259         error = ret;
       
   260         ret = 0;       
       
   261         
       
   262 #ifdef _DEBUG
       
   263         INITLOGGER_WRITE_FORMAT( "***** !!! MMS INIT FAILED (%d) !!! *****", error );  
       
   264         aMtmBuffer.Append( KInitSpaceBuffer );
       
   265         aMtmBuffer.AppendNum( KUidMsgTypeMMSValue );
       
   266 #endif
       
   267         }
       
   268 
       
   269     INITLOGGER( "------------------------< End INIT: MMS >------------------------" );
       
   270 
       
   271     //now check through the other mtms
       
   272 #ifdef _DEBUG    
       
   273     TBuf<KMaxKernelName> debugString;
       
   274 #endif // _DEBUG    
       
   275     TInt count=uiRegistry->NumRegisteredMtmDlls();
       
   276     for (TInt cc=0; cc<count; ++cc)
       
   277         {
       
   278         TUid uid=uiRegistry->MtmTypeUid(cc);
       
   279         
       
   280         INITLOGGER_WRITE_FORMAT( "-------------------< Start INIT: 0x%x >------------------", uid.iUid );
       
   281 #ifdef _DEBUG
       
   282         debugString.Zero();
       
   283         GetMtmName( debugString, uid, *uiRegistry );
       
   284         debugString.Append( '\0' );
       
   285         INITLOGGER_WRITE_FORMAT2( "0x%x: %s", uid.iUid, debugString.Ptr() );
       
   286 #endif // _DEBUG
       
   287         
       
   288         TBool initialise = ETrue;
       
   289         // Postcard or Audio messaging MTMs can be variated by runtime, so depending
       
   290         // on the flags they might not be initialized at all.
       
   291         if( uid == KUidMsgTypePocaUid || uid == KUidMsgTypeAMSUid)
       
   292             {
       
   293             TInt flag = -1;
       
   294             if ( uid == KUidMsgTypePocaUid )
       
   295                 {
       
   296                 flag = KFeatureIdMmsPostcard;
       
   297                 }
       
   298             else if ( uid == KUidMsgTypeAMSUid )
       
   299                 {
       
   300                 flag = KFeatureIdAudioMessaging;
       
   301                 }
       
   302             FeatureManager::InitializeLibL();
       
   303             if ( flag != -1 )
       
   304                 {
       
   305                 if ( !FeatureManager::FeatureSupported( flag ) )
       
   306                     {
       
   307                     INITLOGGER( "* Feature NOT supported *" );
       
   308                     initialise = EFalse;
       
   309                     }
       
   310                 }
       
   311             FeatureManager::UnInitializeLib();
       
   312             }
       
   313 
       
   314         if (uid.iUid == KUidMsgTypeSMSValue || uid.iUid == KUidMsgTypeMMSValue )
       
   315             {
       
   316             INITLOGGER( "* Already Initialized *" );
       
   317             initialise = EFalse;
       
   318             }
       
   319 
       
   320 
       
   321         if ( initialise )        
       
   322             {
       
   323             // return value is ignored, so as not to prevent other MTMs being initialised
       
   324             TRAP(ret, DoInitialiseSingleMtmL(*basemtmregistry, *uiRegistry, uid));
       
   325             if (ret)
       
   326                 {
       
   327                      
       
   328                 // Track Other Uids
       
   329                 AppendPanicString( panicString, uid, *uiRegistry );
       
   330 
       
   331                 error=ret;
       
   332 #ifdef _DEBUG
       
   333                 INITLOGGER_WRITE_FORMAT( "***** !!! INIT FAILED (%d) !!! *****", error );            
       
   334                 aMtmBuffer.Append( KInitSpaceBuffer );
       
   335                 aMtmBuffer.AppendNum( (TInt)uid.iUid );
       
   336 #endif
       
   337                 }
       
   338                 
       
   339             INITLOGGER_WRITE_FORMAT( "--------------------< End INIT: 0x%x >-------------------", uid.iUid );    
       
   340             }
       
   341 #ifdef _DEBUG
       
   342         else
       
   343             {
       
   344             INITLOGGER_WRITE_FORMAT( "--------------------< End INIT: NOT done for 0x%x, see reason above >-------------------", uid.iUid );
       
   345             }
       
   346 #endif
       
   347         }
       
   348     
       
   349     if (error)
       
   350         {
       
   351         INITLOGGER_WRITE_FORMAT( "DoInitialiseMtmsL() - %S", &panicString );
       
   352         INITLOGGER_WRITE_FORMAT( "DoInitialiseMtmsL() - Leave error %d", error);
       
   353 
       
   354         User::RenameThread( panicString );
       
   355         Panic( EMailInitisationFailed );
       
   356         }
       
   357 
       
   358     CleanupStack::PopAndDestroy(4); // uiRegistry,basemtmregistry,session,ob
       
   359     }
       
   360 
       
   361 
       
   362 // ----------------------------------------------------
       
   363 // CMailInitAppUi::~CMailInitAppUi
       
   364 // ----------------------------------------------------
       
   365 CMailInitAppUi::~CMailInitAppUi()
       
   366     {
       
   367     }
       
   368 
       
   369 
       
   370 // ----------------------------------------------------
       
   371 // CMailInitAppUi::ConstructL
       
   372 // ----------------------------------------------------
       
   373 void CMailInitAppUi::ConstructL()
       
   374     {
       
   375     CEikAppUi::BaseConstructL(ENonStandardResourceFile|ENoScreenFurniture);
       
   376     }
       
   377 
       
   378 
       
   379 
       
   380 // ----------------------------------------------------
       
   381 // CMailInitServerEnv::DestroyEnvironment
       
   382 // ----------------------------------------------------
       
   383 void CMailInitServerEnv::DestroyEnvironment()
       
   384     {
       
   385     CEikonEnv::DestroyEnvironment();
       
   386     }
       
   387 
       
   388 // ----------------------------------------------------
       
   389 // CMailInitServerEnv::ConstructL
       
   390 // ----------------------------------------------------
       
   391 void CMailInitServerEnv::ConstructL()
       
   392     {
       
   393     CEikonEnv::ConstructL( EFalse );
       
   394     SetAutoForwarding( ETrue );
       
   395     }
       
   396 
       
   397 // ----------------------------------------------------
       
   398 // DoWork
       
   399 // ----------------------------------------------------
       
   400 LOCAL_C void DoWork()
       
   401     {
       
   402     INITLOGGER("DoWork()");
       
   403 
       
   404 #ifndef _DEBUG
       
   405     TRAPD(leave, DoInitialiseMtmsL());
       
   406     if(leave == KErrNone)
       
   407         {
       
   408         // Delete flag file if present.
       
   409         TRAP_IGNORE(DeleteStoreInitFlagFileL());
       
   410         }
       
   411 #else
       
   412     TBuf<2*KInitErrorMaxTextLength> errorBuffer;
       
   413     TRAPD(leave, DoInitialiseMtmsL( errorBuffer ));
       
   414     if(leave == KErrNone)
       
   415         {
       
   416         // Delete flag file if present.
       
   417         TRAP_IGNORE(DeleteStoreInitFlagFileL());
       
   418         return;
       
   419         }
       
   420 
       
   421 #endif // _DEBUG
       
   422     }
       
   423 
       
   424 
       
   425 // ----------------------------------------------------
       
   426 // StartFunction
       
   427 // ----------------------------------------------------
       
   428 GLDEF_C TInt StartFunction()
       
   429     {
       
   430     INITLOGGER("StartFunction()");
       
   431 #ifdef __EPOC32__
       
   432     // Rename our process (on h/w only).
       
   433     User::RenameProcess( KProcess );
       
   434 #endif
       
   435     // Rename our thread.
       
   436     //User::RenameThread
       
   437     User::RenameThread( KThread );
       
   438 
       
   439     INITLOGGER("StartFunction(): Constructing Env and AppUi");
       
   440     CMailInitServerEnv* dialogEnv = new CMailInitServerEnv;
       
   441     __ASSERT_ALWAYS(dialogEnv!=NULL, Panic(EMailInitPanicStartAllocEnv));
       
   442     CMailInitAppUi* dialogAppUi = new CMailInitAppUi;
       
   443     __ASSERT_ALWAYS(dialogAppUi!=NULL, Panic(EMailInitPanicStartAllocAppUi));
       
   444 
       
   445     TRAPD(leave,{dialogEnv->ConstructL(); dialogAppUi->ConstructL();});
       
   446     __ASSERT_ALWAYS(leave==KErrNone, Panic(EMailInitPanicStartConstructL));
       
   447 
       
   448     dialogEnv->DisableExitChecks(ETrue);
       
   449 
       
   450     CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
       
   451     __ASSERT_ALWAYS(cleanup!=NULL, Panic(EMailInitPanicStartAllocCleanupStack));
       
   452     DoWork();
       
   453     delete cleanup; // destroy clean-up stack
       
   454     
       
   455     // finished
       
   456     dialogAppUi->PrepareToExit();
       
   457 
       
   458     dialogEnv->DestroyEnvironment();
       
   459 
       
   460     return KErrNone;
       
   461     }
       
   462 
       
   463 // ----------------------------------------------------
       
   464 // WinsMain
       
   465 // ----------------------------------------------------
       
   466 #ifdef __WINS__
       
   467 EXPORT_C TInt WinsMain(TAny* /*aParam*/)
       
   468     {
       
   469 	  return KErrNone;
       
   470     }
       
   471 #endif
       
   472 
       
   473 // ----------------------------------------------------
       
   474 // E32Main
       
   475 // ----------------------------------------------------
       
   476 GLDEF_C TInt E32Main()
       
   477     {
       
   478     return StartFunction();
       
   479     }
       
   480 
       
   481 // ----------------------------------------------------
       
   482 // Panic
       
   483 // ----------------------------------------------------
       
   484 GLDEF_C void Panic(TMailInitPanic aPanic) // called if the server fails to be created
       
   485     {
       
   486     User::Panic(KPanic, aPanic);
       
   487     }
       
   488 
       
   489 
       
   490 // ----------------------------------------------------
       
   491 // E32Dll
       
   492 // ----------------------------------------------------
       
   493 #ifdef __WINS__
       
   494 GLDEF_C TInt E32Dll(TInt /*aReason*/)
       
   495     {
       
   496     return KErrNone;
       
   497     }
       
   498 #endif
       
   499 
       
   500 // End of file