Add some tests that involve std::vector.
[kdbg.git] / kdbg / procattach.cpp
blob3b248cdc596453a97311d2cb784e7c4104a83827
1 /*
2 * Copyright Johannes Sixt
3 * This file is licensed under the GNU General Public License Version 2.
4 * See the file COPYING in the toplevel directory of the source directory.
5 */
7 #include "procattach.h"
8 #include <qlistview.h>
9 #include <qtoolbutton.h>
10 #include <qlineedit.h>
11 #include <kprocess.h>
12 #include <ctype.h>
13 #include <kapp.h>
14 #include <kiconloader.h>
15 #include <klocale.h> /* i18n */
16 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
21 ProcAttachPS::ProcAttachPS(QWidget* parent) :
22 ProcAttachBase(parent),
23 m_pidCol(-1),
24 m_ppidCol(-1)
26 m_ps = new KProcess;
27 connect(m_ps, SIGNAL(receivedStdout(KProcess*, char*, int)),
28 this, SLOT(slotTextReceived(KProcess*, char*, int)));
29 connect(m_ps, SIGNAL(processExited(KProcess*)),
30 this, SLOT(slotPSDone()));
32 QIconSet icon = SmallIconSet("clear_left");
33 filterClear->setIconSet(icon);
35 processList->setColumnWidth(0, 300);
36 processList->setColumnWidthMode(0, QListView::Manual);
37 processList->setColumnAlignment(1, Qt::AlignRight);
38 processList->setColumnAlignment(2, Qt::AlignRight);
40 // set the command line
41 static const char* const psCommand[] = {
42 #ifdef PS_COMMAND
43 PS_COMMAND,
44 #else
45 "/bin/false",
46 #endif
49 for (int i = 0; psCommand[i] != 0; i++) {
50 *m_ps << psCommand[i];
53 runPS();
56 ProcAttachPS::~ProcAttachPS()
58 delete m_ps; // kills a running ps
61 void ProcAttachPS::runPS()
63 // clear the parse state from previous runs
64 m_token = "";
65 m_line.clear();
66 m_pidCol = -1;
67 m_ppidCol = -1;
69 m_ps->start(KProcess::NotifyOnExit, KProcess::Stdout);
72 void ProcAttachPS::slotTextReceived(KProcess*, char* buffer, int buflen)
74 const char* end = buffer+buflen;
75 while (buffer < end)
77 // check new line
78 if (*buffer == '\n')
80 // push a tokens onto the line
81 if (!m_token.isEmpty()) {
82 m_line.push_back(QString::fromLatin1(m_token));
83 m_token = "";
85 // and insert the line in the list
86 pushLine();
87 m_line.clear();
88 ++buffer;
90 // blanks: the last column gets the rest of the line, including blanks
91 else if ((m_pidCol < 0 || int(m_line.size()) < processList->columns()-1) &&
92 isspace(*buffer))
94 // push a token onto the line
95 if (!m_token.isEmpty()) {
96 m_line.push_back(QString::fromLatin1(m_token));
97 m_token = "";
99 do {
100 ++buffer;
101 } while (buffer < end && isspace(*buffer));
103 // tokens
104 else
106 const char* start = buffer;
107 do {
108 ++buffer;
109 } while (buffer < end && !isspace(*buffer));
110 // append to the current token
111 m_token += QCString(start, buffer-start+1); // must count the '\0'
116 void ProcAttachPS::pushLine()
118 if (m_line.size() < 3) // we need the PID, PPID, and COMMAND columns
119 return;
121 if (m_pidCol < 0)
123 // create columns if we don't have them yet
124 bool allocate = processList->columns() == 3;
126 // we assume that the last column is the command
127 m_line.pop_back();
129 for (uint i = 0; i < m_line.size(); i++) {
130 // we don't allocate the PID and PPID columns,
131 // but we need to know where in the ps output they are
132 if (m_line[i] == "PID") {
133 m_pidCol = i;
134 } else if (m_line[i] == "PPID") {
135 m_ppidCol = i;
136 } else if (allocate) {
137 processList->addColumn(m_line[i]);
138 // these columns are normally numbers
139 processList->setColumnAlignment(processList->columns()-1,
140 Qt::AlignRight);
144 else
146 // insert a line
147 // find the parent process
148 QListViewItem* parent = 0;
149 if (m_ppidCol >= 0 && m_ppidCol < int(m_line.size())) {
150 parent = processList->findItem(m_line[m_ppidCol], 1);
153 // we assume that the last column is the command
154 QListViewItem* item;
155 if (parent == 0) {
156 item = new QListViewItem(processList, m_line.back());
157 } else {
158 item = new QListViewItem(parent, m_line.back());
160 item->setOpen(true);
161 m_line.pop_back();
162 int k = 3;
163 for (uint i = 0; i < m_line.size(); i++)
165 // display the pid and ppid columns' contents in columns 1 and 2
166 if (int(i) == m_pidCol)
167 item->setText(1, m_line[i]);
168 else if (int(i) == m_ppidCol)
169 item->setText(2, m_line[i]);
170 else
171 item->setText(k++, m_line[i]);
174 if (m_ppidCol >= 0 && m_pidCol >= 0) { // need PID & PPID for this
176 * It could have happened that a process was earlier inserted,
177 * whose parent process is the current process. Such processes
178 * were placed at the root. Here we go through all root items
179 * and check whether we must reparent them.
181 QListViewItem* i = processList->firstChild();
182 while (i != 0)
184 // advance before we reparent the item
185 QListViewItem* it = i;
186 i = i->nextSibling();
187 if (it->text(2) == m_line[m_pidCol]) {
188 processList->takeItem(it);
189 item->insertItem(it);
196 void ProcAttachPS::slotPSDone()
198 filterEdited(filterEdit->text());
201 QString ProcAttachPS::text() const
203 QListViewItem* item = processList->selectedItem();
205 if (item == 0)
206 return QString();
208 return item->text(1);
211 void ProcAttachPS::refresh()
213 if (!m_ps->isRunning())
215 processList->clear();
216 buttonOk->setEnabled(false); // selection was cleared
217 runPS();
221 void ProcAttachPS::filterEdited(const QString& text)
223 QListViewItem* i = processList->firstChild();
224 if (i) {
225 setVisibility(i, text);
230 * Sets the visibility of \a i and
231 * returns whether it was made visible.
233 bool ProcAttachPS::setVisibility(QListViewItem* i, const QString& text)
235 bool visible = false;
236 for (QListViewItem* j = i->firstChild(); j; j = j->nextSibling())
238 if (setVisibility(j, text))
239 visible = true;
241 // look for text in the process name and in the PID
242 visible = visible || text.isEmpty() ||
243 i->text(0).find(text, 0, false) >= 0 ||
244 i->text(1).find(text) >= 0;
246 i->setVisible(visible);
248 // disable the OK button if the selected item becomes invisible
249 if (i->isSelected())
250 buttonOk->setEnabled(visible);
252 return visible;
255 void ProcAttachPS::selectedChanged()
257 buttonOk->setEnabled(processList->selectedItem() != 0);
261 ProcAttach::ProcAttach(QWidget* parent) :
262 QDialog(parent, "procattach", true),
263 m_label(this, "label"),
264 m_processId(this, "procid"),
265 m_buttonOK(this, "ok"),
266 m_buttonCancel(this, "cancel"),
267 m_layout(this, 8),
268 m_buttons(4)
270 QString title = kapp->caption();
271 title += i18n(": Attach to process");
272 setCaption(title);
274 m_label.setMinimumSize(330, 24);
275 m_label.setText(i18n("Specify the process number to attach to:"));
277 m_processId.setMinimumSize(330, 24);
278 m_processId.setMaxLength(100);
279 m_processId.setFrame(true);
281 m_buttonOK.setMinimumSize(100, 30);
282 connect(&m_buttonOK, SIGNAL(clicked()), SLOT(accept()));
283 m_buttonOK.setText(i18n("OK"));
284 m_buttonOK.setDefault(true);
286 m_buttonCancel.setMinimumSize(100, 30);
287 connect(&m_buttonCancel, SIGNAL(clicked()), SLOT(reject()));
288 m_buttonCancel.setText(i18n("Cancel"));
290 m_layout.addWidget(&m_label);
291 m_layout.addWidget(&m_processId);
292 m_layout.addLayout(&m_buttons);
293 m_layout.addStretch(10);
294 m_buttons.addStretch(10);
295 m_buttons.addWidget(&m_buttonOK);
296 m_buttons.addSpacing(40);
297 m_buttons.addWidget(&m_buttonCancel);
298 m_buttons.addStretch(10);
300 m_layout.activate();
302 m_processId.setFocus();
303 resize(350, 120);
306 ProcAttach::~ProcAttach()