Commit e6091e78 authored by Christian Kandeler's avatar Christian Kandeler
Browse files

Maemo: Give users access to remote process list.

parent 2ede8e3c
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "maemoremoteprocessesdialog.h"
#include "ui_maemoremoteprocessesdialog.h"
#include "maemoremoteprocesslist.h"
#include <QtGui/QMessageBox>
#include <QtGui/QSortFilterProxyModel>
namespace Qt4ProjectManager {
namespace Internal {
MaemoRemoteProcessesDialog::MaemoRemoteProcessesDialog(const Core::SshConnectionParameters &params,
QWidget *parent) :
QDialog(parent),
m_ui(new Ui::MaemoRemoteProcessesDialog),
m_processList(new MaemoRemoteProcessList(params, this)),
m_proxyModel(new QSortFilterProxyModel(this))
{
m_ui->setupUi(this);
m_ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
m_proxyModel->setSourceModel(m_processList);
m_proxyModel->setDynamicSortFilter(true);
m_ui->tableView->setModel(m_proxyModel);
connect(m_ui->tableView->selectionModel(),
SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
SLOT(handleSelectionChanged()));
connect(m_ui->updateListButton, SIGNAL(clicked()),
SLOT(updateProcessList()));
connect(m_ui->killProcessButton, SIGNAL(clicked()), SLOT(killProcess()));
connect(m_processList, SIGNAL(error(QString)),
SLOT(handleRemoteError(QString)));
connect(m_processList, SIGNAL(modelReset()),
SLOT(handleProcessListUpdated()));
connect(m_processList, SIGNAL(processKilled()),
SLOT(handleProcessKilled()), Qt::QueuedConnection);
connect(m_proxyModel, SIGNAL(layoutChanged()),
SLOT(handleProcessListUpdated()));
handleSelectionChanged();
updateProcessList();
}
MaemoRemoteProcessesDialog::~MaemoRemoteProcessesDialog()
{
delete m_ui;
}
void MaemoRemoteProcessesDialog::handleRemoteError(const QString &errorMsg)
{
QMessageBox::critical(this, tr("Remote error"), errorMsg);
m_ui->updateListButton->setEnabled(true);
handleSelectionChanged();
}
void MaemoRemoteProcessesDialog::handleProcessListUpdated()
{
m_ui->updateListButton->setEnabled(true);
m_ui->tableView->resizeRowsToContents();
handleSelectionChanged();
}
void MaemoRemoteProcessesDialog::updateProcessList()
{
m_ui->updateListButton->setEnabled(false);
m_ui->killProcessButton->setEnabled(false);
m_processList->update();
}
void MaemoRemoteProcessesDialog::killProcess()
{
const QModelIndexList &indexes
= m_ui->tableView->selectionModel()->selectedIndexes();
if (indexes.empty())
return;
m_ui->updateListButton->setEnabled(false);
m_ui->killProcessButton->setEnabled(false);
m_processList->killProcess(m_proxyModel->mapToSource(indexes.first()).row());
}
void MaemoRemoteProcessesDialog::handleProcessKilled()
{
updateProcessList();
}
void MaemoRemoteProcessesDialog::handleSelectionChanged()
{
m_ui->killProcessButton->setEnabled(m_ui->tableView->selectionModel()->hasSelection());
}
} // namespace Internal
} // namespace Qt4ProjectManager
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef MAEMOREMOTEPROCESSDIALOG_H
#define MAEMOREMOTEPROCESSDIALOG_H
#include <QtGui/QDialog>
QT_BEGIN_NAMESPACE
namespace Ui {
class MaemoRemoteProcessesDialog;
}
class QSortFilterProxyModel;
QT_END_NAMESPACE
namespace Core { class SshConnectionParameters; }
namespace Qt4ProjectManager {
namespace Internal {
class MaemoRemoteProcessList;
class MaemoRemoteProcessesDialog : public QDialog
{
Q_OBJECT
public:
explicit MaemoRemoteProcessesDialog(const Core::SshConnectionParameters &params,
QWidget *parent = 0);
~MaemoRemoteProcessesDialog();
private slots:
void updateProcessList();
void killProcess();
void handleRemoteError(const QString &errorMsg);
void handleProcessListUpdated();
void handleProcessKilled();
void handleSelectionChanged();
private:
Ui::MaemoRemoteProcessesDialog *m_ui;
MaemoRemoteProcessList *const m_processList;
QSortFilterProxyModel *const m_proxyModel;
};
} // namespace Internal
} // namespace Qt4ProjectManager
#endif // MAEMOREMOTEPROCESSDIALOG_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MaemoRemoteProcessesDialog</class>
<widget class="QDialog" name="MaemoRemoteProcessesDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>766</width>
<height>684</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Processes running on remote host:</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTableView" name="tableView">
<property name="showGrid">
<bool>false</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>100</number>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="updateListButton">
<property name="text">
<string>Update List</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="killProcessButton">
<property name="text">
<string>Kill Selected Process</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>MaemoRemoteProcessesDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>257</x>
<y>290</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>MaemoRemoteProcessesDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of Qt Creator.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "maemoremoteprocesslist.h"
#include <coreplugin/ssh/sshremoteprocessrunner.h>
#include <QtCore/QStringList>
using namespace Core;
namespace Qt4ProjectManager {
namespace Internal {
namespace {
const QByteArray LineSeparator1("---");
const QByteArray LineSeparator2("QTCENDOFLINE---");
const QByteArray LineSeparator = LineSeparator1 + LineSeparator2;
} // anonymous namespace
MaemoRemoteProcessList::MaemoRemoteProcessList(const Core::SshConnectionParameters &params,
QObject *parent)
: QAbstractTableModel(parent),
m_process(SshRemoteProcessRunner::create(params)),
m_state(Inactive)
{
}
MaemoRemoteProcessList::~MaemoRemoteProcessList() {}
void MaemoRemoteProcessList::update()
{
if (m_state != Inactive) {
qDebug("%s: Did not expect state to be %d.", Q_FUNC_INFO, m_state);
stop();
}
beginResetModel();
m_remoteProcs.clear();
const QByteArray command = QByteArray()
+ "sep1=" + LineSeparator1 + '\n'
+ "sep2=" + LineSeparator2 + '\n'
+ "pidlist=`ls /proc |grep -E '^[[:digit:]]+$' |sort -n`; "
+ "for pid in $pidlist\n"
+ "do\n"
+ " echo -n \"$pid \"\n"
+ " tr '\\0' ' ' < /proc/$pid/cmdline\n"
+ " echo -n \"$sep1$sep2\"\n"
+ "done\n"
+ "echo ''";
startProcess(command, Listing);
}
void MaemoRemoteProcessList::killProcess(int row)
{
Q_ASSERT(row >= 0 && row < m_remoteProcs.count());
const QByteArray command
= "kill -9 " + QByteArray::number(m_remoteProcs.at(row).pid);
startProcess(command, Killing);
}
void MaemoRemoteProcessList::startProcess(const QByteArray &cmdLine,
State newState)
{
if (m_state != Inactive) {
qDebug("%s: Did not expect state to be %d.", Q_FUNC_INFO, m_state);
stop();
}
m_state = newState;
connect(m_process.data(), SIGNAL(connectionError(Core::SshError)),
SLOT(handleConnectionError()));
connect(m_process.data(), SIGNAL(processOutputAvailable(QByteArray)),
SLOT(handleRemoteStdOut(QByteArray)));
connect(m_process.data(),
SIGNAL(processErrorOutputAvailable(QByteArray)),
SLOT(handleRemoteStdErr(QByteArray)));
connect(m_process.data(), SIGNAL(processClosed(int)),
SLOT(handleRemoteProcessFinished(int)));
m_remoteStdout.clear();
m_remoteStderr.clear();
m_errorMsg.clear();
m_process->run(cmdLine);
}
void MaemoRemoteProcessList::handleConnectionError()
{
if (m_state == Inactive)
return;
emit error(tr("Connection failure: %1")
.arg(m_process->connection()->errorString()));
stop();
}
void MaemoRemoteProcessList::handleRemoteStdOut(const QByteArray &output)
{
if (m_state == Listing)
m_remoteStdout += output;
}
void MaemoRemoteProcessList::handleRemoteStdErr(const QByteArray &output)
{
if (m_state != Inactive)
m_remoteStderr += output;
}
void MaemoRemoteProcessList::handleRemoteProcessFinished(int exitStatus)
{
if (m_state == Inactive)
return;
switch (exitStatus) {
case SshRemoteProcess::FailedToStart:
m_errorMsg = tr("Error: Remote process failed to start: %1")
.arg(m_process->process()->errorString());
break;
case SshRemoteProcess::KilledBySignal:
m_errorMsg = tr("Error: Remote process crashed: %1")
.arg(m_process->process()->errorString());
break;
case SshRemoteProcess::ExitedNormally:
if (m_process->process()->exitCode() == 0) {
if (m_state == Listing)
buildProcessList();
} else {
m_errorMsg = tr("Remote process failed.");
}
break;
default:
Q_ASSERT_X(false, Q_FUNC_INFO, "Invalid exit status");
}
if (!m_errorMsg.isEmpty()) {
if (!m_remoteStderr.isEmpty()) {
m_errorMsg += tr("\nRemote stderr was: %1")
.arg(QString::fromUtf8(m_remoteStderr));
}
emit error(m_errorMsg);
}
stop();
}
void MaemoRemoteProcessList::stop()
{
if (m_state == Inactive)
return;
disconnect(m_process.data(), 0, this, 0);
if (m_state == Listing)
endResetModel();
else if (m_errorMsg.isEmpty())
emit processKilled();
m_state = Inactive;
}
void MaemoRemoteProcessList::buildProcessList()
{
const QString remoteOutput = QString::fromUtf8(m_remoteStdout);
const QStringList &lines
= remoteOutput.split(QString::fromUtf8(LineSeparator));
foreach (const QString &line, lines) {
const int pidEndPos = line.indexOf(' ');
if (pidEndPos == -1)
continue;
bool isNumber;
const int pid = line.left(pidEndPos).toInt(&isNumber);
if (!isNumber) {
qDebug("%s: Non-integer value where pid was expected. Line was: '%s'",
Q_FUNC_INFO, qPrintable(line));
continue;
}
m_remoteProcs << RemoteProc(pid, line.mid(pidEndPos));
}
}
int MaemoRemoteProcessList::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_remoteProcs.count();
}
int MaemoRemoteProcessList::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 2;
}
QVariant MaemoRemoteProcessList::headerData(int section,
Qt::Orientation orientation, int role) const
{
if (orientation != Qt::Horizontal || role != Qt::DisplayRole
|| section < 0 || section >= columnCount())
return QVariant();
if (section == 0)
return tr("PID");
else
return tr("Command Line");
}
QVariant MaemoRemoteProcessList::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= rowCount(index.parent())
|| index.column() >= columnCount() || role != Qt::DisplayRole)
return QVariant();
const RemoteProc &proc = m_remoteProcs.at(index.row());
if (index.column() == 0)
return proc.pid;
else
return proc.cmdLine;
}
} // namespace Internal
} // namespace Qt4ProjectManager