/***************************************************************************
                          projectnewgeneral.cpp  -  description
                             -------------------
    begin                : Fri Oct 27 2000
    copyright            : (C) 2000 by Dmitry Poplavsky & Alexander Yakovlev & Eric Laffoon <pdima@users.sourceforge.net,yshurik@penguinpowered.com,sequitur@easystreet.com>
                           (C) 2001-2002 by Andras Mantia <amantia@kde.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.                                   *
 *                                                                         *
 ***************************************************************************/

// qt includes
#include <tqdir.h>
#include <tqevent.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqlineedit.h>
#include <tqpushbutton.h>
#include <tqradiobutton.h>
#include <tqcombobox.h>

// kde includes
#include <tdefiledialog.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kurl.h>
#include <kprotocolinfo.h>

//app includes
#include "projectnewgeneral.h"
#include "quantacommon.h"
#include "qextfileinfo.h"

ProjectNewGeneral::ProjectNewGeneral(TQWidget *parent, const char *name )
  : ProjectNewGeneralS(parent,name)
{
  imagelabel->setPixmap( UserIcon("wiznewprjglb") );
  linePrjName->setFocus();

  TQStringList protocols = KProtocolInfo::protocols();
  protocols.sort();
  for ( uint i=0; i<protocols.count(); i++ )
  {
    KURL p;
    p.setProtocol(protocols[i]);
    TQString protocol = protocols[i];
    if ( KProtocolInfo::supportsWriting(p) &&
         KProtocolInfo::supportsMakeDir(p) &&
         KProtocolInfo::supportsDeleting(p) &&
         (protocol != "file" && protocol != "fonts" && protocol != "floppy" && protocol != "newcd" ))
    {
      comboProtocol->insertItem(protocol);
    }
  }
  comboProtocol->setCurrentItem(0);

  slotProtocolChanged(i18n("Local"));

  linePrjTmpl->setText("templates");
  linePrjToolbar->setText("toolbars");

  connect(comboProtocol, TQ_SIGNAL(activated(const TQString&)), TQ_SLOT(slotProtocolChanged(const TQString &)));
  connect( linePrjFile, TQ_SIGNAL(textChanged(const TQString &)),
           this,        TQ_SLOT(slotLinePrjFile(const TQString &)));
  connect( linePrjName, TQ_SIGNAL(textChanged(const TQString &)),
           this,        TQ_SLOT(slotLinePrjFile(const TQString &)));
  connect( linePrjDir,  TQ_SIGNAL(textChanged(const TQString &)),
           this,        TQ_SLOT(slotLinePrjFile(const TQString &)));
  connect( buttonDir,    TQ_SIGNAL(clicked()),
           this,        TQ_SLOT(slotButtonDir()));
  connect( linePrjName, TQ_SIGNAL(textChanged(const TQString &)),
           this,        TQ_SLOT(slotChangeNames(const TQString &)));
  connect( linePrjTmpl, TQ_SIGNAL(textChanged(const TQString &)), TQ_SLOT(slotLinePrjFile(const TQString &)));
  connect( buttonTmpl,  TQ_SIGNAL(clicked()), TQ_SLOT(slotButtonTmpl()));
  connect( linePrjToolbar, TQ_SIGNAL(textChanged(const TQString &)), TQ_SLOT(slotLinePrjFile(const TQString &)));
  connect( buttonToolbar,  TQ_SIGNAL(clicked()), TQ_SLOT(slotButtonToolbar()));

  linePrjTmpl->installEventFilter(this);
  linePrjToolbar->installEventFilter(this);
}

ProjectNewGeneral::~ProjectNewGeneral(){
}

void ProjectNewGeneral::slotButtonDir()
{
   slotLinePrjFile(""); // make sure baseUrl is correct
   KURL url = KFileDialog::getExistingURL(baseUrl.url(), this,
                              i18n("Select Project Folder"));
   if (!url.isEmpty())
   {
     linePrjDir->setText(url.path());
   }
}

void ProjectNewGeneral::slotLinePrjFile( const TQString & )
{

  bool valid = !(linePrjFile->text().isEmpty() ||
                 linePrjName->text().isEmpty() ||
                 linePrjDir ->text().isEmpty() ||
                 linePrjTmpl->text().isEmpty() ||
                 linePrjToolbar->text().isEmpty());
  KURL url;
  TQString s = lineHost->text();
  if (! s.isEmpty())
    url.setHost(s);

  s = lineUser->text();
  if (! s.isEmpty())
    url.setUser(s);
  s = linePasswd->text();
  if (! s.isEmpty())
    url.setPass(s);

  s = linePort->text();
  if (! s.isEmpty())
    url.setPort(linePort->text().toInt());

  url.setProtocol(comboProtocol->currentText());
  if (url.protocol() == i18n("Local")) url.setProtocol("file");
  url.setPath(linePrjDir->text());
  url.adjustPath(1);
  if (!url.path().startsWith("/")) url.setPath("/"+url.path());

  if (url.isValid())
  {
    emit setBaseURL(url);
    baseUrl = url;
    if (baseUrl.isLocalFile())
    {
      s = QExtFileInfo::canonicalPath(baseUrl.path());
      if (!s.isEmpty())
        baseUrl.setPath(s);
    }
  }
  emit enableNextButton( this, valid );
}

void ProjectNewGeneral::slotChangeNames( const TQString &text )
{
  int i;
  TQString fname = text.lower();
  while( (i=fname.find(" ")) >=0 ) fname.remove(i,1);

  linePrjFile->setText( fname+".webprj" );
}

TQString ProjectNewGeneral::type()
{
//  if ( radioCvs  ->isChecked() ) return "CVS";
  if ( radioLocal->isChecked() ) return "Local";
  if ( radioWeb  ->isChecked() ) return "Web";
  return "Local";
}

void ProjectNewGeneral::setMargin(int i)
{
  layout()->setMargin(i);
}

void ProjectNewGeneral::slotButtonTmpl()
{
   slotLinePrjFile(""); // make sure baseUrl is correct
   KURL url = KFileDialog::getExistingURL(baseUrl.url(), this,
                              i18n("Select Project Template Folder"));
   if (!url.isEmpty() && baseUrl.isParentOf(url))
   {
     linePrjTmpl->setText(KURL::relativeURL(baseUrl, url));
   } else
   {
     KMessageBox::sorry(this, i18n("<qt>The project templates must be stored under the main project folder: <br><br><b>%1</b></qt>").arg(baseUrl.prettyURL(0, KURL::StripFileProtocol)));
    }
}

void ProjectNewGeneral::slotButtonToolbar()
{
   slotLinePrjFile(""); // make sure baseUrl is correct
   KURL url = KFileDialog::getExistingURL(baseUrl.url(), this,
                              i18n("Select Project Toolbar & Actions Folder"));
   if (!url.isEmpty() && baseUrl.isParentOf(url))
   {
     linePrjToolbar->setText(KURL::relativeURL(baseUrl, url));
   }
   {
      KMessageBox::sorry(0, i18n("<qt>The project toolbars must be stored under the main project folder: <br><br><b>%1</b></qt>")
                                  .arg(baseUrl.prettyURL(0, KURL::StripFileProtocol)));
   }
}


void ProjectNewGeneral::slotProtocolChanged(const TQString& protocol)
{
 bool status = true;
 if (protocol == i18n("Local"))
 {
   status = false;
 }
 lineHost->setEnabled(status);
 lineUser->setEnabled(status);
 linePasswd->setEnabled(status);
 linePort->setEnabled(status);
 radioWeb->setEnabled(!status);
 slotChangeNames(linePrjName->text());
 if ( !status )
     lineHost->clear();
}

bool ProjectNewGeneral::eventFilter ( TQObject * watched, TQEvent * e )
{
  if (e->type() == TQEvent::FocusOut)
  {
    if (watched == linePrjTmpl)
    {
      KURL url = baseUrl;
      QuantaCommon::setUrl(url, linePrjTmpl->text());
      url = QExtFileInfo::toAbsolute(url, baseUrl);
      if (!baseUrl.isParentOf(url))
      {
        KMessageBox::sorry(this,i18n("<qt>The project templates must be stored under the main project folder: <br><br><b>%1</b></qt>").arg(baseUrl.prettyURL(0, KURL::StripFileProtocol)));
        linePrjTmpl->setFocus();
        emit enableNextButton(this, false);
      } else
        emit enableNextButton(this, true);
    } else
    if (watched == linePrjToolbar)
    {
      KURL url = baseUrl;
      QuantaCommon::setUrl(url, linePrjToolbar->text());
      url = QExtFileInfo::toAbsolute(url, baseUrl);
      if (!baseUrl.isParentOf(url))
      {
        KMessageBox::sorry(0,i18n("<qt>The project toolbars must be stored under the main project folder: <br><br><b>%1</b></qt>")
                                    .arg(baseUrl.prettyURL(0, KURL::StripFileProtocol)));
        linePrjToolbar->setFocus();
        emit enableNextButton(this, false);
      } else
        emit enableNextButton(this, true);
    }
  }
  return false;  
}

#include "projectnewgeneral.moc"


