summaryrefslogtreecommitdiff
path: root/noncore/applets/memoryapplet/memory.cpp
authormickeyl <mickeyl>2005-01-29 09:35:52 (UTC)
committer mickeyl <mickeyl>2005-01-29 09:35:52 (UTC)
commitd4cf8c6020c46e5dc97b75f3a9781ddc15416b3a (patch) (unidiff)
treec17be6e75e277c3fa0378f352f45766f75031cdb /noncore/applets/memoryapplet/memory.cpp
parentf85af28663814f3262f5ecfcd20a4b4f67c23067 (diff)
downloadopie-d4cf8c6020c46e5dc97b75f3a9781ddc15416b3a.zip
opie-d4cf8c6020c46e5dc97b75f3a9781ddc15416b3a.tar.gz
opie-d4cf8c6020c46e5dc97b75f3a9781ddc15416b3a.tar.bz2
fix 1505 and suck sysinfo files into the tree instead of cross referencing
eventually we may find that graph and load are of universal usage, then they should appear in some kind of library
Diffstat (limited to 'noncore/applets/memoryapplet/memory.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/applets/memoryapplet/memory.cpp165
1 files changed, 165 insertions, 0 deletions
diff --git a/noncore/applets/memoryapplet/memory.cpp b/noncore/applets/memoryapplet/memory.cpp
new file mode 100644
index 0000000..05349e4
--- a/dev/null
+++ b/noncore/applets/memoryapplet/memory.cpp
@@ -0,0 +1,165 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include <qlabel.h>
22#include <qtimer.h>
23#include <qfile.h>
24#include <qtextstream.h>
25#include <qlayout.h>
26#include <qwhatsthis.h>
27#include "graph.h"
28#include "memory.h"
29
30MemoryInfo::MemoryInfo( QWidget *parent, const char *name, WFlags f )
31 : QWidget( parent, name, f )
32{
33 QVBoxLayout *vb = new QVBoxLayout( this, 5 );
34
35 totalMem = new QLabel( this );
36 vb->addWidget( totalMem );
37
38 data = new GraphData();
39 graph = new BarGraph( this );
40 graph->setFrameStyle( QFrame::Panel | QFrame::Sunken );
41 vb->addWidget( graph, 1 );
42 graph->setData( data );
43
44 legend = new GraphLegend( this );
45 vb->addWidget( legend );
46 legend->setData( data );
47
48 swapMem = new QLabel( this );
49 vb->addWidget( swapMem );
50
51 swapdata = new GraphData();
52 swapgraph = new BarGraph( this );
53 swapgraph->setFrameStyle( QFrame::Panel | QFrame::Sunken );
54 vb->addWidget( swapgraph, 1 );
55 swapgraph->setData( swapdata );
56
57 swaplegend = new GraphLegend( this );
58 vb->addWidget( swaplegend );
59 swaplegend->setData( swapdata );
60
61 vb->addStretch( 1 );
62 updateData();
63
64 QTimer *t = new QTimer( this );
65 connect( t, SIGNAL( timeout() ), this, SLOT( updateData() ) );
66 t->start( 5000 );
67
68 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." ) );
69
70}
71
72MemoryInfo::~MemoryInfo()
73{
74 delete data;
75}
76
77void MemoryInfo::updateData()
78{
79 QFile file( "/proc/meminfo" );
80
81 if ( file.open( IO_ReadOnly ) )
82 {
83 // local variables
84 QString line;
85 QString identifier;
86 QString value;
87 int position;
88 QTextStream t( &file );
89
90 while ( !t.atEnd() )
91 {
92 // read a line
93 line = t.readLine();
94
95 // extract identifier and value from line
96 position = line.find( ":" );
97 identifier = line.left( position );
98 value = line.mid( position + 1, line.length() - position - 3 );
99 value = value.stripWhiteSpace();
100
101 // copy values in variables
102 if ( identifier == "MemTotal" )
103 {
104 total = value.toULong();
105 } else if ( identifier == "MemFree" )
106 {
107 memfree = value.toULong();
108 } else if ( identifier == "Buffers" )
109 {
110 buffers = value.toULong();
111 } else if ( identifier == "Cached" )
112 {
113 cached = value.toULong();
114 } else if ( identifier == "SwapCached" )
115 {
116 } else if ( identifier == "SwapTotal" )
117 {
118 swaptotal = value.toULong();
119 } else if ( identifier == "SwapFree" )
120 {
121 swapfree = value.toULong();
122 }
123 }
124
125 file.close();
126
127 // calculate values
128 used = total - memfree;
129 swapused = swaptotal - swapfree;
130 realUsed = total - ( buffers + cached + memfree );
131
132 // visualize values
133 totalMem->setText( tr( "Total Memory: %1 kB" ).arg( total ) );
134 data->clear();
135 data->addItem( tr("Used (%1 kB)").arg(realUsed), realUsed );
136 data->addItem( tr("Buffers (%1 kB)").arg(buffers), buffers );
137 data->addItem( tr("Cached (%1 kB)").arg(cached), cached );
138 data->addItem( tr("Free (%1 kB)").arg(memfree), memfree );
139
140 graph->hide();
141 graph->show();
142 legend->update();
143
144 if (swaptotal > 0)
145 {
146 swapMem->setText( tr( "Total Swap: %1 kB" ).arg( swaptotal ) );
147 swapdata->clear();
148 swapdata->addItem( tr("Used (%1 kB)").arg(swapused), swapused );
149 swapdata->addItem( tr("Free (%1 kB)").arg(swapfree), swapfree );
150
151 swapMem->show();
152 swapgraph->show();
153 swaplegend->show();
154
155 swapgraph->repaint( FALSE );
156 swaplegend->update();
157 }
158 else
159 {
160 swapMem->hide();
161 swapgraph->hide();
162 swaplegend->hide();
163 }
164 }
165}