1 /***************************************************************************
2 * Copyright (C) 2005-2012 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <QCoreApplication>
24 #include "internalconnection.h"
27 class PeerMessageEvent : public QEvent
30 PeerMessageEvent(InternalConnection *sender, InternalConnection::EventType eventType, const T &message)
31 : QEvent(QEvent::Type(eventType)), sender(sender), message(message) {}
32 InternalConnection *sender;
37 InternalConnection::InternalConnection(QObject *parent)
38 : SignalProxy::AbstractPeer(parent),
47 InternalConnection::~InternalConnection()
54 QString InternalConnection::description() const
56 return tr("internal connection");
60 bool InternalConnection::isOpen() const
66 bool InternalConnection::isSecure() const
72 bool InternalConnection::isLocal() const
78 void InternalConnection::close(const QString &reason)
82 qWarning() << "closing not implemented!";
86 int InternalConnection::lag() const
92 void InternalConnection::setSignalProxy(SignalProxy *proxy)
94 if (!proxy && _proxy) {
103 if (proxy && !_proxy) {
108 qWarning() << Q_FUNC_INFO << "Changing the SignalProxy is not supported!";
112 void InternalConnection::setPeer(InternalConnection *peer)
115 qWarning() << Q_FUNC_INFO << "Peer already set, ignoring!";
119 connect(peer, SIGNAL(disconnected()), SLOT(peerDisconnected()));
123 void InternalConnection::peerDisconnected()
125 disconnect(_peer, 0, this, 0);
134 void InternalConnection::dispatch(const SignalProxy::SyncMessage &msg)
136 dispatch(SyncMessageEvent, msg);
140 void InternalConnection::dispatch(const SignalProxy::RpcCall &msg)
142 dispatch(RpcCallEvent, msg);
146 void InternalConnection::dispatch(const SignalProxy::InitRequest &msg)
148 dispatch(InitRequestEvent, msg);
152 void InternalConnection::dispatch(const SignalProxy::InitData &msg)
154 dispatch(InitDataEvent, msg);
159 void InternalConnection::dispatch(EventType eventType, const T &msg)
162 qWarning() << Q_FUNC_INFO << "Cannot dispatch a message without a peer!";
166 if(QThread::currentThread() == _peer->thread())
169 QCoreApplication::postEvent(_peer, new PeerMessageEvent<T>(this, eventType, msg));
174 void InternalConnection::handle(const T &msg)
177 qWarning() << Q_FUNC_INFO << "Cannot handle a message without having a signal proxy set!";
181 _proxy->handle(this, msg);
185 void InternalConnection::customEvent(QEvent *event)
187 switch ((int)event->type()) {
188 case SyncMessageEvent: {
189 PeerMessageEvent<SignalProxy::SyncMessage> *e = static_cast<PeerMessageEvent<SignalProxy::SyncMessage> *>(event);
194 PeerMessageEvent<SignalProxy::RpcCall> *e = static_cast<PeerMessageEvent<SignalProxy::RpcCall> *>(event);
198 case InitRequestEvent: {
199 PeerMessageEvent<SignalProxy::InitRequest> *e = static_cast<PeerMessageEvent<SignalProxy::InitRequest> *>(event);
203 case InitDataEvent: {
204 PeerMessageEvent<SignalProxy::InitData> *e = static_cast<PeerMessageEvent<SignalProxy::InitData> *>(event);
210 qWarning() << Q_FUNC_INFO << "Received unknown custom event:" << event->type();