plistserializer.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Own includes
  2. #include "plistserializer.h"
  3. // Qt includes
  4. #include <QDomElement>
  5. #include <QDomDocument>
  6. #include <QDate>
  7. #include <QDateTime>
  8. static QDomElement textElement(QDomDocument& doc, const char *tagName, QString contents) {
  9. QDomElement tag = doc.createElement(QString::fromLatin1(tagName));
  10. tag.appendChild(doc.createTextNode(contents));
  11. return tag;
  12. }
  13. static QDomElement serializePrimitive(QDomDocument &doc, const QVariant &variant) {
  14. QDomElement result;
  15. if (variant.type() == QVariant::Bool) {
  16. result = doc.createElement(variant.toBool() ? QStringLiteral("true") : QStringLiteral("false"));
  17. }
  18. else if (variant.type() == QVariant::Date) {
  19. result = textElement(doc, "date", variant.toDate().toString(Qt::ISODate));
  20. }
  21. else if (variant.type() == QVariant::DateTime) {
  22. result = textElement(doc, "date", variant.toDateTime().toString(Qt::ISODate));
  23. }
  24. else if (variant.type() == QVariant::ByteArray) {
  25. result = textElement(doc, "data", QString::fromLatin1(variant.toByteArray().toBase64()));
  26. }
  27. else if (variant.type() == QVariant::String) {
  28. result = textElement(doc, "string", variant.toString());
  29. }
  30. else if (variant.type() == QVariant::Int) {
  31. result = textElement(doc, "integer", QString::number(variant.toInt()));
  32. }
  33. else if (variant.canConvert(QVariant::Double)) {
  34. QString num;
  35. num.setNum(variant.toDouble());
  36. result = textElement(doc, "real", num);
  37. }
  38. return result;
  39. }
  40. QDomElement PListSerializer::serializeElement(QDomDocument &doc, const QVariant &variant) {
  41. if (variant.type() == QVariant::Map) {
  42. return serializeMap(doc, variant.toMap());
  43. }
  44. else if (variant.type() == QVariant::List) {
  45. return serializeList(doc, variant.toList());
  46. }
  47. else {
  48. return serializePrimitive(doc, variant);
  49. }
  50. }
  51. QDomElement PListSerializer::serializeList(QDomDocument &doc, const QVariantList &list) {
  52. QDomElement element = doc.createElement(QStringLiteral("array"));
  53. foreach(QVariant item, list) {
  54. element.appendChild(serializeElement(doc, item));
  55. }
  56. return element;
  57. }
  58. QDomElement PListSerializer::serializeMap(QDomDocument &doc, const QVariantMap &map) {
  59. QDomElement element = doc.createElement(QStringLiteral("dict"));
  60. QList<QString> keys = map.keys();
  61. foreach(QString key, keys) {
  62. QDomElement keyElement = textElement(doc, "key", key);
  63. element.appendChild(keyElement);
  64. element.appendChild(serializeElement(doc, map[key]));
  65. }
  66. return element;
  67. }
  68. QString PListSerializer::toPList(const QVariant &variant) {
  69. QDomDocument document(QStringLiteral("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\""));
  70. document.appendChild(document.createProcessingInstruction(QStringLiteral("xml"), QStringLiteral("version=\"1.0\" encoding=\"UTF-8\"")));
  71. QDomElement plist = document.createElement(QStringLiteral("plist"));
  72. plist.setAttribute(QStringLiteral("version"), QStringLiteral("1.0"));
  73. document.appendChild(plist);
  74. plist.appendChild(serializeElement(document, variant));
  75. return document.toString();
  76. }