4 * Copyright (C) 2008 Ivo Anjo <knuckles@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <QStringList>
30 #include <QVBoxLayout>
31 #include <QHBoxLayout>
32 #include <QLinearGradient>
33 #include <QTreeWidget>
35 #include <kaboutdata.h>
39 #include <sys/param.h> /* for BSD */
44 #include <KPluginFactory>
45 #include <KPluginLoader>
47 #include "chartWidget.h"
49 #include "physicalMemoryChart.h"
50 #include "totalMemoryChart.h"
51 #include "swapMemoryChart.h"
54 all fetchValues()-functions should put either
55 their results _OR_ the value NO_MEMORY_INFO into memoryInfos[]
57 static t_memsize memoryInfos
[MEM_LAST_ENTRY
];
63 static QLabel
*memorySizeLabels
[MEM_LAST_ENTRY
][2];
66 K_PLUGIN_FACTORY(KCMMemoryFactory
,
67 registerPlugin
<KCMMemory
>();
69 K_EXPORT_PLUGIN(KCMMemoryFactory("kcm_memory"))
71 KCMMemory::KCMMemory(QWidget
*parent
, const QVariantList
&) :
72 KCModule(KCMMemoryFactory::componentData(), parent
) {
74 KAboutData
*about
= new KAboutData(I18N_NOOP("kcm_memory"), 0,
75 ki18n("KDE Panel Memory Information Control Module"),
76 0, KLocalizedString(), KAboutData::License_GPL
,
77 ki18n("(c) 1998 - 2002 Helge Deller"));
79 about
->addAuthor(ki18n("Helge Deller"), KLocalizedString(), "deller@gmx.de");
82 QString title
, initial_str
;
86 QVBoxLayout
*top
= new QVBoxLayout(this);
90 QGroupBox
* informationGroup
= initializeText();
91 top
->addWidget(informationGroup
, 1);
94 QGroupBox
* graphicsGroup
= initializeCharts();
95 top
->addWidget(graphicsGroup
, 2);
97 timer
= new QTimer(this);
100 connect(timer
, SIGNAL(timeout()), this, SLOT(updateDatas()));
105 KCMMemory::~KCMMemory() {
110 QString
KCMMemory::quickHelp() const {
111 return i18n("This display shows you the current memory usage of your system."
112 " The values are updated on a regular basis and give you an"
113 " overview of the physical and virtual memory being used.");
116 QGroupBox
* KCMMemory::initializeText() {
117 QGroupBox
* informationGroup
= new QGroupBox(i18n("Memory"));
119 QHBoxLayout
*hbox
= new QHBoxLayout(informationGroup
);
121 /* stretch the left side */
126 //TODO Use the more smart QGridLayout !!!
128 /* first create the Informationtext-Widget */
129 QVBoxLayout
*vbox
= new QVBoxLayout();
130 hbox
->addLayout(vbox
);
132 for (int i
= TOTAL_MEM
; i
< MEM_LAST_ENTRY
; ++i
) {
135 title
= i18n("Total physical memory:");
138 title
= i18n("Free physical memory:");
140 #if !defined(__svr4__) || !defined(sun)
141 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
143 title
= i18n("Shared memory:");
146 title
= i18n("Disk buffers:");
150 title
= i18n("Active memory:");
153 title
= i18n("Inactive memory:");
158 title
= i18n("Disk cache:");
161 vbox
->addSpacing(SPACING
);
162 title
= i18n("Total swap memory:");
165 title
= i18n("Free swap memory:");
171 QLabel
* labelWidget
= new QLabel(title
, this);
172 labelWidget
->setAlignment(Qt::AlignLeft
);
173 vbox
->addWidget(labelWidget
);
178 /* then the memory-content-widgets */
179 for (int j
= 0; j
< 2; j
++) {
180 vbox
= new QVBoxLayout();
181 hbox
->addLayout(vbox
);
183 for (int i
= TOTAL_MEM
; i
< MEM_LAST_ENTRY
; ++i
) {
185 vbox
->addSpacing(SPACING
);
186 QLabel
* labelWidget
= new QLabel(this);
187 labelWidget
->setAlignment(Qt::AlignRight
);
188 memorySizeLabels
[i
][j
] = labelWidget
;
189 vbox
->addWidget(labelWidget
);
196 /* stretch the right side */
199 return informationGroup
;
203 QGroupBox
* KCMMemory::initializeCharts() {
204 QGroupBox
* chartsGroup
= new QGroupBox(i18n("Charts"));
206 QHBoxLayout
* chartsLayout
= new QHBoxLayout(chartsGroup
);
207 chartsLayout
->setSpacing(1);
208 chartsLayout
->setMargin(1);
210 //chartsLayout->addStretch(1);
214 totalMemory
= new ChartWidget(i18n("Total Memory"),
215 i18n("This graph gives you an overview of the "
216 "<b>total sum of physical and virtual memory</b> "
218 new TotalMemoryChart(this), this);
220 chartsLayout
->addWidget(totalMemory
);
221 chartsLayout
->addSpacing(SPACING
);
224 physicalMemory
= new ChartWidget(i18n("Physical Memory"),
225 i18n("This graph gives you an overview of "
226 "the <b>usage of physical memory</b> in your system."
227 "<p>Most operating systems (including Linux) "
228 "will use as much of the available physical "
229 "memory as possible as disk cache, "
230 "to speed up the system performance.</p>"
231 "<p>This means that if you have a small amount "
232 "of <b>Free Physical Memory</b> and a large amount of "
233 "<b>Disk Cache Memory</b>, your system is well "
235 new PhysicalMemoryChart(this), this);
237 chartsLayout
->addWidget(physicalMemory
);
238 chartsLayout
->addSpacing(SPACING
);
240 swapMemory
= new ChartWidget(i18n("Swap Space"),
241 i18n("<p>The swap space is the <b>virtual memory</b> "
242 "available to the system.</p> "
243 "<p>It will be used on demand and is provided "
244 "through one or more swap partitions and/or swap files.</p>"),
245 new SwapMemoryChart(this), this);
248 chartsLayout
->addWidget(swapMemory
);
250 //chartsLayout->addStretch(1);
255 void KCMMemory::updateDatas() {
257 /* get the Information from memory_linux, memory_fbsd */
261 updateMemoryGraphics();
265 void KCMMemory::updateMemoryText() {
266 /* update the byte-strings */
267 for (int i
= TOTAL_MEM
; i
< MEM_LAST_ENTRY
; i
++) {
268 QLabel
* label
= memorySizeLabels
[i
][0];
269 if (memoryInfos
[i
] == NO_MEMORY_INFO
)
272 label
->setText(i18n("%1 bytes =", KGlobal::locale()->
273 formatNumber(memoryInfos
[i
], 0)));
276 /* update the MB-strings */
277 for (int i
= TOTAL_MEM
; i
< MEM_LAST_ENTRY
; i
++) {
278 QLabel
* label
= memorySizeLabels
[i
][1];
279 label
->setText((memoryInfos
[i
] != NO_MEMORY_INFO
) ? Chart::formattedUnit(memoryInfos
[i
]) : i18n("Not available."));
284 void KCMMemory::updateMemoryGraphics() {
285 totalMemory
->setMemoryInfos(memoryInfos
);
286 totalMemory
->refresh();
288 physicalMemory
->setMemoryInfos(memoryInfos
);
289 physicalMemory
->refresh();
291 swapMemory
->setMemoryInfos(memoryInfos
);
292 swapMemory
->refresh();
296 /* Include system-specific code */
299 #include "memory_linux.cpp"
300 #elif defined(__APPLE__)
301 #include "memory_osx.cpp"
302 #elif defined(sgi) && sgi
303 #include "memory_sgi.cpp"
304 #elif defined(__svr4__) && defined(sun)
305 #include "memory_solaris.cpp"
306 #elif defined(__FreeBSD__) || defined(__DragonFly__)
307 #include "memory_fbsd.cpp"
308 #elif defined(__hpux)
309 #include "memory_hpux.cpp"
310 #elif defined(__NetBSD__) || defined(__OpenBSD__)
311 #include "memory_netbsd.cpp"
313 #include "memory_tru64.cpp"
316 /* Default for unsupported systems */
317 void KCMMemory::fetchValues() {
319 for (i
= TOTAL_MEM
; i
< MEM_LAST_ENTRY
; ++i
) {
320 memoryInfos
[i
] = NO_MEMORY_INFO
;
326 #include "memory.moc"