From 3efdb87682a5785bac7b90f9f9a8bb819a1cb053 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Date: Mon, 8 Feb 2010 14:10:51 +0100
Subject: [PATCH] S60/Trk: Make the port a property of TrkDevice.

Associate the port with the device instead of passing it to open
for better handling.
---
 .../gdb/s60debuggerbluetoothstarter.cpp       |  3 --
 .../gdb/s60debuggerbluetoothstarter.h         |  1 -
 src/plugins/debugger/gdb/trkgdbadapter.cpp    |  3 +-
 .../qt-s60/s60devicerunconfiguration.cpp      |  2 --
 .../s60devicerunconfigurationwidget.cpp       |  1 -
 .../qt-s60/s60runconfigbluetoothstarter.cpp   |  3 --
 .../qt-s60/s60runconfigbluetoothstarter.h     |  1 -
 .../symbianutils/communicationstarter.cpp     | 21 ++++---------
 .../symbianutils/communicationstarter.h       |  4 +--
 src/shared/symbianutils/launcher.cpp          |  9 +++---
 src/shared/symbianutils/trkdevice.cpp         | 31 ++++++++++++++-----
 src/shared/symbianutils/trkdevice.h           |  6 +++-
 tests/manual/trklauncher/main.cpp             |  1 -
 13 files changed, 42 insertions(+), 44 deletions(-)

diff --git a/src/plugins/debugger/gdb/s60debuggerbluetoothstarter.cpp b/src/plugins/debugger/gdb/s60debuggerbluetoothstarter.cpp
index ad2869aac58..41856d8014f 100644
--- a/src/plugins/debugger/gdb/s60debuggerbluetoothstarter.cpp
+++ b/src/plugins/debugger/gdb/s60debuggerbluetoothstarter.cpp
@@ -51,7 +51,6 @@ trk::BluetoothListener *S60DebuggerBluetoothStarter::createListener()
 
 trk::PromptStartCommunicationResult
 S60DebuggerBluetoothStarter::startCommunication(const TrkDevicePtr &trkDevice,
-                                                 const QString &device,
                                                  int communicationType,
                                                  QWidget *msgBoxParent,
                                                  QString *errorMessage)
@@ -59,12 +58,10 @@ S60DebuggerBluetoothStarter::startCommunication(const TrkDevicePtr &trkDevice,
     // Bluetooth?
     if (communicationType == TrkOptions::BlueTooth) {
         S60DebuggerBluetoothStarter bluetoothStarter(trkDevice);
-        bluetoothStarter.setDevice(device);
         return trk::promptStartBluetooth(bluetoothStarter, msgBoxParent, errorMessage);
     }
     // Serial
     BaseCommunicationStarter serialStarter(trkDevice);
-    serialStarter.setDevice(device);
     return trk::promptStartSerial(serialStarter, msgBoxParent, errorMessage);
 }
 
diff --git a/src/plugins/debugger/gdb/s60debuggerbluetoothstarter.h b/src/plugins/debugger/gdb/s60debuggerbluetoothstarter.h
index 1fbf16a9c82..c97088c2452 100644
--- a/src/plugins/debugger/gdb/s60debuggerbluetoothstarter.h
+++ b/src/plugins/debugger/gdb/s60debuggerbluetoothstarter.h
@@ -47,7 +47,6 @@ class S60DebuggerBluetoothStarter : public trk::AbstractBluetoothStarter
 public:
     static trk::PromptStartCommunicationResult
         startCommunication(const TrkDevicePtr &trkDevice,
-                           const QString &device,
                            int communicationType,
                            QWidget *msgBoxParent,
                            QString *errorMessage);
diff --git a/src/plugins/debugger/gdb/trkgdbadapter.cpp b/src/plugins/debugger/gdb/trkgdbadapter.cpp
index ca905af1938..a3f4513cd32 100644
--- a/src/plugins/debugger/gdb/trkgdbadapter.cpp
+++ b/src/plugins/debugger/gdb/trkgdbadapter.cpp
@@ -1730,12 +1730,13 @@ void TrkGdbAdapter::startAdapter()
     setState(AdapterStarting);
     debugMessage(_("TRYING TO START ADAPTER"));
     logMessage(QLatin1String("### Starting TrkGdbAdapter"));
+    m_trkDevice->setPort(effectiveTrkDevice());
     m_trkDevice->setSerialFrame(effectiveTrkDeviceType() != TrkOptions::BlueTooth);
     // Prompt the user to start communication
     QString message;
+
     const trk::PromptStartCommunicationResult src =
             S60DebuggerBluetoothStarter::startCommunication(m_trkDevice,
-                                                            effectiveTrkDevice(),
                                                             effectiveTrkDeviceType(),
                                                             0, &message);
     switch (src) {
diff --git a/src/plugins/qt4projectmanager/qt-s60/s60devicerunconfiguration.cpp b/src/plugins/qt4projectmanager/qt-s60/s60devicerunconfiguration.cpp
index e33867b5285..5d0aef4a40d 100644
--- a/src/plugins/qt4projectmanager/qt-s60/s60devicerunconfiguration.cpp
+++ b/src/plugins/qt4projectmanager/qt-s60/s60devicerunconfiguration.cpp
@@ -764,7 +764,6 @@ void S60DeviceRunControlBase::startDeployment()
     // Prompt the user to start up the Blue tooth connection
     const trk::PromptStartCommunicationResult src =
             S60RunConfigBluetoothStarter::startCommunication(m_launcher->trkDevice(),
-                                                             m_serialPortName,
                                                              m_communicationType, 0,
                                                              &errorMessage);
     switch (src) {
@@ -779,7 +778,6 @@ void S60DeviceRunControlBase::startDeployment()
     };
 
     if (!m_launcher->startServer(&errorMessage)) {
-
         error(this, tr("Could not connect to phone on port '%1': %2\n"
                        "Check if the phone is connected and App TRK is running.").arg(m_serialPortName, errorMessage));
         stop();
diff --git a/src/plugins/qt4projectmanager/qt-s60/s60devicerunconfigurationwidget.cpp b/src/plugins/qt4projectmanager/qt-s60/s60devicerunconfigurationwidget.cpp
index 9c82b77d90b..e496b174eb3 100644
--- a/src/plugins/qt4projectmanager/qt-s60/s60devicerunconfigurationwidget.cpp
+++ b/src/plugins/qt4projectmanager/qt-s60/s60devicerunconfigurationwidget.cpp
@@ -330,7 +330,6 @@ void S60DeviceRunConfigurationWidget::updateDeviceInfo()
     QString message;
     const trk::PromptStartCommunicationResult src =
             S60RunConfigBluetoothStarter::startCommunication(m_infoLauncher->trkDevice(),
-                                                             commDev.portName(),
                                                              commDev.type(), this,
                                                              &message);
     switch (src) {
diff --git a/src/plugins/qt4projectmanager/qt-s60/s60runconfigbluetoothstarter.cpp b/src/plugins/qt4projectmanager/qt-s60/s60runconfigbluetoothstarter.cpp
index 593c85c0d2e..9bd6e2a60f2 100644
--- a/src/plugins/qt4projectmanager/qt-s60/s60runconfigbluetoothstarter.cpp
+++ b/src/plugins/qt4projectmanager/qt-s60/s60runconfigbluetoothstarter.cpp
@@ -53,7 +53,6 @@ trk::BluetoothListener *S60RunConfigBluetoothStarter::createListener()
 
 trk::PromptStartCommunicationResult
 S60RunConfigBluetoothStarter::startCommunication(const TrkDevicePtr &trkDevice,
-                                                 const QString &device,
                                                  int communicationType,
                                                  QWidget *msgBoxParent,
                                                  QString *errorMessage)
@@ -61,12 +60,10 @@ S60RunConfigBluetoothStarter::startCommunication(const TrkDevicePtr &trkDevice,
     // Bluetooth?
     if (communicationType == SymbianUtils::BlueToothCommunication) {
         S60RunConfigBluetoothStarter bluetoothStarter(trkDevice);
-        bluetoothStarter.setDevice(device);
         return trk::promptStartBluetooth(bluetoothStarter, msgBoxParent, errorMessage);
     }
     // Serial
     BaseCommunicationStarter serialStarter(trkDevice);
-    serialStarter.setDevice(device);
     return trk::promptStartSerial(serialStarter, msgBoxParent, errorMessage);
 }
 
diff --git a/src/plugins/qt4projectmanager/qt-s60/s60runconfigbluetoothstarter.h b/src/plugins/qt4projectmanager/qt-s60/s60runconfigbluetoothstarter.h
index 70ea0123f5d..67822af3485 100644
--- a/src/plugins/qt4projectmanager/qt-s60/s60runconfigbluetoothstarter.h
+++ b/src/plugins/qt4projectmanager/qt-s60/s60runconfigbluetoothstarter.h
@@ -49,7 +49,6 @@ public:
     // passing on the right messages.
     static trk::PromptStartCommunicationResult
             startCommunication(const TrkDevicePtr &trkDevice,
-                               const QString &device,
                                int communicationType,
                                QWidget *msgBoxParent,
                                QString *errorMessage);
diff --git a/src/shared/symbianutils/communicationstarter.cpp b/src/shared/symbianutils/communicationstarter.cpp
index b425db25062..83121cbe14d 100644
--- a/src/shared/symbianutils/communicationstarter.cpp
+++ b/src/shared/symbianutils/communicationstarter.cpp
@@ -46,7 +46,6 @@ struct BaseCommunicationStarterPrivate {
     int intervalMS;
     int attempts;
     int n;
-    QString device;
     QString errorString;
     BaseCommunicationStarter::State state;
 };
@@ -58,7 +57,6 @@ BaseCommunicationStarterPrivate::BaseCommunicationStarterPrivate(const BaseCommu
         intervalMS(1000),
         attempts(-1),
         n(0),
-        device(QLatin1String("/dev/rfcomm0")),
         state(BaseCommunicationStarter::TimedOut)
 {
 }
@@ -96,7 +94,7 @@ BaseCommunicationStarter::StartResult BaseCommunicationStarter::start()
     // Before we instantiate timers, and such, try to open the device,
     // which should succeed if another listener is already running in
     // 'Watch' mode
-    if (d->trkDevice->open(d->device , &(d->errorString)))
+    if (d->trkDevice->open(&(d->errorString)))
         return ConnectionSucceeded;
     // Pull up resources for next attempt
     d->n = 0;
@@ -143,12 +141,7 @@ void BaseCommunicationStarter::setAttempts(int a)
 
 QString BaseCommunicationStarter::device() const
 {
-    return d->device;
-}
-
-void BaseCommunicationStarter::setDevice(const QString &dv)
-{
-    d->device = dv;
+    return d->trkDevice->port();
 }
 
 QString BaseCommunicationStarter::errorString() const
@@ -163,20 +156,20 @@ void BaseCommunicationStarter::slotTimer()
     if (d->attempts >= 0 && d->n >= d->attempts) {
         stopTimer();
         d->errorString = tr("%1: timed out after %n attempts using an interval of %2ms.", 0, d->n)
-                         .arg(d->device).arg(d->intervalMS);
+                         .arg(d->trkDevice->port()).arg(d->intervalMS);
         d->state = TimedOut;
         emit timeout();
     } else {
         // Attempt n to connect?
-        if (d->trkDevice->open(d->device , &(d->errorString))) {
+        if (d->trkDevice->open(&(d->errorString))) {
             stopTimer();
-            const QString msg = tr("%1: Connection attempt %2 succeeded.").arg(d->device).arg(d->n);
+            const QString msg = tr("%1: Connection attempt %2 succeeded.").arg(d->trkDevice->port()).arg(d->n);
             emit message(msg);
             d->state = Connected;
             emit connected();
         } else {
             const QString msg = tr("%1: Connection attempt %2 failed: %3 (retrying)...")
-                                .arg(d->device).arg(d->n).arg(d->errorString);
+                                .arg(d->trkDevice->port()).arg(d->n).arg(d->errorString);
             emit message(msg);
         }
     }
@@ -216,13 +209,11 @@ BluetoothListener *ConsoleBluetoothStarter::createListener()
 
 bool ConsoleBluetoothStarter::startBluetooth(const TrkDevicePtr &trkDevice,
                                              QObject *listenerParent,
-                                             const QString &device,
                                              int attempts,
                                              QString *errorMessage)
 {
     // Set up a console starter to print to stdout.
     ConsoleBluetoothStarter starter(trkDevice, listenerParent);
-    starter.setDevice(device);
     starter.setAttempts(attempts);
     switch (starter.start()) {
     case Started:
diff --git a/src/shared/symbianutils/communicationstarter.h b/src/shared/symbianutils/communicationstarter.h
index 304ec06f73c..e61d2e4fc55 100644
--- a/src/shared/symbianutils/communicationstarter.h
+++ b/src/shared/symbianutils/communicationstarter.h
@@ -67,8 +67,7 @@ public:
     int attempts() const;
     void setAttempts(int a);
 
-    QString device() const;
-    void setDevice(const QString &);
+    QString device() const; // via TrkDevice
 
     State state() const;
     QString errorString() const;
@@ -130,7 +129,6 @@ class SYMBIANUTILS_EXPORT ConsoleBluetoothStarter : public AbstractBluetoothStar
 public:
     static bool startBluetooth(const TrkDevicePtr& trkDevice,
                                QObject *listenerParent,
-                               const QString &device,
                                int attempts,
                                QString *errorMessage);
 
diff --git a/src/shared/symbianutils/launcher.cpp b/src/shared/symbianutils/launcher.cpp
index e927c412263..6f521571f52 100644
--- a/src/shared/symbianutils/launcher.cpp
+++ b/src/shared/symbianutils/launcher.cpp
@@ -55,7 +55,6 @@ struct LauncherPrivate {
     explicit LauncherPrivate(const TrkDevicePtr &d);
 
     TrkDevicePtr m_device;
-    QString m_trkServerName;
     QByteArray m_trkReadBuffer;
     Launcher::State m_state;
 
@@ -119,12 +118,12 @@ void Launcher::addStartupActions(trk::Launcher::Actions startupActions)
 
 void Launcher::setTrkServerName(const QString &name)
 {
-    d->m_trkServerName = name;
+    d->m_device->setPort(name);
 }
 
 QString Launcher::trkServerName() const
 {
-    return d->m_trkServerName;
+    return d->m_device->port();
 }
 
 TrkDevicePtr Launcher::trkDevice() const
@@ -179,7 +178,7 @@ bool Launcher::startServer(QString *errorMessage)
     errorMessage->clear();
     if (d->m_verbose) {
         const QString msg = QString::fromLatin1("Port=%1 Executable=%2 Arguments=%3 Package=%4 Remote Package=%5 Install file=%6")
-                            .arg(d->m_trkServerName, d->m_fileName,
+                            .arg(trkServerName(), d->m_fileName,
                                  d->m_commandLineArgs.join(QString(QLatin1Char(' '))),
                                  d->m_copyState.sourceFileName, d->m_copyState.destinationFileName, d->m_installFileName);
         logMessage(msg);
@@ -201,7 +200,7 @@ bool Launcher::startServer(QString *errorMessage)
         qWarning("No remote executable given for running.");
         return false;
     }
-    if (!d->m_device->isOpen() && !d->m_device->open(d->m_trkServerName, errorMessage))
+    if (!d->m_device->isOpen() && !d->m_device->open(errorMessage))
         return false;
     if (d->m_closeDevice) {
         connect(this, SIGNAL(finished()), d->m_device.data(), SLOT(close()));
diff --git a/src/shared/symbianutils/trkdevice.cpp b/src/shared/symbianutils/trkdevice.cpp
index e7fb8c4f788..6c5b8b2648f 100644
--- a/src/shared/symbianutils/trkdevice.cpp
+++ b/src/shared/symbianutils/trkdevice.cpp
@@ -871,6 +871,7 @@ struct TrkDevicePrivate
     QByteArray trkReadBuffer;
     int verbose;
     QString errorString;
+    QString port;
 };
 
 ///////////////////////////////////////////////////////////////////////
@@ -902,13 +903,19 @@ TrkDevice::~TrkDevice()
     delete d;
 }
 
-bool TrkDevice::open(const QString &port, QString *errorMessage)
+bool TrkDevice::open(QString *errorMessage)
 {
     if (d->verbose)
-        qDebug() << "Opening" << port << "is open: " << isOpen() << " serialFrame=" << serialFrame();
+        qDebug() << "Opening" << port() << "is open: " << isOpen() << " serialFrame=" << serialFrame();
+    if (d->port.isEmpty()) {
+        *errorMessage = QLatin1String("Internal error: No port set on TrkDevice");
+        return false;
+    }
+
     close();
 #ifdef Q_OS_WIN
-    d->deviceContext->device = CreateFile(QString("\\\\.\\").append(port).toStdWString().c_str(),
+    const QString fullPort = QLatin1String("\\\\.\\") + d->port;
+    d->deviceContext->device = CreateFile(reinterpret_cast<const WCHAR*>(fullPort.utf16()),
                            GENERIC_READ | GENERIC_WRITE,
                            0,
                            NULL,
@@ -917,7 +924,7 @@ bool TrkDevice::open(const QString &port, QString *errorMessage)
                            NULL);
 
     if (INVALID_HANDLE_VALUE == d->deviceContext->device) {
-        *errorMessage = QString::fromLatin1("Could not open device '%1': %2").arg(port, winErrorMessage(GetLastError()));
+        *errorMessage = QString::fromLatin1("Could not open device '%1': %2").arg(port(), winErrorMessage(GetLastError()));
         return false;
     }
     memset(&d->deviceContext->readOverlapped, 0, sizeof(OVERLAPPED));
@@ -929,9 +936,9 @@ bool TrkDevice::open(const QString &port, QString *errorMessage)
         return false;
     }
 #else
-    d->deviceContext->file.setFileName(port);
+    d->deviceContext->file.setFileName(d->port);
     if (!d->deviceContext->file.open(QIODevice::ReadWrite|QIODevice::Unbuffered)) {
-        *errorMessage = QString::fromLatin1("Cannot open %1: %2").arg(port, d->deviceContext->file.errorString());
+        *errorMessage = QString::fromLatin1("Cannot open %1: %2").arg(d->port, d->deviceContext->file.errorString());
         return false;
     }
 
@@ -970,7 +977,7 @@ bool TrkDevice::open(const QString &port, QString *errorMessage)
     d->writerThread->start();
 
     if (d->verbose)
-        qDebug() << "Opened" << port;
+        qDebug() << "Opened" << d->port;
     return true;
 }
 
@@ -1004,6 +1011,16 @@ bool TrkDevice::isOpen() const
 #endif
 }
 
+QString TrkDevice::port() const
+{
+    return d->port;
+}
+
+void TrkDevice::setPort(const QString &p)
+{
+    d->port = p;
+}
+
 QString TrkDevice::errorString() const
 {
     return d->errorString;
diff --git a/src/shared/symbianutils/trkdevice.h b/src/shared/symbianutils/trkdevice.h
index 7a906e5cec5..763b60ff251 100644
--- a/src/shared/symbianutils/trkdevice.h
+++ b/src/shared/symbianutils/trkdevice.h
@@ -68,13 +68,17 @@ class SYMBIANUTILS_EXPORT TrkDevice : public QObject
     Q_OBJECT
     Q_PROPERTY(bool serialFrame READ serialFrame WRITE setSerialFrame)
     Q_PROPERTY(bool verbose READ verbose WRITE setVerbose)
+    Q_PROPERTY(QString port READ port WRITE setPort)
 public:
     explicit TrkDevice(QObject *parent = 0);
     virtual ~TrkDevice();
 
-    bool open(const QString &port, QString *errorMessage);
+    bool open(QString *errorMessage);
     bool isOpen() const;
 
+    QString port() const;
+    void setPort(const QString &p);
+
     QString errorString() const;
 
     bool serialFrame() const;
diff --git a/tests/manual/trklauncher/main.cpp b/tests/manual/trklauncher/main.cpp
index 0f8570646dd..ec53d0bcad9 100644
--- a/tests/manual/trklauncher/main.cpp
+++ b/tests/manual/trklauncher/main.cpp
@@ -153,7 +153,6 @@ int main(int argc, char *argv[])
     QString errorMessage;
     if (bluetooth && !trk::ConsoleBluetoothStarter::startBluetooth(launcher->trkDevice(),
                                                      launcher.data(),
-                                                     launcher->trkServerName(),
                                                      30, &errorMessage)) {
         qWarning("%s\n", qPrintable(errorMessage));
         return -1;
-- 
GitLab