⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendarmodule.cpp

📁 python s60 1.4.5版本的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
extern "C" PyObject *
CalendarDb_export_vcals(CalendarDb_object* self, PyObject *args)
{
  CHECK_AGENDA_STATE_DB

  CArrayFixFlat<TAgnEntryId>* idArray = NULL;
  PyObject* idTuple = NULL;
  PyObject* ret = NULL;
  TInt error = KErrNone;

  if (!PyArg_ParseTuple(args, "O!", 
                        &PyTuple_Type, &idTuple)){ 
    return NULL;
  }
  
  if(PyTuple_Size(idTuple)<1){
    PyErr_SetString(PyExc_SyntaxError, 
                    "no calendar entry id:s given in the tuple");
    return NULL;
  }

  TRAP(error, 
    idArray = new (ELeave) CArrayFixFlat<TAgnEntryId>(PyTuple_Size(idTuple)));
  if(error!=KErrNone){
    delete idArray;
    return SPyErr_SetFromSymbianOSErr(error);
  }

  // Put the calendar entry id:s into the idArray.
  TInt idCount = PyTuple_Size(idTuple);
  for(TInt i=0;i<idCount;i++){
    PyObject* idItem = PyTuple_GetItem(idTuple, i);

    if(!PyInt_Check(idItem)){
      delete idArray;
      PyErr_SetString(PyExc_TypeError, "illegal argument in the tuple");
      return NULL;
    };

    TRAP(error, 
      idArray->AppendL(self->agendaServer->GetEntryId(TAgnUniqueId(PyInt_AsLong(idItem)))));
    if(error != KErrNone){
      delete idArray;
      return SPyErr_SetFromSymbianOSErr(error);
    }
  }

  TRAP(error, {
    CBufFlat* flatBuf = CBufFlat::NewL(4);
    CleanupStack::PushL(flatBuf);
    RBufWriteStream outputStream(*flatBuf);
    CleanupClosePushL(outputStream);

    self->agendaModel->ExportVCalL(outputStream, idArray);

    outputStream.CommitL();        
    CleanupStack::PopAndDestroy(); // Close outputStream.  

    ret = Py_BuildValue("s#", (char*)flatBuf->Ptr(0).Ptr(), flatBuf->Size()); 

    CleanupStack::PopAndDestroy(); // flatBuf.  
  });
  delete idArray;
  if(error!=KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }

  return ret;
}


/*
 * Imports vcalendar entries (given in the string parameter) to the database. 
 * -returns tuple that contains unique ids of the imported entries.
 */
extern "C" PyObject *
CalendarDb_import_vcals(CalendarDb_object* self, PyObject *args)
{
  CHECK_AGENDA_STATE_DB

  CArrayPtrFlat<CAgnEntry>* entryArray = NULL;
  char* vCalStr = NULL;
  TInt32 vCalStrLength = 0;
  PyObject* idTuple = NULL;
  TInt error = KErrNone;

  if (!PyArg_ParseTuple(args, "s#",
                        &vCalStr, &vCalStrLength)){ 
    return NULL;
  }

  TPtrC8 vCalStrPtr((TUint8*)vCalStr, vCalStrLength); 
  
  RMemReadStream inputStream(vCalStrPtr.Ptr(), vCalStrPtr.Length()); 

  TRAP(error, 
    entryArray = new (ELeave) CArrayPtrFlat<CAgnEntry>(ENTRY_PTR_ARR_SIZE));
  if(error!=KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }
    
  TRAP(error, {
    CleanupClosePushL(inputStream);
    self->agendaModel->ImportVCalL(inputStream, entryArray);
    CleanupStack::PopAndDestroy(); // Close inputStream.
  });
  if(error!=KErrNone){
    entryArray->ResetAndDestroy();
    delete entryArray;
    return SPyErr_SetFromSymbianOSErr(error);
  }  

  idTuple = PyTuple_New(entryArray->Count());
  if(NULL==idTuple){
    entryArray->ResetAndDestroy();
    delete entryArray;
    return PyErr_NoMemory();
  }

  TAgnUniqueId id;
  for(TInt i=0;i<entryArray->Count();i++){
    CAgnEntry* entryObj = (*entryArray)[i];
    TRAP(error, 
      id = self->agendaServer->GetUniqueId(self->agendaModel->AddEntryL(entryObj)));
    if(error!=KErrNone){
      entryArray->ResetAndDestroy();
      delete entryArray;
      Py_DECREF(idTuple);
      return SPyErr_SetFromSymbianOSErr(error);
    } 
    PyObject* idObj = Py_BuildValue("i", id.Id());
    if(NULL==idObj){
      entryArray->ResetAndDestroy();
      delete entryArray;
      Py_DECREF(idTuple);
      return NULL;
    } 
    if(PyTuple_SetItem(idTuple, i, idObj)){
      entryArray->ResetAndDestroy();
      delete entryArray;
      Py_DECREF(idTuple);
      return NULL;
    };
  } 
 
  entryArray->ResetAndDestroy();
  delete entryArray;
  return idTuple;
}


/*
 * Compresses the Calendar db file. Returns success status.
 */
extern "C" PyObject *
CalendarDb_compact(CalendarDb_object* self, PyObject */*args*/)
{
  CHECK_AGENDA_STATE_DB
  
  return Py_BuildValue("i", self->agendaServer->CompactFile());
}


/*
 * Entry methods.
 */


/*
 * Creates new calendar entry "wrapper" object.
 * -fetches the entry identified by uniqueIdObj from the 
 * database and wraps that into the python object.
 * -if uniqueIdObj is null-id a new entry is created (but 
 * not added to the database until entry.commit() is called).
 * in this case entry's unique id will remain as a null-id 
 * until the entry is added (by committing) into the database. 
 */
extern "C" PyObject *
new_Entry_object(CalendarDb_object* self, 
                 TAgnUniqueId uniqueIdObj, 
                 CAgnEntry::TType entryType)
{  
  CHECK_AGENDA_STATE_DB

  Entry_object* entryObj = NULL;
  TInt userError = 0;

  if(uniqueIdObj.IsNullId()){

    // New entry must be created

    CAgnEntry* entry = NULL;
    CParaFormatLayer* paraFormatLayer = NULL;
	  CCharFormatLayer* charFormatLayer = NULL;
   
    TRAPD(error, {     
      paraFormatLayer = CParaFormatLayer::NewL();
      CleanupStack::PushL(paraFormatLayer);
      charFormatLayer = CCharFormatLayer::NewL();
      CleanupStack::PushL(charFormatLayer);

      switch(entryType){
        case CAgnEntry::EAppt:       
          entry = CAgnAppt::NewL(paraFormatLayer, charFormatLayer);
        break;
        case CAgnEntry::ETodo:        
          entry = CAgnTodo::NewL(paraFormatLayer, charFormatLayer);
        break;
        case CAgnEntry::EEvent:        
          entry = CAgnEvent::NewL(paraFormatLayer, charFormatLayer);
        break;
        case CAgnEntry::EAnniv:
          entry = CAgnAnniv::NewL(paraFormatLayer, charFormatLayer);
        break;
        default:
          userError = 1;
        break;
      }
      CleanupStack::Pop(); // charFormatLayer
      CleanupStack::Pop(); // paraFormatLayer
    });
  
    if(error!=KErrNone){
      return SPyErr_SetFromSymbianOSErr(error);
    }

    if(userError==1){
      delete paraFormatLayer;
      delete charFormatLayer;
      PyErr_SetString(PyExc_ValueError, "illegal entrytype parameter");
      return NULL; 
    }

    entryObj = PyObject_New(Entry_object, Entry_type); 
    if(entryObj == NULL){
      return PyErr_NoMemory();
    }

    entryObj->charFormatLayer = charFormatLayer;
    entryObj->paraFormatLayer = paraFormatLayer;
    entryObj->calendarDb = self;
    entryObj->entryItem = entry;
    entryObj->uniqueId.SetNullId();
#if SERIES60_VERSION<20
    entryObj->alarm=NULL;
#endif

  }else{

    // Wrap an existing entry. 

    CAgnEntry* entry = NULL;   
    TRAPD(error, {
      entry = self->agendaModel->FetchEntryL(self->agendaServer->GetEntryId(uniqueIdObj));
    });
    if(error!=KErrNone){
      return SPyErr_SetFromSymbianOSErr(error);
    }
    
    if(entry->ReplicationData().HasBeenDeleted()){
      // This entry has been marked as deleted, but has not been removed from the database
      // since it has positive synchronization count. 
      // Anyway, we wont give access to these entrys to users since these entrys are not needed and
      // accessing their data causes crashes in some cases.
      delete entry;
      PyErr_SetString(PyExc_RuntimeError, "cannot access deleted entry");
      return NULL;
    }

    entryObj = PyObject_New(Entry_object, Entry_type); 
    if(entryObj == NULL){
      delete entry;
      return PyErr_NoMemory();
    }

    entryObj->charFormatLayer = NULL;
    entryObj->paraFormatLayer = NULL;
    entryObj->calendarDb = self;
    entryObj->uniqueId = uniqueIdObj;
    entryObj->entryItem = entry;
#if SERIES60_VERSION<20
    entryObj->alarm=NULL;
#endif
  }
  
  Py_INCREF(entryObj->calendarDb);

  return (PyObject*)entryObj;
}


/*
 * Returns entry's location information (text). 
 */
extern "C" PyObject *
Entry_location(Entry_object* self, PyObject* /*args*/)
{
  return Py_BuildValue("u#", self->entryItem->Location().Ptr(), 
                             self->entryItem->Location().Length()); 
}


/*
 * Sets entry's location information (text). 
 */
extern "C" PyObject *
Entry_set_location(Entry_object* self, PyObject* args)
{
  PyObject* location = NULL;
  if (!PyArg_ParseTuple(args, "U", &location)){ 
    return NULL;
  }
  TPtrC locationPtr((TUint16*) PyUnicode_AsUnicode(location), 
                    PyUnicode_GetSize(location));
  TRAPD(error, {
    self->entryItem->SetLocationL(locationPtr);
  });
  if(error!=KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }
  Py_INCREF(Py_None);
  return Py_None;
}


/*
 * Sets entry's start and end datetime.
 */
extern "C" PyObject *
Entry_set_start_and_end_datetime(Entry_object* self, PyObject* args)
{
  TReal startTime = 0;
  TReal endTime = 0;
  if (!PyArg_ParseTuple(args, "dd", &startTime, &endTime)){ 
    return NULL;
  }

  TTime startTTime;
  TTime endTTime;
  pythonRealUtcAsTTime(startTime, startTTime);
  pythonRealUtcAsTTime(endTime, endTTime);

  if(Check_time_validity(startTTime)==EFalse){
    PyErr_SetString(PyExc_ValueError, "illegal start datetime");
    return NULL;
  }
  if(Check_time_validity(endTTime)==EFalse){
    PyErr_SetString(PyExc_ValueError, "illegal end datetime");
    return NULL;
  }

  switch(self->entryItem->Type()){
    case CAgnEntry::EAppt:
      {  
        if(startTTime>endTTime){ 
          PyErr_SetString(PyExc_ValueError, 
                          "start datetime cannot be greater than end datetime");
          return NULL;
        }
        self->entryItem->CastToAppt()->SetStartAndEndDateTime(startTTime, endTTime);
      }
    break;
    case CAgnEntry::EEvent:
      {
        START_END_DATE_CHECK
        self->entryItem->CastToEvent()->SetStartAndEndDate(startTTime, endTTime);
      }
    break;
    case CAgnEntry::EAnniv:
      {
        START_END_DATE_CHECK
        self->entryItem->CastToAnniv()->SetStartAndEndDate(startTTime, endTTime);
      }
    break;
    case CAgnEntry::ETodo:
      {
        START_END_DATE_CHECK
        self->entryItem->CastToTodo()->SetDueDate(endTTime);
        self->entryItem->CastToTodo()->SetDuration(endTTime.DaysFrom(startTTime));
      }
    break; 
  }  
  Py_INCREF(Py_None);
  return Py_None;
}


/*
 * Returns information whether entry is dated (todos only).
 */
extern "C" PyObject *
Entry_is_dated(Entry_object* self, PyObject* /*args*/)
{
  if(self->entryItem->Type()!=CAgnEntry::ETodo){
    PyErr_SetString(PyExc_RuntimeError, "this method is for todos only");
    return NULL;
  }
  return Py_BuildValue("i", self->entryItem->CastToTodo()->IsDated());
}


/*
 * Makes entry undated (todos only).
 */
extern "C" PyObject *
Entry_make_undated(Entry_object* self, PyObject* /*args*/)
{
  if(self->entryItem->Type()!=CAgnEntry::ETodo){
    PyErr_SetString(PyExc_RuntimeError, "this method is for todos only");
    return NULL;
  }
  self->entryItem->CastToTodo()->MakeUndated();
  Py_INCREF(Py_None);
  return Py_None;
}


/*
 * Returns entry's start datetime.
 */
extern "C" PyObject *
Entry_start_datetime(Entry_object* self, PyObject* /*args*/)
{
  TTime startTTime;
  switch(self->entryItem->Type()){
    case CAgnEntry::EAppt:
      {
        startTTime = self->entryItem->CastToAppt()->StartDateTime();
      }
    break;
    case CAgnEntry::EEvent:
      {
        startTTime = self->entryItem->CastToEvent()->StartDate();
      }
    break;
    case CAgnEntry::EAnniv:
      {
        startTTime = self->entryItem->CastToAnniv()->StartDate();
      }
    break;
    case CAgnEntry::ETodo:
      if(self->entryItem->CastToTodo()->IsDated()){
        startTTime = self->entryItem->CastToTodo()->InstanceStartDate();
      }else{
        startTTime=AgnDateTime::AgnDateToTTime(AgnDateTime::NullDate());
      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -