// Modified by Yen-Ju Chen <yjchenx@hotmail.com>
/*
**  Plop.m
**
**  Copyright (c) 2002
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


#include "Plop.h"

// XML Headers needed under GNUstep
#include <Foundation/GSXML.h>
#include <libxml/tree.h>
/*
#include <libxml/entities.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
#include <libxml/SAX.h>
#include <libxml/HTMLparser.h>
#include <libxml/xmlmemory.h>
*/

static int currentVersion = 1;

@implementation Plop 

- (id) initWithContentsOfFile: (NSString *) path
{
  self = [self init];

  [self _importKlipUsingPath: path];

  return self;
}

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

  // We give a defaut size to our frame
  [self setRefresh: 600];

  [self setBannerSource: nil];
  [self setContentSource: nil];

  return self;
}

- (void) dealloc
{
  RELEASE(bannerSource);
  RELEASE(contentSource);
  RELEASE(defaultLink);
  RELEASE(description);
  RELEASE(iconSource);
  RELEASE(iconName);
  RELEASE(plopVersion);

  [super dealloc];
}

// coding protocol

- (void) encodeWithCoder: (NSCoder *) theCoder
{
  [Plop setVersion: currentVersion];

  [theCoder encodeObject: [self bannerSource]];
  [theCoder encodeObject: [self contentSource]];
  [theCoder encodeObject: [self defaultLink]];
  [theCoder encodeObject: [self description]];
  [theCoder encodeObject: [self iconSource]];
  [theCoder encodeObject: [self iconName]];
  [theCoder encodeObject: [self title]];
  [theCoder encodeObject: [NSNumber numberWithInt: [self refresh]] ];
  [theCoder encodeObject: [self plopVersion]];
}

- (id) initWithCoder: (NSCoder *) theCoder
{
  int aVersion;
  
  aVersion = [theCoder versionForClassName: NSStringFromClass([self class])];;

  self = [self init];

  // Version 1 - Corresponds to PlopFolio v0.1
  if ( aVersion == 1 )
    {
      [self setBannerSource: [theCoder decodeObject]];
      [self setContentSource: [theCoder decodeObject]];
      [self setDefaultLink: [theCoder decodeObject]];
      [self setDescription: [theCoder decodeObject]];
      [self setIconSource: [theCoder decodeObject]];
      [self setIconName: [theCoder decodeObject]];
      [self setTitle: [theCoder decodeObject]];
      [self setRefresh: [[theCoder decodeObject] intValue]];
      [self setPlopVersion: [theCoder decodeObject]];
    }
  
  return self;
}

// access / mutation methods
- (NSURL *) bannerSource
{
  return bannerSource;
}

- (void) setBannerSource: (NSURL *) theBannerSource
{
  ASSIGN(bannerSource, theBannerSource);
}

- (NSURL *) iconSource
{
  return iconSource;
}

- (void) setIconSource: (NSURL *) theIconSource
{
  ASSIGN(iconSource, theIconSource);
}

- (NSString *) iconName
{
  return iconName;
}

- (void) setIconName: (NSString *) theName
{
  ASSIGN(iconName, theName);
}

- (NSURL *) contentSource
{
  return contentSource;
}

- (void) setContentSource: (NSURL *) theContentSource
{
  ASSIGN(contentSource, theContentSource);
}

- (NSURL *) defaultLink
{
  return defaultLink;
}

- (void) setDefaultLink: (NSURL *) theDefaultLink
{
  ASSIGN(defaultLink, theDefaultLink);
}

- (NSString *) description
{
  return description;
}

- (void) setDescription: (NSString *) theDescription
{
  ASSIGN(description, theDescription);
}

- (int) refresh
{
  return refresh;
}

- (void) setRefresh: (int) theRefresh
{
  if ( theRefresh <= 0 )
    {
      theRefresh = 600;
    }
  refresh = theRefresh;
}

- (NSString *) title
{
  return title;
}

- (void) setTitle: (NSString *) theTitle
{
  ASSIGN(title, theTitle);
}

- (NSString *) plopVersion
{
  return plopVersion;
}

- (void) setPlopVersion: (NSString *) thePlopVersion
{
  ASSIGN(plopVersion, thePlopVersion);
}

@end

@implementation Plop (Private)

- (void) _importKlipUsingPath: (NSString *) thePath
{
  // GNUstep XML Parsing Code
  GSXMLParser *aParser;
  NSData *aData;

  aData = [NSData dataWithContentsOfFile: thePath];
  aParser = [GSXMLParser parserWithData: aData];
  [aParser keepBlanks: NO];
  
  if ( [aParser parse] )
    {
      GSXMLDocument *aDocument;
      GSXMLNode *aRoot, *aNode;

      aDocument = [aParser document];
  
      aRoot = [aDocument root];
      
      if ( [[aRoot name] caseInsensitiveCompare: @"KLIP"] != NSOrderedSame )
	{
	  NSLog(@"Not a Klip. Ignoring.");
          return;
	}
      
      aNode = [aRoot firstChild];
      NSLog(@"aNode %@", [aNode name]);
      
      //
      // We loop inside our owner/identity/locations/setup/script nodes
      //
      while (aNode != nil)
	{ 
	  if ( [aNode type] == XML_ELEMENT_NODE )
	    {
	      [self _parseKlipInformationsFromNode: aNode
		     plop: self];
	    }
	  
	  // We get the next node
	  aNode = [aNode next];
	}
    }
  else
    {
      NSLog(@"error occured while parsing .klip");
    }
}

- (void) _updatePlop: (Plop *) thePlop
		name: (NSString *) theName
	     content: (NSString *) theContent
{
  if ( [theName caseInsensitiveCompare: @"TITLE"] == NSOrderedSame )
    {
      [thePlop setTitle: theContent];
    }
  else if ( [theName caseInsensitiveCompare: @"ICON"] == NSOrderedSame )
    {
      [thePlop setIconSource: [NSURL URLWithString: theContent]];
    }
  else if ( [theName caseInsensitiveCompare: @"BANNER"] == NSOrderedSame )
    {
      [thePlop setBannerSource: [NSURL URLWithString: theContent] ];
    }
  else if ( [theName caseInsensitiveCompare: @"CONTENTSOURCE"] == NSOrderedSame )
    {
      [thePlop setContentSource: [NSURL URLWithString: theContent] ];
    }
  else if ( [theName caseInsensitiveCompare: @"DEFAULTLINK"] == NSOrderedSame )
    {
      [thePlop setDefaultLink: [NSURL URLWithString: theContent] ];
    }
  else if ( [theName caseInsensitiveCompare: @"DESCRIPTION"] == NSOrderedSame )
    {
      [thePlop setDescription: theContent];
    }
  else if ( [theName caseInsensitiveCompare: @"REFRESH"] == NSOrderedSame )
    {
      [thePlop setRefresh: [theContent intValue] ];
    }
  else if ( [theName caseInsensitiveCompare: @"VERSION"] == NSOrderedSame )
    {
      [thePlop setPlopVersion: theContent];
    }
}

- (void) _parseKlipInformationsFromNode: (id) theNode
				   plop: (Plop *) thePlop
{
  // GNUstep XML Parsing Code
  GSXMLNode *aChild;
  
  // We loop inside our channel/image/item node attributes
  if ( (aChild = [theNode firstChild]) )
    {
      while ( aChild )
	{
	  if ( [aChild type] == XML_ELEMENT_NODE )
	    {
	      NSString *aName, *aContent;
	      GSXMLNode *aNode;

              aNode = [aChild firstChild];
	      
	      while ( aNode )
		{
		  if ( [aNode type] == XML_TEXT_NODE )
		    {
		      if ([[[aNode content] stringByTrimmingSpaces] length] > 0)
			{
			  break;
			}
		    }
		 
		  aNode = [aNode next];
		}
	 
	      if ( !aNode )
		{
		  aChild = [aChild next];
		  continue;
		}

	      aName = [aChild name];
	      aContent = [[aNode content] stringByTrimmingSpaces];

	      if (aName && aContent)
		{
		  [self _updatePlop: thePlop
			name: aName
			content: aContent];
		}
	    }
	      
	  aChild = [aChild next];
	}
    }
  else
    {
      NSLog(@"No children");
    }
}

@end
