
#import "Controller.h"
#import <misckit/MiscString.h>

@implementation Controller

//This code was pretty much taken from the example in the documentation for
//Services.  It's pretty basic; it only does ascii transfers.
//To cut down on code, I just used the User Data: field in the services.txt
//file to be different numbers, then I just atoi that here to figure out
//which service is requested.  I probably should've #defined some things
//or at least made an enumerated type to make it more readable, but oh well.
//It's not so big as this should be a problem.

- alterText:(id)pasteboard userData:(const char*)userData error:(char **)msg;
{
  const char *types[1];
  id theString;
  char *data;
  int length, i = atoi(userData);
  
//  printf("%s\n",userData);
  
  [pasteboard types];    // pretend to check the pasteboard types

  if ([pasteboard readType:NXAsciiPboardType data:&data length:&length]) {
    theString = [MiscString newWithString:""];
    [theString cat:data n:length];
    switch (i) {
      case 0:
        // I assume someone played with the User Data: fields if this happens
        *msg = "Don't play with services.txt!!";
	[theString free];
	return self;
	break;
      case 1: 
        [theString toLower];
	break;
      case 2:
        [theString toUpper];
	break;
      case 3:
        [theString invertCases];
	break;
      case 4:
        [theString reverse];
	break;
      case 5:
        [theString capitalizeEachWord];
	break;
      case 6:
        [theString trimSpaces];
	break;
      case 7:
        [theString trimLeadSpaces];
	break;
      case 8:
        [theString trimTailSpaces];
	break;
      case 9:
        [theString squashSpaces];
	break;
     }
    types[0] = NXAsciiPboardType;
    [pasteboard declareTypes:types num:1 owner:nil];
    [pasteboard writeType:NXAsciiPboardType data:(char *)[theString stringValue] length:[theString length]];
    
    [theString free];
   }
  else *msg = "Error: couldn't alter text.";
  
  return self;
}

// Have to let the workspace know where to find us
- appDidInit:sender
{
  id theListener = [NXApp appListener];
  [theListener setServicesDelegate:self];
  return self;
}

@end
