
/* Generated by Interface Builder */

#import "Controller.h"
#import <appkit/Application.h>
#import <objc/typedstream.h>
#import <objc/NXStringTable.h>
#import <appkit/FontManager.h>
#import <appkit/Listener.h>
#import <defaults.h>

#define GETD(XX) ((strcmp(NXGetDefaultValue([NXApp appName],XX),"on")==0) ? 1 : 0)
#define STATE(XX) ([XX state] ? "on" : "off")

@implementation Controller

- init
{
  [super init];
  
  // get instances of support objects
  openReq = [OpenPanel new];
  archiveReq = [OpenPanel new];
  /*[archiveReq allowBranchSel:YES];*/
  return self;

}

- updateFileViewer
{
   id speaker;
   speaker = [NXApp appSpeaker];
   [speaker setSendPort:NXPortFromName(NX_WORKSPACEREQUEST, NULL)];
   [speaker performRemoteMethod:"updateViewers"];
   return self;
}

- showError: (const char *)errorMessage
{
  NXRunAlertPanel(NULL, errorMessage,
  		  [stringSet valueForStringKey:"OK"], NULL, NULL);
  return self;
}

- openRequest:sender
{
    const char *fileName;
    const char *const types[5] = {"lha", "lzh", "Z", "tar", NULL};

    if ([openReq runModalForTypes:types] && (fileName = [openReq filename])) {
	[self openFile:fileName];
    }
    else
	[self showError:[stringSet valueForStringKey:"errorOnOpenRequest"]];
    return self;
}

-(BOOL) appAcceptsAnotherFile:sender
{
  return (YES);
}

-(int) appOpenFile:(char *)fileName type:(char *)fileType
{
   return [self openFile:fileName];
}

static int
compare_postfix(char *str1, char *str2) {
   int l1, l2;

   l1 = strlen(str1);
   l2 = strlen(str2);
   if (l2>l1)
      return 0;
   if (strcmp(str1 + l1 - l2, str2)==0)
      return 1;
   return 0;
}

-(int) openFile:(const char *)fileName
{
   char buff[256], fname[256], *p;
   int done=0;
   
   strcpy (fname, fileName);
   p = strrchr (fname,'/');
   if (!p) return NO;
   *p = '\0';
   while (!done) {
      if (compare_postfix(p+1,".Z"))
         sprintf (buff, "(cd %s; uncompress %s)", fname, p+1);
      else if (compare_postfix(p+1,".lzh") || compare_postfix(p+1,".lha")) {
         if ( [boxRemoveLha state] )
            sprintf (buff, "(cd %s; lha xq %s; rm %s)", fname, p+1, p+1);
         else
            sprintf (buff, "(cd %s; lha xq %s)", fname, p+1);
        }
      else if (compare_postfix(p+1,".tar")) {
         if ( [boxRemoveTar state] )
            sprintf (buff, "(cd %s; tar -xf %s; rm %s)", fname, p+1, p+1);
         else
            sprintf (buff, "(cd %s; tar -xf %s)", fname, p+1);
        }  
      else if (compare_postfix(p+1,".zip")) {
         if ( [boxRemoveZip state] )
            sprintf (buff, "(cd %s; unzip %s; rm %s)", fname, p+1, p+1);
         else
            sprintf (buff, "(cd %s; unzip %s)", fname, p+1);
        }
      else {
         [self showError:[stringSet valueForStringKey:"errorOnOpen"]];
         return NO;
        }
      system (buff);
      done = 1;
      if ( compare_postfix(p+1,".tar.Z") && [boxAutoUntar state] ) {
         *strrchr (p+1, '.') = '\0';
	 done = 0;
	}
     }
   [self updateFileViewer];
   return YES;
}

- archiveRequest:sender
{
    const char *fileName;
    const char *const types[1] = {NULL};

    if ([archiveReq runModalForTypes:types] && (fileName = [openReq filename])) 
    {
	[self openFile:fileName];
    }
    else
	[self showError:[stringSet valueForStringKey:"errorOnOpenRequest"]];
    return self;
}

- appDidInit:sender
{
	static NXDefaultsVector ArchiveDefaults = {
		{"boxRemoveTar",  "on"},
		{"boxRemoveLha",  "off"},
		{"boxRemoveZip",  "off"},
		{"boxAutoQuit",   "on"},
		{"boxAutoUntar",  "on"},
		{"boxQueryScheme", "off"},
		{"boxQueryRemove", "off"},
		{NULL, NULL}
	};
		
	NXRegisterDefaults([NXApp appName], ArchiveDefaults);
	
	[boxRemoveTar setState:GETD("boxRemoveTar")];
	[boxRemoveLha setState:GETD("boxRemoveLha")];
	[boxRemoveZip setState:GETD("boxRemoveZip")];
	[boxAutoQuit  setState:GETD("boxAutoQuit")];
	[boxAutoUntar setState:GETD("boxAutoUntar")];
	[boxQueryScheme setState:GETD("boxQueryScheme")];
	[boxQueryRemove setState:GETD("boxQueryRemove")];
/*	
	myListener = [NXApp appListener];
	[myListener setServicesDelegate: self];
*/

	return self;
}

- appWillTerminate:sender
{
	NXDefaultsVector ArchiveDefaults = {
		{"boxRemoveTar", STATE(boxRemoveTar)},
		{"boxRemoveLha", STATE(boxRemoveLha)},
		{"boxRemoveZip", STATE(boxRemoveZip)},
		{"boxAutoQuit",  /*STATE(boxAutoQuit)*/ "off"},
		{"boxAutoUntar", STATE(boxAutoUntar)},
		{"boxQueryScheme", /*STATE(boxQueryScheme)*/ "off"},
		{"boxQueryRemove", /*STATE(boxQueryRemove)*/ "off"},
		{NULL, NULL}
	};

        printf("%s\n", STATE(boxRemoveTar));
	NXWriteDefaults([NXApp appName], ArchiveDefaults);
	
	return self;
}

- appDidResignActive:sender
{
   return self;
}

- appDidBecomeActive:sender
{
   return self;
}

@end
