| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
| 4 | ** All rights reserved. |
| 5 | ** |
| 6 | ** Contact: Nokia Corporation (qt-info@nokia.com) |
| 7 | ** |
| 8 | ** This file is part of a Qt Solutions component. |
| 9 | ** |
| 10 | ** You may use this file under the terms of the BSD license as follows: |
| 11 | ** |
| 12 | ** "Redistribution and use in source and binary forms, with or without |
| 13 | ** modification, are permitted provided that the following conditions are |
| 14 | ** met: |
| 15 | ** * Redistributions of source code must retain the above copyright |
| 16 | ** notice, this list of conditions and the following disclaimer. |
| 17 | ** * Redistributions in binary form must reproduce the above copyright |
| 18 | ** notice, this list of conditions and the following disclaimer in |
| 19 | ** the documentation and/or other materials provided with the |
| 20 | ** distribution. |
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
| 22 | ** the names of its contributors may be used to endorse or promote |
| 23 | ** products derived from this software without specific prior written |
| 24 | ** permission. |
| 25 | ** |
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | |
| 41 | #include "qtsinglecoreapplication.h" |
| 42 | #include "qtlocalpeer.h" |
| 43 | |
| 44 | /*! |
| 45 | \class QtSingleCoreApplication qtsinglecoreapplication.h |
| 46 | \brief A variant of the QtSingleApplication class for non-GUI applications. |
| 47 | |
| 48 | This class is a variant of QtSingleApplication suited for use in |
| 49 | console (non-GUI) applications. It is an extension of |
| 50 | QCoreApplication (instead of QApplication). It does not require |
| 51 | the QtGui library. |
| 52 | |
| 53 | The API and usage is identical to QtSingleApplication, except that |
| 54 | functions relating to the "activation window" are not present, for |
| 55 | obvious reasons. Please refer to the QtSingleApplication |
| 56 | documentation for explanation of the usage. |
| 57 | |
| 58 | A QtSingleCoreApplication instance can communicate to a |
| 59 | QtSingleApplication instance if they share the same application |
| 60 | id. Hence, this class can be used to create a light-weight |
| 61 | command-line tool that sends commands to a GUI application. |
| 62 | |
| 63 | \sa QtSingleApplication |
| 64 | */ |
| 65 | |
| 66 | /*! |
| 67 | Creates a QtSingleCoreApplication object. The application identifier |
| 68 | will be QCoreApplication::applicationFilePath(). \a argc and \a |
| 69 | argv are passed on to the QCoreAppliation constructor. |
| 70 | */ |
| 71 | |
| 72 | QtSingleCoreApplication::QtSingleCoreApplication(int &argc, char **argv) |
| 73 | : QCoreApplication(argc, argv) |
| 74 | { |
| 75 | peer = new QtLocalPeer(this); |
| 76 | connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&))); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /*! |
| 81 | Creates a QtSingleCoreApplication object with the application |
| 82 | identifier \a appId. \a argc and \a argv are passed on to the |
| 83 | QCoreAppliation constructor. |
| 84 | */ |
| 85 | QtSingleCoreApplication::QtSingleCoreApplication(const QString &appId, int &argc, char **argv) |
| 86 | : QCoreApplication(argc, argv) |
| 87 | { |
| 88 | peer = new QtLocalPeer(this, appId); |
| 89 | connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&))); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /*! |
| 94 | Returns true if another instance of this application is running; |
| 95 | otherwise false. |
| 96 | |
| 97 | This function does not find instances of this application that are |
| 98 | being run by a different user (on Windows: that are running in |
| 99 | another session). |
| 100 | |
| 101 | \sa sendMessage() |
| 102 | */ |
| 103 | |
| 104 | bool QtSingleCoreApplication::isRunning() |
| 105 | { |
| 106 | return peer->isClient(); |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /*! |
| 111 | Tries to send the text \a message to the currently running |
| 112 | instance. The QtSingleCoreApplication object in the running instance |
| 113 | will emit the messageReceived() signal when it receives the |
| 114 | message. |
| 115 | |
| 116 | This function returns true if the message has been sent to, and |
| 117 | processed by, the current instance. If there is no instance |
| 118 | currently running, or if the running instance fails to process the |
| 119 | message within \a timeout milliseconds, this function return false. |
| 120 | |
| 121 | \sa isRunning(), messageReceived() |
| 122 | */ |
| 123 | |
| 124 | bool QtSingleCoreApplication::sendMessage(const QString &message, int timeout) |
| 125 | { |
| 126 | return peer->sendMessage(message, timeout); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /*! |
| 131 | Returns the application identifier. Two processes with the same |
| 132 | identifier will be regarded as instances of the same application. |
| 133 | */ |
| 134 | |
| 135 | QString QtSingleCoreApplication::id() const |
| 136 | { |
| 137 | return peer->applicationId(); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /*! |
| 142 | \fn void QtSingleCoreApplication::messageReceived(const QString& message) |
| 143 | |
| 144 | This signal is emitted when the current instance receives a \a |
| 145 | message from another instance of this application. |
| 146 | |
| 147 | \sa sendMessage() |
| 148 | */ |