File indexing completed on 2024-04-28 05:31:34
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 } 0052 d->process->start(QStringLiteral("lsof"), args); 0053 return true; 0054 } 0055 0056 void KLsofWidget::finished(int exitCode, QProcess::ExitStatus exitStatus) 0057 { 0058 Q_UNUSED(exitCode); 0059 Q_UNUSED(exitStatus); 0060 0061 char buf[1024]; 0062 QTreeWidgetItem *process = nullptr; 0063 while (true) { 0064 qint64 lineLength = d->process->readLine(buf, sizeof(buf)); 0065 0066 if (lineLength <= 0) { 0067 break; 0068 } 0069 if (buf[lineLength - 1] == '\n') { 0070 lineLength--; 0071 } 0072 0073 switch (buf[0]) { 0074 /* Process related stuff */ 0075 case 'f': 0076 process = new QTreeWidgetItem(this); 0077 process->setText(0, QString::fromUtf8(buf + 1, lineLength - 1)); 0078 break; 0079 case 't': 0080 if (process) { 0081 process->setText(1, QString::fromUtf8(buf + 1, lineLength - 1)); 0082 } 0083 break; 0084 0085 case 'n': 0086 if (process) { 0087 process->setText(2, QString::fromUtf8(buf + 1, lineLength - 1)); 0088 } 0089 break; 0090 default: 0091 break; 0092 } 0093 } 0094 }