From 58bd34016974dbbd0d99adfd497b9aab8ad273bf Mon Sep 17 00:00:00 2001 From: Alexander Volkov Date: Fri, 24 Oct 2014 16:24:37 +0400 Subject: [PATCH] Assistant: Extract StdInListener class from RemoteControl Also rename StdInListenerWin to StdInListener and select the needed version during compilation. Change-Id: I4ef45feee71bae47ef3dfb63e91b7e3154886006 Reviewed-by: Karsten Heimrich Reviewed-by: Christian Kandeler --- src/assistant/assistant/assistant.pro | 9 ++- src/assistant/assistant/remotecontrol.cpp | 79 ++------------------ src/assistant/assistant/remotecontrol.h | 1 - src/assistant/assistant/stdinlistener.cpp | 73 +++++++++++++++++++ src/assistant/assistant/stdinlistener.h | 61 ++++++++++++++++ src/assistant/assistant/stdinlistener_win.cpp | 84 ++++++++++++++++++++++ .../{remotecontrol_win.h => stdinlistener_win.h} | 10 +-- 7 files changed, 236 insertions(+), 81 deletions(-) create mode 100644 src/assistant/assistant/stdinlistener.cpp create mode 100644 src/assistant/assistant/stdinlistener.h create mode 100644 src/assistant/assistant/stdinlistener_win.cpp rename src/assistant/assistant/{remotecontrol_win.h => stdinlistener_win.h} (91%) diff --git a/src/assistant/assistant/assistant.pro b/src/assistant/assistant/assistant.pro index 5f96377..6e82b07 100644 --- a/src/assistant/assistant/assistant.pro +++ b/src/assistant/assistant/assistant.pro @@ -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 \ diff --git a/src/assistant/assistant/remotecontrol.cpp b/src/assistant/assistant/remotecontrol.cpp index c5bf7dd..7758e10 100644 --- a/src/assistant/assistant/remotecontrol.cpp +++ b/src/assistant/assistant/remotecontrol.cpp @@ -41,9 +41,7 @@ #include #include #include -#include #include -#include #include #include @@ -53,57 +51,12 @@ #include #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) diff --git a/src/assistant/assistant/remotecontrol.h b/src/assistant/assistant/remotecontrol.h index 088a9dc..6602681 100644 --- a/src/assistant/assistant/remotecontrol.h +++ b/src/assistant/assistant/remotecontrol.h @@ -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 index 0000000..36cf8fb --- /dev/null +++ b/src/assistant/assistant/stdinlistener.cpp @@ -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 index 0000000..6278b04 --- /dev/null +++ b/src/assistant/assistant/stdinlistener.h @@ -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 + +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 index 0000000..9d80b60 --- /dev/null +++ b/src/assistant/assistant/stdinlistener_win.cpp @@ -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 diff --git a/src/assistant/assistant/remotecontrol_win.h b/src/assistant/assistant/stdinlistener_win.h similarity index 91% rename from src/assistant/assistant/remotecontrol_win.h rename to src/assistant/assistant/stdinlistener_win.h index 5a046a1..a740479 100644 --- a/src/assistant/assistant/remotecontrol_win.h +++ b/src/assistant/assistant/stdinlistener_win.h @@ -31,21 +31,21 @@ ** ****************************************************************************/ -#ifndef REMOTECONTROL_WIN_H -#define REMOTECONTROL_WIN_H +#ifndef STDINLISTENER_WIN_H +#define STDINLISTENER_WIN_H #include #include 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); -- 2.7.4