Messagelib

optionset.cpp
1/******************************************************************************
2 *
3 * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <pragma@kvirc.net>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 *
7 *******************************************************************************/
8
9#include "core/optionset.h"
10
11#include <ctime> // for ::time( time_t * )
12
13#include <QDataStream>
14#include <QIODevice>
15
16static const int gOptionSetInitialMarker = 0xcafe; // don't change
17static const int gOptionSetFinalMarker = 0xbabe; // don't change
18
19static const int gOptionSetWithReadOnLyModeVersion = 0x1002;
20
21using namespace MessageList::Core;
22
23OptionSet::OptionSet()
24{
26}
27
28OptionSet::OptionSet(const OptionSet &set) = default;
29
30OptionSet::OptionSet(const QString &name, const QString &description, bool readOnly)
31 : mName(name)
32 , mDescription(description)
33 , mReadOnly(readOnly)
34{
35 generateUniqueId();
36}
37
38OptionSet::~OptionSet() = default;
39
41{
42 static int nextUniqueId = 0;
43 nextUniqueId++;
44 mId = QStringLiteral("%1-%2").arg((unsigned int)::time(nullptr)).arg(nextUniqueId);
45}
46
48{
49 QByteArray raw;
50
51 {
53
54 s << gOptionSetInitialMarker;
55 s << gOptionSetWithReadOnLyModeVersion;
56 s << mId;
57 s << mName;
58 s << mDescription;
59 s << mReadOnly;
60
61 save(s);
62
63 s << gOptionSetFinalMarker;
64 }
65
66 return QString::fromLatin1(raw.toHex());
67}
68
70{
72
74
75 int marker;
76
77 s >> marker;
78
79 if (marker != gOptionSetInitialMarker) {
80 return false; // invalid configuration
81 }
82
83 int currentVersion;
84
85 s >> currentVersion;
86
87 if (currentVersion > gOptionSetWithReadOnLyModeVersion) {
88 return false; // invalid configuration
89 }
90
91 s >> mId;
92
93 if (mId.isEmpty()) {
94 return false; // invalid configuration
95 }
96
97 s >> mName;
98
99 if (mName.isEmpty()) {
100 return false; // invalid configuration
101 }
102
103 s >> mDescription;
104
105 bool readOnly = false;
106 if (currentVersion == gOptionSetWithReadOnLyModeVersion) {
107 s >> readOnly;
108 }
109 mReadOnly = readOnly;
110
111 if (!load(s)) {
112 return false; // invalid configuration
113 }
114
115 s >> marker;
116
117 if (marker != gOptionSetFinalMarker) {
118 return false; // invalid configuration
119 }
120
121 return true;
122}
A set of options that can be applied to the MessageList in one shot.
Definition optionset.h:33
virtual void save(QDataStream &s) const =0
Saves the inner contents of this option set to the specified data stream.
QString saveToString() const
Packs this configuration object into a string suitable for storing in a config file.
Definition optionset.cpp:47
void generateUniqueId()
(Re)generates a (hopefully) unique identifier for this option set.
Definition optionset.cpp:40
virtual bool load(QDataStream &s)=0
Loads the inner contents of this option set from the specified data stream.
bool loadFromString(const QString &data)
Attempts to unpack this configuration object from a string (that is likely to come out from a config ...
Definition optionset.cpp:69
QString name(StandardAction id)
The implementation independent part of the MessageList library.
Definition aggregation.h:22
QByteArray fromHex(const QByteArray &hexEncoded)
QByteArray toHex(char separator) const const
QString arg(Args &&... args) const const
QString fromLatin1(QByteArrayView str)
QByteArray toLatin1() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:25 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.