examples/ForumNokia/CalendarExample/src/CalendarHelperEntry.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 
00006 // ----------------------------------------------------
00007 //      CCalHelperEntry - a wrapper class for CCalEntry
00008 // ----------------------------------------------------
00009 
00010 // ================= MEMBER FUNCTIONS =======================
00011 
00012 #include "CalendarHelperEntry.h"
00013 
00014 #include <e32math.h>
00015 #include <calalarm.h>
00016 #include <CalInstance.h>
00017 #include "CalendarAPIexample.pan"
00018 
00019 #include <calentry.h>
00020 
00021 // CONSTANTS
00022 const TInt KMaxSync = 2;
00023 const TInt KDefaultAlarmHour = 8;
00024 const TInt KDefaultAnniversaryHour = 8;
00025 const TInt KMinutesInHour = 60;
00026 const TInt KUndefinedModifyIndex = -1;
00027 const TInt KGuidLength = 30;
00028 
00029 // Two-phased constructor.
00030 CCalHelperEntry* CCalHelperEntry::NewL(CCalEntry* aAnniv)
00031     {
00032     CCalHelperEntry* self = CCalHelperEntry::NewLC(aAnniv);
00033     CleanupStack::Pop(self);
00034     return self;
00035     }
00036 
00037 // Two-phased constructor.
00038 CCalHelperEntry* CCalHelperEntry::NewLC(CCalEntry* aAnniv)
00039     {
00040     CCalHelperEntry* self = new (ELeave) CCalHelperEntry;
00041     CleanupStack::PushL(self);
00042     self->ConstructL(aAnniv);
00043     return self;
00044     }
00045 
00046 // Two-phased constructor.
00047 CCalHelperEntry* CCalHelperEntry::NewL(CCalInstance* aAnniv)
00048     {
00049     CCalHelperEntry* self = CCalHelperEntry::NewLC(aAnniv);
00050     CleanupStack::Pop(self);
00051     return self;
00052     }
00053 
00054 // Two-phased constructor.
00055 CCalHelperEntry* CCalHelperEntry::NewLC(CCalInstance* aAnniv)
00056     {
00057     CCalHelperEntry* self = new (ELeave) CCalHelperEntry;
00058     CleanupStack::PushL(self);
00059     self->ConstructL(aAnniv);
00060     return self;
00061     }
00062 
00063 
00064 // ----------------------------------------------------
00065 // Standard Symbian OS 2nd phase constructor
00066 // Will initialize entry with values of parameter aAnniv,
00067 // if parameter aAnniv is provided (not NULL).
00068 // ----------------------------------------------------
00069 //
00070 void CCalHelperEntry::ConstructL(CCalInstance* aAnniv)
00071     {
00072     iInstance = aAnniv;
00073     ConstructL(&aAnniv->Entry());
00074     }
00075 
00076 void CCalHelperEntry::ConstructL(CCalEntry* aAnniv)
00077     {
00078     // initialize today's date for anniversary date and alarm.
00079     TTime now;
00080     now.HomeTime();
00081     iDate = now.DateTime();
00082     
00083     if ( aAnniv )
00084         {            
00085         TTime starttime = aAnniv->StartTimeL().TimeLocalL();
00086         TDateTime time2 = starttime.DateTime();
00087 
00088         TTime endtime = aAnniv->StartTimeL().TimeLocalL();
00089         TDateTime time3 = endtime.DateTime();
00090         }
00091     
00092     iDate.SetHour(KDefaultAnniversaryHour);
00093     iDate.SetMinute(0);
00094     iDate.SetSecond(0);
00095     iDate.SetMicroSecond(0);
00096     
00097     iAlarmTime = now.DateTime();
00098     User::LeaveIfError(iAlarmTime.SetHour(KDefaultAlarmHour));
00099     User::LeaveIfError(iAlarmTime.SetMinute(0));
00100     User::LeaveIfError(iAlarmTime.SetSecond(0));
00101     User::LeaveIfError(iAlarmTime.SetMicroSecond(0));
00102     iAlarmDays = TTimeIntervalDays(0);
00103     
00104     iAlarmMinutes = TTimeIntervalMinutes(KDefaultAlarmHour*KMinutesInHour);
00105 
00106     if (!aAnniv)
00107         {
00108         return;
00109         }
00110 
00111     iAnniv = aAnniv;
00112 
00113     // read the name for the anniversary.
00114     TBuf<KMaxNameLength> name;
00115     name = aAnniv->SummaryL();
00116     iName = name;
00117 
00118     // read the date for the anniversary.
00119     iDate = aAnniv->StartTimeL().TimeLocalL().DateTime();
00120 
00121     iAlarmPtr = iAnniv->AlarmL();
00122     
00123     iAlarm = iAlarmPtr ? ETrue : EFalse;
00124     if (iAlarm)
00125     {
00126 
00127         iAlarmMinutes = -iAlarmPtr->TimeOffset().Int();
00128         iAlarmTime = iDate;
00129         TTime alarm = iAlarmTime;
00130         alarm += iAlarmMinutes;
00131         iAlarmTime = alarm.DateTime();
00132     }
00133 
00134     // read how the anniversary is to be synchronized.
00135     CCalEntry::TReplicationStatus annivReplStatus =
00136         aAnniv->ReplicationStatusL();
00137     if (annivReplStatus == CCalEntry::EOpen)
00138         {
00139         SetSynchronizationMethod(KSyncPublic);
00140         }
00141     else if (annivReplStatus == CCalEntry::EPrivate)
00142         {
00143         SetSynchronizationMethod(KSyncPrivate);
00144         }
00145     else
00146         {
00147         SetSynchronizationMethod(KSyncNo);
00148         }
00149 
00150     iModified = EFalse;
00151     }
00152 
00153 // constructor
00154 CCalHelperEntry::CCalHelperEntry()
00155     {
00156     }
00157     
00158 // destructor
00159 CCalHelperEntry::~CCalHelperEntry()
00160     {
00161     //If there's a CCalInstance then delete it. 
00162     //It deletes the corresponding CCalEntry object
00163     if( iInstance )   
00164         {
00165         delete iInstance;
00166         iInstance = NULL;
00167         }
00168     else //there only the CCalEntry
00169         {
00170         if (iAnniv)
00171             delete iAnniv;
00172         iAnniv=NULL;
00173         }
00174     if (iAlarmPtr)
00175         delete iAlarmPtr;
00176     iAlarmPtr=NULL;
00177     }
00178 
00179 // ----------------------------------------------------
00180 // CCalHelperEntry::Anniv()
00181 // Returns a pointer to the CCalEntry member variable of
00182 // the entry.
00183 // ----------------------------------------------------
00184 //
00185 CCalEntry* CCalHelperEntry::Anniv()
00186     {
00187     CCalEntry* anniv = iAnniv;
00188     return anniv;
00189     }
00190 
00191 // ----------------------------------------------------
00192 // CCalHelperEntry::Name()
00193 // Returns the name of the entry.
00194 // ----------------------------------------------------
00195 //
00196 TBuf<KMaxNameLength> CCalHelperEntry::Name() const
00197     {
00198     return iName;
00199     }
00200 
00201 // ----------------------------------------------------
00202 // CCalHelperEntry::Date()
00203 // Returns the date of the entry.
00204 // ----------------------------------------------------
00205 //
00206 TDateTime CCalHelperEntry::Date() const
00207     {
00208     return iDate;
00209     }
00210 
00211 // ----------------------------------------------------
00212 // CCalHelperEntry::Alarm()
00213 // Returns the alarm status of the entry.
00214 // ----------------------------------------------------
00215 //
00216 TBool CCalHelperEntry::Alarm() const
00217     {
00218     return iAlarm;
00219     }
00220 
00221 // ----------------------------------------------------
00222 // CCalHelperEntry::AlarmTime()
00223 // Returns the alarm time of the entry.
00224 // ----------------------------------------------------
00225 //
00226 TDateTime CCalHelperEntry::AlarmTime() const
00227     {
00228     return iAlarmTime;
00229     }
00230 
00231 // ----------------------------------------------------
00232 // CCalHelperEntry::SynchronizationMethod()
00233 // Returns the syncronization method of the entry.
00234 // ----------------------------------------------------
00235 //
00236 TInt CCalHelperEntry::SynchronizationMethod() const
00237     {
00238     return iSynchronizationMethod;
00239     }
00240 
00241 // ----------------------------------------------------
00242 // CCalHelperEntry::Modified()
00243 // Returns whether entry has been modified or not.
00244 // ----------------------------------------------------
00245 //
00246 TBool CCalHelperEntry::Modified() const
00247     {
00248     return iModified;
00249     }
00250 
00251 // ----------------------------------------------------
00252 // CCalHelperEntry::DateHasChanged()
00253 // Returns whether the date of the entry has been
00254 // modified or not.
00255 // ----------------------------------------------------
00256 //
00257 TBool CCalHelperEntry::DateHasChanged() const
00258     {
00259     return iDateModified;
00260     }
00261 
00262 // ----------------------------------------------------
00263 // CCalHelperEntry::ResetTimeL()
00264 // Sets given dates time to midnight.
00265 // ----------------------------------------------------
00266 //
00267 void CCalHelperEntry::ResetTimeL(TDateTime& aDate) const
00268     {
00269     User::LeaveIfError(aDate.SetHour(0));
00270     User::LeaveIfError(aDate.SetMinute(0));
00271     User::LeaveIfError(aDate.SetSecond(0));
00272     User::LeaveIfError(aDate.SetMicroSecond(0));
00273     }
00274 
00275 // ----------------------------------------------------
00276 // CCalHelperEntry::GetDaysAndMinutes()
00277 // Calculates how many days and minutes aTime is from
00278 // aFromTime.
00279 // ----------------------------------------------------
00280 //
00281 void CCalHelperEntry::GetDaysAndMinutesL(const TDateTime& aTime,
00282                                 const TDateTime& aFromTime,
00283                                 TTimeIntervalDays& aDays,
00284                                 TTimeIntervalMinutes& aMinutes) const
00285     {
00286     // Create modifiable TDateTime (aTime cannot be modified, because it's const).
00287     TDateTime modifiableTime = aTime;
00288     // Reset the modifiableTime. This will set the dates time to midnight.
00289     ResetTimeL(modifiableTime);
00290     // Create new TTime object. This is a reset aTime in TTime format. (modifiableTime has no more use)
00291     TTime resetTime(modifiableTime);
00292 
00293     // Create modifiable TDateTime (aFromTime cannot be modified, because it's const).
00294     TDateTime modifiableFromTime = aFromTime;
00295     // Reset the modifiableTime. This will set the dates time to midnight.
00296     ResetTimeL(modifiableFromTime);
00297     // Create new TTime object. This is a reset aFromTime in TTime format. (modifiableFromTime has no more use)
00298     TTime resetFromTime(modifiableFromTime);
00299 
00300     // Create a TTime object from aTime that is not reset.
00301     TTime unresetTime(aTime);
00302 
00303     // calculate the difference of days between aTime and aFromTime when their
00304     // time is reset and only dates are different.
00305     aDays = resetFromTime.DaysFrom(resetTime);
00306 
00307     TTimeIntervalMinutes minutes;
00308     // calucate the difference of minutes between aTime and aFromTime. This is
00309     // done by just comparing the time of aTime to midnight.
00310     User::LeaveIfError(unresetTime.MinutesFrom(resetTime, minutes));
00311 
00312     aMinutes = minutes;
00313     }
00314 
00315 // ----------------------------------------------------
00316 // CCalHelperEntry::SetValuesL()
00317 // Sets given values to entry. Return true if values
00318 // are valid and false if not.
00319 // ----------------------------------------------------
00320 // This is called by CCalendarAPIexampleEntryItemList
00321 // SaveL once the modifications are done using
00322 // CCalendarAPIexampleEntryView
00323 
00324 TBool CCalHelperEntry::SetValues(   const TDesC& aName,
00325                             const TDateTime& aDate,
00326                             const TBool& aAlarm,
00327                             const TDateTime& aAlarmTime,
00328                             const TInt& aSync)
00329     {
00330     SetDate(aDate); //sets the hours to 8
00331     SetName(aName);
00332     SetSynchronizationMethod(aSync);
00333     SetAlarm(aAlarm);    
00334 
00335     if (aAlarm)
00336         {
00337         // Event time from ENTRY
00338         TTime startTime = iDate;
00339 
00340         // Alarm cannot occur after the entry date.        
00341         // We have set the anniversary time to 8:00 but actually ignore it
00342         
00343         // Alarm time from FORM
00344         TDateTime alarmDay = iDate;
00345         //This doesn't work in the last day of month!
00346         //alarmDay.SetDay( alarmDay.Day() + 1 ); 
00347         
00348         alarmDay.SetHour(0);
00349         alarmDay.SetMinute(0);
00350         alarmDay.SetMicroSecond(0);
00351         
00352         //TDateTime theNextDay = alarmDay 
00353         TTimeIntervalDays oneDay(1);
00354         TTime time = alarmDay;
00355         time += oneDay;
00356         //oneDay
00357         alarmDay = time.DateTime();
00358         
00359         
00360         TTime alarmTime = aAlarmTime;
00361         TTimeIntervalMinutes minutes;
00362         alarmTime.MinutesFrom( alarmDay, minutes );
00363         
00364         if (minutes.Int() > 0)            
00365             {
00366             return EFalse;
00367             }
00368                         
00369         //Now we should check that the alarm time is not in the past:
00370         TTime now;
00371         now.HomeTime();
00372         //iDate = now.DateTime();
00373         now.MinutesFrom(alarmTime, minutes);
00374         
00375         if (minutes.Int() > 0)            
00376             {
00377             return EFalse;
00378             }
00379         
00380         TTime date = iDate;
00381         alarmTime = aAlarmTime;
00382         alarmTime.MinutesFrom(date, minutes);
00383         
00384         iAlarmTime = aAlarmTime;
00385         iAlarmMinutes = minutes;
00386         
00387         iModified = ETrue;
00388         }
00389 
00390     return ETrue;
00391     }
00392 
00393 // ----------------------------------------------------
00394 // CCalHelperEntry::SetName()
00395 // Sets given name to entry. If name has changed,
00396 // iModified is set to true;
00397 // ----------------------------------------------------
00398 //
00399 void CCalHelperEntry::SetName(const TDesC& aName)
00400     {
00401     if (aName != iName)
00402         {
00403         iName = aName;
00404         iModified = ETrue;
00405         }
00406     }
00407 
00408 // ----------------------------------------------------
00409 // CCalHelperEntry::SetDate()
00410 // Sets given date to entry. If date has changed,
00411 // iModified is set to true;
00412 // ----------------------------------------------------
00413 //
00414 void CCalHelperEntry::SetDate(const TDateTime& aDate)
00415     {
00416     // TDateTime cannot be used for comparison, local TTime variables needed.
00417     TTime currentDate(iDate);
00418     TTime newDate(aDate);
00419     if (newDate != currentDate)
00420         {
00421         iDate = aDate;
00422         
00423         iDate.SetHour(KDefaultAnniversaryHour); 
00424         
00425         iModified = ETrue;
00426         iDateModified = ETrue;
00427         }
00428     }
00429 
00430 // ----------------------------------------------------
00431 // CCalHelperEntry::SetAlarm()
00432 // Sets the alarm state of the entry. If name alarm state
00433 // changed, iModified is set to true.
00434 // ----------------------------------------------------
00435 //
00436 void CCalHelperEntry::SetAlarm(const TBool& aAlarm)
00437     {
00438     if (aAlarm != iAlarm)
00439         {
00440         iAlarm = aAlarm;
00441         
00442         iModified = ETrue;
00443         }
00444     }
00445 
00446 // ----------------------------------------------------
00447 // CCalHelperEntry::SetSynchronizationMethod()
00448 // Sets the synchronization state of the entry. If state
00449 // has changed, iModified is set to true. Synchronization
00450 // method defines how the anniversary is synchronized
00451 // e.g. with PC.
00452 // ----------------------------------------------------
00453 //
00454 void CCalHelperEntry::SetSynchronizationMethod(const TInt& aSynchronizationMethod)
00455     {
00456     if (aSynchronizationMethod != iSynchronizationMethod)
00457         {
00458         iSynchronizationMethod = aSynchronizationMethod;
00459         iModified = ETrue;
00460         }
00461     }
00462 
00463 // ----------------------------------------------------
00464 // CCalHelperEntry::CreateAnnivL()
00465 // Creates a new CCalEntry object and initializes it with
00466 // the date of the entry.
00467 // ----------------------------------------------------
00468 //
00469 CCalEntry* CCalHelperEntry::CreateAnnivL()
00470     {
00471     // Create unique ID.
00472     TTime now;
00473     now.HomeTime();
00474     TInt64 seed = now.Int64();
00475     TInt randNum = Math::Rand( seed );
00476     HBufC8* guid = HBufC8::NewLC(KGuidLength);
00477     guid->Des().Num(  randNum );
00478     
00479     //CCalEntry::EMethodNone means that there's no group scheduling
00480     CCalEntry* anniv = CCalEntry::NewL( CCalEntry::EAnniv, guid,
00481                                         CCalEntry::EMethodNone, 0 );
00482     
00483     CleanupStack::Pop( guid );
00484                                             
00485     CleanupStack::PushL(anniv);
00486     TTime startDate(iDate);
00487     TCalTime time;
00488     time.SetTimeLocalL(startDate);
00489     anniv->SetStartAndEndTimeL(time, time);
00490 
00491     if (iAlarm) //if there is a alarm
00492     {   
00493         delete iAlarmPtr;
00494         iAlarmPtr=NULL;
00495         iAlarmPtr = CCalAlarm::NewL(); 
00496 
00497         iAlarmPtr->SetTimeOffset(-iAlarmMinutes.Int());
00498         anniv->SetAlarmL(iAlarmPtr);
00499     }
00500 
00501     CleanupStack::Pop(anniv);
00502 
00503     return anniv;
00504     }
00505 
00506 
00507 
00508 // ----------------------------------------------------
00509 // CCalHelperEntry::SaveValuesL()
00510 // Sets the values of the entry to its member CCalEntry object.
00511 // If member CCalEntry object doesn't exist, it is created.
00512 // ----------------------------------------------------
00513 // This is called by DoSaveL of the engine.
00514 void CCalHelperEntry::SaveValuesL()
00515     {
00516     TBool created = EFalse;
00517     if (iModified)
00518         {
00519         if (!iAnniv) //This is the case when creating a new entry
00520             {        //The values are gotten from CCalendarAPIexampleEntryContainer
00521                      // and CCalendarAPIexampleEntryView.
00522                      // The engine has called this 
00523                      // so there's a iEntry created in the engine.
00524                      // (created by a call to CreateEntryForModificationL)
00525                      // now we need store the given data.
00526                      
00527             //the created CCalHelperEntry goes to engines iEntry.iAnniv
00528             iAnniv = CreateAnnivL(); 
00529             
00530             created = ETrue;
00531             }
00532         
00533         SaveValuesToAnnivL(iAnniv);
00534         
00535         if( !created ) 
00536             {
00537             delete iAlarmPtr;
00538             iAlarmPtr = NULL;
00539             
00540             if( !iAlarm )
00541                 {
00542                 //remove the alarm
00543                 iAnniv->SetAlarmL(NULL);
00544                 }
00545             else
00546                 {
00547                 //Create a new alarm
00548                 iAlarmPtr = CCalAlarm::NewL();
00549                 
00550                 iAlarmPtr->SetTimeOffset(-iAlarmMinutes.Int());
00551                 
00552                 iAnniv->SetAlarmL(iAlarmPtr);
00553                 }
00554             }
00555         
00556         iModified = EFalse;
00557         iDateModified = EFalse;
00558         
00559         }
00560     }
00561 
00562 // ----------------------------------------------------
00563 // CCalHelperEntry::SaveValuesToAnnivL()
00564 // Sets the values of the entry to given CCalEntry object.
00565 // ----------------------------------------------------
00566 //  
00567 void CCalHelperEntry::SaveValuesToAnnivL(CCalEntry* aAnniv)
00568     {
00569     aAnniv->SetSummaryL(iName);
00570     
00571     CCalEntry::TReplicationStatus sync;
00572     switch (iSynchronizationMethod)
00573         {
00574         case KSyncPublic:
00575             sync=CCalEntry::EOpen;
00576             break;
00577         case KSyncPrivate:
00578             sync=CCalEntry::EPrivate;
00579             break;
00580         case KSyncNo:
00581             sync=CCalEntry::ERestricted;
00582             break;
00583         default:
00584             Panic(KInvalidSyncValue);
00585             break;
00586         }
00587     aAnniv->SetReplicationStatusL(sync);
00588     
00589     }
00590 
00591 // ----------------------------------------------------
00592 // CCalHelperEntry::NewAnnivLC()
00593 // Creates a new CCalEntry object and initializes it
00594 // with the data of the entry.
00595 // ----------------------------------------------------
00596 //
00597 CCalEntry* CCalHelperEntry::NewAnnivLC()
00598     {
00599     CCalEntry* anniv = CreateAnnivL();
00600     CleanupStack::PushL(anniv);
00601     SaveValuesToAnnivL(anniv);
00602     return anniv;
00603     }
00604 
00605 // End of File

Generated by  doxygen 1.6.2