Example #1
0
void ContactList::ContactPhoneNumberFound(PhoneNumberType phoneType, std::string phoneNumber)
{
	//retrieve contact added last
	Contact *contact = m_contacts.back();
	contact->AddPhoneNumber(phoneType, phoneNumber);
}
//TODO: Is it possible to set additional information?
v8::Handle<v8::Value> Contacts::add(const v8::Arguments& args) {
    AppLog("Entered Contacts::addContact (args length:%d)", args.Length());

//    if (args.Length() < 1 || Util::isArgumentNull(args[0])) {
    if (args.Length() < 1) {
        AppLog("Bad parameters");
        return v8::ThrowException(v8::String::New("Bad parameters"));
    }
    v8::HandleScope scope;

    String category;
	String name;
	String number;

	v8::Local<v8::Object> obj = args[0]->ToObject();
    if(args[0]->IsObject())
    {
		v8::Local<v8::Value> obj_category = obj->Get(v8::String::New("category"));
		if(obj_category->IsString())
		{
			category = UNWRAP_STRING(obj_category).c_str();
			AppLogTag("Contacts","Add Contacts: [Category:%ls]", category.GetPointer());
		}

		v8::Local<v8::Value> obj_name = obj->Get(v8::String::New("name"));
		if(obj_name->IsString())
		{
			name = UNWRAP_STRING(obj_name).c_str();
			AppLogTag("Contacts","Add Contacts: [Name:%ls]", name.GetPointer());
		}
	}

    if(category == null || name == null)
    {
    	AppLogTag("Contacts","Failed to add Contact");
    	return scope.Close(v8::Boolean::New(false));
    }

    //CREATE CONTACT
    Contact contact;
    contact.SetValue(CONTACT_PROPERTY_ID_FIRST_NAME, name);

    v8::Local<v8::Value> obj_number = obj->Get(v8::String::New("number"));

	if(!obj_number->IsNull() && obj_number->IsString())
	{
		number = UNWRAP_STRING(obj_number).c_str();
		AppLogTag("Contacts","Add Contacts: [Number:%ls]", number.GetPointer());
		PhoneNumber phoneNumber;
		phoneNumber.SetPhoneNumber(number);
		contact.AddPhoneNumber(phoneNumber);
	}

	AddressbookManager* pAddressbookManager = AddressbookManager::GetInstance();
	Addressbook* pAddressbook = pAddressbookManager->GetAddressbookN(DEFAULT_ADDRESSBOOK_ID);

    //GET CATEGORIES TO ADD A CONTACT
	IList* pCategoryList = pAddressbook->GetAllCategoriesN();

	result r = GetLastResult();
	if (IsFailed(r)) {
		AppLogTag("Contacts", "Failed to get categories: %s", GetErrorMessage(r));
		return scope.Close(v8::Boolean::New(false));
	}

	if (pCategoryList != null && pCategoryList->GetCount() > 0) {
		IEnumerator* pCategoryEnum = pCategoryList->GetEnumeratorN();
		Category* pCategory = null;

		while (pCategoryEnum->MoveNext() == E_SUCCESS) {
			pCategory = static_cast<Category*>(pCategoryEnum->GetCurrent());
			if (pCategory->GetName().Equals(category)) {

				pAddressbook->AddContact(contact);
				pAddressbook->AddMemberToCategory(pCategory->GetRecordId(), contact.GetRecordId());

				if (IsFailed(GetLastResult())) {
					return scope.Close(v8::Boolean::New(false));
				} else {
					AppLogTag("Contacts", "%d", contact.GetRecordId());
					return scope.Close(v8::Boolean::New(true));
				}
			}
		}
	}

	AppLogTag("Contacts","No Categories");
	return scope.Close(v8::Boolean::New(false));
}