kde-workspace/libs/ksysguard/lsofui/lsof.cpp

100 lines
2.3 KiB
C++
Raw Normal View History

2014-11-13 19:30:51 +02:00
#include <QString>
#include <QProcess>
#include <klocale.h>
#include "lsof.h"
struct KLsofWidgetPrivate
{
qlonglong pid;
QProcess *process;
2014-11-13 19:30:51 +02:00
};
KLsofWidget::KLsofWidget(QWidget *parent)
: QTreeWidget(parent), d(new KLsofWidgetPrivate())
2014-11-13 19:30:51 +02:00
{
d->pid = -1;
setColumnCount(3);
setUniformRowHeights(true);
setRootIsDecorated(false);
setItemsExpandable(false);
setSortingEnabled(true);
setAllColumnsShowFocus(true);
setHeaderLabels(QStringList() << i18nc("Short for File Descriptor", "FD") << i18n("Type") << i18n("Object"));
d->process = new QProcess(this);
connect(d->process, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int,QProcess::ExitStatus)));
2014-11-13 19:30:51 +02:00
}
KLsofWidget::~KLsofWidget()
{
delete d;
2014-11-13 19:30:51 +02:00
}
qlonglong KLsofWidget::pid() const
{
return d->pid;
2014-11-13 19:30:51 +02:00
}
void KLsofWidget::setPid(qlonglong pid)
{
d->pid = pid;
update();
2014-11-13 19:30:51 +02:00
}
bool KLsofWidget::update()
{
clear();
QStringList args;
d->process->waitForFinished();
args << "-Fftn";
if (d->pid > 0) {
args << ("-p" + QString::number(d->pid));
}
d->process->start("lsof", args);
return true;
2014-11-13 19:30:51 +02:00
}
void KLsofWidget::finished(int exitCode, QProcess::ExitStatus exitStatus)
2014-11-13 19:30:51 +02:00
{
Q_UNUSED(exitCode);
Q_UNUSED(exitStatus);
2014-11-13 19:30:51 +02:00
char buf[1024];
QTreeWidgetItem *process = NULL;
while (true) {
::memset(buf, '\0', sizeof(buf) * sizeof(char));
qint64 lineLength = d->process->readLine(buf, sizeof(buf));
2014-11-13 19:30:51 +02:00
if (lineLength <= 0) {
break;
}
if (buf[lineLength-1] == '\n') {
lineLength--;
}
2014-11-13 19:30:51 +02:00
switch(buf[0]) {
/* Process related stuff */
case 'f': {
process = new QTreeWidgetItem(this);
process->setText(0,QString::fromUtf8(buf+1, lineLength - 1));
break;
}
case 't': {
if (process)
process->setText(1,QString::fromUtf8(buf+1, lineLength - 1));
break;
}
case 'n': {
if (process)
process->setText(2,QString::fromUtf8(buf+1, lineLength - 1));
break;
}
default: {
break;
}
}
}
2014-11-13 19:30:51 +02:00
}
2015-02-27 09:28:46 +00:00
#include "moc_lsof.cpp"