add more spacing
[personal-kdebase.git] / workspace / libs / ksysguard / lsofui / lsof.cpp
blob755504f74e0dfda5623cd7a85520161d0eaab11c
1 #include <QString>
2 #include <QProcess>
3 #include <klocale.h>
5 #include "lsof.h"
7 struct KLsofWidgetPrivate {
8 qlonglong pid;
9 QProcess *process;
12 KLsofWidget::KLsofWidget(QWidget *parent) : QTreeWidget(parent), d(new KLsofWidgetPrivate)
14 d->pid = -1;
15 setColumnCount(3);
16 setUniformRowHeights(true);
17 setRootIsDecorated(false);
18 setItemsExpandable(false);
19 setSortingEnabled(true);
20 setAllColumnsShowFocus(true);
21 setHeaderLabels(QStringList() << i18nc("Short for File Descriptor", "FD") << i18n("Type") << i18n("Object"));
22 d->process = new QProcess(this);
23 connect(d->process, SIGNAL(finished ( int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
26 KLsofWidget::~KLsofWidget()
28 delete d;
31 qlonglong KLsofWidget::pid() const
33 return d->pid;
36 void KLsofWidget::setPid(qlonglong pid) {
37 d->pid = pid;
38 update();
41 bool KLsofWidget::update()
43 clear();
44 QStringList args;
45 d->process->waitForFinished();
46 args << "-Fftn";
47 if(d->pid > 0)
48 args << ("-p" + QString::number(d->pid));
49 d->process->start("lsof", args);
50 return true;
53 void KLsofWidget::finished ( int exitCode, QProcess::ExitStatus exitStatus )
55 char buf[1024];
56 QTreeWidgetItem *process = NULL;
57 while(true) {
58 qint64 lineLength = d->process->readLine(buf, sizeof(buf));
60 if(lineLength <= 0)
61 break;
62 if(buf[lineLength-1] == '\n')
63 lineLength--;
65 switch(buf[0]) {
66 /* Process related stuff */
67 case 'f':
68 process = new QTreeWidgetItem(this);
69 process->setText(0,QString::fromUtf8(buf+1, lineLength - 1));
70 break;
71 case 't':
72 if(process)
73 process->setText(1,QString::fromUtf8(buf+1, lineLength - 1));
74 break;
76 case 'n':
77 if(process)
78 process->setText(2,QString::fromUtf8(buf+1, lineLength - 1));
79 break;
80 default:
81 break;
86 #include "lsof.moc"