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 #include <QIODevice>
15 
16 static const int gOptionSetInitialMarker = 0xcafe; // don't change
17 static const int gOptionSetFinalMarker = 0xbabe; // don't change
18 
19 static const int gOptionSetWithReadOnLyModeVersion = 0x1002;
20 
21 using namespace MessageList::Core;
22 
23 OptionSet::OptionSet()
24 {
26 }
27 
28 OptionSet::OptionSet(const OptionSet &set) = default;
29 
30 OptionSet::OptionSet(const QString &name, const QString &description, bool readOnly)
31  : mName(name)
32  , mDescription(description)
33  , mReadOnly(readOnly)
34 {
35  generateUniqueId();
36 }
37 
38 OptionSet::~OptionSet() = default;
39 
40 void OptionSet::generateUniqueId()
41 {
42  static int nextUniqueId = 0;
43  nextUniqueId++;
44  mId = QStringLiteral("%1-%2").arg((unsigned int)::time(nullptr)).arg(nextUniqueId);
45 }
46 
47 QString OptionSet::saveToString() const
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 
69 bool OptionSet::loadFromString(const QString &data)
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 }
QByteArray toLatin1() const const
void generateUniqueId()
(Re)generates a (hopefully) unique identifier for this option set.
Definition: optionset.cpp:40
QByteArray toHex() const const
QString fromLatin1(const char *str, int size)
QString name(StandardShortcut id)
A set of options that can be applied to the MessageList in one shot.
Definition: optionset.h:32
QByteArray fromHex(const QByteArray &hexEncoded)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Sep 26 2023 04:00:45 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.