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

📄 contactsmodule.cpp

📁 python s60 1.4.5版本的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  if(PyInt_Check(fieldTypeObj)){
    // Only field type id given.
    const CPbkFieldsInfo& fieldsInfo = 
      self->contactsDb->contactEngine->FieldsInfo();

    fieldInfo = fieldsInfo.Find(PyInt_AsLong(fieldTypeObj));
    if (!fieldInfo){
      PyErr_SetString(PyExc_ValueError, "unsupported field type");
      return NULL; 
    }
  }else if(PyTuple_Check(fieldTypeObj)){
    if(PyTuple_Size(fieldTypeObj)!=2){
      PyErr_SetString(PyExc_ValueError, "tuple must contain field and location id:s");
      return NULL; 
    }
    // field type id and field location given.
    PyObject* fieldIdObj = PyTuple_GetItem(fieldTypeObj, 0);
    PyObject* locationIdObj = PyTuple_GetItem(fieldTypeObj, 1);

    if(!(PyInt_Check(fieldIdObj) && PyInt_Check(locationIdObj))){
      PyErr_SetString(PyExc_TypeError, "illegal parameter in the tuple");
      return NULL; 
    }

    const CPbkFieldsInfo& fieldsInfo = 
      self->contactsDb->contactEngine->FieldsInfo();

    fieldInfo = 
      fieldsInfo.Find((TInt)PyInt_AsLong(fieldIdObj), 
                      static_cast<TPbkFieldLocation>(PyInt_AsLong(locationIdObj)));
    if (!fieldInfo){
      PyErr_SetString(PyExc_ValueError, "unsupported field or location type");
      return NULL; 
    }    
  }else{
    PyErr_SetString(PyExc_TypeError, "illegal fieldtype parameter");
    return NULL; 
  }
  

  TRAP(error, theField = &(self->contactItem->AddFieldL(*fieldInfo)));

  if(error != KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }

  TRAP(error, 
    ret = Contact_modify_field_value_or_labelL(*theField, value, valueLength,
                                               label, dateVal));

  if(error != KErrNone){
    self->contactItem->RemoveField(self->contactItem->FindFieldIndex(*theField));
    return SPyErr_SetFromSymbianOSErr(error);
  }

  if(!ret){
    self->contactItem->RemoveField(self->contactItem->FindFieldIndex(*theField));
    return NULL;
  }

  Py_DECREF(ret);
  return Py_BuildValue("i", self->contactItem->FindFieldIndex(*theField)); 
}


/*
 * Removes specified field of the contact.
 */
extern "C" PyObject *
Contact_remove_field(Contact_object* self, TInt fieldIndex)
{  
  ASSERT_CONTACT_READ_WRITE

  CPbkFieldArray& fieldArray = self->contactItem->CardFields();
  if (fieldIndex < 0 || fieldIndex >= fieldArray.Count()){
    PyErr_SetString(PyExc_IndexError, "illegal field index");
    return NULL; 
  }

  self->contactItem->RemoveField(fieldIndex);

  Py_INCREF(Py_None);
  return Py_None;
}


/*
 * Returns contact data (attributes of contact, not it's field data).
 */
extern "C" PyObject *
Contact_entry_data(Contact_object* self, PyObject* /*args*/)
{
  ASSERT_CONTACTOPEN

  HBufC* titleBuf = NULL;
  TInt error = KErrNone;

  TRAP(error, titleBuf = self->contactItem->GetContactTitleL());

  if(error != KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);  
  }

  PyObject* entryData = 
    Py_BuildValue("{s:i,s:u#,s:d}", 
                   (const char*)(&KKeyStrUniqueId)->Ptr(), self->uniqueID,
                   (const char*)(&KKeyStrTitle)->Ptr(), 
                     titleBuf->Ptr(), titleBuf->Length(),
                   (const char*)(&KKeyStrLastModified)->Ptr(), 
                     time_as_UTC_TReal(self->contactItem->ContactItem().LastModified()));
  delete titleBuf;

  return  entryData;
}


/*
 * Sets the contact to the read-only mode.
 */
extern "C" PyObject *
Contact_open_ro(Contact_object* self)
{
  ASSERT_DBOPEN_CONTACT

  if(self->mode == CONTACT_READ_ONLY){
    // Already in read-only mode.
    Py_INCREF(Py_None);
    return Py_None;
  }

  TRAPD(error, {
    CPbkContactItem* contactItem = 
      self->contactsDb->contactEngine->ReadContactL(self->uniqueID);
    self->contactItem = contactItem;
    self->mode = CONTACT_READ_ONLY;
  });  

  RETURN_ERROR_OR_PYNONE(error);
}


/*
 * Sets the contact to the read-write mode.
 */
extern "C" PyObject *
Contact_open_rw(Contact_object* self)
{
  ASSERT_DBOPEN_CONTACT

  if(self->mode == CONTACT_READ_WRITE){
    // Already in read-write mode.
    Py_INCREF(Py_None);
    return Py_None;
  }

  TRAPD(error, {
    self->contactItem = 
      self->contactsDb->contactEngine->OpenContactL(self->uniqueID);
    self->mode = CONTACT_READ_WRITE;
  });

  RETURN_ERROR_OR_PYNONE(error);
}


/*
 * Deletes the CPbkContactItem inside the contact object.
 */
void Contact_delete_entry(Contact_object* self)
{
  delete self->contactItem;
  self->contactItem = NULL;
  self->mode = CONTACT_NOT_OPEN;
}


/*
 * Opens contact for writing.
 */
extern "C" PyObject *
Contact_begin(Contact_object* self, PyObject* /*args*/)
{
  ASSERT_DBOPEN_CONTACT

  if(self->mode != CONTACT_READ_WRITE){
    Contact_delete_entry(self);
    return Contact_open_rw(self);
  }

  Py_INCREF(Py_None);
  return Py_None;
}


/*
 * Commits the contact.
 */
extern "C" PyObject *
Contact_commit(Contact_object* self, PyObject* /*args*/)
{
  ASSERT_CONTACT_READ_WRITE

  TInt error = KErrNone;
  
  if(self->uniqueID == KNullContactId){
    // New contact.
    TRAP(error, 
      self->uniqueID = 
        self->contactsDb->contactEngine->AddNewContactL(*self->contactItem));
  } else {
    // Old contact (begin() called).
    TRAP(error, 
      self->contactsDb->contactEngine->CommitContactL(*self->contactItem));
  }  
  
  if(error != KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }

  Contact_delete_entry(self);

  return Contact_open_ro(self);
}


/*
 * Closes (but does not commit) the contact.
 */
extern "C" PyObject *
Contact_rollback(Contact_object* self, PyObject* /*args*/)
{
  ASSERT_DBOPEN_CONTACT

  if(self->mode != CONTACT_READ_WRITE || self->uniqueID == KNullContactId){
    PyErr_SetString(PyExc_RuntimeError, "begin not called");
    return NULL; 
  }

  TInt error = KErrNone;

  TRAP(error, 
    self->contactsDb->contactEngine->CloseContactL(self->uniqueID));
  
  if(error != KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }

  Contact_delete_entry(self);

  return Contact_open_ro(self);
}


/*
 * Gets field type (e.g. fieldinfo) index in the tuple of supported
 * field types that corresponds to type of the field indicated
 * by given field index (field index in the contact entry's field array).
 */
extern "C" PyObject *
Contact_field_info_index(Contact_object* self, PyObject *args)
{
  ASSERT_CONTACTOPEN

  TInt fieldIndex;

  if (!PyArg_ParseTuple(args, "i", &fieldIndex)){ 
    return NULL;
  }

  CPbkFieldArray& fieldArray = self->contactItem->CardFields();
  if (fieldIndex < 0 || fieldIndex >= fieldArray.Count()){
    PyErr_SetString(PyExc_IndexError, "illegal field index");
    return NULL; 
  }

  TPbkContactItemField& field = fieldArray[fieldIndex];
  const CPbkFieldsInfo& fieldsInfo = 
    self->contactsDb->contactEngine->FieldsInfo();
  for(TInt i = 0; i< fieldsInfo.Count(); i++){
    if (fieldsInfo[i]->IsSame(field.FieldInfo())){      
      return Py_BuildValue("i", i); // Match found
    }
  }
  
  return Py_BuildValue("i", -1);  
}


/*
 * Returns field data of the field indicated by the fieldIndex.
 */
extern "C" PyObject *
Contact_build_field_data_object(Contact_object* self, TInt fieldIndex)
{
  ASSERT_CONTACTOPEN
  
  CPbkFieldArray& fieldArray = self->contactItem->CardFields();

  if (fieldIndex < 0 || fieldIndex >= fieldArray.Count()){
    PyErr_SetString(PyExc_IndexError, "illegal field index");
    return NULL; 
  }  

  TPbkContactItemField& field = fieldArray[fieldIndex];
  
  PyObject* fieldValue = NULL;

  if(KStorageTypeText == field.FieldInfo().FieldStorageType()){
    fieldValue = 
      Py_BuildValue("u#", field.Text().Ptr(), field.Text().Length()); 
  }else if(KStorageTypeDateTime == field.FieldInfo().FieldStorageType()){
    fieldValue = ttimeAsPythonFloat(field.Time());
  }else{
    fieldValue = Py_BuildValue("u", ""); // binary support ???
  }

  if(fieldValue == NULL){      
    return NULL;
  }

  PyObject* ret =  
    Py_BuildValue("{s:u#,s:u#,s:O,s:i,s:i,s:i,s:i}",
                  (const char*)(&KKeyStrFieldName)->Ptr(), 
                    field.FieldInfo().FieldName().Ptr(), 
                    field.FieldInfo().FieldName().Length(),
                  "label",field.Label().Ptr(), field.Label().Length(),
                  "value", fieldValue,
                  "fieldindex", fieldIndex,
                  (const char*)(&KKeyStrFieldId)->Ptr(), 
                    field.FieldInfo().FieldId(),
                  "storagetype", field.FieldInfo().FieldStorageType(),
                  "maxlength", field.FieldInfo().MaxLength());

  Py_DECREF(fieldValue);

  return ret;
}


/*
 * Returns indexes of the fields that represent given fieldtype 
 * and location (optional) values.
 */
extern "C" PyObject *
Contact_find_field_indexes(Contact_object* self, PyObject* args)
{
  ASSERT_CONTACTOPEN

  CArrayVarFlat<TInt>* indexArray = NULL;
  TInt fieldIndex=0;
  TInt fieldId = -1;
  TInt location = -1;
  TPbkContactItemField* field = NULL;
     
  if (!PyArg_ParseTuple(args, "i|i", &fieldId, &location)){ 
    return NULL;
  }

  TRAPD(error, {
    indexArray = new (ELeave) CArrayVarFlat<TInt>(2); // 2 is the granularity (not the size..)
    do{
      field = self->contactItem->FindField(fieldId, fieldIndex);
      if(field && (field->FieldInfo().Location()==location || location==-1)){
        indexArray->AppendL(fieldIndex, sizeof(fieldIndex));  
      }
      fieldIndex++;
    }while(field);
  });
  if(error!=KErrNone){
    delete indexArray;
    return SPyErr_SetFromSymbianOSErr(error);
  }

  PyObject* indexList = PyList_New(indexArray->Count());
  if(indexList==NULL){
    delete indexArray;
    return PyErr_NoMemory();
  }
  for(TInt i=0;i<indexArray->Count();i++){
    PyObject* theIndex = Py_BuildValue("i", indexArray->At(i));
    if(theIndex==NULL){
      delete indexArray;
      Py_DECREF(indexList);
      return NULL;
    }
    if(0>PyList_SetItem(indexList,i,theIndex)){
      delete indexArray;
      Py_DECREF(indexList);
      return NULL;
    }
  }

  delete indexArray;
  return indexList;
}  


/*
 * Returns number of contact's fields.
 */
static int
Contact_len(Contact_object *self)
{
  ASSERT_CONTACTOPEN_RET_INT

  return self->contactItem->CardFields().Count();
}


/*
 * Returns contact object's field data (indicated by field index)
 */ 
static PyObject *
Contact_getitem(Contact_object *self, PyObject *key)
{
  ASSERT_CONTACTOPEN

  if(!PyInt_Check(key)){
      PyErr_SetString(PyExc_TypeError, "illegal argument");
      return NULL;
  };
  return Contact_build_field_data_object(self, PyInt_AsLong(key));
}
 

/*
 * Deletes specified field.
 */
static int
Contact_ass_sub(Contact_object *self, PyObject *key, PyObject *value)
{
  
  ASSERT_CONTACT_READ_WRITE_RET_INT

  PyObject* result = NULL;

  if(value!=NULL){
    PyErr_SetString(PyExc_NotImplementedError, "cannot set field value this way");
    return -1; 
  }

  if(!PyInt_Check(key)){
      PyErr_SetString(PyExc_TypeError, "illegal argument");
      return -1;
  }

   result = Contact_remove_field(self, PyInt_AsLong(key));

   if(!result){
     return -1;
   }else{
     Py_DECREF(result);
   }  

   return 0;  
} 


/*
 * String representation of Contact object.
 */
static PyObject *
Contact_repr_str(Contact_object* self)
{
  ASSERT_CONTACTOPEN

  HBufC* titleBuf = NULL;
  TInt error = KErrNone;

⌨️ 快捷键说明

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