2011-01-30 15:46:25 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* @file connectionmanager.cpp
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
|
|
|
* Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
|
|
|
|
* @addtogroup GCSPlugins GCS Plugins
|
|
|
|
* @{
|
|
|
|
* @addtogroup CorePlugin Core Plugin
|
|
|
|
* @{
|
|
|
|
* @brief The Core GCS plugin
|
|
|
|
*****************************************************************************/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "connectionmanager.h"
|
|
|
|
|
|
|
|
#include <aggregation/aggregate.h>
|
|
|
|
#include <coreplugin/iconnection.h>
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2013-09-15 23:06:25 +02:00
|
|
|
#include <QtSerialPort/QSerialPort>
|
|
|
|
#include <QtSerialPort/QSerialPortInfo>
|
2011-01-30 15:46:25 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QComboBox>
|
2012-01-10 18:03:58 +01:00
|
|
|
#include <QEventLoop>
|
2011-08-03 12:58:39 +02:00
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
namespace Core {
|
2013-04-28 19:41:15 +02:00
|
|
|
ConnectionManager::ConnectionManager(Internal::MainWindow *mainWindow) :
|
2012-09-08 20:33:50 +02:00
|
|
|
QWidget(mainWindow),
|
2012-07-17 01:24:22 +02:00
|
|
|
m_availableDevList(0),
|
2011-01-30 15:46:25 +01:00
|
|
|
m_connectBtn(0),
|
2012-07-17 01:24:22 +02:00
|
|
|
m_ioDev(NULL),
|
2012-09-09 02:45:02 +02:00
|
|
|
polling(true),
|
2012-07-17 01:24:22 +02:00
|
|
|
m_mainWindow(mainWindow)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2013-03-12 22:36:35 +01:00
|
|
|
// device list
|
2011-01-30 15:46:25 +01:00
|
|
|
m_availableDevList = new QComboBox;
|
2014-01-21 02:42:11 +01:00
|
|
|
m_availableDevList->setMinimumContentsLength(tr("USB: OPLinkMini").length());
|
2011-01-30 15:46:25 +01:00
|
|
|
m_availableDevList->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2013-03-12 22:36:35 +01:00
|
|
|
// connect button
|
2011-08-08 13:50:46 +02:00
|
|
|
m_connectBtn = new QPushButton(tr("Connect"));
|
2011-01-30 15:46:25 +01:00
|
|
|
m_connectBtn->setEnabled(false);
|
2013-03-12 22:36:35 +01:00
|
|
|
|
|
|
|
// put everything together
|
|
|
|
QHBoxLayout *layout = new QHBoxLayout;
|
2014-01-21 02:40:15 +01:00
|
|
|
layout->setSpacing(6);
|
2014-01-24 20:50:13 +01:00
|
|
|
layout->setContentsMargins(5, 2, 5, 2);
|
2013-04-28 19:41:15 +02:00
|
|
|
setLayout(layout);
|
2013-03-12 22:36:35 +01:00
|
|
|
|
|
|
|
layout->addWidget(new QLabel(tr("Connections:")), 0, Qt::AlignVCenter);
|
|
|
|
layout->addWidget(m_availableDevList, 0, Qt::AlignVCenter);
|
|
|
|
layout->addWidget(m_connectBtn, 0, Qt::AlignVCenter);
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2012-07-30 13:58:36 +02:00
|
|
|
QObject::connect(m_connectBtn, SIGNAL(clicked()), this, SLOT(onConnectClicked()));
|
2013-03-12 22:36:35 +01:00
|
|
|
QObject::connect(m_availableDevList, SIGNAL(currentIndexChanged(int)), this, SLOT(onDeviceSelectionChanged(int)));
|
2012-08-14 21:35:53 +02:00
|
|
|
|
|
|
|
// setup our reconnect timers
|
|
|
|
reconnect = new QTimer(this);
|
|
|
|
reconnectCheck = new QTimer(this);
|
2013-03-12 22:36:35 +01:00
|
|
|
connect(reconnect, SIGNAL(timeout()), this, SLOT(reconnectSlot()));
|
|
|
|
connect(reconnectCheck, SIGNAL(timeout()), this, SLOT(reconnectCheckSlot()));
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionManager::~ConnectionManager()
|
|
|
|
{
|
2012-09-08 20:33:50 +02:00
|
|
|
disconnectDevice();
|
|
|
|
suspendPolling();
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionManager::init()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// register to the plugin manager so we can receive
|
|
|
|
// new connection object from plugins
|
|
|
|
QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(objectAdded(QObject *)), this, SLOT(objectAdded(QObject *)));
|
|
|
|
QObject::connect(ExtensionSystem::PluginManager::instance(), SIGNAL(aboutToRemoveObject(QObject *)), this, SLOT(aboutToRemoveObject(QObject *)));
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
2013-04-28 19:41:15 +02:00
|
|
|
|
|
|
|
// TODO needs documentation?
|
|
|
|
void ConnectionManager::addWidget(QWidget *widget)
|
|
|
|
{
|
2013-12-02 23:47:27 +01:00
|
|
|
QHBoxLayout *l = (QHBoxLayout *)layout();
|
|
|
|
|
2013-04-28 19:41:15 +02:00
|
|
|
l->insertWidget(0, widget, 0, Qt::AlignVCenter);
|
|
|
|
}
|
|
|
|
|
2011-03-04 21:42:47 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Method called when the user clicks the "Connect" button
|
|
|
|
*/
|
2012-09-13 22:11:37 +02:00
|
|
|
bool ConnectionManager::connectDevice(DevListItem device)
|
2011-03-04 21:42:47 +01:00
|
|
|
{
|
2014-02-26 17:19:58 +01:00
|
|
|
Q_UNUSED(device);
|
2013-03-12 22:36:35 +01:00
|
|
|
QString deviceName = m_availableDevList->itemData(m_availableDevList->currentIndex(), Qt::ToolTipRole).toString();
|
|
|
|
DevListItem connection_device = findDevice(deviceName);
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
if (!connection_device.connection) {
|
2011-03-25 11:59:24 +01:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-03-25 11:59:24 +01:00
|
|
|
|
2012-09-08 22:14:06 +02:00
|
|
|
QIODevice *io_dev = connection_device.connection->openDevice(connection_device.device.name);
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!io_dev) {
|
2011-03-25 11:59:24 +01:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-03-25 11:59:24 +01:00
|
|
|
|
|
|
|
io_dev->open(QIODevice::ReadWrite);
|
|
|
|
|
|
|
|
// check if opening the device worked
|
|
|
|
if (!io_dev->isOpen()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
// we appear to have connected to the device OK
|
|
|
|
// remember the connection/device details
|
2012-07-24 23:10:17 +02:00
|
|
|
m_connectionDevice = connection_device;
|
2011-03-25 11:59:24 +01:00
|
|
|
m_ioDev = io_dev;
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
connect(m_connectionDevice.connection, SIGNAL(destroyed(QObject *)), this, SLOT(onConnectionDestroyed(QObject *)), Qt::QueuedConnection);
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
// signal interested plugins that we connected to the device
|
2012-09-13 22:11:37 +02:00
|
|
|
emit deviceConnected(io_dev);
|
2012-08-03 01:11:18 +02:00
|
|
|
|
2013-04-28 19:41:15 +02:00
|
|
|
m_connectBtn->setText(tr("Disconnect"));
|
|
|
|
m_availableDevList->setEnabled(false);
|
2012-08-03 01:11:18 +02:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
return true;
|
2011-03-04 21:42:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Method called by plugins who want to force a disconnection.
|
|
|
|
* Used by Uploader gadget for instance.
|
|
|
|
*/
|
2011-03-04 21:42:47 +01:00
|
|
|
bool ConnectionManager::disconnectDevice()
|
|
|
|
{
|
2011-03-25 11:59:24 +01:00
|
|
|
if (!m_ioDev) {
|
|
|
|
// apparently we are already disconnected: this can
|
|
|
|
// happen if a plugin tries to force a disconnect whereas
|
|
|
|
// we are not connected. Just return.
|
|
|
|
return false;
|
|
|
|
}
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
// We are connected - disconnect from the device
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2012-08-14 21:35:53 +02:00
|
|
|
// stop our timers
|
2013-05-19 16:37:30 +02:00
|
|
|
if (reconnect->isActive()) {
|
2012-08-14 21:35:53 +02:00
|
|
|
reconnect->stop();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
if (reconnectCheck->isActive()) {
|
2012-08-14 21:35:53 +02:00
|
|
|
reconnectCheck->stop();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-08-14 21:35:53 +02:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
// signal interested plugins that user is disconnecting his device
|
|
|
|
emit deviceAboutToDisconnect();
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
try {
|
2012-09-08 22:14:06 +02:00
|
|
|
if (m_connectionDevice.connection) {
|
|
|
|
m_connectionDevice.connection->closeDevice(m_connectionDevice.getConName());
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
} catch(...) { // handle exception
|
2012-09-08 22:29:38 +02:00
|
|
|
qDebug() << "Exception: m_connectionDevice.connection->closeDevice(" << m_connectionDevice.getConName() << ")";
|
2011-03-25 11:59:24 +01:00
|
|
|
}
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
m_connectionDevice.connection = NULL;
|
|
|
|
m_ioDev = NULL;
|
2011-03-04 21:42:47 +01:00
|
|
|
|
2013-04-28 19:41:15 +02:00
|
|
|
// signal interested plugins that we disconnected from the device
|
2012-07-17 01:24:22 +02:00
|
|
|
emit deviceDisconnected();
|
2013-04-28 19:41:15 +02:00
|
|
|
|
|
|
|
m_connectBtn->setText(tr("Connect"));
|
2011-03-25 11:59:24 +01:00
|
|
|
m_availableDevList->setEnabled(true);
|
2011-03-05 11:55:38 +01:00
|
|
|
|
2011-03-25 11:59:24 +01:00
|
|
|
return true;
|
2011-03-04 21:42:47 +01:00
|
|
|
}
|
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Slot called when a plugin added an object to the core pool
|
|
|
|
*/
|
2011-01-30 15:46:25 +01:00
|
|
|
void ConnectionManager::objectAdded(QObject *obj)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Check if a plugin added a connection object to the pool
|
2011-01-30 15:46:25 +01:00
|
|
|
IConnection *connection = Aggregation::query<IConnection>(obj);
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!connection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// register devices and populate CB
|
2011-01-30 15:46:25 +01:00
|
|
|
devChanged(connection);
|
|
|
|
|
|
|
|
// Keep track of the registration to be able to tell plugins
|
|
|
|
// to do things
|
|
|
|
m_connectionsList.append(connection);
|
|
|
|
|
2012-07-17 01:24:22 +02:00
|
|
|
QObject::connect(connection, SIGNAL(availableDevChanged(IConnection *)), this, SLOT(devChanged(IConnection *)));
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionManager::aboutToRemoveObject(QObject *obj)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// Check if a plugin added a connection object to the pool
|
2011-01-30 15:46:25 +01:00
|
|
|
IConnection *connection = Aggregation::query<IConnection>(obj);
|
2011-02-26 04:19:24 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!connection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_connectionDevice.connection && m_connectionDevice.connection == connection) { // we are currently using the one that is about to be removed
|
2012-09-08 21:31:16 +02:00
|
|
|
disconnectDevice();
|
2012-07-17 01:24:22 +02:00
|
|
|
m_connectionDevice.connection = NULL;
|
|
|
|
m_ioDev = NULL;
|
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (m_connectionsList.contains(connection)) {
|
2012-07-17 01:24:22 +02:00
|
|
|
m_connectionsList.removeAt(m_connectionsList.indexOf(connection));
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
2011-02-26 04:19:24 +01:00
|
|
|
|
2012-09-08 21:31:16 +02:00
|
|
|
void ConnectionManager::onConnectionDestroyed(QObject *obj)
|
2011-02-26 04:19:24 +01:00
|
|
|
{
|
2011-06-07 17:15:16 +02:00
|
|
|
Q_UNUSED(obj)
|
|
|
|
disconnectDevice();
|
2011-02-26 04:19:24 +01:00
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2013-03-12 22:36:35 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Slot called when the user selects a device from the combo box
|
|
|
|
*/
|
2013-03-12 22:36:35 +01:00
|
|
|
void ConnectionManager::onDeviceSelectionChanged(int index)
|
|
|
|
{
|
|
|
|
if (index >= 0) {
|
|
|
|
QString deviceName = m_availableDevList->itemData(index, Qt::ToolTipRole).toString();
|
|
|
|
m_availableDevList->setToolTip(deviceName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Slot called when the user clicks the connect/disconnect button
|
|
|
|
*/
|
2012-07-30 13:58:36 +02:00
|
|
|
void ConnectionManager::onConnectClicked()
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2011-03-25 11:59:24 +01:00
|
|
|
// Check if we have a ioDev already created:
|
2013-03-12 22:36:35 +01:00
|
|
|
if (!m_ioDev) {
|
2012-07-17 01:24:22 +02:00
|
|
|
// connecting to currently selected device
|
2013-03-12 22:36:35 +01:00
|
|
|
QString deviceName = m_availableDevList->itemData(m_availableDevList->currentIndex(), Qt::ToolTipRole).toString();
|
|
|
|
DevListItem device = findDevice(deviceName);
|
|
|
|
if (device.connection) {
|
2012-07-17 01:24:22 +02:00
|
|
|
connectDevice(device);
|
2013-03-12 22:36:35 +01:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2013-03-12 22:36:35 +01:00
|
|
|
// disconnecting
|
2011-03-25 11:59:24 +01:00
|
|
|
disconnectDevice();
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 21:35:53 +02:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Slot called when the telemetry is connected
|
|
|
|
*/
|
2012-08-14 21:35:53 +02:00
|
|
|
void ConnectionManager::telemetryConnected()
|
|
|
|
{
|
|
|
|
qDebug() << "TelemetryMonitor: connected";
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (reconnectCheck->isActive()) {
|
2012-08-17 06:13:07 +02:00
|
|
|
reconnectCheck->stop();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-08-14 21:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Slot called when the telemetry is disconnected
|
|
|
|
*/
|
2012-08-14 21:35:53 +02:00
|
|
|
void ConnectionManager::telemetryDisconnected()
|
|
|
|
{
|
|
|
|
qDebug() << "TelemetryMonitor: disconnected";
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (m_ioDev) {
|
|
|
|
if (m_connectionDevice.connection->shortName() == "Serial") {
|
|
|
|
if (!reconnect->isActive()) {
|
2012-08-17 06:13:07 +02:00
|
|
|
reconnect->start(1000);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-08-17 06:13:07 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-14 21:35:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionManager::reconnectSlot()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
qDebug() << "reconnect";
|
|
|
|
if (m_ioDev->isOpen()) {
|
2012-08-14 21:35:53 +02:00
|
|
|
m_ioDev->close();
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-08-14 21:35:53 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (m_ioDev->open(QIODevice::ReadWrite)) {
|
|
|
|
qDebug() << "reconnect successfull";
|
2012-08-14 21:35:53 +02:00
|
|
|
reconnect->stop();
|
|
|
|
reconnectCheck->start(20000);
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
|
|
|
qDebug() << "reconnect NOT successfull";
|
2012-08-14 21:35:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionManager::reconnectCheckSlot()
|
|
|
|
{
|
|
|
|
reconnectCheck->stop();
|
|
|
|
reconnect->start(1000);
|
2012-08-03 01:11:18 +02:00
|
|
|
}
|
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Find a device by its displayed (visible on screen) name
|
|
|
|
*/
|
2012-09-08 22:45:08 +02:00
|
|
|
DevListItem ConnectionManager::findDevice(const QString &devName)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
foreach(DevListItem d, m_devList) {
|
|
|
|
if (d.getConName() == devName) {
|
2011-01-30 15:46:25 +01:00
|
|
|
return d;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
2011-02-26 04:19:24 +01:00
|
|
|
|
2011-03-29 20:25:24 +02:00
|
|
|
qDebug() << "findDevice: cannot find " << devName << " in device list";
|
2011-02-26 04:19:24 +01:00
|
|
|
|
2012-09-08 22:45:08 +02:00
|
|
|
DevListItem d;
|
2011-01-30 15:46:25 +01:00
|
|
|
d.connection = NULL;
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Tells every connection plugin to stop polling for devices if they
|
|
|
|
* are doing that.
|
|
|
|
*
|
|
|
|
*/
|
2011-01-30 15:46:25 +01:00
|
|
|
void ConnectionManager::suspendPolling()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
foreach(IConnection * cnx, m_connectionsList) {
|
2011-01-30 15:46:25 +01:00
|
|
|
cnx->suspendPolling();
|
|
|
|
}
|
|
|
|
|
2012-07-17 01:24:22 +02:00
|
|
|
m_connectBtn->setEnabled(false);
|
|
|
|
m_availableDevList->setEnabled(false);
|
2012-09-09 02:06:03 +02:00
|
|
|
polling = false;
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Tells every connection plugin to resume polling for devices if they
|
|
|
|
* are doing that.
|
|
|
|
*/
|
2011-01-30 15:46:25 +01:00
|
|
|
void ConnectionManager::resumePolling()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
foreach(IConnection * cnx, m_connectionsList) {
|
2011-01-30 15:46:25 +01:00
|
|
|
cnx->resumePolling();
|
|
|
|
}
|
|
|
|
|
2012-07-17 01:24:22 +02:00
|
|
|
m_connectBtn->setEnabled(true);
|
|
|
|
m_availableDevList->setEnabled(true);
|
2012-09-09 02:06:03 +02:00
|
|
|
polling = true;
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
2011-03-04 21:42:47 +01:00
|
|
|
/**
|
2012-09-08 19:53:00 +02:00
|
|
|
* Synchronize the list of connections displayed with those physically
|
|
|
|
* present
|
|
|
|
* @param[in] connection Connection type that you want to forget about :)
|
|
|
|
*/
|
|
|
|
void ConnectionManager::updateConnectionList(IConnection *connection)
|
2011-03-04 21:42:47 +01:00
|
|
|
{
|
2012-09-08 22:59:54 +02:00
|
|
|
// Get the updated list of devices
|
2012-09-08 20:23:37 +02:00
|
|
|
QList <IConnection::device> availableDev = connection->availableDevices();
|
|
|
|
|
|
|
|
// Go through the list of connections of that type. If they are not in the
|
|
|
|
// available device list then remove them. If they are connected, then
|
|
|
|
// disconnect them.
|
2013-05-19 16:37:30 +02:00
|
|
|
for (QLinkedList<DevListItem>::iterator iter = m_devList.begin(); iter != m_devList.end();) {
|
2012-09-08 22:59:54 +02:00
|
|
|
if (iter->connection != connection) {
|
|
|
|
++iter;
|
|
|
|
continue;
|
2012-09-08 22:14:06 +02:00
|
|
|
}
|
2012-09-08 22:59:54 +02:00
|
|
|
|
|
|
|
// See if device exists in the updated availability list
|
|
|
|
bool found = availableDev.contains(iter->device);
|
|
|
|
if (!found) {
|
|
|
|
// we are currently using the one we are about to erase
|
2012-09-10 00:37:43 +02:00
|
|
|
if (m_connectionDevice.connection && m_connectionDevice.connection == connection && m_connectionDevice.device == iter->device) {
|
2012-07-17 01:24:22 +02:00
|
|
|
disconnectDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
iter = m_devList.erase(iter);
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2012-07-17 01:24:22 +02:00
|
|
|
++iter;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-07-17 01:24:22 +02:00
|
|
|
}
|
2012-09-08 19:53:00 +02:00
|
|
|
|
2012-09-08 22:59:54 +02:00
|
|
|
// Add any back to list that don't exist
|
2013-05-19 16:37:30 +02:00
|
|
|
foreach(IConnection::device dev, availableDev) {
|
2012-09-08 22:59:54 +02:00
|
|
|
bool found = m_devList.contains(DevListItem(connection, dev));
|
2013-05-19 16:37:30 +02:00
|
|
|
|
2012-09-08 20:23:37 +02:00
|
|
|
if (!found) {
|
2013-05-19 16:37:30 +02:00
|
|
|
registerDevice(connection, dev);
|
2012-09-08 20:23:37 +02:00
|
|
|
}
|
2012-09-08 19:53:00 +02:00
|
|
|
}
|
2011-03-04 21:42:47 +01:00
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* Register a device from a specific connection plugin
|
|
|
|
* @param devN contains the connection shortname + device name which is diplayed in the tooltip
|
|
|
|
* @param disp is the name that is displayed in the dropdown menu
|
|
|
|
* @param name is the actual device name
|
|
|
|
*/
|
2012-09-08 21:04:59 +02:00
|
|
|
void ConnectionManager::registerDevice(IConnection *conn, IConnection::device device)
|
2011-01-30 15:46:25 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
DevListItem d(conn, device);
|
|
|
|
|
2011-01-30 15:46:25 +01:00
|
|
|
m_devList.append(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* A device plugin notified us that its device list has changed
|
|
|
|
* (eg: user plug/unplug a usb device)
|
|
|
|
* \param[in] connection Connection type which signaled the update
|
|
|
|
*/
|
2011-01-30 15:46:25 +01:00
|
|
|
void ConnectionManager::devChanged(IConnection *connection)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!ExtensionSystem::PluginManager::instance()->allPluginsLoaded()) {
|
2012-01-10 18:03:58 +01:00
|
|
|
connectionBackup.append(connection);
|
2013-05-19 16:37:30 +02:00
|
|
|
connect(ExtensionSystem::PluginManager::instance(), SIGNAL(pluginsLoadEnded()), this, SLOT(connectionsCallBack()), Qt::UniqueConnection);
|
2012-01-10 18:03:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
// clear device list combobox
|
2011-01-30 15:46:25 +01:00
|
|
|
m_availableDevList->clear();
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// remove registered devices of this IConnection from the list
|
2012-09-08 19:53:00 +02:00
|
|
|
updateConnectionList(connection);
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2012-09-08 22:14:06 +02:00
|
|
|
updateConnectionDropdown();
|
2011-01-30 15:46:25 +01:00
|
|
|
|
2012-07-17 01:24:22 +02:00
|
|
|
qDebug() << "# devices " << m_devList.count();
|
|
|
|
emit availableDevicesChanged(m_devList);
|
|
|
|
|
2012-09-08 22:14:06 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// disable connection button if the liNameif (m_availableDevList->count() > 0)
|
|
|
|
if (m_availableDevList->count() > 0) {
|
2012-09-08 22:14:06 +02:00
|
|
|
m_connectBtn->setEnabled(true);
|
2013-05-19 16:37:30 +02:00
|
|
|
} else {
|
2012-09-08 22:14:06 +02:00
|
|
|
m_connectBtn->setEnabled(false);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2012-09-08 22:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConnectionManager::updateConnectionDropdown()
|
|
|
|
{
|
2013-03-12 22:36:35 +01:00
|
|
|
// add all the list again to the combobox
|
2013-05-19 16:37:30 +02:00
|
|
|
foreach(DevListItem d, m_devList) {
|
2012-09-08 22:14:06 +02:00
|
|
|
m_availableDevList->addItem(d.getConName());
|
2013-03-12 22:36:35 +01:00
|
|
|
m_availableDevList->setItemData(m_availableDevList->count() - 1, d.getConName(), Qt::ToolTipRole);
|
|
|
|
if (!m_ioDev && d.getConName().startsWith("USB")) {
|
|
|
|
if (m_mainWindow->generalSettings()->autoConnect() || m_mainWindow->generalSettings()->autoSelect()) {
|
2012-07-17 01:24:22 +02:00
|
|
|
m_availableDevList->setCurrentIndex(m_availableDevList->count() - 1);
|
2013-03-12 22:36:35 +01:00
|
|
|
}
|
|
|
|
if (m_mainWindow->generalSettings()->autoConnect() && polling) {
|
2012-09-09 02:06:03 +02:00
|
|
|
qDebug() << "Automatically opening device";
|
2012-09-13 22:11:37 +02:00
|
|
|
connectDevice(d);
|
2013-03-12 22:36:35 +01:00
|
|
|
qDebug() << "ConnectionManager::updateConnectionDropdown autoconnected USB device";
|
2011-11-26 01:04:36 +01:00
|
|
|
}
|
2011-05-31 16:29:53 +02:00
|
|
|
}
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
2013-03-12 22:36:35 +01:00
|
|
|
if (m_ioDev) {
|
|
|
|
// if a device is connected make it the one selected on the dropbox
|
|
|
|
for (int i = 0; i < m_availableDevList->count(); i++) {
|
|
|
|
QString deviceName = m_availableDevList->itemData(i, Qt::ToolTipRole).toString();
|
|
|
|
if (m_connectionDevice.getConName() == deviceName) {
|
|
|
|
m_availableDevList->setCurrentIndex(i);
|
|
|
|
}
|
2011-05-31 16:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
2013-03-12 22:36:35 +01:00
|
|
|
// update combo box tooltip
|
|
|
|
onDeviceSelectionChanged(m_availableDevList->currentIndex());
|
2011-01-30 15:46:25 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 18:03:58 +01:00
|
|
|
void Core::ConnectionManager::connectionsCallBack()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
foreach(IConnection * con, connectionBackup) {
|
2012-01-10 18:03:58 +01:00
|
|
|
devChanged(con);
|
|
|
|
}
|
|
|
|
connectionBackup.clear();
|
2013-05-19 16:37:30 +02:00
|
|
|
disconnect(ExtensionSystem::PluginManager::instance(), SIGNAL(pluginsLoadEnded()), this, SLOT(connectionsCallBack()));
|
2012-07-17 01:24:22 +02:00
|
|
|
}
|
2013-05-19 16:37:30 +02:00
|
|
|
} // namespace Core
|