| 1 | /* |
| 2 | * This file is part of libAuthentication |
| 3 | * |
| 4 | * Copyright (C) 2017-2020 Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public License |
| 8 | * version 2.1 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 18 | * 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include "qml-loopback-server.h" |
| 22 | |
| 23 | #include <QDebug> |
| 24 | |
| 25 | using namespace OnlineAccountsPlugin; |
| 26 | |
| 27 | namespace OnlineAccountsPlugin { |
| 28 | |
| 29 | class QmlLoopbackServerPrivate: public QObject |
| 30 | { |
| 31 | Q_OBJECT |
| 32 | Q_DECLARE_PUBLIC(QmlLoopbackServer) |
| 33 | |
| 34 | public: |
| 35 | QmlLoopbackServerPrivate(QmlLoopbackServer *q); |
| 36 | ~QmlLoopbackServerPrivate(); |
| 37 | |
| 38 | void queueUpdate(); |
| 39 | |
| 40 | private Q_SLOTS: |
| 41 | void update(); |
| 42 | |
| 43 | private: |
| 44 | bool m_updateQueued; |
| 45 | int m_port; |
| 46 | bool m_listening; |
| 47 | QmlLoopbackServer *q_ptr; |
| 48 | }; |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | QmlLoopbackServerPrivate::QmlLoopbackServerPrivate(QmlLoopbackServer *q): |
| 53 | m_port(0), |
| 54 | m_listening(true), |
| 55 | q_ptr(q) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | QmlLoopbackServerPrivate::~QmlLoopbackServerPrivate() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | void QmlLoopbackServerPrivate::queueUpdate() |
| 64 | { |
| 65 | if (!m_updateQueued) { |
| 66 | QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection); |
| 67 | m_updateQueued = true; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void QmlLoopbackServerPrivate::update() |
| 72 | { |
| 73 | Q_Q(QmlLoopbackServer); |
| 74 | |
| 75 | m_updateQueued = false; |
| 76 | |
| 77 | bool nothingToDo = m_listening == q->isListening(); |
| 78 | if (nothingToDo) return; |
| 79 | |
| 80 | if (m_listening) { |
| 81 | if (q->listen(uint16_t(m_port))) { |
| 82 | uint16_t realPort = q->port(); |
| 83 | if (realPort != m_port) { |
| 84 | Q_EMIT q->portChanged(); |
| 85 | } |
| 86 | } else { |
| 87 | qWarning() << "Listening failed"; |
| 88 | } |
| 89 | } else { |
| 90 | q->close(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | QmlLoopbackServer::QmlLoopbackServer(QObject *parent): |
| 95 | LoopbackServer(parent), |
| 96 | d_ptr(new QmlLoopbackServerPrivate(this)) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | QmlLoopbackServer::~QmlLoopbackServer() |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | void QmlLoopbackServer::setPort(int port) |
| 105 | { |
| 106 | Q_D(QmlLoopbackServer); |
| 107 | if (isListening()) { |
| 108 | qWarning() << "Cannot change port while already listening"; |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if (port == d->m_port) return; |
| 113 | d->m_port = port; |
| 114 | d->queueUpdate(); |
| 115 | Q_EMIT portChanged(); |
| 116 | } |
| 117 | |
| 118 | int QmlLoopbackServer::port() const |
| 119 | { |
| 120 | Q_D(const QmlLoopbackServer); |
| 121 | return isListening() ? LoopbackServer::port() : d->m_port; |
| 122 | } |
| 123 | |
| 124 | void QmlLoopbackServer::setListening(bool listening) |
| 125 | { |
| 126 | Q_D(QmlLoopbackServer); |
| 127 | |
| 128 | if (listening == d->m_listening) return; |
| 129 | d->m_listening = listening; |
| 130 | |
| 131 | /* The change notification will be emitted when doing the update */ |
| 132 | d->queueUpdate(); |
| 133 | } |
| 134 | |
| 135 | void QmlLoopbackServer::classBegin() |
| 136 | { |
| 137 | Q_D(QmlLoopbackServer); |
| 138 | d->m_updateQueued = true; |
| 139 | } |
| 140 | |
| 141 | void QmlLoopbackServer::componentComplete() |
| 142 | { |
| 143 | Q_D(QmlLoopbackServer); |
| 144 | d->update(); |
| 145 | } |
| 146 | |
| 147 | #include "qml-loopback-server.moc" |