//
// C++ Implementation: aboutdialog
//
// Description: 
//
//
// Author: Andras Mantia <amantia@kdewebdev.org>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <tqfile.h>
#include "aboutdialog.h"

#include "kommanderplugin.h"
#include "specials.h"

#include <tdeaboutdata.h>
#include <tdeaboutapplication.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <kstandarddirs.h>

#include <tqtextstream.h>

enum Functions {
  FirstFunction = 159,
  Initialize,
  AddAuthor,
  AddTranslator,
  SetDescription,
  SetHomepage,
  SetBugAddress,
  SetLicense,
  Version,
  LastFunction
};

AboutDialog::AboutDialog(TQWidget *parent, const char *name)
 : TQLabel(parent, name), KommanderWidget(TQT_TQOBJECT(this))
{
  TQStringList states;
  states << "default";
  setStates(states);
  setDisplayStates(states);
  if (KommanderWidget::inEditor)
  {
    setPixmap(TDEGlobal::iconLoader()->loadIcon("kommander", TDEIcon::NoGroup, TDEIcon::SizeMedium));
    setFrameStyle(TQFrame::Box | TQFrame::Plain);
    setLineWidth(1);
    setFixedSize(pixmap()->size());
  }
  else
    setHidden(true);

  m_aboutData = 0L; 
  KommanderPlugin::setDefaultGroup(Group::DCOP);
  KommanderPlugin::registerFunction(Initialize, "initialize(TQString widget, TQString appName, TQString icon, TQString version, TQString copyright)",
         i18n("Sets information about the application. This is the first method that must me called, any addition to the dialog done before initialization will be ignored."), 5);
  KommanderPlugin::registerFunction(AddAuthor, "addAuthor(TQString widget, TQString author, TQString task, TQString email, TQString webAddress)",
         i18n("Add an author. Only the author name is required."), 2, 5);
   KommanderPlugin::registerFunction(AddTranslator, "addTranslator(TQString widget, TQString author, TQString email)",
         i18n("Add a translator. Only the name is required."), 2, 3);
   KommanderPlugin::registerFunction(SetDescription, "setDescription(TQString widget, TQString description)",
         i18n("Set a short description text."), 2);
   KommanderPlugin::registerFunction(SetHomepage, "setHomepage(TQString widget, TQString homepage)",
         i18n("Set a homepage address."), 2);
   KommanderPlugin::registerFunction(SetBugAddress, "setBugAddress(TQString widget, TQString address)",
         i18n("Set an email address, where bugs can be reported."), 2);
   KommanderPlugin::registerFunction(SetLicense, "setLicense(TQString widget, TQString license)",
         i18n("Sets license information of the application. The parameter can be one of the license keys - GPL_V2, LGPL_V2, BSD, ARTISTIC -, or a freely specified text."), 2);
   KommanderPlugin::registerFunction(Version, "version(TQString widget)",
         i18n("Returns the set version string."), 1);
}


AboutDialog::~AboutDialog()
{
  delete m_aboutData;
  m_aboutData = 0L;
}

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

bool AboutDialog::isKommanderWidget() const
{
  return true;
}
TQStringList AboutDialog::associatedText() const
{
  return KommanderWidget::associatedText();
}

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


bool AboutDialog::isFunctionSupported(int f)
{
  return (f > FirstFunction && f < LastFunction) || f == DCOP::execute;
}

void AboutDialog::initialize(const TQString& appName, const TQString &icon, const TQString& version, const TQString& copyright)
{
  delete m_aboutData;  
  m_authors.clear();
  m_emails.clear();
  m_tasks.clear();
  m_addresses.clear();
  m_description = "";
  m_homepage = "";
  m_bugaddress = "";
  m_version = version;
  m_appName = appName;
  m_icon = icon;
  m_copyright = copyright;
  m_aboutData = new TDEAboutData(m_appName.utf8(), m_appName.utf8(), m_version.utf8());
  m_aboutData->setCopyrightStatement(m_copyright.utf8());
  if (!m_icon.isEmpty())
    m_aboutData->setProgramLogo(TDEGlobal::iconLoader()->loadIcon(m_icon, TDEIcon::NoGroup, TDEIcon::SizeMedium).convertToImage());  
}

void AboutDialog::addAuthor(const TQString& author, const TQString &task, const TQString& email, const TQString &webAddress)
{
  if (!m_aboutData)
    return;
  m_authors.append(author);
  m_emails.append(email);
  m_tasks.append(task);
  m_addresses.append(webAddress);
  m_aboutData->addAuthor(author.utf8(), task.utf8(), email.utf8(), webAddress.utf8());
}

void AboutDialog::addTranslator(const TQString& author, const TQString& email)
{
  if (!m_aboutData)
    return;
  m_authors.append(author);
  m_emails.append(email);
  m_aboutData->setTranslator(author.utf8(), email.utf8());
}

void AboutDialog::setDescription(const TQString& description)
{
  if (!m_aboutData)
    return;
  m_description = description;
  m_aboutData->setShortDescription(m_description.utf8());
}

void AboutDialog::setHomepage(const TQString &homepage)
{
  if (!m_aboutData)
    return;
  m_homepage = homepage;
  m_aboutData->setHomepage(m_homepage.utf8());
}

void AboutDialog::setBugAddress(const TQString &bugAddress)
{
  if (!m_aboutData)
    return;
  m_bugaddress = bugAddress;
  m_aboutData->setBugAddress(m_bugaddress.utf8());
}

void AboutDialog::setLicense(const TQString &key)
{
  if (!m_aboutData)
    return;
  TQString license = key.upper();
  TQString file;
  if (key == "GPL_V2")
  {
    file = locate("data", "LICENSES/GPL_V2");
  } else
  if (key == "LGPL_V2")
  {
    file = locate("data", "LICENSES/LGPL_V2");    
  } else
  if (key == "BSD")
  {
    file = locate("data", "LICENSES/BSD");
  } else
  if (key == "ARTISTIC")
  {
    file = locate("data", "LICENSES/ARTISTIC");
  }
  if (file.isEmpty() && !TQFile::exists( key ))
  {
    if (!key.isEmpty())
      m_license = key;
      m_aboutData->setLicenseText(m_license.utf8());
  }
  else
  {
    if (file.isEmpty())
    {
      if (!key.isEmpty())
        file = key;
    }
    m_aboutData->setLicenseTextFile(file.utf8());
  }

}

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

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

void AboutDialog::populate()
{
  setAssociatedText(KommanderWidget::evalAssociatedText( populationText()));
}

void AboutDialog::execute()
{
  if (m_aboutData)
  {        
    TDEAboutApplication dialog(m_aboutData, this);
    dialog.exec();
  }
}

TQString AboutDialog::handleDCOP(int function, const TQStringList& args)
{
  switch (function) {
    case Initialize:
    {
      initialize(args[0], args[1], args[2], args[3]);
      break;
    }
    case SetLicense:
    {
      setLicense(args[0]);
      break;
    }
    case AddAuthor:
    {
      addAuthor(args[0], args[1], args[2], args[3]);
      break;
    }
    case AddTranslator:
    {
      addTranslator(args[0], args[1]);
      break;
    }
    case SetDescription:
    {
      setDescription(args[0]);
      break;
    }
    case SetHomepage:
    {
      setHomepage(args[0]);
      break;
    }
    case SetBugAddress:
    {
      setBugAddress(args[0]);
      break;
    }
    case Version:
    {
      if (m_aboutData)
        return m_aboutData->version();
      break;
    }
    case DCOP::execute:
    {
      if (m_aboutData)
      {        
        TDEAboutApplication dialog(m_aboutData, this);
        dialog.exec();
      }
      break;
    }
    default:
      return KommanderWidget::handleDCOP(function, args);
  }
  return TQString();
}

#include "aboutdialog.moc"
