2
* Copyright (C) 2008 Petri Damsten <damu@iki.fi>
3
* Copyright (C) 2010 Michel Lafon-Puyo <michel.lafonpuyo@gmail.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Library General Public License version 2 as
7
* published by the Free Software Foundation
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details
14
* You should have received a copy of the GNU Library General Public
15
* License along with this program; if not, write to the
16
* Free Software Foundation, Inc.,
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include <Plasma/Theme>
22
#include <Plasma/ToolTipManager>
23
#include <KConfigDialog>
25
#include <QGraphicsLinearLayout>
28
/* All sources we are interested in. */
29
static const char phys_source[] = "mem/physical/application";
30
static const char swap_source[] = "mem/swap/used";
32
SM::Ram::Ram(QObject *parent, const QVariantList &args)
33
: SM::Applet(parent, args)
35
setHasConfigurationInterface(true);
36
resize(234 + 20 + 23, 135 + 20 + 25);
37
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
46
KGlobal::locale()->insertCatalog("plasma_applet_system-monitor");
47
setEngine(dataEngine("systemmonitor"));
48
setTitle(i18n("RAM"));
50
/* At the time this method is running, not all source may be connected. */
51
connect(engine(), SIGNAL(sourceAdded(const QString&)), this, SLOT(sourceAdded(const QString&)));
52
foreach (const QString& source, engine()->sources()) {
57
void SM::Ram::configChanged()
59
KConfigGroup cg = config();
60
setInterval(cg.readEntry("interval", 2.0) * 1000.0);
61
setSources(cg.readEntry("memories", m_memories));
66
void SM::Ram::sourceAdded(const QString& name)
68
if ((name == phys_source || name == swap_source) && !m_memories.contains(name)) {
70
if (m_memories.count() == 2) {
71
// all sources are ready
72
QTimer::singleShot(0, this, SLOT(sourcesAdded()));
77
void SM::Ram::sourcesAdded()
82
bool SM::Ram::addVisualization(const QString& source)
84
QStringList l = source.split('/');
89
SM::Plotter *plotter = new SM::Plotter(this);
90
plotter->setTitle(ram);
91
plotter->setUnit("B");
92
appendVisualization(source, plotter);
93
setPreferredItemHeight(80);
97
void SM::Ram::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
99
SM::Plotter *plotter = qobject_cast<SM::Plotter*>(visualization(source));
101
/* A factor to convert from default units to bytes.
102
* If units is not "KB", assume it is bytes. */
103
const double factor = (data["units"].toString() == "KB") ? 1024.0 : 1.0;
104
const double value_b = data["value"].toDouble() * factor;
105
const double max_b = data["max"].toDouble() * factor;
106
static const QStringList units = QStringList() << "B" << "KiB" << "MiB" << "GiB" << "TiB";
107
if (value_b > m_max[source]) {
108
m_max[source] = max_b;
109
plotter->setMinMax(0.0, max_b);
112
while (max_b / scale > 1024.0 && i < units.size()) {
116
plotter->setUnit(units[i]);
117
plotter->setScale(scale);
120
plotter->addSample(QList<double>() << value_b);
121
QString temp = KGlobal::locale()->formatByteSize(value_b);
122
if (mode() == SM::Applet::Panel) {
123
setToolTip(source, QString("<tr><td>%1</td><td>%2</td><td>of</td><td>%3</td></tr>")
124
.arg(plotter->title())
126
.arg(KGlobal::locale()->formatByteSize(m_max[source])));
131
void SM::Ram::createConfigurationInterface(KConfigDialog *parent)
133
QWidget *widget = new QWidget();
136
m_model.setHorizontalHeaderLabels(QStringList() << i18n("RAM"));
137
QStandardItem *parentItem = m_model.invisibleRootItem();
138
QRegExp rx("mem/(\\w+)/.*");
140
foreach (const QString& ram, m_memories) {
141
if (rx.indexIn(ram) != -1) {
142
QStandardItem *item1 = new QStandardItem(rx.cap(1));
143
item1->setEditable(false);
144
item1->setCheckable(true);
146
if (sources().contains(ram)) {
147
item1->setCheckState(Qt::Checked);
149
parentItem->appendRow(QList<QStandardItem *>() << item1);
152
ui.treeView->setModel(&m_model);
153
ui.treeView->resizeColumnToContents(0);
154
ui.intervalSpinBox->setValue(interval() / 1000.0);
155
ui.intervalSpinBox->setSuffix(i18nc("second", " s"));
156
parent->addPage(widget, i18n("RAM"), "media-flash-memory-stick");
158
connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
159
connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
160
connect(ui.treeView, SIGNAL(clicked(QModelIndex)), parent, SLOT(settingsModified()));
161
connect(ui.intervalSpinBox, SIGNAL(valueChanged(QString)), parent, SLOT(settingsModified()));
164
void SM::Ram::configAccepted()
166
KConfigGroup cg = config();
167
QStandardItem *parentItem = m_model.invisibleRootItem();
171
for (int i = 0; i < parentItem->rowCount(); ++i) {
172
QStandardItem *item = parentItem->child(i, 0);
174
if (item->checkState() == Qt::Checked) {
175
appendSource(item->data().toString());
179
cg.writeEntry("memories", sources());
181
double interval = ui.intervalSpinBox->value();
182
cg.writeEntry("interval", interval);
184
emit configNeedsSaving();