Commit 155d37a9 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Extract functions to get name and email address of user

Use the functions everywhere to get default name and email address for
new keys/certificates.

GnuPG-bug-id: 5181
parent 8fdf3bdd
Pipeline #46315 passed with stage
in 19 minutes and 11 seconds
......@@ -16,7 +16,8 @@ if(WIN32)
utils/gnupg-registry.c
selftest/registrycheck.cpp
utils/windowsprocessdevice.cpp
)
utils/userinfo_win.cpp
)
else()
set(_kleopatra_extra_uiserver_SRCS uiserver/uiserver_unix.cpp)
set(_kleopatra_extra_SRCS)
......@@ -100,6 +101,7 @@ set(_kleopatra_SRCS
utils/remarks.cpp
utils/writecertassuantransaction.cpp
utils/keyparameters.cpp
utils/userinfo.cpp
selftest/selftest.cpp
selftest/enginecheck.cpp
......
......@@ -12,6 +12,7 @@
#include "certificatedetailsinputwidget.h"
#include <utils/userinfo.h>
#include <utils/validation.h>
#include <Libkleo/Dn>
......@@ -19,7 +20,6 @@
#include <Libkleo/Stl_Util>
#include <KConfigGroup>
#include <KEMailSettings>
#include <KLocalizedString>
#include <KSharedConfig>
......@@ -216,7 +216,6 @@ public:
void createForm()
{
const KEMailSettings emailSettings;
const KConfigGroup config(KSharedConfig::openConfig(), "CertificateCreationWizard");
QStringList attrOrder = config.readEntry("DNAttributeOrder", QStringList());
......@@ -235,8 +234,8 @@ public:
if (attr.isEmpty()) {
continue;
}
const QString defaultPreset = (attr == QLatin1String("CN")) ? emailSettings.getSetting(KEMailSettings::RealName) :
(attr == QLatin1String("EMAIL")) ? emailSettings.getSetting(KEMailSettings::EmailAddress) :
const QString defaultPreset = (attr == QLatin1String("CN")) ? userFullName() :
(attr == QLatin1String("EMAIL")) ? userEmailAddress() :
QString();
const QString preset = config.readEntry(attr, defaultPreset);
const bool required = key.endsWith(QLatin1Char('!'));
......
......@@ -9,6 +9,8 @@
#include "gencardkeydialog.h"
#include "utils/userinfo.h"
#include <QDialogButtonBox>
#include <QLineEdit>
#include <QComboBox>
......@@ -18,7 +20,6 @@
#include <QPushButton>
#include <QLabel>
#include <KEMailSettings>
#include <KEmailAddress>
#include <KLocalizedString>
#include <KColorScheme>
......@@ -43,17 +44,16 @@ public:
int row = 0;
const KEMailSettings e;
if (requiredAttributes & KeyOwnerName) {
auto nameLabel = new QLabel(i18n("Name:"));
mNameEdit = new QLineEdit(e.getSetting(KEMailSettings::RealName));
mNameEdit = new QLineEdit(userFullName());
grid->addWidget(nameLabel, row, 0);
grid->addWidget(mNameEdit, row++, 1);
}
if (requiredAttributes & KeyOwnerEmail) {
auto mailLabel = new QLabel(i18n("EMail:"));
mEmailEdit = new QLineEdit(e.getSetting(KEMailSettings::EmailAddress));
mEmailEdit = new QLineEdit(userEmailAddress());
connect(mEmailEdit, &QLineEdit::textChanged, q, [this]() {checkAcceptable();});
mInvalidEmailLabel = new QLabel(QStringLiteral("<font size='small' color='%1'>%2</font>").arg(
KColorScheme(QPalette::Active, KColorScheme::View).foreground(KColorScheme::NegativeText).color().name(), i18n("Invalid EMail")));
......
......@@ -30,6 +30,7 @@
#include "utils/validation.h"
#include "utils/filedialog.h"
#include "utils/keyparameters.h"
#include "utils/userinfo.h"
#include <Libkleo/GnuPG>
#include <Libkleo/Stl_Util>
......@@ -66,16 +67,8 @@
#include <algorithm>
#include <KSharedConfig>
#include <KEMailSettings>
#include <KEmailAddress>
#include <QLocale>
#ifdef Q_OS_WIN
# include <windows.h>
# define SECURITY_WIN32
# include <secext.h> // For GetUserNameEx
#endif
using namespace Kleo;
using namespace Kleo::NewCertificateUi;
using namespace Kleo::Commands;
......@@ -286,55 +279,6 @@ static void parseAlgoString(const QString &algoString, int *size, Subkey::Pubkey
qCWarning(KLEOPATRA_LOG) << "Failed to parse default_pubkey_algo:" << algoString;
}
/* Use Windows API to query the user name and email.
EXTENDED_NAME_FORMAT is documented in MSDN */
#ifdef Q_OS_WIN
static QString win_get_user_name (EXTENDED_NAME_FORMAT what)
{
QString ret;
wchar_t tmp[1];
ULONG nSize = 1;
if (what == NameUnknown) {
if (GetUserNameW (tmp, &nSize)) {
qCWarning (KLEOPATRA_LOG) << "Got empty username";
return ret;
}
} else if (GetUserNameExW (what, tmp, &nSize)) {
return ret;
}
/* nSize now contains the required size of the buffer */
wchar_t *buf = new wchar_t[nSize];
if (what == NameUnknown) {
if (!GetUserNameW (buf, &nSize)) {
qCWarning (KLEOPATRA_LOG) << "Failed to get username";
delete[] buf;
return ret;
}
} else if (!GetUserNameExW (what, buf, &nSize)) {
delete[] buf;
return ret;
}
ret = QString::fromWCharArray (buf, nSize);
delete[] buf;
return ret.trimmed();
}
#endif
static QString env_get_user_name (bool mbox)
{
const auto var = qEnvironmentVariable("EMAIL");
if (!var.isEmpty()) {
QString name, addrspec, comment;
const auto result = KEmailAddress::splitAddress (var, name, addrspec, comment);
if (result == KEmailAddress::AddressOk) {
return (mbox ? addrspec : name);
}
}
return QString ();
}
Q_DECLARE_METATYPE(GpgME::Subkey::PubkeyAlgo)
namespace Kleo
{
......@@ -1543,33 +1487,11 @@ void EnterDetailsPage::updateForm()
widgets.push_back(ui.resultLE);
widgets.push_back(ui.advancedPB);
const KEMailSettings e;
if (ui.nameLE->text().isEmpty()) {
auto name = e.getSetting(KEMailSettings::RealName);
#ifdef Q_OS_WIN
if (name.isEmpty()) {
name = win_get_user_name(NameDisplay);
}
if (name.isEmpty()) {
name = win_get_user_name(NameUnknown);
}
#endif
if (name.isEmpty()) {
name = env_get_user_name (false);
}
ui.nameLE->setText(name);
ui.nameLE->setText(userFullName());
}
if (ui.emailLE->text().isEmpty()) {
auto mbox = e.getSetting(KEMailSettings::EmailAddress);
#ifdef Q_OS_WIN
if (mbox.isEmpty()) {
mbox = win_get_user_name(NameUserPrincipal);
}
#endif
if (mbox.isEmpty()) {
mbox = env_get_user_name (true);
}
ui.emailLE->setText(mbox);
ui.emailLE->setText(userEmailAddress());
}
set_tab_order(widgets);
......
/*
utils/userinfo.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "userinfo.h"
#ifdef Q_OS_WIN
#include "userinfo_win_p.h"
#endif
#include <KEmailAddress>
#include <KEMailSettings>
namespace
{
enum UserInfoDetail {
UserInfoName,
UserInfoEmailAddress
};
static QString env_get_user_name(UserInfoDetail detail)
{
const auto var = qEnvironmentVariable("EMAIL");
if (!var.isEmpty()) {
QString name, addrspec, comment;
const auto result = KEmailAddress::splitAddress (var, name, addrspec, comment);
if (result == KEmailAddress::AddressOk) {
return (detail == UserInfoEmailAddress ? addrspec : name);
}
}
return QString ();
}
}
QString Kleo::userFullName()
{
const KEMailSettings e;
auto name = e.getSetting(KEMailSettings::RealName);
#ifdef Q_OS_WIN
if (name.isEmpty()) {
name = win_get_user_name(NameDisplay);
}
if (name.isEmpty()) {
name = win_get_user_name(NameUnknown);
}
#endif
if (name.isEmpty()) {
name = env_get_user_name(UserInfoName);
}
return name;
}
QString Kleo::userEmailAddress()
{
const KEMailSettings e;
auto mbox = e.getSetting(KEMailSettings::EmailAddress);
#ifdef Q_OS_WIN
if (mbox.isEmpty()) {
mbox = win_get_user_name(NameUserPrincipal);
}
#endif
if (mbox.isEmpty()) {
mbox = env_get_user_name(UserInfoEmailAddress);
}
return mbox;
}
/*
utils/userinfo.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __KLEOPATRA_UTILS_USERINFO_H__
#define __KLEOPATRA_UTILS_USERINFO_H__
class QString;
namespace Kleo
{
QString userFullName();
QString userEmailAddress();
}
#endif // __KLEOPATRA_UTILS_USERINFO_H__
/*
utils/userinfo_win.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "userinfo_win_p.h"
/* Use Windows API to query the user name and email.
EXTENDED_NAME_FORMAT is documented in MSDN */
QString win_get_user_name(EXTENDED_NAME_FORMAT what)
{
QString ret;
wchar_t tmp[1];
ULONG nSize = 1;
if (what == NameUnknown) {
if (GetUserNameW (tmp, &nSize)) {
qCWarning (KLEOPATRA_LOG) << "Got empty username";
return ret;
}
} else if (GetUserNameExW (what, tmp, &nSize)) {
return ret;
}
/* nSize now contains the required size of the buffer */
wchar_t *buf = new wchar_t[nSize];
if (what == NameUnknown) {
if (!GetUserNameW (buf, &nSize)) {
qCWarning (KLEOPATRA_LOG) << "Failed to get username";
delete[] buf;
return ret;
}
} else if (!GetUserNameExW (what, buf, &nSize)) {
delete[] buf;
return ret;
}
ret = QString::fromWCharArray (buf, nSize);
delete[] buf;
return ret.trimmed();
}
/*
utils/userinfo_win_p.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __KLEOPATRA_UTILS_USERINFO_WIN_P_H__
#define __KLEOPATRA_UTILS_USERINFO_WIN_P_H__
#include <windows.h>
#define SECURITY_WIN32
#include <secext.h> // For GetUserNameEx
QString win_get_user_name(EXTENDED_NAME_FORMAT what);
#endif // __KLEOPATRA_UTILS_USERINFO_WIN_P_H__
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment