Changeset View
Changeset View
Standalone View
Standalone View
libs/bolt/enum.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 - 2019 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 | QString Bolt::statusToString(Bolt::Status status) | ||||
52 | { | ||||
53 | switch (status) { | ||||
54 | case Bolt::Status::Unknown: | ||||
55 | return QStringLiteral("unknown"); | ||||
56 | case Bolt::Status::Disconnected: | ||||
57 | return QStringLiteral("disconnected"); | ||||
58 | case Bolt::Status::Connecting: | ||||
59 | return QStringLiteral("connecting"); | ||||
60 | case Bolt::Status::Connected: | ||||
61 | return QStringLiteral("connected"); | ||||
62 | case Bolt::Status::Authorizing: | ||||
63 | return QStringLiteral("authorizing"); | ||||
64 | case Bolt::Status::Authorized: | ||||
65 | return QStringLiteral("authorized"); | ||||
66 | case Bolt::Status::AuthError: | ||||
67 | return QStringLiteral("auth-error"); | ||||
68 | } | ||||
69 | Q_UNREACHABLE(); | ||||
70 | return {}; | ||||
71 | } | ||||
72 | | ||||
73 | Bolt::AuthFlags Bolt::authFlagsFromString(const QString &str) | ||||
74 | { | ||||
75 | const auto splitRef = str.splitRef(QStringLiteral("|")); | ||||
76 | Bolt::AuthFlags outFlags = Bolt::Auth::None; | ||||
77 | for (const auto &flag : splitRef) { | ||||
78 | const auto f = flag.trimmed(); | ||||
79 | if (f == QLatin1String("none")) { | ||||
80 | outFlags |= Bolt::Auth::None; | ||||
81 | } else if (f == QLatin1String("nopcie")) { | ||||
82 | outFlags |= Bolt::Auth::NoPCIE; | ||||
83 | } else if (f == QLatin1String("secure")) { | ||||
84 | outFlags |= Bolt::Auth::Secure; | ||||
85 | } else if (f == QLatin1String("nokey")) { | ||||
86 | outFlags |= Bolt::Auth::NoKey; | ||||
87 | } else if (f == QLatin1String("boot")) { | ||||
88 | outFlags |= Bolt::Auth::Boot; | ||||
89 | } else { | ||||
90 | qCCritical(log_libkbolt, "Unknown AuthFlags enum value '%s'", qUtf8Printable(str)); | ||||
91 | Q_ASSERT(false); | ||||
92 | return Bolt::Auth::None; | ||||
93 | } | ||||
94 | } | ||||
95 | return outFlags; | ||||
96 | } | ||||
97 | | ||||
98 | QString Bolt::authFlagsToString(AuthFlags flags) | ||||
99 | { | ||||
100 | QStringList str; | ||||
101 | if (flags == AuthFlags(Bolt::Auth::None)) { | ||||
102 | return QStringLiteral("none"); | ||||
103 | } | ||||
104 | if (flags & Bolt::Auth::NoPCIE) { | ||||
105 | str.push_back(QStringLiteral("nopcie")); | ||||
106 | } | ||||
107 | if (flags & Bolt::Auth::Secure) { | ||||
108 | str.push_back(QStringLiteral("secure")); | ||||
109 | } | ||||
110 | if (flags & Bolt::Auth::NoKey) { | ||||
111 | str.push_back(QStringLiteral("nokey")); | ||||
112 | } | ||||
113 | if (flags & Bolt::Auth::Boot) { | ||||
114 | str.push_back(QStringLiteral("boot")); | ||||
115 | } | ||||
116 | | ||||
117 | return str.join(QStringLiteral(" | ")); | ||||
118 | } | ||||
119 | | ||||
120 | | ||||
121 | Bolt::KeyState Bolt::keyStateFromString(const QString &str) | ||||
122 | { | ||||
123 | if (str == QLatin1String("unknown")) { | ||||
124 | return Bolt::KeyState::Unknown; | ||||
125 | } else if (str == QLatin1String("missing")) { | ||||
126 | return Bolt::KeyState::Missing; | ||||
127 | } else if (str == QLatin1String("have")) { | ||||
128 | return Bolt::KeyState::Have; | ||||
129 | } else if (str == QLatin1String("new")) { | ||||
130 | return Bolt::KeyState::New; | ||||
131 | } else { | ||||
132 | qCCritical(log_libkbolt, "Unknown KeyState enum value '%s'", qUtf8Printable(str)); | ||||
133 | Q_ASSERT(false); | ||||
134 | return Bolt::KeyState::Unknown; | ||||
135 | } | ||||
136 | } | ||||
137 | | ||||
138 | Bolt::Policy Bolt::policyFromString(const QString &str) | ||||
139 | { | ||||
140 | if (str == QLatin1String("unknown")) { | ||||
141 | return Bolt::Policy::Unknown; | ||||
142 | } else if (str == QLatin1String("default")) { | ||||
143 | return Bolt::Policy::Default; | ||||
144 | } else if (str == QLatin1String("manual")) { | ||||
145 | return Bolt::Policy::Manual; | ||||
146 | } else if (str == QLatin1String("auto")) { | ||||
147 | return Bolt::Policy::Auto; | ||||
148 | } else { | ||||
149 | qCCritical(log_libkbolt, "Unknown Policy enum value '%s'", qUtf8Printable(str)); | ||||
150 | Q_ASSERT(false); | ||||
151 | return Bolt::Policy::Unknown; | ||||
152 | } | ||||
153 | } | ||||
154 | | ||||
155 | QString Bolt::policyToString(Bolt::Policy policy) | ||||
156 | { | ||||
157 | switch (policy) { | ||||
158 | case Policy::Unknown: | ||||
159 | return QStringLiteral("unknown"); | ||||
160 | case Policy::Auto: | ||||
161 | return QStringLiteral("auto"); | ||||
162 | case Policy::Default: | ||||
163 | return QStringLiteral("default"); | ||||
164 | case Policy::Manual: | ||||
165 | return QStringLiteral("manual"); | ||||
166 | } | ||||
167 | | ||||
168 | Q_UNREACHABLE(); | ||||
169 | return {}; | ||||
170 | } | ||||
171 | | ||||
172 | Bolt::Type Bolt::typeFromString(const QString &str) | ||||
173 | { | ||||
174 | if (str == QLatin1String("unknown")) { | ||||
175 | return Bolt::Type::Unknown; | ||||
176 | } else if (str == QLatin1String("host")) { | ||||
177 | return Bolt::Type::Host; | ||||
178 | } else if (str == QLatin1String("peripheral")) { | ||||
179 | return Bolt::Type::Peripheral; | ||||
180 | } else { | ||||
181 | qCCritical(log_libkbolt, "Unknown Type enum value '%s'", qUtf8Printable(str)); | ||||
182 | Q_ASSERT(false); | ||||
183 | return Bolt::Type::Unknown; | ||||
184 | } | ||||
185 | } | ||||
186 | | ||||
187 | Bolt::AuthMode Bolt::authModeFromString(const QString &str) | ||||
188 | { | ||||
189 | if (str == QLatin1String("disabled")) { | ||||
190 | return Bolt::AuthMode::Disabled; | ||||
191 | } else if (str == QLatin1String("enabled")) { | ||||
192 | return Bolt::AuthMode::Enabled; | ||||
193 | } else { | ||||
194 | qCCritical(log_libkbolt, "Unknown AuthMode enum value '%s'", qUtf8Printable(str)); | ||||
195 | Q_ASSERT(false); | ||||
196 | return Bolt::AuthMode::Disabled; | ||||
197 | } | ||||
198 | } | ||||
199 | | ||||
200 | QString Bolt::authModeToString(Bolt::AuthMode authMode) | ||||
201 | { | ||||
202 | switch (authMode) { | ||||
203 | case Bolt::AuthMode::Enabled: | ||||
204 | return QStringLiteral("enabled"); | ||||
205 | case Bolt::AuthMode::Disabled: | ||||
206 | return QStringLiteral("disabled"); | ||||
207 | } | ||||
208 | | ||||
209 | Q_UNREACHABLE(); | ||||
210 | return {}; | ||||
211 | } | ||||
212 | | ||||
213 | Bolt::Security Bolt::securityFromString(const QString &str) | ||||
214 | { | ||||
215 | if (str == QLatin1String("unknown")) { | ||||
216 | return Bolt::Security::Unknown; | ||||
217 | } else if (str == QLatin1String("none")) { | ||||
218 | return Bolt::Security::None; | ||||
219 | } else if (str == QLatin1String("dponly")) { | ||||
220 | return Bolt::Security::DPOnly; | ||||
221 | } else if (str == QLatin1String("user")) { | ||||
222 | return Bolt::Security::User; | ||||
223 | } else if (str == QLatin1String("secure")) { | ||||
224 | return Bolt::Security::Secure; | ||||
225 | } else if (str == QLatin1String("usbonly")) { | ||||
226 | return Bolt::Security::USBOnly; | ||||
227 | } else { | ||||
228 | qCCritical(log_libkbolt, "Unknown Security enum value '%s'", qUtf8Printable(str)); | ||||
229 | Q_ASSERT(false); | ||||
230 | return Bolt::Security::Unknown; | ||||
231 | } | ||||
232 | } | ||||
233 | |