Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Libraries
KPublicTransport
Commits
7a9312ef
Commit
7a9312ef
authored
Apr 12, 2021
by
Volker Krause
Browse files
Move method to read translated JSON values to the JSON utility namespace
We'll need that elsewhere as well.
parent
f788453b
Pipeline
#57767
canceled with stage
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/lib/datatypes/json.cpp
View file @
7a9312ef
...
...
@@ -10,6 +10,7 @@
#include <QColor>
#include <QDateTime>
#include <QDebug>
#include <QLocale>
#include <QMetaObject>
#include <QMetaProperty>
#include <QRectF>
...
...
@@ -21,6 +22,21 @@
using
namespace
KPublicTransport
;
QString
Json
::
translatedValue
(
const
QJsonObject
&
obj
,
const
QString
&
key
)
{
auto
languageWithCountry
=
QLocale
().
name
();
auto
it
=
obj
.
constFind
(
key
+
QLatin1Char
(
'['
)
+
languageWithCountry
+
QLatin1Char
(
']'
));
if
(
it
!=
obj
.
constEnd
())
{
return
it
.
value
().
toString
();
}
const
auto
language
=
languageWithCountry
.
midRef
(
0
,
languageWithCountry
.
indexOf
(
QLatin1Char
(
'_'
)));
it
=
obj
.
constFind
(
key
+
QLatin1Char
(
'['
)
+
language
+
QLatin1Char
(
']'
));
if
(
it
!=
obj
.
constEnd
())
{
return
it
.
value
().
toString
();
}
return
obj
.
value
(
key
).
toString
();
}
static
QJsonValue
variantToJson
(
const
QVariant
&
v
)
{
switch
(
v
.
userType
())
{
...
...
src/lib/datatypes/json_p.h
View file @
7a9312ef
...
...
@@ -19,6 +19,9 @@ namespace KPublicTransport {
/** De/serialization helper methods. */
namespace
Json
{
/** Looks for a translated value in @p obj with @p key. */
QString
translatedValue
(
const
QJsonObject
&
obj
,
const
QString
&
key
);
QJsonObject
toJson
(
const
QMetaObject
*
mo
,
const
void
*
elem
);
/** Serialize from QMetaObject. */
...
...
src/lib/manager.cpp
View file @
7a9312ef
...
...
@@ -19,6 +19,7 @@
#include "datatypes/attributionutil_p.h"
#include "datatypes/backend.h"
#include "datatypes/disruption.h"
#include "datatypes/json_p.h"
#include "datatypes/platform.h"
#include "datatypes/vehicle.h"
#include "geo/geojson_p.h"
...
...
@@ -113,21 +114,6 @@ QNetworkAccessManager* ManagerPrivate::nam()
return
m_nam
;
}
static
QString
translatedValue
(
const
QJsonObject
&
obj
,
const
QString
&
key
)
{
auto
languageWithCountry
=
QLocale
().
name
();
auto
it
=
obj
.
constFind
(
key
+
QLatin1Char
(
'['
)
+
languageWithCountry
+
QLatin1Char
(
']'
));
if
(
it
!=
obj
.
constEnd
())
{
return
it
.
value
().
toString
();
}
const
auto
language
=
languageWithCountry
.
midRef
(
0
,
languageWithCountry
.
indexOf
(
QLatin1Char
(
'_'
)));
it
=
obj
.
constFind
(
key
+
QLatin1Char
(
'['
)
+
language
+
QLatin1Char
(
']'
));
if
(
it
!=
obj
.
constEnd
())
{
return
it
.
value
().
toString
();
}
return
obj
.
value
(
key
).
toString
();
}
void
ManagerPrivate
::
loadNetworks
()
{
...
...
@@ -157,8 +143,8 @@ void ManagerPrivate::loadNetworks()
Backend
metaData
;
metaData
.
setIdentifier
(
net
->
backendId
());
const
auto
jsonMetaData
=
doc
.
object
().
value
(
QLatin1String
(
"KPlugin"
)).
toObject
();
metaData
.
setName
(
translatedValue
(
jsonMetaData
,
QStringLiteral
(
"Name"
)));
metaData
.
setDescription
(
translatedValue
(
jsonMetaData
,
QStringLiteral
(
"Description"
)));
metaData
.
setName
(
Json
::
translatedValue
(
jsonMetaData
,
QStringLiteral
(
"Name"
)));
metaData
.
setDescription
(
Json
::
translatedValue
(
jsonMetaData
,
QStringLiteral
(
"Description"
)));
metaData
.
setIsSecure
(
net
->
capabilities
()
&
AbstractBackend
::
Secure
);
m_backendMetaData
.
push_back
(
std
::
move
(
metaData
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment