/***************************************************************************
*                                                                         *
*   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; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************/

#include "prefsdialogimpl.h"
#include "parser.h"
#include "kdevmonconfig.h"
#include "frames.h"
#include "statsframe.h"
#include "kdevmon.h"

#include <cmath>
#include <iostream>

#include <qtimer.h>
#include <qstring.h>
#include <qlayout.h>
#include <qdir.h>
#include <qmessagebox.h>
#include <Qt3Support/Q3PopupMenu>
#include <Qt3Support/Q3ValueList>
#include <QtGui/QMouseEvent>

#include <klocale.h>
// #include <kwin.h>
#include <kstandarddirs.h>
#include <kglobal.h>
#include <kaboutdata.h>
#include <kfiledialog.h>
#include <kdebug.h>
#include <kapplication.h>
#include <khelpmenu.h>
#include <kmenu.h>
#include <kaboutapplicationdialog.h>


Kdevmon::Kdevmon(KApplication *, QWidget* , const char *)
{
	m_mainFrame = 0L;
	m_dockFrame = 0L;
	m_bpsInFrame = 0L;
	m_bpsOutFrame = 0L;
	m_barFrame = 0L;
	m_statsFrame = 0L;
	m_parser = 0L;
	m_timer = 0L;
	m_prefsDialog = 0L;
	m_popupMenu = 0L;
	m_config = 0L;

        // create frames and layout
        m_mainLayout = new QVBoxLayout( this );
        m_topFrame = new QFrame( this, "topFrame" );
        m_topFrame->setFrameStyle( QFrame::Panel | QFrame::Raised );
        m_topFrame->setFixedHeight( 20 );
        m_topFrame->installEventFilter( this );
        QGridLayout *topFrameLayout = new QGridLayout(
                m_topFrame, 1, 3, 2, -1, "topFrameLayout" );
        topFrameLayout->setColStretch( 1, 1 );
        m_mainLayout->addWidget( m_topFrame );

        // create child frames
        m_dockFrame = new DockFrame( this);
        m_dockFrame->installEventFilter( this );

        m_bpsInFrame = new BpsFrame( m_topFrame );
        m_bpsInFrame->setFrameStyle( QFrame::Panel | QFrame::Sunken );
        m_bpsInFrame->setFixedWidth( 50 );
	
        topFrameLayout->addWidget( m_bpsInFrame, 0, 0 );
	

        m_bpsOutFrame = new BpsFrame (m_topFrame);
        m_bpsOutFrame->setFrameStyle (QFrame::Panel | QFrame::Sunken);
        m_bpsOutFrame->setFixedWidth (50);
        topFrameLayout->addWidget (m_bpsOutFrame, 0, 2);

        m_barFrame = new BarFrame(this);
        m_barFrame->setFrameStyle( QFrame::Sunken | QFrame::Panel );

        m_mainFrame = new MainFrame( this );
        m_mainFrame->setFrameStyle( QFrame::Sunken | QFrame::Panel );
        m_mainFrame->installEventFilter( this );

        QHBoxLayout * lowerLayout = new QHBoxLayout (m_mainLayout, 3);
        lowerLayout->addWidget (m_mainFrame);
        lowerLayout->addWidget (m_barFrame);

        m_statsFrame = new StatsFrame( 0 );

        // create timer
        m_timer = new QTimer(this);

        m_popupMenu = initPopupMenu();

        // connect signals and slots
        QObject::connect( m_timer, SIGNAL(timeout()), this, SLOT(processTimerEvent()) );
        QObject::connect( m_popupMenu, SIGNAL(activated(int)), this, SLOT(commandCallback(int)));
        QObject::connect( m_popupMenu, SIGNAL(highlighted(int)), this, SLOT(statusCallback(int)));
        initFromConfig();

        m_prefsDialog = initPrefsDialog();

}

Kdevmon::~Kdevmon()
{}

void Kdevmon::initFromConfig()
{
// 	if( m_config == 0L )
// 	{
		KdevmonConfig kc = KdevmonConfig();
		kc.config = KGlobal::config().data();
		m_config = kc.getConfig();
// 	}

// 	if( m_config == 0L )
// 	{
// 		std::cout << "error getting config " << std::endl;
// 		return;
// 	}	

	if( static_cast<int>(m_bytesIn.size()) !=
		m_config->getCommonConfig()->smoothFactor )
	{
		m_lastBytesIn = 0; m_lastBytesOut = 0;
		m_bytesIn.resize( m_config->getCommonConfig()->smoothFactor );
		m_bytesOut.resize( m_config->getCommonConfig()->smoothFactor );
		m_bytesIn.fill( 0 );
		m_bytesOut.fill( 0 );
		m_maxByteRateIn = 0; m_maxByteRateOut = 0;
		m_lastPacketsIn = 0; m_lastPacketsOut = 0;
		m_packetsIn.resize( m_config->getCommonConfig()->smoothFactor );
		m_packetsOut.resize( m_config->getCommonConfig()->smoothFactor );
		m_packetsIn.fill( 0 );
		m_packetsOut.fill( 0 );
		m_maxPacketRateIn = 0; m_maxPacketRateOut = 0;
	}
	
	if( m_parser != 0L )
	{
		if( m_parser->getDevice() != m_config->getGraphConfig()->device )
		{
			delete m_parser;
			m_parser = new ProcParser(
				"/proc/net/dev", m_config->getGraphConfig()->device);
			m_parser->setupNetStats();
		}
	}
	else
	{
		m_parser = new ProcParser(
			"/proc/net/dev", m_config->getGraphConfig()->device);
		m_parser->setupNetStats();
	}
	QString caption = "[" + m_config->getGraphConfig()->device + "] kdevmon";
// 	setCaption (caption);
	
	m_mainFrame->initFromConfig();
	m_mainFrame->show();
	
	m_statsFrame->initFromConfig();

	m_dockFrame->initFromConfig();
	if( m_config->getDockFrameConfig()->isVisible )
		m_dockFrame->show();
	else
		m_dockFrame->hide();

	m_bpsInFrame->setPixmapColor( m_config->getMainFrameConfig()->bgColor );
	m_bpsOutFrame->setPixmapColor( m_config->getMainFrameConfig()->bgColor );

	m_barFrame->initFromConfig();

	if( m_timer != 0L )
		m_timer->stop();
	else
		m_timer = new QTimer( this );
	m_timer->start( m_config->getCommonConfig()->refreshTime );
	
	if( m_config->getMainFrameConfig()->showStatsBar )
		m_topFrame->show();
	else
		m_topFrame->hide();
		
	if( m_config->getMainFrameConfig()->showDecorations )
	{
		setFrameStyle( QFrame::NoFrame | QFrame::Plain );
		setLineWidth(0);
		m_mainLayout->setMargin( 0 );
		clearMask();
		if( isVisible() )
		{
			hide();
			show();
		}
	}
	else
	{
		setFrameStyle( QFrame::Panel | QFrame::Sunken );
		setLineWidth( BORDERWIDTH );
		m_mainLayout->setMargin( BORDERWIDTH );
		setMask( QRegion(rect(), QRegion::Rectangle) );
		if( isVisible() )
		{
			hide();
			show();
		}
	}
    setGeometry( m_config->getMainFrameConfig()->geometry );
}

Q3PopupMenu *Kdevmon::initPopupMenu()
{
	Q3PopupMenu *menu = new Q3PopupMenu();
	
	menu->insertItem( i18n("Clear"), Common::MENU_CLEAR );
	menu->insertItem( i18n("Show dock window"), Common::MENU_SHOW_DOCK );
	menu->insertItem( i18n("Show main window"), Common::MENU_SHOW_MAIN );
	menu->insertItem( i18n("Show stats window"), Common::MENU_SHOW_STATS );
	menu->insertSeparator();
	menu->insertItem( i18n("Preferences..."), Common::MENU_PREFS );
	menu->insertItem( i18n("Load..."), Common::MENU_LOAD );
	menu->insertItem( i18n("Save"), Common::MENU_SAVE );
	menu->insertItem( i18n("Save as..."), Common::MENU_SAVE_AS );
	menu->insertSeparator();
	menu->insertItem( i18n("About"), Common::MENU_ABOUT );
	menu->insertItem( i18n("About KDE"), Common::MENU_ABOUTKDE );
	menu->insertItem( i18n("Help"), Common::MENU_HELP );
	menu->insertSeparator();
	menu->insertItem( i18n("Quit"), Common::MENU_QUIT );
	m_dockFrame->setContextMenu(menu);
	return menu;
}

PrefsDialogImpl *Kdevmon::initPrefsDialog()
{
	PrefsDialogImpl *dlg = new PrefsDialogImpl( this );
	Q3MemArray< int > refreshTimeChoices(7);
	refreshTimeChoices[0] = 100;
	refreshTimeChoices[1] = 200;
	refreshTimeChoices[2] = 500;
	refreshTimeChoices[3] = 1000;
	refreshTimeChoices[4] = 2000;
	refreshTimeChoices[5] = 5000;
	refreshTimeChoices[6] = 10000;
	dlg->setRefreshTimeChoices( refreshTimeChoices );
	dlg->setRefreshTimeRange( 50, 100000 );
	Q3MemArray< int > smoothFactorChoices(4);
	smoothFactorChoices[0] = 1;
	smoothFactorChoices[1] = 2;
	smoothFactorChoices[2] = 4;
	smoothFactorChoices[3] = 8;
	dlg->setSmoothFactorChoices( smoothFactorChoices );
	dlg->setSmoothFactorRange( 1, 20 );
	QVector< QString > deviceChoices(4);
	deviceChoices.insert(0, QString("eth0") );
	deviceChoices.insert(1, QString("eth1") );
	deviceChoices.insert(2, QString("ppp0") );
	deviceChoices.insert(3, QString("eth2") );
	dlg->setDeviceChoices( deviceChoices );
	Q3MemArray< long > scalingChoices(9);
	scalingChoices[0] = 33600;
	scalingChoices[1] = 56000;
	scalingChoices[2] = 64000;
	scalingChoices[3] = 128000;
	scalingChoices[4] = 256000;
	scalingChoices[5] = 500000;
	scalingChoices[6] = 1000000;
	scalingChoices[7] = 10000000;
	scalingChoices[8] = 100000000;
	dlg->setScalingChoices( scalingChoices );
	dlg->setScalingRange( 1000, 2000000000 );

	return dlg;
}

void Kdevmon::commandCallback(int id) {
	switch(id)
	{
		case Common::MENU_CLEAR:
			setClear(); break;
		case Common::MENU_SHOW_DOCK:
			setDockFrameVisible( ! m_dockFrame->isVisible() ); break;
		case Common::MENU_SHOW_MAIN:
			setMainFrameVisible( ! isVisible() ); break;
		case Common::MENU_SHOW_STATS:
			setStatsFrameVisible( ! m_statsFrame->isVisible() ); break;
		case Common::MENU_PREFS:
			preferences(); break;
		case Common::MENU_LOAD:
			load(); break;
		case Common::MENU_SAVE:
			save(); break;
		case Common::MENU_SAVE_AS:
			saveAs(); break;
		case Common::MENU_HELP:
			help(); break;
		case Common::MENU_ABOUT:
			about(); break;
		case Common::MENU_ABOUTKDE:
			aboutkde(); break;
		case Common::MENU_QUIT:
			quit(); break;
	}
}

void Kdevmon::statusCallback(int) {
	// creating tooltips
}

void Kdevmon::setClear() {
	m_mainFrame->clear();
	m_dockFrame->clear();
	m_barFrame->clear();

	m_bytesIn.fill(0); m_bytesOut.fill(0);
	m_lastBytesIn = 0; m_lastBytesOut = 0;
	m_maxByteRateIn = 0; m_maxByteRateOut = 0;
	m_packetsIn.fill(0); m_packetsOut.fill(0);
	m_lastPacketsIn = 0; m_lastPacketsOut = 0;
	m_maxPacketRateIn = 0; m_maxPacketRateOut = 0;
	m_parser->setupNetStats();
}

void Kdevmon::load()
{
	QDir cdUpDir( KGlobal::dirs()->localkdedir() + "/share/apps" );
	bool dirExists = cdUpDir.exists( "kdevmon" );
	if( ! dirExists )
	{
		dirExists = cdUpDir.mkdir( "kdevmon" );
	}
	if( ! dirExists )
	{
// 		kdDebug() << "The directory " + cdUpDir.path() + "/kdevmon does not exist."			<< endl;
		return;
	}
	QString dirName = cdUpDir.path() + "/kdevmon";
//     kdDebug() << dirName << endl;
	QString fileName = KFileDialog::getOpenFileName( dirName, "*rc" );
	if( fileName != QString::null )
	{
		m_config->getCommonConfig()->configFile = fileName;
		m_config->readSettings();
		initFromConfig();
	}
}

void Kdevmon::save() {
// 	// create dir if necessary
// 	QDir cdUpDir( KGlobal::dirs()->localkdedir() + "/share/apps" );
// 	if( ! cdUpDir.exists("kdevmon") )
// 		cdUpDir.mkdir("kdevmon");
	
	// save entries that are not automatically saved
	m_config->getMainFrameConfig()->isVisible = isVisible();
	m_config->getMainFrameConfig()->geometry = geometry();
	m_config->getStatsFrameConfig()->position = m_statsFrame->pos();
	m_config->writeSettings();
}

void Kdevmon::saveAs()
{
	QDir cdUpDir( KGlobal::dirs()->localkdedir() + "/share/apps" );
	if( ! cdUpDir.exists("kdevmon") )
		cdUpDir.mkdir("kdevmon");
	QString dirName = cdUpDir.path() + "/kdevmon";
//     kdDebug() << dirName << endl;
	QString fileName = KFileDialog::getSaveFileName( dirName, "*rc" );
	if( fileName != QString::null )
	{
		m_config->getCommonConfig()->configFile = fileName;
		save();
	}
}

void Kdevmon::preferences()
{
	m_config->getMainFrameConfig()->geometry = geometry();
	m_config->getMainFrameConfig()->isVisible = isVisible();
	m_config->getDockFrameConfig()->isVisible = m_dockFrame->isVisible();
	m_config->getStatsFrameConfig()->position = m_statsFrame->pos();
	
	m_prefsDialog->setRefreshTime( m_config->getCommonConfig()->refreshTime );
	m_prefsDialog->setSmoothFactor( m_config->getCommonConfig()->smoothFactor );
	m_prefsDialog->setUnit( m_config->getCommonConfig()->unit );
	m_prefsDialog->setIncomingColor( m_config->getCommonConfig()->colorIn );
	m_prefsDialog->setOutgoingColor( m_config->getCommonConfig()->colorOut );
	m_prefsDialog->setDevice( m_config->getGraphConfig()->device );
	m_prefsDialog->setScaling( m_config->getGraphConfig()->scaling );
	m_prefsDialog->setScalingMode( m_config->getGraphConfig()->scalingMode );
	m_prefsDialog->setGraphPosition( m_config->getGraphConfig()->graphPosition );
	m_prefsDialog->setGraphStyle( m_config->getGraphConfig()->graphStyle );
	m_prefsDialog->setShowGrid( m_config->getGraphConfig()->showGrid );
	m_prefsDialog->setMainFrameBgColor( m_config->getMainFrameConfig()->bgColor );
	m_prefsDialog->setMainFrameGridColor( m_config->getMainFrameConfig()->gridColor );
	m_prefsDialog->setMainFrameShowTooltips( m_config->getMainFrameConfig()->showTooltips );
	m_prefsDialog->setMainFrameShowStatsBar( m_config->getMainFrameConfig()->showStatsBar );
	m_prefsDialog->setMainFrameTransparent( m_config->getMainFrameConfig()->transparent );
	m_prefsDialog->setMainFrameShowDecorations( m_config->getMainFrameConfig()->showDecorations );
	m_prefsDialog->setDockFrameBgColor( m_config->getDockFrameConfig()->bgColor );
	m_prefsDialog->setDockFrameGridColor( m_config->getDockFrameConfig()->gridColor );
	m_prefsDialog->setDockFrameShowText( m_config->getDockFrameConfig()->showText );
	m_prefsDialog->setDockFrameTextSize( m_config->getDockFrameConfig()->TextSize );
	m_prefsDialog->setStatsFrameBgColor( m_config->getStatsFrameConfig()->bgColor );
	
	if( m_prefsDialog->exec() )
	{
		m_config->getCommonConfig()->refreshTime = m_prefsDialog->getRefreshTime();
		m_config->getCommonConfig()->smoothFactor = m_prefsDialog->getSmoothFactor();
		m_config->getCommonConfig()->unit = m_prefsDialog->getUnit();
		m_config->getCommonConfig()->colorIn = m_prefsDialog->getIncomingColor();
		m_config->getCommonConfig()->colorOut = m_prefsDialog->getOutgoingColor();
		m_config->getGraphConfig()->device = m_prefsDialog->getDevice();
		m_config->getGraphConfig()->scaling = m_prefsDialog->getScaling();
		m_config->getGraphConfig()->scalingMode = m_prefsDialog->getScalingMode();
		m_config->getGraphConfig()->graphPosition = m_prefsDialog->getGraphPosition();
		m_config->getGraphConfig()->graphStyle = m_prefsDialog->getGraphStyle();
		m_config->getGraphConfig()->showGrid = m_prefsDialog->getShowGrid();
		m_config->getMainFrameConfig()->bgColor = m_prefsDialog->getMainFrameBgColor();
		m_config->getMainFrameConfig()->gridColor = m_prefsDialog->getMainFrameGridColor();
		m_config->getMainFrameConfig()->showTooltips = m_prefsDialog->getMainFrameShowTooltips();
		m_config->getMainFrameConfig()->showStatsBar = m_prefsDialog->getMainFrameShowStatsBar();
		m_config->getMainFrameConfig()->transparent = m_prefsDialog->getMainFrameTransparent();
		m_config->getMainFrameConfig()->showDecorations = m_prefsDialog->getMainFrameShowDecorations();
		m_config->getDockFrameConfig()->bgColor = m_prefsDialog->getDockFrameBgColor();
		m_config->getDockFrameConfig()->gridColor = m_prefsDialog->getDockFrameGridColor();
		m_config->getDockFrameConfig()->showText = m_prefsDialog->getDockFrameShowText();
		m_config->getDockFrameConfig()->TextSize = m_prefsDialog->getDockFrameTextSize();
		m_config->getStatsFrameConfig()->bgColor = m_prefsDialog->getStatsFrameBgColor();
		
		initFromConfig();
	}
}

void Kdevmon::about()
{
	const KAboutData *about= KGlobal::mainComponent().aboutData();
	KAboutApplicationDialog  *aboutdlg = new KAboutApplicationDialog (about,(QWidget *)this);
	aboutdlg->show();
}

void Kdevmon::aboutkde()
{
	KHelpMenu *m_help = new KHelpMenu( 0, KGlobal::mainComponent().aboutData() );
	m_help-> aboutKDE();
}

void Kdevmon::help()
{
// 	KApplication::kApplication()->invokeHelp();
}

void Kdevmon::quit() {
	save();
	KApplication::kApplication()->quit();
}

void Kdevmon::closeEvent(QCloseEvent *event)
{
	hide();
	event->ignore();
}

void Kdevmon::resizeEvent( QResizeEvent * )
{
	if( m_config->getMainFrameConfig()->showDecorations )
		clearMask();
	else
		setMask( QRegion(rect(), QRegion::Rectangle) );
}

bool Kdevmon::eventFilter( QObject *o, QEvent *e )
{
	if( (o == m_mainFrame) || (o == m_topFrame)  )
	{
		if( e->type() == QEvent::MouseButtonPress )
		{
			QMouseEvent *me = static_cast<QMouseEvent*>(e);
			if( me->button() == Qt::RightButton )
				showPopupMenu( me->globalPos() );
			else if( me->button() == Qt::MidButton )
				setStatsFrameVisible( ! m_statsFrame->isVisible() );
			return true;
		}
	}
	else if( o == m_dockFrame )
	{
		if( e->type() == QEvent::MouseButtonPress )
		{
			QMouseEvent *me = static_cast<QMouseEvent*>(e);
			if( me->button() == Qt::RightButton )
				showPopupMenu( me->globalPos() );
			else if( me->button() == Qt::MidButton )
				setStatsFrameVisible( ! m_statsFrame->isVisible() );
			else if( me->button() == Qt::LeftButton )
				setMainFrameVisible( ! isVisible() );
			return true;
		}
	}
	return false;
}

void Kdevmon::processTimerEvent()
{
	static int t=0;
	ParserBytes diffBytes;
	ParserBytes totalBytes;
	ParserPackets diffPackets;
	ParserBytes totalPackets;
	bool success = m_parser->updateNetStats();
	
	if( success )
	{
		diffBytes = m_parser->getDiffBytes();
//		totalBytes = m_parser->getTotalBytes();
		totalBytes = m_parser->getDiffBytes();
		diffPackets = m_parser->getDiffPackets();
//		totalPackets = m_parser->getTotalPackets();
		totalPackets = m_parser->getDiffPackets();
	}
	else
	{
		diffBytes.in = 0;
		diffBytes.out = 0;
		totalBytes.in = 0;
		totalBytes.out = 0;
		diffPackets.in = 0;
		diffPackets.out = 0;
		totalPackets.in = 0;
		totalPackets.out = 0;
	}
	
	m_bytesIn[t] = diffBytes.in - m_lastBytesIn;
	m_bytesOut[t] = diffBytes.out - m_lastBytesOut;
	m_lastBytesIn = diffBytes.in;
	m_lastBytesOut = diffBytes.out;
	m_packetsIn[t] = diffPackets.in - m_lastPacketsIn;
	m_packetsOut[t] = diffPackets.out - m_lastPacketsOut;
	m_lastPacketsIn = diffPackets.in;
	m_lastPacketsOut = diffPackets.out;

	// calculate bps
	double byteRateIn = 0;
	double byteRateOut = 0;
	double packetRateIn = 0;
	double packetRateOut = 0;
	int n = m_config->getCommonConfig()->smoothFactor;
		
	for (int i = 0; i < n; i++)
		byteRateIn += m_bytesIn[i];
	byteRateIn /= n;
	byteRateIn =
		byteRateIn / m_config->getCommonConfig()->refreshTime * 1000;
	for (int i = 0; i < n; i++)
		byteRateOut += m_bytesOut[i];
	byteRateOut /= n;
	byteRateOut =
		byteRateOut / m_config->getCommonConfig()->refreshTime * 1000;
	for (int i = 0; i < n; i++)
		packetRateIn += m_packetsIn[i];
	packetRateIn /= n;
	packetRateIn =
		packetRateIn / m_config->getCommonConfig()->refreshTime * 1000;
	for (int i = 0; i < n; i++)
		packetRateOut += m_packetsOut[i];
	packetRateOut /= n;
	packetRateOut =
		packetRateOut / m_config->getCommonConfig()->refreshTime * 1000;

	if( byteRateIn > m_maxByteRateIn )
		m_maxByteRateIn = byteRateIn;
	if( byteRateOut > m_maxByteRateOut )
		m_maxByteRateOut = byteRateOut;
	if( packetRateIn > m_maxPacketRateIn )
		m_maxPacketRateIn = packetRateIn;
	if( packetRateOut > m_maxPacketRateOut )
		m_maxPacketRateOut = packetRateOut;
	
	t = (t + 1) % n;

	m_dockFrame->updateDisplay( byteRateIn, byteRateOut );
	m_mainFrame->updateDisplay( byteRateIn, byteRateOut );

	double scaling = m_config->getGraphConfig()->scaling / 8.0;
	if (m_config->getGraphConfig()->graphPosition==Common::POS_BOTTOM)
	{
		scaling = byteRateIn>scaling?byteRateIn:scaling;
		scaling = byteRateOut>scaling?byteRateOut:scaling;
	}
	else
		scaling = byteRateIn+byteRateOut>scaling?byteRateIn+byteRateOut:scaling;
	if( m_config->getGraphConfig()->scalingMode == Common::MODE_CBRT )
		m_barFrame->updatePixmap(
			cbrt(byteRateIn/scaling), cbrt(byteRateOut/scaling) );
	else
		m_barFrame->updatePixmap(
			byteRateIn/scaling, byteRateOut/scaling );

	QString textIn, textOut;
	if( m_config->getCommonConfig()->unit == Common::UNIT_BIT )
	{
		textIn = Tools::double2bit( byteRateIn );
		textOut = Tools::double2bit( byteRateOut );
	}
	else if( m_config->getCommonConfig()->unit == Common::UNIT_BYTE )
	{
		textIn = Tools::double2byte( byteRateIn );
		textOut = Tools::double2byte( byteRateOut );
	}
	m_bpsInFrame->drawText (textIn, m_config->getCommonConfig()->colorIn);
	m_bpsOutFrame->drawText (textOut, m_config->getCommonConfig()->colorOut);

	// update stats frame
	Q3MemArray<double> byteArray(6);
	byteArray[0] = byteRateIn;
	byteArray[1] = m_maxByteRateIn;
	byteArray[2] = totalBytes.in;
	byteArray[3] = byteRateOut;
	byteArray[4] = m_maxByteRateOut;
	byteArray[5] = totalBytes.out;
	Q3MemArray<double> packetArray(6);
	packetArray[0] = packetRateIn;
	packetArray[1] = m_maxPacketRateIn;
	packetArray[2] = totalPackets.in;
	packetArray[3] = packetRateOut;
	packetArray[4] = m_maxPacketRateOut;
	packetArray[5] = totalPackets.out;
	m_statsFrame->updateView( byteArray, packetArray );
}

void Kdevmon::showPopupMenu( const QPoint& pos )
{
	if( isVisible() )
		m_popupMenu->changeItem( Common::MENU_SHOW_MAIN, i18n("Hide main window") );
	else
		m_popupMenu->changeItem( Common::MENU_SHOW_MAIN, i18n("Show main window") );
	if( m_dockFrame->isVisible() )
		m_popupMenu->changeItem( Common::MENU_SHOW_DOCK, i18n("Hide dock window") );
	else
		m_popupMenu->changeItem( Common::MENU_SHOW_DOCK, i18n("Show dock window") );
	if( m_statsFrame->isVisible() )
		m_popupMenu->changeItem( Common::MENU_SHOW_STATS, i18n("Hide stats window") );
	else
		m_popupMenu->changeItem( Common::MENU_SHOW_STATS, i18n("Show stats window") );
	
// 	if( (isVisible()) && (! m_dockFrame->isVisible()) )
// 		m_popupMenu->setShortcutEnabled( Common::MENU_SHOW_MAIN, false );
// 	else
// 		m_popupMenu->setShortcutEnabled( Common::MENU_SHOW_MAIN, true );
// 	if( (! isVisible()) && (m_dockFrame->isVisible()) )
// 		m_popupMenu->setShortcutEnabled( Common::MENU_SHOW_DOCK, false );
// 	else
// 		m_popupMenu->setShortcutEnabled( Common::MENU_SHOW_DOCK, true );
		
			
	m_popupMenu->popup(pos);
}

void Kdevmon::setStatsFrameVisible( bool visible )
{
	if( visible )
	{
		m_statsFrame->move( m_config->getStatsFrameConfig()->position );
		m_statsFrame->show();
	}
	else
	{
		m_config->getStatsFrameConfig()->position = m_statsFrame->pos();
		m_statsFrame->hide();
	}
}

void Kdevmon::setDockFrameVisible( bool enable ) {
	m_config->getDockFrameConfig()->isVisible = enable;

	if( enable )
		m_dockFrame->show();
	else
		m_dockFrame->hide();
}

void Kdevmon::setMainFrameVisible( bool enable )
{
	m_config->getMainFrameConfig()->isVisible = enable;
	if( enable )
	{
		setGeometry( m_config->getMainFrameConfig()->geometry );
		show();
	}
	else
	{
		m_config->getMainFrameConfig()->geometry = geometry();
		hide();
	}
}

