/*
 * Bob_Vadnais@pdh.com Sun Jun 27 13:46:11 PDT 1993
 *
 * Interface Builder palette that preserves the CVS directory inside a 
 * nib documement.
 *
 * Wouldn't it be nice if all apps had a document controller paradigm?
 * Then we could solve this same problem with Edit wiping out the CVS
 * directory inside .rtfd documents.
 */
 
#import "CVSPalette.h"

@implementation CVSPalette

    // Quick and dirty debugging code - I found that NSLog() and
    // kin didn't work here.  This won't work, either, unless you
    // create the logfile first.
#define printf alterPrint
void alterPrint( format, parm1, parm2)
const char *format;
{
#if 0
    char buf[ 1024];
    static NSFileHandle *handle=nil;

    if( handle==nil) {
        handle=[NSFileHandle fileHandleForWritingAtPath:@"/temp/logfile"];
        handle=[handle retain];
    }
    sprintf( buf, format, parm1, parm2);
    [handle writeData:[NSData dataWithBytesNoCopy:buf length:strlen( buf)]];
#endif
}

   // Register for @"IBDidSaveDocumentNotification"s.  Note that
   // it _doesn't_ register for IBDidSaveDocumentNotification
   // (as provided in IBDocuments.h).  For some reason that didn't
   // work (perhaps we aren't fully loaded at this point?).  To
   // find out the actual name, I just registered for all names
   // on all objects, and stored the notification descriptions to
   // the logfile.
-(void)finishInstantiate;
{
    [[NSNotificationCenter defaultCenter]
		addObserver:self
     		selector:@selector( didSaveDocument:)
         	name:@"IBDidSaveDocumentNotification"
       		object:nil];
}
-(void)didSaveDocument:(NSNotification *)aNotification
{
    NSString *source;

    source=[[aNotification object] documentPath];
    source=[source stringByAppendingString:@"~/CVS"];

    if( [[NSFileManager defaultManager] fileExistsAtPath:source]) {
        NSString *target;

        target=[[aNotification object] documentPath];
        target=[target stringByAppendingString:@"/CVS"];
        [[NSFileManager defaultManager] copyPath:source toPath:target handler:self];
    }
}

    // I honestly don't know if this "works".  I copied it directly
    // from the documentation, and don't really want to think of a
    // scenario to cause it to fire.
-(BOOL)fileManager:(NSFileManager *)manager
      shouldProceedAfterError:(NSDictionary *)errorDict
{
    int result;
    result=NSRunAlertPanel( @"CVS", @"File operation error: %@ with file: %@",
                            @"Proceed", @"Stop", NULL,
                            [errorDict objectForKey:@"Error"],
                            [errorDict objectForKey:@"Path"]);

    return result==NSAlertDefaultReturn;
}
@end
