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.
Conflicts: src/preferences/preferences.cpp
This commit is contained in:
parent
1f13dd0cc3
commit
5740c933fb
1 changed files with 93 additions and 15 deletions
|
@ -49,6 +49,10 @@
|
||||||
#include "fs_utils.h"
|
#include "fs_utils.h"
|
||||||
#include "qinisettings.h"
|
#include "qinisettings.h"
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
#include <winreg.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define QBT_REALM "Web UI Access"
|
#define QBT_REALM "Web UI Access"
|
||||||
enum scheduler_days { EVERY_DAY, WEEK_DAYS, WEEK_ENDS, MON, TUE, WED, THU, FRI, SAT, SUN };
|
enum scheduler_days { EVERY_DAY, WEEK_DAYS, WEEK_ENDS, MON, TUE, WED, THU, FRI, SAT, SUN };
|
||||||
enum maxRatioAction {PAUSE_ACTION, REMOVE_ACTION};
|
enum maxRatioAction {PAUSE_ACTION, REMOVE_ACTION};
|
||||||
|
@ -1219,28 +1223,44 @@ public:
|
||||||
|
|
||||||
#ifdef Q_WS_WIN
|
#ifdef Q_WS_WIN
|
||||||
static QString getPythonPath() {
|
static QString 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().replace("/", "\\");
|
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;
|
||||||
}
|
}
|
||||||
|
@ -1379,6 +1399,64 @@ public:
|
||||||
void setTrayIconStyle(TrayIcon::Style style) {
|
void setTrayIconStyle(TrayIcon::Style style) {
|
||||||
setValue(QString::fromUtf8("Preferences/Advanced/TrayIconStyle"), style);
|
setValue(QString::fromUtf8("Preferences/Advanced/TrayIconStyle"), style);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
private:
|
||||||
|
static 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static 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;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PREFERENCES_H
|
#endif // PREFERENCES_H
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue