/* AppView.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 "AppView.h"
#include "Command.h"
#include <AppKit/NSScrollView.h>
#include <AppKit/NSTableView.h>
#include <AppKit/NSTableColumn.h>
#include <AppKit/NSTableHeaderCell.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSPathUtilities.h>
#include <Foundation/NSFileManager.h>

@implementation AppView

- (id) initWithFrame: (NSRect) frame 
{
  NSTableColumn *nameColumn;
  NSScrollView *scrollView;

  self = [super initWithFrame: frame];

  apps = [NSMutableArray new];

  nameColumn = [[NSTableColumn alloc] initWithIdentifier: @"name"];
  [nameColumn setEditable: NO];
  [nameColumn setMinWidth: 20];
  [[nameColumn headerCell] setStringValue: _(@"Applications")];
 
  tableView = [[NSTableView alloc] initWithFrame: frame];
  [tableView setDrawsGrid: NO];
  [tableView addTableColumn: nameColumn];
  [tableView setAutoresizesAllColumnsToFit: YES];
  [tableView setAllowsColumnSelection: NO];
  [tableView setAllowsMultipleSelection: NO];
  [tableView setAllowsEmptySelection: YES];
  [tableView setTarget: self];
  [tableView setDataSource: self];
  [tableView setDelegate: self];
  [tableView setDoubleAction: @selector(doubleAction:)];
  [tableView setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable];
  RELEASE(nameColumn);

  scrollView = [[NSScrollView alloc] initWithFrame: [self bounds]];
  [scrollView setHasHorizontalScroller:YES];
  [scrollView setHasVerticalScroller:YES];
  [scrollView setBorderType: NSBezelBorder];
  [scrollView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
  [scrollView setDocumentView: tableView];
  RELEASE(tableView);

  [self addSubview: scrollView];
  RELEASE(scrollView);

  [self reload];

  return self;
}

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

- (void) doubleAction: (id) sender
{
  Command *command = [[Command alloc] initWithCommand: @"openapp"
                                                 args: [NSArray arrayWithObject: [apps objectAtIndex: [tableView selectedRow]]]];
  [command executeCommand];
}

- (void) reload
{
  int i;
  id object;
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory,
                                                       NSAllDomainsMask,
                                                       YES);

  [apps removeAllObjects];
  for(i = 0; i < [paths count]; i++)
    {
      object = [[NSFileManager defaultManager] directoryContentsAtPath: [paths objectAtIndex: i]];
      [apps addObjectsFromArray: object];
    }
  [tableView reloadData];
}

- (int) numberOfRowsInTableView: (NSTableView *) view
{
  return [apps count];
}

- (id) tableView: (NSTableView *) view
           objectValueForTableColumn: (NSTableColumn *) column
           row: (int) row
{
  if ([[column identifier] isEqualToString: @"name"])
    {
//      return [[apps objectAtIndex: row] stringByDeletingPathExtension];
      return [apps objectAtIndex: row];
    }
  else // Description
    {
//      return [[[BundleLoader defaultLoader] classNameOf: [apps objectAtIndex: row]] bundleDescription];
      return nil;
    }
}

@end
