Windows QPA: Do not use QSettings to access the registry

When commenting out the warning in createOrOpenKey()
(src\corelib\io\qsettings_win.cpp:157), applications produce warnings:
QSettings: Failed to create subkey "Software\Microsoft\Windows NT\CurrentVersion\FontSubstitutes": Access is denied.
indicating that an attempt to open the registry in read/write mode fails.
Add a utility function to read out registry strings in read-only mode to
the font database and use that instead.

Change-Id: I4187344cac7ec2ba27f15b51e237575efc171853
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Friedemann Kleint 2016-10-28 14:11:21 +02:00
parent e5451f086b
commit 8cd8124559
5 changed files with 41 additions and 17 deletions

View File

@ -1981,4 +1981,26 @@ int QWindowsFontDatabase::defaultVerticalDPI()
return vDPI;
}
QString QWindowsFontDatabase::readRegistryString(HKEY parentHandle, const wchar_t *keyPath, const wchar_t *keyName)
{
QString result;
HKEY handle = 0;
if (RegOpenKeyEx(parentHandle, keyPath, 0, KEY_READ, &handle) == ERROR_SUCCESS) {
// get the size and type of the value
DWORD dataType;
DWORD dataSize;
if (RegQueryValueEx(handle, keyName, 0, &dataType, 0, &dataSize) == ERROR_SUCCESS) {
if (dataType == REG_SZ || dataType == REG_EXPAND_SZ) {
dataSize += 2; // '\0' missing?
QVarLengthArray<unsigned char> data(dataSize);
data[dataSize - 2] = data[dataSize - 1] = '\0';
if (RegQueryValueEx(handle, keyName, 0, 0, data.data(), &dataSize) == ERROR_SUCCESS)
result = QString::fromWCharArray(reinterpret_cast<const wchar_t *>(data.data()));
}
}
RegCloseKey(handle);
}
return result;
}
QT_END_NAMESPACE

View File

@ -130,6 +130,8 @@ public:
static void setFontOptions(unsigned options);
static unsigned fontOptions();
static QString readRegistryString(HKEY parentHandle, const wchar_t *keyPath, const wchar_t *keyName);
private:
void populateFamily(const QString &familyName, bool registerAlias);
void removeApplicationFonts();

View File

@ -42,7 +42,6 @@
#include "qwindowsfontenginedirectwrite_p.h"
#include "qwindowsfontdatabase_p.h"
#include <QtCore/QSettings>
#include <QtCore/QtEndian>
#include <QtCore/QVarLengthArray>
#include <QtCore/QFile>
@ -915,9 +914,11 @@ void QWindowsFontEngineDirectWrite::initFontInfo(const QFontDef &request,
QString QWindowsFontEngineDirectWrite::fontNameSubstitute(const QString &familyName)
{
static const char keyC[] = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\"
"FontSubstitutes";
return QSettings(QLatin1String(keyC), QSettings::NativeFormat).value(familyName, familyName).toString();
const wchar_t key[] = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes";
const QString substitute =
QWindowsFontDatabase::readRegistryString(HKEY_LOCAL_MACHINE, key,
reinterpret_cast<const wchar_t *>(familyName.utf16()));
return substitute.isEmpty() ? familyName : substitute;
}
glyph_metrics_t QWindowsFontEngineDirectWrite::alphaMapBoundingBox(glyph_t glyph,

View File

@ -47,13 +47,13 @@
#include <QtCore/qmap.h>
#include <QtCore/qpair.h>
#include <QtCore/qpointer.h>
#include <QtCore/qsettings.h>
#include <QtGui/qaccessible.h>
#include <QtGui/private/qguiapplication_p.h>
#include <qpa/qplatformnativeinterface.h>
#include <qpa/qplatformintegration.h>
#include <QtGui/qwindow.h>
#include <QtGui/qguiapplication.h>
#include <QtFontDatabaseSupport/private/qwindowsfontdatabase_p.h> // registry helper
#include "qwindowsaccessibility.h"
#ifdef Q_CC_MINGW
@ -114,6 +114,14 @@ static inline QString messageBoxAlertSound(const QObject *messageBox)
return QString();
}
static QString soundFileName(const QString &soundName)
{
const QString key = QStringLiteral("AppEvents\\Schemes\\Apps\\.Default\\")
+ soundName + QStringLiteral("\\.Current");
return QWindowsFontDatabase::readRegistryString(HKEY_CURRENT_USER,
reinterpret_cast<const wchar_t *>(key.utf16()), L"");
}
void QWindowsAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event)
{
QString soundName;
@ -134,17 +142,9 @@ void QWindowsAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event)
break;
}
if (!soundName.isEmpty()) {
#ifndef QT_NO_SETTINGS
QSettings settings(QLatin1String("HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\.Default\\") + soundName,
QSettings::NativeFormat);
QString file = settings.value(QLatin1String(".Current/.")).toString();
#else
QString file;
#endif
if (!file.isEmpty()) {
PlaySound(reinterpret_cast<const wchar_t *>(soundName.utf16()), 0, SND_ALIAS | SND_ASYNC | SND_NODEFAULT | SND_NOWAIT);
}
if (!soundName.isEmpty() && !soundFileName(soundName).isEmpty()) {
PlaySound(reinterpret_cast<const wchar_t *>(soundName.utf16()), 0,
SND_ALIAS | SND_ASYNC | SND_NODEFAULT | SND_NOWAIT);
}
// An event has to be associated with a window,

View File

@ -50,7 +50,6 @@
#include <QtCore/qdebug.h>
#include <QtCore/qmap.h>
#include <QtCore/qpair.h>
#include <QtCore/qsettings.h>
#include <QtGui/qaccessible.h>
#include <QtGui/qguiapplication.h>
#include <qpa/qplatformnativeinterface.h>