// -------------------------------------------------------------------------------------
// misc application utilities
// -------------------------------------------------------------------------------------
// Permission is granted to freely redistribute this source code, and to use fragments
// of this code in your own applications if you find them to be useful.  This class,
// along with the source code, come with no warranty of any kind, and the user assumes
// all responsibility for its use.
// -------------------------------------------------------------------------------------

#import <appkit/appkit.h>
#import <stdio.h>
#import <string.h>
#import <ctype.h>
#import <libc.h>
#import <sys/types.h>
#import <sys/stat.h>
#import <sys/param.h>
#import <sys/dir.h>
#import <defaults/defaults.h>

// -------------------------------------------------------------------------------------
// Application path functions

/* return path to application ("/appName" is removed from path) */
/* Note: This should be called at the beginning of the app to cache the app path name */
const char *XAppPath()
{
	static char	*appPathName = (char*)nil;
	if (!appPathName) {
		char *p, path[MAXPATHLEN + 1];
    	if (NXArgv[0][0] == '/') strcpy(path, NXArgv[0]);
		else { 
			getwd(path);
			strcat(path, "/");
			strcat(path, NXArgv[0]);
		}
		if ((p = rindex(path, '/')) && (p != path)) *p = 0;
		appPathName = strcpy((char*)malloc(strlen(path) + 1), path);
	}
	return appPathName;
}

/* return fully qualified file path name */
const char *XFullPath(const char *fileName)
{
	char	path[MAXPATHLEN + 1] = { 0 }, *p = path;
	if (*fileName != '/') p += strlen(strcpy(path, XAppPath()));
	if ((p > path) && (*(p - 1) != '/')) *p++ = '/';
	strcpy(p, fileName);
	return NXCopyStringBuffer(path);
}

// -------------------------------------------------------------------------------------
// change/set application icon

void XSetAppIcon(void *imageV)
{
	id				image = (id)imageV;
	id              iconWin = [NXApp appIcon];      
	NXPoint         iconOrigin = { 0.0, 0.0 };
	NXSize          imageSize;
	static id       tileImage = (id)nil;
	static NXSize   tileSize = { 0.0, 0.0 };

	/* init dock icon tile */
	if (!tileImage && (tileImage = [NXImage findImageNamed:"NXAppTile"])) {
		[tileImage getSize:&tileSize];
	}

	/* redraw AppIcon */
	[[iconWin contentView] lockFocus];
	PSsetinstance(FALSE);
	[tileImage composite:NX_COPY toPoint:&iconOrigin];
	[tileImage getSize:&tileSize];
	[image getSize:&imageSize];
	iconOrigin.x = (tileSize.width  - imageSize.width ) / 2.0;
	iconOrigin.y = (tileSize.height - imageSize.height) / 2.0;
	[image composite:NX_SOVER toPoint:&iconOrigin];
	[[iconWin contentView] unlockFocus];
	[iconWin display];
  
}

// -------------------------------------------------------------------------------------
// Launch applications

/* launch application with specified file */
int XLaunchApplication(const char *applName, const char *hostName, const char *openFile)
{
  port_t		port;
  int			ok = YES;
  char			*host = (hostName && *hostName)? hostName : (char*)nil;
  if ((port = NXPortFromName(applName, host)) == PORT_NULL) return -1;
  [[NXApp appSpeaker] setSendPort:port];
  [[NXApp appSpeaker] openFile:openFile ok:&ok];
  return 0;
}

/* kill all tasks currently running with the given name */
int XKillNamedTask(char *name)
{
  char	line[256], processName[256];
  int	pid;
  FILE	*fpsys = popen("ps -x", "r");
  if (!fpsys) return -1;
  while (fgets(line, 256, fpsys))  {
	sscanf(line, "%d %*s %*s %*s %s", &pid, processName);
	if (!strcmp(processName, name)) kill(pid, SIGKILL);
  }
  pclose(fpsys);
  return 0;
}
