File: memory.cpp

package info (click to toggle)
kde-workspace 4%3A4.8.4-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 89,920 kB
  • sloc: cpp: 373,584; ansic: 35,020; xml: 7,435; perl: 1,550; sh: 1,329; ruby: 1,135; python: 646; asm: 566; makefile: 38
file content (326 lines) | stat: -rw-r--r-- 8,224 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

/*
 *  memory.cpp
 *
 *  Copyright (C) 2008 Ivo Anjo <knuckles@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "memory.h"

#include <QStringList>

#include <QGroupBox>
#include <QLayout>
#include <QPainter>
#include <QPixmap>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLinearGradient>
#include <QTreeWidget>

#include <kaboutdata.h>
#include <kdialog.h>
#include <kdebug.h>

#include <sys/param.h>		/* for BSD */

#include <klocale.h>
#include <kglobal.h>

#include <KPluginFactory>
#include <KPluginLoader>

#include "chartWidget.h"

#include "physicalMemoryChart.h"
#include "totalMemoryChart.h"
#include "swapMemoryChart.h"

/*
 all fetchValues()-functions should put either
 their results _OR_ the value NO_MEMORY_INFO into memoryInfos[]
 */
static t_memsize memoryInfos[MEM_LAST_ENTRY];

/******************/
/* Implementation */
/******************/

static QLabel *memorySizeLabels[MEM_LAST_ENTRY][2];


K_PLUGIN_FACTORY(KCMMemoryFactory,
		registerPlugin<KCMMemory>();
)
K_EXPORT_PLUGIN(KCMMemoryFactory("kcm_memory"))

KCMMemory::KCMMemory(QWidget *parent, const QVariantList &) :
	KCModule(KCMMemoryFactory::componentData(), parent) {

	KAboutData *about = new KAboutData(I18N_NOOP("kcm_memory"), 0,
			ki18n("KDE Panel Memory Information Control Module"),
			0, KLocalizedString(), KAboutData::License_GPL,
			ki18n("(c) 1998 - 2002 Helge Deller"));

	about->addAuthor(ki18n("Helge Deller"), KLocalizedString(), "deller@gmx.de");
	setAboutData(about);

	QString title, initial_str;

	setButtons(Help);

	QVBoxLayout *top = new QVBoxLayout(this);
	top->setMargin(0);
	top->setSpacing(1);

	QGroupBox* informationGroup = initializeText();
	top->addWidget(informationGroup, 1);

	// Now the Graphics
	QGroupBox* graphicsGroup = initializeCharts();
	top->addWidget(graphicsGroup, 2);

	timer = new QTimer(this);
	timer->start(100);

	connect(timer, SIGNAL(timeout()), this, SLOT(updateDatas()));
	
	updateDatas();
}

KCMMemory::~KCMMemory() {
	/* stop the timer */
	timer->stop();
}

QString KCMMemory::quickHelp() const {
	return i18n("This display shows you the current memory usage of your system."
		" The values are updated on a regular basis and give you an"
		" overview of the physical and virtual memory being used.");
}

QGroupBox* KCMMemory::initializeText() {
	QGroupBox* informationGroup = new QGroupBox(i18n("Memory"));

	QHBoxLayout *hbox = new QHBoxLayout(informationGroup);

	/* stretch the left side */
	hbox->addStretch();

	QString title;

	//TODO Use the more smart QGridLayout !!!

	/* first create the Informationtext-Widget */
	QVBoxLayout *vbox = new QVBoxLayout();
	hbox->addLayout(vbox);
	vbox->setSpacing(0);
	for (int i = TOTAL_MEM; i < MEM_LAST_ENTRY; ++i) {
		switch (i) {
		case TOTAL_MEM:
			title = i18n("Total physical memory:");
			break;
		case FREE_MEM:
			title = i18n("Free physical memory:");
			break;
#if !defined(__svr4__) || !defined(sun)
#if !defined(__NetBSD__) && !defined(__OpenBSD__)
		case SHARED_MEM:
			title = i18n("Shared memory:");
			break;
		case BUFFER_MEM:
			title = i18n("Disk buffers:");
			break;
#else
			case ACTIVE_MEM:
			title = i18n("Active memory:");
			break;
			case INACTIVE_MEM:
			title = i18n("Inactive memory:");
			break;
#endif
#endif
		case CACHED_MEM:
			title = i18n("Disk cache:");
			break;
		case SWAP_MEM:
			vbox->addSpacing(SPACING);
			title = i18n("Total swap memory:");
			break;
		case FREESWAP_MEM:
			title = i18n("Free swap memory:");
			break;
		default:
			title = "";
			break;
		};
		QLabel* labelWidget = new QLabel(title, this);
		labelWidget->setAlignment(Qt::AlignLeft);
		vbox->addWidget(labelWidget);
	}

	vbox->addStretch();

	/* then the memory-content-widgets */
	for (int j = 0; j < 2; j++) {
		vbox = new QVBoxLayout();
		hbox->addLayout(vbox);
		vbox->setSpacing(0);
		for (int i = TOTAL_MEM; i < MEM_LAST_ENTRY; ++i) {
			if (i == SWAP_MEM)
				vbox->addSpacing(SPACING);
			QLabel* labelWidget = new QLabel(this);
			labelWidget->setAlignment(Qt::AlignRight);
			memorySizeLabels[i][j] = labelWidget;
			vbox->addWidget(labelWidget);
		}

		vbox->addStretch();

	}

	/* stretch the right side */
	hbox->addStretch();

	return informationGroup;

}

QGroupBox* KCMMemory::initializeCharts() {
	QGroupBox* chartsGroup = new QGroupBox(i18n("Charts"));

	QHBoxLayout* chartsLayout = new QHBoxLayout(chartsGroup);
	chartsLayout->setSpacing(1);
	chartsLayout->setMargin(1);

	//chartsLayout->addStretch(1);


	
	totalMemory = new ChartWidget(i18n("Total Memory"), 
			i18n("This graph gives you an overview of the "
			"<b>total sum of physical and virtual memory</b> "
			"in your system."), 
			new TotalMemoryChart(this), this);
	
	chartsLayout->addWidget(totalMemory);
	chartsLayout->addSpacing(SPACING);


	physicalMemory = new ChartWidget(i18n("Physical Memory"), 
			i18n("This graph gives you an overview of "
					"the <b>usage of physical memory</b> in your system."
					"<p>Most operating systems (including Linux) "
					"will use as much of the available physical "
					"memory as possible as disk cache, "
					"to speed up the system performance.</p>"
					"<p>This means that if you have a small amount "
					"of <b>Free Physical Memory</b> and a large amount of "
					"<b>Disk Cache Memory</b>, your system is well "
					"configured.</p>"), 
			new PhysicalMemoryChart(this), this);
	
	chartsLayout->addWidget(physicalMemory);
	chartsLayout->addSpacing(SPACING);

	swapMemory = new ChartWidget(i18n("Swap Space"), 
			i18n("<p>The swap space is the <b>virtual memory</b> "
				"available to the system.</p> "
				"<p>It will be used on demand and is provided "
				"through one or more swap partitions and/or swap files.</p>"), 
			new SwapMemoryChart(this), this);

	
	chartsLayout->addWidget(swapMemory);

	//chartsLayout->addStretch(1);

	return chartsGroup;
}

void KCMMemory::updateDatas() {

	/* get the Information from memory_linux, memory_fbsd */
	fetchValues(); 
	
	updateMemoryText();
	updateMemoryGraphics();
}


void KCMMemory::updateMemoryText() {
	/* update the byte-strings */
	for (int i = TOTAL_MEM; i < MEM_LAST_ENTRY; i++) {
		QLabel* label = memorySizeLabels[i][0];
		if (memoryInfos[i] == NO_MEMORY_INFO)
			label->clear();
		else
			label->setText(i18np("1 byte =", "%1 bytes =", memoryInfos[i]));
	}

	/* update the MB-strings */
	for (int i = TOTAL_MEM; i < MEM_LAST_ENTRY; i++) {
		QLabel* label = memorySizeLabels[i][1];
		label->setText((memoryInfos[i] != NO_MEMORY_INFO) ? Chart::formattedUnit(memoryInfos[i]) : i18n("Not available."));
	}

}

void KCMMemory::updateMemoryGraphics() {
	totalMemory->setMemoryInfos(memoryInfos);
	totalMemory->refresh();

	physicalMemory->setMemoryInfos(memoryInfos);
	physicalMemory->refresh();

	swapMemory->setMemoryInfos(memoryInfos);
	swapMemory->refresh();

}

/* Include system-specific code */

#ifdef __linux__
#include "memory_linux.cpp"
#elif defined(__APPLE__)
#include "memory_osx.cpp"
#elif defined(sgi) && sgi
#include "memory_sgi.cpp"
#elif defined(__svr4__) && defined(sun)
#include "memory_solaris.cpp"
#elif defined(__FreeBSD__) || defined(__DragonFly__)
#include "memory_fbsd.cpp"
#elif defined(__hpux)
#include "memory_hpux.cpp"
#elif defined(__NetBSD__) || defined(__OpenBSD__)
#include "memory_netbsd.cpp"
#elif defined(__osf__)
#include "memory_tru64.cpp"
#else

/* Default for unsupported systems */
void KCMMemory::fetchValues() {
	int i;
	for (i = TOTAL_MEM; i < MEM_LAST_ENTRY; ++i) {
		memoryInfos[i] = NO_MEMORY_INFO;
	}
}

#endif

#include "memory.moc"