summaryrefslogtreecommitdiffstats
path: root/common/storage/key.h
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2018-06-26 14:10:30 +0200
committerMinijackson <minijackson@riseup.net>2018-07-04 15:37:14 +0200
commitc90ba4a98292a39eb0b3df12fd7e2dec0300e58d (patch)
tree0c0c252087e6d8ccf31ba521ea76a7153032f20d /common/storage/key.h
parent922e0979e2c27ff8dbc765ae151d17c7815b98a0 (diff)
downloadsink-c90ba4a98292a39eb0b3df12fd7e2dec0300e58d.tar.gz
sink-c90ba4a98292a39eb0b3df12fd7e2dec0300e58d.zip
Move Key API into own files + some fixes
Diffstat (limited to 'common/storage/key.h')
-rw-r--r--common/storage/key.h100
1 files changed, 100 insertions, 0 deletions
diff --git a/common/storage/key.h b/common/storage/key.h
new file mode 100644
index 0000000..76dbd13
--- /dev/null
+++ b/common/storage/key.h
@@ -0,0 +1,100 @@
1/*
2 * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
3 * Copyright (C) 2018 Rémi Nicole <minijackson@riseup.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) version 3, or any
9 * later version accepted by the membership of KDE e.V. (or its
10 * successor approved by the membership of KDE e.V.), which shall
11 * act as a proxy defined in Section 6 of version 3 of the license.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include "sink_export.h"
25
26#include <QByteArray>
27#include <QDebug>
28#include <QUuid>
29
30namespace Sink {
31namespace Storage {
32
33class Identifier
34{
35public:
36 // RFC 4122 Section 4.1.2 says 128 bits -> 16 bytes
37 static const constexpr size_t INTERNAL_REPR_SIZE = 16;
38 static const constexpr size_t DISPLAY_REPR_SIZE = 38;
39
40 Identifier() : uid(QUuid::createUuid()){};
41
42 QByteArray toInternalByteArray() const;
43 static Identifier fromInternalByteArray(const QByteArray &bytes);
44 QString toDisplayString() const;
45 QByteArray toDisplayByteArray() const;
46 static Identifier fromDisplayByteArray(const QByteArray &bytes);
47
48private:
49 explicit Identifier(const QUuid &uid) : uid(uid) {}
50 QUuid uid;
51};
52
53class Revision
54{
55public:
56 // qint64 has a 19 digit decimal representation
57 static const constexpr size_t INTERNAL_REPR_SIZE = 19;
58 static const constexpr size_t DISPLAY_REPR_SIZE = 19;
59
60 Revision(qint64 rev) : rev(rev) {}
61
62 QByteArray toInternalByteArray() const;
63 static Revision fromInternalByteArray(const QByteArray &bytes);
64 QString toDisplayString() const;
65 QByteArray toDisplayByteArray() const;
66 static Revision fromDisplayByteArray(const QByteArray &bytes);
67 qint64 toQint64() const;
68
69private:
70 qint64 rev;
71};
72
73class Key
74{
75public:
76 static const constexpr size_t INTERNAL_REPR_SIZE = Identifier::INTERNAL_REPR_SIZE + Revision::INTERNAL_REPR_SIZE;
77 static const constexpr size_t DISPLAY_REPR_SIZE = Identifier::DISPLAY_REPR_SIZE + Revision::DISPLAY_REPR_SIZE;
78
79 Key(const Identifier &id, const Revision &rev) : id(id), rev(rev) {}
80
81 QByteArray toInternalByteArray() const;
82 static Key fromInternalByteArray(const QByteArray &bytes);
83 QString toDisplayString() const;
84 QByteArray toDisplayByteArray() const;
85 static Key fromDisplayByteArray(const QByteArray &bytes);
86 const Identifier &identifier() const;
87 const Revision &revision() const;
88 void setRevision(const Revision &newRev);
89
90private:
91 Identifier id;
92 Revision rev;
93};
94
95} // namespace Storage
96} // namespace Sink
97
98SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Identifier &);
99SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Revision &);
100SINK_EXPORT QDebug& operator<<(QDebug &dbg, const Sink::Storage::Key &);