calendarui/server/CalenSvr/src/CalenSvrDBManager.cpp
branchRCL_3
changeset 66 bd7edf625bdd
child 67 1539a383d7b6
equal deleted inserted replaced
65:12af337248b1 66:bd7edf625bdd
       
     1 /*
       
     2 * Copyright (c) 2004 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:   Provides methods for handling client interaction with server and 
       
    15 *								 contains state machine for handling connection with agenda server.  
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 //debug
       
    22 #include "calendarui_debug.h"
       
    23 
       
    24 // INCLUDES
       
    25 #include "CalenSvrDBManager.h"
       
    26 
       
    27 #include <calenglobaldata.h>
       
    28 
       
    29 #include <e32base.h>
       
    30 #include <sbdefs.h>
       
    31 #include <e32property.h>
       
    32 
       
    33 // LOCAL CONSTANTS AND MACROS
       
    34 // FIXME: change timeout to e.g. 1:30 seconds
       
    35 const TInt KClosingTimeoutMicroSecs(30 /*seconds*/ * 1000000);
       
    36 
       
    37 enum TCalenSvrDBManagerPanic
       
    38     {
       
    39     EPanicInvalidState = 1,
       
    40     EPanicIllegalBootReady,
       
    41     EPanicIllegalOpenCompleted,
       
    42     EPanicIllegalUnregister,
       
    43     EPanicIllegalClosingTimeOut,
       
    44     };
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // ?classname::?member_function
       
    48 // ?implementation_description
       
    49 // (other items were commented in a header).
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 static void Panic(TCalenSvrDBManagerPanic aPanic)
       
    53     {
       
    54     TRACE_ENTRY_POINT;
       
    55 
       
    56     _LIT(KCategory, "CCalenSvrDBManager");
       
    57     User::Panic(KCategory, aPanic);
       
    58 
       
    59     TRACE_EXIT_POINT;
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // ?classname::?member_function
       
    64 // ?implementation_description
       
    65 // (other items were commented in a header).
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 CCalenSvrDBManager* CCalenSvrDBManager::NewL()
       
    69     {
       
    70     TRACE_ENTRY_POINT;
       
    71 
       
    72     CCalenSvrDBManager* self = new (ELeave) CCalenSvrDBManager();
       
    73     CleanupStack::PushL(self);
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop(self);
       
    76 
       
    77     TRACE_EXIT_POINT;
       
    78     return self;
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // ?classname::?member_function
       
    83 // ?implementation_description
       
    84 // (other items were commented in a header).
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 CCalenSvrDBManager::CCalenSvrDBManager()
       
    88     : iState(EStateBooting)
       
    89     {
       
    90     TRACE_ENTRY_POINT;
       
    91     TRACE_EXIT_POINT;
       
    92     }
       
    93 
       
    94 // -----------------------------------------------------------------------------
       
    95 // ?classname::?member_function
       
    96 // ?implementation_description
       
    97 // (other items were commented in a header).
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CCalenSvrDBManager::ConstructL()
       
   101     {
       
   102     TRACE_ENTRY_POINT;
       
   103 
       
   104     iClosingTimer = CPeriodic::NewL(0);
       
   105 
       
   106     // The first instance of global data has
       
   107     // to be created using NewL() which takes a
       
   108     // MCalProgressCallBack callback. We don't need a context change
       
   109     // observer so this is NULL.
       
   110     iGlobalData = CCalenGlobalData::NewL( *this, NULL );
       
   111     iBackupObserver = CPropertyObserver::NewL( *this, KUidSystemCategory, conn::KUidBackupRestoreKey );
       
   112 
       
   113     TRACE_EXIT_POINT;
       
   114     }
       
   115 
       
   116 // -----------------------------------------------------------------------------
       
   117 // CCalenSvrDBManager::Completed
       
   118 // Called when the construction of the instance view
       
   119 // has completed.
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 void CCalenSvrDBManager::Completed(TInt /*aError*/)
       
   123     {
       
   124     TRACE_ENTRY_POINT;
       
   125     // The global data is informed that the instance view
       
   126     // has been created
       
   127     iGlobalData->HandleNotification( ECalenNotifyEntryInstanceViewCreated );
       
   128 
       
   129     TRACE_EXIT_POINT;
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CCalenSvrDBManager::NotifyProgress()
       
   134 // Returns if progress notifications are
       
   135 // required. As there is no interest in
       
   136 // the progress returns EFalse.
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 TBool CCalenSvrDBManager::NotifyProgress()
       
   140     {
       
   141     TRACE_ENTRY_POINT;
       
   142 
       
   143     return EFalse;
       
   144 
       
   145     TRACE_EXIT_POINT;
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CCalenSvrDBManager::Progress
       
   150 // Intentionally empty as there is no interest in the
       
   151 // progress of the instance view creation
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 void CCalenSvrDBManager::Progress(TInt /*aPercentageCompleted*/)
       
   155     {
       
   156     TRACE_ENTRY_POINT;
       
   157     TRACE_EXIT_POINT;
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // ?classname::?member_function
       
   162 // ?implementation_description
       
   163 // (other items were commented in a header).
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 CCalenSvrDBManager::~CCalenSvrDBManager()
       
   167     {
       
   168     TRACE_ENTRY_POINT;
       
   169 
       
   170     delete iBackupObserver;
       
   171     if( iGlobalData )
       
   172         {
       
   173         iGlobalData->Release();
       
   174         iGlobalData = NULL;
       
   175         }
       
   176     delete iClosingTimer;
       
   177     iUsers.Reset();
       
   178 
       
   179     TRACE_EXIT_POINT;
       
   180     }
       
   181 
       
   182 // This method is called by server, when booting is ready.
       
   183 // We need to keep count of registered db users also during booting
       
   184 void CCalenSvrDBManager::BootReadyL()
       
   185     {
       
   186     TRACE_ENTRY_POINT;
       
   187 
       
   188     switch(iState)
       
   189         {
       
   190         case EStateBooting:
       
   191             if (iUsers.Count() > 0)
       
   192                 {
       
   193                 iState = EStateOpeningDB;
       
   194                 if( !iGlobalData )
       
   195                     {
       
   196                     iGlobalData = CCalenGlobalData::Instance();
       
   197                     if ( !iGlobalData )
       
   198                         {
       
   199                         // Services and context change notifiers are NULL.
       
   200                         iGlobalData = CCalenGlobalData::NewL( *this, NULL );
       
   201                         }
       
   202                     }
       
   203                 iGlobalData->InstanceViewL();
       
   204                 OpenDatabaseCompletedL();
       
   205                 }
       
   206             else
       
   207                 {
       
   208                 iState = EStateGoingToCloseDB;
       
   209                 StartClosingTimer();
       
   210                 }
       
   211             break;
       
   212         case EStateRestoring:
       
   213             // ignore
       
   214             break;
       
   215         case EStateOpeningDB:
       
   216         case EStateDBOpen:
       
   217         case EStateGoingToCloseDB:
       
   218         case EStateDBClosed:
       
   219             Panic(EPanicIllegalBootReady);
       
   220             break;
       
   221         default:
       
   222             Panic(EPanicInvalidState);
       
   223         }
       
   224 
       
   225     TRACE_EXIT_POINT;
       
   226     }
       
   227 
       
   228 // RegisterUserL is called either when Calendar Server client session
       
   229 // registers as db user, or when server itself needs to queue alarms
       
   230 // -----------------------------------------------------------------------------
       
   231 // ?classname::?member_function
       
   232 // ?implementation_description
       
   233 // (other items were commented in a header).
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 void CCalenSvrDBManager::RegisterUserL(CCalenSvrDBManager::MCalenDBUser& aUser)
       
   237     {
       
   238     TRACE_ENTRY_POINT;
       
   239 
       
   240     User::LeaveIfError(iUsers.Append(&aUser));
       
   241 
       
   242     switch(iState)
       
   243         {
       
   244         case EStateBooting:
       
   245         case EStateOpeningDB:
       
   246         case EStateRestoring:
       
   247             // do nothing, keep state, users will be notified when db is open
       
   248             break;
       
   249 
       
   250         case EStateDBOpen:
       
   251             // notify user immediately, keep state
       
   252             aUser.DatabaseOpened();
       
   253             break;
       
   254 
       
   255         case EStateGoingToCloseDB:
       
   256             iClosingTimer->Cancel();
       
   257             iState = EStateDBOpen;
       
   258             aUser.DatabaseOpened();
       
   259             break;
       
   260 
       
   261         case EStateDBClosed:
       
   262             iState = EStateOpeningDB;
       
   263             //Chances of leave due to file corruption or no memory.
       
   264             TRAPD(err,
       
   265             if( !iGlobalData )
       
   266                 {
       
   267                 iGlobalData = CCalenGlobalData::Instance();
       
   268                 if ( !iGlobalData )
       
   269                     {
       
   270                     // Context change notifier is NULL.
       
   271                     iGlobalData = CCalenGlobalData::NewL( *this, NULL );
       
   272                     }
       
   273                 }
       
   274             iGlobalData->InstanceViewL();
       
   275 				  );
       
   276 			OpenDatabaseCompletedL(err);
       
   277             break;
       
   278 
       
   279         default:
       
   280             Panic(EPanicInvalidState);
       
   281         }
       
   282 
       
   283     TRACE_EXIT_POINT;
       
   284     }
       
   285 
       
   286 // RegisterUserL is called either when Calendar Server client session
       
   287 // unregisters as db user, or when server has itself has queued alarms
       
   288 void CCalenSvrDBManager::UnregisterUserL(CCalenSvrDBManager::MCalenDBUser& aUser)
       
   289     {
       
   290     TRACE_ENTRY_POINT;
       
   291 
       
   292     TInt pos = iUsers.Find(&aUser);
       
   293     User::LeaveIfError(pos);
       
   294     iUsers.Remove(pos);
       
   295 
       
   296     switch(iState)
       
   297         {
       
   298         case EStateBooting:
       
   299         case EStateOpeningDB:
       
   300         case EStateRestoring:
       
   301             // do nothing, keep state;
       
   302             break;
       
   303 
       
   304         case EStateDBOpen:
       
   305             if (iUsers.Count() > 0)
       
   306                 {
       
   307                 // do nothing, keep state, there are still users
       
   308                 }
       
   309             else
       
   310                 {
       
   311                 iState = EStateGoingToCloseDB;
       
   312                 StartClosingTimer();
       
   313                 }
       
   314             break;
       
   315 
       
   316         case EStateGoingToCloseDB:
       
   317         case EStateDBClosed:
       
   318             Panic(EPanicIllegalUnregister); // invalid state;
       
   319             break;
       
   320 
       
   321         default:
       
   322             Panic(EPanicInvalidState);
       
   323         }
       
   324 
       
   325     TRACE_EXIT_POINT;
       
   326     }
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // ?classname::?member_function
       
   330 // ?implementation_description
       
   331 // (other items were commented in a header).
       
   332 // -----------------------------------------------------------------------------
       
   333 //
       
   334 // From PropertyObserver
       
   335 void CCalenSvrDBManager::HandlePropertyChange(const TUid& /*aCategory*/, const TUint& /*aKey*/, const TInt& aValue)
       
   336     {
       
   337     TRACE_ENTRY_POINT;
       
   338 
       
   339     const TUint backupMask( conn::KBURPartTypeMask ^ conn::EBURNormal );
       
   340 
       
   341     if( aValue & backupMask )
       
   342         {
       
   343         RestoreStarted();
       
   344         }
       
   345     else // aValue == conn::TBURPartType::EBURUnset || aValue & conn::TBURPartType::EBURNormal
       
   346         {
       
   347         // Resume lock
       
   348         PIM_TRAPD_HANDLE( RestoreFinishedL() );
       
   349         }
       
   350 
       
   351     TRACE_EXIT_POINT;
       
   352     }
       
   353 
       
   354 
       
   355 ////////////////////
       
   356 // PRIVATE METHODS
       
   357 ////////////////////
       
   358 
       
   359 // -----------------------------------------------------------------------------
       
   360 // CCalenSvrDBManager::OpenDatabaseCompletedL
       
   361 // OpenDatabaseCompletedL is called back, when engine is ready
       
   362 // (other items were commented in a header).
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 void CCalenSvrDBManager::OpenDatabaseCompletedL(TInt aErrorVal)
       
   366     {
       
   367     TRACE_ENTRY_POINT;
       
   368     switch(iState)
       
   369         {
       
   370         case EStateOpeningDB:
       
   371             {
       
   372             if (iUsers.Count() > 0)
       
   373                 {
       
   374                 NotifyUsersL(aErrorVal);
       
   375                 iState = EStateDBOpen;
       
   376                 }
       
   377             else
       
   378                 {
       
   379                 iState = EStateGoingToCloseDB;
       
   380                 StartClosingTimer();
       
   381                 }
       
   382             }
       
   383             break;
       
   384 
       
   385         case EStateRestoring:
       
   386         case EStateBooting:
       
   387         case EStateDBOpen:
       
   388         case EStateGoingToCloseDB:
       
   389         case EStateDBClosed:
       
   390             Panic(EPanicIllegalOpenCompleted); // invalid state;
       
   391             break;
       
   392 
       
   393         default:
       
   394             Panic(EPanicInvalidState);
       
   395         }
       
   396 
       
   397     TRACE_EXIT_POINT;
       
   398     }
       
   399 
       
   400 // Closing Timeout is called when closing timer fires
       
   401 // -----------------------------------------------------------------------------
       
   402 // ?classname::?member_function
       
   403 // ?implementation_description
       
   404 // (other items were commented in a header).
       
   405 // -----------------------------------------------------------------------------
       
   406 //
       
   407 void CCalenSvrDBManager::ClosingTimeOut()
       
   408     {
       
   409     TRACE_ENTRY_POINT;
       
   410 
       
   411     iClosingTimer->Cancel();
       
   412 
       
   413     switch(iState)
       
   414         {
       
   415         case EStateGoingToCloseDB:
       
   416             if( iGlobalData )
       
   417                 {
       
   418                 iGlobalData->Release();
       
   419                 iGlobalData = NULL;
       
   420                 }
       
   421             iState = EStateDBClosed;
       
   422             break;
       
   423 
       
   424         case EStateRestoring:
       
   425         case EStateBooting:
       
   426         case EStateOpeningDB:
       
   427         case EStateDBOpen:
       
   428         case EStateDBClosed:
       
   429             Panic(EPanicIllegalClosingTimeOut); // invalid state;
       
   430             break;
       
   431 
       
   432         default:
       
   433             Panic(EPanicInvalidState);
       
   434         }
       
   435 
       
   436     TRACE_EXIT_POINT;
       
   437     }
       
   438 
       
   439 // ClosingTimeOutCallback is just static callback function to wrap actual
       
   440 // object method
       
   441 // -----------------------------------------------------------------------------
       
   442 // ?classname::?member_function
       
   443 // ?implementation_description
       
   444 // (other items were commented in a header).
       
   445 // -----------------------------------------------------------------------------
       
   446 //
       
   447 TInt CCalenSvrDBManager::ClosingTimeOutCallback(TAny* aManager)
       
   448     {
       
   449     TRACE_ENTRY_POINT;
       
   450 
       
   451     static_cast<CCalenSvrDBManager*>(aManager)->ClosingTimeOut();
       
   452 
       
   453     TRACE_EXIT_POINT;
       
   454     return 0;
       
   455     }
       
   456 
       
   457 // Helper function
       
   458 // -----------------------------------------------------------------------------
       
   459 // ?classname::?member_function
       
   460 // ?implementation_description
       
   461 // (other items were commented in a header).
       
   462 // -----------------------------------------------------------------------------
       
   463 //
       
   464 void CCalenSvrDBManager::StartClosingTimer()
       
   465     {
       
   466     TRACE_ENTRY_POINT;
       
   467 
       
   468     TTimeIntervalMicroSeconds32 delay(KClosingTimeoutMicroSecs);
       
   469     iClosingTimer->Start(
       
   470     delay, delay,
       
   471     TCallBack(&CCalenSvrDBManager::ClosingTimeOutCallback, this));
       
   472 
       
   473     TRACE_EXIT_POINT;
       
   474     }
       
   475 
       
   476 // -----------------------------------------------------------------------------
       
   477 // ?classname::?member_function
       
   478 // ?implementation_description
       
   479 // (other items were commented in a header).
       
   480 // -----------------------------------------------------------------------------
       
   481 //
       
   482 void CCalenSvrDBManager::RestoreStarted()
       
   483     {
       
   484     TRACE_ENTRY_POINT;
       
   485 
       
   486     switch(iState)
       
   487         {
       
   488         case EStateDBOpen:
       
   489             {
       
   490             if( iGlobalData )
       
   491                 {
       
   492                 iGlobalData->Release();
       
   493                 iGlobalData = NULL;
       
   494                 }
       
   495             // Notify users of temporarily closing
       
   496             for(TInt i=0; i<iUsers.Count(); ++i)
       
   497                 {
       
   498                 iUsers[i]->DatabaseTemporarilyClosed();
       
   499                 }
       
   500             iState = EStateRestoring;
       
   501             }
       
   502             break;
       
   503 
       
   504         case EStateOpeningDB:
       
   505             if( iGlobalData )
       
   506                 {
       
   507                 iGlobalData->Release();
       
   508                 iGlobalData = NULL;
       
   509                 }
       
   510             iState = EStateRestoring;
       
   511             break;
       
   512 
       
   513         case EStateGoingToCloseDB:
       
   514             iClosingTimer->Cancel();
       
   515             if( iGlobalData )
       
   516                 {
       
   517                 iGlobalData->Release();
       
   518                 iGlobalData = NULL;
       
   519                 }
       
   520             iState = EStateDBClosed;
       
   521             break;
       
   522 
       
   523         case EStateRestoring:
       
   524         case EStateBooting:
       
   525         case EStateDBClosed:
       
   526             break;
       
   527 
       
   528         default:
       
   529             Panic(EPanicInvalidState);
       
   530         }
       
   531 
       
   532     TRACE_EXIT_POINT;
       
   533     }
       
   534 
       
   535 // -----------------------------------------------------------------------------
       
   536 // ?classname::?member_function
       
   537 // ?implementation_description
       
   538 // (other items were commented in a header).
       
   539 // -----------------------------------------------------------------------------
       
   540 //
       
   541 void CCalenSvrDBManager::RestoreFinishedL()
       
   542     {
       
   543     TRACE_ENTRY_POINT;
       
   544 
       
   545     switch(iState)
       
   546         {
       
   547         case EStateRestoring:
       
   548             {
       
   549             if(iUsers.Count())
       
   550                 {
       
   551                 iState = EStateOpeningDB;
       
   552                 if( !iGlobalData )
       
   553                     {
       
   554                     iGlobalData = CCalenGlobalData::Instance();
       
   555                     if ( !iGlobalData )
       
   556                         {
       
   557                         // Context change notifier is NULL.
       
   558                         iGlobalData = CCalenGlobalData::NewL( *this, NULL );
       
   559                         }
       
   560                     }
       
   561                 iGlobalData->InstanceViewL();
       
   562                 OpenDatabaseCompletedL();
       
   563                 }
       
   564             else
       
   565                 {
       
   566                 iState = EStateDBClosed;
       
   567                 }
       
   568             break;
       
   569             }
       
   570         case EStateDBOpen:
       
   571         case EStateOpeningDB:
       
   572         case EStateGoingToCloseDB:
       
   573         case EStateBooting:
       
   574         case EStateDBClosed:
       
   575             break;
       
   576         default:
       
   577             Panic(EPanicInvalidState);
       
   578         }
       
   579     TRACE_EXIT_POINT;
       
   580     }
       
   581 
       
   582 // -----------------------------------------------------------------------------
       
   583 // CCalenSvrDBManager::NotifyUsersL
       
   584 // (other items were commented in a header).
       
   585 // -----------------------------------------------------------------------------
       
   586 //
       
   587 void CCalenSvrDBManager::NotifyUsersL(TInt aErrorVal)
       
   588     {
       
   589     TRACE_ENTRY_POINT;
       
   590 
       
   591     RPointerArray<MCalenDBUser> users( iUsers.Count() );
       
   592     CleanupClosePushL( users ); // content not owned
       
   593 
       
   594     for(TInt i(0); i<iUsers.Count(); i++)
       
   595         {
       
   596         users.Append( iUsers[i] );
       
   597         }
       
   598 
       
   599     for( TInt i(0); i < users.Count(); ++i )
       
   600         {
       
   601         if(aErrorVal != KErrNone)
       
   602         	{
       
   603         	users[i]->HandleError();
       
   604         	}
       
   605          else
       
   606          	{
       
   607          	users[i]->DatabaseOpened();
       
   608          	}
       
   609         }
       
   610 
       
   611     CleanupStack::PopAndDestroy( &users );
       
   612 
       
   613     TRACE_EXIT_POINT;
       
   614     }
       
   615 
       
   616 
       
   617 // End of File