// BWSnippetKeeper -- container object that keeps track of HTML fragments.
//		      Individual values accessed by KVC because I'm lazy

// Copyright (C) 2005 Borkware, LLC
// We use the BSD License.  Check out http://borkware.com/license

#import "BWSnippetKeeper.h"


@implementation BWSnippetKeeper


// snippets are actually stored in the user preferences.  Cheezy, but it
// works

- (void) setDefaultPrefs
{
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

    // the defaults make a very basic page
    [dictionary setObject: @"<html><head>\n<title>Gallery Thumbnails</title>\n</head><body>\n<h2>Gallery Thumbnails</h2>"
                forKey: @"documentHeader"];

    [dictionary setObject: @"<table border=0 cellpadding=2 cellspacing=2 width=700>\n<tr>"
                forKey: @"beforeThumbnails"];

    [dictionary setObject: @"<td>%@</td>"
                forKey: @"forEachImage"];

    [dictionary setObject: @"</tr>\n<tr>"
                forKey: @"forEveryXImages"];

    [dictionary setObject: @"5"
                forKey: @"everyX"];

    [dictionary setObject: @"</tr></table>"
                forKey: @"afterThumbnails"];

    [dictionary setObject: @"<hr></body></html>"
                forKey: @"documentFooter"];

    [[NSUserDefaults standardUserDefaults]
        registerDefaults: dictionary];

} // setDefaultPrefs



// bottleneck to get the string for a key from the defaults

- (NSString *) stringFromDefaultsFor: (NSString *) key
{
    NSString *value;
    value = [[NSUserDefaults standardUserDefaults] stringForKey: key];

    return (value);

} // stringFromDefaultsFor



// bottleneck to set a string for a key into the defaults

- (void) setString: (NSString *) value  
     inDefaultsFor: (NSString *) key
{
    [[NSUserDefaults standardUserDefaults] 
        setObject: value
        forKey: key];

} // setStringInDefaults



// populate the textfields from the defaults

- (void) updateUI
{
    [documentHeaderTextView
        setString: [self stringFromDefaultsFor: @"documentHeader"]];
    [beforeThumbnailsTextView
        setString: [self stringFromDefaultsFor: @"beforeThumbnails"]];
    [forEachImageTextView
        setString: [self stringFromDefaultsFor: @"forEachImage"]];
    [forEveryXImagesTextView
        setString: [self stringFromDefaultsFor: @"forEveryXImages"]];
    [afterThumbnailsTextView
        setString: [self stringFromDefaultsFor: @"afterThumbnails"]];
    [documentFooterTextView
        setString: [self stringFromDefaultsFor: @"documentFooter"]];
    
    [xField setStringValue: [self stringFromDefaultsFor: @"everyX"]];

} // updateUI



// set the values in the user defaults from the text fields

- (void) setStringsFromFields
{
    [self setString: [documentHeaderTextView string]
              inDefaultsFor: @"documentHeader"];

    [self setString: [beforeThumbnailsTextView string]
              inDefaultsFor: @"beforeThumbnails"];

    [self setString: [forEachImageTextView string]
              inDefaultsFor: @"forEachImage"];

    [self setString: [forEveryXImagesTextView string]
              inDefaultsFor: @"forEveryXImages"];

    [self setString: [afterThumbnailsTextView string]
              inDefaultsFor: @"afterThumbnails"];

    [self setString: [documentFooterTextView string]
              inDefaultsFor: @"documentFooter"];

    [self setString: [xField stringValue]
              inDefaultsFor: @"everyX"];

} // setStringsFromFields



// init a new object.  Register the new defaults

- (id) init
{
    if (self = [super init]) {
        [self setDefaultPrefs];
    }

    return (self);

} // init


// open the snippets editor in its own window

- (void) showSnippets
{
    if (window == nil) {
        [NSBundle loadNibNamed: @"BWHTMLSnippet"
                  owner: self];
    }
    [self updateUI];

    [window makeKeyAndOrderFront: self];

} // showSnippets



// callback from the editor sheet.  Just a no-op since the work
// is done in the ok: or cancel: methods

- (void) sheetDidEnd: (NSWindow *) sheet
          returnCode: (int) returnCode
         contextInfo: (void *) contextInfo
{
} // sheetDidEnd



// show the snippet editor as a sheet relative to another windowx

- (void) showSnippetsAsSheetInWindow: (NSWindow *) mainWindow
{
    if (window == nil) {
        [NSBundle loadNibNamed: @"BWHTMLSnippet"
                  owner: self];
    }
    
    // populate the text views
    [self updateUI];

    // start the sheet
    [NSApp beginSheet: window
           modalForWindow: mainWindow
           modalDelegate: self
           didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:)
           contextInfo: NULL];

} // showSnippetsAsSheetInWindow



// close the snippet window

- (void) closeSnippets
{
    [window orderOut: self];

} // closeSnippets



// accept the changes and store them into the user defaults

- (IBAction) ok: (id) sender
{
    [self setStringsFromFields];

    if ([window isSheet]) {
        [NSApp endSheet: window];
    }
    [self closeSnippets];

    // try to write them
    [[NSUserDefaults standardUserDefaults] synchronize];

} // ok



// reset the UI window to the existing settings

- (IBAction) cancel: (id) sender
{
    [self updateUI];

    if ([window isSheet]) {
        [NSApp endSheet: window];
    }
    [self closeSnippets];

    [[NSUserDefaults standardUserDefaults] synchronize];

} // cancel



// KVC magic.  They can do a valueForKey: for a snippet.  Rather than
// providing an API for everything, just use this to get the values.

- (id) valueForUndefinedKey: (NSString *) key
{
    id value;
    value = [self stringFromDefaultsFor: key];

    if (value == nil) {
        value = [super valueForUndefinedKey: key];
    }
    
    return (value);
    
} // valueForUndefinedKey


@end // BWSnippetKeeper

