/*************************************************************************
 *                                                                       *
 *  KTheme                                                               *
 *                                                                       *
 * This file is part of the KTheme-Package. For detail information about *
 * this program have a look at the README-File.                          *
 *                                                                       *
 * Copyright(c) 1998 Christian Poulter.                                  *
 *                                                                       *
 * 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 - version 2.                             *
 *                                                                       *
 * This program 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 General Public License in file COPYING for more details.          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program; if not, write to the Free Software           *
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             *
 *                                                                       *
 *  On the Net at http://www.wilhelmshaven-online.de/benutzer/cpoulter   *
 *  Email to cpoulter@ebs-net.com                                        *
 *                                                                       *
 *************************************************************************/

#include <qdir.h>
#include <qgrpbox.h>
#include <qimage.h>
#include <qlistbox.h>
#include <qlabel.h>
#include <qradiobt.h>

#include <ksimpleconfig.h>
#include <klocale.h>
#include <kwm.h>

#include "kthemegfxconfig.h"
#include "jpeg.h"

static bool GUI;

KThemeGfxConfig::~KThemeGfxConfig (){
	if (GUI){
	}
}

KThemeGfxConfig::KThemeGfxConfig (QWidget * parent, const char *name, bool init):KConfigWidget (parent, name){
	QImageIO::defineIOHandler("JFIF","^\377\330\377\340",0, read_jpeg_jfif, NULL);	

	if (init)
		GUI = FALSE;
	else
		GUI = TRUE;
	
	if (GUI){
		desktopgroup=new QGroupBox(klocale->translate("Desktop"),this);
		desktopgroup->resize(200,90);	

		desktop = new QListBox(desktopgroup, "listBox" );
		desktop->setGeometry(20,20,160,60);	
		desktop->clear();
		desktop->setAutoUpdate( FALSE );

		if (KWM::isKWMInitialized()){
			desktops=KWM::numberOfDesktops();
			for (int i = 0; i < desktops; i++ ) desktop->insertItem( KWM::getDesktopName(i+1) );
			desktop->setCurrentItem(KWM::currentDesktop() - 1);
    		}else{
			desktops=0;
			desktop->insertItem("Default");
			desktop->setCurrentItem(0);
		}
		desktop->setAutoUpdate( TRUE );    
		loadSettings();
	
		wallpapergroup=new QGroupBox(klocale->translate("Wallpaper"),this);
		wallpapergroup->resize(200,90);

		wallpaperbuttongroup = new QButtonGroup( this );
		wallpaperbuttongroup->hide();
		wallpaperbuttongroup->setExclusive( true );

		wprb1 = new QRadioButton( klocale->translate("Ti&led"), wallpapergroup );
		wprb1->move(20,20);
		wprb1->adjustSize();
		wallpaperbuttongroup->insert( wprb1, 1);
    		
		wprb2 = new QRadioButton( klocale->translate("&Centred"),wallpapergroup);
		wprb2->move(20,40);
		wprb2->adjustSize();
		wallpaperbuttongroup->insert( wprb2,2);
	
		wprb3 = new QRadioButton( klocale->translate("&Scaled"),wallpapergroup);
		wprb3->move(20,60);
		wprb3->adjustSize();
		wallpaperbuttongroup->insert( wprb3,3);		

		gfx_listbox = new QListBox( this, "listBox" );
		gfx_listbox->resize(200,200);
		gfx_listbox->clear();
		gfx_listbox->setAutoUpdate( FALSE );

		QDir d(path_pic);
		gfx_listbox->insertItem(klocale->translate("Keine Grafik"));
		for ( unsigned int i=0; i<d.count(); i++ ) 
			if ( (strcmp(d[i],"..")!=0) && (strcmp(d[i],".")!=0) ) 
				gfx_listbox->insertItem(d[i]);
		gfx_listbox->setAutoUpdate(TRUE);

		preview = new QLabel(this);
		preview->setText(klocale->translate("Couldnīt load pic!"));
		preview->resize(200,200);	
		preview->setAlignment( AlignCenter );

		connect( gfx_listbox, SIGNAL(highlighted(int)),SLOT(loadImage(int)));
		connect( desktop, SIGNAL(highlighted(int)),SLOT(setDesktop()));
		connect( wprb1, SIGNAL(clicked()),SLOT(buttonSelect()));
		connect( wprb2, SIGNAL(clicked()),SLOT(buttonSelect()));
		connect( wprb3, SIGNAL(clicked()),SLOT(buttonSelect()));

		resize(450,430);
		setMinimumSize(size());
	
		setDesktop();
	}
}

void KThemeGfxConfig::resizeEvent(QResizeEvent *){
	gfx_listbox->setGeometry(20,20,width()-250,height()-40);
	preview->move(width()-220,20);
	desktopgroup->move(width()-220,240);	
	wallpapergroup->move(width()-220,340);	
}

void KThemeGfxConfig::loadSettings(){
	char group[80];
	KConfigBase *config;

	path_pic=KApplication::kde_datadir()+"/ktheme/wallpapers";

	config=new KSimpleConfig( KApplication::localconfigdir() + "/kdisplayrc" );

	QDir d(path_pic);
	unsigned int j=1;
	picentrys=d.count();
	for ( unsigned int i=0; i<picentrys; i++ ) 
		if ( (strcmp(d[i],"..")!=0) && (strcmp(d[i],".")!=0) ){
			wpnames[j]=d[i];
			j++;
		}

	for (int i=0;i<desktops;i++){
		tile[i]=false;center[i]=false;scale[i]=false;wpnum[i]=0;

		sprintf( group, "Desktop%d",i+1);
		config->setGroup(group);
	
		QString s=config->readEntry("WallpaperMode");
		if (!s.isNull()){
			if (s=="Tiled") tile[i]=true;
			else if (s=="Centred") center[i]=true;
			else if (s=="Scaled") scale[i]=true;
		}else tile[i]=true;
	
		s=config->readEntry("Wallpaper");

		for (j=1;j<picentrys;j++){
			QString tmp=path_pic+"/"+wpnames[j];
			if (tmp==s) wpnum[i]=j;
		}
	}
}

void KThemeGfxConfig::writeSettings(){
	char group[80];
	KConfigBase *config;

	config=new KSimpleConfig( KApplication::localconfigdir() + "/kdisplayrc" );
	
	for (int i=0;i<desktops;i++){
		sprintf( group, "Desktop%d",i+1);
		config->setGroup(group);
	
		QString tmp;
		if (wpnum[i]==0) {
			tmp="Keine Grafik";
			config->writeEntry("UseWallpaper", false);
		}else{
			tmp=path_pic+"/"+wpnames[wpnum[i]];
			config->writeEntry("UseWallpaper", true);
		}

		config->writeEntry("Wallpaper", tmp);
		config->writeEntry("WallpaperMode", "Tiled" );

		if (tile[i]) config->writeEntry( "WallpaperMode", "Tiled" );
		if (center[i]) config->writeEntry( "WallpaperMode", "Centred" );
		if (scale[i]) config->writeEntry( "WallpaperMode", "Scaled" );
	}
	config->sync();
}

void KThemeGfxConfig::buttonSelect(){
	int d=desktop->currentItem();
	tile[d]=wprb1->isChecked();
	center[d]=wprb2->isChecked();
	scale[d]=wprb3->isChecked();
}

void KThemeGfxConfig::setDesktop(){
	int d=desktop->currentItem();
	wprb1->setChecked(tile[d]);
	wprb2->setChecked(center[d]);
	wprb3->setChecked(scale[d]);
	gfx_listbox->setCurrentItem(wpnum[d]);
}

void KThemeGfxConfig::applySettings(){
	QApplication::setOverrideCursor( waitCursor );
	writeSettings();
	QString cmd = "kbgwm_reconfigure";
	KWM::sendKWMCommand(cmd);
	QApplication::restoreOverrideCursor();
}

void KThemeGfxConfig::loadImage(int index){
		QApplication::setOverrideCursor( waitCursor );
		QString tmp=gfx_listbox->text(index);
		wpnum[desktop->currentItem()]=index;
		QImageIO io(path_pic+"/"+tmp,0);
		if (io.read()) {
			QPixmap tmp,tmp2;
			tmp=io.image();
			tmp2.resize(preview->width(),preview->height());

			float sx = (float)preview->width() / tmp.width();
			float sy = (float)preview->height() / tmp.height();
			
			QWMatrix matrix;
			matrix.scale( sx, sy );
			bitBlt( &tmp2, 0, 0, &tmp.xForm( matrix ) ); 

			preview->setAlignment( AlignCenter );
		 	preview->setPixmap(tmp2);
			preview->repaint();
		}else 
			if (index!=0){
				preview->setText(klocale->translate("Couldnīt load Picture!"));
			}else preview->setText(klocale->translate("No Picture available!"));
		QApplication::restoreOverrideCursor();
}


