/***************************************************************************
                          kpersonalizer.cpp  -  description
                             -------------------
    begin                : Die Mai 22 17:24:18 CEST 2001
    copyright            : (C) 2001 by Ralf Nolden
    email                : nolden@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute 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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <unistd.h>

#include <qpushbutton.h>
#include <qlabel.h>
#include <qstring.h>
#include <qstringlist.h>
#include <qfile.h>
#include <qtimer.h>

#include <ksimpleconfig.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kapplication.h>
#include <klistview.h>
#include <krun.h>
#include <kmessagebox.h>
#include <kconfig.h>

#include <stdlib.h>

#include <kdebug.h>

#include "kpersonalizer.h"
#include "kpersonalizer.moc"

bool KPersonalizer::before_session = false;

KPersonalizer::KPersonalizer(QWidget *parent, const char *name)
    : QWizard(parent, name, true)
{
    // first, reset the startup from true (see desktop file in share/autostart) to false
    setCaption(kapp->caption());
    kapp->config()->setGroup("General");
    os_dirty = eye_dirty = style_dirty=false;
    kapp->config()->writeEntry("FirstLogin", false);
    kapp->config()->sync();

   countrypage= new KCountryPage(this);
   addPage( countrypage, i18n( "Step 1: Introduction" ) );
   setHelpEnabled(QWizard::page(0), false);

   ospage= new KOSPage(this);
   addPage(ospage, i18n( "Step 2: I want it My Way ..." ) );
   setHelpEnabled(QWizard::page(1), false);

   eyecandy= new KEyeCandyPage(this);
   addPage( eyecandy, i18n( "Step 3: Eyecandy-O-Meter" ) );
   setHelpEnabled(QWizard::page(2), false);

   stylepage= new KStylePage(this);
   addPage( stylepage, i18n( "Step 4: Everybody loves Themes" ) );
   setHelpEnabled(QWizard::page(3), false);

   refinepage=new KRefinePage(this);
   addPage( refinepage, i18n( "Step 5: Time to Refine" ) );
   setHelpEnabled(QWizard::page(4), false);

   cancelButton()->setText(i18n("S&kip Wizard"));

   setFinishEnabled(QWizard::page(4), true);

   locale = new KLocale("kpersonalizer");
   locale->setLanguage(KLocale::defaultLanguage());

   connect(ospage, SIGNAL(selectedOS(const QString&)), stylepage, SLOT(presetStyle(const QString& )));
   connect(countrypage, SIGNAL(sigRestart()), this, SLOT(slotDelayedRestart()));
}

KPersonalizer::~KPersonalizer()
{
}


void KPersonalizer::next()
{
    if(currentPage()==countrypage) {
        // only restart kp, if the language was changed and
        // if we have not already changed one of the later pages
        if ( (countrypage->b_languageChanged) && !(os_dirty || eye_dirty || style_dirty) ) {
            countrypage->save(countrypage->cb_country, countrypage->cb_language, true);
        }
        else {
            countrypage->save(countrypage->cb_country, countrypage->cb_language, false);
            QWizard::next();
        }
    }
    else if(currentPage()==ospage){
      os_dirty=true;  // set the dirty flag, changes done that need reverting
      ospage->save();
      QWizard::next();
    }
    else if(currentPage()==eyecandy){
      eye_dirty=true;  // set the dirty flag, changes done that need reverting
      eyecandy->save();
      QWizard::next();
    }
    else if(currentPage()==stylepage){
      style_dirty=true;  // set the dirty flag, changes done that need reverting
      stylepage->save();
      QWizard::next();
    }
    if(currentPage()==refinepage) {
        finishButton()->setFocus();
    }
}

void KPersonalizer::back()
{
    QWizard::back();
}

/** calls the save() functions of the abstract page classes. */
void KPersonalizer::applySettings(){
//    ospage->save();
//    eyecandy->save();
}

bool KPersonalizer::askClose(){
  QString text;
	text = i18n("Are you sure you want to quit the personalizer?\n"
                      "If not, press 'Cancel'.\n"
                      "To quit and keep the current settings, press 'Quit and Keep'.\n"
                      "To quit and revert all settings to those before KPersonalizer started, press 'Quit and Revert'.\n"
                      "This wizard can be restarted at any time by selecting 'K->System->Desktop Settings Wizard'.\n");
    int status = KMessageBox::warningYesNoCancel(this,  text, i18n("Really Quit?"), i18n("Quit and &Keep"), i18n("Quit and &Revert"));
    if(status==KMessageBox::Yes){
        return true;
    }
    if(status==KMessageBox::No){
        setDefaults();
        return true;
    }
    else
        return false;
}
/** the cancel button is connected to the reject() slot of QDialog,
 *  so we have to reimplement this here to add a dialogbox to ask if we
 *  really want to quit the wizard.
 */
void KPersonalizer::reject(){
  if (askClose()){
    exit(0);
  }
}

void KPersonalizer::closeEvent(QCloseEvent* e){
  if (askClose()){
       exit(0);
  }
  else
       e->ignore();
}

/** maybe call a dialog that the wizard has finished.
  * Calls applySettings() to save the current selection.
  */
void KPersonalizer::accept(){
  applySettings();
  exit(0);
}

/** calls all save functions after resetting all features/ OS/ theme selections to KDE default */
void KPersonalizer::setDefaults(){

// KCountryPage: The user may need his mother language anyway

  if(os_dirty)
    ospage->save(false);
  if(eye_dirty)
    eyecandy->save(false);
  if(style_dirty)
    stylepage->save(false);
}


/** restart kpersonalizer */
void KPersonalizer::slotRestart() {
    delete countrypage; countrypage = 0;
    delete ospage; ospage = 0;
    delete eyecandy; eyecandy = 0;
    delete stylepage; stylepage = 0;
    delete refinepage; refinepage = 0;

    if( !beforeSession() ){
        QString command = "kpersonalizer -r";
        QString binaryName = "kpersonalizer";
        QString icon = "kpersonalizer";
        KRun::runCommand(command, binaryName, icon);
    }

    exit(1); // exit with value 1
}

void KPersonalizer::slotDelayedRestart() {
    QTimer::singleShot(0, this, SLOT(slotRestart()));
}

/** this session is restarted, so we want to start with ospage */
void KPersonalizer::restarted(){
    showPage(ospage);
}

/** when kpersonalizer is started before KDE session, it doesn't
    offer a button for starting KControl, it also doesn't restart
    itself automatically and only exits with exitcode 1 */
void KPersonalizer::setBeforeSession(){
    before_session = true;
}
