From 4eb8be372e144d87c7cd6049a0cac5499824d403 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 30 May 2010 14:46:47 +0000 Subject: [PATCH] Detect Python interpreter on Windows (the search engine is now working!) --- src/preferences.h | 12 ++++++++ src/searchengine.cpp | 70 +++++++++++++++++++++++++++++++++++++++--- src/searchengine.h | 3 ++ src/src.pro | 2 +- src/supportedengines.h | 3 ++ 5 files changed, 84 insertions(+), 6 deletions(-) diff --git a/src/preferences.h b/src/preferences.h index fb7085671..71a752779 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -916,6 +916,18 @@ public: settings.setValue(QString::fromUtf8("Preferences/Connection/ResolvePeerHostNames"), resolve); } +#ifdef Q_WS_WIN + static void setPythonPath(QString path) { + QSettings settings("qBittorrent", "qBittorrent"); + settings.setValue(QString::fromUtf8("Preferences/Win32/PythonPath"), path); + } + + static QString getPythonPath() { + QSettings settings("qBittorrent", "qBittorrent"); + return settings.value(QString::fromUtf8("Preferences/Win32/PythonPath"), "").toString(); + } +#endif + }; #endif // PREFERENCES_H diff --git a/src/searchengine.cpp b/src/searchengine.cpp index 1a133f48f..c6003db05 100644 --- a/src/searchengine.cpp +++ b/src/searchengine.cpp @@ -42,11 +42,17 @@ #include #include #include +#include + +#ifdef Q_WS_WIN +#include +#endif #include "searchengine.h" #include "bittorrent.h" #include "downloadthread.h" #include "misc.h" +#include "preferences.h" #include "searchlistdelegate.h" #include "GUI.h" @@ -72,13 +78,11 @@ SearchEngine::SearchEngine(GUI *parent, Bittorrent *BTSession) : QWidget(parent) // Boolean initialization search_stopped = false; // Creating Search Process +#ifdef Q_WS_WIN + checkForPythonExe(); +#endif searchProcess = new QProcess(this); QStringList env = QProcess::systemEnvironment(); -#ifdef Q_WS_WIN - // add qBittorrent executable folder to PATH environment variable - qDebug("qBittorrent executable path: %s", qPrintable(qApp->applicationDirPath())); - env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), "PATH=\\1;"+qApp->applicationDirPath()); -#endif searchProcess->setEnvironment(env); connect(searchProcess, SIGNAL(started()), this, SLOT(searchStarted())); connect(searchProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readSearchOutput())); @@ -106,6 +110,62 @@ void SearchEngine::fillCatCombobox() { } } +#ifdef Q_WS_WIN +void SearchEngine::checkForPythonExe() { + QString python_path = Preferences::getPythonPath(); + if(python_path.isEmpty() || !QFile::exists(python_path+QDir::separator()+"python.exe")) { + // Attempt to detect python in standard location + QStringList filters; + filters << "Python25" << "Python26"; + QStringList python_folders = QDir::root().entryList(filters, QDir::Dirs, QDir::Name); + if(!python_folders.isEmpty()) { + python_path = QDir::root().absoluteFilePath(python_folders.last()); + qDebug("Detected python folder at %s", qPrintable(python_path)); + } else { + filters.clear(); + filters << "Python*"; + python_folders = QDir::root().entryList(filters, QDir::Dirs, QDir::Name); + if(!python_folders.isEmpty()) { + python_path = QDir::root().absoluteFilePath(python_folders.last()); + qDebug("Detected python folder at %s", qPrintable(python_path)); + } else { + qDebug("Failed to detect Python folder"); + } + } + } + if(python_path.isEmpty() || !QFile::exists(python_path+QDir::separator()+"python.exe")) { + QMessageBox::warning(0, tr("Failed to locate the Python interpreter"), tr("The Python interpreter was not found.\nqBittorrent will now ask you to point to its correct location.")); + QString python_exe_path = QFileDialog::getOpenFileName(0, tr("Please point to its location on your hard disk."), + QDir::root().absolutePath(), tr("Python executable (python.exe)")); + if(python_exe_path.isEmpty() || !QFile::exists(python_exe_path)) { + QMessageBox::warning(0, tr("No Python interpreter"), tr("The Python interpreter is missing. qBittorrent search engine will not work.")); + return; + } + qDebug("Python exe path is: %s", qPrintable(python_exe_path)); + QStringList tmp_list = python_exe_path.split(QDir::separator()); + if(tmp_list.size() == 1) + tmp_list = tmp_list.first().split("/"); + tmp_list.removeLast(); + python_path = tmp_list.join(QDir::separator()); + qDebug("New Python path is: %s", qPrintable(python_path)); + // Save python path + Preferences::setPythonPath(python_path); + } + // Add it to PATH envvar + QString path_envar = QString::fromLocal8Bit(getenv("PATH")); + if(path_envar.isNull()) { + path_envar = ""; + } else { + if(!path_envar.endsWith(";")) + path_envar += ";"; + } + path_envar += python_path+";"; + qDebug("New PATH envvar is: %s", qPrintable(path_envar)); + QString envar = "PATH="+path_envar; + putenv(envar.toLocal8Bit().data()); +} +#endif + QString SearchEngine::selectedCategory() const { return comboCategory->itemData(comboCategory->currentIndex()).toString(); } diff --git a/src/searchengine.h b/src/searchengine.h index c8343e5b4..0eafa817e 100644 --- a/src/searchengine.h +++ b/src/searchengine.h @@ -126,6 +126,9 @@ protected slots: void createCompleter(); void fillCatCombobox(); void searchTextEdited(QString); +#ifdef Q_WS_WIN + void checkForPythonExe(); +#endif }; #endif diff --git a/src/src.pro b/src/src.pro index 33928c7b0..5deaedf32 100644 --- a/src/src.pro +++ b/src/src.pro @@ -3,7 +3,7 @@ LANG_PATH = lang ICONS_PATH = Icons # Set the following variable to 1 to enable debug -DEBUG_MODE = 1 +DEBUG_MODE = 0 # Global TEMPLATE = app diff --git a/src/supportedengines.h b/src/supportedengines.h index 04a93c5fe..884a90403 100644 --- a/src/supportedengines.h +++ b/src/supportedengines.h @@ -39,6 +39,7 @@ #include #include #include +#include #include "misc.h" @@ -139,6 +140,7 @@ public: public slots: void update() { QProcess nova; + nova.setEnvironment(QProcess::systemEnvironment()); QStringList params; params << misc::searchEngineLocation()+QDir::separator()+"nova2.py"; params << "--capabilities"; @@ -149,6 +151,7 @@ public slots: QDomDocument xml_doc; if(!xml_doc.setContent(capabilities)) { std::cerr << "Could not parse Nova search engine capabilities, msg: " << capabilities.toLocal8Bit().data() << std::endl; + std::cerr << "Error: " << nova.readAllStandardError().constData() << std::endl; return; } QDomElement root = xml_doc.documentElement();