Skip to content

Commit 78caebc

Browse files
committed
Add version parameter and cleanup
Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 0de1619 commit 78caebc

8 files changed

Lines changed: 85 additions & 67 deletions

File tree

src/controller/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ add_library(controller STATIC
3333
utils/erasedata.hpp
3434
utils/observer_ptr.hpp
3535
utils/qdisablecopymove.hpp
36+
utils/qt_comp.hpp
3637
utils/utils.hpp
3738
writeresponse.cpp
3839
writeresponse.hpp

src/controller/application.cpp

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "certandpininfo.hpp"
2525
#include "logging.hpp"
2626
#include "retriableerror.hpp"
27+
#include "utils/qt_comp.hpp"
2728

2829
#include <QCommandLineParser>
2930
#include <QDir>
@@ -36,6 +37,11 @@
3637
#include <QStyleHints>
3738
#include <QTranslator>
3839

40+
#include <algorithm>
41+
#include <array>
42+
43+
using namespace Qt::Literals::StringLiterals;
44+
3945
inline CommandWithArguments::second_type parseArgumentJson(const QString& argumentStr)
4046
{
4147
const auto argumentJson = QJsonDocument::fromJson(argumentStr.toUtf8());
@@ -51,26 +57,26 @@ Application::Application(int& argc, char** argv, const QString& name) :
5157
QApplication(argc, argv), translator(new QTranslator(this))
5258
{
5359
setApplicationName(name);
54-
setApplicationDisplayName(QStringLiteral("Web eID"));
60+
setApplicationDisplayName(u"Web eID"_s);
5561
setApplicationVersion(QStringLiteral(PROJECT_VERSION));
56-
setOrganizationDomain(QStringLiteral("web-eid.eu"));
57-
setOrganizationName(QStringLiteral("RIA"));
62+
setOrganizationDomain(u"web-eid.eu"_s);
63+
setOrganizationName(u"RIA"_s);
5864
setQuitOnLastWindowClosed(false);
5965

6066
installTranslator(translator);
6167
loadTranslations();
6268

6369
auto list = QUrl::idnWhitelist();
6470
list.append({
65-
QStringLiteral("fi"),
66-
QStringLiteral("ee"),
67-
QStringLiteral("lt"),
68-
QStringLiteral("lv"),
71+
u"fi"_s,
72+
u"ee"_s,
73+
u"lt"_s,
74+
u"lv"_s,
6975
});
7076
QUrl::setIdnWhitelist(list);
7177

72-
for (const QString& font : QDir(QStringLiteral(":/fonts")).entryList()) {
73-
QFontDatabase::addApplicationFont(QStringLiteral(":/fonts/%1").arg(font));
78+
for (const QString& font : QDir(u":/fonts"_s).entryList()) {
79+
QFontDatabase::addApplicationFont(u":/fonts/%1"_s.arg(font));
7480
}
7581

7682
registerMetatypes();
@@ -93,8 +99,7 @@ bool Application::isDarkTheme()
9399
// supported OS-s.
94100
static const bool isDarkTheme = [] {
95101
QProcess p;
96-
p.start(QStringLiteral("gsettings"),
97-
{"get", "org.gnome.desktop.interface", "color-scheme"});
102+
p.start(u"gsettings"_s, {u"get"_s, u"org.gnome.desktop.interface"_s, u"color-scheme"_s});
98103
if (p.waitForFinished()) {
99104
return p.readAllStandardOutput().contains("dark");
100105
}
@@ -108,56 +113,52 @@ bool Application::isDarkTheme()
108113

109114
void Application::loadTranslations(const QString& lang)
110115
{
111-
static const QStringList SUPPORTED_LANGS {
112-
QStringLiteral("en"), QStringLiteral("et"), QStringLiteral("fi"), QStringLiteral("hr"),
113-
QStringLiteral("ru"), QStringLiteral("de"), QStringLiteral("fr"), QStringLiteral("nl"),
114-
QStringLiteral("cs"), QStringLiteral("sk")};
116+
static constexpr std::array SUPPORTED_LANGS {u"en"_sv, u"et"_sv, u"fi"_sv, u"hr"_sv, u"ru"_sv,
117+
u"de"_sv, u"fr"_sv, u"nl"_sv, u"cs"_sv, u"sk"_sv};
115118
QLocale locale;
116-
QString langSetting = QSettings().value(QStringLiteral("lang"), lang).toString();
117-
if (SUPPORTED_LANGS.contains(langSetting)) {
119+
QString langSetting = QSettings().value(u"lang"_s, lang).toString();
120+
if (std::ranges::find(SUPPORTED_LANGS, langSetting) != SUPPORTED_LANGS.cend()) {
118121
locale = QLocale(langSetting);
119122
}
120-
void(translator->load(locale, QStringLiteral(":/translations/")));
123+
void(translator->load(locale, u":/translations/"_s));
121124
}
122125

123126
CommandWithArgumentsPtr Application::parseArgs()
124127
{
125128
// On Windows Chrome, the native messaging host is also passed a command line argument with a
126129
// handle to the calling Chrome native window: --parent-window=<decimal handle value>.
127130
// We don't use it, but need to support it to avoid unknown option errors.
128-
QCommandLineOption parentWindow(QStringLiteral("parent-window"),
129-
QStringLiteral("Parent window handle (unused)"),
130-
QStringLiteral("parent-window"));
131+
QCommandLineOption parentWindow(u"parent-window"_s, u"Parent window handle (unused)"_s,
132+
u"parent-window"_s);
131133

132-
QCommandLineOption aboutArgument(QStringLiteral("about"),
133-
QStringLiteral("Show Web-eID about window"));
134+
QCommandLineOption aboutArgument(u"about"_s, u"Show Web-eID about window"_s);
135+
QCommandLineOption commandLineMode(
136+
{u"c"_s, u"command-line-mode"_s},
137+
u"Command-line mode, read commands from command line arguments instead of "
138+
"standard input."_s);
134139

135140
QCommandLineParser parser;
136-
parser.setApplicationDescription(QStringLiteral(
137-
"Application that communicates with the Web eID browser extension via standard input and "
141+
parser.setApplicationDescription(
142+
u"Application that communicates with the Web eID browser extension via standard input and "
138143
"output, but also works standalone in command-line mode. Performs PKI cryptographic "
139-
"operations with eID smart cards for signing and authentication purposes."));
144+
"operations with eID smart cards for signing and authentication purposes."_s);
140145

141146
parser.addHelpOption();
142-
parser.addOptions({{{"c", "command-line-mode"},
143-
"Command-line mode, read commands from command line arguments instead of "
144-
"standard input."},
145-
aboutArgument,
146-
parentWindow});
147+
parser.addVersionOption();
148+
parser.addOptions({commandLineMode, aboutArgument, parentWindow});
147149

148-
static const auto COMMANDS = "'" + CMDLINE_GET_SIGNING_CERTIFICATE + "', '"
149-
+ CMDLINE_AUTHENTICATE + "', '" + CMDLINE_SIGN + "'.";
150+
static const auto COMMANDS = u"'%1', '%2', '%3'."_s.arg(CMDLINE_GET_SIGNING_CERTIFICATE,
151+
CMDLINE_AUTHENTICATE, CMDLINE_SIGN);
150152

151153
parser.addPositionalArgument(
152-
QStringLiteral("command"),
153-
QStringLiteral("The command to execute in command-line mode, any of ") + COMMANDS);
154-
parser.addPositionalArgument(
155-
QStringLiteral("arguments"),
156-
QStringLiteral("Arguments to the given command as a JSON-encoded string."));
154+
u"command"_s, u"The command to execute in command-line mode, any of "_s + COMMANDS,
155+
u"(%1|%2|%3)"_s.arg(CMDLINE_GET_SIGNING_CERTIFICATE, CMDLINE_AUTHENTICATE, CMDLINE_SIGN));
156+
parser.addPositionalArgument(u"arguments"_s,
157+
u"Arguments to the given command as a JSON-encoded string."_s);
157158

158159
parser.process(arguments());
159160

160-
if (parser.isSet(QStringLiteral("command-line-mode"))) {
161+
if (parser.isSet(commandLineMode)) {
161162
const auto args = parser.positionalArguments();
162163
if (args.size() != 2) {
163164
throw ArgumentError("Provide two positional arguments in command-line mode.");

src/controller/commands.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@
2727
#include <stdexcept>
2828
#include <map>
2929

30-
const QString CMDLINE_GET_SIGNING_CERTIFICATE = QStringLiteral("get-signing-certificate");
31-
const QString CMDLINE_AUTHENTICATE = QStringLiteral("authenticate");
32-
const QString CMDLINE_SIGN = QStringLiteral("sign");
3330
// A special command for stdin mode for quitting the application after sending the version.
34-
const QString STDINMODE_QUIT = QStringLiteral("quit");
31+
constexpr QStringView STDINMODE_QUIT {u"quit"};
3532

3633
CommandType::CommandType(const QString& cmdName)
3734
{
38-
static const std::map<QString, CommandType> SUPPORTED_COMMANDS {
35+
static const std::map<QStringView, CommandType> SUPPORTED_COMMANDS {
3936
{CMDLINE_GET_SIGNING_CERTIFICATE, CommandType::GET_SIGNING_CERTIFICATE},
4037
{CMDLINE_AUTHENTICATE, CommandType::AUTHENTICATE},
4138
{CMDLINE_SIGN, CommandType::SIGN},

src/controller/commands.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class CommandType
5454
CommandTypeEnum value;
5555
};
5656

57-
extern const QString CMDLINE_GET_SIGNING_CERTIFICATE;
58-
extern const QString CMDLINE_AUTHENTICATE;
59-
extern const QString CMDLINE_SIGN;
57+
constexpr QStringView CMDLINE_GET_SIGNING_CERTIFICATE {u"get-signing-certificate"};
58+
constexpr QStringView CMDLINE_AUTHENTICATE {u"authenticate"};
59+
constexpr QStringView CMDLINE_SIGN {u"sign"};
6060

6161
using CommandWithArguments = std::pair<CommandType, QVariantMap>;
6262
using CommandWithArgumentsPtr = std::unique_ptr<CommandWithArguments>;

src/controller/logging.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include "logging.hpp"
2424

25+
#include "utils/qt_comp.hpp"
26+
2527
#include <QCoreApplication>
2628
#include <QDateTime>
2729
#include <QDir>

src/controller/logging.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@
2626

2727
void setupLogging();
2828

29-
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
30-
template <typename... Args>
31-
inline QDebug operator<<(QDebug out, const std::basic_string<char, Args...>& s)
32-
{
33-
return out << QUtf8StringView(s);
34-
}
35-
#endif
36-
3729
inline QDebug operator<<(QDebug out, const std::exception& e)
3830
{
3931
out << e.what();

src/controller/utils/qt_comp.hpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-FileCopyrightText: Estonian Information System Authority
2+
// SPDX-License-Identifier: MIT
3+
4+
#pragma once
5+
6+
#include <QString>
7+
8+
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
9+
namespace Qt::Literals::StringLiterals
10+
{
11+
12+
constexpr inline QLatin1String operator"" _L1(const char* str, size_t size) noexcept
13+
{
14+
return QLatin1String(str, qsizetype(size));
15+
}
16+
17+
inline QString operator""_s(const char16_t* str, size_t size) noexcept
18+
{
19+
return QString(QStringPrivate(nullptr, const_cast<char16_t*>(str), qsizetype(size)));
20+
}
21+
22+
constexpr QStringView operator""_sv(const char16_t* str, size_t size) noexcept
23+
{
24+
return QStringView(str, qsizetype(size));
25+
}
26+
27+
} // namespace Qt::Literals::StringLiterals
28+
#endif
29+
30+
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
31+
template <typename... Args>
32+
inline QDebug operator<<(QDebug out, const std::basic_string<char, Args...>& s)
33+
{
34+
return out << QUtf8StringView(s);
35+
}
36+
#endif

src/ui/webeiddialog.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "webeiddialog.hpp"
2424
#include "application.hpp"
2525
#include "languageselect.hpp"
26+
#include "utils/qt_comp.hpp"
2627

2728
#include "ui_dialog.h"
2829

@@ -43,19 +44,7 @@
4344
#include <unistd.h>
4445
#endif
4546

46-
#if QT_VERSION < QT_VERSION_CHECK(6, 4, 0)
47-
constexpr inline QLatin1String operator"" _L1(const char* str, size_t size) noexcept
48-
{
49-
return QLatin1String(str, int(size));
50-
}
51-
52-
inline QString operator""_s(const char16_t* str, size_t size) noexcept
53-
{
54-
return QString(QStringPrivate(nullptr, const_cast<char16_t*>(str), qsizetype(size)));
55-
}
56-
#else
5747
using namespace Qt::Literals::StringLiterals;
58-
#endif
5948

6049
using namespace electronic_id;
6150

0 commit comments

Comments
 (0)