Skip to content
This repository has been archived by the owner on Apr 18, 2019. It is now read-only.

Commit

Permalink
adding contact support names/phonenumbers/emails
Browse files Browse the repository at this point in the history
  • Loading branch information
imhotep committed Apr 2, 2011
1 parent c6e4fdf commit 9fefe49
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 71 deletions.
50 changes: 31 additions & 19 deletions Res/main.js
Expand Up @@ -173,25 +173,37 @@ function createContact() {
console.log("The contact, "+myContact.displayName + ", is of the "+myContact.gender +" gender");
}

function saveContact(contacts) {
var onSuccess = function() {
debugPrint("Save Success "+contacts);
};
var onError = function(contactError) {
debugPrint("Error = "+contactError.code);
};
var contact = navigator.service.contacts.create();
contact.displayName = "John";
contact.nickname = "Plumber";
contact.phoneNumbers = ["6047894567"]
contact.emails = ["nomail@noset.com"]
contact.urls = ["http://www.domain.com"]

var name = new ContactName();
name.givenName = "Jane";
name.familtyName = "Doe";
contact.name = name;
contact.save(onSuccess, onError);
function saveContact() {
try {
var onSuccess = function(result) {
debugPrint("Save Success: "+result.message);
};
var onError = function(contactError) {
debugPrint("Error = "+contactError.code);
};
var contact = navigator.service.contacts.create();
contact.name = new ContactName();
contact.name.familyName = "Doe";
contact.name.givenName = "John";
contact.displayName = "John Doe";
contact.nickname = "Plumber";
contact.phoneNumbers = [new ContactField("Mobile", "6047894567"), new ContactField("Home", "7789989674"), new ContactField("Other", "7789989673")];
contact.emails = [new ContactField("Personal", "nomail@noset.com"), new ContactField("Work", "nomail2@noset.com"), new ContactField("Other", "nomail3@noset.com")];
contact.urls = [new ContactField("work", "http://www.domain.com"), new ContactField("home", "http://www.domain2.com")];
contact.organization = new ContactOrganization();
contact.organization.name = "Nitobi Software Inc";
contact.organization.title = "Software Engineer";
contact.birthday = new Date();
contact.address = new ContactAddress();
contact.address.streetAddress = "200 Abbott Street";
contact.address.locality = "Vancouver";
contact.address.region = "BC";
contact.address.postalCode = "V6Z2X6";
contact.address.country = "Canada";
contact.save(onSuccess, onError);
} catch(e) {
debugPrint("Error Occured: "+e.message);
}
}

// Compass
Expand Down
5 changes: 3 additions & 2 deletions Res/phonegap/contact.js
Expand Up @@ -25,6 +25,7 @@
* @param {ContactField[]} urls contact's web sites
* @param {DOMString} timezone UTC time zone offset
*/

var Contact = function(id, displayName, name, nickname, phoneNumbers, emails, addresses,
ims, organizations, revision, birthday, gender, note, photos, categories, urls, timezone) {
this.id = id || null;
Expand Down Expand Up @@ -173,8 +174,8 @@ Contact.prototype.clone = function() {
*/
Contact.prototype.save = function(successCB, errorCB) {
// don't modify the original contact
var bada_contact = {email: this.emails[0], phone: this.phoneNumbers[0], email: this.emails[0], url: this.urls[0]};
PhoneGap.exec(successCB, errorCB, "com.phonegap.Contacts", "save", [bada_contact]);
var id = navigator.service.contacts.records.push(this) - 1;
PhoneGap.exec(successCB, errorCB, "com.phonegap.Contacts", "save", [id]);
};

/**
Expand Down
22 changes: 15 additions & 7 deletions inc/Contacts.h
Expand Up @@ -8,24 +8,32 @@
#ifndef CONTACTS_H_
#define CONTACTS_H_

#include <FApp.h>
#include <FSocial.h>
#include "PhoneGapCommand.h"
using namespace Osp::Social;
using namespace Osp::Base::Collection;
using namespace Osp::App;

class Contacts: public PhoneGapCommand, IAppControlEventListener {
class Contacts: public PhoneGapCommand {
public:
Contacts(Web* pWeb);
virtual ~Contacts();
public:
void Run(const String& command);
void Create(const String& query);
void Create(const int contactId);
void Find();
public:
virtual void OnAppControlCompleted (const Osp::Base::String &appControlId, const Osp::Base::String &operationId, const Osp::Base::Collection::IList *pResultList);
private:
ArrayList* GetDataList(const String& query);
String callbackId;
private:
void SetNickname(Contact& contact, const int cid);
void SetFirstName(Contact& contact, const int cid);
void SetLastName(Contact& contact, const int cid);
void SetPhoneNumbers(Contact& contact, const int cid);
void SetEmails(Contact& contact, const int cid);
void SetUrls(Contact& contact, const int cid);
void SetOrganization(Contact& contact, const int cid);
void SetBirthday(Contact& contact, const int cid);
void SetAddress(Contact& contact, const int cid);

};

#endif /* CONTACTS_H_ */
235 changes: 192 additions & 43 deletions src/Contacts.cpp
Expand Up @@ -8,77 +8,226 @@
#include "../inc/Contacts.h"

Contacts::Contacts(Web* pWeb) : PhoneGapCommand(pWeb) {
// TODO Auto-generated constructor stub

}

Contacts::~Contacts() {
// TODO Auto-generated destructor stub
}

void
Contacts::Run(const String& command) {
if(!command.IsEmpty()) {
// URL decoding
Uri commandUri;
commandUri.SetUri(command);
String method = commandUri.GetHost();
callbackId = commandUri.GetPath();
callbackId.Replace(L"/", L"");
String contactInfo = commandUri.GetQuery();

// AppLogDebug("Method %S, callbackId %S, hostAddr %S URI %S", method.GetPointer(), callbackId.GetPointer(), hostAddr.GetPointer(), uri.ToString().GetPointer());
AppLogDebug("Method %S callbackId %S query %S", method.GetPointer(), callbackId.GetPointer() , contactInfo.GetPointer());
StringTokenizer strTok(commandUri.GetPath(), L"/");
if(strTok.GetTokenCount() < 2) {
AppLogException("Not enough params");
return;
}
strTok.GetNextToken(callbackId);
String contactId;
strTok.GetNextToken(contactId);
int cid = -1;
result r = E_SUCCESS;
r = Integer::Parse(contactId, cid);
if(IsFailed(r)) {
AppLogException("Could not retrieve contact ID");
}
AppLogDebug("Method %S callbackId %S contactId %d", method.GetPointer(), callbackId.GetPointer(), cid);
if(method == L"com.phonegap.Contacts.save" && !callbackId.IsEmpty()) {
Create(contactInfo);
AppLogDebug("Saving contact...");
Create(cid);
}

}
}

ArrayList*
Contacts::GetDataList(const String& query) {
ArrayList* pDataList = new ArrayList();
pDataList->Construct();
String* pStorage = new String(L"storageType:phone");
pDataList->Add(*pStorage);

String delim(L"&");
StringTokenizer strTok(query, delim);

while(strTok.HasMoreTokens()) {
String* pData = new String();
strTok.GetNextToken(*pData);
pData->Replace(L"=", L":");
AppLogDebug("param %S", pData->GetPointer());
pDataList->Add(*pData);
void
Contacts::SetNickname(Contact& contact, const int cid) {
String* value = NULL;
String eval;
eval.Format(64, L"navigator.service.contacts.records[%d].nickname", cid);
value = pWeb->EvaluateJavascriptN(eval);
if(!value->IsEmpty()) {
AppLogDebug("nickname: %S", value->GetPointer());
contact.SetValue(CONTACT_PROPERTY_ID_NICK_NAME, *value);
}
delete value;
}

return pDataList;
void
Contacts::SetFirstName(Contact& contact, const int cid) {
String* value = NULL;
String eval;
eval.Format(64, L"navigator.service.contacts.records[%d].name.givenName", cid);
value = pWeb->EvaluateJavascriptN(eval);
if(!value->IsEmpty()) {
AppLogDebug("First Name: %S", value->GetPointer());
contact.SetValue(CONTACT_PROPERTY_ID_FIRST_NAME, *value);
}
delete value;
}

void
Contacts::SetLastName(Contact& contact, const int cid) {
String* value = NULL;
String eval;
eval.Format(64, L"navigator.service.contacts.records[%d].name.familyName", cid);
value = pWeb->EvaluateJavascriptN(eval);
if(value->IsEmpty()) {
AppLogDebug("Last Name: %S", value->GetPointer());
contact.SetValue(CONTACT_PROPERTY_ID_LAST_NAME, *value);
}
delete value;
}

void
Contacts::OnAppControlCompleted(const Osp::Base::String &appControlId,
const Osp::Base::String &operationId,
const Osp::Base::Collection::IList *pResultList) {
AppLogDebug("appControlId %S operationId %S", appControlId.GetPointer(), operationId.GetPointer());
for(int i = 0 ; i < pResultList->GetCount() ; i++) {
String* obj = (String*)pResultList->GetAt(i);
AppLogDebug("OBJ %S", obj->GetPointer());
Contacts::SetPhoneNumbers(Contact& contact, const int cid) {
// Getting phone numbers length
String* lengthStr = NULL;
String eval;
eval.Format(64, L"navigator.service.contacts.records[%d].phoneNumbers.length", cid);
lengthStr = pWeb->EvaluateJavascriptN(eval);
if(!lengthStr->IsEmpty()) {
int length;
result r = Integer::Parse(*lengthStr, length);
if(IsFailed(r)) {
AppLogException("Could not get phoneNumbers length");
return;
}
delete lengthStr;
for(int i = 0 ; i < length ; i++) {
String* type = NULL;
String* number = NULL;

// Getting phone number type
eval.Clear();
eval.Format(64, L"navigator.service.contacts.records[%d].phoneNumbers[%d].type", cid, i);
type = pWeb->EvaluateJavascriptN(eval);

// Getting phone number
eval.Clear();
eval.Format(64, L"navigator.service.contacts.records[%d].phoneNumbers[%d].value", cid, i);
number = pWeb->EvaluateJavascriptN(eval);

if(type != NULL && number != NULL) {
if(*type == PHONENUMBER_TYPE_HOME) {
PhoneNumber phoneNumber(PHONENUMBER_TYPE_HOME, *number);
contact.AddPhoneNumber(phoneNumber);
} else if(*type == PHONENUMBER_TYPE_MOBILE) {
PhoneNumber phoneNumber(PHONENUMBER_TYPE_MOBILE, *number);
contact.AddPhoneNumber(phoneNumber);
} else if(*type == PHONENUMBER_TYPE_PAGER) {
PhoneNumber phoneNumber(PHONENUMBER_TYPE_PAGER, *number);
contact.AddPhoneNumber(phoneNumber);
} else if(*type == PHONENUMBER_TYPE_WORK) {
PhoneNumber phoneNumber(PHONENUMBER_TYPE_WORK, *number);
contact.AddPhoneNumber(phoneNumber);
} else if(*type == PHONENUMBER_TYPE_OTHER) {
PhoneNumber phoneNumber(PHONENUMBER_TYPE_OTHER, *number);
contact.AddPhoneNumber(phoneNumber);
}
}
delete type;
delete number;
}
}
}

void
Contacts::Create(const String& query) {
ArrayList* pDataList = GetDataList(query);
AppControl* pAc = AppManager::FindAppControlN(APPCONTROL_CONTACT,OPERATION_ADD);
if(pAc)
{
pAc->Start(pDataList, this);
delete pAc;
Contacts::SetEmails(Contact& contact, const int cid) {
// Getting phone numbers length
String* lengthStr = NULL;
String eval;
eval.Format(64, L"navigator.service.contacts.records[%d].emails.length", cid);
lengthStr = pWeb->EvaluateJavascriptN(eval);
if(!lengthStr->IsEmpty()) {
int length;
result r = Integer::Parse(*lengthStr, length);
if(IsFailed(r)) {
AppLogException("Could not get emails length");
return;
}
delete lengthStr;
for(int i = 0 ; i < length ; i++) {
String* type = NULL;
String* address = NULL;

// Getting phone number type
eval.Clear();
eval.Format(64, L"navigator.service.contacts.records[%d].emails[%d].type", cid, i);
type = pWeb->EvaluateJavascriptN(eval);

// Getting phone number
eval.Clear();
eval.Format(64, L"navigator.service.contacts.records[%d].emails[%d].value", cid, i);
address = pWeb->EvaluateJavascriptN(eval);

if(type != NULL && address != NULL) {
if(*type == EMAIL_TYPE_PERSONAL) {
Email email(EMAIL_TYPE_PERSONAL, *address);
contact.AddEmail(email);
} else if(*type == EMAIL_TYPE_WORK) {
Email email(EMAIL_TYPE_WORK, *address);
contact.AddEmail(email);
} else if(*type == EMAIL_TYPE_OTHER) {
Email email(EMAIL_TYPE_OTHER, *address);
contact.AddEmail(email);
}
}
delete type;
delete address;
}
}
}

void
Contacts::SetUrls(Contact& contact, const int cid) {
}

void
Contacts::SetOrganization(Contact& contact, const int cid) {

}
void
Contacts::SetBirthday(Contact& contact, const int cid) {
}

void
Contacts::SetAddress(Contact& contact, const int cid) {
}

void
Contacts::Create(const int cid) {
result r = E_SUCCESS;
Addressbook addressbook;

r = addressbook.Construct();

if(IsFailed(r)) {
AppLogException("Could not create AddressBook");
return;
}

Contact contact;
SetNickname(contact, cid);
SetFirstName(contact, cid);
SetLastName(contact, cid);
SetPhoneNumbers(contact, cid);
SetEmails(contact, cid);
SetUrls(contact, cid);
SetOrganization(contact, cid);
SetBirthday(contact, cid);
SetAddress(contact, cid);

r = addressbook.AddContact(contact);

if(IsFailed(r)) {
AppLogException("Could not add contact");
} else {
AppLogDebug("Contact Successfully Added");
}
pDataList->RemoveAll(true);
delete pDataList;
}

void
Expand Down

0 comments on commit 9fefe49

Please sign in to comment.