bool writeConfFile(QIODevice &device, const QSettings::SettingsMap &map)
{
        int maxKeyWidth = 20;
        QRegExp reKeyXt("^(.+)/(.+)$");  // for extracting keys/values

        // first go over map and find longest key length
        for(int i=0; i<map.keys().size(); i++)
        {
                QString k = map.keys().at(i);
                QString key = k;
                if (reKeyXt.exactMatch(k))
                        key = reKeyXt.cap(2);
                if (key.size() > maxKeyWidth) maxKeyWidth = key.size();
        }

        // OK, this time actually write to the file - first non-section values
        QString outputLine;
        for(int i=0; i<map.keys().size(); i++)
        {
                QString k = map.keys().at(i);
                if (!reKeyXt.exactMatch(k))
                {
                        // this is for those keys without a section
                        outputLine = QString("%1=%2\n").arg(k,map[k].toString());
                        device.write(outputLine.toUtf8());
                }
        }

        return true;
}
Пример #2
0
bool writeXmlFile(QIODevice &device, const QSettings::SettingsMap &map)
{
	QXmlStreamWriter xmlWriter(&device);

	xmlWriter.setAutoFormatting(true);
	xmlWriter.writeStartDocument();
	xmlWriter.writeStartElement("Settings");

	QStringList prev_elements;
	QSettings::SettingsMap::ConstIterator map_i;

	// Alle Elemente der Map durchlaufen
	for (map_i = map.begin(); map_i != map.end(); map_i++) {

		QStringList elements = map_i.key().split("/");

		int x = 0;
		// Zu schließende Elemente ermitteln
		while (x < prev_elements.size() && elements.at(x) == prev_elements.at(x)) {
			x++;
		}

		// Elemente schließen
		for (int i = prev_elements.size() - 1; i >= x; i--) {
			xmlWriter.writeEndElement();
		}

		// Elemente öffnen
		for (int i = x; i < elements.size(); i++) {
			xmlWriter.writeStartElement(elements.at(i));
		}

		// Wert eintragen
		xmlWriter.writeCharacters(map_i.value().toString());

		prev_elements = elements;
	}

	// Noch offene Elemente schließen
	for (int i = 0; i < prev_elements.size(); i++) {
		xmlWriter.writeEndElement();
	}

	xmlWriter.writeEndElement();
	xmlWriter.writeEndDocument();

	return true;
}
Пример #3
0
bool writeSettings(QIODevice &device, const QSettings::SettingsMap &map)
{
    try {
        YAML::Emitter out;
        SettingsIterator it = map.begin();
        writeGroup( "", out, it, map );
        device.write(out.c_str());
        return true;
    }
    catch (std::exception & e) {
        qWarning() << "Exception when writing YAML config file:" << e.what();
        return false;
    }
}
Пример #4
0
// Functions to parse plist format are borrowed from pluginsystem.cpp from qutIM instant messenger (see:
// http://www.qutim.org/)
QSettings::SettingsMap parseDict(const QDomNode &rootElement)
{
    QSettings::SettingsMap styleHash;

    if (rootElement.isNull())
        return styleHash;

    QDomNode subelement = rootElement;

    QString key;

    for (QDomNode node = subelement.firstChild(); !node.isNull(); node = node.nextSibling())
    {
        QDomElement element = node.toElement();
        if (element.nodeName() == "key")
            key = element.text();
        else
        {
            QVariant value;
            if (element.nodeName() == "true")
                value = QVariant(true);
            else if (element.nodeName() == "false")
                value = QVariant(false);
            else if (element.nodeName() == "real")
                value = QVariant(element.text().toDouble());
            else if (element.nodeName() == "string")
                value = QVariant(element.text());
            else if (element.nodeName() == "integer")
                value = QVariant(element.text().toInt());
            else if (element.nodeName() == "dict")
                value = parseDict(node);
            styleHash.insert(key, value);
        }
    }
    return styleHash;
}
Пример #5
0
static void writeGroup( const QString &groupKey, YAML::Emitter &out,
                        SettingsIterator &it, const QSettings::SettingsMap &map )
{
    out << YAML::BeginMap;

    int groupKeyLen = groupKey.size();

    while(it != map.end())
    {
        QString key( it.key() );

        if (!key.startsWith(groupKey)) break;

        int i_separ = key.indexOf("/", groupKeyLen);
        if (i_separ != -1) {
            // There is child nodes
            key.truncate( i_separ + 1 );

            QString yamlKey(key);
            yamlKey.remove(0, groupKeyLen);
            yamlKey.chop(1);

            out << YAML::Key << yamlKey.toStdString();
            out << YAML::Value;

            writeGroup( key, out, it, map );
        }
        else
        {
            // There is no child nodes
            key.remove(0, groupKeyLen);

            out << YAML::Key << key.toStdString();
            out << YAML::Value;

            writeValue( it.value(), out );

            ++it;
        }
    }

    out << YAML::EndMap;
}
Пример #6
0
QJsonDocument variantMapToJsonDocument(const QSettings::SettingsMap& map) {
    QJsonObject object;
    for (auto it = map.cbegin(); it != map.cend(); ++it) {
        auto& key = it.key();
        auto& variant = it.value();
        auto variantType = variant.type();

        // Switch some types so they are readable/modifiable in the json file
        if (variantType == QVariant(1.0f).type()) { // float
            variantType = QVariant::Double;
        }
        if (variantType == QVariant((quint16)0).type()) { // uint16
            variantType = QVariant::UInt;
        }
        if (variantType == QVariant::Url) { // QUrl
            variantType = QVariant::String;
        }

        switch (variantType) {
            case QVariant::Hash: {
                qCritical() << "Unsupported variant type" << variant.typeName() << ";" << key << variant;
                Q_ASSERT(false);
                break;
            }

            case QVariant::Invalid:
                object.insert(key, QJsonValue());
                break;
            case QVariant::LongLong:
            case QVariant::ULongLong:
            case QVariant::Int:
            case QVariant::UInt:
            case QVariant::Bool:
            case QVariant::Double:
            case QVariant::Map:
            case QVariant::List:
                object.insert(key, QJsonValue::fromVariant(variant));
                break;

            case QVariant::String: {
                QString result = variant.toString();
                if (result.startsWith(QLatin1Char('@'))) {
                    result.prepend(QLatin1Char('@'));
                }
                object.insert(key, result);
                break;
            }

            case QVariant::ByteArray: {
                QByteArray a = variant.toByteArray();
                QString result = QLatin1String("@ByteArray(");
                result += QString::fromLatin1(a.constData(), a.size());
                result += QLatin1Char(')');
                object.insert(key, result);
                break;
            }
            case QVariant::Rect: {
                QRect r = qvariant_cast<QRect>(variant);
                QString result = QLatin1String("@Rect(");
                result += QString::number(r.x());
                result += QLatin1Char(' ');
                result += QString::number(r.y());
                result += QLatin1Char(' ');
                result += QString::number(r.width());
                result += QLatin1Char(' ');
                result += QString::number(r.height());
                result += QLatin1Char(')');
                object.insert(key, result);
                break;
            }
            case QVariant::Size: {
                QSize s = qvariant_cast<QSize>(variant);
                QString result = QLatin1String("@Size(");
                result += QString::number(s.width());
                result += QLatin1Char(' ');
                result += QString::number(s.height());
                result += QLatin1Char(')');
                object.insert(key, result);
                break;
            }
            case QVariant::Point: {
                QPoint p = qvariant_cast<QPoint>(variant);
                QString result = QLatin1String("@Point(");
                result += QString::number(p.x());
                result += QLatin1Char(' ');
                result += QString::number(p.y());
                result += QLatin1Char(')');
                object.insert(key, result);
                break;
            }

            default: {
                QByteArray array;
                {
                    QDataStream stream(&array, QIODevice::WriteOnly);
                    stream.setVersion(QDataStream::Qt_4_0);
                    stream << variant;
                }

                QString result = QLatin1String("@Variant(");
                result += QString::fromLatin1(array.constData(), array.size());
                result += QLatin1Char(')');
                object.insert(key, result);
                break;
            }
        }
    }

    return QJsonDocument(object);
}