calendarui/globaldata/src/calenglobaldata.cpp
branchRCL_3
changeset 29 12af337248b1
parent 28 96907930389d
child 30 bd7edf625bdd
equal deleted inserted replaced
28:96907930389d 29:12af337248b1
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:   Global Data for Calendar application
    14 * Description:  Global Data for Calendar application
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 
       
    20 //debug
    19 //debug
    21 #include "calendarui_debug.h"
    20 #include "calendarui_debug.h"
    22 
    21 
    23 // INCLUDE FILES
    22 // INCLUDE FILES
    24 #include "calenglobaldata.h"        // CCalenGlobalData
    23 #include "calenglobaldata.h"        // CalenGlobalData
    25 #include "RImplInfoPtrArrayOwn.inl" // Wrapper class with array deletion on close
    24 #include "calencontext.h"           // Calendar context
    26 #include "calensend.h"              // CCalenSend
       
    27 #include "CalenInterimUtils2.h"     // CalenInterimUtils
       
    28 #include "calencontextimpl.h"       // Calendar context implementation
    25 #include "calencontextimpl.h"       // Calendar context implementation
    29 #include "calenfilemapping.h"
    26 #include "hb_calencommands.hrh"
    30 #include "missedalarmstore.h"
    27 #include <CalSession.h>             // Calendar database session
    31 #include "CleanupResetAndDestroy.h"
       
    32 #include "calendar.hrh"
       
    33 
       
    34 #include <Calendar.rsg>             // Calendar resources
       
    35 #include <cmrmailboxutils.h>        // CMRMailboxUtils
       
    36 #include <utf.h>                    // CnvUtfConverter
       
    37 #include <StringLoader.h>           // Loads strings from resource files
       
    38 #include <featmgr.h>                // Feature discovery API
       
    39 #include <calsession.h>             // Calendar database session
       
    40 #include <calinstanceview.h>        // Calendar Instance view
    28 #include <calinstanceview.h>        // Calendar Instance view
    41 #include <calentryview.h>           // Calendar Entry view
    29 #include <calentryview.h>           // Calendar Entry view
    42 #include <cmrmailboxutils.h>        // CMRMailboxUtils
    30 
    43 #include <calencommands.hrh>            // Calendar commands
       
    44 #include <aknlists.h>
       
    45 #include <aknPopup.h>
       
    46 #include <AknUtils.h>
       
    47 #include <AknQueryDialog.h>
       
    48 #include <calencontext.h>
       
    49 #include <calcalendarinfo.h>
       
    50 #include <calcalendariterator.h>
       
    51 #include <CalendarInternalCRKeys.h>
       
    52 #include <calenmulticalutil.h>
       
    53 
       
    54 #include <bautils.h>
       
    55 #include <calensvrmissedalarmmanagerresource.rsg>
       
    56 // Default Calendar database path
    31 // Default Calendar database path
    57 _LIT( KCalendarDatabaseFilePath, "c:calendar" );
    32 _LIT( KCalendarDatabaseFilePath, "c:Calendar" );
    58 _LIT( KPhoneCalendar,"Personal" );
    33 
    59 _LIT( KExtCalendar,"Ext calendar" );
       
    60 const TInt KBuffLength = 24;
       
    61 
       
    62 
       
    63 _LIT(KPersonal,"Personal");
       
    64 
       
    65 _LIT( KMissedAlarmResourceFile, "z:\\resource\\CalenSvrMissedAlarmManagerResource.rsc"); // changes done
       
    66 
    34 
    67 // ============================ MEMBER FUNCTIONS ===============================
    35 // ============================ MEMBER FUNCTIONS ===============================
    68 
    36 
    69 // -----------------------------------------------------------------------------
    37 // -----------------------------------------------------------------------------
    70 // CCalenGlobalData::CCalenGlobalData
    38 // CCalenGlobalData::CCalenGlobalData
    71 // C++ default constructor can NOT contain any code, that might leave.
    39 // C++ default constructor can NOT contain any code, that might leave.
    72 // (other items were commented in a header).
    40 // (other items were commented in a header).
    73 // -----------------------------------------------------------------------------
    41 // -----------------------------------------------------------------------------
    74 //
    42 //
       
    43 
    75 CCalenGlobalData::CCalenGlobalData( MCalProgressCallBack& aCalCallBack )
    44 CCalenGlobalData::CCalenGlobalData( MCalProgressCallBack& aCalCallBack )
    76     : iCalCallBack( aCalCallBack )
    45     : iCalCallBack( aCalCallBack )
    77     {
    46     {
    78     TRACE_ENTRY_POINT;
    47     TRACE_ENTRY_POINT;
    79 
    48 
    80     iGlobalDataOwnsCalSession = EFalse;
    49     iGlobalDataOwnsCalSession = EFalse;
    81     iGlobalDataOwnsEntryView = EFalse;
    50 
    82     
       
    83     iCalendarForcedExit = EFalse;
       
    84     
       
    85     TRACE_EXIT_POINT;
    51     TRACE_EXIT_POINT;
    86     }
    52     }
    87 
    53 
    88 // ----------------------------------------------------------------------------
    54 // ----------------------------------------------------------------------------
    89 // CCalenGlobalData::NewL
    55 // CCalenGlobalData::NewL
    93 // which should be handled by the controller.
    59 // which should be handled by the controller.
    94 // (other items were commented in a header).
    60 // (other items were commented in a header).
    95 // ----------------------------------------------------------------------------
    61 // ----------------------------------------------------------------------------
    96 //
    62 //
    97 EXPORT_C CCalenGlobalData* CCalenGlobalData::NewL( MCalProgressCallBack& aCalCallBack,
    63 EXPORT_C CCalenGlobalData* CCalenGlobalData::NewL( MCalProgressCallBack& aCalCallBack,
    98                                                    MCalenContextChangeObserver* aNotifier,
    64                                                    MCalenContextChangeObserver* aNotifier )
    99                                                    MCalenDBChangeObserver* aDBNotifier)
    65     {
   100     {
    66     TRACE_ENTRY_POINT;
   101     TRACE_ENTRY_POINT;
       
   102 
       
   103     CCalenGlobalData* self = NULL;
    67     CCalenGlobalData* self = NULL;
   104     TAny* tlsPtr = Dll::Tls();
    68     TAny* tlsPtr = Dll::Tls();
   105 
    69 
   106     // Check Thread local storage
    70     // Check Thread local storage
   107     if (!tlsPtr)
    71     if (!tlsPtr)
   113         User::LeaveIfError( Dll::SetTls( static_cast<TAny*>( self ) ) );
    77         User::LeaveIfError( Dll::SetTls( static_cast<TAny*>( self ) ) );
   114         // Increment ref count right away. If we don't do it here, and someone
    78         // Increment ref count right away. If we don't do it here, and someone
   115         // calls CCalenGlobalData::InstanceL in ConstructL and then ConstructL
    79         // calls CCalenGlobalData::InstanceL in ConstructL and then ConstructL
   116         // leaves, we will double delete the global data.
    80         // leaves, we will double delete the global data.
   117         ++self->iRefCount;
    81         ++self->iRefCount;
   118         self->ConstructL( aNotifier, aDBNotifier );
    82         self->ConstructL( aNotifier );
   119         CleanupStack::Pop( self );
    83         CleanupStack::Pop( self );
   120         }
    84         }
   121     else
    85     else
   122         {
    86         {
   123         // An instance of the global data exists already.
    87         // An instance of the global data exists already.
   133 // CCalenGlobalData::InstanceL
    97 // CCalenGlobalData::InstanceL
   134 // Two-phased constructor.
    98 // Two-phased constructor.
   135 // (other items were commented in a header).
    99 // (other items were commented in a header).
   136 // -----------------------------------------------------------------------------
   100 // -----------------------------------------------------------------------------
   137 EXPORT_C CCalenGlobalData* CCalenGlobalData::InstanceL()
   101 EXPORT_C CCalenGlobalData* CCalenGlobalData::InstanceL()
   138     {
   102     {    	
   139     TRACE_ENTRY_POINT;
   103     TRACE_ENTRY_POINT;
   140 
   104 
   141     CCalenGlobalData* self = NULL;
   105     CCalenGlobalData* self = NULL;
   142     TAny* tlsPtr = Dll::Tls();
   106     TAny* tlsPtr = Dll::Tls();
   143 
   107 
   144     // Check Thread local storage
   108     // Check Thread local storage
   145     if (!tlsPtr)
   109     if (!tlsPtr)
   146         {
   110         {
   147         // The global data has not yet been constructed.
   111         // The global data has not yet been constructed.
   148         // FIXME - Revise comment
   112         // FIXME - Revise comment
   149         //TLS is NULL, so no CCalenGlobalData has been created yet.
   113         //TLS is NULL, so no CCalenGlobalData has been created yet.      
   150       /*  self = new( ELeave ) CCalenGlobalData;
       
   151         CleanupStack::PushL( self );
       
   152         self->ConstructL();
       
   153         //Store a self pointer in TLS
       
   154         User::LeaveIfError( Dll::SetTls(static_cast<TAny*>(self)));
       
   155         CleanupStack::Pop(self);*/
       
   156         User::Leave( KErrNotReady );
   114         User::Leave( KErrNotReady );
   157         }
   115         }
   158     else
   116     else
   159         {
   117         {
   160         self = static_cast<CCalenGlobalData*>( tlsPtr );
   118         self = static_cast<CCalenGlobalData*>( tlsPtr );
   195 // (other items were commented in a header).
   153 // (other items were commented in a header).
   196 // -----------------------------------------------------------------------------
   154 // -----------------------------------------------------------------------------
   197 EXPORT_C CCalenGlobalData* CCalenGlobalData::Instance()
   155 EXPORT_C CCalenGlobalData* CCalenGlobalData::Instance()
   198     {
   156     {
   199     TRACE_ENTRY_POINT;
   157     TRACE_ENTRY_POINT;
   200 
       
   201     TAny* tlsPtr = Dll::Tls();
   158     TAny* tlsPtr = Dll::Tls();
   202     CCalenGlobalData* self = static_cast<CCalenGlobalData*>( tlsPtr );
   159     CCalenGlobalData* self = static_cast<CCalenGlobalData*>( tlsPtr );
   203 
   160 
   204     if ( self )
   161     if ( self )
   205         {
   162         {
   213 // -----------------------------------------------------------------------------
   170 // -----------------------------------------------------------------------------
   214 // CCalenGlobalData::ConstructL
   171 // CCalenGlobalData::ConstructL
   215 // Symbian 2nd phase constructor can leave.
   172 // Symbian 2nd phase constructor can leave.
   216 // (other items were commented in a header).
   173 // (other items were commented in a header).
   217 // -----------------------------------------------------------------------------
   174 // -----------------------------------------------------------------------------
   218 void CCalenGlobalData::ConstructL( MCalenContextChangeObserver* aNotifier,
   175 void CCalenGlobalData::ConstructL( MCalenContextChangeObserver* aNotifier)
   219                                     MCalenDBChangeObserver* aDBNotifier)
   176     {
   220     {
   177     TRACE_ENTRY_POINT;
   221     TRACE_ENTRY_POINT;
   178 
   222 
   179     iContext = new (ELeave ) CalenContextImpl( aNotifier );
   223     iContext = new (ELeave ) CCalenContextImpl( aNotifier );  
   180 
   224     
       
   225     iNewInstanceViewCreation = NULL;
       
   226     
       
   227     iDBChangeNotifier = aDBNotifier;
       
   228     
       
   229     TRACE_EXIT_POINT;
   181     TRACE_EXIT_POINT;
   230     }
   182     }
   231 
   183 
   232 // -----------------------------------------------------------------------------
   184 // -----------------------------------------------------------------------------
   233 // CCalenGlobalData::SetSessionL
   185 // CCalenGlobalData::SetSessionL
   278 // (other items were commented in a header).
   230 // (other items were commented in a header).
   279 // -----------------------------------------------------------------------------
   231 // -----------------------------------------------------------------------------
   280 CCalenGlobalData::~CCalenGlobalData()
   232 CCalenGlobalData::~CCalenGlobalData()
   281     {
   233     {
   282     TRACE_ENTRY_POINT;
   234     TRACE_ENTRY_POINT;
   283 
       
   284     delete iSend;
       
   285     delete iInterimUtils;
       
   286     delete iUtilsAs;
       
   287     delete iMailboxUtils;
       
   288     delete iMRUtils;
       
   289 
       
   290     if (iInstanceView)
       
   291         {
       
   292         delete iInstanceView;
       
   293         iInstanceView = NULL;
       
   294         }
       
   295 
       
   296     if (iInstanceViewCreation)
       
   297         {
       
   298         delete iInstanceViewCreation;
       
   299         iInstanceViewCreation = NULL;
       
   300         }
       
   301 
       
   302     if (iEntryViewCreation)
       
   303         {
       
   304         delete iEntryViewCreation;
       
   305         iEntryViewCreation = NULL;
       
   306         }
       
   307     
   235     
   308     if (iNewInstanceView)
   236     delete iInterimUtils;    
   309         {
   237     delete iEntryView;
   310         delete iNewInstanceView;
   238     delete iInstanceView;
   311         iNewInstanceView = NULL;
   239     delete iEntryViewCreation;
   312         iNewInstanceViewCreation = NULL;
   240     delete iInstanceViewCreation;
   313         }
       
   314 
       
   315     if (iNewInstanceViewCreation)
       
   316         {
       
   317         delete iNewInstanceViewCreation;
       
   318         iNewInstanceViewCreation = NULL;
       
   319         }
       
   320 
       
   321     delete iContext;
   241     delete iContext;
       
   242 
       
   243     if (iGlobalDataOwnsCalSession)
       
   244         {
       
   245         delete iCalSession;
       
   246         }
   322     
   247     
   323     if(iGlobalDataOwnsEntryView)
   248     --iRefCount;
   324         {
       
   325         if(iEntryView)
       
   326             {
       
   327             delete iEntryView;
       
   328             iEntryView = NULL;           
       
   329             }
       
   330         }
       
   331 
       
   332    if (iGlobalDataOwnsCalSession)
       
   333         {
       
   334         delete iCalSession;
       
   335         }
       
   336     
   249     
   337    if (iCalendarInfoList.Count())
   250     // The controller owns its own instance of the services, therefore the
   338        {
   251     // reference count will be one, immediatley before deletion.
   339        iCalendarInfoList.ResetAndDestroy();
   252     if (iRefCount == 1)
   340        }
   253         {
   341    
   254         delete this;
   342     if( iFileMappingArray.Count() )
   255         }
   343         {
       
   344         iFileMappingArray.ResetAndDestroy();
       
   345         }
       
   346     
       
   347     iKeyQueue.Reset();
       
   348     
       
   349     iHashDbidIndexMap.Close();
       
   350 
   256 
   351     Dll::SetTls(NULL);
   257     Dll::SetTls(NULL);
   352 
   258 
   353     if (iCalendarsSession)
   259     TRACE_EXIT_POINT;
   354         {
   260     }
   355         delete iCalendarsSession;
   261 
   356         iCalendarsSession = NULL;
       
   357         }
       
   358 
       
   359     TRACE_EXIT_POINT;
       
   360     }
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CCalenGlobalData::MailboxUtilsL
       
   364 // Create MailBoxUtils if necessary and returns a reference
       
   365 // (other items were commented in a header).
       
   366 // -----------------------------------------------------------------------------
       
   367 EXPORT_C CMRMailboxUtils& CCalenGlobalData::MRMailboxUtilsL()
       
   368     {
       
   369     TRACE_ENTRY_POINT;
       
   370 
       
   371     if (!iMailboxUtils)
       
   372         {
       
   373         if (InterimUtilsL().MRViewersEnabledL())
       
   374             {
       
   375             iMailboxUtils = CMRMailboxUtils::NewL();
       
   376             }
       
   377         else
       
   378             {
       
   379             User::Leave(KErrNotSupported);
       
   380             }
       
   381         }
       
   382 
       
   383     TRACE_EXIT_POINT;
       
   384     return *iMailboxUtils;
       
   385     }
       
   386 
   262 
   387 // -----------------------------------------------------------------------------
   263 // -----------------------------------------------------------------------------
   388 // CCalenGlobalData::CalSessionL
   264 // CCalenGlobalData::CalSessionL
   389 // Create a Calendar session if necessary and return a reference
   265 // Create a Calendar session if necessary and return a reference
   390 // (other items were commented in a header).
   266 // (other items were commented in a header).
   391 // -----------------------------------------------------------------------------
   267 // -----------------------------------------------------------------------------
   392 EXPORT_C CCalSession& CCalenGlobalData::CalSessionL()
   268 EXPORT_C CCalSession& CCalenGlobalData::CalSessionL()
   393     {
   269     {
   394     TRACE_ENTRY_POINT;
   270     TRACE_ENTRY_POINT;
   395     
   271 
   396     if (!iCalSession)
   272     if (!iCalSession)
   397         {
   273         {
   398         iGlobalDataOwnsCalSession = ETrue;
   274         iGlobalDataOwnsCalSession = ETrue;
   399         iCalSession = CCalSession::NewL();
   275         iCalSession = CCalSession::NewL();
   400 
   276 
   401         TRAPD( err, iCalSession->OpenL(KCalendarDatabaseFilePath));
   277         TRAPD( err, iCalSession->OpenL(KCalendarDatabaseFilePath));
   402         if (err == KErrNotFound)
   278         if ( err == KErrNotFound )
   403             {
   279             {
   404             CCalCalendarInfo* calendarInfo = GetDefaultCalendarInfoL();
   280             iCalSession->CreateCalFileL(KCalendarDatabaseFilePath);
   405             CleanupStack::PushL(calendarInfo);
   281             iCalSession->OpenL(KCalendarDatabaseFilePath); // not trapped anymore
   406             iCalSession->CreateCalFileL( KCalendarDatabaseFilePath,
       
   407                                         *calendarInfo);
       
   408             iCalSession->OpenL(KCalendarDatabaseFilePath);
       
   409             CleanupStack::PopAndDestroy(calendarInfo);
       
   410             }
       
   411         else if(err == KErrNone)
       
   412             {
       
   413             // temp solution...need to handle this case
       
   414             CCalCalendarInfo* calendarInfo = iCalSession->CalendarInfoL();
       
   415             CleanupStack::PushL(calendarInfo);
       
   416             if( !calendarInfo->NameL().Compare(KNullDesC) && 
       
   417                  !calendarInfo->Enabled() )
       
   418                 {
       
   419                 calendarInfo->SetNameL(KExtCalendar);
       
   420                 calendarInfo->SetColor(KRgbBlack.Value());
       
   421                 iCalSession->SetCalendarInfoL(*calendarInfo);
       
   422                 }
       
   423             CleanupStack::PopAndDestroy(calendarInfo);
       
   424             
       
   425             }
   282             }
   426         else
   283         else
   427             {
   284             {
   428             User::LeaveIfError( err );
   285             User::LeaveIfError( err );
   429             }
   286             }
       
   287 
   430         }
   288         }
   431 
   289 
   432     TRACE_EXIT_POINT;
   290     TRACE_EXIT_POINT;
   433     return *iCalSession;
   291     return *iCalSession;
   434     }
       
   435 
       
   436 // -----------------------------------------------------------------------------
       
   437 // CCalenGlobalData::CalSessionL
       
   438 // Based on the calendar name return already cached session from iFileMappingArray
       
   439 // (other items were commented in a header).
       
   440 // -----------------------------------------------------------------------------
       
   441 EXPORT_C CCalSession& CCalenGlobalData::CalSessionL(const TDesC& aCalendar)
       
   442     {
       
   443     TRACE_ENTRY_POINT;
       
   444     CCalSession* session = NULL;
       
   445     HBufC* calendarFileName = aCalendar.AllocLC();
       
   446     TInt index = iFileMappingArray.Find(
       
   447             *calendarFileName,CCalenGlobalData::CalenInfoIdentifierL);
       
   448     CleanupStack::PopAndDestroy(calendarFileName);
       
   449     if(KErrNotFound != index)
       
   450         {
       
   451         session = iFileMappingArray[index]->GetSessionPtr();
       
   452         }
       
   453     else
       
   454         {
       
   455         User::LeaveIfError(KErrNotFound);
       
   456         }
       
   457     
       
   458     TRACE_EXIT_POINT;
       
   459     return *session;
       
   460     }
   292     }
   461 
   293 
   462 // -----------------------------------------------------------------------------
   294 // -----------------------------------------------------------------------------
   463 // CCalenGlobalData::InstanceViewL
   295 // CCalenGlobalData::InstanceViewL
   464 // Create an instance view if necessary and returns a reference.  Creating an
   296 // Create an instance view if necessary and returns a reference.  Creating an
   470 // (other items were commented in a header).
   302 // (other items were commented in a header).
   471 // -----------------------------------------------------------------------------
   303 // -----------------------------------------------------------------------------
   472 EXPORT_C CCalInstanceView* CCalenGlobalData::InstanceViewL()
   304 EXPORT_C CCalInstanceView* CCalenGlobalData::InstanceViewL()
   473     {
   305     {
   474     TRACE_ENTRY_POINT;
   306     TRACE_ENTRY_POINT;
   475     
   307 
   476     if (!iInstanceView && !iInstanceViewCreation)
   308     if (!iInstanceView && !iInstanceViewCreation)
   477         {
   309         {
   478         if ( iEntryViewCreation )
   310         if ( iEntryViewCreation )
   479             {
   311             {
       
   312             /* We need queueing to avoid two immediate instance and entry view creation requests to symbian.
       
   313              * Though symbian handle two immediate requests ,this we require because we have only one observer
       
   314              * for callback and we would not be able to differentiate whether instance view got created or entry view got created.
       
   315              * If we dont queue the second request, we may end up giving incomplete object to the caller function.
       
   316              */
   480             iQueued = &CreateInstanceViewL;
   317             iQueued = &CreateInstanceViewL;
   481             }
   318             }
   482         else
   319         else
   483             {
   320             {
   484             CreateInstanceViewL();
   321             CreateInstanceViewL();
   485             }
   322             }
   486         }
   323         }
   487     
   324 
   488     TRACE_EXIT_POINT;
   325     TRACE_EXIT_POINT;
   489     return iInstanceView;
   326     return iInstanceView;
   490     }
       
   491 
       
   492 // -----------------------------------------------------------------------------
       
   493 // CCalenGlobalData::InstanceViewL
       
   494 // Based on collection id array Create an instance view if necessary and returns a reference.
       
   495 // Creating an instance view is an asynchronous operation, but this call returns
       
   496 // synchronously.  When the view is created, Completed() is called, which stops
       
   497 // iAs allowing this function to complete.
       
   498 // Leaves with KErrInUse if this class is in the process of creating the
       
   499 // instance view
       
   500 // (other items were commented in a header).
       
   501 // -----------------------------------------------------------------------------
       
   502 EXPORT_C CCalInstanceView* CCalenGlobalData::InstanceViewL(
       
   503                                         const RArray<TInt>& aCollectionIds)
       
   504     {
       
   505     TRACE_ENTRY_POINT;
       
   506     
       
   507     if(!iNewInstanceView && !iNewInstanceViewCreation)
       
   508         {
       
   509         CreateInstanceViewL(aCollectionIds);
       
   510         }
       
   511     
       
   512     TRACE_EXIT_POINT;
       
   513     return iNewInstanceView;
       
   514     }
   327     }
   515 
   328 
   516 // -----------------------------------------------------------------------------
   329 // -----------------------------------------------------------------------------
   517 // CCalenGlobalData::EntryViewL
   330 // CCalenGlobalData::EntryViewL
   518 // Create an entry view if necessary and returns a reference.  Creating an
   331 // Create an entry view if necessary and returns a reference.  Creating an
   524 // (other items were commented in a header).
   337 // (other items were commented in a header).
   525 // -----------------------------------------------------------------------------
   338 // -----------------------------------------------------------------------------
   526 EXPORT_C CCalEntryView* CCalenGlobalData::EntryViewL()
   339 EXPORT_C CCalEntryView* CCalenGlobalData::EntryViewL()
   527     {
   340     {
   528     TRACE_ENTRY_POINT;
   341     TRACE_ENTRY_POINT;
   529     
   342 
   530      if (!iEntryView && !iEntryViewCreation)
   343     if (!iEntryView && !iEntryViewCreation)
   531         {
   344         {
   532         if ( iInstanceViewCreation )
   345         if ( iInstanceViewCreation )
   533             {
   346             {
       
   347             /* We need queueing to avoid two immediate instance and entry view creation requests to symbian.
       
   348              * Though symbian handle two immediate requests ,this we require because we have only one observer
       
   349              * for callback and we would not be able to differentiate whether instance view got created or entry view got created.
       
   350              * If we dont queue the second request, we may end up giving incomplete object to the caller function.
       
   351              */
   534             iQueued = &CreateEntryViewL;
   352             iQueued = &CreateEntryViewL;
   535             }
   353             }
   536         else
   354         else
   537             {
   355             {
   538             CreateEntryViewL();
   356             CreateEntryViewL();
   539             }
   357             }
   540         }
   358         }
   541     
   359     TRACE_EXIT_POINT;
   542      TRACE_EXIT_POINT;
       
   543     return iEntryView;
   360     return iEntryView;
   544     }
   361     }
   545 
   362 
   546 
       
   547 // -----------------------------------------------------------------------------
       
   548 // CCalenGlobalData::EntryViewL
       
   549 // Based on collection id array Create an entry view if necessary and returns a reference.  
       
   550 // Creating an entry view is an asynchronous operation, but this call returns
       
   551 // synchronously.  When the view is created, Completed() is called, which stops
       
   552 // iAs allowing this function to complete.
       
   553 // Leaves with KErrInUse if this class is in the process of creating the
       
   554 // entry view
       
   555 // (other items were commented in a header).
       
   556 // -----------------------------------------------------------------------------
       
   557 EXPORT_C CCalEntryView* CCalenGlobalData::EntryViewL(
       
   558                                                 const TCalCollectionId aCollectionId)
       
   559     {
       
   560     TRACE_ENTRY_POINT;
       
   561     __ASSERT_ALWAYS(aCollectionId > 0 ,User::Panic(_L("CCalenGlobalData"),-1));
       
   562     
       
   563     CCalEntryView* entryView = NULL;
       
   564     
       
   565     TInt index = iHashDbidIndexMap.FindL(aCollectionId);
       
   566     if(index != KErrNotFound)
       
   567         {
       
   568         entryView = iFileMappingArray[index]->GetEntryView();
       
   569         }    
       
   570     
       
   571     if(!entryView)
       
   572         {
       
   573         User::LeaveIfNull(entryView);
       
   574         }
       
   575     
       
   576     TRACE_EXIT_POINT;
       
   577     return entryView;
       
   578     }
       
   579 
       
   580 // -----------------------------------------------------------------------------
       
   581 // CCalenGlobalData::RegisterEntryViewObserver
       
   582 // Add observer into view observers entry observer list.
       
   583 // (other items were commented in a header).
       
   584 // -----------------------------------------------------------------------------
       
   585 EXPORT_C TBool CCalenGlobalData::EntryViewExists()
       
   586     {
       
   587     TRACE_ENTRY_POINT;
       
   588     TRACE_EXIT_POINT;
       
   589     return ( iEntryView != NULL );
       
   590     }
       
   591 
       
   592 // -----------------------------------------------------------------------------
       
   593 // CCalenGlobalData::MeetingRequestUtilsL
       
   594 // Create MR Utils if necessary and returns a reference
       
   595 // (other items were commented in a header).
       
   596 // -----------------------------------------------------------------------------
       
   597 EXPORT_C CMRUtils& CCalenGlobalData::MeetingRequestUtilsL()
       
   598     {
       
   599     TRACE_ENTRY_POINT;
       
   600 
       
   601     iCreateError = KErrNone;
       
   602     if (!iUtilsAs)
       
   603         {
       
   604         iUtilsAs = new (ELeave) CActiveSchedulerWait;
       
   605         }
       
   606     if (!iMRUtils)
       
   607         {
       
   608         if (InterimUtilsL().MRViewersEnabledL())
       
   609             {
       
   610             if (iUtilsAs->IsStarted())
       
   611                 {
       
   612                 User::Leave(KErrInUse);
       
   613                 }
       
   614             else
       
   615                 {
       
   616                 //Get the currently selected mailbox
       
   617                 CMRMailboxUtils::TMailboxInfo defaultMailbox;
       
   618                 MRMailboxUtilsL().GetDefaultMRMailBoxL(defaultMailbox);
       
   619                 //CMRUtils resolves meeting request utilities based on mtm uid
       
   620                 TBuf8<KMaxUidName> mrMtm;
       
   621                 CnvUtfConverter::ConvertFromUnicodeToUtf8(mrMtm, defaultMailbox.iMtmUid.Name());
       
   622                 iMRUtils = CMRUtils::NewL(mrMtm, *this, CalSessionL(), NULL);
       
   623                 iUtilsAs->Start();
       
   624                 User::LeaveIfError(iCreateError);
       
   625                 }
       
   626             }
       
   627         else
       
   628             {
       
   629             User::Leave(KErrNotSupported);
       
   630             }
       
   631         }
       
   632 
       
   633     TRACE_EXIT_POINT;
       
   634     return *iMRUtils;
       
   635     }
       
   636 
       
   637 // -----------------------------------------------------------------------------
       
   638 // CCalenGlobalData::InterimUtilsL
       
   639 // Create a CCalenInterimUtils object if neccessary and return a reference
       
   640 // (other items were commented in a header).
       
   641 // -----------------------------------------------------------------------------
       
   642 EXPORT_C CCalenInterimUtils2& CCalenGlobalData::InterimUtilsL()
       
   643     {
       
   644     TRACE_ENTRY_POINT;
       
   645 
       
   646     if(!iInterimUtils)
       
   647         {
       
   648         iInterimUtils = CCalenInterimUtils2::NewL();
       
   649         }
       
   650 
       
   651     TRACE_EXIT_POINT;
       
   652     return *iInterimUtils;
       
   653     }
       
   654 
       
   655 // -----------------------------------------------------------------------------
       
   656 // CCalenGlobalData::CalenSendL
       
   657 // Create a CCalenSend object if neccessary and return a reference
       
   658 // (other items were commented in a header).
       
   659 // -----------------------------------------------------------------------------
       
   660 EXPORT_C CCalenSend& CCalenGlobalData::CalenSendL()
       
   661     {
       
   662     TRACE_ENTRY_POINT;
       
   663 
       
   664     if( !iSend )
       
   665         {
       
   666         iSend = CCalenSend::NewL( ECalenSend, CalSessionL() );
       
   667         }
       
   668 
       
   669     TRACE_EXIT_POINT;
       
   670     return *iSend;
       
   671     }
       
   672 
       
   673 // -----------------------------------------------------------------------------
       
   674 // CCalenGlobalData::Context
       
   675 // Return a reference to the CCalenContext object
       
   676 // (other items were commented in a header).
       
   677 // -----------------------------------------------------------------------------
       
   678 EXPORT_C MCalenContext& CCalenGlobalData::Context()
       
   679     {
       
   680     TRACE_ENTRY_POINT;
       
   681     TRACE_EXIT_POINT;
       
   682     return *iContext;
       
   683     }
       
   684 
       
   685 // -----------------------------------------------------------------------------
       
   686 // CCalenEditUi::AttemptToRetrieveDefaultMailboxL
       
   687 // Check to see if we have any mailboxes defined, and if we have a default
       
   688 // mailbox. If we don't, inform the user and return EFalse. This function
       
   689 // returns ETrue if aDefaultMailbox was initialised, and it is OK to create
       
   690 // a new MR.
       
   691 // (other items were commented in a header).
       
   692 // -----------------------------------------------------------------------------
       
   693 EXPORT_C TBool CCalenGlobalData::AttemptToRetrieveDefaultMailboxL( CMRMailboxUtils::TMailboxInfo& aDefaultMailbox)
       
   694     {
       
   695     TRACE_ENTRY_POINT;
       
   696 
       
   697     CMRMailboxUtils& mbUtils = MRMailboxUtilsL();
       
   698     // Get the currently defined mailboxes.
       
   699     RArray<CMRMailboxUtils::TMailboxInfo> mailboxes;
       
   700     CleanupClosePushL(mailboxes);
       
   701     mbUtils.ListMailBoxesL(mailboxes);
       
   702 
       
   703     TBool canCreateMeetingRequest=EFalse;
       
   704     TInt mbCount = mailboxes.Count();
       
   705 
       
   706     // The following 'if-else' block should always initialise
       
   707     // defaultMailBox if canCreateMeetingRequest comes out as ETrue.
       
   708     if(mbCount > 0)
       
   709         {
       
   710         TInt res = KErrNone;
       
   711         TRAPD(err, res = mbUtils.GetDefaultMRMailBoxL(aDefaultMailbox));
       
   712         if( res == KErrNone && err == KErrNone)
       
   713             {
       
   714             canCreateMeetingRequest = ETrue;
       
   715             }
       
   716         else
       
   717             {
       
   718             // We have one or more mailboxes defined, but failed to get a default.
       
   719             // Ask user to answer "Select default mailbox" query.  The available mailboxes must
       
   720             // first be reduced to only show mailboxes with an associated meetnig request solution
       
   721             RImplInfoPtrArrayOwn implArray;
       
   722              CleanupClosePushL( implArray );
       
   723             //Get all MRViewers Implementation
       
   724              const TUid mrViewersIface = {KMRViewersInterfaceUID};
       
   725              REComSession::ListImplementationsL(mrViewersIface, implArray );
       
   726 
       
   727             for (TInt i = mailboxes.Count() - 1; i >= 0; --i)
       
   728                 {
       
   729                 //See if any meeting request implementation matches this mailbox
       
   730                 for (TInt j=0; j<implArray.Count(); ++j)
       
   731                      {
       
   732                      TBuf16<KMaxUidName> mbName;
       
   733                      CnvUtfConverter::ConvertToUnicodeFromUtf8( mbName, implArray[j]->DataType() );
       
   734                      if(mailboxes[i].iMtmUid.Name().CompareF(mbName) == 0)
       
   735                          {
       
   736                          //Found a match
       
   737                         break;
       
   738                         }
       
   739                     //Mailbox has no matching meeting request solution
       
   740                     //remove from list
       
   741                     mailboxes.Remove(i);
       
   742                     }
       
   743                 }
       
   744             CleanupStack::PopAndDestroy();    //implArray
       
   745             
       
   746             TInt selected = PromptToSelectDefaultMailboxL(mailboxes);
       
   747 
       
   748             if(selected >= KErrNone)
       
   749                 {
       
   750                 mbUtils.SetDefaultMRMailBoxL( mailboxes[selected].iEntryId );
       
   751                 canCreateMeetingRequest = ETrue;
       
   752                 __ASSERT_ALWAYS((mbUtils.GetDefaultMRMailBoxL( aDefaultMailbox ) == KErrNone), User::Invariant()); // so we don't return ref to local variable.
       
   753                 }
       
   754             }
       
   755         }
       
   756     else
       
   757         {
       
   758         // Force the check of MR Impl existing
       
   759         InterimUtilsL().MRViewersEnabledL(ETrue); //ignore result
       
   760         }
       
   761    
       
   762     CleanupStack::PopAndDestroy(); // mailboxes
       
   763 
       
   764     TRACE_EXIT_POINT;
       
   765     return canCreateMeetingRequest;
       
   766     }
       
   767 
       
   768 // -----------------------------------------------------------------------------
       
   769 // CCalenGlobalData::ResetKeyEventQueue
       
   770 // Reset Keyevents queue
       
   771 // (other items were commented in a header).
       
   772 // -----------------------------------------------------------------------------
       
   773 EXPORT_C void CCalenGlobalData::ResetKeyEventQueue()
       
   774     {
       
   775     TRACE_ENTRY_POINT;
       
   776     iKeyQueue.Reset();
       
   777     TRACE_EXIT_POINT;
       
   778     }
       
   779 
       
   780 // -----------------------------------------------------------------------------
       
   781 // CCalenGlobalData::QueueKeyEvent
       
   782 // Queue key press event in a queue
       
   783 // (other items were commented in a header).
       
   784 // -----------------------------------------------------------------------------
       
   785 EXPORT_C TBool CCalenGlobalData::QueueKeyEvent(const TKeyEvent& aEvent,
       
   786                                                TEventCode aType)
       
   787     {
       
   788     TRACE_ENTRY_POINT;
       
   789     TBool result(EFalse);
       
   790     TQueuedKeyEvent item;
       
   791     item.iType = aType;
       
   792     item.iEvent = aEvent;
       
   793     if( iKeyQueue.Insert(item, iKeyQueue.Count()) == KErrNone) // add to last
       
   794         {
       
   795         result = ETrue;
       
   796         }
       
   797     TRACE_EXIT_POINT;
       
   798     return result;
       
   799     }
       
   800 
       
   801 // -----------------------------------------------------------------------------
       
   802 // CCalenGlobalData::GetQueueKeyEvent
       
   803 // Get 1 queued event from the queue
       
   804 // (other items were commented in a header).
       
   805 // -----------------------------------------------------------------------------
       
   806 EXPORT_C TBool CCalenGlobalData::GetQueuedKeyEvent(TKeyEvent& aEvent, TEventCode& aType)
       
   807     {
       
   808     TRACE_ENTRY_POINT;
       
   809     TBool result(EFalse);
       
   810     if(iKeyQueue.Count() > 0)
       
   811         {
       
   812         TQueuedKeyEvent item;
       
   813         item = iKeyQueue[0];// get first
       
   814         aEvent = item.iEvent;
       
   815         aType = item.iType;
       
   816         iKeyQueue.Remove(0); // remove first
       
   817         result = ETrue;
       
   818         iKeyQueue.Compress();
       
   819         }
       
   820     TRACE_EXIT_POINT;
       
   821     return result;
       
   822     }
       
   823 
       
   824 // -----------------------------------------------------------------------------
       
   825 // CCalenGlobalData::ConstructFileMappingL
       
   826 // Constructs mapping between symbian calendar file and metabd info
       
   827 // -----------------------------------------------------------------------------
       
   828 EXPORT_C TBool CCalenGlobalData::ConstructFileMappingL()
       
   829     {
       
   830     TRACE_ENTRY_POINT;
       
   831     iCalendarsSession = CCalSession::NewL();
       
   832     
       
   833     ConstructCalendarsListL();
       
   834     
       
   835     iFileMappingArray.Reset();
       
   836         
       
   837     for(TInt index = 0; index < iCalendarInfoList.Count(); index++)
       
   838         {
       
   839         TPtrC calendarFileName = iCalendarInfoList[index]->FileNameL();
       
   840         CCalenFileMapping* filemap = CCalenFileMapping::NewL(); 
       
   841         CleanupStack::PushL(filemap);
       
   842         filemap->SetCalendarFileNameL(calendarFileName);
       
   843         
       
   844         iNewEntryView  = NULL;
       
   845         iNewEntryViewCreation  = NULL;
       
   846         CCalenDbChangeNotifier* dbChangeNotifier = NULL;
       
   847         if(iCalSession)
       
   848             {
       
   849             iNewCalSession = NULL;
       
   850             CCalCalendarInfo* calendarInfo = iCalendarInfoList[index];
       
   851             
       
   852             CCalSession& tempSession = CreateNewSessionL( calendarFileName,
       
   853                                     *calendarInfo );
       
   854             filemap->SetSessionPtr(&tempSession);
       
   855             filemap->SetCollectionId(tempSession.CollectionIdL());
       
   856             CreateEntryViewL(tempSession);
       
   857             filemap->SetEntryView( iNewEntryView );
       
   858             dbChangeNotifier = CCalenDbChangeNotifier::NewL( tempSession );
       
   859             dbChangeNotifier->RegisterObserverL(*iDBChangeNotifier);
       
   860             filemap->SetDBChangeNotifier(dbChangeNotifier);
       
   861             }            
       
   862          else
       
   863             {
       
   864             CCalSession& tempSession = CalSessionL();
       
   865             filemap->SetSessionPtr(&tempSession);
       
   866             filemap->SetCollectionId(tempSession.CollectionIdL());
       
   867             CreateEntryViewL(tempSession);
       
   868             filemap->SetEntryView( iNewEntryView );
       
   869             iGlobalDataOwnsEntryView = EFalse;
       
   870             iGlobalDataOwnsCalSession = EFalse;
       
   871             dbChangeNotifier = CCalenDbChangeNotifier::NewL( tempSession );
       
   872             dbChangeNotifier->RegisterObserverL(*iDBChangeNotifier);
       
   873             filemap->SetDBChangeNotifier(dbChangeNotifier);
       
   874             }
       
   875         iFileMappingArray.Append(filemap);        
       
   876         CleanupStack::Pop(filemap);
       
   877         }   
       
   878     
       
   879     for(TInt index = 0; index < iFileMappingArray.Count(); index++)
       
   880         {
       
   881         iHashDbidIndexMap.InsertL( iFileMappingArray[index]->GetCollectionId(),
       
   882                                     index);
       
   883         }
       
   884     
       
   885     TRACE_EXIT_POINT
       
   886     return ETrue;
       
   887     }
       
   888 
       
   889 // -----------------------------------------------------------------------------
       
   890 // CCalenGlobalData::InitializeGlobalDataL
       
   891 // Initializes multiple db related objects
       
   892 // -----------------------------------------------------------------------------
       
   893 EXPORT_C void CCalenGlobalData::InitializeGlobalDataL()
       
   894     {
       
   895     TRACE_ENTRY_POINT
       
   896     
       
   897     ConstructFileMappingL();
       
   898     
       
   899     TRACE_EXIT_POINT
       
   900     }
       
   901 
       
   902 // -----------------------------------------------------------------------------
       
   903 // CCalenGlobalData::PromptToSelectDefaultMailboxL
       
   904 // Prompts the user to select the default mailbox. If the user cancels, the
       
   905 // function returns KErrCancel, otherwise it returns the index of the selected
       
   906 // mailbox. This function does not set the default mailbox, but rather returns
       
   907 // the index of the given array to which the default should be set.
       
   908 // (other items were commented in a header).
       
   909 // -----------------------------------------------------------------------------
       
   910 TInt CCalenGlobalData::PromptToSelectDefaultMailboxL( RArray<CMRMailboxUtils::TMailboxInfo>& aMailboxes )
       
   911     {
       
   912     TRACE_ENTRY_POINT;
       
   913 
       
   914     TInt mbCount = aMailboxes.Count();
       
   915 
       
   916     TInt selected;
       
   917     if (mbCount > 0)
       
   918         {
       
   919         CAknSinglePopupMenuStyleListBox* list =
       
   920             new (ELeave) CAknSinglePopupMenuStyleListBox;
       
   921         CleanupStack::PushL(list);
       
   922 
       
   923         CAknPopupList* popupList = CAknPopupList::NewL(list, R_AVKON_SOFTKEYS_OK_CANCEL);
       
   924         CleanupStack::PushL(popupList);
       
   925 
       
   926         list->ConstructL(popupList, CEikListBox::ELeftDownInViewRect);
       
   927         list->CreateScrollBarFrameL(ETrue);
       
   928         list->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   929             CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
   930 
       
   931         CEikonEnv* eikEnv = CEikonEnv::Static();
       
   932 
       
   933         CDesCArrayFlat* items = new (ELeave)CDesCArrayFlat(mbCount);
       
   934         CleanupStack::PushL(items);
       
   935         for(TInt i=0; i<mbCount; ++i)
       
   936             {
       
   937             items->AppendL( aMailboxes[i].iName );
       
   938             }
       
   939         CleanupStack::Pop(items);
       
   940         CTextListBoxModel* model = list->Model();
       
   941 
       
   942         //Pass ownersip of items to model
       
   943         model->SetItemTextArray(items);
       
   944 
       
   945         HBufC* title = StringLoader::LoadLC(R_CALEN_SELECT_MAILBOX, eikEnv);
       
   946         popupList->SetTitleL(*title);
       
   947         CleanupStack::PopAndDestroy(title);
       
   948 
       
   949         TBool accepted = popupList->ExecuteLD();
       
   950         CleanupStack::Pop(); // popupList
       
   951 
       
   952         if(accepted)
       
   953             {
       
   954             selected = list->CurrentItemIndex();
       
   955             }
       
   956         else
       
   957             {
       
   958             selected = KErrCancel;
       
   959             }
       
   960 
       
   961         CleanupStack::PopAndDestroy();  // list
       
   962         }
       
   963     else
       
   964         {
       
   965         //No mailboxes defined.  Could prompt user to define one here?
       
   966         selected = KErrCancel;
       
   967         }
       
   968 
       
   969     TRACE_EXIT_POINT;
       
   970     return selected;
       
   971     }
       
   972 
       
   973 // -----------------------------------------------------------------------------
       
   974 // CCalenGlobalData::HandleCalEngStatus
       
   975 // Called when Meeting Request utils are creation is complete.
       
   976 // Stops the CActiveSchedulerWait, allowing MeetingRequestUtilsL() to complete
       
   977 // (other items were commented in a header).
       
   978 // -----------------------------------------------------------------------------
       
   979 void CCalenGlobalData::HandleCalEngStatus( TMRUtilsCalEngStatus aStatus )
       
   980     {
       
   981     TRACE_ENTRY_POINT;
       
   982 
       
   983     ASSERT(iUtilsAs);
       
   984 
       
   985     switch (aStatus)
       
   986         {
       
   987         case ENotReady:
       
   988             {
       
   989             //Keep waiting
       
   990             }
       
   991         break;
       
   992         case EAvailableWithoutTombs:
       
   993         case EAvailable:
       
   994             {
       
   995             //Ready for use.  Stop the CActiveSchedulerWait and return
       
   996             iUtilsAs->AsyncStop();
       
   997             iCreateError = KErrNone;
       
   998             }
       
   999         break;
       
  1000         case ENotAvailable:
       
  1001             {
       
  1002             //Somethings gone wrong.  Stop the CActiveSchedulerWait but force
       
  1003             //MeetingRequestUtilsL to leave.
       
  1004             //We'll use KErrNotFound for now but we don't know what the problem is.
       
  1005             iUtilsAs->AsyncStop();
       
  1006             iCreateError = KErrNotFound;
       
  1007             }
       
  1008         break;
       
  1009         default:
       
  1010             {
       
  1011             //Shouldn't ever get called.
       
  1012             iUtilsAs->AsyncStop();
       
  1013             iCreateError = KErrArgument;
       
  1014             }
       
  1015         break;
       
  1016         }
       
  1017 
       
  1018     TRACE_EXIT_POINT;
       
  1019     }
       
  1020 
       
  1021 // -----------------------------------------------------------------------------
       
  1022 // CCalenGlobalData::HandleNotification
       
  1023 // Handle any notification if registered for
       
  1024 // -----------------------------------------------------------------------------
       
  1025 void CCalenGlobalData::HandleNotification(const TCalenNotification aNotification )
       
  1026 	{
       
  1027 	TRACE_ENTRY_POINT;
       
  1028 
       
  1029 	switch(aNotification)
       
  1030 		{
       
  1031 		case ECalenNotifyEntryInstanceViewCreated:
       
  1032 			{
       
  1033 			ViewCreationCompleted( ETrue );
       
  1034 			}
       
  1035 			break;
       
  1036 		case ECalenNotifyEntryInstanceViewCreationFailed:
       
  1037 			{
       
  1038 			ViewCreationCompleted( EFalse );
       
  1039 			}
       
  1040 			break;
       
  1041 		case ECalenNotifyDeleteInstanceView:
       
  1042 			{
       
  1043 			//InstanceView has to be deleted since deleting any session(Calendar) 
       
  1044 			//entryView and instanceView has to be deleted before deleting session.
       
  1045 			if(iNewInstanceView)
       
  1046 				{
       
  1047 				delete iNewInstanceView;
       
  1048 				iNewInstanceViewCreation = iNewInstanceView =  NULL;
       
  1049 				}
       
  1050 			
       
  1051 			//If InstanceView is called by any component single instanceView 
       
  1052 			// without the knowledge of multi session (calendar).
       
  1053 			//This instance also should be deleted for deleting the session.  
       
  1054 			if(iInstanceView)
       
  1055 				{
       
  1056 				delete iInstanceView;
       
  1057 				iInstanceView = iInstanceViewCreation = NULL;
       
  1058 				}
       
  1059 			}
       
  1060 			break;
       
  1061 		case ECalenNotifyRealExit:
       
  1062 			{
       
  1063 			iCalendarForcedExit = ETrue;
       
  1064 			}
       
  1065 			break;
       
  1066 		case ECalenNotifyCalendarInfoCreated:
       
  1067 			{
       
  1068 			TRAP_IGNORE(HandleCalendarInfoCreatedL());
       
  1069 			}
       
  1070 			break;
       
  1071 		case ECalenNotifyCalendarInfoUpdated:
       
  1072 			{
       
  1073 			TRAP_IGNORE(HandleCalendarInfoUpdatedL());
       
  1074 			}
       
  1075 			break;
       
  1076 		case ECalenNotifyCalendarFileDeleted:
       
  1077 			{
       
  1078 			TRAP_IGNORE(HandleCalendarFileDeletedL());
       
  1079 			}
       
  1080 			break;
       
  1081 		default:
       
  1082 			break;
       
  1083 		}
       
  1084 
       
  1085     TRACE_EXIT_POINT;
       
  1086     }
       
  1087 
       
  1088 // -----------------------------------------------------------------------------
       
  1089 // CCalenGlobalData::CreateEntryViewL
       
  1090 // -----------------------------------------------------------------------------
       
  1091 void CCalenGlobalData::CreateEntryViewL()
   363 void CCalenGlobalData::CreateEntryViewL()
  1092     {
   364     {
  1093     TRACE_ENTRY_POINT;
   365     TRACE_ENTRY_POINT;
  1094 
   366     iEntryViewCreation = CCalEntryView::NewL( CalSessionL(), iCalCallBack );
  1095     HBufC* calendarFileName = KCalendarDatabaseFilePath().AllocLC();
   367     TRACE_EXIT_POINT;
  1096     TInt index  = iFileMappingArray.Find(   
   368     }
  1097             *calendarFileName, CCalenGlobalData::CalenInfoIdentifierL);
   369 
  1098     CleanupStack::PopAndDestroy(calendarFileName);
       
  1099     if(index != KErrNotFound)
       
  1100         {
       
  1101         iEntryView = iFileMappingArray[index]->GetEntryView();
       
  1102         }
       
  1103     
       
  1104     if(!iEntryView)
       
  1105         {
       
  1106         User::LeaveIfNull(iEntryView);
       
  1107         }
       
  1108     
       
  1109     TRACE_EXIT_POINT;
       
  1110     }
       
  1111 
       
  1112 // -----------------------------------------------------------------------------
       
  1113 // CCalenGlobalData::CreateEntryViewL
       
  1114 // -----------------------------------------------------------------------------
       
  1115 void  CCalenGlobalData::CreateEntryViewL(CCalSession& session)
       
  1116     {
       
  1117     TRACE_ENTRY_POINT;
       
  1118     iNewEntryViewCreation = CCalEntryView::NewL( session, iCalCallBack );
       
  1119     
       
  1120     if(iNewEntryViewCreation)
       
  1121         {
       
  1122         iNewEntryView = iNewEntryViewCreation;
       
  1123         }
       
  1124     
       
  1125     TRACE_EXIT_POINT;
       
  1126     }
       
  1127 
       
  1128 // -----------------------------------------------------------------------------
       
  1129 // CCalenGlobalData::CreateInstanceViewL
       
  1130 // -----------------------------------------------------------------------------
       
  1131 void CCalenGlobalData::CreateInstanceViewL()
   370 void CCalenGlobalData::CreateInstanceViewL()
  1132     {
   371     {
  1133     TRACE_ENTRY_POINT;
   372     TRACE_ENTRY_POINT;
  1134     iInstanceViewCreation = CCalInstanceView::NewL( CalSessionL(), iCalCallBack );
   373     iInstanceViewCreation = CCalInstanceView::NewL( CalSessionL(), iCalCallBack );
  1135      if(iInstanceViewCreation)
       
  1136         {
       
  1137         iInstanceView = iInstanceViewCreation;
       
  1138         }
       
  1139     TRACE_EXIT_POINT;
       
  1140     }
       
  1141 
       
  1142 // -----------------------------------------------------------------------------
       
  1143 // CCalenGlobalData::CreateEntryViewL
       
  1144 // -----------------------------------------------------------------------------
       
  1145 void CCalenGlobalData::CreateInstanceViewL(const RArray<TInt>& aCollectionIds)
       
  1146     {
       
  1147     TRACE_ENTRY_POINT;
       
  1148     
       
  1149     RPointerArray<CCalSession> sessionArray;
       
  1150     
       
  1151     for(TInt i = 0; i < aCollectionIds.Count() ; i++)
       
  1152         {
       
  1153         TInt index = iHashDbidIndexMap.FindL(aCollectionIds[i]);
       
  1154         if(index != KErrNotFound)
       
  1155            {
       
  1156            CCalSession* sessionPtr = iFileMappingArray[index]->GetSessionPtr();
       
  1157            sessionArray.AppendL(sessionPtr);
       
  1158            }
       
  1159         }
       
  1160     
       
  1161     if(sessionArray.Count())
       
  1162         {
       
  1163         iNewInstanceViewCreation = CCalInstanceView::NewL( sessionArray, iCalCallBack );
       
  1164         if(iNewInstanceViewCreation)
       
  1165            {
       
  1166            iNewInstanceView = iNewInstanceViewCreation;        
       
  1167            }
       
  1168         }
       
  1169     
       
  1170     sessionArray.Reset();
       
  1171     TRACE_EXIT_POINT;
   374     TRACE_EXIT_POINT;
  1172     }
   375     }
  1173 
   376 
  1174 // -----------------------------------------------------------------------------
   377 // -----------------------------------------------------------------------------
  1175 // CEntryViewObserver::ViewCreationCompleted
   378 // CEntryViewObserver::ViewCreationCompleted
  1182     TRACE_ENTRY_POINT;
   385     TRACE_ENTRY_POINT;
  1183 
   386 
  1184     if ( !aSuccess )
   387     if ( !aSuccess )
  1185         {
   388         {
  1186         // Something has gone wrong
   389         // Something has gone wrong
  1187         if (iEntryViewCreation)
   390         delete iEntryViewCreation;
  1188             {
   391         delete iInstanceViewCreation;
  1189             delete iEntryViewCreation;
       
  1190             iEntryViewCreation = NULL;
       
  1191             }
       
  1192         if (iInstanceViewCreation)
       
  1193             {
       
  1194             delete iInstanceViewCreation;
       
  1195             iInstanceViewCreation = NULL;
       
  1196             }
       
  1197 
       
  1198         if (iNewEntryViewCreation)
       
  1199             {
       
  1200             delete iNewEntryViewCreation;
       
  1201             iNewEntryViewCreation = NULL;
       
  1202 
       
  1203             HBufC* fileName = iContext->GetCalendarFileNameL().AllocLC();
       
  1204             TInt index = iFileMappingArray.Find(*fileName,
       
  1205                     CCalenGlobalData::CalenInfoIdentifierL);
       
  1206             CleanupStack::PopAndDestroy(fileName);
       
  1207 
       
  1208             if (index != KErrNotFound)
       
  1209                 {
       
  1210                 iFileMappingArray[index]->SetEntryView(NULL);
       
  1211                 }
       
  1212             }
       
  1213 
       
  1214         if (iNewInstanceViewCreation)
       
  1215             {
       
  1216             delete iNewInstanceViewCreation;
       
  1217             iNewInstanceViewCreation = NULL;
       
  1218             }
       
  1219 
       
  1220         }
   392         }
  1221     else
   393     else
  1222         {
   394         {
  1223         if ( iEntryViewCreation )
   395         if ( iEntryViewCreation )
  1224             {
   396             {
  1225             iEntryView = iEntryViewCreation;
   397             iEntryView = iEntryViewCreation;
  1226             }
   398             }
  1227         else if ( iInstanceViewCreation )
   399         else if ( iInstanceViewCreation )
  1228             {
   400             {
  1229             iInstanceView = iInstanceViewCreation;
   401             iInstanceView = iInstanceViewCreation;
  1230             }
       
  1231         
       
  1232         if ( iNewEntryViewCreation )
       
  1233             {
       
  1234             iNewEntryView = iNewEntryViewCreation;
       
  1235             }
       
  1236         else if ( iNewInstanceViewCreation )
       
  1237             {
       
  1238             iNewInstanceView = iNewInstanceViewCreation;
       
  1239             }
   402             }
  1240         }
   403         }
  1241 
   404 
  1242     iEntryViewCreation = NULL;
   405     iEntryViewCreation = NULL;
  1243     iInstanceViewCreation = NULL;
   406     iInstanceViewCreation = NULL;
  1244     iNewEntryViewCreation = NULL;
   407 
  1245     iNewInstanceViewCreation = NULL;
       
  1246     
       
  1247     if ( iQueued )
   408     if ( iQueued )
  1248         {
   409         {
  1249         (this->*iQueued)();
   410         (this->*iQueued)();
  1250         iQueued = NULL;
   411         iQueued = NULL;
  1251         }
   412         }
  1252 
   413 
  1253     TRACE_EXIT_POINT;
   414     TRACE_EXIT_POINT;
  1254     }
   415     }
  1255 
   416 
  1256 // -----------------------------------------------------------------------------
   417 // -----------------------------------------------------------------------------
  1257 // CCalenGlobalData::CreateNewSessionL
   418 // CCalenGlobalData::RegisterEntryViewObserver
  1258 // Create a Calendar session if necessary and return a reference
   419 // Add observer into view observers entry observer list.
  1259 // (other items were commented in a header).
   420 // (other items were commented in a header).
  1260 // -----------------------------------------------------------------------------
   421 // -----------------------------------------------------------------------------
  1261 CCalSession& CCalenGlobalData::CreateNewSessionL( const TDesC& aCalendar )
   422 EXPORT_C TBool CCalenGlobalData::EntryViewExists()
  1262     {
   423     {
  1263     TRACE_ENTRY_POINT;
   424     TRACE_ENTRY_POINT;
  1264 
   425     TRACE_EXIT_POINT;
  1265     if(!iNewCalSession)
   426     return ( iEntryView != NULL );
  1266         {
   427     }
  1267         iNewCalSession = CCalSession::NewL(*iCalSession);
   428 
  1268         
   429 
  1269         TRAPD( err, iNewCalSession->OpenL(aCalendar));
   430 // -----------------------------------------------------------------------------
  1270         if ( err == KErrNotFound )
   431 // CCalenGlobalData::Context
  1271            {
   432 // Return a reference to the CCalenContext object
  1272            iNewCalSession->CreateCalFileL(aCalendar);
   433 // (other items were commented in a header).
  1273            iNewCalSession->OpenL(aCalendar); // not trapped anymore
   434 // -----------------------------------------------------------------------------
  1274            }
   435 EXPORT_C MCalenContext& CCalenGlobalData::Context()
  1275         else
   436     {
  1276            {
   437     TRACE_ENTRY_POINT;
  1277            User::LeaveIfError( err );
   438     TRACE_EXIT_POINT;
  1278            }
   439     return *iContext;
  1279         }
   440     }
  1280     TRACE_EXIT_POINT;
   441 
  1281     return *iNewCalSession;
   442 void CCalenGlobalData::HandleNotification(const TCalenNotification aNotification )
  1282     }
   443     {
  1283 
   444     TRACE_ENTRY_POINT;
  1284 // -----------------------------------------------------------------------------
   445 
  1285 // CCalenGlobalData::CalenInfoIdentifierL
   446     if ( aNotification == ECalenNotifyEntryInstanceViewCreated )
  1286 // -----------------------------------------------------------------------------
   447         {
  1287 //
   448         ViewCreationCompleted( ETrue );
  1288 TBool CCalenGlobalData::CalenInfoIdentifierL( const HBufC* aName,
   449         }
  1289                                         const CCalenFileMapping& aFileMapping)
   450     else if (aNotification == ECalenNotifyEntryInstanceViewCreationFailed )
  1290     {
   451         {
  1291     TRACE_ENTRY_POINT;
   452         ViewCreationCompleted( EFalse );
  1292     TPtrC calendarFileName = aFileMapping.GetCalendarFileName();
   453         }
  1293     TRACE_EXIT_POINT;
   454 
  1294     return (!calendarFileName.CompareF(*aName));
   455     TRACE_EXIT_POINT;
  1295     }
   456     }
  1296 
   457 
  1297 // -----------------------------------------------------------------------------
   458 // Enf of file
  1298 // CCalenGlobalData::AddCalendarL
       
  1299 // adds new calendar file and sets calendar info to it 
       
  1300 // -----------------------------------------------------------------------------
       
  1301 //
       
  1302 EXPORT_C void CCalenGlobalData::AddCalendarL(CCalCalendarInfo* aCalendarInfo)
       
  1303 	{	
       
  1304 	TRACE_ENTRY_POINT;
       
  1305 	CleanupStack::PushL(aCalendarInfo);
       
  1306 	
       
  1307 	CCalenFileMapping* fileMapping = CCalenFileMapping::NewL();
       
  1308 	CleanupStack::PushL(fileMapping);
       
  1309 	
       
  1310 	HBufC* calendarFileName = CCalenMultiCalUtil::GetNextAvailableCalFileL();
       
  1311 	CleanupStack::PushL(calendarFileName);
       
  1312 	fileMapping->SetCalendarFileNameL(calendarFileName->Des());
       
  1313 	CleanupStack::PopAndDestroy(calendarFileName);
       
  1314 	if(iCalSession)
       
  1315 		{
       
  1316 		iNewCalSession = NULL;
       
  1317 		iNewEntryView  = NULL;
       
  1318 		iNewEntryViewCreation  = NULL;
       
  1319 		TPtrC calFileName = fileMapping->GetCalendarFileName();
       
  1320 		CCalSession& tempSession = CreateNewSessionL( calFileName,
       
  1321 													*aCalendarInfo );
       
  1322 		fileMapping->SetSessionPtr(&tempSession);
       
  1323 		fileMapping->SetCollectionId(tempSession.CollectionIdL());
       
  1324 		
       
  1325 		if(!iCalendarForcedExit)
       
  1326 			{
       
  1327 			CreateEntryViewL(tempSession);
       
  1328 			}
       
  1329 		fileMapping->SetEntryView( iNewEntryView );
       
  1330 		CCalenDbChangeNotifier* dbChangeNotifier = CCalenDbChangeNotifier::NewL( tempSession );
       
  1331 		dbChangeNotifier->RegisterObserverL(*iDBChangeNotifier);
       
  1332 		fileMapping->SetDBChangeNotifier(dbChangeNotifier);
       
  1333 		iCalendarInfoList.Append(tempSession.CalendarInfoL());
       
  1334 		}
       
  1335 	
       
  1336 	CleanupStack::Pop(fileMapping);
       
  1337 	iFileMappingArray.Append(fileMapping);
       
  1338 	
       
  1339 	iHashDbidIndexMap.Close();   
       
  1340 	for(TInt index = 0; index < iFileMappingArray.Count(); index++)
       
  1341 	    {
       
  1342 	    iHashDbidIndexMap.InsertL( iFileMappingArray[index]->GetCollectionId(),
       
  1343                                    index);
       
  1344 	    }
       
  1345 	
       
  1346 	CleanupStack::PopAndDestroy(aCalendarInfo);
       
  1347 	aCalendarInfo = NULL;
       
  1348 	
       
  1349 	TRACE_EXIT_POINT
       
  1350 	}
       
  1351 
       
  1352 // -----------------------------------------------------------------------------
       
  1353 // CCalenGlobalData::RemoveDeadCalendarsL
       
  1354 // removes dead calendar files from the file system
       
  1355 // -----------------------------------------------------------------------------
       
  1356 //
       
  1357 EXPORT_C void CCalenGlobalData::RemoveDeadCalendarsL()
       
  1358     {
       
  1359     TRACE_ENTRY_POINT;
       
  1360     CCalCalendarIterator* calendarIterator = 
       
  1361                       CCalCalendarIterator::NewL(*iCalendarsSession);
       
  1362     CleanupStack::PushL(calendarIterator);
       
  1363     
       
  1364     for( CCalCalendarInfo* calendarInfo = calendarIterator->FirstL();
       
  1365          calendarInfo!=NULL;calendarInfo = calendarIterator->NextL())
       
  1366         {
       
  1367         CleanupStack::PushL(calendarInfo);
       
  1368         HBufC* filename = calendarInfo->FileNameL().AllocLC();
       
  1369         TInt index = iFileMappingArray.Find( *filename, 
       
  1370                                  CCalenGlobalData::CalenInfoIdentifierL);
       
  1371         if(KErrNotFound == index)
       
  1372             {
       
  1373             CCalSession* session = CCalSession::NewL(*iCalSession);
       
  1374             CleanupStack::PushL(session);
       
  1375             TRAP_IGNORE(session->DeleteCalFileL( *filename ));
       
  1376             CleanupStack::PopAndDestroy(session);
       
  1377             }
       
  1378         CleanupStack::PopAndDestroy(filename);
       
  1379         CleanupStack::PopAndDestroy(calendarInfo);
       
  1380         }
       
  1381     CleanupStack::PopAndDestroy(calendarIterator);
       
  1382     TRACE_EXIT_POINT;
       
  1383     }
       
  1384 
       
  1385 // -----------------------------------------------------------------------------
       
  1386 // CCalenGlobalData::RemoveCalendarL
       
  1387 // removes calendar file from the file system
       
  1388 // -----------------------------------------------------------------------------
       
  1389 //
       
  1390 EXPORT_C void CCalenGlobalData::RemoveCalendarL(const TDesC& aCalendarFileName)
       
  1391 	{
       
  1392 	TRACE_ENTRY_POINT;
       
  1393 
       
  1394 	HBufC* calendarFilename = aCalendarFileName.AllocLC();
       
  1395 	TInt index = iFileMappingArray.Find( *calendarFilename, 
       
  1396 				CCalenGlobalData::CalenInfoIdentifierL);
       
  1397 	
       
  1398     if( KErrNotFound == index )
       
  1399         {
       
  1400         CleanupStack::PopAndDestroy(calendarFilename);
       
  1401         // reconstruct the calendar list using the iterator
       
  1402         ConstructCalendarsListL();
       
  1403         
       
  1404         TRACE_EXIT_POINT;
       
  1405         return;
       
  1406         }
       
  1407 
       
  1408 	if( index >= 0 )
       
  1409 		{
       
  1410         CCalenFileMapping* filemap = iFileMappingArray.operator [](index);
       
  1411         CCalSession* session = filemap->GetSessionPtr();
       
  1412 		if(session)
       
  1413 			{
       
  1414             TRAPD(err, session->DeleteCalFileL(aCalendarFileName));
       
  1415             if( err == KErrInUse )
       
  1416                 {
       
  1417                 //Calendar file is opened by other application.
       
  1418                 //Mark the file as deleted. Try delete the file at later time.
       
  1419                 // Get the CalFile
       
  1420                 CCalCalendarInfo* caleninfo = session->CalendarInfoL();
       
  1421                 CleanupStack::PushL(caleninfo);
       
  1422                 
       
  1423                 // Mark the CalFile as Hidden
       
  1424                 caleninfo->SetEnabled( EFalse );
       
  1425                             
       
  1426                 
       
  1427                 TBuf8<KBuffLength> keyBuff;
       
  1428                 
       
  1429                 // Set the modification time as home time.
       
  1430                 keyBuff.Zero();
       
  1431                 keyBuff.AppendNum(EModificationTime);
       
  1432                 TTime modificationTime;
       
  1433                 modificationTime.HomeTime();
       
  1434                 TPckgC<TTime> pkgModificationTime(modificationTime);
       
  1435                 caleninfo->SetPropertyL(keyBuff, pkgModificationTime);
       
  1436                 
       
  1437                 // Set the SyncStatus to False
       
  1438                 keyBuff.Zero();
       
  1439                 keyBuff.AppendNum( ESyncStatus );
       
  1440                 TBool syncstatus( EFalse );
       
  1441                 TPckgC<TBool> pckgSyncStatusValue( syncstatus );
       
  1442                 caleninfo->SetPropertyL( keyBuff, pckgSyncStatusValue );
       
  1443                 
       
  1444                 // Mark the meta property as SoftDeleted
       
  1445                 keyBuff.Zero();
       
  1446                 keyBuff.AppendNum(EMarkAsDelete);
       
  1447                 TPckgC<TBool> pkgSoftDelete( ETrue );
       
  1448                 caleninfo->SetPropertyL(keyBuff, pkgSoftDelete);
       
  1449                 
       
  1450                 session->SetCalendarInfoL( *caleninfo );
       
  1451                 CleanupStack::PopAndDestroy(caleninfo);            
       
  1452                 }
       
  1453             
       
  1454             TRAP_IGNORE(DeleteCalendarMissedAlarmsL(aCalendarFileName));
       
  1455             
       
  1456             TInt infoListIndex = iCalendarInfoList.Find(*calendarFilename,
       
  1457                         CCalenGlobalData::CalenCalendarInfoIdentiferL);
       
  1458             
       
  1459             CCalCalendarInfo* calendarInfo = iCalendarInfoList[infoListIndex];
       
  1460             iCalendarInfoList.Remove(infoListIndex);
       
  1461             delete calendarInfo;
       
  1462             calendarInfo = NULL;
       
  1463 
       
  1464             iHashDbidIndexMap.Close();
       
  1465 
       
  1466             iFileMappingArray.Remove(index);
       
  1467             delete filemap;
       
  1468             for(TInt index = 0; index < iFileMappingArray.Count(); index++)
       
  1469                 {
       
  1470                 iHashDbidIndexMap.InsertL(iFileMappingArray[index]->GetCollectionId(), index);
       
  1471                 }
       
  1472 			}
       
  1473 		}
       
  1474 
       
  1475 	
       
  1476 	CleanupStack::PopAndDestroy(calendarFilename);        
       
  1477 	TRACE_EXIT_POINT;
       
  1478 	}
       
  1479 
       
  1480 // -----------------------------------------------------------------------------
       
  1481 // CCalenGlobalData::UpdateCalendarL
       
  1482 // updates calendar info for the calendar file
       
  1483 // -----------------------------------------------------------------------------
       
  1484 //
       
  1485 EXPORT_C void CCalenGlobalData::UpdateCalendarL(CCalCalendarInfo* aCalendarInfo)
       
  1486 	{
       
  1487 	TRACE_ENTRY_POINT;
       
  1488 
       
  1489 	HBufC* calendar = aCalendarInfo->FileNameL().AllocLC();
       
  1490 	TInt index = iFileMappingArray.Find( *calendar, 
       
  1491 				CCalenGlobalData::CalenInfoIdentifierL);
       
  1492 
       
  1493 	if(index != KErrNotFound)
       
  1494 		{
       
  1495 		CCalSession* session = iFileMappingArray[index]->GetSessionPtr();
       
  1496 		if(session)
       
  1497 			{
       
  1498 			session->SetCalendarInfoL(*aCalendarInfo);
       
  1499 			}
       
  1500 		}
       
  1501 	CleanupStack::PopAndDestroy(calendar);
       
  1502 
       
  1503 	TRACE_EXIT_POINT;
       
  1504 	}
       
  1505 
       
  1506 // -----------------------------------------------------------------------------
       
  1507 // CCalenGlobalData::GetCalFileNameForCollectionId
       
  1508 // Get Calendar file name for the given collectionid
       
  1509 // -----------------------------------------------------------------------------
       
  1510 //
       
  1511 EXPORT_C const TDesC& CCalenGlobalData::GetCalFileNameForCollectionId( 
       
  1512      const TCalCollectionId aColId)
       
  1513 	{
       
  1514 	TRACE_ENTRY_POINT;
       
  1515 
       
  1516 	TInt index = iFileMappingArray.Find( aColId,
       
  1517 			CCalenGlobalData::CalenFileMapIdentifierForColId );
       
  1518 	
       
  1519 	if(index != KErrNotFound)
       
  1520 		{
       
  1521 		TRACE_EXIT_POINT;
       
  1522 		return iFileMappingArray[index]->GetCalendarFileName();
       
  1523 		}
       
  1524 	else
       
  1525 	    {
       
  1526 	    TRACE_EXIT_POINT;
       
  1527 	    return KNullDesC;
       
  1528 	    }
       
  1529 	}
       
  1530 
       
  1531 // -----------------------------------------------------------------------------
       
  1532 // CCalenGlobalData::GetAllCalendarInfoL
       
  1533 // Get all available calendar info
       
  1534 // -----------------------------------------------------------------------------
       
  1535 //
       
  1536 EXPORT_C void CCalenGlobalData::GetAllCalendarInfoL( 
       
  1537                     RPointerArray<CCalCalendarInfo>& aCalendarInfoList )
       
  1538 	{
       
  1539 	TRACE_ENTRY_POINT;
       
  1540 	CleanupClosePushL(aCalendarInfoList);
       
  1541     RFs fsSession;
       
  1542     CleanupClosePushL( fsSession );
       
  1543     RResourceFile resourceFile;
       
  1544     CleanupClosePushL( resourceFile );
       
  1545     User::LeaveIfError( fsSession.Connect() );
       
  1546     TFileName resourceFileName( KMissedAlarmResourceFile );
       
  1547 		
       
  1548     BaflUtils::NearestLanguageFile( fsSession, resourceFileName );
       
  1549     resourceFile.OpenL(fsSession, resourceFileName );
       
  1550     resourceFile.ConfirmSignatureL( 0 );   
       
  1551 	for(TInt index=0;index < iCalendarInfoList.Count();index++)
       
  1552 	    {
       
  1553         
       
  1554 	    TPtrC calendarNamePtr = iCalendarInfoList[index]->NameL();
       
  1555         if(calendarNamePtr.Compare(KPersonal) == 0)
       
  1556             {  
       
  1557             HBufC8* personalBuffer = resourceFile.AllocReadLC( R_CALE_DB_PERSONAL );    
       
  1558             const TPtrC16 ptrPBuffer(( TText16*) personalBuffer->Ptr(),
       
  1559                                              ( personalBuffer->Length()+1 )>>1 );    
       
  1560             HBufC *personalCalendar = ptrPBuffer.AllocL();    
       
  1561             CleanupStack::PushL(personalCalendar);
       
  1562             iCalendarInfoList[index]->SetNameL(*personalCalendar);
       
  1563             CleanupStack::PopAndDestroy( 2,personalBuffer );
       
  1564             }
       
  1565        
       
  1566 	   
       
  1567             aCalendarInfoList.AppendL(iCalendarInfoList[index]);
       
  1568 	        
       
  1569 	    }
       
  1570 	 CleanupStack::PopAndDestroy(2);
       
  1571 	 CleanupStack::Pop(&aCalendarInfoList);
       
  1572 	TRACE_EXIT_POINT;
       
  1573 	}
       
  1574 
       
  1575 // -----------------------------------------------------------------------------
       
  1576 // CCalenGlobalData::CreateNewSessionL
       
  1577 // Create new session with metadata
       
  1578 // -----------------------------------------------------------------------------
       
  1579 //
       
  1580 CCalSession& CCalenGlobalData::CreateNewSessionL( const TDesC& aCalendar,
       
  1581                      const CCalCalendarInfo& aCalendarInfo )
       
  1582 	{
       
  1583 	TRACE_ENTRY_POINT;
       
  1584 
       
  1585 	if(!iNewCalSession)
       
  1586 		{
       
  1587 		iNewCalSession = CCalSession::NewL(*iCalSession);
       
  1588 		
       
  1589 		TRAPD( err, iNewCalSession->OpenL(aCalendar));
       
  1590 		if ( err == KErrNotFound )
       
  1591 		   {
       
  1592 		   iNewCalSession->CreateCalFileL( aCalendar, aCalendarInfo );
       
  1593 		   iNewCalSession->OpenL(aCalendar); // not trapped anymore
       
  1594 		   }
       
  1595 		else
       
  1596 		   {
       
  1597 		   User::LeaveIfError( err );
       
  1598 		   }
       
  1599 		}
       
  1600 	TRACE_EXIT_POINT;
       
  1601 	return *iNewCalSession;
       
  1602 	}
       
  1603 
       
  1604 // -----------------------------------------------------------------------------
       
  1605 // CCalenGlobalData::CalenFileMapIdentifierForColId
       
  1606 // Search for filemap index based on collection id
       
  1607 // -----------------------------------------------------------------------------
       
  1608 //
       
  1609 TBool CCalenGlobalData::CalenFileMapIdentifierForColId(const TCalCollectionId* aId,
       
  1610                                        const CCalenFileMapping& aFileMapping )
       
  1611 	{
       
  1612 	TRACE_ENTRY_POINT;
       
  1613 	TRACE_EXIT_POINT;
       
  1614 	return (*aId == (aFileMapping.GetCollectionId()));
       
  1615 	}
       
  1616 
       
  1617 // -----------------------------------------------------------------------------
       
  1618 // CCalenGlobalData::CalenCalendarInfoIdentiferL
       
  1619 // Construct calendar list from the calendar iterator
       
  1620 // -----------------------------------------------------------------------------
       
  1621 //
       
  1622 TBool CCalenGlobalData::CalenCalendarInfoIdentiferL(const HBufC* aFileName,
       
  1623                                                         const CCalCalendarInfo& aCalendarInfo)
       
  1624     {
       
  1625     TRACE_ENTRY_POINT
       
  1626     TPtrC calendarFileName = aCalendarInfo.FileNameL();
       
  1627     TRACE_EXIT_POINT
       
  1628     return (!calendarFileName.CompareF(*aFileName) );
       
  1629     }
       
  1630 
       
  1631 // -----------------------------------------------------------------------------
       
  1632 // CCalenGlobalData::ConstructCalendarsListL
       
  1633 // Construct calendar list from the calendar iterator
       
  1634 // -----------------------------------------------------------------------------
       
  1635 //
       
  1636 void CCalenGlobalData::ConstructCalendarsListL()
       
  1637     {
       
  1638     TRACE_ENTRY_POINT;
       
  1639     
       
  1640     iCalendarInfoList.ResetAndDestroy();
       
  1641     CCalCalendarIterator* calendarIterator = 
       
  1642         CCalCalendarIterator::NewL(*iCalendarsSession);
       
  1643     CleanupStack::PushL(calendarIterator);
       
  1644     
       
  1645     for( CCalCalendarInfo* calendarInfo = calendarIterator->FirstL();
       
  1646          calendarInfo!=NULL;calendarInfo = calendarIterator->NextL())
       
  1647         {
       
  1648         TBuf8<KBuffLength> keyBuff;
       
  1649         // Mark the meta property as SoftDeleted
       
  1650         keyBuff.Zero();
       
  1651         keyBuff.AppendNum(EMarkAsDelete);
       
  1652         TBool softDelete = EFalse;
       
  1653         TPckgC<TBool> pkgSoftDelete( softDelete );
       
  1654         TRAPD(err,pkgSoftDelete.Set(calendarInfo->PropertyValueL(keyBuff)));
       
  1655         if( KErrNone == err )
       
  1656             {
       
  1657             softDelete = pkgSoftDelete();
       
  1658             }
       
  1659         if( softDelete && 
       
  1660                 calendarInfo->FileNameL().CompareF(CalSessionL().DefaultFileNameL()) )
       
  1661             {
       
  1662             // delete caleninfo except for default calendar.
       
  1663             delete calendarInfo;
       
  1664             }
       
  1665         else
       
  1666             {
       
  1667             iCalendarInfoList.Append(calendarInfo);
       
  1668             }
       
  1669         }
       
  1670     CleanupStack::PopAndDestroy(calendarIterator);
       
  1671     
       
  1672     TRACE_EXIT_POINT;
       
  1673     }
       
  1674 
       
  1675 // -----------------------------------------------------------------------------
       
  1676 // CCalenGlobalData::UpdateCalendarListL
       
  1677 // Update calendar list whenever CalendarInfoUpdated notification is received.
       
  1678 // -----------------------------------------------------------------------------
       
  1679 //
       
  1680 void CCalenGlobalData::UpdateCalendarListL()
       
  1681     {
       
  1682 	//Getting calendar name updated from calennotifier infochangednotification
       
  1683     HBufC* calendarNameUpdated = iContext->GetCalendarFileNameL().AllocLC();
       
  1684     TInt index = iFileMappingArray.Find( *calendarNameUpdated, 
       
  1685                     CCalenGlobalData::CalenInfoIdentifierL);
       
  1686 
       
  1687     if(index != KErrNotFound)
       
  1688         {
       
  1689         CCalSession* session = iFileMappingArray[index]->GetSessionPtr();
       
  1690         for(TInt i=0; i<iCalendarInfoList.Count() ;i++)
       
  1691             {
       
  1692             if( !iCalendarInfoList[i]->FileNameL().CompareF(calendarNameUpdated->Des()))
       
  1693                 {
       
  1694                 iCalendarInfoList[i] = session->CalendarInfoL();
       
  1695                 }
       
  1696             }
       
  1697         }
       
  1698     CleanupStack::PopAndDestroy(calendarNameUpdated);
       
  1699     }
       
  1700 // -----------------------------------------------------------------------------
       
  1701 // CCalenGlobalData::GetDefaultCalendarInfoL
       
  1702 // Get default calendar info
       
  1703 // -----------------------------------------------------------------------------
       
  1704 //
       
  1705 CCalCalendarInfo* CCalenGlobalData::GetDefaultCalendarInfoL()
       
  1706 	{
       
  1707 	TRACE_ENTRY_POINT;
       
  1708 
       
  1709 	CCalCalendarInfo* defaultCalendarInfo = CCalCalendarInfo::NewL();
       
  1710 	CleanupStack::PushL(defaultCalendarInfo);
       
  1711 	defaultCalendarInfo->SetColor(KCalenBlue.Value());
       
  1712 	defaultCalendarInfo->SetEnabled(ETrue);
       
  1713 	defaultCalendarInfo->SetNameL(KPhoneCalendar);
       
  1714 	CleanupStack::Pop(defaultCalendarInfo);
       
  1715 
       
  1716 	TRACE_EXIT_POINT;
       
  1717 	return defaultCalendarInfo;
       
  1718 	}
       
  1719 
       
  1720 // -----------------------------------------------------------------------------
       
  1721 // CCalenGlobalData::HandleCalendarInfoCreatedL
       
  1722 // Handles the notification ECalenNotifyCalendarInfoCreated
       
  1723 // -----------------------------------------------------------------------------
       
  1724 //
       
  1725 void CCalenGlobalData::HandleCalendarInfoCreatedL()
       
  1726 	{
       
  1727 	TRACE_ENTRY_POINT;
       
  1728 	// get the filename from the context
       
  1729 	TDesC& fileName = iContext->GetCalendarFileNameL();
       
  1730 
       
  1731 	// check for the calendar file already exists in filemap list
       
  1732 	HBufC* calendar = fileName.AllocLC();
       
  1733 	TInt index = iFileMappingArray.Find(*calendar,
       
  1734 			CCalenGlobalData::CalenInfoIdentifierL);
       
  1735 	CleanupStack::PopAndDestroy(calendar);
       
  1736 	
       
  1737 	// if not exists,add to filemapper list
       
  1738 	if (KErrNotFound == index)
       
  1739 		{
       
  1740 		CCalenFileMapping* fileMapper = CCalenFileMapping::NewL();
       
  1741 		CleanupStack::PushL(fileMapper);
       
  1742 		fileMapper->SetCalendarFileNameL(fileName);
       
  1743 
       
  1744 		iNewEntryView  = NULL;
       
  1745 		iNewEntryViewCreation  = NULL;
       
  1746 		CCalSession* newSession = NULL;
       
  1747 		if(iCalSession)
       
  1748 			{
       
  1749 			// create the new session using default session
       
  1750 			newSession = CCalSession::NewL(*iCalSession);
       
  1751 			CleanupStack::PushL(newSession);
       
  1752 			TRAPD(err,newSession->OpenL(fileName));
       
  1753 			User::LeaveIfError( err );
       
  1754 			CleanupStack::Pop(newSession);
       
  1755 			// set filemapper data 
       
  1756 			fileMapper->SetSessionPtr(newSession);
       
  1757 			fileMapper->SetCollectionId(newSession->CollectionIdL());
       
  1758 			 
       
  1759 			if(!iCalendarForcedExit)
       
  1760 				{
       
  1761 				CreateEntryViewL(*newSession);
       
  1762 				}
       
  1763 			fileMapper->SetEntryView( iNewEntryView );
       
  1764 			
       
  1765 			iCalendarInfoList.AppendL(newSession->CalendarInfoL());
       
  1766 		    CCalenDbChangeNotifier* dbChangeNotifier = CCalenDbChangeNotifier::NewL( *newSession );
       
  1767 		    dbChangeNotifier->RegisterObserverL(*iDBChangeNotifier);
       
  1768 		    fileMapper->SetDBChangeNotifier(dbChangeNotifier);
       
  1769 			}
       
  1770 		else
       
  1771 			{
       
  1772 			// create the first session
       
  1773 			iCalSession = CCalSession::NewL();
       
  1774 			TRAPD(err,iCalSession->OpenL(fileName));
       
  1775 			User::LeaveIfError(err);
       
  1776 			
       
  1777 			// set filemapper data 
       
  1778 			fileMapper->SetSessionPtr(iCalSession);
       
  1779 			fileMapper->SetCollectionId(iCalSession->CollectionIdL());
       
  1780 			
       
  1781 			if(!iCalendarForcedExit)
       
  1782 				{
       
  1783 				CreateEntryViewL(*iCalSession);
       
  1784 				}
       
  1785 			
       
  1786 			fileMapper->SetEntryView(iNewEntryView);
       
  1787 	        CCalenDbChangeNotifier* dbChangeNotifier = CCalenDbChangeNotifier::NewL( *iCalSession );
       
  1788 	        fileMapper->SetDBChangeNotifier(dbChangeNotifier);
       
  1789 	        dbChangeNotifier->RegisterObserverL(*iDBChangeNotifier);
       
  1790 			iGlobalDataOwnsEntryView = EFalse;
       
  1791 			iGlobalDataOwnsCalSession = EFalse;
       
  1792 			iCalendarInfoList.AppendL(iCalSession->CalendarInfoL());
       
  1793 			}
       
  1794 		// append to filemapper list
       
  1795 		iFileMappingArray.Append(fileMapper);
       
  1796 		CleanupStack::Pop(fileMapper);
       
  1797 
       
  1798 		iHashDbidIndexMap.Close();
       
  1799 		for(TInt index = 0; index < iFileMappingArray.Count(); index++)
       
  1800 			{
       
  1801 			iHashDbidIndexMap.InsertL(
       
  1802 				iFileMappingArray[index]->GetCollectionId(), index);
       
  1803 			}
       
  1804 		}
       
  1805 	TRACE_EXIT_POINT;
       
  1806 	}
       
  1807 
       
  1808 // -----------------------------------------------------------------------------
       
  1809 // CCalenGlobalData::HandleCalendarInfoUpdatedL
       
  1810 // Handles the notification ECalenNotifyCalendarInfoUpdated
       
  1811 // -----------------------------------------------------------------------------
       
  1812 //
       
  1813 void CCalenGlobalData::HandleCalendarInfoUpdatedL()
       
  1814 	{
       
  1815 	TRACE_ENTRY_POINT;
       
  1816     
       
  1817 	//Update Calendar list.
       
  1818     	UpdateCalendarListL();
       
  1819 	    
       
  1820 	TRACE_EXIT_POINT;
       
  1821 	}
       
  1822 
       
  1823 // -----------------------------------------------------------------------------
       
  1824 // CCalenGlobalData::HandleCalendarFileDeletedL
       
  1825 // Handles the notification ECalenNotifyCalendarFileDeleted
       
  1826 // -----------------------------------------------------------------------------
       
  1827 //
       
  1828 void CCalenGlobalData::HandleCalendarFileDeletedL()
       
  1829 	{
       
  1830 	TRACE_ENTRY_POINT;
       
  1831 
       
  1832 	// get the filename from the context
       
  1833 	TDesC& fileName = iContext->GetCalendarFileNameL();
       
  1834 
       
  1835 	// delete the calendar file
       
  1836 	RemoveCalendarL(fileName);
       
  1837 	
       
  1838 	TRACE_EXIT_POINT;
       
  1839 	}
       
  1840 
       
  1841 // -----------------------------------------------------------------------------
       
  1842 // CCalenGlobalData::DeleteCalendarMissedAlarmsL
       
  1843 // Delete all missed alarms for deleted calendar 
       
  1844 // -----------------------------------------------------------------------------
       
  1845 //
       
  1846 void CCalenGlobalData::DeleteCalendarMissedAlarmsL(const TDesC& aCalendarFilename)
       
  1847     {
       
  1848     TRACE_ENTRY_POINT
       
  1849     //Clear all missed alarms related to this calendar
       
  1850     CMissedAlarmStore* missedAlarmStore = CMissedAlarmStore::NewLC();
       
  1851     RPointerArray<CMissedAlarm> missedAlarms;
       
  1852     CleanupResetAndDestroyPushL(missedAlarms);
       
  1853 
       
  1854     missedAlarmStore->GetL(missedAlarms);
       
  1855 
       
  1856     for (TInt idx = 0; idx < missedAlarms.Count(); idx++)
       
  1857         {
       
  1858         CMissedAlarm* missedAlarm = missedAlarms[idx];
       
  1859         if (!missedAlarm->iCalFileName.CompareF(aCalendarFilename))
       
  1860             {
       
  1861             missedAlarmStore->RemoveL(*missedAlarm);
       
  1862             }
       
  1863         }
       
  1864 
       
  1865     CleanupStack::PopAndDestroy(&missedAlarms);
       
  1866     CleanupStack::PopAndDestroy(missedAlarmStore);
       
  1867     TRACE_EXIT_POINT    
       
  1868     }
       
  1869 
       
  1870 // End of file