From e0c0894f9ec4f8e7f09d099d93623dd570f26b13 Mon Sep 17 00:00:00 2001 From: mickeyl Date: Mon, 03 Nov 2003 22:43:25 +0000 Subject: add memoryapplet (displays memory and manages a swap partition) written by Anton Maslovsky (http://my-zaurus.narod.ru) --- 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 @@ +Makefile* +advancedconfigbase.cpp +advancedconfigbase.h +moc_* 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 @@ + config MEMORYAPPLET + boolean "opie-memoryapplet (view memory status and manage a swap file)" + default "y" + depends ( LIBQPE || LIBQPE-X11 ) && LIBOPIE && SYSINFO + comment "opie-memoryapplet needs a libqpe, libopie and sysinfo" + 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 @@ +/********************************************************************** +** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** +** This file is part of the Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#include "memorymeter.h" +#include "memoryappletimpl.h" + +MemoryAppletImpl::MemoryAppletImpl() + : memory(0), ref(0) +{ +} + +MemoryAppletImpl::~MemoryAppletImpl() +{ + delete memory; +} + +QWidget *MemoryAppletImpl::applet( QWidget *parent ) +{ + if ( !memory ) + memory = new MemoryMeter( parent ); + + return memory; +} + +int MemoryAppletImpl::position() const +{ + return 8; +} + +QRESULT MemoryAppletImpl::queryInterface( const QUuid &uuid, QUnknownInterface **iface ) +{ + *iface = 0; + if ( uuid == IID_QUnknown ) + *iface = this; + else if ( uuid == IID_TaskbarApplet ) + *iface = this; + else + return QS_FALSE; + + (*iface)->addRef(); + + return QS_OK; +} + +Q_EXPORT_INTERFACE() +{ + Q_CREATE_INSTANCE( MemoryAppletImpl ) +} + 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 @@ +/********************************************************************** +** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** +** This file is part of the Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#ifndef MEMORYAPPLETIMPL_H +#define MEMORYAPPLETIMPL_H + +#include + +class MemoryMeter; + +class /*QTOPIA_PLUGIN_EXPORT*/ MemoryAppletImpl : public TaskbarAppletInterface +{ +public: + MemoryAppletImpl(); + virtual ~MemoryAppletImpl(); + + QRESULT queryInterface( const QUuid&, QUnknownInterface** ); + Q_REFCOUNT + + virtual QWidget *applet( QWidget *parent ); + virtual int position() const; + +private: + MemoryMeter *memory; + ulong ref; +}; + +#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 @@ +/********************************************************************** +** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** +** This file is part of the Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#include "memorymeter.h" +#include "memorystatus.h" + +#include +#include + +#if defined(Q_WS_QWS) && !defined(QT_NO_COP) +#include +#endif + +#include +#include +#include + +#include + +MemoryMeter::MemoryMeter( QWidget *parent ) + : QWidget( parent ), memoryView(0) +{ + bvsz = QSize(); + if ( qApp->desktop()->height() >= 300 ) + { + memoryView = new MemoryStatus( 0, WStyle_StaysOnTop | WType_Popup ); + memoryView->setFrameStyle( QFrame::Panel | QFrame::Raised ); + } + else + { + memoryView = new MemoryStatus( 0 ); + memoryView->showMaximized(); + } + + Config config("MemoryPlugin"); + config.setGroup("Warning levels"); + low = config.readNumEntry("low", 40); + critical = config.readNumEntry("critical", 20); + + startTimer( 10000 ); + setFixedWidth(10); + setFixedHeight(AppLnk::smallIconSize()); + usageTimer = new QTimer( this ); + connect( usageTimer, SIGNAL(timeout()), this, SLOT(usageTimeout()) ); + timerEvent(0); +} + +MemoryMeter::~MemoryMeter() +{ + delete (QWidget *) memoryView; +} + +QSize MemoryMeter::sizeHint() const +{ + return QSize(10, AppLnk::smallIconSize()); +} + +bool MemoryMeter::updateMemoryViewGeometry() +{ + if (memoryView != 0) + { + QSize sz = memoryView->sizeHint(); + if ( sz != bvsz ) + { + bvsz = sz; + QRect r(memoryView->pos(), memoryView->sizeHint()); + if ( qApp->desktop()->height() >= 300 ) + { + QPoint curPos = mapToGlobal( rect().topLeft() ); + int lp = qApp->desktop()->width() - memoryView->sizeHint().width(); + r.moveTopLeft( QPoint(lp, curPos.y() - memoryView->sizeHint().height()-1) ); + } + memoryView->setGeometry(r); + return TRUE; + } + return FALSE; + } + + return FALSE; +} + +void MemoryMeter::mousePressEvent( QMouseEvent *) +{ + if ( memoryView->isVisible() ) + { + memoryView->hide(); + } + else + { + bvsz = QSize(); + updateMemoryViewGeometry(); + memoryView->raise(); + memoryView->show(); + } +} + +void MemoryMeter::timerEvent( QTimerEvent * ) +{ + if (memoryView != 0) + { + // read memory status + percent = (memoryView->percent()); + usageTimer->start( 1000 ); + } +} + +void MemoryMeter::usageTimeout() +{ + if (memoryView != 0) + { + percent = (memoryView->percent()); + if (updateMemoryViewGeometry() && memoryView->isVisible()) + { + memoryView->hide(); + memoryView->show(); + } + + repaint(FALSE); + } +} + +void MemoryMeter::paintEvent( QPaintEvent* ) +{ + QPainter p(this); + + QColor c; + QColor darkc; + QColor lightc; + + if (percent > low) + c = green; + else if (percent > critical) + c = yellow.dark(110); + else + c = red; + + darkc = c.dark(120); + lightc = c.light(160); + + // + // To simulate a 3-d memory, we use 4 bands of colour. From left + // to right, these are: medium, light, medium, dark. To avoid + // hardcoding values for band "width", figure everything out on the run. + // + int batt_width; // width of each band + int batt_height; // memory height (not including terminal) + int used_height; // used amount of memory (scanlines) + + int batt_yoffset; // top of terminal + int batt_xoffset; // left edge of core + + int band_width; // width of colour band + + int w = QMIN(height(), width()); + band_width = (w-2) / 4; + if ( band_width < 1 ) + band_width = 1; + + batt_width = 4 * band_width + 2; // +2 for 1 pixel border on both sides + batt_height = height()-2; + batt_xoffset = (width() - batt_width) / 2; + batt_yoffset = (height() - batt_height) / 2; + + // + // Memory border. +1 to make space for the terminal at row 0. + // + p.setPen(QColor(80, 80, 80)); + p.drawRect(batt_xoffset, batt_yoffset + 1, batt_width, batt_height); + + // + // Draw terminal. +1 to take into account the left border. + // + //p.drawLine(batt_xoffset + band_width + 1, batt_yoffset, batt_xoffset + 3 * band_width, batt_yoffset); + + batt_height -= 2; // -2 because we don't want to include border + batt_yoffset += 2; // +2 to account for border and terminal + batt_xoffset++; + + // + // 100 - percent, since percent is amount remaining, and we draw + // reverse to this. + // + used_height = percent * batt_height / 100; + if (used_height < 0) + used_height = 0; + + // + // Drained section. + // + if (used_height != 0) + { + p.setPen(NoPen); + p.setBrush(gray); + p.drawRect(batt_xoffset, batt_yoffset, band_width, used_height); + p.drawRect(batt_xoffset + 2 * band_width, batt_yoffset, band_width, used_height); + + p.setBrush(gray/*.light(130)*/); + p.drawRect(batt_xoffset + band_width, batt_yoffset, band_width, used_height); + + p.setBrush(gray/*.dark(120)*/); + p.drawRect(batt_xoffset + 3 * band_width, batt_yoffset, band_width, used_height); + } + + // + // Unused section. + // + if ( batt_height - used_height > 0 ) + { + int unused_offset = used_height + batt_yoffset; + int unused_height = batt_height - used_height; + p.setPen(NoPen); + p.setBrush(c); + p.drawRect(batt_xoffset, unused_offset, band_width, unused_height); + p.drawRect(batt_xoffset + 2 * band_width, unused_offset, band_width, unused_height); + + p.setBrush(lightc); + p.drawRect(batt_xoffset + band_width, unused_offset, band_width, unused_height); + + p.setBrush(darkc); + p.drawRect(batt_xoffset + 3 * band_width, unused_offset, band_width, unused_height); + } +} + 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 @@ +/********************************************************************** +** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** +** This file is part of the Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#ifndef MEMORY_H +#define MEMORY_H + +#include + +class MemoryStatus; +class QTimer; + +class MemoryMeter : public QWidget +{ + Q_OBJECT +public: + MemoryMeter( QWidget *parent = 0 ); + ~MemoryMeter(); + + QSize sizeHint() const; + MemoryStatus* memoryView; + +protected: + void timerEvent( QTimerEvent * ); + void paintEvent( QPaintEvent* ); + void mousePressEvent( QMouseEvent * ); + +protected slots: + void usageTimeout(); + +protected: + QTimer *usageTimer; + int percent, low, critical; + QSize bvsz; + bool updateMemoryViewGeometry(); +}; + +#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 @@ +/********************************************************************** +** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** +** This file is part of the Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#include "memorystatus.h" +#include "../../apps/sysinfo/memory.h" +#include "swapfile.h" + +#include +#include +#include +#include + +#include +#include + +MemoryStatus::MemoryStatus(QWidget *parent, WFlags f ) + : QFrame(parent, 0, f), mi(0), sf(0) +{ + setCaption( tr("Memory Status") ); + //resize( 220, 180 ); + + QVBoxLayout *lay = new QVBoxLayout( this ); + tab = new OTabWidget( this, "tabwidget", OTabWidget::Global ); + lay->addWidget( tab ); + tab->addTab( mi = new MemoryInfo( tab ), "memory/memorytabicon", tr("Memory") ); + tab->addTab( sf = new Swapfile( tab ), "memory/storagetabicon", tr("Swapfile") ); + + QLabel* about = new QLabel(tr("
Memory Monitor Plugin
" + "Copyright (C) 2003 Anton Maslovsky
" + "<my-zaurus@narod.ru>
" + "http://my-zaurus.narod.ru
" + "Based on source code from:
qswap (udoseidel@gmx.de)
Battery Applet (trolltech.com)
SysInfo (OPIE)

" + "This program is licensed under GNU GPL.
"), tab); + + tab->addTab( about, "memory/info", tr("About") ); + + tab->setCurrentTab( tr( "Memory" ) ); +} + +int MemoryStatus::percent() +{ + if (mi == 0) + return 100; + + int total = mi->total; + if (mi->swaptotal > 0) + total += mi->swaptotal; + + int used = mi->realUsed; + if (mi->swapused > 0) + total += mi->swapused; + + return ((total - used) * 100)/total; +} + +QSize MemoryStatus::sizeHint() const +{ + QSize s = tab->size(); + s.setWidth(200); + s.setHeight((mi->swaptotal > 0) ? 220 : 200); + return s; +} + +MemoryStatus::~MemoryStatus() +{ +} 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 @@ +/********************************************************************** +** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** +** This file is part of the Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ +#ifndef MEMORY_STATUS_H +#define MEMORY_STATUS_H + +#include + +class MemoryInfo; +class Swapfile; +class OTabWidget; + +class MemoryStatus : public QFrame +{ + Q_OBJECT +public: + MemoryStatus(QWidget *parent = 0, WFlags f = 0); + ~MemoryStatus(); + + QSize sizeHint() const; + MemoryInfo* mi; + Swapfile* sf; + + int percent(); + +private: + OTabWidget *tab; +}; + +#endif + 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 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#include "swapfile.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +Swapfile::Swapfile( QWidget *parent, const char *name, WFlags f ) + : QWidget( parent, name, f ) +{ + // are we running as root? + isRoot = geteuid() == 0; + + QVBoxLayout* vb = new QVBoxLayout(this, 5); + + QHButtonGroup* cfsdRBG = new QHButtonGroup(tr("Swapfile location"), this); + cfsdRBG->setRadioButtonExclusive(true); + vb->addWidget(cfsdRBG); + + ramRB = new QRadioButton(tr("RAM"), cfsdRBG); + cfRB = new QRadioButton(tr("CF Card"), cfsdRBG); + sdRB = new QRadioButton(tr("SD Card"), cfsdRBG); + + QHBox *hb1 = new QHBox(this); + hb1->setSpacing(5); + + swapPath1 = new QLineEdit(hb1); + swapPath1->setEnabled(false); + + QPushButton* swapOn = new QPushButton(tr(" On "), hb1); + QPushButton* swapOff = new QPushButton(tr(" Off "), hb1); + vb->addWidget(hb1); + + QVGroupBox* box1 = new QVGroupBox(tr("Manage Swapfile"), this); + vb->addWidget(box1); + + QHBox *hb2 = new QHBox(box1); + hb2->setSpacing(5); + QPushButton* mkSwap = new QPushButton(tr("Generate"), hb2); + QPushButton* rmSwap = new QPushButton(tr("Remove"), hb2); + + QHBox *hb3 = new QHBox(box1); + hb3->setSpacing(5); + swapSize = new QComboBox(hb3); + swapSize->insertStringList(QStringList::split(",", tr("2 Mb,4 Mb,6 Mb,8 Mb"))); + + mkswapProgress = new QProgressBar(3, hb3); + mkswapProgress->setCenterIndicator(true); + + QHBox *hb4 = new QHBox(this); + hb4->setSpacing(5); + + swapStatusIcon = new QLabel(hb4); + swapStatus = new QLabel(tr(""), hb4); + hb4->setStretchFactor(swapStatus, 99); + vb->addWidget(hb4); + + connect(swapOn, SIGNAL(clicked()), this, SLOT(swapon())); + connect(swapOff, SIGNAL(clicked()), this, SLOT(swapoff())); + connect(cfRB, SIGNAL(clicked()), this, SLOT(cfsdchecked())); + connect(sdRB, SIGNAL(clicked()), this, SLOT(cfsdchecked())); + connect(ramRB, SIGNAL(clicked()), this, SLOT(cfsdchecked())); + connect(mkSwap, SIGNAL(clicked()), this, SLOT(makeswapfile())); + connect(rmSwap, SIGNAL(clicked()), this, SLOT(removeswapfile())); + + cfRB->setEnabled(FALSE); + sdRB->setEnabled(FALSE); + + QCopChannel *pcmciaChannel = new QCopChannel("QPE/Card", this); + connect(pcmciaChannel, SIGNAL(received(const QCString &, const QByteArray &)), this, SLOT(cardnotify(const QCString &, const QByteArray &))); + QCopChannel *sdChannel = new QCopChannel("QPE/Card", this); + connect(sdChannel, SIGNAL(received(const QCString &, const QByteArray &)), this, SLOT(cardnotify(const QCString &, const QByteArray &))); + + cardInPcmcia0 = FALSE; + cardInPcmcia1 = FALSE; + cardInSd = FALSE; + + Swapfile::status(); + Swapfile::getStatusPcmcia(); + Swapfile::getStatusSd(); +} + +int Swapfile::exec(const QString& arg) +{ + return system((!isRoot ? "sudo " : "") + arg); +} + + +Swapfile::~Swapfile() +{ +} + +void Swapfile::cardnotify(const QCString & msg, const QByteArray &) +{ + if (msg == "stabChanged()") + { + getStatusPcmcia(); + } + else if (msg == "mtabChanged()") + { + getStatusSd(); + } +} + +void Swapfile::getStatusPcmcia() +{ + + bool cardWas0 = cardInPcmcia0; // remember last state + bool cardWas1 = cardInPcmcia1; + + QString fileName; + + // one of these 3 files should exist + if (QFile::exists("/var/run/stab")) { + fileName = "/var/run/stab"; + } else if (QFile::exists("/var/state/pcmcia/stab")) { + fileName = "/var/state/pcmcia/stab"; + } else { + fileName = "/var/lib/pcmcia/stab"; + } + + QFile f(fileName); + + if (f.open(IO_ReadOnly)) { + QStringList list; + QTextStream stream(&f); + QString streamIn; + streamIn = stream.read(); + list = QStringList::split("\n", streamIn); + for (QStringList::Iterator line = list.begin(); line != list.end(); + line++) { + if ((*line).startsWith("Socket 0:")) { + if ((*line).startsWith("Socket 0: empty") && cardInPcmcia0) { + cardInPcmcia0 = FALSE; + } else if (!(*line).startsWith("Socket 0: empty") + && !cardInPcmcia0) { + cardInPcmcia0 = TRUE; + } + } else if ((*line).startsWith("Socket 1:")) { + if ((*line).startsWith("Socket 1: empty") && cardInPcmcia1) { + cardInPcmcia1 = FALSE; + } else if (!(*line).startsWith("Socket 1: empty") + && !cardInPcmcia1) { + cardInPcmcia1 = TRUE; + } + } + } + f.close(); + + if (cardWas0 != cardInPcmcia0 || cardWas1 != cardInPcmcia1) { + QString text = QString::null; + QString what = QString::null; + if (cardWas0 != cardInPcmcia0) { + if (cardInPcmcia0) { + cfRB->setEnabled(TRUE); + } else { + cfRB->setChecked(FALSE); + cfRB->setEnabled(FALSE); + } + } + + if (cardWas1 != cardInPcmcia1) { + if (cardInPcmcia1) { + cfRB->setEnabled(TRUE); + } else { + cfRB->setChecked(FALSE); + cfRB->setEnabled(FALSE); + } + } + } + } else { + // no file found + qDebug("no file found"); + cardInPcmcia0 = FALSE; + cardInPcmcia1 = FALSE; + } + Swapfile::cfsdchecked(); +} + + +void Swapfile::getStatusSd() +{ + + bool cardWas = cardInSd; // remember last state + cardInSd = FALSE; + +#if defined(_OS_LINUX_) || defined(Q_OS_LINUX) + struct mntent *me; + FILE *mntfp = setmntent("/etc/mtab", "r"); + + if (mntfp) { + while ((me = getmntent(mntfp)) != 0) { + QString fs = me->mnt_fsname; + if (fs.left(14) == "/dev/mmc/part1" || fs.left(7) == "/dev/sd" + || fs.left(9) == "/dev/mmcd") { + cardInSd = TRUE; + show(); + } + } + endmntent(mntfp); + } + + if (cardWas != cardInSd) { + QString text = QString::null; + QString what = QString::null; + if (cardInSd) { + sdRB->setEnabled(TRUE); + } else { + sdRB->setChecked(FALSE); + sdRB->setEnabled(FALSE); + } + } +#else +#error "Not on Linux" +#endif + Swapfile::cfsdchecked(); +} + +int rc=0; + +void Swapfile::swapon() +{ + char swapcmd[128] ="swapon "; + Swapfile::cfsdchecked(); + strcat(swapcmd,swapPath1->text()); + char *runcmd = swapcmd; + rc = exec(QString("%1").arg(runcmd)); + if (rc != 0) { + setStatusMessage("Failed to attach swapfile.", true); + } + else { +/* QMessageBox::information(this, "Information", "Swapfile is active!"); */ + setStatusMessage("Swapfile activated."); + } + Swapfile::status(); +} + + +void Swapfile::setStatusMessage(const QString& text, bool error /* = false */) +{ + swapStatus->setText("" + text + ""); + swapStatusIcon->setPixmap(Resource::loadPixmap(error ? "close" : "done")); +} + + +void Swapfile::swapoff() +{ + char swapcmd[128] ="swapoff "; + if (Swapfile::cfRB->isChecked() == TRUE) + Swapfile::cfsdchecked(); + strcat(swapcmd,swapPath1->text()); + char *runcmd = swapcmd; + rc = exec(QString("%1").arg(runcmd)); + if (rc != 0) { + setStatusMessage(tr("Failed to detach swapfile."), true); + } + else { +/* QMessageBox::information(this, "Information", "Swapfile is inactive!"); */ + setStatusMessage(tr("Swapfile deactivated.")); +/* Swapfile::swapPath->clear();*/ + } + Swapfile::status(); +} + +void Swapfile::cfsdchecked() +{ +/* Swapfile::swapPath->clear();*/ + Swapfile::swapPath1->clear(); + if (Swapfile::ramRB->isChecked() == TRUE) + { + Swapfile::swapPath1->insert("/home/swapfile"); + } + if (Swapfile::sdRB->isChecked() == TRUE) + { + Swapfile::swapPath1->insert("/mnt/card/swapfile"); + } + if (Swapfile::cfRB->isChecked() == TRUE) + { + Swapfile::swapPath1->insert("/mnt/cf/swapfile"); + } +/* Swapfile::swapPath->insert(Swapfile::swapPath1->text());*/ +} + +void Swapfile::makeswapfile() +{ + int i = swapSize->currentItem(); + + mkswapProgress->setProgress(1); + switch ( i ) { + case 0: rc=exec(QString("dd if=/dev/zero of=%1 bs=1k count=2048").arg(swapPath1->text())); + break; + case 1: rc=exec(QString("dd if=/dev/zero of=%1 bs=1k count=4096").arg(swapPath1->text())); + break; + case 2: rc=exec(QString("dd if=/dev/zero of=%1 bs=1k count=6144").arg(swapPath1->text())); + break; + case 3: rc=exec(QString("dd if=/dev/zero of=%1 bs=1k count=8192").arg(swapPath1->text())); + break; + } + if (rc != 0) { + setStatusMessage(tr("Failed to create swapfile."), true); + } + + mkswapProgress->setProgress(2); + rc=exec(QString("mkswap %1").arg(swapPath1->text())); + if (rc != 0) { + setStatusMessage(tr("Failed to initialize swapfile."), true); + } + mkswapProgress->setProgress(3); + mkswapProgress->reset(); + setStatusMessage(tr("Swapfile created.")); +} + +void Swapfile::removeswapfile() +{ + exec(QString("swapoff %1").arg(swapPath1->text())); + rc=exec(QString("rm -rf %1").arg(swapPath1->text())); + if (rc != 0) { + setStatusMessage(tr("Failed to remove swapfile."), true); + } + Swapfile::status(); + Swapfile::cfsdchecked(); + setStatusMessage(tr("Swapfile removed.")); +} + +void Swapfile::status() +{ + FILE *fp; + char buffer[128], swapfile[128], temp[128]; + int swapsize=2000, i=1; + + fp=fopen("/proc/swaps", "r"); + while ( (fgets(buffer,128,fp)) != NULL ) { + sscanf(buffer, "%s %s %d %s %s\n", swapfile, temp, &swapsize, temp, temp); + } + fclose(fp); + + ramRB->setChecked(FALSE); + cfRB->setChecked(FALSE); + sdRB->setChecked(FALSE); + + i=strcmp(swapfile, "/home/swapfile"); + if ( i == 0 ) { + ramRB->setChecked(TRUE); +/* QMessageBox::information(this, "Information", "Swapfile is active!"); */ + setStatusMessage(tr("Swapfile activated.")); + } + i=strcmp(swapfile, "/usr/mnt.rom/cf/swapfile"); + if ( i == 0 ) { + cfRB->setChecked(TRUE); +/* QMessageBox::information(this, "Information", "Swapfile is active!"); */ + setStatusMessage(tr("Swapfile activated.")); + } + i=strcmp(swapfile, "/mnt/cf/swapfile"); + if ( i == 0 ) { + cfRB->setChecked(TRUE); +/* QMessageBox::information(this, "Information", "Swapfile is active!"); */ + setStatusMessage(tr("Swapfile activated.")); + } + i=strcmp(swapfile, "/usr/mnt.rom/card/swapfile"); + if ( i == 0 ) { + sdRB->setChecked(TRUE); +/* QMessageBox::information(this, "Information", "Swapfile is active!"); */ + setStatusMessage(tr("Swapfile activated.")); + } + i=strcmp(swapfile, "/mnt/card/swapfile"); + if ( i == 0 ) { + sdRB->setChecked(TRUE); +/* QMessageBox::information(this, "Information", "Swapfile is active!"); */ + setStatusMessage(tr("Swapfile activated.")); + } + + Swapfile::cfsdchecked(); + + + swapsize /=1000; + + switch ( swapsize ) { + case 2: swapSize->setCurrentItem(0); + break; + case 4: swapSize->setCurrentItem(1); + break; + case 6: swapSize->setCurrentItem(2); + break; + case 8: swapSize->setCurrentItem(3); + break; + } + + +} + + 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 @@ +/********************************************************************** +** Copyright (C) 2000 Trolltech AS. All rights reserved. +** +** This file is part of Qtopia Environment. +** +** This file may be distributed and/or modified under the terms of the +** GNU General Public License version 2 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** See http://www.trolltech.com/gpl/ for GPL licensing information. +** +** Contact info@trolltech.com if any conditions of this licensing are +** not clear to you. +** +**********************************************************************/ + +#ifndef SWAPFILE_H +#define SWAPFILE_H + +#include + +class QLabel; +class QRadioButton; +class QLineEdit; +class QComboBox; +class QProgressBar; + +class Swapfile : public QWidget +{ + Q_OBJECT +public: + Swapfile( QWidget *parent = 0, const char *name = 0, WFlags f = 0 ); + ~Swapfile(); + +private slots: + void swapon(); + void swapoff(); + void cfsdchecked(); + void makeswapfile(); + void removeswapfile(); + void cardnotify( const QCString &msg, const QByteArray & ); + void getStatusPcmcia(); + void getStatusSd(); + void status(); + +private: + bool cardInPcmcia0; + bool cardInPcmcia1; + bool cardInSd; + + QRadioButton* ramRB; + QRadioButton* cfRB; + QRadioButton* sdRB; + QLineEdit* swapPath1; + QLabel* swapStatus; + QLabel* swapStatusIcon; + QComboBox* swapSize; + QProgressBar* mkswapProgress; + + bool isRoot; + + int exec(const QString& arg); + void setStatusMessage(const QString& text, bool error = false); +}; + +#endif -- cgit v0.9.0.2