mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-20 21:33:27 -07:00
WINDOWS: Don't create keys in the registry if python isn't found. Closes #1370.
This commit is contained in:
parent
0799dc293c
commit
574abc7cdb
1 changed files with 90 additions and 16 deletions
|
@ -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,29 +1339,103 @@ 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);
|
||||||
qDebug("Python versions nb: %d", versions.size());
|
if (res == ERROR_SUCCESS) {
|
||||||
//versions = versions.filter(QRegExp("2\\..*"));
|
QStringList versions = getRegSubkeys(key_handle1);
|
||||||
versions.sort();
|
qDebug("Python versions nb: %d", versions.size());
|
||||||
while(!versions.empty()) {
|
versions.sort();
|
||||||
const QString version = versions.takeLast();
|
|
||||||
qDebug("Detected possible Python v%s location", qPrintable(version));
|
while(!versions.empty()) {
|
||||||
QString path = reg_python.value(version+"/InstallPath/Default", "").toString();
|
const QString version = versions.takeLast()+"\\InstallPath";
|
||||||
if (!path.isEmpty() && QDir(path).exists("python.exe")) {
|
HKEY key_handle2;
|
||||||
qDebug("Found python.exe at %s", qPrintable(path));
|
LPTSTR subkey = new TCHAR[version.size()+1];
|
||||||
return path;
|
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));
|
||||||
|
QString path = getRegValue(key_handle2);
|
||||||
|
::RegCloseKey(key_handle2);
|
||||||
|
if (!path.isEmpty() && QDir(path).exists("python.exe")) {
|
||||||
|
qDebug("Found python.exe at %s", qPrintable(path));
|
||||||
|
::RegCloseKey(key_handle1);
|
||||||
|
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…
Add table
Add a link
Reference in a new issue