| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 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 https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "remotecontrol.h" |
| 52 | |
| 53 | #include <QDir> |
| 54 | #include <QLibraryInfo> |
| 55 | #include <QMessageBox> |
| 56 | #include <QProcess> |
| 57 | #include <QTextStream> |
| 58 | |
| 59 | RemoteControl::RemoteControl(QWidget *parent, Qt::WindowFlags flags) |
| 60 | : QMainWindow(parent, flags) |
| 61 | { |
| 62 | ui.setupUi(this); |
| 63 | connect(sender: ui.indexLineEdit, SIGNAL(returnPressed()), |
| 64 | receiver: this, SLOT(on_indexButton_clicked())); |
| 65 | connect(sender: ui.identifierLineEdit, SIGNAL(returnPressed()), |
| 66 | receiver: this, SLOT(on_identifierButton_clicked())); |
| 67 | connect(sender: ui.urlLineEdit, SIGNAL(returnPressed()), |
| 68 | receiver: this, SLOT(on_urlButton_clicked())); |
| 69 | |
| 70 | QString rc; |
| 71 | QTextStream(&rc) << QLatin1String("qthelp://org.qt-project.qtdoc." ) |
| 72 | << (QT_VERSION >> 16) << ((QT_VERSION >> 8) & 0xFF) |
| 73 | << (QT_VERSION & 0xFF) |
| 74 | << QLatin1String("/qtdoc/index.html" ); |
| 75 | |
| 76 | ui.startUrlLineEdit->setText(rc); |
| 77 | |
| 78 | process = new QProcess(this); |
| 79 | connect(sender: process, SIGNAL(finished(int,QProcess::ExitStatus)), |
| 80 | receiver: this, SLOT(helpViewerClosed())); |
| 81 | } |
| 82 | |
| 83 | RemoteControl::~RemoteControl() |
| 84 | { |
| 85 | if (process->state() == QProcess::Running) { |
| 86 | process->terminate(); |
| 87 | process->waitForFinished(msecs: 3000); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void RemoteControl::on_actionQuit_triggered() |
| 92 | { |
| 93 | close(); |
| 94 | } |
| 95 | |
| 96 | void RemoteControl::on_launchButton_clicked() |
| 97 | { |
| 98 | if (process->state() == QProcess::Running) |
| 99 | return; |
| 100 | |
| 101 | QString app = QLibraryInfo::location(QLibraryInfo::BinariesPath); |
| 102 | #if !defined(Q_OS_MAC) |
| 103 | app += QLatin1String("/assistant" ); |
| 104 | #else |
| 105 | app += QLatin1String("/Assistant.app/Contents/MacOS/Assistant" ); |
| 106 | #endif |
| 107 | |
| 108 | ui.contentsCheckBox->setChecked(true); |
| 109 | ui.indexCheckBox->setChecked(true); |
| 110 | ui.bookmarksCheckBox->setChecked(true); |
| 111 | |
| 112 | QStringList args; |
| 113 | args << QLatin1String("-enableRemoteControl" ); |
| 114 | process->start(program: app, arguments: args); |
| 115 | if (!process->waitForStarted()) { |
| 116 | QMessageBox::critical( |
| 117 | parent: this, title: tr(s: "Remote Control" ), |
| 118 | text: tr(s: "Could not start Qt Assistant from %1." ).arg(a: QDir::toNativeSeparators(pathName: app))); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | if (!ui.startUrlLineEdit->text().isEmpty()) |
| 123 | sendCommand(cmd: QLatin1String("SetSource " ) |
| 124 | + ui.startUrlLineEdit->text()); |
| 125 | |
| 126 | ui.launchButton->setEnabled(false); |
| 127 | ui.startUrlLineEdit->setEnabled(false); |
| 128 | ui.actionGroupBox->setEnabled(true); |
| 129 | } |
| 130 | |
| 131 | void RemoteControl::sendCommand(const QString &cmd) |
| 132 | { |
| 133 | if (process->state() != QProcess::Running) |
| 134 | return; |
| 135 | process->write(data: cmd.toLocal8Bit() + '\n'); |
| 136 | } |
| 137 | |
| 138 | void RemoteControl::on_indexButton_clicked() |
| 139 | { |
| 140 | sendCommand(cmd: QLatin1String("ActivateKeyword " ) |
| 141 | + ui.indexLineEdit->text()); |
| 142 | } |
| 143 | |
| 144 | void RemoteControl::on_identifierButton_clicked() |
| 145 | { |
| 146 | sendCommand(cmd: QLatin1String("ActivateIdentifier " ) |
| 147 | + ui.identifierLineEdit->text()); |
| 148 | } |
| 149 | |
| 150 | void RemoteControl::on_urlButton_clicked() |
| 151 | { |
| 152 | sendCommand(cmd: QLatin1String("SetSource " ) |
| 153 | + ui.urlLineEdit->text()); |
| 154 | } |
| 155 | |
| 156 | void RemoteControl::on_syncContentsButton_clicked() |
| 157 | { |
| 158 | sendCommand(cmd: QLatin1String("SyncContents" )); |
| 159 | } |
| 160 | |
| 161 | void RemoteControl::on_contentsCheckBox_toggled(bool checked) |
| 162 | { |
| 163 | sendCommand(cmd: checked ? |
| 164 | QLatin1String("Show Contents" ) : QLatin1String("Hide Contents" )); |
| 165 | } |
| 166 | |
| 167 | void RemoteControl::on_indexCheckBox_toggled(bool checked) |
| 168 | { |
| 169 | sendCommand(cmd: checked ? |
| 170 | QLatin1String("Show Index" ) : QLatin1String("Hide Index" )); |
| 171 | } |
| 172 | |
| 173 | void RemoteControl::on_bookmarksCheckBox_toggled(bool checked) |
| 174 | { |
| 175 | sendCommand(cmd: checked ? |
| 176 | QLatin1String("Show Bookmarks" ) : QLatin1String("Hide Bookmarks" )); |
| 177 | } |
| 178 | |
| 179 | void RemoteControl::helpViewerClosed() |
| 180 | { |
| 181 | ui.launchButton->setEnabled(true); |
| 182 | ui.startUrlLineEdit->setEnabled(true); |
| 183 | ui.actionGroupBox->setEnabled(false); |
| 184 | } |
| 185 | |