/***************************************************************************
                          cpudisplay.cpp  -  description
                             -------------------
    begin                : Sun Nov 25 2001
    copyright            : (C) 2001 by Miguel Novas
    email                : michaell@teleline.es
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "cpupanel.h"
#include "qlcddraw.h"

#include <tqcolor.h>
#include <tqpainter.h>
#include <tqlcdnumber.h>

#include <string.h>

bool getCpuInfoValue(const char *name,TQString &value)
{
char buffer[128];
char *ptr;
FILE *fp;
bool result;

 result= false;
 if( (fp= fopen("/proc/cpuinfo","r")) ) {
   while( fgets(buffer, 127, fp) ) {
     if(!strncmp(name,buffer,strlen(name))) {
        ptr= strchr(buffer,':');
        if(ptr) {
          value= (const char *)(ptr+2);
          value = value.stripWhiteSpace();
        }
        result= true;
        break;
     }
   }
  fclose(fp);
 }
 return result;
}

void  adjustString(TQString &str,int maxlen)
{
int i= str.find('(');

 str.truncate(maxlen);
 if(i>=0) str.truncate(i);
 str= str.stripWhiteSpace();
}

CpuPanel::CpuPanel(TQWidget *parent, const char *name) : Panel(parent,name)
{
 sCpu= "Unknown";
 getCpuInfoValue("model name",sCpu);
 adjustString(sCpu,16);

 sVendor= "Unknown";
 getCpuInfoValue("vendor_id",sVendor);
 adjustString(sVendor,16);

 sBogomips= "0";
 getCpuInfoValue("bogomips",sBogomips);
 sBogomips.sprintf("%.0f BMPS",sBogomips.toDouble());

 TQString sSpeed= "0";
 getCpuInfoValue("cpu MHz",sSpeed);
 sSpeed.sprintf("%.0f",sSpeed.toDouble());
 speed= new TQLCDNumber(this);
 speed->setGeometry(6,20,52,26);
 speed->setNumDigits(sSpeed.length());
 speed->setSegmentStyle(TQLCDNumber::Filled);
 speed->setFrameShape(TQFrame::NoFrame);
 speed->display(sSpeed);
 TQPalette cg= speed->palette();
 cg.setColor( TQColorGroup::Foreground, TQColor( 255, 0, 0) );
 cg.setColor( TQColorGroup::Light, TQColor( 255, 0, 0) );
 cg.setColor( TQColorGroup::Midlight, TQColor( 231, 232, 246) );
 cg.setColor( TQColorGroup::Dark, TQColor( 104, 105, 118) );
 cg.setColor( TQColorGroup::Mid, TQColor( 139, 140, 158) );
 cg.setColor( TQColorGroup::Background, palette().active().background() );
 speed->setPalette(cg);
 speed->installEventFilter(this);
}

CpuPanel::~CpuPanel()
{
}

void CpuPanel::updateInfo()
{
 getCpuInfoValue("bogomips",sBogomips);
 sBogomips.sprintf("%.0f BMPS",sBogomips.toDouble());

 getCpuInfoValue("cpu MHz",sSpeed);
 sSpeed.sprintf("%.0f",sSpeed.toDouble());
 speed->setNumDigits(sSpeed.length());
 speed->display(sSpeed);
 update();
}

void CpuPanel::drawContents(TQPainter *p)
{
 int w= width();
 int h= height();

 int i2= (h * 4) / 5;
 int th= h-i2-h/11;

 TQLcd::draw(p, 2,h/10,w-4,th,sCpu.latin1(),TQLcd::alignJustify, &getColorTitle());
 TQLcd::draw(p, 2,i2+1,w-4,th,sBogomips.latin1(),TQLcd::alignCenter,&getColorTitle());
}

void CpuPanel::paletteChange( const TQPalette &oldPalette)
{
TQPalette cg= speed->palette();

 cg.setColor(TQColorGroup::Background,palette().active().background());
 speed->setPalette(cg);
}


void CpuPanel::resizeEvent ( TQResizeEvent *e )
{
 int w= width();
 int h= height();

 int mw= w/10;

 speed->setGeometry(mw,h/3,w-mw*2,(h*2)/5);
}

#include "cpupanel.moc"
