Commit c6fef3ef authored by kh1's avatar kh1
Browse files

Long overdue changes and refactoring for qemu handling.

Fixes: - react on build/ run configuration changes
       - now notices environment changes, like switching Qt versions
       - start button is now only shown if we have a valid qemu runtime
       - enabled/ disabled state works properly when loading a project

Task-number: QTCREATOR-210, QTCREATORBUG-1252, QTCREATORBUG-1082
Reviewed-by: ck
parent 993891fb
......@@ -39,10 +39,22 @@
namespace Qt4ProjectManager {
namespace Internal {
enum QemuStatus { QemuStarting, QemuFailedToStart, QemuFinished, QemuCrashed };
enum QemuStatus {
QemuStarting,
QemuFailedToStart,
QemuFinished,
QemuCrashed,
QemuUserReason
};
#define PREFIX "Qt4ProjectManager.MaemoRunConfiguration"
#ifdef Q_OS_WIN32
#define EXEC_SUFFIX ".exe"
#else
#define EXEC_SUFFIX ""
#endif
static const QLatin1String MAEMO_RC_ID(PREFIX);
static const QLatin1String MAEMO_RC_ID_PREFIX(PREFIX ".");
......
......@@ -28,55 +28,39 @@
**************************************************************************/
#include "maemomanager.h"
#include "qtversionmanager.h"
#include "maemoconstants.h"
#include "maemodeviceconfigurations.h"
#include "maemopackagecreationfactory.h"
#include "maemorunfactories.h"
#include "maemosettingspage.h"
#include "maemotoolchain.h"
#include "maemorunconfiguration.h"
#include "qemuruntimemanager.h"
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/icore.h>
#include <coreplugin/modemanager.h>
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <qt4projectmanager/qtversionmanager.h>
#include <QtCore/QDir>
#include <QtCore/QFile>
#include <QtCore/QList>
#include <QtCore/QTextStream>
#include <QtGui/QAction>
#include <QtGui/QMessageBox>
namespace Qt4ProjectManager {
namespace Internal {
MaemoManager *MaemoManager::m_instance = 0;
const QSize iconSize = QSize(24, 20);
MaemoManager::MaemoManager()
: QObject(0)
, m_runControlFactory(new MaemoRunControlFactory(this))
, m_runConfigurationFactory(new MaemoRunConfigurationFactory(this))
, m_packageCreationFactory(new MaemoPackageCreationFactory(this))
, m_settingsPage(new MaemoSettingsPage(this))
, m_qemuAction(0)
{
Q_ASSERT(!m_instance);
m_instance = this;
MaemoDeviceConfigurations::instance(this);
icon.addFile(":/qt-maemo/images/qemu-run.png", iconSize);
icon.addFile(":/qt-maemo/images/qemu-stop.png", iconSize, QIcon::Normal,
QIcon::On);
ExtensionSystem::PluginManager *pluginManager
= ExtensionSystem::PluginManager::instance();
pluginManager->addObject(m_runControlFactory);
......@@ -103,6 +87,11 @@ MaemoManager &MaemoManager::instance()
return *m_instance;
}
void MaemoManager::init()
{
m_qemuRuntimeManager = new QemuRuntimeManager(this);
}
bool
MaemoManager::isValidMaemoQtVersion(const Qt4ProjectManager::QtVersion *version) const
{
......@@ -135,108 +124,5 @@ MaemoManager::maemoToolChain(const QtVersion *version) const
return new MaemoToolChain(targetRoot);
}
void
MaemoManager::addQemuSimulatorStarter(Project *project)
{
if (projects.contains(project))
return;
projects.insert(project);
if (m_qemuAction) {
m_qemuAction->setVisible(true);
return;
}
m_qemuAction = new QAction("Maemo Emulator", this);
m_qemuAction->setEnabled(false);
m_qemuAction->setIcon(icon.pixmap(iconSize));
m_qemuAction->setToolTip(tr("Start Maemo Emulator"));
connect(m_qemuAction, SIGNAL(triggered()), this, SLOT(triggered()));
Core::ICore *core = Core::ICore::instance();
Core::ActionManager *actionManager = core->actionManager();
Core::Command *qemuCommand = actionManager->registerAction(m_qemuAction,
"MaemoEmulator", QList<int>() << Core::Constants::C_GLOBAL_ID);
qemuCommand->setAttribute(Core::Command::CA_UpdateText);
qemuCommand->setAttribute(Core::Command::CA_UpdateIcon);
Core::ModeManager *modeManager = core->modeManager();
modeManager->addAction(qemuCommand, 1);
}
void
MaemoManager::removeQemuSimulatorStarter(Project *project)
{
if (projects.contains(project)) {
projects.remove(project);
if (projects.isEmpty() && m_qemuAction)
m_qemuAction->setVisible(false);
}
}
void
MaemoManager::setQemuSimulatorStarterEnabled(bool enable)
{
if (m_qemuAction)
m_qemuAction->setEnabled(enable);
}
void
MaemoManager::triggered()
{
emit startStopQemu();
}
void
MaemoManager::qemuStatusChanged(QemuStatus status, const QString &error)
{
if (!m_qemuAction)
return;
bool running;
QString message;
switch (status) {
case QemuStarting:
running = true;
break;
case QemuFailedToStart:
running = false;
message = tr("Qemu failed to start: %1").arg(error);
break;
case QemuCrashed:
running = false;
message = tr("Qemu crashed");
break;
case QemuFinished:
running = false;
break;
default:
Q_ASSERT(!"Missing handling of Qemu status");
}
if (!message.isEmpty())
QMessageBox::warning(0, tr("Qemu error"), message);
updateQemuIcon(running);
}
void MaemoManager::updateQemuIcon(bool running)
{
if (!m_qemuAction)
return;
QIcon::State state;
QString toolTip;
if (running) {
state = QIcon::On;
toolTip = tr("Stop Maemo Emulator");
} else {
state = QIcon::Off;
toolTip = tr("Start Maemo Emulator");
}
m_qemuAction->setToolTip(toolTip);
m_qemuAction->setIcon(icon.pixmap(iconSize, QIcon::Normal, state));
}
} // namespace Internal
} // namespace Internal
} // namespace Qt4ProjectManager
......@@ -30,20 +30,11 @@
#ifndef MAEMOMANAGER_H
#define MAEMOMANAGER_H
#include "maemoconstants.h"
#include <QtCore/QObject>
#include <QtCore/QSet>
#include <QtGui/QIcon>
QT_FORWARD_DECLARE_CLASS(QAction);
namespace ProjectExplorer {
class Project;
class ToolChain;
}
using ProjectExplorer::Project;
using ProjectExplorer::ToolChain;
namespace Qt4ProjectManager {
......@@ -54,34 +45,24 @@ class MaemoPackageCreationFactory;
class MaemoRunControlFactory;
class MaemoRunConfigurationFactory;
class MaemoSettingsPage;
class QemuRuntimeManager;
class MaemoManager : public QObject
{
Q_OBJECT
public:
MaemoManager();
~MaemoManager();
static MaemoManager &instance();
void init();
bool isValidMaemoQtVersion(const Qt4ProjectManager::QtVersion *version) const;
void addVersion(const Qt4ProjectManager::QtVersion *version) { Q_UNUSED(version); }
ToolChain *maemoToolChain(const Qt4ProjectManager::QtVersion *version) const;
void addQemuSimulatorStarter(Project *project);
void removeQemuSimulatorStarter(Project *project);
void setQemuSimulatorStarterEnabled(bool state);
void updateQemuIcon(bool running);
MaemoSettingsPage *settingsPage() const { return m_settingsPage; }
public slots:
void triggered();
void qemuStatusChanged(QemuStatus status, const QString &error);
signals:
void startStopQemu();
private:
static MaemoManager *m_instance;
......@@ -89,11 +70,7 @@ private:
MaemoRunConfigurationFactory *m_runConfigurationFactory;
MaemoPackageCreationFactory *m_packageCreationFactory;
MaemoSettingsPage *m_settingsPage;
QIcon icon;
int m_runCount;
QSet<Project*> projects;
QAction *m_qemuAction;
QemuRuntimeManager *m_qemuRuntimeManager;
};
} // namespace Internal
......
......@@ -26,13 +26,13 @@
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "maemorunconfiguration.h"
#include "maemoconstants.h"
#include "maemomanager.h"
#include "maemopackagecreationstep.h"
#include "maemorunconfigurationwidget.h"
#include "maemotoolchain.h"
#include "qemuruntimemanager.h"
#include <coreplugin/icore.h>
#include <coreplugin/messagemanager.h>
......@@ -45,9 +45,6 @@
#include <utils/qtcassert.h>
#include <QtCore/QDebug>
#include <QtCore/QProcess>
#include <QtCore/QString>
#include <QtCore/QStringBuilder>
namespace Qt4ProjectManager {
......@@ -59,8 +56,6 @@ MaemoRunConfiguration::MaemoRunConfiguration(Qt4Target *parent,
const QString &proFilePath)
: RunConfiguration(parent, QLatin1String(MAEMO_RC_ID))
, m_proFilePath(proFilePath)
, m_cachedSimulatorInformationValid(false)
, qemu(0)
{
init();
}
......@@ -69,20 +64,11 @@ MaemoRunConfiguration::MaemoRunConfiguration(Qt4Target *parent,
MaemoRunConfiguration *source)
: RunConfiguration(parent, source)
, m_proFilePath(source->m_proFilePath)
, m_simulator(source->m_simulator)
, m_simulatorArgs(source->m_simulatorArgs)
, m_simulatorPath(source->m_simulatorPath)
, m_visibleSimulatorParameter(source->m_visibleSimulatorParameter)
, m_simulatorLibPath(source->m_simulatorLibPath)
, m_simulatorSshPort(source->m_simulatorSshPort)
, m_simulatorGdbServerPort(source->m_simulatorGdbServerPort)
, m_cachedSimulatorInformationValid(false)
, m_gdbPath(source->m_gdbPath)
, m_devConfig(source->m_devConfig)
, m_arguments(source->m_arguments)
, m_lastDeployed(source->m_lastDeployed)
, m_debuggingHelpersLastDeployed(source->m_debuggingHelpersLastDeployed)
, qemu(0)
{
init();
}
......@@ -98,28 +84,10 @@ void MaemoRunConfiguration::init()
connect(qt4Target()->qt4Project(),
SIGNAL(proFileUpdated(Qt4ProjectManager::Internal::Qt4ProFileNode*)),
this, SLOT(proFileUpdate(Qt4ProjectManager::Internal::Qt4ProFileNode*)));
qemu = new QProcess(this);
connect(qemu, SIGNAL(error(QProcess::ProcessError)), this,
SLOT(qemuProcessError(QProcess::ProcessError)));
connect(qemu, SIGNAL(finished(int, QProcess::ExitStatus)), this,
SLOT(qemuProcessFinished()));
connect(&MaemoManager::instance(), SIGNAL(startStopQemu()), this,
SLOT(startStopQemu()));
connect(this, SIGNAL(qemuProcessStatus(QemuStatus, QString)),
&MaemoManager::instance(),
SLOT(qemuStatusChanged(QemuStatus,QString)));
}
MaemoRunConfiguration::~MaemoRunConfiguration()
{
if (qemu && qemu->state() != QProcess::NotRunning) {
qemu->terminate();
qemu->kill();
}
delete qemu;
qemu = NULL;
}
Qt4Target *MaemoRunConfiguration::qt4Target() const
......@@ -161,8 +129,6 @@ QVariantMap MaemoRunConfiguration::toMap() const
addDeployTimesToMap(DebuggingHelpersLastDeployedKey,
m_debuggingHelpersLastDeployed, map);
map.insert(SimulatorPathKey, m_simulatorPath);
const QDir dir = QDir(target()->project()->projectDirectory());
map.insert(ProFileKey, dir.relativeFilePath(m_proFilePath));
......@@ -192,8 +158,6 @@ bool MaemoRunConfiguration::fromMap(const QVariantMap &map)
getDeployTimesFromMap(DebuggingHelpersLastDeployedKey,
m_debuggingHelpersLastDeployed, map);
m_simulatorPath = map.value(SimulatorPathKey).toString();
const QDir dir = QDir(target()->project()->projectDirectory());
m_proFilePath = dir.filePath(map.value(ProFileKey).toString());
......@@ -351,40 +315,15 @@ QString MaemoRunConfiguration::executable() const
+ QLatin1Char('/') + ti.target));
}
QString MaemoRunConfiguration::simulatorSshPort() const
{
updateSimulatorInformation();
return m_simulatorSshPort;
}
QString MaemoRunConfiguration::simulatorGdbServerPort() const
{
updateSimulatorInformation();;
return m_simulatorGdbServerPort;
}
QString MaemoRunConfiguration::simulatorPath() const
{
updateSimulatorInformation();
return m_simulatorPath;
}
QString MaemoRunConfiguration::visibleSimulatorParameter() const
QString MaemoRunConfiguration::runtimeGdbServerPort() const
{
updateSimulatorInformation();
return m_visibleSimulatorParameter;
}
QString MaemoRunConfiguration::simulator() const
{
updateSimulatorInformation();
return m_simulator;
}
QString MaemoRunConfiguration::simulatorArgs() const
{
updateSimulatorInformation();
return m_simulatorArgs;
if (Qt4BuildConfiguration *qt4bc = activeQt4BuildConfiguration()) {
Runtime rt;
const int id = qt4bc->qtVersion()->uniqueId();
if (QemuRuntimeManager::instance().runtimeForQtVersion(id, &rt))
return rt.m_gdbServerPort;
}
return QLatin1String("13219");
}
void MaemoRunConfiguration::setArguments(const QStringList &args)
......@@ -392,137 +331,6 @@ void MaemoRunConfiguration::setArguments(const QStringList &args)
m_arguments = args;
}
bool MaemoRunConfiguration::isQemuRunning() const
{
return (qemu && qemu->state() != QProcess::NotRunning);
}
void MaemoRunConfiguration::updateSimulatorInformation() const
{
if (m_cachedSimulatorInformationValid)
return;
m_simulator.clear();
m_simulatorPath.clear();
m_simulatorArgs.clear();
m_visibleSimulatorParameter.clear();
m_simulatorLibPath.clear();
m_simulatorSshPort.clear();
m_simulatorGdbServerPort.clear();
m_cachedSimulatorInformationValid = true;
if (const MaemoToolChain *tc = toolchain())
m_simulatorPath = QDir::toNativeSeparators(tc->simulatorRoot());
if (!m_simulatorPath.isEmpty()) {
m_visibleSimulatorParameter = tr("'%1' does not contain a valid Maemo "
"simulator image.").arg(m_simulatorPath);
}
QDir dir = QDir(m_simulatorPath);
if (!m_simulatorPath.isEmpty() && dir.exists(m_simulatorPath)) {
const QStringList &files = dir.entryList(QDir::Files | QDir::NoSymLinks
| QDir::NoDotAndDotDot);
if (files.count() >= 2) {
const QLatin1String info("information");
if (files.contains(info)) {
QFile file(m_simulatorPath + QLatin1Char('/') + info);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMap<QString, QString> map;
QTextStream stream(&file);
while (!stream.atEnd()) {
const QString &line = stream.readLine().trimmed();
const int index = line.indexOf(QLatin1Char('='));
map.insert(line.mid(0, index).remove(QLatin1Char('\'')),
line.mid(index + 1).remove(QLatin1Char('\'')));
}
m_simulator = map.value(QLatin1String("qemu"));
m_simulatorArgs = map.value(QLatin1String("qemu_args"));
const QString &libPathSpec
= map.value(QLatin1String("libpath"));
m_simulatorLibPath
= libPathSpec.mid(libPathSpec.indexOf(QLatin1Char('=')) + 1);
m_simulatorSshPort = map.value(QLatin1String("sshport"));
m_simulatorGdbServerPort
= map.value(QLatin1String("redirport2"));
m_visibleSimulatorParameter = m_simulator
#ifdef Q_OS_WIN
+ QLatin1String(".exe")
#endif
+ QLatin1Char(' ') + m_simulatorArgs;
}
}
}
} else {
m_visibleSimulatorParameter = tr("Simulator could not be found. Please "
"check the Qt Version you are using and that a simulator image is "
"already installed.");
}
emit cachedSimulatorInformationChanged();
}
void MaemoRunConfiguration::startStopQemu()
{
ProjectExplorerPlugin *explorer = ProjectExplorerPlugin::instance();
if (explorer->session()->startupProject() != target()->project())
return;
const MaemoDeviceConfig &config = deviceConfig();
if (!config.isValid()|| config.type != MaemoDeviceConfig::Simulator)
return;
if (qemu->state() != QProcess::NotRunning) {
if (qemu->state() == QProcess::Running) {
qemu->terminate();
qemu->kill();
}
return;
}
QString root = maddeRoot();
if (root.isEmpty() || simulator().isEmpty())
return;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
#ifdef Q_OS_WIN
const QLatin1Char colon(';');
const QString path = QDir::toNativeSeparators(root + QLatin1Char('/'));
const QLatin1String key("PATH");
env.insert(key, env.value(key) % colon % path % QLatin1String("bin"));
env.insert(key, env.value(key) % colon % path % QLatin1String("madlib"));
#elif defined(Q_OS_UNIX)
const QLatin1String key("LD_LIBRARY_PATH");
env.insert(key, env.value(key) % QLatin1Char(':') % m_simulatorLibPath);
#endif
qemu->setProcessEnvironment(env);
qemu->setWorkingDirectory(simulatorPath());
const QString app = root % QLatin1String("/madlib/") % simulator()
#ifdef Q_OS_WIN
% QLatin1String(".exe")
#endif
; // keep
qemu->start(app % QLatin1Char(' ') % simulatorArgs(), QIODevice::ReadWrite);
emit qemuProcessStatus(QemuStarting);
}
void MaemoRunConfiguration::qemuProcessFinished()
{
const QemuStatus status
= qemu->exitStatus() == QProcess::CrashExit ? QemuCrashed : QemuFinished;
emit qemuProcessStatus(status);
}
void MaemoRunConfiguration::qemuProcessError(QProcess::ProcessError error)
{
if (error == QProcess::FailedToStart)
emit qemuProcessStatus(QemuFailedToStart, qemu->errorString());
}
void MaemoRunConfiguration::updateDeviceConfigurations()
{
const MaemoDeviceConfigurations &configManager
......
......@@ -36,10 +36,8 @@
#include <projectexplorer/runconfiguration.h>
#include <QtCore/QDateTime>
#include <QtCore/QProcess>
#include <QtCore/QStringList>
QT_FORWARD_DECLARE_CLASS(QProcess)
QT_FORWARD_DECLARE_CLASS(QWidget)
namespace Qt4ProjectManager {
......@@ -88,29 +86,19 @@ public:
void setArguments(const QStringList &args);
void setDeviceConfig(const MaemoDeviceConfig &deviceConfig);
MaemoDeviceConfig deviceConfig() const;
QString simulator() const;
QString simulatorArgs() const;
QString simulatorPath() const;
QString simulatorSshPort() const;
QString simulatorGdbServerPort() const;
QString visibleSimulatorParameter() const;
QString runtimeGdbServerPort() const;
const QString sshCmd() const;