//
// C++ Implementation: komposesettings
//
// Description:
//
//
// Author: Hans Oischinger <hans.oischinger@kde-mail.net>, (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "komposesettings.h"

#include "komposeviewmanager.h"
#include "komposetaskmanager.h"
#include "komposepreferences.h"

#include <tqcolor.h>

#include <kglobalaccel.h>
#include <tdeapplication.h>
#include <tdeconfig.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <kdebug.h>

static KomposeSettings* settingsInstance = 0;


/*
 * KomposeSettings is a singleton
 */
KomposeSettings* KomposeSettings::instance()
{
  if ( !settingsInstance )
  {
    // kdDebug() << "KomposeSettings::instance() - Creating Singleton instance"); // FIXME: How can this apper multiple times??
    settingsInstance = new KomposeSettings();
  }
  return settingsInstance;
}


KomposeSettings::KomposeSettings(TQObject *parent, const char *name)
    : TQObject(parent, name),
    dialogOpen(0),
    windowTitleFontMetrics(0)
{

  // Init global shortcut object
  globalAccel = new TDEGlobalAccel( this );
  globalAccel->insert( "showDefaultView", i18n("Show Komposé (default view)"),
                       i18n("Displays the view you have configured as default"),
                       KKey::QtWIN+Key_Tab, KKey::QtWIN+CTRL+SHIFT+Key_Tab,
                       KomposeViewManager::instance(), TQ_SLOT(createDefaultView()) );
  globalAccel->insert( "showWorldView", i18n("Show Komposé (ungrouped)"),
                       i18n("Displays all windows unsorted"),
                       CTRL+SHIFT+Key_J, KKey::QtWIN+CTRL+SHIFT+Key_J,
                       KomposeViewManager::instance(), TQ_SLOT(createWorldView()) );
  globalAccel->insert( "showVirtualDesktopView", i18n("Show Komposé (grouped by virtual desktops)"),
                       i18n("Displays all windows sorted by virtual desktops"),
                       CTRL+SHIFT+Key_I, KKey::QtWIN+CTRL+SHIFT+Key_I,
                       KomposeViewManager::instance(), TQ_SLOT(createVirtualDesktopView()) );
  globalAccel->insert( "showCurrentDesktopView", i18n("Show Komposé (current virtual desktop)"),
                       i18n("Displays all windows on the current desktop"),
                       CTRL+SHIFT+Key_K, KKey::QtWIN+CTRL+SHIFT+Key_K,
                       KomposeViewManager::instance(), TQ_SLOT(createCurrentDesktopView()) );

  // read Settings from cfg file
  readConfig();

  settingsInstance = this;
}


KomposeSettings::~KomposeSettings()
{
  delete globalAccel;
  delete settingsInstance;
}


void KomposeSettings::readConfig()
{
  kdDebug() << "KomposeSettings::readConfig()" << endl;
  // Read Shortcut Settings from config
  globalAccel->readSettings();
  globalAccel->updateConnections();

  kapp->config()->setGroup("Main window");

  defaultView = kapp->config()->readNumEntry("defaultView", KOMPOSEDISPLAY_VIRTUALDESKS);

  useComposite = kapp->config()->readBoolEntry("useComposite", false);

  passiveScreenshots = kapp->config()->readBoolEntry("passiveScreenshots", true);
  screenshotGrabDelay = kapp->config()->readNumEntry("screenshotGrabDelay", 400000000);

  // dynamicVirtDeskLayout = kapp->config()->readBoolEntry("dynamicVirtDeskLayout", true);

  imageEffects = kapp->config()->readBoolEntry("imageEffects", true);
  blurBackground = kapp->config()->readBoolEntry("blurBackground", true);

  showIcons = kapp->config()->readBoolEntry("showIcons", true);
  iconSize = kapp->config()->readNumEntry("iconSize", 3);

  showWindowTitles = kapp->config()->readBoolEntry("showWindowTitles", true );
  windowTitleFont = kapp->config()->readFontEntry("windowTitleFont", new TQFont( "arial", 11, TQFont::Bold ) );
  windowTitleFontColor = kapp->config()->readColorEntry("windowTitleFontColor", new TQColor(TQt::black) );
  showWindowTitleShadow = kapp->config()->readBoolEntry("showWindowTitleShadow", true );
  windowTitleFontShadowColor = kapp->config()->readColorEntry("windowTitleFontShadowColor", new TQColor(TQt::lightGray) );

  windowBorderActiveColor = kapp->config()->readColorEntry("windowBorderActiveColor", new TQColor(TQt::gray) );
  windowBorderInactiveColor = kapp->config()->readColorEntry("windowBorderInactiveColor", new TQColor(TQt::black) );
  windowBorderActiveWidth = kapp->config()->readNumEntry("windowBorderActiveWidth", 2 );
  windowBorderInactiveWidth = kapp->config()->readNumEntry("windowBorderInactiveWidth", 1 );
  windowBorderActiveStyle = kapp->config()->readNumEntry("windowBorderActiveStyle", 3 ); // dotted
  windowBorderInactiveStyle = kapp->config()->readNumEntry("windowBorderInactiveStyle", 1 ); // solid

  desktopTitleFont = kapp->config()->readFontEntry("desktopTitleFont", new TQFont( "arial", 10 ) );
  desktopTitleFontColor = kapp->config()->readColorEntry("desktopTitleFontColor", new TQColor(TQt::gray) );
  desktopTitleFontHighlightColor = kapp->config()->readColorEntry("desktopTitleFontHighlightColor", new TQColor(TQt::black) );

  cacheScaledPixmaps = kapp->config()->readBoolEntry("cacheScaledPixmaps", true);

  activateOnTopLeftCorner = kapp->config()->readBoolEntry("activateOnTopLeftCorner", false );
  activateOnTopRightCorner = kapp->config()->readBoolEntry("activateOnTopRightCorner", false );
  activateOnBottomLeftCorner = kapp->config()->readBoolEntry("activateOnBottomLeftCorner", false );
  activateOnBottomRightCorner = kapp->config()->readBoolEntry("activateOnBottomRightCorner", false );

  activateOnTopEdge = kapp->config()->readBoolEntry("activateOnTopEdge", false );
  activateOnBottomEdge = kapp->config()->readBoolEntry("activateOnBottomEdge", false );
  activateOnLeftEdge = kapp->config()->readBoolEntry("activateOnLeftEdge", false );
  activateOnRightEdge = kapp->config()->readBoolEntry("activateOnRightEdge", false );

  autoLockDelay = kapp->config()->readNumEntry("autoLockDelay", 1000);

  showDesktopNum = kapp->config()->readBoolEntry("showDesktopNum", false );

  viewScreen = kapp->config()->readNumEntry("viewScreen", -1);

  calcFontMetrics();
  emit settingsChanged();
}

void KomposeSettings::writeConfig()
{
  kdDebug() << "KomposeSettings::writeConfig()" << endl;

  globalAccel->writeSettings();
  globalAccel->updateConnections();

  // Read from config file
  kapp->config()->setGroup("Main window");

  kapp->config()->writeEntry("defaultView", defaultView );

  kapp->config()->writeEntry("useComposite", useComposite );

  kapp->config()->writeEntry("passiveScreenshots", passiveScreenshots );
  kapp->config()->writeEntry("screenshotGrabDelay", screenshotGrabDelay);

  // kapp->config()->writeEntry("dynamicVirtDeskLayout", dynamicVirtDeskLayout);

  kapp->config()->writeEntry("imageEffects", imageEffects);
  kapp->config()->writeEntry("blurBackground", blurBackground);

  kapp->config()->writeEntry("showIcons", showIcons);
  kapp->config()->writeEntry("iconSize", iconSize);

  kapp->config()->writeEntry("showWindowTitles", showWindowTitles);
  kapp->config()->writeEntry("windowTitleFont", windowTitleFont);
  kapp->config()->writeEntry("windowTitleFontColor", windowTitleFontColor);
  kapp->config()->writeEntry("showWindowTitleShadow", showWindowTitleShadow);
  kapp->config()->writeEntry("windowTitleFontShadowColor", windowTitleFontShadowColor);

  kapp->config()->writeEntry("windowBorderActiveColor", windowBorderActiveColor);
  kapp->config()->writeEntry("windowBorderInactiveColor", windowBorderInactiveColor );
  kapp->config()->writeEntry("windowBorderActiveWidth", windowBorderActiveWidth );
  kapp->config()->writeEntry("windowBorderInactiveWidth", windowBorderInactiveWidth );
  kapp->config()->writeEntry("windowBorderActiveStyle", windowBorderActiveStyle );
  kapp->config()->writeEntry("windowBorderInactiveStyle", windowBorderInactiveStyle );

  kapp->config()->writeEntry("desktopTitleFont", desktopTitleFont);
  kapp->config()->writeEntry("desktopTitleFontColor", desktopTitleFontColor);
  kapp->config()->writeEntry("desktopTitleFontHighlightColor", desktopTitleFontHighlightColor);

  kapp->config()->writeEntry("cacheScaledPixmaps", cacheScaledPixmaps);

  kapp->config()->writeEntry("activateOnTopLeftCorner", activateOnTopLeftCorner);
  kapp->config()->writeEntry("activateOnTopRightCorner", activateOnTopRightCorner);
  kapp->config()->writeEntry("activateOnBottomLeftCorner", activateOnBottomLeftCorner);
  kapp->config()->writeEntry("activateOnBottomRightCorner", activateOnBottomRightCorner);

  kapp->config()->writeEntry("activateOnTopEdge", activateOnTopEdge);
  kapp->config()->writeEntry("activateOnBottomEdge", activateOnBottomEdge);
  kapp->config()->writeEntry("activateOnLeftEdge", activateOnLeftEdge);
  kapp->config()->writeEntry("activateOnRightEdge", activateOnRightEdge);
  kapp->config()->writeEntry("autoLockDelay", autoLockDelay);

  kapp->config()->writeEntry("showDesktopNum", showDesktopNum);

  kapp->config()->writeEntry("viewScreen", viewScreen);

  kapp->config()->sync();

  kdDebug() << "KomposeSettings::writeConfig() - Settings saved to cfg file" << endl;

  calcFontMetrics();
  emit settingsChanged();
}


void KomposeSettings::showPreferencesDlg()
{
  dialogOpen = true;
  // popup preference dialog
  KomposePreferences *dlg = new KomposePreferences();

  dlg->exec();

  delete dlg;
  dialogOpen = false;
}

int KomposeSettings::getIconSizePixels()
{
  switch( iconSize )
  {
  case 0:
    return 16;
  case 1:
    return 32;
  case 2:
    return 64;
  case 3:
    return -1;
  }
  return 32;
}

void KomposeSettings::calcFontMetrics()
{
  if (windowTitleFontMetrics != 0)
    delete windowTitleFontMetrics;
  windowTitleFontMetrics = new TQFontMetrics( windowTitleFont );
  windowTitleFontAscent = windowTitleFontMetrics->ascent();
  windowTitleFontHeight = windowTitleFontMetrics->height();
}

#include "komposesettings.moc"
