1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the assistant application of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qassistantclient.h"
31
#include <qtcpsocket.h>
32
#include <qtextstream.h>
34
#include <qfileinfo.h>
37
class QAssistantClientPrivate
39
friend class QAssistantClient;
40
QStringList arguments;
43
static QMap<const QAssistantClient*,QAssistantClientPrivate*> *dpointers = 0;
45
static QAssistantClientPrivate *data( const QAssistantClient *client, bool create=false )
48
dpointers = new QMap<const QAssistantClient*,QAssistantClientPrivate*>;
49
QAssistantClientPrivate *d = (*dpointers)[client];
51
d = new QAssistantClientPrivate;
52
dpointers->insert( client, d );
58
\class QAssistantClient
59
\brief The QAssistantClient class provides a means of using Qt
60
Assistant as an application's help tool.
64
Using Qt Assistant is simple: create a QAssistantClient instance,
65
then call showPage() as often as necessary to show your help
66
pages. When you call showPage(), Qt Assistant will be launched if
67
it isn't already running.
69
The QAssistantClient instance can open (openAssistant()) or close
70
(closeAssistant()) Qt Assistant whenever required. If Qt Assistant
71
is open, isOpen() returns true.
73
One QAssistantClient instance interacts with one Qt Assistant
74
instance, so every time you call openAssistant(), showPage() or
75
closeAssistant() they are applied to the particular Qt Assistant
76
instance associated with the QAssistantClient.
78
When you call openAssistant() the assistantOpened() signal is
79
emitted. Similarly when closeAssistant() is called,
80
assistantClosed() is emitted. In either case, if an error occurs,
83
This class is not included in the Qt library itself. To use it you
84
must link against \c libqassistantclient.a (Unix) or \c
85
qassistantclient.lib (Windows), which is built into \c INSTALL/lib
86
if you built the Qt tools (\c INSTALL is the directory where Qt is
87
installed). If you use qmake, then you can simply add the following
88
line to your pro file:
91
LIBS += -lqassistantclient
94
See also "Adding Documentation to Qt Assistant" in the \link
95
assistant.book Qt Assistant manual\endlink.
99
\fn void QAssistantClient::assistantOpened()
101
This signal is emitted when Qt Assistant is open and the
102
client-server communication is set up.
106
\fn void QAssistantClient::assistantClosed()
108
This signal is emitted when the connection to Qt Assistant is
109
closed. This happens when the user exits Qt Assistant, if an
110
error in the server or client occurs, or if closeAssistant() is
115
\fn void QAssistantClient::error( const QString &message )
117
This signal is emitted if Qt Assistant cannot be started, or if an
118
error occurs during the initialization of the connection between
119
Qt Assistant and the calling application. The \a message provides an
120
explanation of the error.
124
Constructs an assistant client object. The \a path specifies the
125
path to the Qt Assistant executable. If \a path is an empty
126
string the system path (\c{%PATH%} or \c $PATH) is used.
128
The assistant client object is a child of \a parent and is called
131
QAssistantClient::QAssistantClient( const QString &path, QObject *parent )
132
: QObject( parent ), host ( "localhost" )
134
if ( path.isEmpty() )
135
assistantCommand = "assistant";
137
QFileInfo fi( path );
139
assistantCommand = path + "/assistant";
141
assistantCommand = path;
144
#if defined(Q_OS_MAC)
145
assistantCommand += ".app/Contents/MacOS/assistant";
148
socket = new QTcpSocket( this );
149
connect( socket, SIGNAL(connected()),
150
SLOT(socketConnected()) );
151
connect( socket, SIGNAL(disconnected()),
152
SLOT(socketConnectionClosed()) );
153
connect( socket, SIGNAL(error(SocketError)),
154
SLOT(socketError(SocketError)) );
156
proc = new QProcess( this );
159
connect( proc, SIGNAL(readyReadStandardError()),
160
this, SLOT(readStdError()) );
164
Destroys the assistant client object.
166
QAssistantClient::~QAssistantClient()
168
if ( proc->state() == QProcess::Running )
172
QAssistantClientPrivate *d = (*dpointers)[ this ];
174
dpointers->remove(this);
176
if( dpointers->isEmpty() ) {
185
This function opens Qt Assistant, and sets up the client-server
186
communiction between the application and Qt Assistant. If it is
187
already open, this function does nothing. If an error occurs,
190
\sa assistantOpened()
192
void QAssistantClient::openAssistant()
194
if ( proc->state() == QProcess::Running )
198
args.append("-server");
199
if( !pageBuffer.isEmpty() ) {
200
args.append( "-file" );
201
args.append( pageBuffer );
204
QAssistantClientPrivate *d = data( this );
206
QStringList::ConstIterator it = d->arguments.begin();
207
while( it!=d->arguments.end() ) {
213
connect( proc, SIGNAL(readyReadStandardOutput()),
214
this, SLOT(readPort()) );
215
connect( proc, SIGNAL(error(ProcessError)),
216
this, SLOT(procError(ProcessError)) );
218
proc->start(assistantCommand, args);
221
void QAssistantClient::procError(QProcess::ProcessError err)
225
case QProcess::FailedToStart:
226
emit error( tr( "Failed to start Qt Assistant." ) );
228
case QProcess::Crashed:
229
emit error( tr( "Qt Assistant crashed." ) );
232
emit error( tr( "Error while running Qt Assistant." ) );
236
void QAssistantClient::readPort()
238
QString p = proc->readAllStandardOutput();
239
quint16 port = p.toUShort();
241
emit error( tr( "Cannot connect to Qt Assistant." ) );
244
socket->connectToHost( host, port );
245
disconnect( proc, SIGNAL(readyReadStandardOutput()),
246
this, SLOT(readPort()) );
250
Use this function to close Qt Assistant.
252
\sa assistantClosed()
254
void QAssistantClient::closeAssistant()
262
Call this function to make Qt Assistant show a particular \a page.
263
The \a page is a filename (e.g. \c myhelpfile.html). See "Adding
264
Documentation to Qt Assistant" in the \link assistant.book Qt
265
Assistant manual\endlink for further information.
267
If Qt Assistant hasn't been \link openAssistant() opened\endlink
268
yet, this function will do nothing. You can use isOpen() to
269
determine whether Qt Assistant is up and running, or you can
270
connect to the asssistantOpened() signal.
272
\sa isOpen(), assistantOpened()
274
void QAssistantClient::showPage( const QString &page )
282
QTextStream os( socket );
287
\property QAssistantClient::open
288
\brief whether Qt Assistant is open
291
bool QAssistantClient::isOpen() const
296
void QAssistantClient::socketConnected()
299
if ( !pageBuffer.isEmpty() )
300
showPage( pageBuffer );
301
emit assistantOpened();
304
void QAssistantClient::socketConnectionClosed()
307
emit assistantClosed();
310
void QAssistantClient::socketError(QTcpSocket::SocketError err)
312
if (err == QTcpSocket::ConnectionRefusedError)
313
emit error( tr( "Could not connect to Assistant: Connection refused" ) );
314
else if (err == QTcpSocket::HostNotFoundError)
315
emit error( tr( "Could not connect to Assistant: Host not found" ) );
317
emit error( tr( "Communication error" ) );
320
void QAssistantClient::readStdError()
322
QString errmsg = proc->readAllStandardError();
324
if (!errmsg.isEmpty())
325
emit error( errmsg.simplified() );
329
\fn void QAssistantClient::setArguments(const QStringList &arguments)
331
Sets the command line \a arguments used when Qt Assistant is
334
void QAssistantClient::setArguments( const QStringList &args )
336
QAssistantClientPrivate *d = data( this, true );