Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kinfocenter / base / info_netbsd.cpp
blob6ad4e37dc01e5e36d17e4f377e8df6d9e786e03c
1 /*
2 * info_netbsd.cpp is part of the KDE program kcminfo. This displays
3 * various information about the NetBSD system it's running on.
5 * Originally written by Jaromir Dolecek <dolecek@ics.muni.cz>. CPU info
6 * code has been imported from implementation of processor.cpp for KDE 1.0
7 * by David Brownlee <abs@NetBSD.org> as found in NetBSD packages collection.
8 * Hubert Feyer <hubertf@NetBSD.org> enhanced the sound information printing
9 * quite a lot, too.
11 * The code is placed into public domain. Do whatever you want with it.
15 * all following functions should return true, when the Information
16 * was filled into the Tree Widget. Returning false indicates that
17 * information was not available.
20 #include <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/sysctl.h>
23 #include <sys/mount.h>
24 #include <stdio.h> /* for NULL */
25 #include <stdlib.h> /* for malloc(3) */
26 #include <fstab.h>
28 #include <QFile>
29 #include <QFontMetrics>
30 #include <QStringList>
31 #include <QTextStream>
32 #include <QTreeWidgetItemIterator>
34 #include <kdebug.h>
35 #include <kio/global.h> /* for KIO::convertSize() */
37 typedef struct {
38 int string;
39 int name;
40 const char *title;
41 } hw_info_mib_list_t;
43 bool GetInfo_CPU(QTreeWidget* tree) {
44 static hw_info_mib_list_t hw_info_mib_list[]= { { 1, HW_MODEL, "Model" }, { 1, HW_MACHINE, "Machine" }, { 1, HW_MACHINE_ARCH, "Architecture" }, { 0, HW_NCPU, "Number of CPUs" }, { 0, HW_PAGESIZE, "Pagesize" }, { 0, 0, 0 } };
45 hw_info_mib_list_t *hw_info_mib;
47 int mib[2], num;
48 char *buf;
49 size_t len;
50 QString value;
52 QStringList list;
53 list << i18n("Information") << i18n("Value");
54 tree->setHeaderLabels(list);
56 for (hw_info_mib = hw_info_mib_list; hw_info_mib->title; ++hw_info_mib) {
57 mib[0] = CTL_HW;
58 mib[1] = hw_info_mib->name;
59 if (hw_info_mib->string) {
60 sysctl(mib, 2, NULL, &len, NULL, 0);
61 if ( (buf = (char*)malloc(len))) {
62 sysctl(mib, 2, buf, &len, NULL, 0);
63 value = QString::fromLocal8Bit(buf);
64 free(buf);
65 } else {
66 value = QString("Unknown");
68 } else {
69 len = sizeof(num);
70 sysctl(mib, 2, &num, &len, NULL, 0);
71 value = QString::number(num);
74 QStringList list;
75 list << hw_info_mib->title << value;
77 new QTreeWidgetItem(tree, list);
80 return true;
83 // this is used to find out which devices are currently
84 // on system
85 static bool GetDmesgInfo(QTreeWidget* tree, const char *filter, void func(QTreeWidget* tree, QString s)) {
86 QFile *dmesg = new QFile("/var/run/dmesg.boot");
87 bool usepipe = false;
88 FILE *pipe= NULL;
89 QTextStream *t;
90 bool seencpu = false;
91 QString s;
92 bool found = false;
94 if (dmesg->exists() && dmesg->open(QIODevice::ReadOnly)) {
95 t = new QTextStream(dmesg);
96 } else {
97 delete dmesg;
98 pipe = popen("/sbin/dmesg", "r");
99 if (!pipe)
100 return false;
101 usepipe = true;
102 t = new QTextStream(pipe, QIODevice::ReadOnly);
105 while (!(s = t->readLine().toLocal8Bit()).isNull()) {
106 if (!seencpu) {
107 if (s.contains("cpu"))
108 seencpu = true;
109 else
110 continue;
112 if (s.contains("boot device") || s.contains("WARNING: old BSD partition ID!"))
113 break;
115 if (!filter || s.contains(QRegExp(filter))) {
116 if (func)
117 func(tree, s);
118 else {
119 QStringList list;
120 list << s;
121 new QTreeWidgetItem(tree, list);
123 found = true;
127 delete t;
128 if (pipe)
129 pclose(pipe);
130 else {
131 dmesg->close();
132 delete dmesg;
135 return found;
138 void AddIRQLine(QTreeWidget* tree, QString s) {
139 int irqnum;
140 QString s2;
141 char numstr[3];
142 bool ok;
144 s2 = s.mid(s.indexOf(QRegExp("[ (]irq "))+5);
145 irqnum = s2.remove(QRegExp("[^0-9].*")).toInt(&ok);
146 if (ok)
147 snprintf(numstr, 3, "%02d", irqnum);
148 else {
149 // this should never happen
150 strcpy(numstr, "??");
153 QStringList list;
154 list << numstr << s;
155 new QTreeWidgetItem(tree, list);
158 bool GetInfo_IRQ(QTreeWidget* tree) {
160 QStringList headers;
161 headers << i18n("IRQ") << i18n("Device");
162 tree->setHeaderLabels(headers);
164 tree->sortItems(0, Qt::AscendingOrder);
166 tree->setSortingEnabled(false);
168 (void) GetDmesgInfo(tree, "[ (]irq ", AddIRQLine);
169 return true;
172 bool GetInfo_DMA(QTreeWidget*) {
173 return false;
176 bool GetInfo_PCI(QTreeWidget* tree) {
177 if (!GetDmesgInfo(tree, "at pci", NULL)) {
178 QStringList list;
179 list << i18n("No PCI devices found.");
180 new QTreeWidgetItem(tree, list);
182 return true;
185 bool GetInfo_IO_Ports(QTreeWidget* tree) {
186 if (!GetDmesgInfo(tree, "port 0x", NULL)) {
187 QStringList list;
188 list << i18n("No I/O port devices found.");
189 new QTreeWidgetItem(tree, list);
191 return true;
194 bool GetInfo_Sound(QTreeWidget* tree) {
195 tree->setSortingEnabled(false);
197 if (!GetDmesgInfo(tree, "audio", NULL)) {
198 QStringList list;
199 list << i18n("No audio devices found.");
200 new QTreeWidgetItem(tree, list);
203 // append information for each audio devices found
205 QTreeWidgetItemIterator it(tree, QTreeWidgetItemIterator::All);
206 while ( *it != NULL) {
207 QString s, s2;
208 int pos;
209 char *dev;
211 s = (*it)->text(0);
212 // The autoconf message is in form 'audio0 at auvia0: ...'
213 if (s.find("audio") == 0 && (pos = s.find(" at ")) > 0) {
214 s2 = s.mid(pos+4); // skip " at "
215 s2.remove(QRegExp("[:\n\t ].*"));
216 dev = strdup(s2.toAscii().data());
218 GetDmesgInfo(tree, dev, NULL);
220 free(dev);
223 ++it;
226 return true;
229 bool GetInfo_Devices(QTreeWidget* tree) {
230 (void) GetDmesgInfo(tree, NULL, NULL);
231 return true;
234 bool GetInfo_SCSI(QTreeWidget* tree) {
235 if (!GetDmesgInfo(tree, "scsibus", NULL)) {
236 QStringList list;
237 list << i18n("No SCSI devices found.");
238 new QTreeWidgetItem(tree, list);
241 // remove the 'waiting %d seconds for devices to settle' message
242 QTreeWidgetItemIterator it(tree, QTreeWidgetItemIterator::All);
243 while ( *it != NULL) {
244 QString s = (*it)->text(0);
246 if (s.contains("seconds for devices to settle")) {
247 delete tree->takeItem( tree->indexOfTopLevelItem(*it) );
248 break;
251 ++it;
254 return true;
257 bool GetInfo_Partitions(QTreeWidget* tree) {
258 int num; // number of mounts
259 #ifdef HAVE_STATVFS
260 struct statvfs *mnt; // mount data pointer
261 #else
262 struct statfs *mnt; // mount data pointer
263 #endif
265 // get mount info
266 if (!(num=getmntinfo(&mnt, MNT_WAIT))) {
267 kError() << "getmntinfo failed" << endl;
268 return false;
271 // table headers
272 QStringList headers;
273 headers << i18n("Device") << i18n("Mount Point") << i18n("FS Type") << i18n("Total Size") << i18n("Free Size") << i18n("Total Nodes") << i18n("Free Nodes") << i18n("Flags");
274 tree->setHeaderLabels(headers);
276 // mnt points into a static array (no need to free it)
277 for (; num--; ++mnt) {
278 unsigned long long big[2];
279 QString vv[5];
281 #ifdef HAVE_STATVFS
282 big[0] = big[1] = mnt->f_frsize; // coerce the product
283 #else
284 big[0] = big[1] = mnt->f_bsize; // coerce the product
285 #endif
286 big[0] *= mnt->f_blocks;
287 big[1] *= mnt->f_bavail; // FIXME: use f_bfree if root?
289 // convert to strings
290 vv[0] = KIO::convertSize(big[0]);
291 vv[1] = QString("%1 (%2%)")
292 .arg(KIO::convertSize(big[1]))
293 .arg(mnt->f_blocks ? mnt->f_bavail*100/mnt->f_blocks : 0);
295 vv[2] = QString("%L1").arg(mnt->f_files);
296 vv[3] = QString("%L1 (%2%) ")
297 .arg(mnt->f_ffree)
298 .arg(mnt->f_files ? mnt->f_ffree*100/mnt->f_files : 0);
300 vv[4].clear();
301 #ifdef HAVE_STATVFS
302 #define MNTF(x) if (mnt->f_flag & ST_##x) vv[4] += QLatin1String(#x " ");
303 #else
304 #define MNTF(x) if (mnt->f_flags & MNT_##x) vv[4] += QLatin1String(#x " ");
305 #endif
306 MNTF(ASYNC)
307 MNTF(DEFEXPORTED)
308 MNTF(EXKERB)
309 MNTF(EXNORESPORT)
310 MNTF(EXPORTANON)
311 MNTF(EXPORTED)
312 MNTF(EXPUBLIC)
313 MNTF(EXRDONLY)
314 #ifndef HAVE_STATVFS
315 MNTF(IGNORE)
316 #endif
317 MNTF(LOCAL)
318 MNTF(NOATIME)
319 MNTF(NOCOREDUMP)
320 MNTF(NODEV)
321 MNTF(NODEVMTIME)
322 MNTF(NOEXEC)
323 MNTF(NOSUID)
324 MNTF(QUOTA)
325 MNTF(RDONLY)
326 MNTF(ROOTFS)
327 MNTF(SOFTDEP)
328 MNTF(SYMPERM)
329 MNTF(SYNCHRONOUS)
330 MNTF(UNION)
331 #undef MNTF
333 // put it in the table
335 QStringList list;
336 // FIXME: there're more data but we have limited args (this is wrong! just add!)
337 // FIXME: names need pad space
338 list << mnt->f_mntfromname << mnt->f_mntonname << mnt->f_fstypename << vv[0] << vv[1] << vv[2] << vv[3] << vv[4];
339 new QTreeWidgetItem(tree, list);
342 // job well done
343 return true;
346 bool GetInfo_XServer_and_Video(QTreeWidget* tree) {
347 return GetInfo_XServer_Generic(tree);