Browse Source

WINDOWS: Don't create keys in the registry if python isn't found. Closes #1370.

adaptive-webui-19844
sledgehammer999 10 years ago
parent
commit
574abc7cdb
  1. 92
      src/preferences/preferences.cpp

92
src/preferences/preferences.cpp

@ -47,6 +47,7 @@
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <ShlObj.h> #include <ShlObj.h>
#include <winreg.h>
#endif #endif
#include "misc.h" #include "misc.h"
@ -55,7 +56,6 @@
Preferences* Preferences::m_instance = 0; Preferences* Preferences::m_instance = 0;
Preferences::Preferences() : dirty(false), lock(QReadWriteLock::Recursive) { Preferences::Preferences() : dirty(false), lock(QReadWriteLock::Recursive) {
QIniSettings *settings = new QIniSettings; QIniSettings *settings = new QIniSettings;
#ifndef Q_OS_MAC #ifndef Q_OS_MAC
@ -1339,30 +1339,104 @@ void Preferences::disableRecursiveDownload(bool disable) {
} }
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
namespace {
QStringList getRegSubkeys(const HKEY &handle) {
QStringList keys;
DWORD subkeys_count = 0;
DWORD max_subkey_len = 0;
long res = ::RegQueryInfoKey(handle, NULL, NULL, NULL, &subkeys_count, &max_subkey_len, NULL, NULL, NULL, NULL, NULL, NULL);
if (res == ERROR_SUCCESS) {
max_subkey_len++; //For null character
LPTSTR key_name = new TCHAR[max_subkey_len];
for (uint i=0; i<subkeys_count; i++) {
res = ::RegEnumKeyEx(handle, 0, key_name, &max_subkey_len, NULL, NULL, NULL, NULL);
if (res == ERROR_SUCCESS)
keys.push_back(QString::fromWCharArray(key_name));
}
delete[] key_name;
}
return keys;
}
QString getRegValue(const HKEY &handle, const QString &name = QString()) {
QString end_result;
DWORD type = 0;
DWORD size = 0;
DWORD array_size = 0;
LPTSTR value_name = NULL;
if (!name.isEmpty()) {
value_name = new TCHAR[name.size()+1];
name.toWCharArray(value_name);
value_name[name.size()] = '\0';
}
// Discover the size of the value
::RegQueryValueEx(handle, value_name, NULL, &type, NULL, &size);
array_size = size / sizeof(TCHAR);
if (size % sizeof(TCHAR))
array_size++;
array_size++; //For null character
LPTSTR value = new TCHAR[array_size];
long res = ::RegQueryValueEx(handle, value_name, NULL, &type, (LPBYTE)value, &size);
if (res == ERROR_SUCCESS) {
value[array_size] = '\0';
end_result = QString::fromWCharArray(value);
}
if (value_name)
delete[] value_name;
if (value)
delete[] value;
return end_result;
}
}
QString Preferences::getPythonPath() { QString Preferences::getPythonPath() {
QSettings reg_python("HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore", QIniSettings::NativeFormat); HKEY key_handle1;
QStringList versions = reg_python.childGroups(); long res = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Python\\PythonCore"), 0, KEY_READ, &key_handle1);
if (res == ERROR_SUCCESS) {
QStringList versions = getRegSubkeys(key_handle1);
qDebug("Python versions nb: %d", versions.size()); qDebug("Python versions nb: %d", versions.size());
//versions = versions.filter(QRegExp("2\\..*"));
versions.sort(); versions.sort();
while(!versions.empty()) { while(!versions.empty()) {
const QString version = versions.takeLast(); const QString version = versions.takeLast()+"\\InstallPath";
HKEY key_handle2;
LPTSTR subkey = new TCHAR[version.size()+1];
version.toWCharArray(subkey);
subkey[version.size()] = '\0';
res = ::RegOpenKeyEx(key_handle1, subkey, 0, KEY_READ, &key_handle2);
delete[] subkey;
if (res == ERROR_SUCCESS) {
qDebug("Detected possible Python v%s location", qPrintable(version)); qDebug("Detected possible Python v%s location", qPrintable(version));
QString path = reg_python.value(version+"/InstallPath/Default", "").toString(); QString path = getRegValue(key_handle2);
::RegCloseKey(key_handle2);
if (!path.isEmpty() && QDir(path).exists("python.exe")) { if (!path.isEmpty() && QDir(path).exists("python.exe")) {
qDebug("Found python.exe at %s", qPrintable(path)); qDebug("Found python.exe at %s", qPrintable(path));
::RegCloseKey(key_handle1);
return path; return path;
} }
} }
else
::RegCloseKey(key_handle2);
}
}
::RegCloseKey(key_handle1);
// Fallback: Detect python from default locations // Fallback: Detect python from default locations
QStringList supported_versions; QStringList supported_versions;
supported_versions << "32" << "31" << "30" << "27" << "26" << "25"; supported_versions << "32" << "31" << "30" << "27" << "26" << "25";
foreach (const QString &v, supported_versions) { foreach (const QString &v, supported_versions) {
if (QFile::exists("C:/Python"+v+"/python.exe")) { if (QFile::exists("C:/Python"+v+"/python.exe"))
reg_python.setValue(v[0]+"."+v[1]+"/InstallPath/Default", QString("C:/Python"+v));
return "C:/Python"+v; return "C:/Python"+v;
} }
}
return QString::null; return QString::null;
} }

Loading…
Cancel
Save