From 514ba53b3585c20db19ef4d9b841deaf597fac33 Mon Sep 17 00:00:00 2001
From: Marco Bubke <marco.bubke@digia.com>
Date: Thu, 4 Jul 2013 15:44:58 +0200
Subject: [PATCH] QmlDesigner: Add capturing for qml puppet streams

Change-Id: I094e01b85cfe1ddafb0590f56d9d5b810a390cb4
Reviewed-by: Thomas Hartmann <Thomas.Hartmann@digia.com>
---
 .../qmldesigner/QmlDesigner.pluginspec.in     |  3 ++
 .../instances/nodeinstanceserverproxy.cpp     | 37 +++++++++++++++----
 .../instances/nodeinstanceserverproxy.h       |  7 ++--
 src/plugins/qmldesigner/qmldesignerplugin.cpp |  1 +
 4 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/src/plugins/qmldesigner/QmlDesigner.pluginspec.in b/src/plugins/qmldesigner/QmlDesigner.pluginspec.in
index 693fd5f814..b9fb1896a0 100644
--- a/src/plugins/qmldesigner/QmlDesigner.pluginspec.in
+++ b/src/plugins/qmldesigner/QmlDesigner.pluginspec.in
@@ -20,4 +20,7 @@ will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.</license>
     <description>Visual Designer for QML files.</description>
     <url>http://www.qt-project.org</url>
     $$dependencyList
+   <argumentList>
+      <argument name=\"-capture-puppet-stream\" parameter=\"capture file\">Captures the Qml Puppet stream</argument>
+    </argumentList>
 </plugin>
diff --git a/src/plugins/qmldesigner/designercore/instances/nodeinstanceserverproxy.cpp b/src/plugins/qmldesigner/designercore/instances/nodeinstanceserverproxy.cpp
index f366abbb01..b99756e0a4 100644
--- a/src/plugins/qmldesigner/designercore/instances/nodeinstanceserverproxy.cpp
+++ b/src/plugins/qmldesigner/designercore/instances/nodeinstanceserverproxy.cpp
@@ -38,6 +38,7 @@
 #include <QDir>
 #include <QTimer>
 #include <QTextStream>
+#include <QMessageBox>
 
 #include "createinstancescommand.h"
 #include "createscenecommand.h"
@@ -240,6 +241,13 @@ NodeInstanceServerProxy::NodeInstanceServerProxy(NodeInstanceView *nodeInstanceV
    } else {
            QMessageBox::warning(0, tr("Cannot Find QML Puppet Executable"), missingQmlPuppetErrorMessage(applicationPath));
    }
+
+   int indexOfCapturePuppetStream = QCoreApplication::arguments().indexOf("-capture-puppet-stream");
+   if (indexOfCapturePuppetStream > 0) {
+       m_captureFileForTest.setFileName(QCoreApplication::arguments().at(indexOfCapturePuppetStream + 1));
+       bool isOpen = m_captureFileForTest.open(QIODevice::WriteOnly);
+       qDebug() << "file is open: " << isOpen;
+   }
 }
 
 NodeInstanceServerProxy::~NodeInstanceServerProxy()
@@ -333,9 +341,9 @@ QString NodeInstanceServerProxy::missingQmlPuppetErrorMessage(const QString &app
     return message;
 }
 
-static void writeCommandToSocket(const QVariant &command, QLocalSocket *socket, unsigned int commandCounter)
+static void writeCommandToIODecive(const QVariant &command, QIODevice *ioDevice, unsigned int commandCounter)
 {
-    if (socket) {
+    if (ioDevice) {
         QByteArray block;
         QDataStream out(&block, QIODevice::WriteOnly);
         out.setVersion(QDataStream::Qt_4_8);
@@ -345,22 +353,29 @@ static void writeCommandToSocket(const QVariant &command, QLocalSocket *socket,
         out.device()->seek(0);
         out << quint32(block.size() - sizeof(quint32));
 
-        socket->write(block);
+        ioDevice->write(block);
     }
 }
 
 void NodeInstanceServerProxy::writeCommand(const QVariant &command)
 {
-    writeCommandToSocket(command, m_firstSocket.data(), m_writeCommandCounter);
-    writeCommandToSocket(command, m_secondSocket.data(), m_writeCommandCounter);
-    writeCommandToSocket(command, m_thirdSocket.data(), m_writeCommandCounter);
+    writeCommandToIODecive(command, m_firstSocket.data(), m_writeCommandCounter);
+    writeCommandToIODecive(command, m_secondSocket.data(), m_writeCommandCounter);
+    writeCommandToIODecive(command, m_thirdSocket.data(), m_writeCommandCounter);
+
+    if (m_captureFileForTest.isWritable()) {
+        qDebug() << "Write strean to file: " << m_captureFileForTest.fileName();
+        writeCommandToIODecive(command, &m_captureFileForTest, m_writeCommandCounter);
+        qDebug() << "\twrite file: " << m_captureFileForTest.pos();
+    }
+
     m_writeCommandCounter++;
     if (m_runModus == TestModus) {
         static int synchronizeId = 0;
         synchronizeId++;
         SynchronizeCommand synchronizeCommand(synchronizeId);
 
-        writeCommandToSocket(QVariant::fromValue(synchronizeCommand), m_firstSocket.data(), m_writeCommandCounter);
+        writeCommandToIODecive(QVariant::fromValue(synchronizeCommand), m_firstSocket.data(), m_writeCommandCounter);
         m_writeCommandCounter++;
 
         while (m_firstSocket->waitForReadyRead(100)) {
@@ -375,6 +390,14 @@ void NodeInstanceServerProxy::processFinished(int /*exitCode*/, QProcess::ExitSt
 {
     qDebug() << "Process finished:" << sender();
 
+    if (m_captureFileForTest.isOpen()) {
+        m_captureFileForTest.close();
+        m_captureFileForTest.remove();
+        QMessageBox::warning(0, tr("Qml Puppet crashes"), tr("Your are recording a Puppet stream and the puppet crashes. "
+                                                             "It is recommended to reopen the Qml Designer and start again."));
+    }
+
+
     writeCommand(QVariant::fromValue(EndPuppetCommand()));
 
     if (m_firstSocket)
diff --git a/src/plugins/qmldesigner/designercore/instances/nodeinstanceserverproxy.h b/src/plugins/qmldesigner/designercore/instances/nodeinstanceserverproxy.h
index d68ff85aac..a8f0b44ff0 100644
--- a/src/plugins/qmldesigner/designercore/instances/nodeinstanceserverproxy.h
+++ b/src/plugins/qmldesigner/designercore/instances/nodeinstanceserverproxy.h
@@ -34,6 +34,7 @@
 
 #include <QWeakPointer>
 #include <QProcess>
+#include <QFile>
 
 QT_BEGIN_NAMESPACE
 class QLocalServer;
@@ -75,6 +76,8 @@ protected:
     void dispatchCommand(const QVariant &command);
     NodeInstanceClientInterface *nodeInstanceClient() const;
     QString missingQmlPuppetErrorMessage(const QString &applicationPath) const;
+    QString qmlPuppetApplicationName() const;
+    QString macOSBundlePath(const QString &path) const;
 
 signals:
     void processCrashed();
@@ -86,9 +89,7 @@ private slots:
     void readThirdDataStream();
 
 private:
-    QString qmlPuppetApplicationName() const;
-    QString macOSBundlePath(const QString &path) const;
-
+    QFile m_captureFileForTest;
     QWeakPointer<QLocalServer> m_localServer;
     QWeakPointer<QLocalSocket> m_firstSocket;
     QWeakPointer<QLocalSocket> m_secondSocket;
diff --git a/src/plugins/qmldesigner/qmldesignerplugin.cpp b/src/plugins/qmldesigner/qmldesignerplugin.cpp
index 94bc843fe2..c03a851c95 100644
--- a/src/plugins/qmldesigner/qmldesignerplugin.cpp
+++ b/src/plugins/qmldesigner/qmldesignerplugin.cpp
@@ -41,6 +41,7 @@
 #include <coreplugin/designmode.h>
 #include <coreplugin/icore.h>
 #include <coreplugin/modemanager.h>
+#include <extensionsystem/pluginspec.h>
 
 #include <projectexplorer/projectexplorerconstants.h>
 
-- 
GitLab