src/ext/amaretto/calendar/inc/calendarmodule.h
changeset 0 ca70ae20a155
equal deleted inserted replaced
-1:000000000000 0:ca70ae20a155
       
     1 /*
       
     2  * ====================================================================
       
     3  *  calendarmodule.h
       
     4  *
       
     5  *  Python API to Series 60 Calendar database.
       
     6  *
       
     7  * Copyright (c) 2006-2007 Nokia Corporation
       
     8  *
       
     9  * Licensed under the Apache License, Version 2.0 (the "License");
       
    10  * you may not use this file except in compliance with the License.
       
    11  * You may obtain a copy of the License at
       
    12  *
       
    13  *     http://www.apache.org/licenses/LICENSE-2.0
       
    14  *
       
    15  * Unless required by applicable law or agreed to in writing, software
       
    16  * distributed under the License is distributed on an "AS IS" BASIS,
       
    17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    18  * See the License for the specific language governing permissions and
       
    19  * limitations under the License.
       
    20  * ====================================================================
       
    21  */
       
    22 
       
    23 #include "Python.h"
       
    24 
       
    25 #include "symbian_python_ext_util.h"
       
    26 
       
    27 #include <calsession.h> 
       
    28 #include <calentryview.h> 
       
    29 #include <calinstanceview.h> 
       
    30 #include <calinstance.h> 
       
    31 #include <calentry.h>
       
    32 #include <caliterator.h>
       
    33 #include <calprogresscallback.h>
       
    34 #include <e32base.h>
       
    35 #include <calalarm.h>
       
    36 #include <s32mem.h>
       
    37 #include <caldataexchange.h>
       
    38 #include <CalDataFormat.h>
       
    39 #include <e32std.h>
       
    40 #include <calrrule.h> 
       
    41 #include <calcommon.h>
       
    42 #include <tz.h>
       
    43 
       
    44 #define KNullEntryId 0
       
    45 
       
    46 #define NOT_REPEATED 0
       
    47 #define DAILY_REPEAT 1
       
    48 #define WEEKLY_REPEAT 2
       
    49 #define MONTHLY_BY_DATES_REPEAT 3
       
    50 #define MONTHLY_BY_DAYS_REPEAT 4
       
    51 #define YEARLY_BY_DATE_REPEAT 5
       
    52 #define YEARLY_BY_DAY_REPEAT 6
       
    53 
       
    54 #define DAYS_IN_WEEK 7
       
    55 #define DAYS_IN_MONTH 31
       
    56 #define WEEKS_IN_MONTH 5
       
    57 #define MONTHS_IN_YEAR 12
       
    58 
       
    59 #define EARLIEST_ALARM_DAY_INTERVAL  1001
       
    60 
       
    61 // Keys of repeat dictionary
       
    62 _LIT8(KRepeatType, "type");
       
    63 _LIT8(KStartDate, "start");
       
    64 _LIT8(KEndDate, "end");
       
    65 _LIT8(KRepeatDays, "days");
       
    66 _LIT8(KExceptions, "exceptions");
       
    67 _LIT8(KInterval, "interval");
       
    68 
       
    69 _LIT8(KWeek, "week");
       
    70 _LIT8(KMonth, "month");
       
    71 _LIT8(KDay, "day");
       
    72 
       
    73 // Repeat types
       
    74 _LIT8(KDaily, "daily");
       
    75 _LIT8(KWeekly, "weekly");
       
    76 _LIT8(KMonthlyByDates, "monthly_by_dates");
       
    77 _LIT8(KMonthlyByDays, "monthly_by_days");
       
    78 _LIT8(KYearlyByDate, "yearly_by_date");
       
    79 _LIT8(KYearlyByDay, "yearly_by_day");
       
    80 _LIT8(KRepeatNone, "no_repeat");
       
    81 
       
    82 _LIT8(KUniqueId, "id");
       
    83 _LIT8(KDateTime, "datetime");
       
    84 
       
    85 
       
    86 
       
    87 
       
    88 NONSHARABLE_CLASS(CCalEntryViewAdapter) : public CBase, public MCalProgressCallBack
       
    89 {
       
    90  public:
       
    91   static CCalEntryViewAdapter* NewL(CCalSession& aSession)
       
    92   {
       
    93     CCalEntryViewAdapter* self = new (ELeave) CCalEntryViewAdapter(aSession);
       
    94     CleanupStack::PushL(self);
       
    95     self->ConstructL();
       
    96     CleanupStack::Pop();
       
    97     return self;
       
    98   };         
       
    99   void ConstructL()
       
   100   {
       
   101     iWait = new (ELeave) CActiveSchedulerWait;
       
   102   };
       
   103   CCalEntryViewAdapter(CCalSession& aSession):iWait(NULL),iSession(aSession),iError(KErrNone),iEntryView(NULL)
       
   104   {
       
   105   };
       
   106   virtual ~CCalEntryViewAdapter()
       
   107   {
       
   108     delete iWait;
       
   109     delete iEntryView;
       
   110   };
       
   111   TInt InitiateL()
       
   112   {
       
   113     iEntryView = CCalEntryView::NewL(iSession, *this);
       
   114     iWait->Start();
       
   115     return iError;
       
   116   };  
       
   117  private:
       
   118   void Progress(TInt /*aPercentageCompleted*/){
       
   119   };
       
   120   void Completed(TInt aError){
       
   121     iError = aError;
       
   122     iWait->AsyncStop(); 
       
   123   };
       
   124   TBool NotifyProgress(){
       
   125     return ETrue;
       
   126   }
       
   127   
       
   128   CActiveSchedulerWait* iWait; 
       
   129   CCalSession& iSession; 
       
   130   TInt iError;
       
   131  public:
       
   132   CCalEntryView* iEntryView; 
       
   133 };
       
   134 
       
   135 
       
   136 NONSHARABLE_CLASS(CCalInstanceViewAdapter) : public CBase, public MCalProgressCallBack
       
   137 {
       
   138  public:
       
   139   static CCalInstanceViewAdapter* NewL(CCalSession& aSession)
       
   140   {
       
   141     CCalInstanceViewAdapter* self = new (ELeave) CCalInstanceViewAdapter(aSession);
       
   142     CleanupStack::PushL(self);
       
   143     self->ConstructL();
       
   144     CleanupStack::Pop();
       
   145     return self;
       
   146   };         
       
   147   void ConstructL()
       
   148   {
       
   149     iWait = new (ELeave) CActiveSchedulerWait;
       
   150   };
       
   151   CCalInstanceViewAdapter(CCalSession& aSession):iWait(NULL),iSession(aSession),iError(KErrNone),iInstanceView(NULL)
       
   152   {
       
   153   };
       
   154   virtual ~CCalInstanceViewAdapter()
       
   155   {
       
   156     delete iWait;
       
   157     delete iInstanceView;
       
   158   };
       
   159   TInt InitiateL()
       
   160   {
       
   161     iInstanceView = CCalInstanceView::NewL(iSession, *this);
       
   162     iWait->Start();
       
   163     return iError;
       
   164   };  
       
   165  private:
       
   166   void Progress(TInt /*aPercentageCompleted*/){
       
   167   };
       
   168   void Completed(TInt aError){
       
   169     iError = aError;
       
   170     iWait->AsyncStop(); 
       
   171   };
       
   172   TBool NotifyProgress(){
       
   173     return ETrue;
       
   174   }
       
   175   
       
   176   CActiveSchedulerWait* iWait; 
       
   177   CCalSession& iSession; 
       
   178   TInt iError;
       
   179  public:
       
   180   CCalInstanceView* iInstanceView; 
       
   181 };
       
   182 
       
   183 
       
   184 
       
   185 //////////////TYPE DEFINITION
       
   186 
       
   187 
       
   188 
       
   189 #define CalendarDb_type ((PyTypeObject*)SPyGetGlobalString("CalendarDbType"))
       
   190 struct CalendarDb_object {
       
   191   PyObject_VAR_HEAD
       
   192   CCalSession* session;
       
   193   CCalEntryViewAdapter* entryViewAdapter;  
       
   194   CCalInstanceViewAdapter* instanceViewAdapter;  
       
   195 };
       
   196 
       
   197 #define Entry_type ((PyTypeObject*)SPyGetGlobalString("EntryType"))
       
   198 struct Entry_object {
       
   199   PyObject_VAR_HEAD
       
   200   CalendarDb_object* calendarDb;
       
   201   CCalEntry* entry;
       
   202 };
       
   203 
       
   204 #define EntryIterator_type ((PyTypeObject*)SPyGetGlobalString("EntryIteratorType"))
       
   205 struct EntryIterator_object {
       
   206   PyObject_VAR_HEAD
       
   207   CalendarDb_object* calendarDb;
       
   208   RArray<TCalLocalUid>* ids;
       
   209   TInt index;
       
   210   TBool Initialized;
       
   211 };
       
   212 
       
   213 
       
   214 
       
   215 //////////////MACRO DEFINITION///////////////
       
   216 
       
   217 
       
   218 
       
   219 #define GET_VALUE_FROM_DICT(str_key, dict) \
       
   220   key = Py_BuildValue("s", (&str_key)->Ptr());\
       
   221   if(NULL==key){ \
       
   222     return NULL; \
       
   223   } \
       
   224   value = PyDict_GetItem(dict, key); \
       
   225   Py_DECREF(key); \
       
   226   
       
   227 #define GET_REP_START_AND_END \
       
   228   pythonRealAsTTime(startDate, startTime);\
       
   229   pythonRealAsTTime(endDate, endTime); \
       
   230   startTime=truncToDate(startTime); \
       
   231   endTime=truncToDate(endTime); \
       
   232   if(eternalRepeat!=EFalse){ \
       
   233     endTime=startTime; \
       
   234   } \
       
   235   if(startDate==0 || EFalse==Check_time_validity(startTime)){ \
       
   236     PyErr_SetString(PyExc_TypeError, "start date illegal or missing"); \
       
   237     return NULL; \
       
   238   } \
       
   239   if((eternalRepeat==EFalse && endDate==0) || EFalse==Check_time_validity(endTime)){ \
       
   240     PyErr_SetString(PyExc_TypeError, "end date illegal or missing"); \
       
   241     return NULL; \
       
   242   } \
       
   243   if(endTime<=startTime){ \
       
   244     PyErr_SetString(PyExc_TypeError, "end date must be greater than start date"); \
       
   245     return NULL; \
       
   246   } \
       
   247   endTime+=TTimeIntervalDays(1); \
       
   248   TRAP(error,{ \
       
   249     startCalTime.SetTimeUtcL(startTime); \
       
   250     endCalTime.SetTimeUtcL(endTime); \
       
   251   }); \
       
   252   if(error!=KErrNone){ \
       
   253     return SPyErr_SetFromSymbianOSErr(error); \
       
   254   } \
       
   255  
       
   256 #define SET_REPEAT_DATES \
       
   257   rpt.SetDtStart(startCalTime); \
       
   258   if(eternalRepeat==EFalse){ \
       
   259     rpt.SetUntil(endCalTime); \
       
   260   } \
       
   261   rpt.SetInterval(interval); \
       
   262   
       
   263 #define ADD_REPEAT \
       
   264   TRAP(error, { \
       
   265     self->entry->SetRRuleL(rpt); \
       
   266   }); \
       
   267   if(error!=KErrNone){ \
       
   268     return SPyErr_SetFromSymbianOSErr(error); \
       
   269   }; \
       
   270   if(exceptionArray){ \
       
   271     TRAP(error, { \
       
   272       self->entry->SetExceptionDatesL(*exceptionArray); \
       
   273     }); \
       
   274     if(error != KErrNone){ \
       
   275       return SPyErr_SetFromSymbianOSErr(error); \
       
   276     } \
       
   277   } \
       
   278   
       
   279 #define ADD_ITEM_TO_REP_DICT(key, value) \
       
   280   if(!(key && value)){ \
       
   281     Py_XDECREF(key); \
       
   282     Py_XDECREF(value); \
       
   283     Py_DECREF(repeatDataDict); \
       
   284     return NULL; \
       
   285   } \
       
   286   err = PyDict_SetItem(repeatDataDict, key, value); \
       
   287   Py_DECREF(key); \
       
   288   Py_DECREF(value); \
       
   289   if(err){ \
       
   290     Py_DECREF(repeatDataDict); \
       
   291     return NULL; \
       
   292   } \
       
   293   
       
   294 #define CHECK_DAYLIST_CREATION \
       
   295   if(dayList==NULL){ \
       
   296     days.Close(); \
       
   297     return PyErr_NoMemory(); \
       
   298   } \
       
   299   
       
   300 #define SET_ITEM_TO_DAYLIST \
       
   301   if(dayNum==NULL){ \
       
   302     days.Close(); \
       
   303     Py_DECREF(dayList); \
       
   304     return NULL; \
       
   305   } \
       
   306   if(PyList_SetItem(dayList, i, dayNum)){ \
       
   307     days.Close(); \
       
   308     Py_DECREF(dayList); \
       
   309     return NULL; \
       
   310   } \
       
   311 
       
   312 #define DELETE_INSTANCES \
       
   313   while(instanceArray.Count()>0){ \
       
   314     delete instanceArray[0]; \
       
   315     instanceArray.Remove(0); \
       
   316   } \
       
   317   instanceArray.Close(); \
       
   318   
       
   319 
       
   320     
       
   321 //////////////METHODS///////////////
       
   322   
       
   323 
       
   324 
       
   325 extern "C" PyObject *
       
   326 new_Entry_object(CalendarDb_object* self, 
       
   327                  TCalLocalUid uniqueId, 
       
   328                  CCalEntry::TType entryType = CCalEntry::EAppt,
       
   329                  const TDesC8* globalUid = NULL);
       
   330                  
       
   331