[Tcladdressbook-commits] Source TclAddressBookUtils.c,1.15,1.16
Status: Alpha
Brought to you by:
bdesgraupes
|
From: Bernard D. <bde...@us...> - 2004-07-30 11:14:55
|
Update of /cvsroot/tcladdressbook/Source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18389/Source Modified Files: TclAddressBookUtils.c Log Message: Subcommands to [identifier] command Index: TclAddressBookUtils.c =================================================================== RCS file: /cvsroot/tcladdressbook/Source/TclAddressBookUtils.c,v retrieving revision 1.15 retrieving revision 1.16 diff -u -d -r1.15 -r1.16 --- TclAddressBookUtils.c 29 Jul 2004 13:30:36 -0000 1.15 +++ TclAddressBookUtils.c 30 Jul 2004 11:14:46 -0000 1.16 @@ -1,7 +1,7 @@ /* * File : "TclAddressBookUtils.c" * Created: 2003-12-05 10:08:03 - * Last modification: 2004-07-29 11:07:23 + * Last modification: 2004-07-30 10:51:55 * Author: Bernard Desgraupes * e-mail: <bde...@ea...> * @@ -1331,6 +1331,458 @@ /* *---------------------------------------------------------------------- * + * TclAB_IdentifierCount -- + * + * This function is invoked to process the [addressbook identifier count] + * Tcl command. + * See the user documentation for details on what it does. + * + * Syntax: + * addressbook identifier count personID propName + * + * Results: + * A standard Tcl result. + * + * Side effects: + * See the user documentation. + * + *---------------------------------------------------------------------- + */ + +int +TclAB_IdentifierCount( + 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; + CFStringRef propertyRef, valueRef; + ABRecordRef personRecord; +// ABPropertyType thePropType; + int length, result = TCL_OK; + unsigned int mvCount; + + if (objc != 5) { + Tcl_WrongNumArgs(interp, 2, objv, "recordID propName"); + 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; + } + + Tcl_IncrRefCount(objv[4]); + theProperty = Tcl_GetStringFromObj(objv[4], &length); + + // Make CFString object for property + 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 type (string, date, multivalue...) +// // kABMultiArrayProperty +// // kABMultiDateProperty +// // kABMultiIntegerProperty +// // kABMultiRealProperty +// // kABMultiStringProperty + +// thePropType = ABTypeOfProperty(ab, kABPersonRecordType, propertyRef); +// if (thePropType == kABErrorInProperty) { +// Tcl_AppendStringsToObj(resultPtr, "Unknown property '", theProperty, +// "' for Person records", (char *) NULL); +// CFRelease(propertyRef); +// result = TCL_ERROR; +// goto DONE; +// } + + valueRef = ABRecordCopyValue(personRecord, propertyRef); + if (!valueRef) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get value for '", + theProperty, "' property", (char *) NULL); + result = TCL_ERROR; + } else { + mvCount = ABMultiValueCount((ABMutableMultiValueRef) valueRef); + CFRelease(valueRef); + Tcl_SetObjResult(interp, Tcl_NewIntObj(mvCount)); + } + CFRelease(propertyRef); + +DONE: + Tcl_DecrRefCount(objv[4]); + CFRelease(personRecord); + return result; +} + + +/* + *---------------------------------------------------------------------- + * + * TclAB_IdentifierGet -- + * + * This function is invoked to process the [addressbook identifier get] + * Tcl command. + * See the user documentation for details on what it does. + * + * Syntax: + * addressbook identifier get personID propName index + * + * Results: + * A standard Tcl result. + * + * Side effects: + * See the user documentation. + * + *---------------------------------------------------------------------- + */ + +int +TclAB_IdentifierGet( + 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; + Tcl_DString ds; + CONST84 char * theProperty, * theUID, * theIndex; + CFStringRef propertyRef, valueRef, identRef; + ABRecordRef personRecord; +// ABPropertyType thePropType; + char theString[256]; + int index, length, result = TCL_OK; + + if (objc != 6) { + Tcl_WrongNumArgs(interp, 2, objv, "personID propName index"); + 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; + } + + // Retrieve the index + Tcl_IncrRefCount(objv[5]); + theIndex = Tcl_GetStringFromObj(objv[5], &length); + index = atoi(theIndex); + +// // Check the property type (string, date, multivalue...) +// // kABMultiArrayProperty +// // kABMultiDateProperty +// // kABMultiIntegerProperty +// // kABMultiRealProperty +// // kABMultiStringProperty +// thePropType = ABTypeOfProperty(ab, kABPersonRecordType, propertyRef); +// if (thePropType == kABErrorInProperty) { +// Tcl_AppendStringsToObj(resultPtr, "Unknown property '", theProperty, +// "' for Person records", (char *) NULL); +// CFRelease(propertyRef); +// result = TCL_ERROR; +// goto DONE; +// } + + valueRef = ABRecordCopyValue(personRecord, propertyRef); + if (!valueRef) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get value for '", theProperty, "' property", (char *) NULL); + result = TCL_ERROR; + } else { + identRef = ABMultiValueCopyIdentifierAtIndex((ABMutableMultiValueRef) valueRef, index); + if (!identRef) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get identifier at index ", theIndex, (char *) NULL); + result = TCL_ERROR; + } else { + CFStringGetCString(identRef, theString, sizeof(theString), NULL); + Tcl_ExternalToUtfDString(gMacRomanEnc, theString, strlen(theString), &ds); + Tcl_DStringResult(interp, &ds); + Tcl_DStringFree(&ds); + CFRelease(identRef); + } + CFRelease(valueRef); + } + 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] + * Tcl command. + * See the user documentation for details on what it does. + * + * Syntax: + * addressbook identifier primary personID propName + * + * Results: + * A standard Tcl result. + * + * Side effects: + * See the user documentation. + * + *---------------------------------------------------------------------- + */ + +int +TclAB_IdentifierPrimary( + 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; + Tcl_DString ds; + CONST84 char * theProperty, * theUID; + CFStringRef propertyRef, valueRef, identRef; + ABRecordRef personRecord; +// ABPropertyType thePropType; + char theString[256]; + int length, result = TCL_OK; + + if (objc != 5) { + Tcl_WrongNumArgs(interp, 2, objv, "personID propName"); + 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 type (string, date, multivalue...) +// // kABMultiArrayProperty +// // kABMultiDateProperty +// // kABMultiIntegerProperty +// // kABMultiRealProperty +// // kABMultiStringProperty +// thePropType = ABTypeOfProperty(ab, kABPersonRecordType, propertyRef); +// if (thePropType == kABErrorInProperty) { +// Tcl_AppendStringsToObj(resultPtr, "Unknown property '", theProperty, +// "' for Person records", (char *) NULL); +// CFRelease(propertyRef); +// result = TCL_ERROR; +// goto DONE; +// } + + valueRef = ABRecordCopyValue(personRecord, propertyRef); + if (!valueRef) { + 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; + } else { + CFStringGetCString(identRef, theString, sizeof(theString), NULL); + Tcl_ExternalToUtfDString(gMacRomanEnc, theString, strlen(theString), &ds); + Tcl_DStringResult(interp, &ds); + Tcl_DStringFree(&ds); + CFRelease(identRef); + } + CFRelease(valueRef); + } + CFRelease(propertyRef); + +DONE: + Tcl_DecrRefCount(objv[4]); + CFRelease(personRecord); + return result; +} + + +/* + *---------------------------------------------------------------------- + * + * TclAB_IdentifierSet -- + * + * This function is invoked to process the [addressbook identifier set] + * Tcl command. + * See the user documentation for details on what it does. + * + * Syntax: + * addressbook identifier set groupID personID propName ?identifier? + * + * If the ?identifier? argument is specified, the command tries to set the + * distribution identifier of the property to that value, otherwise it returns + * the current value of the distribution identifier. It concerns + * multi-value list properties and applies to a particular person belonging to + * a particular group. + * + * Results: + * A standard Tcl result. + * + * Side effects: + * See the user documentation. + * + *---------------------------------------------------------------------- + */ + +int +TclAB_IdentifierSet( + 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; + Tcl_DString ds; + CONST84 char * theProperty, * theGroupUID, * thePersonUID, * theNewVal; + CFStringRef propertyRef, valueRef; + ABRecordRef groupRecord, personRecord; + ABPropertyType thePropType; + int length, result = TCL_OK; + Boolean setIt = false; + + if (objc != 6 && objc != 7) { + Tcl_WrongNumArgs(interp, 2, objv, "groupID personID propName ?identifier?"); + return TCL_ERROR; + } + + Tcl_ResetResult(interp); + + theGroupUID = Tcl_GetStringFromObj(objv[3], &length); + thePersonUID = Tcl_GetStringFromObj(objv[4], &length); + + // Get the address book + ab = ABGetSharedAddressBook(); + + // Check if third argument is a group and get the corresponding record + if ( !TclAB_CheckItem(interp, ab, theGroupUID, "third", &groupRecord, rec_group) ) { + return TCL_ERROR; + } + + // Check if fourth argument is a person and get the corresponding record + if ( !TclAB_CheckItem(interp, ab, thePersonUID, "fourth", &personRecord, rec_person) ) { + CFRelease(groupRecord); + return TCL_ERROR; + } + + Tcl_IncrRefCount(objv[5]); + theProperty = Tcl_GetStringFromObj(objv[5], &length); + + // Make CFString object for property + 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 type (string, date, multivalue...) + thePropType = ABTypeOfProperty(ab, kABPersonRecordType, propertyRef); + if (thePropType == kABErrorInProperty) { + Tcl_AppendStringsToObj(resultPtr, "Unknown property '", theProperty, + "' for Person records", (char *) NULL); + CFRelease(propertyRef); + result = TCL_ERROR; + goto DONE; + } + + if (objc == 7) { + Tcl_IncrRefCount(objv[6]); + theNewVal = Tcl_GetStringFromObj(objv[6], &length); + valueRef = CFStringCreateWithCString(NULL, theNewVal, kCFStringEncodingUTF8); + if (!valueRef) { + Tcl_AppendStringsToObj(resultPtr, "Problem getting CFString for identifier", (char *) NULL); + CFRelease(propertyRef); + result = TCL_ERROR; + goto DONE; + } + setIt = true; + } + + if (setIt) { + if (!ABGroupSetDistributionIdentifier(groupRecord, personRecord, propertyRef, valueRef)) { + 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_DStringResult(interp, &ds); + Tcl_DStringFree(&ds); + } + CFRelease(valueRef); + } else { + CFStringRef identRef = ABGroupCopyDistributionIdentifier(groupRecord, personRecord, propertyRef); + if (!identRef) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get distribution identifier of '", + theProperty, "' property for this record", (char *) NULL); + result = TCL_ERROR; + } else { + char theString[256]; + CFStringGetCString(identRef, theString, sizeof(theString), NULL); + CFRelease(identRef); + Tcl_ExternalToUtfDString(gMacRomanEnc, theString, strlen(theString), &ds); + Tcl_DStringResult(interp, &ds); + Tcl_DStringFree(&ds); + } + } + CFRelease(propertyRef); + +DONE: + Tcl_DecrRefCount(objv[5]); + if (objc == 7) Tcl_DecrRefCount(objv[6]); + CFRelease(groupRecord); + CFRelease(personRecord); + return result; +} + + +/* + *---------------------------------------------------------------------- + * * TclAB_MakeSearchRefFromTcl -- * * This function is invoked to build a CF object representing a value to |