examples/ForumNokia/CalendarExample/src/CalendarHelperEntry.cpp

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

Generated by  doxygen 1.6.2