| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). |
| 4 |
** Contact: http://www.qt-project.org/legal |
| 5 |
** |
| 6 |
** This file is part of the Qt Solutions component. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:BSD$ |
| 9 |
** You may use this file under the terms of the BSD license as follows: |
| 10 |
** |
| 11 |
** "Redistribution and use in source and binary forms, with or without |
| 12 |
** modification, are permitted provided that the following conditions are |
| 13 |
** met: |
| 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 |
| 19 |
** distribution. |
| 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. |
| 23 |
** |
| 24 |
** |
| 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." |
| 36 |
** |
| 37 |
** $QT_END_LICENSE$ |
| 38 |
** |
| 39 |
****************************************************************************/ |
| 40 |
|
| 41 |
|
| 42 |
#include "qtsinglecoreapplication.h" |
| 43 |
#include "qtlocalpeer.h" |
| 44 |
|
| 45 |
/*! |
| 46 |
\class QtSingleCoreApplication qtsinglecoreapplication.h |
| 47 |
\brief A variant of the QtSingleApplication class for non-GUI applications. |
| 48 |
|
| 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 |
| 52 |
the QtGui library. |
| 53 |
|
| 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. |
| 58 |
|
| 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. |
| 63 |
|
| 64 |
\sa QtSingleApplication |
| 65 |
*/ |
| 66 |
|
| 67 |
/*! |
| 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. |
| 71 |
*/ |
| 72 |
|
| 73 |
QtSingleCoreApplication::QtSingleCoreApplication(int &argc, char **argv) |
| 74 |
: QCoreApplication(argc, argv) |
| 75 |
{ |
| 76 |
peer = new QtLocalPeer(this); |
| 77 |
connect( peer, &QtLocalPeer::messageReceived, this, &QtSingleCoreApplication::messageReceived ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
/*! |
| 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. |
| 85 |
*/ |
| 86 |
QtSingleCoreApplication::QtSingleCoreApplication(const QString &appId, int &argc, char **argv) |
| 87 |
: QCoreApplication(argc, argv) |
| 88 |
{ |
| 89 |
peer = new QtLocalPeer(this, appId); |
| 90 |
connect( peer, &QtLocalPeer::messageReceived, this, &QtSingleCoreApplication::messageReceived ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/*! |
| 95 |
Returns true if another instance of this application is running; |
| 96 |
otherwise false. |
| 97 |
|
| 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 |
| 100 |
another session). |
| 101 |
|
| 102 |
\sa sendMessage() |
| 103 |
*/ |
| 104 |
|
| 105 |
bool QtSingleCoreApplication::isRunning() |
| 106 |
{ |
| 107 |
return peer->isClient(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
/*! |
| 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 |
| 115 |
message. |
| 116 |
|
| 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. |
| 121 |
|
| 122 |
\sa isRunning(), messageReceived() |
| 123 |
*/ |
| 124 |
|
| 125 |
bool QtSingleCoreApplication::sendMessage(const QString &message, int timeout) |
| 126 |
{ |
| 127 |
return peer->sendMessage(message, timeout); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/*! |
| 132 |
Returns the application identifier. Two processes with the same |
| 133 |
identifier will be regarded as instances of the same application. |
| 134 |
*/ |
| 135 |
|
| 136 |
QString QtSingleCoreApplication::id() const |
| 137 |
{ |
| 138 |
return peer->applicationId(); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/*! |
| 143 |
\fn void QtSingleCoreApplication::messageReceived(const QString& message) |
| 144 |
|
| 145 |
This signal is emitted when the current instance receives a \a |
| 146 |
message from another instance of this application. |
| 147 |
|
| 148 |
\sa sendMessage() |
| 149 |
*/ |