void Client::send(const QDomElement &x) { if(!d->stream) return; //QString out; //QTextStream ts(&out, IO_WriteOnly); //x.save(ts, 0); //QString out = Stream::xmlToString(x); //debug(QString("Client: outgoing: [\n%1]\n").arg(out)); //xmlOutgoing(out); QDomElement e = addCorrectNS(x); Stanza s = d->stream->createStanza(e); if(s.isNull()) { //printf("bad stanza??\n"); return; } QString out = s.toString(); debug(QString("Client: outgoing: [\n%1]\n").arg(out)); xmlOutgoing(out); //printf("x[%s] x2[%s] s[%s]\n", Stream::xmlToString(x).latin1(), Stream::xmlToString(e).latin1(), s.toString().latin1()); d->stream->write(s); }
QDomElement addCorrectNS(const QDomElement &e) { int x; // grab child nodes /*QDomDocumentFragment frag = e.ownerDocument().createDocumentFragment(); QDomNodeList nl = e.childNodes(); for(x = 0; x < nl.count(); ++x) frag.appendChild(nl.item(x).cloneNode());*/ // find closest xmlns QDomNode n = e; while(!n.isNull() && !n.toElement().hasAttribute("xmlns") && n.toElement().namespaceURI() == "" ) n = n.parentNode(); QString ns; if(n.isNull() || !n.toElement().hasAttribute("xmlns")){ if (n.toElement().namespaceURI () == ""){ ns = "jabber:client"; } else { ns = n.toElement().namespaceURI(); } } else { ns = n.toElement().attribute("xmlns"); } // make a new node QDomElement i = e.ownerDocument().createElementNS(ns, e.tagName()); // copy attributes QDomNamedNodeMap al = e.attributes(); for(x = 0; x < al.count(); ++x) { QDomAttr a = al.item(x).toAttr(); if(a.name() != "xmlns") i.setAttributeNodeNS(a.cloneNode().toAttr()); } // copy children QDomNodeList nl = e.childNodes(); for(x = 0; x < nl.count(); ++x) { QDomNode n = nl.item(x); if(n.isElement()) i.appendChild(addCorrectNS(n.toElement())); else i.appendChild(n.cloneNode()); } //i.appendChild(frag); return i; }