ftp.nice.ch/pub/next/tools/workspace/NewFile.2.0.s.tar.gz#/NewFile2-src/Main.m

This is Main.m in view mode; [Download] [Up]

/* Generated by Interface Builder */

#import "Main.h"
#import "Protos.h"
#import "Server.h"
#import <appkit/Application.h>
#import <appkit/Listener.h>
#import <appkit/Speaker.h>
#import <appkit/Panel.h>
#import <objc/List.h>
#import <stdio.h>
#import <libc.h>
#import <appkit/defaults.h>

// Temporary files for updating services.
#define SERVICE_FILE "/tmp/NewFileService"
#define EXEC_FILE "/tmp/NewFileExec"

@implementation Main

// The application's folder.
- (char *)appdir
{
	return appdir;
}

// Translate a prototype's name into an absolute pathname stored in fullname.
// Returns fullname.
- (char *)fullprotoname:(char *)fullname:(char *)name
{
	sprintf(fullname, "%s/blanks/%s", [self appdir], name);
	return fullname;
}

// Copy a file/directory.
- (int)copyfile:(char *)to:(char *)from
{
	int reply;
	char command[3*MAXPATHLEN];
	sprintf(command,"/bin/cp -rp '%s' '%s'", from, to);
	reply = system(command);
	return reply;;
}

- appDidInit:sender
{
	//Find the application's folder.
	strcpy(appdir, NXArgv[0]);
	*rindex(appdir, '/') = '\0';

	listener = [NXApp appListener];
	speaker = [NXApp appSpeaker];
	[listener setServicesDelegate:server];
	
	// Read in list of types.
	[self loadstate];
	// Tell the servier to initialize itself.
	[server inittypes:[protos protoslist]];
	
	return self;
}

/* Intercept termination to write text and defaults.*/
- appWillTerminate:sender
{
	[self savestate];
	return self;
}


// Initialization proper.
- loadstate
{
	char	rc[MAXPATHLEN];
	FILE *fp;
	
	// First, check for rc file in the user's home directory.
	sprintf(rc, "%s/.NewFilerc", getenv("HOME"));
	fp = fopen(rc, "r");
	if (fp)
	{
		[protos readprotos:fp];
		fclose(fp);
	}
	else
	{
		// No rc file in home directory, so use the one in the app's folder,
		sprintf(rc, "%s/NewFilerc", [self appdir]);
		fp = fopen(rc, "r");
		if (fp)
		{
			[protos readprotos:fp];
			fclose(fp);
		}
		else
			// Should never get to here.
			fprintf(stderr, "NewFile: can't open rc file: your version of NewFile is incomplete.\n");
	}
	
	return self;
}

// Store the defaults.
- savestate;
{
	char	rc[MAXPATHLEN];
	FILE *fp;
	sprintf(rc, "%s/.NewFilerc", getenv("HOME"));
	fp = fopen(rc, "w");
	if (fp)
	{
		[protos writeprotos:fp];
		fclose(fp);
	}
	else
		fprintf(stderr, "NewFile: can't open rc file\n");
	return self;
}


// Open a file by messaging the Workspace Manager.
- openinws:(char *)file
{
	int msgDelivered, fileOpened;
	port_t	wsport;

	wsport = NXPortFromName(NX_WORKSPACEREQUEST, NULL);
	if (wsport == PORT_NULL) fprintf(stderr, "NewFile: Can't get Workspace port\n");
	[speaker setSendPort:wsport];
	msgDelivered = [speaker openFile:file ok:&fileOpened];
	if ((msgDelivered!=0) || (fileOpened==NO))
		fprintf(stderr, "NewFile: Couldn't open %s\n", file);
	return self;
}

// Open a file by messaging an application.
- openineditor:(char *)file:(char *)app
{
	int msgDelivered, fileOpened; 
	port_t port;
	
	port = NXPortFromName(app, NULL);
	if (port!=PORT_NULL)
	{
		[speaker setSendPort:port];
		msgDelivered = [speaker openFile:file ok:&fileOpened];
		if ((msgDelivered!=0) || (fileOpened==NO))
			fprintf(stderr, "NewFile: Couldn't open %s\n", file);
	}
	else
	{
		fprintf(stderr, "NewFile: Couldn't get port for %s. Opening in Workspace\n", app);
		[self openinws:file];
	}
	return self;
}

// I'd rather discuss this.
- updateservices:sender
{
	char cmd[MAXPATHLEN];
	FILE *serviceFile;
	int i, count;
	id	list;
	char	*typename;
	
	if (NXRunAlertPanel("Update services?", "This action will alter the executable file and exit NewFile.", "Go ahead", "Cancel", NULL) == NX_ALERTDEFAULT)
	{
		[self savestate];
		if ((serviceFile = fopen(SERVICE_FILE, "w")) == NULL) {
			NXRunAlertPanel(NULL, "Could not open temporary file %s", NULL, NULL, 
				NULL, SERVICE_FILE);
			exit(1);
		}
		
		list = [protos protoslist];
		count = [list count];
		for (i=0;i<count;i++)
		{
			typename = [[list objectAt:i] typename];
			fprintf(serviceFile, "Message: createNew\nPort: NewFile\nMenu Item: New File/%s\nUser Data: %s\nSend Type: NXFilenamePboardType\nDeactivate Requestor: YES\n\n", typename, typename);
		}
		fclose(serviceFile);
	
		/*
			* OK, now we have a new service file, so insert it in the executable.
			*/
			sprintf(cmd, "/bin/segedit %s -r __ICON __services %s -o %s", NXArgv[0],
			SERVICE_FILE, EXEC_FILE);
		if (system(cmd) != 0) {
			NXRunAlertPanel(NULL, "Command %s failed!\nYou should copy NewFile.app into ~/Apps. See Help.", NULL, NULL, NULL, cmd);
			exit(1);
		}
		sprintf(cmd, "/bin/rm -f %s ; /bin/mv %s %s ; chmod a+x %s", NXArgv[0], EXEC_FILE, 
		NXArgv[0], NXArgv[0]);
		if (system(cmd) != 0) {
			NXRunAlertPanel(NULL, "Command %s failed!\nYou should copy NewFile.app into ~/Apps. See Help.", NULL, NULL, NULL, cmd);
			exit(1);
		}
		if (system("make_services") != 0) {
			NXRunAlertPanel(NULL, "Command make_services failed!", NULL, NULL, NULL);
			exit(1);
		}
		NXRunAlertPanel(NULL, "Update of services complete. Applications launched from now on should show new services.", NULL, NULL, NULL);
		exit(0);
	}
	return self;	
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.