1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
/*
SPDX-FileCopyrightText: 2009 Aleix Pol <aleixpol@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "processselection.h"
#include <util/scopeddialog.h>
#include <processcore/process.h>
#include <processcore/process_data_model.h>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
#include <KUser>
#include <QLineEdit>
#include <QPushButton>
#include <QSortFilterProxyModel>
#include <QTreeView>
using namespace KDevMI;
static constexpr bool isSystemUser(unsigned int userId)
{
constexpr unsigned int minimumNonSystemId = 1000;
constexpr unsigned int maximumNonSystemId = 65533;
return userId < minimumNonSystemId || userId > maximumNonSystemId;
}
class ProcessesSortFilterModel : public QSortFilterProxyModel
{
public:
enum class ProcessOwner { Self, Users, System, All };
explicit ProcessesSortFilterModel(int uidColumn, QObject* parent = nullptr)
: QSortFilterProxyModel(parent)
, m_uidColumn(uidColumn)
{
setSortRole(KSysGuard::ProcessDataModel::Value);
setSortCaseSensitivity(Qt::CaseInsensitive);
setSortLocaleAware(true);
setFilterRole(KSysGuard::ProcessDataModel::Value);
setFilterCaseSensitivity(Qt::CaseInsensitive);
setRecursiveFilteringEnabled(true);
}
bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override
{
const auto uid = sourceModel()->data(sourceModel()->index(sourceRow, m_uidColumn, sourceParent)).toUInt();
bool accept = true;
switch (m_processOwner) {
case ProcessOwner::Self:
accept = m_currentUserUid == uid;
break;
case ProcessOwner::Users:
accept = !isSystemUser(uid);
break;
case ProcessOwner::System:
accept = isSystemUser(uid);
break;
case ProcessOwner::All:
break;
}
if (!accept) {
return false;
}
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
bool filterAcceptsColumn(int sourceColumn, const QModelIndex& sourceParent) const override
{
if (sourceColumn == m_uidColumn) {
return false;
}
return QSortFilterProxyModel::filterAcceptsColumn(sourceColumn, sourceParent);
}
ProcessOwner filterProcessOwner() const
{
return m_processOwner;
}
void setFilterProcessOwner(ProcessOwner owner)
{
m_processOwner = owner;
invalidateFilter();
}
private:
const uint m_currentUserUid = KUserId::currentEffectiveUserId().nativeId();
const int m_uidColumn;
ProcessOwner m_processOwner = ProcessOwner::Self;
};
ProcessSelectionDialog::ProcessSelectionDialog(QWidget* parent)
: QDialog(parent)
{
m_ui.setupUi(this);
auto* const view = m_ui.view;
const QStringList attributes = {QStringLiteral("name"), QStringLiteral("pid"), QStringLiteral("username"),
QStringLiteral("startTime"), QStringLiteral("command"), QStringLiteral("euid")};
m_dataModel = new KSysGuard::ProcessDataModel(this);
m_dataModel->setEnabledAttributes(attributes);
m_sortModel = new ProcessesSortFilterModel(attributes.indexOf(QStringLiteral("euid")), this);
m_sortModel->setSourceModel(m_dataModel);
view->setModel(m_sortModel);
m_pidColumn = attributes.indexOf(QStringLiteral("pid"));
connect(m_ui.filterEdit, &QLineEdit::textChanged, this, [this](const QString& text) {
m_sortModel->setFilterFixedString(text);
});
connect(m_ui.processesCombo, &QComboBox::activated, this, &ProcessSelectionDialog::onProcessesComboActivated);
connect(view->selectionModel(), &QItemSelectionModel::selectionChanged, this,
&ProcessSelectionDialog::selectionChanged);
connect(m_ui.buttonList, &QToolButton::toggled, this, [this](bool checked) {
m_ui.buttonTree->setChecked(!checked);
});
connect(m_ui.buttonTree, &QToolButton::toggled, this, [this](bool checked) {
m_ui.view->clearSelection();
m_dataModel->setFlatList(!checked);
m_ui.buttonList->setChecked(!checked);
m_ui.view->expandAll();
});
auto* const buttonBox = m_ui.buttonBox;
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
m_attachButton = buttonBox->button(QDialogButtonBox::Ok);
m_attachButton->setDefault(true);
m_attachButton->setText(i18nc("@action:button", "Attach"));
m_attachButton->setShortcut(Qt::CTRL | Qt::Key_Return);
m_attachButton->setEnabled(false);
const KConfigGroup config = KSharedConfig::openConfig()->group(QStringLiteral("ProcessSelectionDialog"));
m_ui.filterEdit->setText(config.readEntry("filterText", QString()));
restoreGeometry(config.readEntry("dialogGeometry", QByteArray{}));
m_ui.processesCombo->setCurrentIndex(config.readEntry("processOwner", 0));
onProcessesComboActivated(m_ui.processesCombo->currentIndex());
const auto headerState = config.readEntry("headerState", QByteArray{});
if (headerState.isEmpty()) {
m_ui.view->sortByColumn(0, Qt::SortOrder::AscendingOrder);
m_ui.view->setColumnWidth(0, 250);
} else {
m_ui.view->header()->restoreState(headerState);
}
if (config.readEntry("treeView", false)) {
m_ui.buttonTree->toggle();
}
}
ProcessSelectionDialog::~ProcessSelectionDialog()
{
KConfigGroup config = KSharedConfig::openConfig()->group(QStringLiteral("ProcessSelectionDialog"));
config.writeEntry("filterText", m_ui.filterEdit->text());
config.writeEntry("dialogGeometry", saveGeometry());
config.writeEntry("processOwner", static_cast<int>(m_sortModel->filterProcessOwner()));
config.writeEntry("headerState", m_ui.view->header()->saveState());
config.writeEntry("treeView", m_ui.buttonTree->isChecked());
}
long long ProcessSelectionDialog::pidSelected() const
{
const auto indexes = m_ui.view->selectionModel()->selectedIndexes();
return m_sortModel->data(indexes.at(m_pidColumn), KSysGuard::ProcessDataModel::Value).toLongLong();
}
void ProcessSelectionDialog::selectionChanged(const QItemSelection& newSelection,
const QItemSelection& /*oldSelection*/)
{
m_attachButton->setEnabled(!newSelection.isEmpty());
}
void ProcessSelectionDialog::onProcessesComboActivated(int index)
{
m_sortModel->setFilterProcessOwner(static_cast<ProcessesSortFilterModel::ProcessOwner>(index));
}
long long KDevMI::askUserForProcessId(QWidget* dialogParent)
{
const KDevelop::ScopedDialog<ProcessSelectionDialog> dlg(dialogParent);
if (dlg->exec()) {
return dlg->pidSelected();
}
return 0LL;
}
#include "moc_processselection.cpp"
|