QDomElement stringListToXml(QDomDocument &doc, const QString &name, const QStringList &l) { QDomElement tag = doc.createElement(name); for(QStringList::ConstIterator it = l.begin(); it != l.end(); ++it) tag.appendChild(textTag(doc, "item", *it)); return tag; }
ContactListModelSelection::ContactListModelSelection(QList<ContactListItemProxy*> items) : QMimeData() , mimeData_(0) { QDomDocument doc; QDomElement root = doc.createElement("items"); root.setAttribute("version", "2.0"); doc.appendChild(root); // TODO: maybe also embed a random instance-specific token to // prevent drag'n'drop with other running Psi instances? QStringList jids; foreach(ContactListItemProxy* itemProxy, items) { Q_ASSERT(itemProxy); PsiContact* contact = 0; ContactListNestedGroup* group = 0; ContactListAccountGroup* account = 0; if ((contact = dynamic_cast<PsiContact*>(itemProxy->item()))) { QDomElement tag = textTag(&doc, "contact", contact->jid().full()); tag.setAttribute("account", contact->account()->id()); tag.setAttribute("group", itemProxy->parent() ? itemProxy->parent()->fullName() : ""); root.appendChild(tag); jids << contact->jid().full(); } else if ((account = dynamic_cast<ContactListAccountGroup*>(itemProxy->item()))) { QDomElement tag = doc.createElement("account"); tag.setAttribute("id", account->account()->id()); root.appendChild(tag); jids << account->displayName(); } else if ((group = dynamic_cast<ContactListNestedGroup*>(itemProxy->item()))) { // if group->fullName() consists only of whitespace when we'll try // to read it back we'll get an empty string, so we're using CDATA // QDomElement tag = textTag(&doc, "group", group->fullName()); QDomElement tag = doc.createElement("group"); QDomText text = doc.createCDATASection(TextUtil::escape(group->fullName())); tag.appendChild(text); root.appendChild(tag); jids << group->fullName(); } else { qWarning("ContactListModelSelection::ContactListModelSelection(): Unable to serialize %d, unsupported type", itemProxy->item()->type()); } }
QDomElement ProxySettings::toXml(QDomDocument *doc) const { QDomElement e = doc->createElement("proxySettings"); e.appendChild(textTag(*doc, "host", host)); e.appendChild(textTag(*doc, "port", QString::number(port))); e.appendChild(textTag(*doc, "url", url)); e.appendChild(textTag(*doc, "useAuth", useAuth)); e.appendChild(textTag(*doc, "user", user)); e.appendChild(textTag(*doc, "pass", pass)); return e; }
nsresult TextEditorTest::TestTextProperties() { nsCOMPtr<nsIDOMDocument>doc; nsresult result = mEditor->GetDocument(getter_AddRefs(doc)); TEST_RESULT(result); TEST_POINTER(doc.get()); nsCOMPtr<nsIDOMNodeList>nodeList; // XXX This is broken, text nodes are not elements. nsAutoString textTag(NS_LITERAL_STRING("#text")); result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList)); TEST_RESULT(result); TEST_POINTER(nodeList.get()); uint32_t count; nodeList->GetLength(&count); NS_ASSERTION(0!=count, "there are no text nodes in the document!"); nsCOMPtr<nsIDOMNode>textNode; result = nodeList->Item(count-1, getter_AddRefs(textNode)); TEST_RESULT(result); TEST_POINTER(textNode.get()); // set the whole text node to bold printf("set the whole first text node to bold\n"); nsCOMPtr<nsISelection>selection; result = mEditor->GetSelection(getter_AddRefs(selection)); TEST_RESULT(result); TEST_POINTER(selection.get()); nsCOMPtr<nsIDOMCharacterData>textData; textData = do_QueryInterface(textNode); uint32_t length; textData->GetLength(&length); selection->Collapse(textNode, 0); selection->Extend(textNode, length); nsCOMPtr<nsIHTMLEditor> htmlEditor (do_QueryInterface(mTextEditor)); NS_ENSURE_TRUE(htmlEditor, NS_ERROR_FAILURE); bool any = false; bool all = false; bool first=false; const nsAFlatString& empty = EmptyString(); result = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first, &any, &all); TEST_RESULT(result); NS_ASSERTION(false==first, "first should be false"); NS_ASSERTION(false==any, "any should be false"); NS_ASSERTION(false==all, "all should be false"); result = htmlEditor->SetInlineProperty(nsGkAtoms::b, empty, empty); TEST_RESULT(result); result = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first, &any, &all); TEST_RESULT(result); NS_ASSERTION(true==first, "first should be true"); NS_ASSERTION(true==any, "any should be true"); NS_ASSERTION(true==all, "all should be true"); mEditor->DebugDumpContent(); // remove the bold we just set printf("set the whole first text node to not bold\n"); result = htmlEditor->RemoveInlineProperty(nsGkAtoms::b, empty); TEST_RESULT(result); result = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first, &any, &all); TEST_RESULT(result); NS_ASSERTION(false==first, "first should be false"); NS_ASSERTION(false==any, "any should be false"); NS_ASSERTION(false==all, "all should be false"); mEditor->DebugDumpContent(); // set all but the first and last character to bold printf("set the first text node (1, length-1) to bold and italic, and (2, length-1) to underline.\n"); selection->Collapse(textNode, 1); selection->Extend(textNode, length-1); result = htmlEditor->SetInlineProperty(nsGkAtoms::b, empty, empty); TEST_RESULT(result); result = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first, &any, &all); TEST_RESULT(result); NS_ASSERTION(true==first, "first should be true"); NS_ASSERTION(true==any, "any should be true"); NS_ASSERTION(true==all, "all should be true"); mEditor->DebugDumpContent(); // make all that same text italic result = htmlEditor->SetInlineProperty(nsGkAtoms::i, empty, empty); TEST_RESULT(result); result = htmlEditor->GetInlineProperty(nsGkAtoms::i, empty, empty, &first, &any, &all); TEST_RESULT(result); NS_ASSERTION(true==first, "first should be true"); NS_ASSERTION(true==any, "any should be true"); NS_ASSERTION(true==all, "all should be true"); result = htmlEditor->GetInlineProperty(nsGkAtoms::b, empty, empty, &first, &any, &all); TEST_RESULT(result); NS_ASSERTION(true==first, "first should be true"); NS_ASSERTION(true==any, "any should be true"); NS_ASSERTION(true==all, "all should be true"); mEditor->DebugDumpContent(); // make all the text underlined, except for the first 2 and last 2 characters result = doc->GetElementsByTagName(textTag, getter_AddRefs(nodeList)); TEST_RESULT(result); TEST_POINTER(nodeList.get()); nodeList->GetLength(&count); NS_ASSERTION(0!=count, "there are no text nodes in the document!"); result = nodeList->Item(count-2, getter_AddRefs(textNode)); TEST_RESULT(result); TEST_POINTER(textNode.get()); textData = do_QueryInterface(textNode); textData->GetLength(&length); NS_ASSERTION(length==915, "wrong text node"); selection->Collapse(textNode, 1); selection->Extend(textNode, length-2); result = htmlEditor->SetInlineProperty(nsGkAtoms::u, empty, empty); TEST_RESULT(result); result = htmlEditor->GetInlineProperty(nsGkAtoms::u, empty, empty, &first, &any, &all); TEST_RESULT(result); NS_ASSERTION(true==first, "first should be true"); NS_ASSERTION(true==any, "any should be true"); NS_ASSERTION(true==all, "all should be true"); mEditor->DebugDumpContent(); return result; }
QDomElement VCard::toXml(QDomDocument *doc) const { QDomElement v = doc->createElement("vCard"); v.setAttribute("version", "2.0"); v.setAttribute("prodid", "-//HandGen//NONSGML vGen v1.0//EN"); v.setAttribute("xmlns", "vcard-temp"); if ( !d->version.isEmpty() ) v.appendChild( textTag(doc, "VERSION", d->version) ); if ( !d->fullName.isEmpty() ) v.appendChild( textTag(doc, "FN", d->fullName) ); if ( !d->familyName.isEmpty() || !d->givenName.isEmpty() || !d->middleName.isEmpty() || !d->prefixName.isEmpty() || !d->suffixName.isEmpty() ) { QDomElement w = doc->createElement("N"); if ( !d->familyName.isEmpty() ) w.appendChild( textTag(doc, "FAMILY", d->familyName) ); if ( !d->givenName.isEmpty() ) w.appendChild( textTag(doc, "GIVEN", d->givenName) ); if ( !d->middleName.isEmpty() ) w.appendChild( textTag(doc, "MIDDLE", d->middleName) ); if ( !d->prefixName.isEmpty() ) w.appendChild( textTag(doc, "PREFIX", d->prefixName) ); if ( !d->suffixName.isEmpty() ) w.appendChild( textTag(doc, "SUFFIX", d->suffixName) ); v.appendChild(w); } if ( !d->nickName.isEmpty() ) v.appendChild( textTag(doc, "NICKNAME", d->nickName) ); if ( !d->photo.isEmpty() || !d->photoURI.isEmpty() ) { QDomElement w = doc->createElement("PHOTO"); if ( !d->photo.isEmpty() ) { w.appendChild( textTag(doc, "TYPE", image2type(d->photo)) ); w.appendChild( textTag(doc, "BINVAL", foldString( Base64::arrayToString(d->photo)) ) ); } else if ( !d->photoURI.isEmpty() ) w.appendChild( textTag(doc, "EXTVAL", d->photoURI) ); v.appendChild(w); } if ( !d->bday.isEmpty() ) v.appendChild( textTag(doc, "BDAY", d->bday) ); if ( !d->addressList.isEmpty() ) { AddressList::Iterator it = d->addressList.begin(); for ( ; it != d->addressList.end(); ++it ) { QDomElement w = doc->createElement("ADR"); Address a = *it; if ( a.home ) w.appendChild( emptyTag(doc, "HOME") ); if ( a.work ) w.appendChild( emptyTag(doc, "WORK") ); if ( a.postal ) w.appendChild( emptyTag(doc, "POSTAL") ); if ( a.parcel ) w.appendChild( emptyTag(doc, "PARCEL") ); if ( a.dom ) w.appendChild( emptyTag(doc, "DOM") ); if ( a.intl ) w.appendChild( emptyTag(doc, "INTL") ); if ( a.pref ) w.appendChild( emptyTag(doc, "PREF") ); if ( !a.pobox.isEmpty() ) w.appendChild( textTag(doc, "POBOX", a.pobox) ); if ( !a.extaddr.isEmpty() ) w.appendChild( textTag(doc, "EXTADR", a.extaddr) ); if ( !a.street.isEmpty() ) w.appendChild( textTag(doc, "STREET", a.street) ); if ( !a.locality.isEmpty() ) w.appendChild( textTag(doc, "LOCALITY", a.locality) ); if ( !a.region.isEmpty() ) w.appendChild( textTag(doc, "REGION", a.region) ); if ( !a.pcode.isEmpty() ) w.appendChild( textTag(doc, "PCODE", a.pcode) ); if ( !a.country.isEmpty() ) w.appendChild( textTag(doc, "CTRY", a.country) ); v.appendChild(w); } } if ( !d->labelList.isEmpty() ) { LabelList::Iterator it = d->labelList.begin(); for ( ; it != d->labelList.end(); ++it ) { QDomElement w = doc->createElement("LABEL"); Label l = *it; if ( l.home ) w.appendChild( emptyTag(doc, "HOME") ); if ( l.work ) w.appendChild( emptyTag(doc, "WORK") ); if ( l.postal ) w.appendChild( emptyTag(doc, "POSTAL") ); if ( l.parcel ) w.appendChild( emptyTag(doc, "PARCEL") ); if ( l.dom ) w.appendChild( emptyTag(doc, "DOM") ); if ( l.intl ) w.appendChild( emptyTag(doc, "INTL") ); if ( l.pref ) w.appendChild( emptyTag(doc, "PREF") ); if ( !l.lines.isEmpty() ) { QStringList::Iterator it = l.lines.begin(); for ( ; it != l.lines.end(); ++it ) w.appendChild( textTag(doc, "LINE", *it) ); } v.appendChild(w); } } if ( !d->phoneList.isEmpty() ) { PhoneList::Iterator it = d->phoneList.begin(); for ( ; it != d->phoneList.end(); ++it ) { QDomElement w = doc->createElement("TEL"); Phone p = *it; if ( p.home ) w.appendChild( emptyTag(doc, "HOME") ); if ( p.work ) w.appendChild( emptyTag(doc, "WORK") ); if ( p.voice ) w.appendChild( emptyTag(doc, "VOICE") ); if ( p.fax ) w.appendChild( emptyTag(doc, "FAX") ); if ( p.pager ) w.appendChild( emptyTag(doc, "PAGER") ); if ( p.msg ) w.appendChild( emptyTag(doc, "MSG") ); if ( p.cell ) w.appendChild( emptyTag(doc, "CELL") ); if ( p.video ) w.appendChild( emptyTag(doc, "VIDEO") ); if ( p.bbs ) w.appendChild( emptyTag(doc, "BBS") ); if ( p.modem ) w.appendChild( emptyTag(doc, "MODEM") ); if ( p.isdn ) w.appendChild( emptyTag(doc, "ISDN") ); if ( p.pcs ) w.appendChild( emptyTag(doc, "PCS") ); if ( p.pref ) w.appendChild( emptyTag(doc, "PREF") ); if ( !p.number.isEmpty() ) w.appendChild( textTag(doc, "NUMBER", p.number) ); v.appendChild(w); } } if ( !d->emailList.isEmpty() ) { EmailList::Iterator it = d->emailList.begin(); for ( ; it != d->emailList.end(); ++it ) { QDomElement w = doc->createElement("EMAIL"); Email e = *it; if ( e.home ) w.appendChild( emptyTag(doc, "HOME") ); if ( e.work ) w.appendChild( emptyTag(doc, "WORK") ); if ( e.internet ) w.appendChild( emptyTag(doc, "INTERNET") ); if ( e.x400 ) w.appendChild( emptyTag(doc, "X400") ); if ( e.userid ) w.appendChild( textTag(doc, "USERID", e.userid) ); v.appendChild(w); } } if ( !d->jid.isEmpty() ) v.appendChild( textTag(doc, "JABBERID", d->jid) ); if ( !d->mailer.isEmpty() ) v.appendChild( textTag(doc, "MAILER", d->mailer) ); if ( !d->timezone.isEmpty() ) v.appendChild( textTag(doc, "TZ", d->timezone) ); if ( !d->geo.lat.isEmpty() || !d->geo.lon.isEmpty() ) { QDomElement w = doc->createElement("GEO"); if ( !d->geo.lat.isEmpty() ) w.appendChild( textTag(doc, "LAT", d->geo.lat) ); if ( !d->geo.lon.isEmpty() ) w.appendChild( textTag(doc, "LON", d->geo.lon)); v.appendChild(w); } if ( !d->title.isEmpty() ) v.appendChild( textTag(doc, "TITLE", d->title) ); if ( !d->role.isEmpty() ) v.appendChild( textTag(doc, "ROLE", d->role) ); if ( !d->logo.isEmpty() || !d->logoURI.isEmpty() ) { QDomElement w = doc->createElement("LOGO"); if ( !d->logo.isEmpty() ) { w.appendChild( textTag(doc, "TYPE", image2type(d->logo)) ); w.appendChild( textTag(doc, "BINVAL", foldString( Base64::arrayToString(d->logo)) ) ); } else if ( !d->logoURI.isEmpty() ) w.appendChild( textTag(doc, "EXTVAL", d->logoURI) ); v.appendChild(w); } if ( !d->agentURI.isEmpty() || (d->agent && d->agent->isEmpty()) ) { QDomElement w = doc->createElement("AGENT"); if ( d->agent && !d->agent->isEmpty() ) w.appendChild( d->agent->toXml(doc) ); else if ( !d->agentURI.isEmpty() ) w.appendChild( textTag(doc, "EXTVAL", d->agentURI) ); v.appendChild(w); } if ( !d->org.name.isEmpty() || !d->org.unit.isEmpty() ) { QDomElement w = doc->createElement("ORG"); if ( !d->org.name.isEmpty() ) w.appendChild( textTag(doc, "ORGNAME", d->org.name) ); if ( !d->org.unit.isEmpty() ) { QStringList::Iterator it = d->org.unit.begin(); for ( ; it != d->org.unit.end(); ++it ) w.appendChild( textTag(doc, "ORGUNIT", *it) ); } v.appendChild(w); } if ( !d->categories.isEmpty() ) { QDomElement w = doc->createElement("CATEGORIES"); QStringList::Iterator it = d->categories.begin(); for ( ; it != d->categories.end(); ++it ) w.appendChild( textTag(doc, "KEYWORD", *it) ); v.appendChild(w); } if ( !d->note.isEmpty() ) v.appendChild( textTag(doc, "NOTE", d->note) ); if ( !d->prodId.isEmpty() ) v.appendChild( textTag(doc, "PRODID", d->prodId) ); if ( !d->rev.isEmpty() ) v.appendChild( textTag(doc, "REV", d->rev) ); if ( !d->sortString.isEmpty() ) v.appendChild( textTag(doc, "SORT-STRING", d->sortString) ); if ( !d->sound.isEmpty() || !d->soundURI.isEmpty() || !d->soundPhonetic.isEmpty() ) { QDomElement w = doc->createElement("SOUND"); if ( !d->sound.isEmpty() ) w.appendChild( textTag(doc, "BINVAL", foldString( Base64::arrayToString(d->sound)) ) ); else if ( !d->soundURI.isEmpty() ) w.appendChild( textTag(doc, "EXTVAL", d->soundURI) ); else if ( !d->soundPhonetic.isEmpty() ) w.appendChild( textTag(doc, "PHONETIC", d->soundPhonetic) ); v.appendChild(w); } if ( !d->uid.isEmpty() ) v.appendChild( textTag(doc, "UID", d->uid) ); if ( !d->url.isEmpty() ) v.appendChild( textTag(doc, "URL", d->url) ); if ( !d->desc.isEmpty() ) v.appendChild( textTag(doc, "DESC", d->desc) ); if ( d->privacyClass != pcNone ) { QDomElement w = doc->createElement("CLASS"); if ( d->privacyClass == pcPublic ) w.appendChild( emptyTag(doc, "PUBLIC") ); else if ( d->privacyClass == pcPrivate ) w.appendChild( emptyTag(doc, "PRIVATE") ); else if ( d->privacyClass == pcConfidential ) w.appendChild( emptyTag(doc, "CONFIDENTIAL") ); v.appendChild(w); } if ( !d->key.isEmpty() ) { QDomElement w = doc->createElement("KEY"); // TODO: Justin, please check out this code w.appendChild( textTag(doc, "TYPE", "text/plain")); // FIXME w.appendChild( textTag(doc, "CRED", QString::fromUtf8(d->key)) ); // FIXME v.appendChild(w); } return v; }