/***************************************************************************
                         fileselector.cpp - File selection widget 
                             -------------------
    copyright            : (C) 2002-2003 Marc Britton <consume@optusnet.com.au>
                           (C) 2004      Michal Rudolf <mrudolf@kdewebdev.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/


/* KDE INCLUDES */
#include <tdefiledialog.h>
#include <klineedit.h>
#include <kpushbutton.h>
#include <kurlcompletion.h>
#include <kiconloader.h>

/* QT INCLUDES */
#include <tqstring.h>
#include <tqwidget.h>
#include <tqstringlist.h>
#include <tqevent.h>
#include <tqlayout.h>
#include <tqsizepolicy.h>

/* OTHER INCLUDES */
#include <specials.h>
#include "fileselector.h"

FileSelector::FileSelector(TQWidget * a_parent, const char *a_name)
   : TQWidget(a_parent, a_name), KommanderWidget(TQT_TQOBJECT(this))
{
  TQStringList states;
  states << "default";
  setStates(states);
  setDisplayStates(states);

  m_lineEdit = new KLineEdit(this);
  
  m_selectButton = new KPushButton(BarIconSet("document-open"), "", this);

  m_boxLayout = new TQHBoxLayout(this, 0, 11);
  m_boxLayout->addWidget(m_lineEdit);
  m_boxLayout->addWidget(m_selectButton);

  setSizePolicy(m_lineEdit->sizePolicy());
  m_lineEdit->setSizePolicy(TQSizePolicy(TQSizePolicy::Expanding, TQSizePolicy::Expanding));
  m_selectButton->setSizePolicy(TQSizePolicy(TQSizePolicy::Minimum, TQSizePolicy::Expanding));

  connect(m_selectButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(makeSelection()));

  setSelectionType(Open);
  setSelectionOpenMultiple(false);
}

FileSelector::~FileSelector()
{
}

TQString FileSelector::currentState() const
{
  return TQString("default");
}

bool FileSelector::isKommanderWidget() const
{
  return true;
}

TQStringList FileSelector::associatedText() const
{
  return KommanderWidget::associatedText();
}

void FileSelector::setAssociatedText(const TQStringList& a_at)
{
  KommanderWidget::setAssociatedText(a_at);
}

void FileSelector::setPopulationText(const TQString& a_text)
{
  KommanderWidget::setPopulationText(a_text);
}

TQString FileSelector::populationText() const
{
  return KommanderWidget::populationText();
}

void FileSelector::populate()
{
  TQString txt = KommanderWidget::evalAssociatedText(populationText());
  setWidgetText(txt);
}

void FileSelector::setWidgetText(const TQString& a_text)
{
  m_lineEdit->setText(a_text);
  emit widgetTextChanged(a_text);
}

FileSelector::SelectionType FileSelector::selectionType() const
{
  return m_selectionType;
}

void FileSelector::setSelectionType(SelectionType a_selectionType)
{
  m_selectionType = a_selectionType;
  delete m_lineEdit->completionObject();
  if (m_selectionType == Directory) 
     m_lineEdit->setCompletionObject(new KURLCompletion(KURLCompletion::DirCompletion));
  else
     m_lineEdit->setCompletionObject(new KURLCompletion(KURLCompletion::FileCompletion));
  
}

TQString FileSelector::selectionFilter() const
{
  return m_filter;
}

void FileSelector::setSelectionFilter(const TQString& a_filter)
{
  m_filter = a_filter;
}

TQString FileSelector::selectionCaption() const
{
  return m_caption;
}

void FileSelector::setSelectionCaption(const TQString& a_caption)
{
  m_caption = a_caption;
}

bool FileSelector::selectionOpenMultiple() const
{
  return m_openMultiple;
}

void FileSelector::setSelectionOpenMultiple(bool a_openMultiple)
{
  m_openMultiple = a_openMultiple;
}

void FileSelector::makeSelection()
{
  TQString text;
  if (m_selectionType == Open && m_openMultiple)
    text = KFileDialog::getOpenFileNames(m_lineEdit->text(), m_filter, this, m_caption).join("\n");
  else if (m_selectionType == Open && !m_openMultiple)
    text = KFileDialog::getOpenFileName(m_lineEdit->text(), m_filter, this, m_caption);
  else if (m_selectionType == Save)
    text = KFileDialog::getSaveFileName(m_lineEdit->text(), m_filter, this, m_caption);
  else if (m_selectionType == Directory)
    text = KFileDialog::getExistingDirectory(m_lineEdit->text(), this, m_caption);
  
  if (!text.isEmpty())
    setWidgetText(text);
}

TQString FileSelector::caption() const
{
  return m_selectButton->text();
}

void FileSelector::setCaption(const TQString& a_text)
{
  m_selectButton->setText(a_text);
}

bool FileSelector::showEditor() const
{
  return m_lineEdit->isVisible();
}

void FileSelector::setShowEditor(bool a_visible)
{
  if (a_visible)
    m_lineEdit->show();
  else
    m_lineEdit->hide();
}

void FileSelector::showEvent(TQShowEvent * e)
{
  TQWidget::showEvent(e);
  emit widgetOpened();
}

void FileSelector::contextMenuEvent( TQContextMenuEvent * e )
{
  e->accept();
  TQPoint p = e->globalPos();
  emit contextMenuRequested(p.x(), p.y());
}

bool FileSelector::isFunctionSupported(int f)
{
  return f == DCOP::text || f == DCOP::setText || f == DCOP::selection || f == DCOP::setSelection ||
      f == DCOP::clear;
}

TQString FileSelector::handleDCOP(int function, const TQStringList& args)
{
  switch (function) {
    case DCOP::text:
      return m_lineEdit->text();
    case DCOP::setText:
      setWidgetText(args[0]);
      break;
    case DCOP::selection:
      return m_lineEdit->selectedText();
    case DCOP::setSelection:
    { 
      int f = m_lineEdit->text().find(args[0]);
      if (f != -1)
        m_lineEdit->setSelection(f, args[0].length());
      break;
    }
    case DCOP::clear:
      setWidgetText(TQString());
      break;
    default:
      return KommanderWidget::handleDCOP(function, args);
  }
  return TQString();
}


#include "fileselector.moc"