File indexing completed on 2024-12-15 04:54:40

0001 /******************************************************************************
0002  *
0003  *  SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  *
0007  *******************************************************************************/
0008 
0009 #include "core/optionset.h"
0010 
0011 #include <ctime> // for ::time( time_t * )
0012 
0013 #include <QDataStream>
0014 #include <QIODevice>
0015 
0016 static const int gOptionSetInitialMarker = 0xcafe; // don't change
0017 static const int gOptionSetFinalMarker = 0xbabe; // don't change
0018 
0019 static const int gOptionSetWithReadOnLyModeVersion = 0x1002;
0020 
0021 using namespace MessageList::Core;
0022 
0023 OptionSet::OptionSet()
0024 {
0025     generateUniqueId();
0026 }
0027 
0028 OptionSet::OptionSet(const OptionSet &set) = default;
0029 
0030 OptionSet::OptionSet(const QString &name, const QString &description, bool readOnly)
0031     : mName(name)
0032     , mDescription(description)
0033     , mReadOnly(readOnly)
0034 {
0035     generateUniqueId();
0036 }
0037 
0038 OptionSet::~OptionSet() = default;
0039 
0040 void OptionSet::generateUniqueId()
0041 {
0042     static int nextUniqueId = 0;
0043     nextUniqueId++;
0044     mId = QStringLiteral("%1-%2").arg((unsigned int)::time(nullptr)).arg(nextUniqueId);
0045 }
0046 
0047 QString OptionSet::saveToString() const
0048 {
0049     QByteArray raw;
0050 
0051     {
0052         QDataStream s(&raw, QIODevice::WriteOnly);
0053 
0054         s << gOptionSetInitialMarker;
0055         s << gOptionSetWithReadOnLyModeVersion;
0056         s << mId;
0057         s << mName;
0058         s << mDescription;
0059         s << mReadOnly;
0060 
0061         save(s);
0062 
0063         s << gOptionSetFinalMarker;
0064     }
0065 
0066     return QString::fromLatin1(raw.toHex());
0067 }
0068 
0069 bool OptionSet::loadFromString(const QString &data)
0070 {
0071     QByteArray raw = QByteArray::fromHex(data.toLatin1());
0072 
0073     QDataStream s(&raw, QIODevice::ReadOnly);
0074 
0075     int marker;
0076 
0077     s >> marker;
0078 
0079     if (marker != gOptionSetInitialMarker) {
0080         return false; // invalid configuration
0081     }
0082 
0083     int currentVersion;
0084 
0085     s >> currentVersion;
0086 
0087     if (currentVersion > gOptionSetWithReadOnLyModeVersion) {
0088         return false; // invalid configuration
0089     }
0090 
0091     s >> mId;
0092 
0093     if (mId.isEmpty()) {
0094         return false; // invalid configuration
0095     }
0096 
0097     s >> mName;
0098 
0099     if (mName.isEmpty()) {
0100         return false; // invalid configuration
0101     }
0102 
0103     s >> mDescription;
0104 
0105     bool readOnly = false;
0106     if (currentVersion == gOptionSetWithReadOnLyModeVersion) {
0107         s >> readOnly;
0108     }
0109     mReadOnly = readOnly;
0110 
0111     if (!load(s)) {
0112         return false; // invalid configuration
0113     }
0114 
0115     s >> marker;
0116 
0117     if (marker != gOptionSetFinalMarker) {
0118         return false; // invalid configuration
0119     }
0120 
0121     return true;
0122 }