File indexing completed on 2024-04-21 05:51:36

0001 /*
0002  *  SPDX-FileCopyrightText: 2002-2003 Jesper K. Pedersen <blackie@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-only
0005  **/
0006 
0007 #include "compoundregexp.h"
0008 
0009 #include <KLocalizedString>
0010 
0011 #include "widgetfactory.h"
0012 
0013 CompoundRegExp::CompoundRegExp(bool selected, const QString &title, const QString &description, bool hidden, bool allowReplace, RegExp *child)
0014     : RegExp(selected)
0015     , _title(title)
0016     , _description(description)
0017     , _hidden(hidden)
0018     , _allowReplace(allowReplace)
0019     , _child(child)
0020 {
0021     if (child) {
0022         addChild(child);
0023     }
0024 }
0025 
0026 bool CompoundRegExp::check(ErrorMap &map, bool first, bool last)
0027 {
0028     return _child->check(map, first, last);
0029 }
0030 
0031 QDomNode CompoundRegExp::toXml(QDomDocument *doc) const
0032 {
0033     QDomElement top = doc->createElement(QStringLiteral("Compound"));
0034     if (_hidden) {
0035         top.setAttribute(QStringLiteral("hidden"), true);
0036     }
0037 
0038     if (_allowReplace) {
0039         top.setAttribute(QStringLiteral("allowReplace"), true);
0040     }
0041 
0042     QDomElement title = doc->createElement(QStringLiteral("Title"));
0043     QDomText titleTxt = doc->createTextNode(_title);
0044     title.appendChild(titleTxt);
0045     top.appendChild(title);
0046 
0047     QDomElement description = doc->createElement(QStringLiteral("Description"));
0048     QDomText descriptionTxt = doc->createTextNode(_description);
0049     description.appendChild(descriptionTxt);
0050     top.appendChild(description);
0051 
0052     top.appendChild(_child->toXml(doc));
0053 
0054     return top;
0055 }
0056 
0057 bool CompoundRegExp::load(const QDomElement &top, const QString &version)
0058 {
0059     Q_ASSERT(top.tagName() == QStringLiteral("Compound"));
0060     QString str = top.attribute(QStringLiteral("hidden"), QStringLiteral("0"));
0061     _hidden = true; // alway hidden. (str == QStringLiteral("1") );
0062 
0063     str = top.attribute(QStringLiteral("allowReplace"), QStringLiteral("0"));
0064     _allowReplace = (str == QStringLiteral("1"));
0065 
0066     for (QDomNode node = top.firstChild(); !node.isNull(); node = node.nextSibling()) {
0067         if (!node.isElement()) {
0068             continue; // skip past comments.
0069         }
0070 
0071         QString txt;
0072         QDomElement child = node.toElement();
0073         QDomNode txtNode = child.firstChild();
0074         if (txtNode.isText()) {
0075             txt = txtNode.toText().data();
0076         }
0077 
0078         if (child.tagName() == QStringLiteral("Title")) {
0079             if (txt.isEmpty()) {
0080                 _title = txt;
0081             } else {
0082                 _title = i18n(txt.toUtf8().data());
0083             }
0084         } else if (child.tagName() == QStringLiteral("Description")) {
0085             if (txt.isEmpty()) {
0086                 _description = txt;
0087             } else {
0088                 _description = i18n(txt.toUtf8().data());
0089             }
0090         } else {
0091             _child = WidgetFactory::createRegExp(child, version);
0092             if (_child) {
0093                 addChild(_child);
0094                 return true;
0095             } else {
0096                 return false;
0097             }
0098         }
0099     }
0100     return false;
0101 }
0102 
0103 bool CompoundRegExp::operator==(const RegExp &other) const
0104 {
0105     // Note the order is important in the comparison below.
0106     // Using other as the first argument, means that
0107     // the following will be evaluated: other.operator== rather than (*child).operator==
0108     // This means that if other is a CompoundRegExp too, then this level will be striped.
0109     return other == *_child;
0110 }