1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel IRC Development Team *
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) any later version. *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "signalproxy.h"
25 SignalProxy::SignalProxy(ProxyType _type, QIODevice *dev, QObject *parent) : QObject(parent), type(_type) {
28 qWarning() << tr("Device given for ProxyType == Server, ignoring...").toAscii();
35 SignalProxy::~SignalProxy() {
36 //qDebug() << "peers:" << peers.count();
37 foreach(Connection conn, peers) {
38 conn.peer->deleteLater(); conn.device->deleteLater();
42 void SignalProxy::addPeer(QIODevice *dev) {
43 if(type == Client && peers.count()) {
44 qWarning() << tr("Cannot add more than one peer to a SignalProxy in client mode!").toAscii();
49 conn.peer = new QxtRPCPeer(dev, QxtRPCPeer::Peer, this);
50 connect(conn.peer, SIGNAL(peerDisconnected()), this, SLOT(socketDisconnected()));
52 foreach(SlotDesc slot, attachedSlots) {
53 conn.peer->attachSlot(slot.rpcFunction, slot.recv, slot.slot);
55 foreach(SignalDesc sig, attachedSignals) {
56 conn.peer->attachSignal(sig.sender, sig.signal, sig.rpcFunction);
62 void SignalProxy::socketDisconnected() {
63 for(int i = 0; i < peers.count(); i++) {
64 Connection conn = peers[i];
65 QAbstractSocket *sock = qobject_cast<QAbstractSocket*>(conn.device);
67 if(sock->state() == QAbstractSocket::UnconnectedState) {
68 peers[i].peer->deleteLater(); peers[i].device->deleteLater();
70 emit peerDisconnected();
76 void SignalProxy::attachSignal(QObject* sender, const char* signal, const QByteArray& rpcFunction) {
77 disconnect(sender, SIGNAL(destroyed(QObject *)), this, SLOT(detachObject(QObject *)));
78 connect(sender, SIGNAL(destroyed(QObject *)), this, SLOT(detachObject(QObject *)));
80 foreach(Connection conn, peers) {
81 conn.peer->attachSignal(sender, signal, rpcFunction);
83 attachedSignals.append(SignalDesc(sender, signal, rpcFunction));
86 void SignalProxy::attachSlot(const QByteArray& rpcFunction, QObject* recv, const char* slot) {
87 disconnect(recv, SIGNAL(destroyed(QObject *)), this, SLOT(detachObject(QObject *)));
88 connect(recv, SIGNAL(destroyed(QObject *)), this, SLOT(detachObject(QObject *)));
90 foreach(Connection conn, peers) {
91 conn.peer->attachSlot(rpcFunction, recv, slot);
93 attachedSlots.append(SlotDesc(rpcFunction, recv, slot));
96 void SignalProxy::detachObject(QObject* obj) {
97 //Q_ASSERT(false); // not done yet
98 foreach(Connection conn, peers) {
99 conn.peer->detachObject(obj);
101 QList<SignalDesc> sigs;
102 foreach(SignalDesc desc, attachedSignals) {
103 if(desc.sender != obj) sigs << desc;
105 attachedSignals = sigs;
106 QList<SlotDesc> slot;
107 foreach(SlotDesc desc, attachedSlots) {
108 if(desc.recv != obj) slot << desc;
110 attachedSlots = slot;
114 void SignalProxy::sendSignal(const char *signal, QVariant p1, QVariant p2, QVariant p3, QVariant p4, QVariant p5, QVariant p6, QVariant p7, QVariant p8, QVariant p9) {
115 foreach(Connection conn, peers) {
116 conn.peer->call(signal, p1, p2, p3, p4, p5, p6, p7, p8, p9);