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

📄 contactsmodule.cpp

📁 python s60 1.4.5版本的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    return SPyErr_SetFromSymbianOSErr(error);
  }
  
  if(IsContactGroup(*item)==EFalse){
    delete item;
    PyErr_SetString(PyExc_RuntimeError, "not a contact group");
    return NULL;
  }
  delete item;
  
  
  TRAP(error, {
    self->contactEngine->Database().AddContactToGroupL(entryId,groupId);
  });
  if(error!=KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }
  
  Py_INCREF(Py_None);
  return Py_None;
}


/*
 * Removes contact from the group.
 */
extern "C" PyObject *
ContactsDb_remove_contact_from_group(ContactsDb_object* self, PyObject *args)
{
  TInt error = KErrNone;
  TInt32 entryId = -1;
  TInt32 groupId = -1;
  CContactItem* item = NULL;
  CContactGroup* group = NULL;
  
  if (!PyArg_ParseTuple(args, "ii", &entryId, &groupId)){ 
    return NULL;
  }
  
  TRAP(error, {
    item = self->contactEngine->Database().ReadContactL(groupId);
  });
  if(error!=KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }
  
  if(IsContactGroup(*item)==EFalse){
    delete item;
    PyErr_SetString(PyExc_RuntimeError, "not a contact group");
    return NULL;
  }
  group = static_cast<CContactGroup*>(item);
  if(group->ContainsItem(entryId)==EFalse){
    delete item;
    PyErr_SetString(PyExc_RuntimeError, "contact is not a member of the group");
    return NULL;
  }
  delete item;
 
  TRAP(error, {
    self->contactEngine->Database().RemoveContactFromGroupL(entryId,groupId);
  });
  if(error!=KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }
  
  Py_INCREF(Py_None);
  return Py_None;
}


/*
 * Creates a new contact group.
 */
extern "C" PyObject *
ContactsDb_create_contact_group(ContactsDb_object* self, PyObject* /*args*/)
{
  TInt error = KErrNone;
  CContactItem* item = NULL;
  PyObject* ret = NULL;
    
  TRAP(error, {
    item = self->contactEngine->Database().CreateContactGroupL();
  });
  if(error!=KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }
  
  ret = Py_BuildValue("i", item->Id());
  delete item;
  
  return ret;
}


/*
 * Returns number of contact groups.
 */
extern "C" PyObject *
ContactsDb_contact_group_count(ContactsDb_object* self, PyObject* /*args*/)
{    
  return Py_BuildValue("i", self->contactEngine->Database().GroupCount());
}


/*
 * ContactsDb_object deletes given contact entry from the database.
 */
extern "C" PyObject *
ContactsDb_delete_entry(ContactsDb_object* self, PyObject* args)
{
  TInt32 entryId = -1;
  if (!PyArg_ParseTuple(args, "i", &entryId)){ 
    return NULL;
  }
    
  
  TInt error = KErrNone;
  TRAP(error, self->contactEngine->Database().DeleteContactL(entryId));
  if(error != KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }

  Py_INCREF(Py_None);
  return Py_None;   
}





/*
 * Returns contact object (indicated by unique id given)
 */ 
static PyObject *
ContactsDb_getitem(ContactsDb_object *self, PyObject *key)
{
  ASSERT_DBOPEN

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


/*
 * Deletes specified contact.
 */
static int
ContactsDb_ass_sub(ContactsDb_object *self, PyObject *key, PyObject *value)
{
  ASSERT_DBOPEN_RET_INT

  PyObject* result = NULL;
 
  if(value!=NULL){
    PyErr_SetString(PyExc_NotImplementedError, "illegal usage");
    return -1; 
  }

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

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

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


/*
 * Returns number of contacts.
 */
static int
ContactsDb_len(ContactsDb_object *self)
{
  ASSERT_DBOPEN_RET_INT

  TInt length = -1;

  TRAPD(error, {
    length = self->contactEngine->Database().CountL();
  });

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

  return length;
}


/*
 * Tests whether a compress is recommended.
 */
extern "C" PyObject *
ContactsDb_compact_recommended(ContactsDb_object* self, PyObject* args)
{
  return Py_BuildValue("i", self->contactEngine->Database().CompressRequired());
}


/*
 * Compresses the database.
 */
extern "C" PyObject *
ContactsDb_compact(ContactsDb_object* self, PyObject* args)
{
  ASSERT_DBOPEN

  TInt error = KErrNone;

  Py_BEGIN_ALLOW_THREADS
  TRAP(error, {
    self->contactEngine->Database().CompactL();
  });
  Py_END_ALLOW_THREADS

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


/*
 * Contact methods.
 */


/*
 * Create new Contact object.
 * if (uniqueId == KNullContactId) completely new contact entry is created.
 * Else only new (python) contact object is created (wrapper object to 
 * existing contact entry that the uniqueId identifies).
 */
extern "C" PyObject *
new_Contact_object(ContactsDb_object* self, TContactItemId uniqueId)
{
  ASSERT_DBOPEN

  Contact_object* contact = PyObject_New(Contact_object, Contact_type);
  if (contact == NULL){
    return PyErr_NoMemory();
  }

  // Initialize the contact struct.
  contact->mode = CONTACT_NOT_OPEN;
  contact->contactItem = NULL;
  contact->contactsDb = NULL;
  contact->uniqueID = KNullContactId;
  
  if(uniqueId == KNullContactId){        
    // a new contact entry must be created into the database.
    TRAPD(error, {
      CPbkContactItem* newContact = 
        self->contactEngine->CreateEmptyContactL(); 
      contact->contactItem = newContact;
      contact->mode = CONTACT_READ_WRITE;
    });

    if(error != KErrNone){  
      PyObject_Del(contact);
      return SPyErr_SetFromSymbianOSErr(error);
    } 
  }else{
    // contact entry that corresponds to given unique id exists.
    TRAPD(error, {
      CPbkContactItem* contactItem = 
        self->contactEngine->ReadContactL(uniqueId); 
      contact->contactItem = contactItem;
      contact->mode = CONTACT_READ_ONLY;
    });
    if(error != KErrNone){  
      PyObject_Del(contact);
      return SPyErr_SetFromSymbianOSErr(error);
    } 
  }

  contact->contactsDb = self;
  contact->uniqueID = uniqueId;
  Py_INCREF(contact->contactsDb);
  return (PyObject*)contact;
}


/*
 * Test if the contact entry is a contact group.
 */
extern "C" PyObject *
Contact_is_contact_group(Contact_object* self, PyObject* /*args*/)
{ 
  TBool isGroup = EFalse; 
  if(EFalse!=IsContactGroup(self->contactItem->ContactItem())){
    isGroup = ETrue;
  }
  return Py_BuildValue("i", isGroup);
}


/*
 * Deallocate Contact_object.
 */
extern "C" {
  static void Contact_dealloc(Contact_object *contact)
  {
    Contact_delete_entry(contact);
    Py_DECREF(contact->contactsDb); 
    PyObject_Del(contact);    
  }
}


/*
 * Sets value and/or label to given field.
 */
extern "C" PyObject *
Contact_modify_field_value_or_labelL(TPbkContactItemField & theField,
                                     char* value,
                                     TInt valueLength,
                                     PyObject* label,
                                     TReal timeValue)
{    
  if(value && theField.StorageType() == KStorageTypeText){
    // Text type.
    TPtrC valuePtr((TUint16*) value, valueLength);  
    theField.TextStorage()->SetTextL(valuePtr);
  }else if((timeValue>0) && theField.StorageType() == KStorageTypeDateTime){
    // DateTime type.
    TTime time;
    pythonRealAsTTime(timeValue, time);     
    theField.DateTimeStorage()->SetTime(time.DateTime());
  }else if(value || (timeValue>0)){
    PyErr_SetString(PyExc_ValueError, "illegal field value and type combination");
    return NULL; 
  }
  
  if(label){
    TPtrC labelPtr((TUint16*) PyUnicode_AsUnicode(label), 
                              PyUnicode_GetSize(label)); 
    theField.SetLabelL(labelPtr);
  }
  Py_INCREF(Py_None);
  return Py_None;
}


/*
 * Modifies fields value and/or label.
 */
extern "C" PyObject *
Contact_modify_field(Contact_object* self, PyObject *args, PyObject *keywds)
{
  ASSERT_CONTACT_READ_WRITE

  TInt fieldIndex;
  PyObject* valueObj = NULL;
  char* value = NULL; 
  TInt valueLength = 0;
  PyObject* label = NULL;
  PyObject* ret = NULL;
  TReal dateVal = 0;
 
  static const char *const kwlist[] = 
    {"fieldindex", "value", "label", NULL};
 
  if (!PyArg_ParseTupleAndKeywords(args, keywds, "i|OU", (char**)kwlist,
                                   &fieldIndex, &valueObj,
                                   &label)){ 
    return NULL;
  }

  if(!valueObj && !label){
    PyErr_SetString(PyExc_SyntaxError, "value and/or label must be given");
    return NULL;
  }

  if(valueObj){
    if(PyFloat_Check(valueObj)){
      // DateTime type.
      dateVal = PyFloat_AsDouble(valueObj);
    } else if(PyUnicode_Check(valueObj)){
      // Text type.
      value = (char*)PyUnicode_AsUnicode(valueObj);
      valueLength = PyUnicode_GetSize(valueObj);
    } else {
      PyErr_SetString(PyExc_TypeError, "illegal value parameter");
      return NULL;
    }
  }

  CPbkFieldArray& fieldArray = self->contactItem->CardFields();

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

  TPbkContactItemField& theField = fieldArray[fieldIndex];

  TRAPD(error, {    
    ret = Contact_modify_field_value_or_labelL(theField,
                                               value,
                                               valueLength,
                                               label,
                                               dateVal);   
  });
  
  if(error != KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);
  }
  return ret;
}


/*
 * Adds field to the contact.
 */
extern "C" PyObject *
Contact_add_field(Contact_object* self, PyObject *args, PyObject *keywds)
{
  ASSERT_CONTACT_READ_WRITE
  
  PyObject* fieldTypeObj = NULL;
  PyObject* valueObj = NULL;
  char* value = NULL; 
  TInt valueLength = 0;
  PyObject* label = NULL;
  PyObject* ret = NULL;
  CPbkFieldInfo* fieldInfo = NULL;
  TReal dateVal = 0;
  TInt error = KErrNone;
  TPbkContactItemField* theField = NULL;
 
  static const char *const kwlist[] = 
    {"fieldtype", "value", "label", NULL};
 
  if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|OU", (char**)kwlist,
                                   &fieldTypeObj, 
                                   &valueObj, 
                                   &label)){ 
    return NULL;
  }

  if(valueObj){
    if(PyFloat_Check(valueObj)){
      // DateTime type.
      dateVal = PyFloat_AsDouble(valueObj);
    } else if(PyUnicode_Check(valueObj)){
      // Text type.
      value = (char*)PyUnicode_AsUnicode(valueObj);
      valueLength = PyUnicode_GetSize(valueObj);
    } else {
      PyErr_SetString(PyExc_TypeError, "illegal value parameter");
      return NULL;
    }
  }

⌨️ 快捷键说明

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