Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kinfocenter / memory / memory.cpp
blob1399f39d207501619b10af85a7f715a230cde09e
1 /*
2 * memory.cpp
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.
21 #include "memory.h"
23 #include <QStringList>
25 #include <QGroupBox>
26 #include <QLayout>
27 #include <QPainter>
28 #include <QPixmap>
29 #include <QLabel>
30 #include <QVBoxLayout>
31 #include <QHBoxLayout>
32 #include <QLinearGradient>
33 #include <QTreeWidget>
35 #include <kaboutdata.h>
36 #include <kdialog.h>
37 #include <kdebug.h>
39 #include <sys/param.h> /* for BSD */
41 #include <klocale.h>
42 #include <kglobal.h>
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];
59 /******************/
60 /* Implementation */
61 /******************/
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");
80 setAboutData(about);
82 QString title, initial_str;
84 setButtons(Help);
86 QVBoxLayout *top = new QVBoxLayout(this);
87 top->setMargin(0);
88 top->setSpacing(1);
90 QGroupBox* informationGroup = initializeText();
91 top->addWidget(informationGroup, 1);
93 // Now the Graphics
94 QGroupBox* graphicsGroup = initializeCharts();
95 top->addWidget(graphicsGroup, 2);
97 timer = new QTimer(this);
98 timer->start(100);
100 connect(timer, SIGNAL(timeout()), this, SLOT(updateDatas()));
102 updateDatas();
105 KCMMemory::~KCMMemory() {
106 /* stop the timer */
107 timer->stop();
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 */
122 hbox->addStretch();
124 QString title;
126 //TODO Use the more smart QGridLayout !!!
128 /* first create the Informationtext-Widget */
129 QVBoxLayout *vbox = new QVBoxLayout();
130 hbox->addLayout(vbox);
131 vbox->setSpacing(0);
132 for (int i = TOTAL_MEM; i < MEM_LAST_ENTRY; ++i) {
133 switch (i) {
134 case TOTAL_MEM:
135 title = i18n("Total physical memory:");
136 break;
137 case FREE_MEM:
138 title = i18n("Free physical memory:");
139 break;
140 #if !defined(__svr4__) || !defined(sun)
141 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
142 case SHARED_MEM:
143 title = i18n("Shared memory:");
144 break;
145 case BUFFER_MEM:
146 title = i18n("Disk buffers:");
147 break;
148 #else
149 case ACTIVE_MEM:
150 title = i18n("Active memory:");
151 break;
152 case INACTIVE_MEM:
153 title = i18n("Inactive memory:");
154 break;
155 #endif
156 #endif
157 case CACHED_MEM:
158 title = i18n("Disk cache:");
159 break;
160 case SWAP_MEM:
161 vbox->addSpacing(SPACING);
162 title = i18n("Total swap memory:");
163 break;
164 case FREESWAP_MEM:
165 title = i18n("Free swap memory:");
166 break;
167 default:
168 title = "";
169 break;
171 QLabel* labelWidget = new QLabel(title, this);
172 labelWidget->setAlignment(Qt::AlignLeft);
173 vbox->addWidget(labelWidget);
176 vbox->addStretch();
178 /* then the memory-content-widgets */
179 for (int j = 0; j < 2; j++) {
180 vbox = new QVBoxLayout();
181 hbox->addLayout(vbox);
182 vbox->setSpacing(0);
183 for (int i = TOTAL_MEM; i < MEM_LAST_ENTRY; ++i) {
184 if (i == SWAP_MEM)
185 vbox->addSpacing(SPACING);
186 QLabel* labelWidget = new QLabel(this);
187 labelWidget->setAlignment(Qt::AlignRight);
188 memorySizeLabels[i][j] = labelWidget;
189 vbox->addWidget(labelWidget);
192 vbox->addStretch();
196 /* stretch the right side */
197 hbox->addStretch();
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> "
217 "in your system."),
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 "
234 "configured.</p>"),
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);
252 return chartsGroup;
255 void KCMMemory::updateDatas() {
257 /* get the Information from memory_linux, memory_fbsd */
258 fetchValues();
260 updateMemoryText();
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)
270 label->clear();
271 else
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 */
298 #ifdef __linux__
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"
312 #elif __osf__
313 #include "memory_tru64.cpp"
314 #else
316 /* Default for unsupported systems */
317 void KCMMemory::fetchValues() {
318 int i;
319 for (i = TOTAL_MEM; i < MEM_LAST_ENTRY; ++i) {
320 memoryInfos[i] = NO_MEMORY_INFO;
324 #endif
326 #include "memory.moc"