// Modified by Yen-Ju Chen <yjchenx@hotmail.com>
/*
**  PlopWindow.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"
#include "PlopletView.h"
#include "Channel.h"
#include <Toolbox/Utilities.h>
#include <AppKit/AppKit.h>

@implementation PlopletView

- (void) reload
{
  NSRange range;
  Channel *aChannel;

  range = [location rangeOfString: @"http://" options: NSCaseInsensitiveSearch];
  if (range.location == NSNotFound)
    {
      range = [location rangeOfString: @".klip"
                              options: NSCaseInsensitiveSearch];
      if (range.location == NSNotFound)
        {
          // Local file
          aChannel = [[Channel alloc] initWithContentsOfFile: [location stringByStandardizingPath]];
        }
      else
        {
          // Klip
          Plop *plop = [[Plop alloc] initWithContentsOfFile: [location stringByStandardizingPath]];
          aChannel = [[Channel alloc] initWithURL: [plop contentSource]];
          [aChannel setPlop: plop];
          RELEASE(plop);
        }
    }
  else
    {
      // for URL
      NSURL *url = [NSURL URLWithString: location];
      aChannel = [[Channel alloc] initWithURL: url];
    }
  [self setChannel: aChannel];
  RELEASE(aChannel);
}

- (id) initWithFrame: (NSRect) rect andLocation: (NSString *) loc
{
  NSScrollView *scrollView;

  self = [super initWithFrame: rect];
  ASSIGN(location, loc);

  scrollView = [[NSScrollView alloc] initWithFrame: [self bounds]];

  textView = [[NSTextView alloc] initWithFrame: rect];
  //[textView setTextContainerInset: NSMakeSize(5,5)];
  [textView setBackgroundColor: [NSColor whiteColor]];
  [textView setDrawsBackground: YES];
  [textView setRichText: YES];
  [textView setUsesFontPanel: YES];
  [textView setHorizontallyResizable: NO];
  [textView setVerticallyResizable: YES];
  [textView setMinSize: NSMakeSize (0, 0)];
  [textView setMaxSize: NSMakeSize (1E7, 1E7)];
  [textView setAutoresizingMask: NSViewHeightSizable|NSViewWidthSizable];
  [[textView textContainer] setContainerSize: 
             NSMakeSize([[scrollView contentView] frame].size.width, 1E7)];
  [[textView textContainer] setWidthTracksTextView: YES];
  [textView setEditable: NO];
  [textView setDelegate: self];

  [scrollView setDocumentView: textView];
  [scrollView setHasHorizontalScroller: NO];
  [scrollView setHasVerticalScroller: YES];
  [scrollView setBorderType: NSBezelBorder];
  [scrollView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
  RELEASE(textView);
  
  [self addSubview: scrollView];
  RELEASE(scrollView);

  [self reload];

  return self;
}

- (void) dealloc
{
  RELEASE(location);

  [channel setPlopletView: nil];
  RELEASE(channel);
  [super dealloc];
}

- (NSTextView *) textView
{
  return textView;
}

- (Channel *) channel
{
  return channel;
}

- (void) setChannel: (Channel *) theChannel
{
  if ( theChannel )
    {
      ASSIGN(channel, theChannel);

      [channel setPlopletView: self];
      
      // We now update the text view with the content of the channel
      [self updateTextView: nil];
    }
  else
    {
      RELEASE(channel);
      channel = nil;
    }
}

// Other methods

- (void) updateTextView: (NSString *) text
{
  NSMutableAttributedString *attr;
  int i;
  
  if ( ![self channel] )
    {
      return;
    }
  if (text)
    {
      attr = [[NSMutableAttributedString alloc] initWithString: text
                                                    attributes: nil];
      [[textView textStorage] setAttributedString: attr];
      RELEASE(attr);

      // We set the count of plopview and ask to refresh it
      [[self superview] setNeedsDisplay: YES];

      return;
    }

  attr = [[NSMutableAttributedString alloc] init];
  
  for (i = 0; i < [[self channel] count]; i++)
    {
      NSDictionary *attributes;
      Element *element;
      
      element = [[self channel] elementAtIndex: i];
      
      if ( [element type] == ITEM && [element link] )
	{
	  NSAttributedString *string;
          
	  attributes= [NSDictionary dictionaryWithObjectsAndKeys: 
                                [element link], NSLinkAttributeName, 
  [NSNumber numberWithInt: GSNoUnderlineStyle], NSUnderlineStyleAttributeName,
                          [NSColor blackColor], NSForegroundColorAttributeName,
  [NSColor colorWithCalibratedRed: 0.9 green:0.9 blue: 1.0 alpha: 1.0], NSBackgroundColorAttributeName,
             [NSFont boldSystemFontOfSize: 14], NSFontAttributeName,
                                          nil];
	  string = [[NSAttributedString alloc] initWithString: [[element title] stringByAppendingString: @"\n"]
             	                                   attributes: attributes];
	  
	  [attr appendAttributedString: string];
          RELEASE(string);

          attributes= [NSDictionary dictionaryWithObjectsAndKeys: 
                               [element link], NSLinkAttributeName,
                          [NSColor blackColor], NSForegroundColorAttributeName,
                   [NSFont userFontOfSize: 12], NSFontAttributeName,
                                          nil];

          string = [[NSAttributedString alloc] initWithString: [[element description] stringByAppendingString: @"\n"]
                                                   attributes: attributes];

          [attr appendAttributedString: string];
          RELEASE(string);
	}
  }

  [[textView textStorage] setAttributedString: attr];
  RELEASE(attr);

  // We set the count of plopview and ask to refresh it
  [[self superview] setNeedsDisplay: YES];
}

- (BOOL) textView: (NSTextView *) textView
    clickedOnLink: (id) link 
          atIndex: (unsigned) charIndex
{
  OpenURL(link); /* Methods in Utilities.h */
  return YES;
}

@end
