/* InternetSearch.m

   Copyright (C) 2003 

   Author:  Yen-Ju Chen <yjchenx@hotmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; see the file COPYING.LIB.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "InternetSearchView.h"
#include <Toolbox/BundleLoader.h>
#include <Toolbox/Utilities.h>
#include <Foundation/Foundation.h>
#include <AppKit/NSTextField.h>
#include <AppKit/NSPopUpButton.h>

@implementation InternetSearchView

- (id) initWithFrame: (NSRect) frame andLocation: (NSString *) loc
{ 
  NSButton *goButton;
  id object;
  int i;

  self = [super initWithFrame: frame];
  sites = [NSMutableArray new];

  if ((loc == nil) || ([loc isEqualToString: @""]))
    {
      NSString *path = [[BundleLoader defaultLoader] 
                                pathForResource: @"InternetSearchDefaults"
                                         ofType: @"plist"
                                       ofBundle: @"InternetSearch.toolbox"];

      object = [NSArray arrayWithContentsOfFile: path];

      for (i = 0; i < [object count]; i++)
        {
          NSArray *temp = [[[object objectAtIndex: i] objectForKey: @"location"] componentsSeparatedByString: @","];
          [sites addObject: [NSDictionary dictionaryWithObjectsAndKeys: 
                   [temp objectAtIndex: 1], [temp objectAtIndex: 0], nil]];
        }
    }
  else
    {
      object = [loc componentsSeparatedByString: @","];

      for (i = 0; i < [object count]; i += 2)
        {
          [sites addObject: [NSDictionary dictionaryWithObjectsAndKeys:
                     [object objectAtIndex: i+1],
                     [object objectAtIndex: i],
                     nil]];
        }
    }

  sitesButton = [[NSPopUpButton alloc] initWithFrame: NSMakeRect(0, frame.size.height-22, 180, 22)];
  [sitesButton setTarget: self];
  [sitesButton setAction: @selector(sitesButtonAction:)];
  [self addSubview: sitesButton];
  RELEASE(sitesButton);

  for (i = 0; i < [sites count]; i++)
    {
      [sitesButton addItemWithTitle: [[[sites objectAtIndex: i] allKeys] objectAtIndex: 0]];
    }

  textField = [[NSTextField alloc] initWithFrame: NSMakeRect(0, frame.size.height-50, 180, 22)];
  [textField setTarget: self];
  [textField setAction: @selector(goAction:)];
  [self addSubview: textField];
  RELEASE(textField);

  goButton = [[NSButton alloc] initWithFrame: NSMakeRect(185, frame.size.height-50, 40, 22)];
  [goButton setButtonType:NSMomentaryPushButton];
  [goButton setKeyEquivalent: @"\r"];
  [goButton setTitle: _(@"Go !")];
  [goButton setTarget: self];
  [goButton setAction: @selector(goAction:)];
  [self addSubview: goButton];
  RELEASE(goButton);

  return self;
}

- (void) dealloc
{
  RELEASE(sites);
  [super dealloc];
}

- (void) goAction: (id) sender
{
  int index = [sitesButton indexOfSelectedItem];
  NSString *query = [[sites objectAtIndex: index] objectForKey: [sitesButton titleOfSelectedItem]];
  query = [NSString stringWithFormat: query, [textField stringValue]];
//  NSLog(@"query %@", [NSURL URLWithString: query]);
  OpenURL([NSURL URLWithString: query]);
}

- (void) sitesButtonAction: (id) sender
{
}

@end
