Changeset View
Changeset View
Standalone View
Standalone View
libs/bolt/enum.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 Daniel Vrátil <dvratil@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "enum.h" | ||||
22 | #include "libkbolt_debug.h" | ||||
23 | | ||||
24 | #include <QStringList> | ||||
25 | #include <QVector> | ||||
26 | #include <QStringRef> | ||||
27 | | ||||
28 | Bolt::Status Bolt::statusFromString(const QString &str) | ||||
29 | { | ||||
30 | if (str == QLatin1String("unknown")) { | ||||
31 | return Bolt::Status::Unknown; | ||||
32 | } else if (str == QLatin1String("disconnected")) { | ||||
33 | return Bolt::Status::Disconnected; | ||||
34 | } else if (str == QLatin1String("connecting")) { | ||||
35 | return Bolt::Status::Connecting; | ||||
36 | } else if (str == QLatin1String("connected")) { | ||||
37 | return Bolt::Status::Connected; | ||||
38 | } else if (str == QLatin1String("authorizing")) { | ||||
39 | return Bolt::Status::Authorizing; | ||||
40 | } else if (str == QLatin1String("authorized")) { | ||||
41 | return Bolt::Status::Authorized; | ||||
42 | } else if (str == QLatin1String("auth-error")) { | ||||
43 | return Bolt::Status::AuthError; | ||||
44 | } else { | ||||
45 | qCCritical(log_libkbolt, "Unknown Status enum value '%s'", qUtf8Printable(str)); | ||||
46 | Q_ASSERT(false); | ||||
47 | return Bolt::Status::Unknown; | ||||
48 | } | ||||
49 | } | ||||
50 | | ||||
51 | Bolt::AuthFlags Bolt::authFlagsFromString(const QString &str) | ||||
52 | { | ||||
53 | const auto splitRef = str.splitRef(QStringLiteral("|")); | ||||
54 | Bolt::AuthFlags outFlags = Bolt::Auth::None; | ||||
55 | for (const auto &flag : splitRef) { | ||||
56 | const auto f = flag.trimmed(); | ||||
57 | if (f == QLatin1String("none")) { | ||||
58 | outFlags |= Bolt::Auth::None; | ||||
59 | } else if (f == QLatin1String("nopcie")) { | ||||
60 | outFlags |= Bolt::Auth::NoPCIE; | ||||
61 | } else if (f == QLatin1String("secure")) { | ||||
62 | outFlags |= Bolt::Auth::Secure; | ||||
63 | } else if (f == QLatin1String("nokey")) { | ||||
64 | outFlags |= Bolt::Auth::NoKey; | ||||
65 | } else if (f == QLatin1String("boot")) { | ||||
66 | outFlags |= Bolt::Auth::Boot; | ||||
67 | } else { | ||||
68 | qCCritical(log_libkbolt, "Unknown AuthFlags enum value '%s'", qUtf8Printable(str)); | ||||
69 | Q_ASSERT(false); | ||||
70 | return Bolt::Auth::None; | ||||
71 | } | ||||
72 | } | ||||
73 | return outFlags; | ||||
74 | } | ||||
75 | | ||||
76 | QString Bolt::authFlagsToString(AuthFlags flags) | ||||
77 | { | ||||
78 | QStringList str; | ||||
79 | if (flags == AuthFlags(Bolt::Auth::None)) { | ||||
80 | return QStringLiteral("none"); | ||||
81 | } | ||||
82 | if (flags & Bolt::Auth::NoPCIE) { | ||||
83 | str.push_back(QStringLiteral("nopcie")); | ||||
84 | } | ||||
85 | if (flags & Bolt::Auth::Secure) { | ||||
86 | str.push_back(QStringLiteral("secure")); | ||||
87 | } | ||||
88 | if (flags & Bolt::Auth::NoKey) { | ||||
89 | str.push_back(QStringLiteral("nokey")); | ||||
90 | } | ||||
91 | if (flags & Bolt::Auth::Boot) { | ||||
92 | str.push_back(QStringLiteral("boot")); | ||||
93 | } | ||||
94 | | ||||
95 | return str.join(QStringLiteral(" | ")); | ||||
96 | } | ||||
97 | | ||||
98 | | ||||
99 | Bolt::KeyState Bolt::keyStateFromString(const QString &str) | ||||
100 | { | ||||
101 | if (str == QLatin1String("unknown")) { | ||||
102 | return Bolt::KeyState::Unknown; | ||||
103 | } else if (str == QLatin1String("missing")) { | ||||
104 | return Bolt::KeyState::Missing; | ||||
105 | } else if (str == QLatin1String("have")) { | ||||
106 | return Bolt::KeyState::Have; | ||||
107 | } else if (str == QLatin1String("new")) { | ||||
108 | return Bolt::KeyState::New; | ||||
109 | } else { | ||||
110 | qCCritical(log_libkbolt, "Unknown KeyState enum value '%s'", qUtf8Printable(str)); | ||||
111 | Q_ASSERT(false); | ||||
112 | return Bolt::KeyState::Unknown; | ||||
113 | } | ||||
114 | } | ||||
115 | | ||||
116 | Bolt::Policy Bolt::policyFromString(const QString &str) | ||||
117 | { | ||||
118 | if (str == QLatin1String("unknown")) { | ||||
119 | return Bolt::Policy::Unknown; | ||||
120 | } else if (str == QLatin1String("default")) { | ||||
121 | return Bolt::Policy::Default; | ||||
122 | } else if (str == QLatin1String("manual")) { | ||||
123 | return Bolt::Policy::Manual; | ||||
124 | } else if (str == QLatin1String("auto")) { | ||||
125 | return Bolt::Policy::Auto; | ||||
126 | } else { | ||||
127 | qCCritical(log_libkbolt, "Unknown Policy enum value '%s'", qUtf8Printable(str)); | ||||
128 | Q_ASSERT(false); | ||||
129 | return Bolt::Policy::Unknown; | ||||
130 | } | ||||
131 | } | ||||
132 | | ||||
133 | QString Bolt::policyToString(Bolt::Policy policy) | ||||
134 | { | ||||
135 | switch (policy) { | ||||
136 | case Policy::Unknown: | ||||
137 | return QStringLiteral("unknown"); | ||||
138 | case Policy::Auto: | ||||
139 | return QStringLiteral("auto"); | ||||
140 | case Policy::Default: | ||||
141 | return QStringLiteral("default"); | ||||
142 | case Policy::Manual: | ||||
143 | return QStringLiteral("manual"); | ||||
144 | } | ||||
145 | | ||||
146 | Q_UNREACHABLE(); | ||||
147 | return {}; | ||||
148 | } | ||||
149 | | ||||
150 | Bolt::Type Bolt::typeFromString(const QString &str) | ||||
151 | { | ||||
152 | if (str == QLatin1String("unknown")) { | ||||
153 | return Bolt::Type::Unknown; | ||||
154 | } else if (str == QLatin1String("host")) { | ||||
155 | return Bolt::Type::Host; | ||||
156 | } else if (str == QLatin1String("peripheral")) { | ||||
157 | return Bolt::Type::Peripheral; | ||||
158 | } else { | ||||
159 | qCCritical(log_libkbolt, "Unknown Type enum value '%s'", qUtf8Printable(str)); | ||||
160 | Q_ASSERT(false); | ||||
161 | return Bolt::Type::Unknown; | ||||
162 | } | ||||
163 | } | ||||
164 | | ||||
165 | Bolt::AuthMode Bolt::authModeFromString(const QString &str) | ||||
166 | { | ||||
167 | if (str == QLatin1String("disabled")) { | ||||
168 | return Bolt::AuthMode::Disabled; | ||||
169 | } else if (str == QLatin1String("enabled")) { | ||||
170 | return Bolt::AuthMode::Enabled; | ||||
171 | } else { | ||||
172 | qCCritical(log_libkbolt, "Unknown AuthMode enum value '%s'", qUtf8Printable(str)); | ||||
173 | Q_ASSERT(false); | ||||
174 | return Bolt::AuthMode::Disabled; | ||||
175 | } | ||||
176 | } | ||||
177 | | ||||
178 | QString Bolt::authModeToString(Bolt::AuthMode authMode) | ||||
179 | { | ||||
180 | switch (authMode) { | ||||
181 | case Bolt::AuthMode::Enabled: | ||||
182 | return QStringLiteral("enabled"); | ||||
183 | case Bolt::AuthMode::Disabled: | ||||
184 | return QStringLiteral("disabled"); | ||||
185 | } | ||||
186 | | ||||
187 | Q_UNREACHABLE(); | ||||
188 | return {}; | ||||
189 | } | ||||
190 | | ||||
191 | Bolt::Security Bolt::securityFromString(const QString &str) | ||||
192 | { | ||||
193 | if (str == QLatin1String("unknown")) { | ||||
194 | return Bolt::Security::Unknown; | ||||
195 | } else if (str == QLatin1String("none")) { | ||||
196 | return Bolt::Security::None; | ||||
197 | } else if (str == QLatin1String("dponly")) { | ||||
198 | return Bolt::Security::DPOnly; | ||||
199 | } else if (str == QLatin1String("user")) { | ||||
200 | return Bolt::Security::User; | ||||
201 | } else if (str == QLatin1String("secure")) { | ||||
202 | return Bolt::Security::Secure; | ||||
203 | } else if (str == QLatin1String("usbonly")) { | ||||
204 | return Bolt::Security::USBOnly; | ||||
205 | } else { | ||||
206 | qCCritical(log_libkbolt, "Unknown Security enum value '%s'", qUtf8Printable(str)); | ||||
207 | Q_ASSERT(false); | ||||
208 | return Bolt::Security::Unknown; | ||||
209 | } | ||||
210 | } | ||||
211 | |