File indexing completed on 2024-04-28 16:49:50
0001 #include <KLocalizedString> 0002 #include <QString> 0003 0004 #include "lsof.h" 0005 0006 struct KLsofWidgetPrivate { 0007 qlonglong pid; 0008 QProcess *process; 0009 }; 0010 0011 KLsofWidget::KLsofWidget(QWidget *parent) 0012 : QTreeWidget(parent) 0013 , d(new KLsofWidgetPrivate) 0014 { 0015 d->pid = -1; 0016 setColumnCount(3); 0017 setUniformRowHeights(true); 0018 setRootIsDecorated(false); 0019 setItemsExpandable(false); 0020 setSortingEnabled(true); 0021 setAllColumnsShowFocus(true); 0022 setHeaderLabels(QStringList() << i18nc("Short for File Descriptor", "FD") << i18n("Type") << i18n("Object")); 0023 d->process = new QProcess(this); 0024 connect(d->process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus))); 0025 } 0026 0027 KLsofWidget::~KLsofWidget() 0028 { 0029 delete d; 0030 } 0031 0032 qlonglong KLsofWidget::pid() const 0033 { 0034 return d->pid; 0035 } 0036 0037 void KLsofWidget::setPid(qlonglong pid) 0038 { 0039 d->pid = pid; 0040 update(); 0041 } 0042 0043 bool KLsofWidget::update() 0044 { 0045 clear(); 0046 QStringList args; 0047 d->process->waitForFinished(); 0048 args << QStringLiteral("-Fftn"); 0049 if (d->pid > 0) 0050 args << (QStringLiteral("-p") + QString::number(d->pid)); 0051 d->process->start(QStringLiteral("lsof"), args); 0052 return true; 0053 } 0054 0055 void KLsofWidget::finished(int exitCode, QProcess::ExitStatus exitStatus) 0056 { 0057 Q_UNUSED(exitCode); 0058 Q_UNUSED(exitStatus); 0059 0060 char buf[1024]; 0061 QTreeWidgetItem *process = nullptr; 0062 while (true) { 0063 qint64 lineLength = d->process->readLine(buf, sizeof(buf)); 0064 0065 if (lineLength <= 0) 0066 break; 0067 if (buf[lineLength - 1] == '\n') 0068 lineLength--; 0069 0070 switch (buf[0]) { 0071 /* Process related stuff */ 0072 case 'f': 0073 process = new QTreeWidgetItem(this); 0074 process->setText(0, QString::fromUtf8(buf + 1, lineLength - 1)); 0075 break; 0076 case 't': 0077 if (process) 0078 process->setText(1, QString::fromUtf8(buf + 1, lineLength - 1)); 0079 break; 0080 0081 case 'n': 0082 if (process) 0083 process->setText(2, QString::fromUtf8(buf + 1, lineLength - 1)); 0084 break; 0085 default: 0086 break; 0087 } 0088 } 0089 }