[Tcladdressbook-commits] Source TclAddressBook.c,1.32,1.33
Status: Alpha
Brought to you by:
bdesgraupes
|
From: Bernard D. <bde...@us...> - 2004-08-01 17:58:24
|
Update of /cvsroot/tcladdressbook/Source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1650/Source Modified Files: TclAddressBook.c Log Message: New command [addressbook label] Index: TclAddressBook.c =================================================================== RCS file: /cvsroot/tcladdressbook/Source/TclAddressBook.c,v retrieving revision 1.32 retrieving revision 1.33 diff -u -d -r1.32 -r1.33 --- TclAddressBook.c 1 Aug 2004 06:34:50 -0000 1.32 +++ TclAddressBook.c 1 Aug 2004 17:58:14 -0000 1.33 @@ -1,7 +1,7 @@ /* * File : "TclAddressBook.c" * Created: 2003-11-26 12:54:15 - * Last modification: 2004-07-30 11:36:35 + * Last modification: 2004-08-01 19:19:06 * Author: Bernard Desgraupes * e-mail: <bde...@ea...> * @@ -37,6 +37,7 @@ static int TclABCmd_Identifier(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], Tcl_Obj *resultPtr); static int TclABCmd_Image(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], Tcl_Obj *resultPtr); static int TclABCmd_Import(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], Tcl_Obj *resultPtr); +static int TclABCmd_Label(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], Tcl_Obj *resultPtr); static int TclABCmd_Parents(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], Tcl_Obj *resultPtr); static int TclABCmd_Persons(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], Tcl_Obj *resultPtr); static int TclABCmd_Property(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[], Tcl_Obj *resultPtr); @@ -85,23 +86,23 @@ } #endif - /* Register the [addressbook] command */ + // Register the [addressbook] command Tcl_CreateObjCommand(interp, "addressbook", TclABCmd_AddressBook, (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL); - /* Version numbering */ + // Version numbering if (TCLADDRESSBOOK_STAGE=='f') { if (TCLADDRESSBOOK_SUBMINOR) { - sprintf(vstr,"%d.%d\0", TCLADDRESSBOOK_MAJOR, TCLADDRESSBOOK_MINOR); - } else { sprintf(vstr,"%d.%d.%d\0", TCLADDRESSBOOK_MAJOR, TCLADDRESSBOOK_MINOR, TCLADDRESSBOOK_SUBMINOR); + } else { + sprintf(vstr,"%d.%d\0", TCLADDRESSBOOK_MAJOR, TCLADDRESSBOOK_MINOR); } } else { sprintf(vstr,"%d.%d%c%d\0", TCLADDRESSBOOK_MAJOR, TCLADDRESSBOOK_MINOR, TCLADDRESSBOOK_STAGE, TCLADDRESSBOOK_SUBMINOR); } - /* Declare the addressbook package */ + // Declare the addressbook package if (Tcl_PkgProvide(interp, "addressbook", vstr) != TCL_OK) { return TCL_ERROR; } @@ -146,16 +147,16 @@ static CONST char *switches[] = { "add", "changed", "count", "create", "delete", "export", - "getme", "groups", "identifier", "image", "import", "parents", - "persons", "property", "record", "remove", "save", "search", - "set", "setme", "type", (char *) NULL + "getme", "groups", "identifier", "image", "import", "label", + "parents", "persons", "property", "record", "remove", "save", + "search", "set", "setme", "type", (char *) NULL }; enum { TCLAB_ADD, TCLAB_CHANGED, TCLAB_COUNT, TCLAB_CREATE, TCLAB_DELETE, TCLAB_EXPORT, - TCLAB_GETME, TCLAB_GROUPS, TCLAB_IDENTIFIER, TCLAB_IMAGE, TCLAB_IMPORT, TCLAB_PARENTS, - TCLAB_PERSONS, TCLAB_PROPERTY, TCLAB_RECORD, TCLAB_REMOVE, TCLAB_SAVE, TCLAB_SEARCH, - TCLAB_SET, TCLAB_SETME, TCLAB_TYPE + TCLAB_GETME, TCLAB_GROUPS, TCLAB_IDENTIFIER, TCLAB_IMAGE, TCLAB_IMPORT, TCLAB_LABEL, + TCLAB_PARENTS, TCLAB_PERSONS, TCLAB_PROPERTY, TCLAB_RECORD, TCLAB_REMOVE, TCLAB_SAVE, + TCLAB_SEARCH, TCLAB_SET, TCLAB_SETME, TCLAB_TYPE }; resultPtr = Tcl_GetObjResult(interp); @@ -215,6 +216,10 @@ result = TclABCmd_Import(clientData, interp, objc, objv, resultPtr); break; + case TCLAB_LABEL: + result = TclABCmd_Label(clientData, interp, objc, objv, resultPtr); + break; + case TCLAB_PARENTS: result = TclABCmd_Parents(clientData, interp, objc, objv, resultPtr); break; @@ -1074,6 +1079,143 @@ /* *---------------------------------------------------------------------- * + * TclABCmd_Label -- + * + * This procedure is invoked to process the [addressbook parents] Tcl command. + * See the user documentation for details on what it does. + * + * Syntax: + * addressbook label personID propName index ?value? + * + * Results: + * A standard Tcl result. + * + * Side effects: + * See the user documentation. + * + *---------------------------------------------------------------------- + */ + +int +TclABCmd_Label( + ClientData clientData, /* Not used. */ + 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, * thePersonUID, * theIndex, * theNewValue; + CFStringRef propertyRef, newvalueRef, multivalueRef; + ABRecordRef personRecord; + int index, length, result = TCL_OK; + Boolean setIt = false; + + if (objc != 5 && objc != 6) { + Tcl_WrongNumArgs(interp, 2, objv, "personID propName index ?value?"); + return TCL_ERROR; + } + + Tcl_ResetResult(interp); + + // Get the address book + ab = ABGetSharedAddressBook(); + + thePersonUID = Tcl_GetStringFromObj(objv[2], &length); + theProperty = Tcl_GetStringFromObj(objv[3], &length); + theIndex = Tcl_GetStringFromObj(objv[4], &length); + + // Check if second argument is a person and get the corresponding record + if ( !TclAB_CheckItem(interp, ab, thePersonUID, "second", &personRecord, rec_person) ) { + return TCL_ERROR; + } + + // 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's type + if ( ! TclAB_CheckMultiValueProperty(ab, propertyRef, rec_person) ) { + Tcl_AppendStringsToObj(resultPtr, "Not a multi-string list property", (char *) NULL); + CFRelease(propertyRef); + result = TCL_ERROR; + goto DONE; + } + + // Retrieve the index + index = atoi(theIndex); + + 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 label", (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 property", (char *) NULL); + result = TCL_ERROR; + } else { + if (setIt) { + ABMutableMultiValueRef mutableRef = ABMultiValueCreateMutableCopy(multivalueRef); + if (!mutableRef) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't copy value ref", (char *) NULL); + result = TCL_ERROR; + } else { + if (!ABMultiValueReplaceLabel(mutableRef, newvalueRef, index)) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't set label", (char *) NULL); + result = TCL_ERROR; + } else { + ABRecordSetValue(personRecord, propertyRef, mutableRef); + Tcl_ExternalToUtfDString(gMacRomanEnc, theNewValue, strlen(theNewValue), &ds); + Tcl_DStringResult(interp, &ds); + Tcl_DStringFree(&ds); + } + CFRelease(mutableRef); + } + CFRelease(newvalueRef); + } else { + CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(multivalueRef, index); + if (!labelRef) { + Tcl_AppendStringsToObj(resultPtr, "Couldn't get label", (char *) NULL); + result = TCL_ERROR; + } else { + char theString[256]; + CFStringRef localizedRef = ABCopyLocalizedPropertyOrLabel(labelRef); + CFStringGetCString(localizedRef, theString, sizeof(theString), NULL); + CFRelease(localizedRef); + CFRelease(labelRef); + Tcl_ExternalToUtfDString(gMacRomanEnc, theString, strlen(theString), &ds); + Tcl_DStringResult(interp, &ds); + Tcl_DStringFree(&ds); + } + } + CFRelease(multivalueRef); + } + CFRelease(propertyRef); + +DONE: + if (objc == 6) Tcl_DecrRefCount(objv[5]); + CFRelease(personRecord); + return result; +} + + +/* + *---------------------------------------------------------------------- + * * TclABCmd_Parents -- * * This procedure is invoked to process the [addressbook parents] Tcl command. |