// // notifications.m // MacPorts.Framework // // Created by Randall Hansen Wood on 13/3/2008. // Copyright 2008 __MyCompanyName__. All rights reserved. // #include #include int Notifications_Send(int objc, Tcl_Obj *CONST objv[], int global, Tcl_Interp *interpreter) { NSString *name = nil; NSMutableDictionary *info = nil; int tclCount; int tclResult; int i; const char **tclElements; name = [NSString stringWithUTF8String:Tcl_GetString(*objv)]; ++objv; --objc; tclResult = Tcl_SplitList(interpreter, Tcl_GetString(*objv), &tclCount, &tclElements); if (tclResult == TCL_OK) { info = [NSMutableDictionary dictionaryWithCapacity:(tclCount / 2)]; for (i = 0; i < tclCount; i +=2) { [info setObject:[NSString stringWithUTF8String:tclElements[i + 1]] forKey:[NSString stringWithUTF8String:tclElements[i]]]; } if (global != 0) { [[NSDistributedNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info]; } else { [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info]; } } else { return TCL_ERROR; } return TCL_OK; } int Notifications_Command(ClientData clientData, Tcl_Interp *interpreter, int objc, Tcl_Obj *CONST objv[]) { NSAutoreleasePool *pool = [NSAutoreleasePool new]; NSString *action = nil; int returnCode = TCL_ERROR; ++objv, --objc; if (objc) { action = [NSString stringWithUTF8String:Tcl_GetString(*objv)]; ++objv, --objc; if ([action isEqualToString:@"send"]) { if ([[NSString stringWithUTF8String:Tcl_GetString(*objv)] isEqualToString:@"global"]) { ++objv, --objc; returnCode = Notifications_Send(objc, objv, 1, interpreter); } else { returnCode = Notifications_Send(objc, objv, 0, interpreter); } } } [pool release]; return returnCode; } int Notifications_Init(Tcl_Interp *interpreter) { if (Tcl_InitStubs(interpreter, "8.4", 0) == NULL) { return TCL_ERROR; } Tcl_CreateObjCommand(interpreter, "notifications", Notifications_Command, NULL, NULL); if (Tcl_PkgProvide(interpreter, "notifications", "1.0") != TCL_OK) { return TCL_ERROR; } return TCL_OK; } int Notifications_SafeInit(Tcl_Interp *interpreter) { return Notifications_Init(interpreter); }