[Tcladdressbook-commits] Source TclAddressBookUtils.c,1.17,1.18
Status: Alpha
Brought to you by:
bdesgraupes
|
From: Bernard D. <bde...@us...> - 2004-07-30 21:29:26
|
Update of /cvsroot/tcladdressbook/Source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31277/Source Modified Files: TclAddressBookUtils.c Log Message: [identifier index] command Index: TclAddressBookUtils.c =================================================================== RCS file: /cvsroot/tcladdressbook/Source/TclAddressBookUtils.c,v retrieving revision 1.17 retrieving revision 1.18 diff -u -d -r1.17 -r1.18 --- TclAddressBookUtils.c 30 Jul 2004 13:05:58 -0000 1.17 +++ TclAddressBookUtils.c 30 Jul 2004 21:29:17 -0000 1.18 @@ -1,7 +1,7 @@ /* * File : "TclAddressBookUtils.c" * Created: 2003-12-05 10:08:03 - * Last modification: 2004-07-30 10:51:55 + * Last modification: 2004-07-30 23:12:50 * Author: Bernard Desgraupes * e-mail: <bde...@ea...> * @@ -1560,6 +1560,109 @@ /* *---------------------------------------------------------------------- * + * TclAB_IdentifierIndex -- + * + * This function is invoked to process the [addressbook identifier index] + * Tcl command. + * See the user documentation for details on what it does. + * + * Syntax: + * addressbook identifier index personID propName identifier + * + * Results: + * A standard Tcl result. + * + * Side effects: + * See the user documentation. + * + *---------------------------------------------------------------------- + */ + +int +TclAB_IdentifierIndex( + Tcl_Interp *interp, /* Current interpreter. */ + int objc, /* Number of arguments. */ + Tcl_Obj *CONST objv[], /* Argument values. */ + Tcl_Obj *resultPtr) /* Pointer to store the result. */ +{ + ABAddressBookRef ab; + CONST84 char * theProperty, * theUID, * theIdentifier; + CFStringRef propertyRef, identRef, multivalueRef; + ABRecordRef personRecord; + int index, length, result = TCL_OK; + + if (objc != 6) { + Tcl_WrongNumArgs(interp, 2, objv, "personID propName identifier"); + return TCL_ERROR; + } + + Tcl_ResetResult(interp); + + // Get the address book + ab = ABGetSharedAddressBook(); + + // Check if third argument is a person and get the corresponding record + theUID = Tcl_GetStringFromObj(objv[3], &length); + if ( !TclAB_CheckItem(interp, ab, theUID, "third", &personRecord, rec_person) ) { + return TCL_ERROR; + } + + // Make CFString object for property + Tcl_IncrRefCount(objv[4]); + theProperty = Tcl_GetStringFromObj(objv[4], &length); + propertyRef = CFStringCreateWithCString(NULL, theProperty, kCFStringEncodingUTF8); + if (!propertyRef) { + Tcl_AppendStringsToObj(resultPtr, "Problem getting CFString for property", (char *) NULL); + result = TCL_ERROR; + goto DONE; + } + + // Check the property's type + if ( ! TclAB_CheckMultiString(ab, propertyRef, rec_person) ) { + Tcl_AppendStringsToObj(resultPtr, "Not a multi-string list property", (char *) NULL); + CFRelease(propertyRef); + result = TCL_ERROR; + goto DONE; + } + + Tcl_IncrRefCount(objv[5]); + theIdentifier = Tcl_GetStringFromObj(objv[5], &length); + identRef = CFStringCreateWithCString(NULL, theIdentifier, kCFStringEncodingUTF8); + if (!identRef) { + Tcl_AppendStringsToObj(resultPtr, "Problem getting CFString for identifier", (char *) NULL); + CFRelease(propertyRef); + result = TCL_ERROR; + goto DONE; + } + + multivalueRef = ABRecordCopyValue(personRecord, propertyRef); + if (!multivalueRef) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get value for '", theProperty, "' property", (char *) NULL); + result = TCL_ERROR; + } else { + index = ABMultiValueIndexForIdentifier((ABMutableMultiValueRef) multivalueRef, identRef); + if (index == 0x7fffffff) { + Tcl_AppendStringsToObj(resultPtr, "Bad identifier ", theIdentifier, (char *) NULL); + result = TCL_ERROR; + } else { + Tcl_SetObjResult(interp, Tcl_NewIntObj(index)); + } + CFRelease(multivalueRef); + } + CFRelease(identRef); + CFRelease(propertyRef); + +DONE: + Tcl_DecrRefCount(objv[4]); + Tcl_DecrRefCount(objv[5]); + CFRelease(personRecord); + return result; +} + + +/* + *---------------------------------------------------------------------- + * * TclAB_IdentifierPrimary -- * * This function is invoked to process the [addressbook identifier primary] @@ -1567,7 +1670,7 @@ * See the user documentation for details on what it does. * * Syntax: - * addressbook identifier primary personID propName + * addressbook identifier primary personID propName ?ident? * * Results: * A standard Tcl result. @@ -1587,14 +1690,17 @@ { ABAddressBookRef ab; Tcl_DString ds; - CONST84 char * theProperty, * theUID; - CFStringRef propertyRef, valueRef, identRef; + CONST84 char * theProperty, * theUID, * theNewValue; + CFStringRef propertyRef, newvalueRef, multivalueRef; ABRecordRef personRecord; char theString[256]; int length, result = TCL_OK; + Boolean setIt = false; + // Setting the primary does not seem to work. Temporarily disabling it. + // objc != 5 && objc != 6 if (objc != 5) { - Tcl_WrongNumArgs(interp, 2, objv, "personID propName"); + Tcl_WrongNumArgs(interp, 2, objv, "personID propName ?identifier?"); return TCL_ERROR; } @@ -1627,28 +1733,54 @@ goto DONE; } - valueRef = ABRecordCopyValue(personRecord, propertyRef); - if (!valueRef) { + if (objc == 6) { + Tcl_IncrRefCount(objv[5]); + theNewValue = Tcl_GetStringFromObj(objv[5], &length); + newvalueRef = CFStringCreateWithCString(NULL, theNewValue, kCFStringEncodingUTF8); + if (!newvalueRef) { + Tcl_AppendStringsToObj(resultPtr, "Problem getting CFString for identifier", (char *) NULL); + CFRelease(propertyRef); + result = TCL_ERROR; + goto DONE; + } + setIt = true; + } + + multivalueRef = ABRecordCopyValue(personRecord, propertyRef); + if (!multivalueRef) { Tcl_AppendStringsToObj(resultPtr, "Couldn't get value for '", theProperty, "' property", (char *) NULL); result = TCL_ERROR; } else { - identRef = ABMultiValueCopyPrimaryIdentifier((ABMutableMultiValueRef) valueRef); - if (!identRef) { - Tcl_AppendStringsToObj(resultPtr, "Couldn't get primary identifier for '", theProperty, "' property", (char *) NULL); - result = TCL_ERROR; + if (setIt) { + if (!ABMultiValueSetPrimaryIdentifier((ABMutableMultiValueRef) multivalueRef, newvalueRef)) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't set primary identifier", (char *) NULL); + result = TCL_ERROR; + } else { + Tcl_ExternalToUtfDString(gMacRomanEnc, theNewValue, strlen(theNewValue), &ds); + Tcl_DStringResult(interp, &ds); + Tcl_DStringFree(&ds); + } + CFRelease(newvalueRef); } else { - CFStringGetCString(identRef, theString, sizeof(theString), NULL); - Tcl_ExternalToUtfDString(gMacRomanEnc, theString, strlen(theString), &ds); - Tcl_DStringResult(interp, &ds); - Tcl_DStringFree(&ds); - CFRelease(identRef); + CFStringRef identRef = ABMultiValueCopyPrimaryIdentifier((ABMutableMultiValueRef) multivalueRef); + if (!identRef) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get primary identifier for '", theProperty, "' property", (char *) NULL); + result = TCL_ERROR; + } else { + CFStringGetCString(identRef, theString, sizeof(theString), NULL); + CFRelease(identRef); + Tcl_ExternalToUtfDString(gMacRomanEnc, theString, strlen(theString), &ds); + Tcl_DStringResult(interp, &ds); + Tcl_DStringFree(&ds); + } } - CFRelease(valueRef); + CFRelease(multivalueRef); } CFRelease(propertyRef); DONE: Tcl_DecrRefCount(objv[4]); + if (objc == 6) Tcl_DecrRefCount(objv[5]); CFRelease(personRecord); return result; } @@ -1690,10 +1822,9 @@ { ABAddressBookRef ab; Tcl_DString ds; - CONST84 char * theProperty, * theGroupUID, * thePersonUID, * theNewVal; - CFStringRef propertyRef, valueRef; + CONST84 char * theProperty, * theGroupUID, * thePersonUID, * theNewValue; + CFStringRef propertyRef, newvalueRef; ABRecordRef groupRecord, personRecord; - ABPropertyType thePropType; int length, result = TCL_OK; Boolean setIt = false; @@ -1742,9 +1873,9 @@ if (objc == 7) { Tcl_IncrRefCount(objv[6]); - theNewVal = Tcl_GetStringFromObj(objv[6], &length); - valueRef = CFStringCreateWithCString(NULL, theNewVal, kCFStringEncodingUTF8); - if (!valueRef) { + theNewValue = Tcl_GetStringFromObj(objv[6], &length); + newvalueRef = CFStringCreateWithCString(NULL, theNewValue, kCFStringEncodingUTF8); + if (!newvalueRef) { Tcl_AppendStringsToObj(resultPtr, "Problem getting CFString for identifier", (char *) NULL); CFRelease(propertyRef); result = TCL_ERROR; @@ -1754,16 +1885,16 @@ } if (setIt) { - if (!ABGroupSetDistributionIdentifier(groupRecord, personRecord, propertyRef, valueRef)) { + if (!ABGroupSetDistributionIdentifier(groupRecord, personRecord, propertyRef, newvalueRef)) { Tcl_AppendStringsToObj(resultPtr, "Couldn't set distribution identifier for '", theProperty, "' property", (char *) NULL); result = TCL_ERROR; } else { - Tcl_ExternalToUtfDString(gMacRomanEnc, theNewVal, strlen(theNewVal), &ds); + Tcl_ExternalToUtfDString(gMacRomanEnc, theNewValue, strlen(theNewValue), &ds); Tcl_DStringResult(interp, &ds); Tcl_DStringFree(&ds); } - CFRelease(valueRef); + CFRelease(newvalueRef); } else { CFStringRef identRef = ABGroupCopyDistributionIdentifier(groupRecord, personRecord, propertyRef); if (!identRef) { |