1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Assistant of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include "remotecontrol.h"
42
43 #include "centralwidget.h"
44 #include "helpenginewrapper.h"
45 #include "mainwindow.h"
46 #include "openpagesmanager.h"
47 #include "tracer.h"
48
49 #include <QtCore/QFile>
50 #include <QtCore/QFileInfo>
51 #include <QtCore/QFileSystemWatcher>
52 #include <QtCore/QThread>
53 #include <QtCore/QTextStream>
54 #include <QtCore/QSocketNotifier>
55
56 #include <QtGui/QMessageBox>
57 #include <QtGui/QApplication>
58
59 #include <QtHelp/QHelpEngine>
60 #include <QtHelp/QHelpIndexWidget>
61 #include <QtHelp/QHelpSearchQueryWidget>
62
63 #ifdef Q_OS_WIN
64 # include "remotecontrol_win.h"
65 #endif
66
67 QT_BEGIN_NAMESPACE
68
69 #ifdef Q_OS_WIN
70
StdInListenerWin(QObject * parent)71 StdInListenerWin::StdInListenerWin(QObject *parent)
72 : QThread(parent)
73 {
74 TRACE_OBJ
75 }
76
~StdInListenerWin()77 StdInListenerWin::~StdInListenerWin()
78 {
79 TRACE_OBJ
80 terminate();
81 wait();
82 }
83
run()84 void StdInListenerWin::run()
85 {
86 TRACE_OBJ
87 bool ok = true;
88 char chBuf[4096];
89 DWORD dwRead;
90
91 #ifndef Q_WS_WINCE
92 HANDLE hStdin, hStdinDup;
93
94 hStdin = GetStdHandle(STD_INPUT_HANDLE);
95 if (hStdin == INVALID_HANDLE_VALUE)
96 return;
97
98 DuplicateHandle(GetCurrentProcess(), hStdin,
99 GetCurrentProcess(), &hStdinDup,
100 0, false, DUPLICATE_SAME_ACCESS);
101
102 CloseHandle(hStdin);
103 #else
104 HANDLE hStdinDup;
105 hStdinDup = stdin;
106 #endif
107
108 while (ok) {
109 ok = ReadFile(hStdinDup, chBuf, sizeof(chBuf), &dwRead, NULL);
110 if (ok && dwRead != 0)
111 emit receivedCommand(QString::fromLocal8Bit(chBuf, dwRead));
112 }
113 }
114 #endif
115
RemoteControl(MainWindow * mainWindow)116 RemoteControl::RemoteControl(MainWindow *mainWindow)
117 : QObject(mainWindow)
118 , m_mainWindow(mainWindow)
119 , m_debug(false)
120 , m_caching(true)
121 , m_syncContents(false)
122 , m_expandTOC(-2)
123 , helpEngine(HelpEngineWrapper::instance())
124
125 {
126 TRACE_OBJ
127 connect(m_mainWindow, SIGNAL(initDone()), this, SLOT(applyCache()));
128 #ifdef Q_OS_WIN
129 StdInListenerWin *l = new StdInListenerWin(this);
130 connect(l, SIGNAL(receivedCommand(QString)),
131 this, SLOT(handleCommandString(QString)));
132 l->start();
133 #else
134 QSocketNotifier *notifier = new QSocketNotifier(fileno(stdin),
135 QSocketNotifier::Read, this);
136 connect(notifier, SIGNAL(activated(int)), this, SLOT(receivedData()));
137 notifier->setEnabled(true);
138 #endif
139 }
140
receivedData()141 void RemoteControl::receivedData()
142 {
143 TRACE_OBJ
144 QByteArray ba;
145 while (true) {
146 char c = getc(stdin);
147 if (c == EOF || c == '\0')
148 break;
149 if (c)
150 ba.append(c);
151 if (c == '\n')
152 break;
153 }
154 handleCommandString(QString::fromLocal8Bit(ba));
155 }
156
handleCommandString(const QString & cmdString)157 void RemoteControl::handleCommandString(const QString &cmdString)
158 {
159 TRACE_OBJ
160 QStringList cmds = cmdString.split(QLatin1Char(';'));
161 QStringList::const_iterator it = cmds.constBegin();
162 while (it != cmds.constEnd()) {
163 QString cmd, arg;
164 splitInputString(*it, cmd, arg);
165
166 if (m_debug)
167 QMessageBox::information(0, tr("Debugging Remote Control"),
168 tr("Received Command: %1 %2").arg(cmd).arg(arg));
169
170 if (cmd == QLatin1String("debug"))
171 handleDebugCommand(arg);
172 else if (cmd == QLatin1String("show"))
173 handleShowOrHideCommand(arg, true);
174 else if (cmd == QLatin1String("hide"))
175 handleShowOrHideCommand(arg, false);
176 else if (cmd == QLatin1String("setsource"))
177 handleSetSourceCommand(arg);
178 else if (cmd == QLatin1String("synccontents"))
179 handleSyncContentsCommand();
180 else if (cmd == QLatin1String("activatekeyword"))
181 handleActivateKeywordCommand(arg);
182 else if (cmd == QLatin1String("activateidentifier"))
183 handleActivateIdentifierCommand(arg);
184 else if (cmd == QLatin1String("expandtoc"))
185 handleExpandTocCommand(arg);
186 else if (cmd == QLatin1String("setcurrentfilter"))
187 handleSetCurrentFilterCommand(arg);
188 else if (cmd == QLatin1String("register"))
189 handleRegisterCommand(arg);
190 else if (cmd == QLatin1String("unregister"))
191 handleUnregisterCommand(arg);
192 else
193 break;
194
195 ++it;
196 }
197 m_mainWindow->raise();
198 m_mainWindow->activateWindow();
199 }
200
splitInputString(const QString & input,QString & cmd,QString & arg)201 void RemoteControl::splitInputString(const QString &input, QString &cmd,
202 QString &arg)
203 {
204 TRACE_OBJ
205 QString cmdLine = input.trimmed();
206 int i = cmdLine.indexOf(QLatin1Char(' '));
207 cmd = cmdLine.left(i);
208 arg = cmdLine.mid(i+1);
209 cmd = cmd.toLower();
210 }
211
handleDebugCommand(const QString & arg)212 void RemoteControl::handleDebugCommand(const QString &arg)
213 {
214 TRACE_OBJ
215 m_debug = arg == QLatin1String("on");
216 }
217
handleShowOrHideCommand(const QString & arg,bool show)218 void RemoteControl::handleShowOrHideCommand(const QString &arg, bool show)
219 {
220 TRACE_OBJ
221 if (arg.toLower() == QLatin1String("contents"))
222 m_mainWindow->setContentsVisible(show);
223 else if (arg.toLower() == QLatin1String("index"))
224 m_mainWindow->setIndexVisible(show);
225 else if (arg.toLower() == QLatin1String("bookmarks"))
226 m_mainWindow->setBookmarksVisible(show);
227 else if (arg.toLower() == QLatin1String("search"))
228 m_mainWindow->setSearchVisible(show);
229 }
230
handleSetSourceCommand(const QString & arg)231 void RemoteControl::handleSetSourceCommand(const QString &arg)
232 {
233 TRACE_OBJ
234 QUrl url(arg);
235 if (url.isValid()) {
236 if (url.isRelative())
237 url = CentralWidget::instance()->currentSource().resolved(url);
238 if (m_caching) {
239 clearCache();
240 m_setSource = url;
241 } else {
242 CentralWidget::instance()->setSource(url);
243 }
244 }
245 }
246
handleSyncContentsCommand()247 void RemoteControl::handleSyncContentsCommand()
248 {
249 TRACE_OBJ
250 if (m_caching)
251 m_syncContents = true;
252 else
253 m_mainWindow->syncContents();
254 }
255
handleActivateKeywordCommand(const QString & arg)256 void RemoteControl::handleActivateKeywordCommand(const QString &arg)
257 {
258 TRACE_OBJ
259 if (m_caching) {
260 clearCache();
261 m_activateKeyword = arg;
262 } else {
263 m_mainWindow->setIndexString(arg);
264 if (!arg.isEmpty()) {
265 if (!helpEngine.indexWidget()->currentIndex().isValid()
266 && helpEngine.fullTextSearchFallbackEnabled()) {
267 if (QHelpSearchEngine *se = helpEngine.searchEngine()) {
268 m_mainWindow->setSearchVisible(true);
269 if (QHelpSearchQueryWidget *w = se->queryWidget()) {
270 w->collapseExtendedSearch();
271 QList<QHelpSearchQuery> queryList;
272 queryList << QHelpSearchQuery(QHelpSearchQuery::DEFAULT,
273 QStringList(arg));
274 w->setQuery(queryList);
275 se->search(queryList);
276 }
277 }
278 } else {
279 m_mainWindow->setIndexVisible(true);
280 helpEngine.indexWidget()->activateCurrentItem();
281 }
282 }
283 }
284 }
285
handleActivateIdentifierCommand(const QString & arg)286 void RemoteControl::handleActivateIdentifierCommand(const QString &arg)
287 {
288 TRACE_OBJ
289 if (m_caching) {
290 clearCache();
291 m_activateIdentifier = arg;
292 } else {
293 const QMap<QString, QUrl> &links = helpEngine.linksForIdentifier(arg);
294 if (!links.isEmpty())
295 CentralWidget::instance()->setSource(links.constBegin().value());
296 }
297 }
298
handleExpandTocCommand(const QString & arg)299 void RemoteControl::handleExpandTocCommand(const QString &arg)
300 {
301 TRACE_OBJ
302 bool ok = false;
303 int depth = -2;
304 if (!arg.isEmpty())
305 depth = arg.toInt(&ok);
306 if (!ok || depth < -2)
307 depth = -2;
308
309 if (m_caching)
310 m_expandTOC = depth;
311 else if (depth != -2)
312 m_mainWindow->expandTOC(depth);
313 }
314
handleSetCurrentFilterCommand(const QString & arg)315 void RemoteControl::handleSetCurrentFilterCommand(const QString &arg)
316 {
317 TRACE_OBJ
318 if (helpEngine.customFilters().contains(arg)) {
319 if (m_caching) {
320 clearCache();
321 m_currentFilter = arg;
322 } else {
323 helpEngine.setCurrentFilter(arg);
324 }
325 }
326 }
327
handleRegisterCommand(const QString & arg)328 void RemoteControl::handleRegisterCommand(const QString &arg)
329 {
330 TRACE_OBJ
331 const QString &absFileName = QFileInfo(arg).absoluteFilePath();
332 if (helpEngine.registeredDocumentations().
333 contains(QHelpEngineCore::namespaceName(absFileName)))
334 return;
335 if (helpEngine.registerDocumentation(absFileName))
336 helpEngine.setupData();
337 }
338
handleUnregisterCommand(const QString & arg)339 void RemoteControl::handleUnregisterCommand(const QString &arg)
340 {
341 TRACE_OBJ
342 const QString &absFileName = QFileInfo(arg).absoluteFilePath();
343 const QString &ns = QHelpEngineCore::namespaceName(absFileName);
344 if (helpEngine.registeredDocumentations().contains(ns)) {
345 OpenPagesManager::instance()->closePages(ns);
346 if (helpEngine.unregisterDocumentation(ns))
347 helpEngine.setupData();
348 }
349 }
350
applyCache()351 void RemoteControl::applyCache()
352 {
353 TRACE_OBJ
354 if (m_setSource.isValid()) {
355 CentralWidget::instance()->setSource(m_setSource);
356 } else if (!m_activateKeyword.isEmpty()) {
357 m_mainWindow->setIndexString(m_activateKeyword);
358 helpEngine.indexWidget()->activateCurrentItem();
359 } else if (!m_activateIdentifier.isEmpty()) {
360 QMap<QString, QUrl> links =
361 helpEngine.linksForIdentifier(m_activateIdentifier);
362 if (!links.isEmpty())
363 CentralWidget::instance()->setSource(links.constBegin().value());
364 } else if (!m_currentFilter.isEmpty()) {
365 helpEngine.setCurrentFilter(m_currentFilter);
366 }
367
368 if (m_syncContents)
369 m_mainWindow->syncContents();
370
371 Q_ASSERT(m_expandTOC >= -2);
372 if (m_expandTOC != -2)
373 m_mainWindow->expandTOC(m_expandTOC);
374
375 m_caching = false;
376 }
377
clearCache()378 void RemoteControl::clearCache()
379 {
380 TRACE_OBJ
381 m_currentFilter.clear();
382 m_setSource.clear();
383 m_syncContents = false;
384 m_activateKeyword.clear();
385 m_activateIdentifier.clear();
386 }
387
388 QT_END_NAMESPACE
389