1 /****************************************************************************
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the Qt Solutions component.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
42 #include "qtsinglecoreapplication.h"
43 #include "qtlocalpeer.h"
46 \class QtSingleCoreApplication qtsinglecoreapplication.h
47 \brief A variant of the QtSingleApplication class for non-GUI applications.
49 This class is a variant of QtSingleApplication suited for use in
50 console (non-GUI) applications. It is an extension of
51 QCoreApplication (instead of QApplication). It does not require
54 The API and usage is identical to QtSingleApplication, except that
55 functions relating to the "activation window" are not present, for
56 obvious reasons. Please refer to the QtSingleApplication
57 documentation for explanation of the usage.
59 A QtSingleCoreApplication instance can communicate to a
60 QtSingleApplication instance if they share the same application
61 id. Hence, this class can be used to create a light-weight
62 command-line tool that sends commands to a GUI application.
64 \sa QtSingleApplication
68 Creates a QtSingleCoreApplication object. The application identifier
69 will be QCoreApplication::applicationFilePath(). \a argc and \a
70 argv are passed on to the QCoreAppliation constructor.
73 QtSingleCoreApplication::QtSingleCoreApplication(int &argc, char **argv)
74 : QCoreApplication(argc, argv)
76 peer = new QtLocalPeer(this);
77 connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
82 Creates a QtSingleCoreApplication object with the application
83 identifier \a appId. \a argc and \a argv are passed on to the
84 QCoreAppliation constructor.
86 QtSingleCoreApplication::QtSingleCoreApplication(const QString &appId, int &argc, char **argv)
87 : QCoreApplication(argc, argv)
89 peer = new QtLocalPeer(this, appId);
90 connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
95 Returns true if another instance of this application is running;
98 This function does not find instances of this application that are
99 being run by a different user (on Windows: that are running in
105 bool QtSingleCoreApplication::isRunning()
107 return peer->isClient();
112 Tries to send the text \a message to the currently running
113 instance. The QtSingleCoreApplication object in the running instance
114 will emit the messageReceived() signal when it receives the
117 This function returns true if the message has been sent to, and
118 processed by, the current instance. If there is no instance
119 currently running, or if the running instance fails to process the
120 message within \a timeout milliseconds, this function return false.
122 \sa isRunning(), messageReceived()
125 bool QtSingleCoreApplication::sendMessage(const QString &message, int timeout)
127 return peer->sendMessage(message, timeout);
132 Returns the application identifier. Two processes with the same
133 identifier will be regarded as instances of the same application.
136 QString QtSingleCoreApplication::id() const
138 return peer->applicationId();
143 \fn void QtSingleCoreApplication::messageReceived(const QString& message)
145 This signal is emitted when the current instance receives a \a
146 message from another instance of this application.