Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

memory.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include <qlabel.h>
00022 #include <qtimer.h>
00023 #include <qfile.h>
00024 #include <qtextstream.h>
00025 #include <qlayout.h>
00026 #include <qwhatsthis.h>
00027 #include "graph.h"
00028 #include "memory.h"
00029 
00030 MemoryInfo::MemoryInfo( QWidget *parent, const char *name, WFlags f )
00031     : QWidget( parent, name, f )
00032 {
00033     QVBoxLayout *vb = new QVBoxLayout( this, 5 );
00034 
00035     totalMem = new QLabel( this );
00036     vb->addWidget( totalMem );
00037 
00038     data = new GraphData();
00039     graph = new BarGraph( this );
00040     graph->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00041     vb->addWidget( graph, 1 );
00042     graph->setData( data );
00043 
00044     legend = new GraphLegend( this );
00045     vb->addWidget( legend );
00046     legend->setData( data );
00047 
00048     swapMem = new QLabel( this );
00049     vb->addWidget( swapMem );
00050 
00051     swapdata = new GraphData();
00052     swapgraph = new BarGraph( this );
00053     swapgraph->setFrameStyle( QFrame::Panel | QFrame::Sunken );
00054     vb->addWidget( swapgraph, 1 );
00055     swapgraph->setData( swapdata );
00056 
00057     swaplegend = new GraphLegend( this );
00058     vb->addWidget( swaplegend );
00059     swaplegend->setData( swapdata );
00060 
00061     vb->addStretch( 1 );
00062     updateData();
00063 
00064     QTimer *t = new QTimer( this );
00065     connect( t, SIGNAL( timeout() ), this, SLOT( updateData() ) );
00066     t->start( 5000 );
00067 
00068     QWhatsThis::add( this, tr( "This page shows how memory (i.e. RAM) is being allocated on your device.\nMemory is categorized as follows:\n\n1. Used - memory used to by Opie and any running applications.\n2. Buffers - temporary storage used to improve performance\n3. Cached - information that has recently been used, but has not been freed yet.\n4. Free - memory not currently used by Opie or any running applications." ) );
00069 
00070 }
00071 
00072 MemoryInfo::~MemoryInfo()
00073 {
00074     delete data;
00075 }
00076 
00077 void MemoryInfo::updateData()
00078 {
00079     QFile file( "/proc/meminfo" );
00080 
00081     if ( file.open( IO_ReadOnly ) )
00082     {
00083         // local variables
00084         QString line;
00085         QString identifier;
00086         QString value;
00087         int position;
00088         QTextStream t( &file );
00089 
00090         while ( !t.atEnd() )
00091         {
00092             // read a line
00093             line = t.readLine();
00094 
00095             // extract identifier and value from line
00096             position = line.find( ":" );
00097             identifier = line.left( position );
00098             value = line.mid( position + 1, line.length() - position - 3 );
00099             value = value.stripWhiteSpace();
00100 
00101             // copy values in variables
00102             if ( identifier == "MemTotal" )
00103             {
00104                 total = value.toULong();
00105             } else if ( identifier == "MemFree" )
00106             {
00107                 memfree = value.toULong();
00108             } else if ( identifier == "Buffers" )
00109             {
00110                 buffers = value.toULong();
00111             } else if ( identifier == "Cached" )
00112             {
00113                 cached = value.toULong();
00114             } else if ( identifier == "SwapCached" )
00115             {
00116             } else if ( identifier == "SwapTotal" )
00117             {
00118                 swaptotal = value.toULong();
00119             } else if ( identifier == "SwapFree" )
00120             {
00121                 swapfree = value.toULong();
00122             }
00123         }
00124 
00125         file.close();
00126 
00127         // calculate values
00128         used = total - memfree;
00129         swapused = swaptotal - swapfree;
00130         realUsed = total - ( buffers + cached + memfree );
00131 
00132         // visualize values
00133         totalMem->setText( tr( "Total Memory: %1 kB" ).arg( total ) );
00134         data->clear();
00135         data->addItem( tr("Used (%1 kB)").arg(realUsed), realUsed );
00136         data->addItem( tr("Buffers (%1 kB)").arg(buffers), buffers );
00137         data->addItem( tr("Cached (%1 kB)").arg(cached), cached );
00138         data->addItem( tr("Free (%1 kB)").arg(memfree), memfree );
00139 
00140         graph->hide();
00141         graph->show();
00142         legend->update();
00143 
00144         if (swaptotal > 0)
00145         {
00146             swapMem->setText( tr( "Total Swap: %1 kB" ).arg( swaptotal ) );
00147             swapdata->clear();
00148             swapdata->addItem( tr("Used (%1 kB)").arg(swapused), swapused );
00149             swapdata->addItem( tr("Free (%1 kB)").arg(swapfree), swapfree );
00150 
00151             swapMem->show();
00152             swapgraph->show();
00153             swaplegend->show();
00154 
00155             swapgraph->repaint( FALSE );
00156             swaplegend->update();
00157         }
00158         else
00159         {
00160             swapMem->hide();
00161             swapgraph->hide();
00162             swaplegend->hide();
00163         }
00164     }
00165 }

Generated on Sat Nov 5 16:16:44 2005 for OPIE by  doxygen 1.4.2