Messagelib

optionset.cpp
1 /******************************************************************************
2  *
3  * SPDX-FileCopyrightText: 2008 Szymon Tomasz Stefanek <[email protected]>
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 
15 static const int gOptionSetInitialMarker = 0xcafe; // don't change
16 static const int gOptionSetFinalMarker = 0xbabe; // don't change
17 
18 static const int gOptionSetWithReadOnLyModeVersion = 0x1002;
19 
20 using namespace MessageList::Core;
21 
22 OptionSet::OptionSet()
23 {
25 }
26 
27 OptionSet::OptionSet(const OptionSet &set)
28 
29  = default;
30 
31 OptionSet::OptionSet(const QString &name, const QString &description, bool readOnly)
32  : mName(name)
33  , mDescription(description)
34  , mReadOnly(readOnly)
35 {
36  generateUniqueId();
37 }
38 
39 OptionSet::~OptionSet() = default;
40 
41 void OptionSet::generateUniqueId()
42 {
43  static int nextUniqueId = 0;
44  nextUniqueId++;
45  mId = QStringLiteral("%1-%2").arg((unsigned int)::time(nullptr)).arg(nextUniqueId);
46 }
47 
48 QString OptionSet::saveToString() const
49 {
50  QByteArray raw;
51 
52  {
54 
55  s << gOptionSetInitialMarker;
56  s << gOptionSetWithReadOnLyModeVersion;
57  s << mId;
58  s << mName;
59  s << mDescription;
60  s << mReadOnly;
61 
62  save(s);
63 
64  s << gOptionSetFinalMarker;
65  }
66 
67  return QString::fromLatin1(raw.toHex());
68 }
69 
70 bool OptionSet::loadFromString(const QString &data)
71 {
73 
75 
76  int marker;
77 
78  s >> marker;
79 
80  if (marker != gOptionSetInitialMarker) {
81  return false; // invalid configuration
82  }
83 
84  int currentVersion;
85 
86  s >> currentVersion;
87 
88  if (currentVersion > gOptionSetWithReadOnLyModeVersion) {
89  return false; // invalid configuration
90  }
91 
92  s >> mId;
93 
94  if (mId.isEmpty()) {
95  return false; // invalid configuration
96  }
97 
98  s >> mName;
99 
100  if (mName.isEmpty()) {
101  return false; // invalid configuration
102  }
103 
104  s >> mDescription;
105 
106  bool readOnly = false;
107  if (currentVersion == gOptionSetWithReadOnLyModeVersion) {
108  s >> readOnly;
109  }
110  mReadOnly = readOnly;
111 
112  if (!load(s)) {
113  return false; // invalid configuration
114  }
115 
116  s >> marker;
117 
118  if (marker != gOptionSetFinalMarker) {
119  return false; // invalid configuration
120  }
121 
122  return true;
123 }
A set of options that can be applied to the MessageList in one shot.
Definition: optionset.h:32
QByteArray toHex() const const
const QString & description() const
Returns a description of this option set.
Definition: optionset.h:79
The implementation independent part of the MessageList library.
Definition: aggregation.h:21
const QString & name() const
Returns the name of this OptionSet.
Definition: optionset.h:59
void generateUniqueId()
(Re)generates a (hopefully) unique identifier for this option set.
Definition: optionset.cpp:41
QByteArray fromHex(const QByteArray &hexEncoded)
QByteArray toLatin1() const const
QString fromLatin1(const char *str, int size)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Dec 5 2021 23:04:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.