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 <time.h> // 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  : mReadOnly(false)
24 {
25  generateUniqueId();
26 }
27 
28 OptionSet::OptionSet(const OptionSet &set)
29  : mId(set.mId)
30  , mName(set.mName)
31  , mDescription(set.mDescription)
32  , mReadOnly(set.mReadOnly)
33 {
34 }
35 
36 OptionSet::OptionSet(const QString &name, const QString &description, bool readOnly)
37  : mName(name)
38  , mDescription(description)
39  , mReadOnly(readOnly)
40 {
41  generateUniqueId();
42 }
43 
44 OptionSet::~OptionSet()
45 {
46 }
47 
48 void OptionSet::generateUniqueId()
49 {
50  static int nextUniqueId = 0;
51  nextUniqueId++;
52  mId = QStringLiteral("%1-%2").arg((unsigned int)::time(nullptr)).arg(nextUniqueId);
53 }
54 
55 QString OptionSet::saveToString() const
56 {
57  QByteArray raw;
58 
59  {
61 
62  s << gOptionSetInitialMarker;
63  s << gOptionSetWithReadOnLyModeVersion;
64  s << mId;
65  s << mName;
66  s << mDescription;
67  s << mReadOnly;
68 
69  save(s);
70 
71  s << gOptionSetFinalMarker;
72  }
73 
74  return QString::fromLatin1(raw.toHex());
75 }
76 
77 bool OptionSet::loadFromString(const QString &data)
78 {
80 
82 
83  int marker;
84 
85  s >> marker;
86 
87  if (marker != gOptionSetInitialMarker) {
88  return false; // invalid configuration
89  }
90 
91  int currentVersion;
92 
93  s >> currentVersion;
94 
95  if (currentVersion > gOptionSetWithReadOnLyModeVersion) {
96  return false; // invalid configuration
97  }
98 
99  s >> mId;
100 
101  if (mId.isEmpty()) {
102  return false; // invalid configuration
103  }
104 
105  s >> mName;
106 
107  if (mName.isEmpty()) {
108  return false; // invalid configuration
109  }
110 
111  s >> mDescription;
112 
113  bool readOnly = false;
114  if (currentVersion == gOptionSetWithReadOnLyModeVersion) {
115  s >> readOnly;
116  }
117  mReadOnly = readOnly;
118 
119  if (!load(s)) {
120  return false; // invalid configuration
121  }
122 
123  s >> marker;
124 
125  if (marker != gOptionSetFinalMarker) {
126  return false; // invalid configuration
127  }
128 
129  return true;
130 }
A set of options that can be applied to the MessageList in one shot.
Definition: optionset.h:31
QByteArray toHex() const const
The implementation independent part of the MessageList library.
Definition: aggregation.h:21
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 Sat Jan 16 2021 23:16:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.