/**
 * prefsmodule.cpp
 *
 * Copyright (C)  2003-2004  Zack Rusin <zack@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "prefsmodule.h"

#include <tdeaboutdata.h>
#include <kdebug.h>
#include <kcombobox.h>
#include <tdelocale.h>
#include <ktrader.h>

#include <tqlayout.h>
#include <tqlabel.h>
#include <tqbuttongroup.h>

#include <tdemacros.h>

extern "C"
{
  TDE_EXPORT TDECModule *create_komposerconfig( TQWidget *parent, const char * ) {
    return new Komposer::PrefsModule( parent, "komposerprefs" );
  }
}
using namespace Komposer;

PrefsModule::PrefsModule( TQWidget *parent, const char *name )
  : KPrefsModule( Komposer::Prefs::self(), parent, name )
{
  TQVBoxLayout *topLayout = new TQVBoxLayout( this );

  EditorSelection *editors = new EditorSelection( i18n( "Editors" ),
                                                  Komposer::Prefs::self()->m_activeEditor,
                                                  this );
  topLayout->addWidget( editors->groupBox() );

  addWid( editors );

  load();
}

const TDEAboutData*
PrefsModule::aboutData() const
{
  TDEAboutData *about = new TDEAboutData( I18N_NOOP( "komposerconfig" ),
                                      I18N_NOOP( "TDE Komposer" ),
                                      0, 0, TDEAboutData::License_LGPL,
                                      I18N_NOOP( "(c), 2003-2004 Zack Rusin" ) );

  about->addAuthor( "Zack Rusin", 0, "zack@kde.org" );;

  return about;
}


EditorSelection::EditorSelection( const TQString &text, TQString &reference,
                                  TQWidget *parent )
  : m_reference( reference )
{
  m_box = new TQGroupBox( 0, TQt::Vertical, text, parent );
  TQVBoxLayout *boxLayout = new TQVBoxLayout( m_box->layout() );
  boxLayout->setAlignment( TQt::AlignTop );

  m_editorsCombo = new KComboBox( m_box );
  boxLayout->addWidget( m_editorsCombo );

  connect( m_editorsCombo, TQ_SIGNAL(activated(const TQString&)),
           TQ_SLOT(slotActivated(const TQString&)) );
}

EditorSelection::~EditorSelection()
{
}

TQGroupBox*
EditorSelection::groupBox()  const
{
  return m_box;
}

void
EditorSelection::readConfig()
{
  m_editorsCombo->clear();

  TDETrader::OfferList editors = TDETrader::self()->query(
    TQString::fromLatin1( "Komposer/Editor" ) );
  TDETrader::OfferList::ConstIterator it;
  int i = 0;
  for ( it = editors.begin(); it != editors.end(); ++it, ++i ) {
    if ( !(*it)->hasServiceType( TQString::fromLatin1( "Komposer/Editor" ) ) )
      continue;

    TQString name = (*it)->property( "X-TDE-KomposerIdentifier" ).toString();
    m_editorsCombo->insertItem( name );
    if ( m_reference.contains( name ) )
      m_editorsCombo->setCurrentItem( i );
  }
}

void EditorSelection::writeConfig()
{
  m_reference =  m_services[ m_editorsCombo->currentText()]->
                 property( "X-TDE-KomposerIdentifier" ).toString();
}

void
EditorSelection::slotActivated( const TQString &editor )
{
  if ( !editor.isEmpty() )
    emit changed();
}

void
EditorSelection::setItem( const TQString &str )
{
  for ( int i = 0; i < m_editorsCombo->count(); ++i ) {
    if ( m_editorsCombo->text( i ) == str ) {
      m_editorsCombo->setCurrentItem( i );
      break;
    }
  }
}

#include "prefsmodule.moc"
