summaryrefslogtreecommitdiff
authormickeyl <mickeyl>2003-11-03 22:43:25 (UTC)
committer mickeyl <mickeyl>2003-11-03 22:43:25 (UTC)
commite0c0894f9ec4f8e7f09d099d93623dd570f26b13 (patch) (unidiff)
tree380f69b89944d63d23e51c5a62129f5f05183852
parent28812a7f2c5bd370a55824a73f6a0010f8ea9f7e (diff)
downloadopie-e0c0894f9ec4f8e7f09d099d93623dd570f26b13.zip
opie-e0c0894f9ec4f8e7f09d099d93623dd570f26b13.tar.gz
opie-e0c0894f9ec4f8e7f09d099d93623dd570f26b13.tar.bz2
add memoryapplet (displays memory and manages a swap partition)
written by Anton Maslovsky (http://my-zaurus.narod.ru)
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--noncore/applets/memoryapplet/.cvsignore4
-rw-r--r--noncore/applets/memoryapplet/config.in6
-rw-r--r--noncore/applets/memoryapplet/memoryappletimpl.cpp65
-rw-r--r--noncore/applets/memoryapplet/memoryappletimpl.h44
-rw-r--r--noncore/applets/memoryapplet/memorymeter.cpp239
-rw-r--r--noncore/applets/memoryapplet/memorymeter.h53
-rw-r--r--noncore/applets/memoryapplet/memorystatus.cpp82
-rw-r--r--noncore/applets/memoryapplet/memorystatus.h47
-rw-r--r--noncore/applets/memoryapplet/swapfile.cpp436
-rw-r--r--noncore/applets/memoryapplet/swapfile.h70
10 files changed, 1046 insertions, 0 deletions
diff --git a/noncore/applets/memoryapplet/.cvsignore b/noncore/applets/memoryapplet/.cvsignore
new file mode 100644
index 0000000..51d12e8
--- a/dev/null
+++ b/noncore/applets/memoryapplet/.cvsignore
@@ -0,0 +1,4 @@
1Makefile*
2advancedconfigbase.cpp
3advancedconfigbase.h
4moc_*
diff --git a/noncore/applets/memoryapplet/config.in b/noncore/applets/memoryapplet/config.in
new file mode 100644
index 0000000..578aaf9
--- a/dev/null
+++ b/noncore/applets/memoryapplet/config.in
@@ -0,0 +1,6 @@
1 config MEMORYAPPLET
2 boolean "opie-memoryapplet (view memory status and manage a swap file)"
3 default "y"
4 depends ( LIBQPE || LIBQPE-X11 ) && LIBOPIE && SYSINFO
5 comment "opie-memoryapplet needs a libqpe, libopie and sysinfo"
6 depends ! ( LIBOPIE && SYSINFO )
diff --git a/noncore/applets/memoryapplet/memoryappletimpl.cpp b/noncore/applets/memoryapplet/memoryappletimpl.cpp
new file mode 100644
index 0000000..a57f4a9
--- a/dev/null
+++ b/noncore/applets/memoryapplet/memoryappletimpl.cpp
@@ -0,0 +1,65 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the 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#include "memorymeter.h"
21#include "memoryappletimpl.h"
22
23MemoryAppletImpl::MemoryAppletImpl()
24 : memory(0), ref(0)
25{
26}
27
28MemoryAppletImpl::~MemoryAppletImpl()
29{
30 delete memory;
31}
32
33QWidget *MemoryAppletImpl::applet( QWidget *parent )
34{
35 if ( !memory )
36 memory = new MemoryMeter( parent );
37
38 return memory;
39}
40
41int MemoryAppletImpl::position() const
42{
43 return 8;
44}
45
46QRESULT MemoryAppletImpl::queryInterface( const QUuid &uuid, QUnknownInterface **iface )
47{
48 *iface = 0;
49 if ( uuid == IID_QUnknown )
50 *iface = this;
51 else if ( uuid == IID_TaskbarApplet )
52 *iface = this;
53 else
54 return QS_FALSE;
55
56 (*iface)->addRef();
57
58 return QS_OK;
59}
60
61Q_EXPORT_INTERFACE()
62{
63 Q_CREATE_INSTANCE( MemoryAppletImpl )
64}
65
diff --git a/noncore/applets/memoryapplet/memoryappletimpl.h b/noncore/applets/memoryapplet/memoryappletimpl.h
new file mode 100644
index 0000000..e0016e1
--- a/dev/null
+++ b/noncore/applets/memoryapplet/memoryappletimpl.h
@@ -0,0 +1,44 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the 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#ifndef MEMORYAPPLETIMPL_H
21#define MEMORYAPPLETIMPL_H
22
23#include <qtopia/taskbarappletinterface.h>
24
25class MemoryMeter;
26
27class /*QTOPIA_PLUGIN_EXPORT*/ MemoryAppletImpl : public TaskbarAppletInterface
28{
29public:
30 MemoryAppletImpl();
31 virtual ~MemoryAppletImpl();
32
33 QRESULT queryInterface( const QUuid&, QUnknownInterface** );
34 Q_REFCOUNT
35
36 virtual QWidget *applet( QWidget *parent );
37 virtual int position() const;
38
39private:
40 MemoryMeter *memory;
41 ulong ref;
42};
43
44#endif
diff --git a/noncore/applets/memoryapplet/memorymeter.cpp b/noncore/applets/memoryapplet/memorymeter.cpp
new file mode 100644
index 0000000..54b5c52
--- a/dev/null
+++ b/noncore/applets/memoryapplet/memorymeter.cpp
@@ -0,0 +1,239 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the 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#include "memorymeter.h"
21#include "memorystatus.h"
22
23#include <qtopia/power.h>
24#include <qtopia/config.h>
25
26#if defined(Q_WS_QWS) && !defined(QT_NO_COP)
27#include <qtopia/qcopenvelope_qws.h>
28#endif
29
30#include <qpainter.h>
31#include <qtimer.h>
32#include <qapplication.h>
33
34#include <qtopia/applnk.h>
35
36MemoryMeter::MemoryMeter( QWidget *parent )
37 : QWidget( parent ), memoryView(0)
38{
39 bvsz = QSize();
40 if ( qApp->desktop()->height() >= 300 )
41 {
42 memoryView = new MemoryStatus( 0, WStyle_StaysOnTop | WType_Popup );
43 memoryView->setFrameStyle( QFrame::Panel | QFrame::Raised );
44 }
45 else
46 {
47 memoryView = new MemoryStatus( 0 );
48 memoryView->showMaximized();
49 }
50
51 Config config("MemoryPlugin");
52 config.setGroup("Warning levels");
53 low = config.readNumEntry("low", 40);
54 critical = config.readNumEntry("critical", 20);
55
56 startTimer( 10000 );
57 setFixedWidth(10);
58 setFixedHeight(AppLnk::smallIconSize());
59 usageTimer = new QTimer( this );
60 connect( usageTimer, SIGNAL(timeout()), this, SLOT(usageTimeout()) );
61 timerEvent(0);
62}
63
64MemoryMeter::~MemoryMeter()
65{
66 delete (QWidget *) memoryView;
67}
68
69QSize MemoryMeter::sizeHint() const
70{
71 return QSize(10, AppLnk::smallIconSize());
72}
73
74bool MemoryMeter::updateMemoryViewGeometry()
75{
76 if (memoryView != 0)
77 {
78 QSize sz = memoryView->sizeHint();
79 if ( sz != bvsz )
80 {
81 bvsz = sz;
82 QRect r(memoryView->pos(), memoryView->sizeHint());
83 if ( qApp->desktop()->height() >= 300 )
84 {
85 QPoint curPos = mapToGlobal( rect().topLeft() );
86 int lp = qApp->desktop()->width() - memoryView->sizeHint().width();
87 r.moveTopLeft( QPoint(lp, curPos.y() - memoryView->sizeHint().height()-1) );
88 }
89 memoryView->setGeometry(r);
90 return TRUE;
91 }
92 return FALSE;
93 }
94
95 return FALSE;
96}
97
98void MemoryMeter::mousePressEvent( QMouseEvent *)
99{
100 if ( memoryView->isVisible() )
101 {
102 memoryView->hide();
103 }
104 else
105 {
106 bvsz = QSize();
107 updateMemoryViewGeometry();
108 memoryView->raise();
109 memoryView->show();
110 }
111}
112
113void MemoryMeter::timerEvent( QTimerEvent * )
114{
115 if (memoryView != 0)
116 {
117 // read memory status
118 percent = (memoryView->percent());
119 usageTimer->start( 1000 );
120 }
121}
122
123void MemoryMeter::usageTimeout()
124{
125 if (memoryView != 0)
126 {
127 percent = (memoryView->percent());
128 if (updateMemoryViewGeometry() && memoryView->isVisible())
129 {
130 memoryView->hide();
131 memoryView->show();
132 }
133
134 repaint(FALSE);
135 }
136}
137
138void MemoryMeter::paintEvent( QPaintEvent* )
139{
140 QPainter p(this);
141
142 QColor c;
143 QColor darkc;
144 QColor lightc;
145
146 if (percent > low)
147 c = green;
148 else if (percent > critical)
149 c = yellow.dark(110);
150 else
151 c = red;
152
153 darkc = c.dark(120);
154 lightc = c.light(160);
155
156 //
157 // To simulate a 3-d memory, we use 4 bands of colour. From left
158 // to right, these are: medium, light, medium, dark. To avoid
159 // hardcoding values for band "width", figure everything out on the run.
160 //
161 int batt_width; // width of each band
162 int batt_height; // memory height (not including terminal)
163 int used_height; // used amount of memory (scanlines)
164
165 int batt_yoffset; // top of terminal
166 int batt_xoffset; // left edge of core
167
168 int band_width; // width of colour band
169
170 int w = QMIN(height(), width());
171 band_width = (w-2) / 4;
172 if ( band_width < 1 )
173 band_width = 1;
174
175 batt_width = 4 * band_width + 2;// +2 for 1 pixel border on both sides
176 batt_height = height()-2;
177 batt_xoffset = (width() - batt_width) / 2;
178 batt_yoffset = (height() - batt_height) / 2;
179
180 //
181 // Memory border. +1 to make space for the terminal at row 0.
182 //
183 p.setPen(QColor(80, 80, 80));
184 p.drawRect(batt_xoffset, batt_yoffset + 1, batt_width, batt_height);
185
186 //
187 // Draw terminal. +1 to take into account the left border.
188 //
189 //p.drawLine(batt_xoffset + band_width + 1, batt_yoffset, batt_xoffset + 3 * band_width, batt_yoffset);
190
191 batt_height -= 2;// -2 because we don't want to include border
192 batt_yoffset += 2; // +2 to account for border and terminal
193 batt_xoffset++;
194
195 //
196 // 100 - percent, since percent is amount remaining, and we draw
197 // reverse to this.
198 //
199 used_height = percent * batt_height / 100;
200 if (used_height < 0)
201 used_height = 0;
202
203 //
204 // Drained section.
205 //
206 if (used_height != 0)
207 {
208 p.setPen(NoPen);
209 p.setBrush(gray);
210 p.drawRect(batt_xoffset, batt_yoffset, band_width, used_height);
211 p.drawRect(batt_xoffset + 2 * band_width, batt_yoffset, band_width, used_height);
212
213 p.setBrush(gray/*.light(130)*/);
214 p.drawRect(batt_xoffset + band_width, batt_yoffset, band_width, used_height);
215
216 p.setBrush(gray/*.dark(120)*/);
217 p.drawRect(batt_xoffset + 3 * band_width, batt_yoffset, band_width, used_height);
218 }
219
220 //
221 // Unused section.
222 //
223 if ( batt_height - used_height > 0 )
224 {
225 int unused_offset = used_height + batt_yoffset;
226 int unused_height = batt_height - used_height;
227 p.setPen(NoPen);
228 p.setBrush(c);
229 p.drawRect(batt_xoffset, unused_offset, band_width, unused_height);
230 p.drawRect(batt_xoffset + 2 * band_width, unused_offset, band_width, unused_height);
231
232 p.setBrush(lightc);
233 p.drawRect(batt_xoffset + band_width, unused_offset, band_width, unused_height);
234
235 p.setBrush(darkc);
236 p.drawRect(batt_xoffset + 3 * band_width, unused_offset, band_width, unused_height);
237 }
238}
239
diff --git a/noncore/applets/memoryapplet/memorymeter.h b/noncore/applets/memoryapplet/memorymeter.h
new file mode 100644
index 0000000..0f3cb0b
--- a/dev/null
+++ b/noncore/applets/memoryapplet/memorymeter.h
@@ -0,0 +1,53 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the 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#ifndef MEMORY_H
21#define MEMORY_H
22
23#include <qwidget.h>
24
25class MemoryStatus;
26class QTimer;
27
28class MemoryMeter : public QWidget
29{
30 Q_OBJECT
31public:
32 MemoryMeter( QWidget *parent = 0 );
33 ~MemoryMeter();
34
35 QSize sizeHint() const;
36 MemoryStatus* memoryView;
37
38protected:
39 void timerEvent( QTimerEvent * );
40 void paintEvent( QPaintEvent* );
41 void mousePressEvent( QMouseEvent * );
42
43protected slots:
44 void usageTimeout();
45
46protected:
47 QTimer *usageTimer;
48 int percent, low, critical;
49 QSize bvsz;
50 bool updateMemoryViewGeometry();
51};
52
53#endif
diff --git a/noncore/applets/memoryapplet/memorystatus.cpp b/noncore/applets/memoryapplet/memorystatus.cpp
new file mode 100644
index 0000000..a800fa5
--- a/dev/null
+++ b/noncore/applets/memoryapplet/memorystatus.cpp
@@ -0,0 +1,82 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the 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#include "memorystatus.h"
21#include "../../apps/sysinfo/memory.h"
22#include "swapfile.h"
23
24#include <qpainter.h>
25#include <qpushbutton.h>
26#include <qdrawutil.h>
27#include <qlabel.h>
28
29#include <opie/otabwidget.h>
30#include <qlayout.h>
31
32MemoryStatus::MemoryStatus(QWidget *parent, WFlags f )
33 : QFrame(parent, 0, f), mi(0), sf(0)
34{
35 setCaption( tr("Memory Status") );
36 //resize( 220, 180 );
37
38 QVBoxLayout *lay = new QVBoxLayout( this );
39 tab = new OTabWidget( this, "tabwidget", OTabWidget::Global );
40 lay->addWidget( tab );
41 tab->addTab( mi = new MemoryInfo( tab ), "memory/memorytabicon", tr("Memory") );
42 tab->addTab( sf = new Swapfile( tab ), "memory/storagetabicon", tr("Swapfile") );
43
44 QLabel* about = new QLabel(tr("<center><b>Memory Monitor Plugin</b><br>"
45 "Copyright (C) 2003 Anton Maslovsky<br>"
46 "&lt;<a href=\"mailto:my-zaurus@narod.ru\">my-zaurus@narod.ru</a>&gt;<br>"
47 "<a href=\"http://my-zaurus.narod.ru\">http://my-zaurus.narod.ru</a><br>"
48 "Based on source code from:<br> qswap (udoseidel@gmx.de) <br> Battery Applet (trolltech.com) <br> SysInfo (OPIE)<br><br>"
49 "This program is licensed under GNU GPL.</center>"), tab);
50
51 tab->addTab( about, "memory/info", tr("About") );
52
53 tab->setCurrentTab( tr( "Memory" ) );
54}
55
56int MemoryStatus::percent()
57{
58 if (mi == 0)
59 return 100;
60
61 int total = mi->total;
62 if (mi->swaptotal > 0)
63 total += mi->swaptotal;
64
65 int used = mi->realUsed;
66 if (mi->swapused > 0)
67 total += mi->swapused;
68
69 return ((total - used) * 100)/total;
70}
71
72QSize MemoryStatus::sizeHint() const
73{
74 QSize s = tab->size();
75 s.setWidth(200);
76 s.setHeight((mi->swaptotal > 0) ? 220 : 200);
77 return s;
78}
79
80MemoryStatus::~MemoryStatus()
81{
82}
diff --git a/noncore/applets/memoryapplet/memorystatus.h b/noncore/applets/memoryapplet/memorystatus.h
new file mode 100644
index 0000000..5c73833
--- a/dev/null
+++ b/noncore/applets/memoryapplet/memorystatus.h
@@ -0,0 +1,47 @@
1/**********************************************************************
2** Copyright (C) 2000-2002 Trolltech AS. All rights reserved.
3**
4** This file is part of the 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#ifndef MEMORY_STATUS_H
21#define MEMORY_STATUS_H
22
23#include <qframe.h>
24
25class MemoryInfo;
26class Swapfile;
27class OTabWidget;
28
29class MemoryStatus : public QFrame
30{
31 Q_OBJECT
32public:
33 MemoryStatus(QWidget *parent = 0, WFlags f = 0);
34 ~MemoryStatus();
35
36 QSize sizeHint() const;
37 MemoryInfo* mi;
38 Swapfile* sf;
39
40 int percent();
41
42private:
43 OTabWidget *tab;
44};
45
46#endif
47
diff --git a/noncore/applets/memoryapplet/swapfile.cpp b/noncore/applets/memoryapplet/swapfile.cpp
new file mode 100644
index 0000000..06746a7
--- a/dev/null
+++ b/noncore/applets/memoryapplet/swapfile.cpp
@@ -0,0 +1,436 @@
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 "swapfile.h"
22
23#include <qlabel.h>
24#include <qtimer.h>
25#include <qfile.h>
26#include <qtextstream.h>
27#include <qlayout.h>
28#include <qpushbutton.h>
29#include <qhbuttongroup.h>
30#include <qradiobutton.h>
31#include <qlineedit.h>
32#include <qprogressbar.h>
33#include <qcombobox.h>
34#include <qvgroupbox.h>
35#include <qhbox.h>
36#include <qmessagebox.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <qfile.h>
41#include <qtextstream.h>
42
43#include <qcopchannel_qws.h>
44#include <qpe/resource.h>
45
46#include <unistd.h>
47#include <fcntl.h>
48#include <sys/vfs.h>
49#include <mntent.h>
50#include <unistd.h>
51#include <sys/types.h>
52
53Swapfile::Swapfile( QWidget *parent, const char *name, WFlags f )
54 : QWidget( parent, name, f )
55{
56 // are we running as root?
57 isRoot = geteuid() == 0;
58
59 QVBoxLayout* vb = new QVBoxLayout(this, 5);
60
61 QHButtonGroup* cfsdRBG = new QHButtonGroup(tr("Swapfile location"), this);
62 cfsdRBG->setRadioButtonExclusive(true);
63 vb->addWidget(cfsdRBG);
64
65 ramRB = new QRadioButton(tr("RAM"), cfsdRBG);
66 cfRB = new QRadioButton(tr("CF Card"), cfsdRBG);
67 sdRB = new QRadioButton(tr("SD Card"), cfsdRBG);
68
69 QHBox *hb1 = new QHBox(this);
70 hb1->setSpacing(5);
71
72 swapPath1 = new QLineEdit(hb1);
73 swapPath1->setEnabled(false);
74
75 QPushButton* swapOn = new QPushButton(tr(" On "), hb1);
76 QPushButton* swapOff = new QPushButton(tr(" Off "), hb1);
77 vb->addWidget(hb1);
78
79 QVGroupBox* box1 = new QVGroupBox(tr("Manage Swapfile"), this);
80 vb->addWidget(box1);
81
82 QHBox *hb2 = new QHBox(box1);
83 hb2->setSpacing(5);
84 QPushButton* mkSwap = new QPushButton(tr("Generate"), hb2);
85 QPushButton* rmSwap = new QPushButton(tr("Remove"), hb2);
86
87 QHBox *hb3 = new QHBox(box1);
88 hb3->setSpacing(5);
89 swapSize = new QComboBox(hb3);
90 swapSize->insertStringList(QStringList::split(",", tr("2 Mb,4 Mb,6 Mb,8 Mb")));
91
92 mkswapProgress = new QProgressBar(3, hb3);
93 mkswapProgress->setCenterIndicator(true);
94
95 QHBox *hb4 = new QHBox(this);
96 hb4->setSpacing(5);
97
98 swapStatusIcon = new QLabel(hb4);
99 swapStatus = new QLabel(tr(""), hb4);
100 hb4->setStretchFactor(swapStatus, 99);
101 vb->addWidget(hb4);
102
103 connect(swapOn, SIGNAL(clicked()), this, SLOT(swapon()));
104 connect(swapOff, SIGNAL(clicked()), this, SLOT(swapoff()));
105 connect(cfRB, SIGNAL(clicked()), this, SLOT(cfsdchecked()));
106 connect(sdRB, SIGNAL(clicked()), this, SLOT(cfsdchecked()));
107 connect(ramRB, SIGNAL(clicked()), this, SLOT(cfsdchecked()));
108 connect(mkSwap, SIGNAL(clicked()), this, SLOT(makeswapfile()));
109 connect(rmSwap, SIGNAL(clicked()), this, SLOT(removeswapfile()));
110
111 cfRB->setEnabled(FALSE);
112 sdRB->setEnabled(FALSE);
113
114 QCopChannel *pcmciaChannel = new QCopChannel("QPE/Card", this);
115 connect(pcmciaChannel, SIGNAL(received(const QCString &, const QByteArray &)), this, SLOT(cardnotify(const QCString &, const QByteArray &)));
116 QCopChannel *sdChannel = new QCopChannel("QPE/Card", this);
117 connect(sdChannel, SIGNAL(received(const QCString &, const QByteArray &)), this, SLOT(cardnotify(const QCString &, const QByteArray &)));
118
119 cardInPcmcia0 = FALSE;
120 cardInPcmcia1 = FALSE;
121 cardInSd = FALSE;
122
123 Swapfile::status();
124 Swapfile::getStatusPcmcia();
125 Swapfile::getStatusSd();
126}
127
128int Swapfile::exec(const QString& arg)
129{
130 return system((!isRoot ? "sudo " : "") + arg);
131}
132
133
134Swapfile::~Swapfile()
135{
136}
137
138void Swapfile::cardnotify(const QCString & msg, const QByteArray &)
139{
140 if (msg == "stabChanged()")
141 {
142 getStatusPcmcia();
143 }
144 else if (msg == "mtabChanged()")
145 {
146 getStatusSd();
147 }
148}
149
150void Swapfile::getStatusPcmcia()
151{
152
153 bool cardWas0 = cardInPcmcia0;// remember last state
154 bool cardWas1 = cardInPcmcia1;
155
156 QString fileName;
157
158 // one of these 3 files should exist
159 if (QFile::exists("/var/run/stab")) {
160 fileName = "/var/run/stab";
161 } else if (QFile::exists("/var/state/pcmcia/stab")) {
162 fileName = "/var/state/pcmcia/stab";
163 } else {
164 fileName = "/var/lib/pcmcia/stab";
165 }
166
167 QFile f(fileName);
168
169 if (f.open(IO_ReadOnly)) {
170 QStringList list;
171 QTextStream stream(&f);
172 QString streamIn;
173 streamIn = stream.read();
174 list = QStringList::split("\n", streamIn);
175 for (QStringList::Iterator line = list.begin(); line != list.end();
176 line++) {
177 if ((*line).startsWith("Socket 0:")) {
178 if ((*line).startsWith("Socket 0: empty") && cardInPcmcia0) {
179 cardInPcmcia0 = FALSE;
180 } else if (!(*line).startsWith("Socket 0: empty")
181 && !cardInPcmcia0) {
182 cardInPcmcia0 = TRUE;
183 }
184 } else if ((*line).startsWith("Socket 1:")) {
185 if ((*line).startsWith("Socket 1: empty") && cardInPcmcia1) {
186 cardInPcmcia1 = FALSE;
187 } else if (!(*line).startsWith("Socket 1: empty")
188 && !cardInPcmcia1) {
189 cardInPcmcia1 = TRUE;
190 }
191 }
192 }
193 f.close();
194
195 if (cardWas0 != cardInPcmcia0 || cardWas1 != cardInPcmcia1) {
196 QString text = QString::null;
197 QString what = QString::null;
198 if (cardWas0 != cardInPcmcia0) {
199 if (cardInPcmcia0) {
200 cfRB->setEnabled(TRUE);
201 } else {
202 cfRB->setChecked(FALSE);
203 cfRB->setEnabled(FALSE);
204 }
205 }
206
207 if (cardWas1 != cardInPcmcia1) {
208 if (cardInPcmcia1) {
209 cfRB->setEnabled(TRUE);
210 } else {
211 cfRB->setChecked(FALSE);
212 cfRB->setEnabled(FALSE);
213 }
214 }
215 }
216 } else {
217 // no file found
218 qDebug("no file found");
219 cardInPcmcia0 = FALSE;
220 cardInPcmcia1 = FALSE;
221 }
222 Swapfile::cfsdchecked();
223}
224
225
226void Swapfile::getStatusSd()
227{
228
229 bool cardWas = cardInSd;// remember last state
230 cardInSd = FALSE;
231
232#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
233 struct mntent *me;
234 FILE *mntfp = setmntent("/etc/mtab", "r");
235
236 if (mntfp) {
237 while ((me = getmntent(mntfp)) != 0) {
238 QString fs = me->mnt_fsname;
239 if (fs.left(14) == "/dev/mmc/part1" || fs.left(7) == "/dev/sd"
240 || fs.left(9) == "/dev/mmcd") {
241 cardInSd = TRUE;
242 show();
243 }
244 }
245 endmntent(mntfp);
246 }
247
248 if (cardWas != cardInSd) {
249 QString text = QString::null;
250 QString what = QString::null;
251 if (cardInSd) {
252 sdRB->setEnabled(TRUE);
253 } else {
254 sdRB->setChecked(FALSE);
255 sdRB->setEnabled(FALSE);
256 }
257 }
258#else
259#error "Not on Linux"
260#endif
261 Swapfile::cfsdchecked();
262}
263
264int rc=0;
265
266void Swapfile::swapon()
267{
268 char swapcmd[128] ="swapon ";
269 Swapfile::cfsdchecked();
270 strcat(swapcmd,swapPath1->text());
271 char *runcmd = swapcmd;
272 rc = exec(QString("%1").arg(runcmd));
273 if (rc != 0) {
274 setStatusMessage("Failed to attach swapfile.", true);
275 }
276 else {
277 /* QMessageBox::information(this, "Information", "Swapfile is active!"); */
278 setStatusMessage("Swapfile activated.");
279 }
280 Swapfile::status();
281}
282
283
284void Swapfile::setStatusMessage(const QString& text, bool error /* = false */)
285{
286 swapStatus->setText("<b>" + text + "</b>");
287 swapStatusIcon->setPixmap(Resource::loadPixmap(error ? "close" : "done"));
288}
289
290
291void Swapfile::swapoff()
292{
293 char swapcmd[128] ="swapoff ";
294 if (Swapfile::cfRB->isChecked() == TRUE)
295 Swapfile::cfsdchecked();
296 strcat(swapcmd,swapPath1->text());
297 char *runcmd = swapcmd;
298 rc = exec(QString("%1").arg(runcmd));
299 if (rc != 0) {
300 setStatusMessage(tr("Failed to detach swapfile."), true);
301 }
302 else {
303 /* QMessageBox::information(this, "Information", "Swapfile is inactive!"); */
304 setStatusMessage(tr("Swapfile deactivated."));
305 /* Swapfile::swapPath->clear();*/
306 }
307 Swapfile::status();
308}
309
310void Swapfile::cfsdchecked()
311{
312 /* Swapfile::swapPath->clear();*/
313 Swapfile::swapPath1->clear();
314 if (Swapfile::ramRB->isChecked() == TRUE)
315 {
316 Swapfile::swapPath1->insert("/home/swapfile");
317 }
318 if (Swapfile::sdRB->isChecked() == TRUE)
319 {
320 Swapfile::swapPath1->insert("/mnt/card/swapfile");
321 }
322 if (Swapfile::cfRB->isChecked() == TRUE)
323 {
324 Swapfile::swapPath1->insert("/mnt/cf/swapfile");
325 }
326 /*Swapfile::swapPath->insert(Swapfile::swapPath1->text());*/
327}
328
329void Swapfile::makeswapfile()
330 {
331 int i = swapSize->currentItem();
332
333 mkswapProgress->setProgress(1);
334 switch ( i ) {
335 case 0: rc=exec(QString("dd if=/dev/zero of=%1 bs=1k count=2048").arg(swapPath1->text()));
336 break;
337 case 1: rc=exec(QString("dd if=/dev/zero of=%1 bs=1k count=4096").arg(swapPath1->text()));
338 break;
339 case 2: rc=exec(QString("dd if=/dev/zero of=%1 bs=1k count=6144").arg(swapPath1->text()));
340 break;
341 case 3: rc=exec(QString("dd if=/dev/zero of=%1 bs=1k count=8192").arg(swapPath1->text()));
342 break;
343 }
344 if (rc != 0) {
345 setStatusMessage(tr("Failed to create swapfile."), true);
346 }
347
348 mkswapProgress->setProgress(2);
349 rc=exec(QString("mkswap %1").arg(swapPath1->text()));
350 if (rc != 0) {
351 setStatusMessage(tr("Failed to initialize swapfile."), true);
352 }
353 mkswapProgress->setProgress(3);
354 mkswapProgress->reset();
355 setStatusMessage(tr("Swapfile created."));
356 }
357
358void Swapfile::removeswapfile()
359{
360 exec(QString("swapoff %1").arg(swapPath1->text()));
361 rc=exec(QString("rm -rf %1").arg(swapPath1->text()));
362 if (rc != 0) {
363 setStatusMessage(tr("Failed to remove swapfile."), true);
364 }
365 Swapfile::status();
366 Swapfile::cfsdchecked();
367 setStatusMessage(tr("Swapfile removed."));
368}
369
370void Swapfile::status()
371{
372 FILE *fp;
373 char buffer[128], swapfile[128], temp[128];
374 int swapsize=2000, i=1;
375
376 fp=fopen("/proc/swaps", "r");
377 while ( (fgets(buffer,128,fp)) != NULL ) {
378 sscanf(buffer, "%s %s %d %s %s\n", swapfile, temp, &swapsize, temp, temp);
379 }
380 fclose(fp);
381
382 ramRB->setChecked(FALSE);
383 cfRB->setChecked(FALSE);
384 sdRB->setChecked(FALSE);
385
386 i=strcmp(swapfile, "/home/swapfile");
387 if ( i == 0 ) {
388 ramRB->setChecked(TRUE);
389 /* QMessageBox::information(this, "Information", "Swapfile is active!"); */
390 setStatusMessage(tr("Swapfile activated."));
391 }
392 i=strcmp(swapfile, "/usr/mnt.rom/cf/swapfile");
393 if ( i == 0 ) {
394 cfRB->setChecked(TRUE);
395 /* QMessageBox::information(this, "Information", "Swapfile is active!"); */
396 setStatusMessage(tr("Swapfile activated."));
397 }
398 i=strcmp(swapfile, "/mnt/cf/swapfile");
399 if ( i == 0 ) {
400 cfRB->setChecked(TRUE);
401 /* QMessageBox::information(this, "Information", "Swapfile is active!"); */
402 setStatusMessage(tr("Swapfile activated."));
403 }
404 i=strcmp(swapfile, "/usr/mnt.rom/card/swapfile");
405 if ( i == 0 ) {
406 sdRB->setChecked(TRUE);
407 /* QMessageBox::information(this, "Information", "Swapfile is active!"); */
408 setStatusMessage(tr("Swapfile activated."));
409 }
410 i=strcmp(swapfile, "/mnt/card/swapfile");
411 if ( i == 0 ) {
412 sdRB->setChecked(TRUE);
413 /* QMessageBox::information(this, "Information", "Swapfile is active!"); */
414 setStatusMessage(tr("Swapfile activated."));
415 }
416
417 Swapfile::cfsdchecked();
418
419
420 swapsize /=1000;
421
422 switch ( swapsize ) {
423 case 2: swapSize->setCurrentItem(0);
424 break;
425 case 4: swapSize->setCurrentItem(1);
426 break;
427 case 6: swapSize->setCurrentItem(2);
428 break;
429 case 8: swapSize->setCurrentItem(3);
430 break;
431 }
432
433
434}
435
436
diff --git a/noncore/applets/memoryapplet/swapfile.h b/noncore/applets/memoryapplet/swapfile.h
new file mode 100644
index 0000000..8a6d25a
--- a/dev/null
+++ b/noncore/applets/memoryapplet/swapfile.h
@@ -0,0 +1,70 @@
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#ifndef SWAPFILE_H
22#define SWAPFILE_H
23
24#include <qwidget.h>
25
26class QLabel;
27class QRadioButton;
28class QLineEdit;
29class QComboBox;
30class QProgressBar;
31
32class Swapfile : public QWidget
33{
34 Q_OBJECT
35public:
36 Swapfile( QWidget *parent = 0, const char *name = 0, WFlags f = 0 );
37 ~Swapfile();
38
39private slots:
40 void swapon();
41 void swapoff();
42 void cfsdchecked();
43 void makeswapfile();
44 void removeswapfile();
45 void cardnotify( const QCString &msg, const QByteArray & );
46 void getStatusPcmcia();
47 void getStatusSd();
48 void status();
49
50private:
51 bool cardInPcmcia0;
52 bool cardInPcmcia1;
53 bool cardInSd;
54
55 QRadioButton* ramRB;
56 QRadioButton* cfRB;
57 QRadioButton* sdRB;
58 QLineEdit* swapPath1;
59 QLabel* swapStatus;
60 QLabel* swapStatusIcon;
61 QComboBox* swapSize;
62 QProgressBar* mkswapProgress;
63
64 bool isRoot;
65
66 int exec(const QString& arg);
67 void setStatusMessage(const QString& text, bool error = false);
68};
69
70#endif