a few fixes on contacts
diff --git a/Res/index.html b/Res/index.html
index c394ea8..dfe0461 100755
--- a/Res/index.html
+++ b/Res/index.html
@@ -67,6 +67,7 @@
<input onclick="createContact()" type="submit" value="Contact.create">
<input onclick="saveContact()" type="submit" value="Contact.save">
<input onclick="findContact()" type="submit" value="Contact.find">
+ <input onclick="removeContacts()" type="submit" value="Contact.remove">
</div>
</body>
</html>
diff --git a/Res/main.js b/Res/main.js
index 7ce093b..7f1142d 100755
--- a/Res/main.js
+++ b/Res/main.js
@@ -189,7 +189,7 @@
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.urls = [new ContactField("Work", "http://www.domain.com"), new ContactField("Personal", "http://www.domain2.com")];
contact.organization = new ContactOrganization();
contact.organization.name = "Nitobi Software Inc";
contact.organization.title = "Software Engineer";
@@ -214,12 +214,28 @@
var onFailure = function() {
debugPrint("ERROR");
};
- navigator.service.contacts.find(["displayName", "firstName"], onSuccess, onFailure, {filter:"John"});
+ navigator.service.contacts.find(["displayName", "firstName"], onSuccess, onFailure, {filter:"7789989674"});
} catch(e) {
debugPrint("Error Occured: "+e.message);
}
}
+function removeContacts() {
+ try {
+ var onSuccess = function(result) {
+ debugPrint(result.message);
+ };
+ var onFailure = function(result) {
+ debugPrint("ERROR in Removing Contact: "+result.message);
+ };
+ for(c in navigator.service.contacts.results) {
+ c.remove(onSuccess, onFailure);
+ }
+ } catch(e) {
+ debugPrint("Error Occured in remove Contact: "+e.message);
+ }
+}
+
// Compass
var watchCompassId = null;
diff --git a/Res/phonegap/contact.js b/Res/phonegap/contact.js
index 15e9ab6..216b0fd 100644
--- a/Res/phonegap/contact.js
+++ b/Res/phonegap/contact.js
@@ -54,13 +54,13 @@
*/
Contact.prototype.remove = function(successCB, errorCB) {
if (this.id == null) {
- var errorObj = new ContactError();
- errorObj.code = ContactError.NOT_FOUND_ERROR;
- errorCB(errorObj);
- }
- else {
- PhoneGap.exec(successCB, errorCB, "com.phonegap.Contacts", "remove", [{"contact": this}]);
- }
+ var errorObj = new ContactError();
+ errorObj.code = ContactError.NOT_FOUND_ERROR;
+ errorCB(errorObj);
+ }
+ else {
+ PhoneGap.exec(successCB, errorCB, "com.phonegap.Contacts", "remove", [this.id]);
+ }
};
/**
* Bada ONLY
@@ -70,13 +70,13 @@
*/
Contact.prototype.display = function(successCB, errorCB, options) {
if (this.id == null) {
- var errorObj = new ContactError();
- errorObj.code = ContactError.NOT_FOUND_ERROR;
- errorCB(errorObj);
- }
- else {
- PhoneGap.exec(successCB, errorCB, "com.phonegap.Contacts","displayContact", [this.id, options]);
- }
+ var errorObj = new ContactError();
+ errorObj.code = ContactError.NOT_FOUND_ERROR;
+ errorCB(errorObj);
+ }
+ else {
+ PhoneGap.exec(successCB, errorCB, "com.phonegap.Contacts","displayContact", [this.id, options]);
+ }
};
/**
@@ -255,6 +255,7 @@
if(contact) {
try {
this.results.push(this.create(contact));
+ console.log("contact added");
} catch(e){
console.log("Error parsing contact");
}
diff --git a/inc/Contacts.h b/inc/Contacts.h
index 2df03ff..90070ee 100755
--- a/inc/Contacts.h
+++ b/inc/Contacts.h
@@ -21,9 +21,11 @@
void Run(const String& command);
void Create(const int contactId);
void Find(const String& filter);
+ void Remove(const String& id);
private:
String callbackId;
private:
+ void SetUserId(Contact& contact);
void SetNickname(Contact& contact, const int cid);
void SetFirstName(Contact& contact, const int cid);
void SetLastName(Contact& contact, const int cid);
diff --git a/src/Contacts.cpp b/src/Contacts.cpp
index 756d2fe..e63929f 100755
--- a/src/Contacts.cpp
+++ b/src/Contacts.cpp
@@ -49,6 +49,15 @@
}
void
+Contacts::SetUserId(Contact& contact) {
+ // FIXME: generate random number
+ int value = 14;
+ Integer userId(value);
+ AppLogDebug("Setting User ID %S", userId.ToString().GetPointer());
+ contact.SetValue(CONTACT_PROPERTY_ID_USER_ID, userId.ToString());
+}
+
+void
Contacts::SetNickname(Contact& contact, const int cid) {
String* value = NULL;
String eval;
@@ -80,7 +89,7 @@
String eval;
eval.Format(128, L"navigator.service.contacts.records[%d].name.familyName", cid);
value = pWeb->EvaluateJavascriptN(eval);
- if(value->IsEmpty()) {
+ if(!value->IsEmpty()) {
AppLogDebug("Last Name: %S", value->GetPointer());
contact.SetValue(CONTACT_PROPERTY_ID_LAST_NAME, *value);
}
@@ -117,19 +126,24 @@
number = pWeb->EvaluateJavascriptN(eval);
if(!type->IsEmpty() && !number->IsEmpty()) {
- if(*type == PHONENUMBER_TYPE_HOME) {
+ if(*type == "Home") {
+ AppLogDebug("Adding HOME phone number %S", number->GetPointer());
PhoneNumber phoneNumber(PHONENUMBER_TYPE_HOME, *number);
contact.AddPhoneNumber(phoneNumber);
- } else if(*type == PHONENUMBER_TYPE_MOBILE) {
+ } else if(*type == "Mobile") {
+ AppLogDebug("Adding MOBILE phone number %S", number->GetPointer());
PhoneNumber phoneNumber(PHONENUMBER_TYPE_MOBILE, *number);
contact.AddPhoneNumber(phoneNumber);
- } else if(*type == PHONENUMBER_TYPE_PAGER) {
+ } else if(*type == "Pager") {
+ AppLogDebug("Adding PAGER phone number %S", number->GetPointer());
PhoneNumber phoneNumber(PHONENUMBER_TYPE_PAGER, *number);
contact.AddPhoneNumber(phoneNumber);
- } else if(*type == PHONENUMBER_TYPE_WORK) {
+ } else if(*type == "Work") {
+ AppLogDebug("Adding WORK phone number %S", number->GetPointer());
PhoneNumber phoneNumber(PHONENUMBER_TYPE_WORK, *number);
contact.AddPhoneNumber(phoneNumber);
- } else if(*type == PHONENUMBER_TYPE_OTHER) {
+ } else if(*type == "Other") {
+ AppLogDebug("Adding OTHER phone number %S", number->GetPointer());
PhoneNumber phoneNumber(PHONENUMBER_TYPE_OTHER, *number);
contact.AddPhoneNumber(phoneNumber);
}
@@ -170,13 +184,16 @@
address = pWeb->EvaluateJavascriptN(eval);
if(!type->IsEmpty() && !address->IsEmpty()) {
- if(*type == EMAIL_TYPE_PERSONAL) {
+ if(*type == "Personal") {
+ AppLogDebug("Adding PERSONAL email %S", address->GetPointer());
Email email(EMAIL_TYPE_PERSONAL, *address);
contact.AddEmail(email);
- } else if(*type == EMAIL_TYPE_WORK) {
+ } else if(*type == "Work") {
+ AppLogDebug("Adding WORK email %S", address->GetPointer());
Email email(EMAIL_TYPE_WORK, *address);
contact.AddEmail(email);
- } else if(*type == EMAIL_TYPE_OTHER) {
+ } else if(*type == "Other") {
+ AppLogDebug("Adding OTHER email %S", address->GetPointer());
Email email(EMAIL_TYPE_OTHER, *address);
contact.AddEmail(email);
}
@@ -217,13 +234,16 @@
address = pWeb->EvaluateJavascriptN(eval);
if(!type->IsEmpty() && !address->IsEmpty()) {
- if(*type == URL_TYPE_PERSONAL) {
+ if(*type == "Personal") {
+ AppLogDebug("Adding PERSONAL URL %S", address->GetPointer());
Url url(URL_TYPE_PERSONAL, *address);
contact.AddUrl(url);
- } else if(*type == URL_TYPE_WORK) {
+ } else if(*type == "Work") {
+ AppLogDebug("Adding WORK URL %S", address->GetPointer());
Url url(URL_TYPE_WORK, *address);
contact.AddUrl(url);
- } else if(*type == URL_TYPE_OTHER) {
+ } else if(*type == "Other") {
+ AppLogDebug("Adding OTHER URL %S", address->GetPointer());
Url url(URL_TYPE_OTHER, *address);
contact.AddUrl(url);
}
@@ -377,6 +397,7 @@
}
Contact contact;
+ SetUserId(contact);
SetNickname(contact, cid);
SetFirstName(contact, cid);
SetLastName(contact, cid);
@@ -407,13 +428,17 @@
Contacts::UpdateSearch(Contact* pContact) const {
// TODO: update this add other fields too (emails, urls, phonenumbers, etc...)
String eval, displayName, firstName, lastName;
+ UserId userId;
pContact->GetValue(CONTACT_PROPERTY_ID_DISPLAY_NAME, displayName);
pContact->GetValue(CONTACT_PROPERTY_ID_FIRST_NAME, firstName);
pContact->GetValue(CONTACT_PROPERTY_ID_LAST_NAME, lastName);
- eval.Format(256, L"navigator.service.contacts._findCallback({displayName:'%S', name:{firstName:'%S',lastName:'%S'}})",
+ pContact->GetValue(CONTACT_PROPERTY_ID_USER_ID, userId);
+ eval.Format(256, L"navigator.service.contacts._findCallback({id:'%S', displayName:'%S', name:{firstName:'%S',lastName:'%S'}})",
+ userId.GetPointer(),
displayName.GetPointer(),
firstName.GetPointer(),
lastName.GetPointer());
+ AppLogDebug("%S user ID: %S", eval.GetPointer(), userId.GetPointer());
pWeb->EvaluateJavascriptN(eval);
}
@@ -433,6 +458,7 @@
// Searching by Email
pContactList = addressbook.SearchContactsByNameN(filter);
+ AppLogDebug("Names Matched %d", pContactList->GetCount());
pContactEnum = pContactList->GetEnumeratorN();
while (E_SUCCESS == pContactEnum->MoveNext())
{
@@ -459,6 +485,7 @@
// Searching by Email
pContactList = addressbook.SearchContactsByEmailN(filter);
+ AppLogDebug("Emails Matched %d", pContactList->GetCount());
pContactEnum = pContactList->GetEnumeratorN();
while (E_SUCCESS == pContactEnum->MoveNext())
{
@@ -484,6 +511,7 @@
}
// Searching by Email
pContactList = addressbook.SearchContactsByPhoneNumberN(filter);
+ AppLogDebug("Phone Number Matched %d", pContactList->GetCount());
pContactEnum = pContactList->GetEnumeratorN();
while (E_SUCCESS == pContactEnum->MoveNext())
{
@@ -525,3 +553,29 @@
pWeb->EvaluateJavascriptN(eval);
}
}
+
+void
+Contacts::Remove(const String& idStr) {
+ String eval;
+ Addressbook addressbook;
+ long long id;
+ result r = addressbook.Construct();
+ if(IsFailed(r))
+ {
+ AppLogException("Could not construct Address Book");
+ return;
+ }
+ r = LongLong::Parse(idStr, id);
+ if(IsFailed(r)) {
+ AppLogException("Could not parse ID");
+ } else {
+ r = addressbook.RemoveContact(id);
+ if(IsFailed(r)) {
+ eval.Format(128, L"PhoneGap.callbacks['%S'].fail({message:'%s', code:%d})", callbackId.GetPointer(), GetErrorMessage(r), r);
+ pWeb->EvaluateJavascriptN(eval);
+ } else {
+ eval.Format(128, L"PhoneGap.callbacks['%S'].success({message:'Contact with ID %d removed', code:01})", callbackId.GetPointer(), id);
+ pWeb->EvaluateJavascriptN(eval);
+ }
+ }
+}