| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2006-2008 fullmetalcoder <[email protected]> |
| 4 | ** |
| 5 | ** This file is part of the Edyuk project <http://edyuk.org> |
| 6 | ** |
| 7 | ** This file may be used under the terms of the GNU General Public License |
| 8 | ** version 2 as published by the Free Software Foundation and appearing in the |
| 9 | ** file GPL.txt included in the packaging of this file. |
| 10 | ** |
| 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | ** |
| 14 | ****************************************************************************/ |
| 15 | |
| 16 | #include "qsingleapplication.h" |
| 17 | |
| 18 | #include "qinterprocesschannel.h" |
| 19 | #include "qmanagedrequest.h" |
| 20 | |
| 21 | #include <QStringList> |
| 22 | |
| 23 | /*! |
| 24 | \brief Constructor |
| 25 | */ |
| 26 | QSingleApplication::QSingleApplication(int& argc, char** argv, bool useGui, |
| 27 | const QString& appName) |
| 28 | : QApplication(argc, argv, useGui) { |
| 29 | setApplicationName(appName); |
| 30 | |
| 31 | pChannel = new QInterProcessChannel(this); |
| 32 | |
| 33 | connect(pChannel, SIGNAL(message(QString)), this, SLOT(message(QString))); |
| 34 | |
| 35 | connect(pChannel, SIGNAL(request(QStringList)), this, |
| 36 | SLOT(request(QStringList))); |
| 37 | |
| 38 | setMessagingPolicy(Signals); |
| 39 | setInstanciationPolicy(None); |
| 40 | } |
| 41 | |
| 42 | /*! |
| 43 | \brief Destructor |
| 44 | */ |
| 45 | QSingleApplication::~QSingleApplication() { pChannel->close(); } |
| 46 | |
| 47 | /*! |
| 48 | \return Wheter the creation of an instance is allowed. |
| 49 | */ |
| 50 | bool QSingleApplication::isInstanceAllowed() const { |
| 51 | return pChannel->isServer(); |
| 52 | } |
| 53 | |
| 54 | /*! |
| 55 | \return The current messaging policy |
| 56 | */ |
| 57 | QSingleApplication::MessagingPolicy QSingleApplication::messagingPolicy() |
| 58 | const { |
| 59 | return m_messaging; |
| 60 | } |
| 61 | |
| 62 | /*! |
| 63 | \brief Sets the messaging policy |
| 64 | |
| 65 | \see messagingPolicy() |
| 66 | */ |
| 67 | void QSingleApplication::setMessagingPolicy(MessagingPolicy p) { |
| 68 | m_messaging = p; |
| 69 | } |
| 70 | |
| 71 | /*! |
| 72 | \return The current instanciation policy |
| 73 | */ |
| 74 | QSingleApplication::InstanciationPolicy |
| 75 | QSingleApplication::instanciationPolicy() const { |
| 76 | return m_instanciation; |
| 77 | } |
| 78 | |
| 79 | /*! |
| 80 | \brief Sets the instanciation policy |
| 81 | |
| 82 | \see instanciationPolicy() |
| 83 | */ |
| 84 | void QSingleApplication::setInstanciationPolicy(InstanciationPolicy p) { |
| 85 | m_instanciation = p; |
| 86 | } |
| 87 | |
| 88 | /*! |
| 89 | \brief Launchs the application if allowed |
| 90 | |
| 91 | \see instanciationPolicy() |
| 92 | */ |
| 93 | int QSingleApplication::exec() { |
| 94 | int ret = -1; |
| 95 | |
| 96 | if (isInstanceAllowed()) { |
| 97 | ret = QApplication::exec(); |
| 98 | |
| 99 | } else { |
| 100 | switch (instanciationPolicy()) { |
| 101 | case ForwardArguments: |
| 102 | qWarning("QSingleApplication : forwarding parameters"); |
| 103 | sendRequest(arguments()); |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | qWarning("QSingleApplication : all instances already occupied"); |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | /*! |
| 116 | \overload |
| 117 | */ |
| 118 | void QSingleApplication::sendRequest(const QString& s) { |
| 119 | sendRequest(QStringList(s)); |
| 120 | } |
| 121 | |
| 122 | /*! |
| 123 | \brief sends a message to the running instance of the application |
| 124 | |
| 125 | \param l A list of arguments that make up a request to be sent. |
| 126 | */ |
| 127 | void QSingleApplication::sendRequest(const QStringList& l) { |
| 128 | if (!pChannel || l.isEmpty()) return; |
| 129 | |
| 130 | /* |
| 131 | qDebug("sending message to %s:%i over TCP/IP : \"%s\"", |
| 132 | qPrintable(pManager->address().toString()), |
| 133 | pManager->port(), |
| 134 | msg.constData()); |
| 135 | */ |
| 136 | |
| 137 | pChannel->sendMessage(QManagedRequest::joinArguments("--request", l)); |
| 138 | } |
| 139 | |
| 140 | /*! |
| 141 | \internal |
| 142 | */ |
| 143 | void QSingleApplication::message(const QString& s) { |
| 144 | if (s.isEmpty()) return; |
| 145 | |
| 146 | switch (messagingPolicy()) { |
| 147 | case Events: |
| 148 | |
| 149 | /* |
| 150 | QRequestEvent *e = new QRequestEvent(s); |
| 151 | postEvent(s); |
| 152 | */ |
| 153 | |
| 154 | break; |
| 155 | |
| 156 | case Signals: |
| 157 | emit request(s); |
| 158 | break; |
| 159 | |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /*! |
| 166 | \internal |
| 167 | */ |
| 168 | void QSingleApplication::request(const QStringList& l) { |
| 169 | if (l.isEmpty()) return; |
| 170 | |
| 171 | switch (messagingPolicy()) { |
| 172 | case Events: |
| 173 | |
| 174 | /* |
| 175 | QRequestEvent *e = new QRequestEvent(s); |
| 176 | postEvent(s); |
| 177 | */ |
| 178 | |
| 179 | break; |
| 180 | |
| 181 | case Signals: |
| 182 | emit request(l.join(" | ")); |
| 183 | break; |
| 184 | |
| 185 | default: |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /*! |
| 191 | \internal |
| 192 | */ |
| 193 | bool QSingleApplication::event(QEvent* e) { return QApplication::event(e); } |