Assistant: Extract StdInListener class from RemoteControl
authorAlexander Volkov <a.volkov@rusbitech.ru>
Fri, 24 Oct 2014 12:24:37 +0000 (16:24 +0400)
committerAlexander Volkov <a.volkov@rusbitech.ru>
Mon, 22 Dec 2014 15:59:37 +0000 (16:59 +0100)
Also rename StdInListenerWin to StdInListener and select
the needed version during compilation.

Change-Id: I4ef45feee71bae47ef3dfb63e91b7e3154886006
Reviewed-by: Karsten Heimrich <karsten.heimrich@theqtcompany.com>
Reviewed-by: Christian Kandeler <christian.kandeler@theqtcompany.com>
src/assistant/assistant/assistant.pro
src/assistant/assistant/remotecontrol.cpp
src/assistant/assistant/remotecontrol.h
src/assistant/assistant/stdinlistener.cpp [new file with mode: 0644]
src/assistant/assistant/stdinlistener.h [new file with mode: 0644]
src/assistant/assistant/stdinlistener_win.cpp [new file with mode: 0644]
src/assistant/assistant/stdinlistener_win.h [moved from src/assistant/assistant/remotecontrol_win.h with 91% similarity]

index 5f96377..6e82b07 100644 (file)
@@ -41,7 +41,6 @@ HEADERS += aboutdialog.h \
     openpageswidget.h \
     openpagesmanager.h \
     openpagesswitcher.h
-win32:HEADERS += remotecontrol_win.h
 
 SOURCES += aboutdialog.cpp \
     bookmarkdialog.cpp \
@@ -78,6 +77,14 @@ qtHaveModule(webkitwidgets):!contains(QT_CONFIG, static) {
     SOURCES += helpviewer_qtb.cpp
 }
 
+win32 {
+    HEADERS += stdinlistener_win.h
+    SOURCES += stdinlistener_win.cpp
+} else {
+    HEADERS += stdinlistener.h
+    SOURCES += stdinlistener.cpp
+}
+
 FORMS += bookmarkdialog.ui \
     bookmarkmanagerwidget.ui \
     bookmarkwidget.ui \
index c5bf7dd..7758e10 100644 (file)
@@ -41,9 +41,7 @@
 #include <QtCore/QFile>
 #include <QtCore/QFileInfo>
 #include <QtCore/QFileSystemWatcher>
-#include <QtCore/QThread>
 #include <QtCore/QTextStream>
-#include <QtCore/QSocketNotifier>
 
 #include <QtWidgets/QMessageBox>
 #include <QtWidgets/QApplication>
 #include <QtHelp/QHelpSearchQueryWidget>
 
 #ifdef Q_OS_WIN
-#   include "remotecontrol_win.h"
-#endif
-
-QT_BEGIN_NAMESPACE
-
-#ifdef Q_OS_WIN
-
-StdInListenerWin::StdInListenerWin(QObject *parent)
-    : QThread(parent)
-{
-    TRACE_OBJ
-}
-
-StdInListenerWin::~StdInListenerWin()
-{
-    TRACE_OBJ
-    terminate();
-    wait();
-}
-
-void StdInListenerWin::run()
-{
-    TRACE_OBJ
-    bool ok = true;
-    char chBuf[4096];
-    DWORD dwRead;
-
-#ifndef Q_OS_WINCE
-    HANDLE hStdin, hStdinDup;
-
-    hStdin = GetStdHandle(STD_INPUT_HANDLE);
-    if (hStdin == INVALID_HANDLE_VALUE)
-        return;
-
-    DuplicateHandle(GetCurrentProcess(), hStdin,
-        GetCurrentProcess(), &hStdinDup,
-        0, false, DUPLICATE_SAME_ACCESS);
-
-    CloseHandle(hStdin);
+#   include "stdinlistener_win.h"
 #else
-    HANDLE hStdinDup;
-    hStdinDup = stdin;
+#   include "stdinlistener.h"
 #endif
 
-    while (ok) {
-        ok = ReadFile(hStdinDup, chBuf, sizeof(chBuf), &dwRead, NULL);
-        if (ok && dwRead != 0)
-            emit receivedCommand(QString::fromLocal8Bit(chBuf, dwRead));
-    }
-}
-#endif
+QT_BEGIN_NAMESPACE
 
 RemoteControl::RemoteControl(MainWindow *mainWindow)
     : QObject(mainWindow)
@@ -117,33 +70,11 @@ RemoteControl::RemoteControl(MainWindow *mainWindow)
 {
     TRACE_OBJ
     connect(m_mainWindow, SIGNAL(initDone()), this, SLOT(applyCache()));
-#ifdef Q_OS_WIN
-    StdInListenerWin *l = new StdInListenerWin(this);
+
+    StdInListener *l = new StdInListener(this);
     connect(l, SIGNAL(receivedCommand(QString)),
         this, SLOT(handleCommandString(QString)));
     l->start();
-#else
-    QSocketNotifier *notifier = new QSocketNotifier(fileno(stdin),
-        QSocketNotifier::Read, this);
-    connect(notifier, SIGNAL(activated(int)), this, SLOT(receivedData()));
-    notifier->setEnabled(true);
-#endif
-}
-
-void RemoteControl::receivedData()
-{
-    TRACE_OBJ
-    QByteArray ba;
-    while (true) {
-        const int c = getc(stdin);
-        if (c == EOF || c == '\0')
-            break;
-        if (c)
-            ba.append(char(c));
-         if (c == '\n')
-             break;
-    }
-    handleCommandString(QString::fromLocal8Bit(ba));
 }
 
 void RemoteControl::handleCommandString(const QString &cmdString)
index 088a9dc..6602681 100644 (file)
@@ -51,7 +51,6 @@ public:
     RemoteControl(MainWindow *mainWindow);
 
 private slots:
-    void receivedData();
     void handleCommandString(const QString &cmdString);
     void applyCache();
 
diff --git a/src/assistant/assistant/stdinlistener.cpp b/src/assistant/assistant/stdinlistener.cpp
new file mode 100644 (file)
index 0000000..36cf8fb
--- /dev/null
@@ -0,0 +1,73 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Assistant of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** 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 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "stdinlistener.h"
+
+#include "tracer.h"
+
+QT_BEGIN_NAMESPACE
+
+StdInListener::StdInListener(QObject *parent)
+    : QSocketNotifier(fileno(stdin), QSocketNotifier::Read, parent)
+{
+    TRACE_OBJ
+    connect(this, SIGNAL(activated(int)), this, SLOT(receivedData()));
+}
+
+StdInListener::~StdInListener()
+{
+    TRACE_OBJ
+}
+
+void StdInListener::start()
+{
+    setEnabled(true);
+}
+
+void StdInListener::receivedData()
+{
+    TRACE_OBJ
+    QByteArray ba;
+    while (true) {
+        const int c = getc(stdin);
+        if (c == EOF || c == '\0')
+            break;
+        if (c)
+            ba.append(char(c));
+         if (c == '\n')
+             break;
+    }
+    emit receivedCommand(QString::fromLocal8Bit(ba));
+}
+
+QT_END_NAMESPACE
diff --git a/src/assistant/assistant/stdinlistener.h b/src/assistant/assistant/stdinlistener.h
new file mode 100644 (file)
index 0000000..6278b04
--- /dev/null
@@ -0,0 +1,61 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Assistant of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** 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 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef STDINLISTENER_H
+#define STDINLISTENER_H
+
+#include <QtCore/QSocketNotifier>
+
+QT_BEGIN_NAMESPACE
+
+class StdInListener : public QSocketNotifier
+{
+    Q_OBJECT
+
+public:
+    StdInListener(QObject *parent);
+    ~StdInListener();
+
+public slots:
+    void start();
+
+signals:
+    void receivedCommand(const QString &cmd);
+
+private slots:
+    void receivedData();
+};
+
+QT_END_NAMESPACE
+
+#endif
diff --git a/src/assistant/assistant/stdinlistener_win.cpp b/src/assistant/assistant/stdinlistener_win.cpp
new file mode 100644 (file)
index 0000000..9d80b60
--- /dev/null
@@ -0,0 +1,84 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the Qt Assistant of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL21$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** 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 or version 3 as published by the Free
+** Software Foundation and appearing in the file LICENSE.LGPLv21 and
+** LICENSE.LGPLv3 included in the packaging of this file. Please review the
+** following information to ensure the GNU Lesser General Public License
+** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "stdinlistener_win.h"
+
+#include "tracer.h"
+
+QT_BEGIN_NAMESPACE
+
+StdInListener::StdInListener(QObject *parent)
+    : QThread(parent)
+{
+    TRACE_OBJ
+}
+
+StdInListener::~StdInListener()
+{
+    TRACE_OBJ
+    terminate();
+    wait();
+}
+
+void StdInListener::run()
+{
+    TRACE_OBJ
+    bool ok = true;
+    char chBuf[4096];
+    DWORD dwRead;
+
+#ifndef Q_OS_WINCE
+    HANDLE hStdin, hStdinDup;
+
+    hStdin = GetStdHandle(STD_INPUT_HANDLE);
+    if (hStdin == INVALID_HANDLE_VALUE)
+        return;
+
+    DuplicateHandle(GetCurrentProcess(), hStdin,
+        GetCurrentProcess(), &hStdinDup,
+        0, false, DUPLICATE_SAME_ACCESS);
+
+    CloseHandle(hStdin);
+#else
+    HANDLE hStdinDup;
+    hStdinDup = stdin;
+#endif
+
+    while (ok) {
+        ok = ReadFile(hStdinDup, chBuf, sizeof(chBuf), &dwRead, NULL);
+        if (ok && dwRead != 0)
+            emit receivedCommand(QString::fromLocal8Bit(chBuf, dwRead));
+    }
+}
+
+QT_END_NAMESPACE
similarity index 91%
rename from src/assistant/assistant/remotecontrol_win.h
rename to src/assistant/assistant/stdinlistener_win.h
index 5a046a1..a740479 100644 (file)
 **
 ****************************************************************************/
 
-#ifndef REMOTECONTROL_WIN_H
-#define REMOTECONTROL_WIN_H
+#ifndef STDINLISTENER_WIN_H
+#define STDINLISTENER_WIN_H
 
 #include <windows.h>
 #include <QtCore/QThread>
 
 QT_BEGIN_NAMESPACE
 
-class StdInListenerWin : public QThread
+class StdInListener : public QThread
 {
     Q_OBJECT
 
 public:
-    StdInListenerWin(QObject *parent);
-    ~StdInListenerWin();
+    StdInListener(QObject *parent);
+    ~StdInListener();
 
 signals:
     void receivedCommand(const QString &cmd);