diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-05-25 02:12:11 -0400 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-05-25 02:12:11 -0400 |
| commit | 9a014db4b3a64dc93450dff8afe980db3694c595 (patch) | |
| tree | 587ce82229f3f7c5ce6ab61ec4d35946dbc33afb /src/organizercore.cpp | |
| parent | f5330efd0d2692eb14738d8ccbe4e269ce165b46 (diff) | |
moved GetFileExecutionContext() out of OrganizerCore, no reason for it to be there
added ExploreFile(), changed all mentions of ShellExecute() in MainWindow to use it
Diffstat (limited to 'src/organizercore.cpp')
| -rw-r--r-- | src/organizercore.cpp | 150 |
1 files changed, 86 insertions, 64 deletions
diff --git a/src/organizercore.cpp b/src/organizercore.cpp index bdaf4ffc..2542545f 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -267,6 +267,91 @@ bool checkService() return serviceRunning; } + +bool GetFileExecutionContext( + QWidget* parent, const QFileInfo &targetInfo, + QFileInfo &binaryInfo, QString &arguments, FileExecutionTypes& type) +{ + QString extension = targetInfo.suffix(); + if ((extension.compare("cmd", Qt::CaseInsensitive) == 0) || + (extension.compare("com", Qt::CaseInsensitive) == 0) || + (extension.compare("bat", Qt::CaseInsensitive) == 0)) { + binaryInfo = QFileInfo("C:\\Windows\\System32\\cmd.exe"); + arguments = QString("/C \"%1\"").arg(QDir::toNativeSeparators(targetInfo.absoluteFilePath())); + type = FileExecutionTypes::executable; + return true; + } else if (extension.compare("exe", Qt::CaseInsensitive) == 0) { + binaryInfo = targetInfo; + type = FileExecutionTypes::executable; + return true; + } else if (extension.compare("jar", Qt::CaseInsensitive) == 0) { + // types that need to be injected into + std::wstring targetPathW = targetInfo.absoluteFilePath().toStdWString(); + QString binaryPath; + + { // try to find java automatically + WCHAR buffer[MAX_PATH]; + if (::FindExecutableW(targetPathW.c_str(), nullptr, buffer) > (HINSTANCE)32) { + DWORD binaryType = 0UL; + if (!::GetBinaryTypeW(buffer, &binaryType)) { + qDebug("failed to determine binary type of \"%ls\": %lu", buffer, ::GetLastError()); + } else if (binaryType == SCS_32BIT_BINARY) { + binaryPath = QString::fromWCharArray(buffer); + } + } + } + if (binaryPath.isEmpty() && (extension == "jar")) { + // second attempt: look to the registry + QSettings javaReg("HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Java Runtime Environment", QSettings::NativeFormat); + if (javaReg.contains("CurrentVersion")) { + QString currentVersion = javaReg.value("CurrentVersion").toString(); + binaryPath = javaReg.value(QString("%1/JavaHome").arg(currentVersion)).toString().append("\\bin\\javaw.exe"); + } + } + if (binaryPath.isEmpty()) { + binaryPath = QFileDialog::getOpenFileName( + parent, QObject::tr("Select binary"), QString(), QObject::tr("Binary") + " (*.exe)"); + } + if (binaryPath.isEmpty()) { + return false; + } + binaryInfo = QFileInfo(binaryPath); + if (extension == "jar") { + arguments = QString("-jar \"%1\"").arg(QDir::toNativeSeparators(targetInfo.absoluteFilePath())); + } else { + arguments = QString("\"%1\"").arg(QDir::toNativeSeparators(targetInfo.absoluteFilePath())); + } + + type = FileExecutionTypes::executable; + return true; + } else { + type = FileExecutionTypes::other; + return true; + } +} + +bool ExploreFile(const QString& path) +{ + const auto ws = path.toStdWString(); + + const auto h = ::ShellExecuteW( + nullptr, L"explore", ws.c_str(), nullptr, nullptr, SW_SHOWNORMAL); + + // anything <= 32 is not an actual HINSTANCE and signals failure + return (h > reinterpret_cast<HINSTANCE>(32)); +} + +bool ExploreFile(const QFileInfo& info) +{ + return ExploreFile(info.absolutePath()); +} + +bool ExploreFile(const QDir& dir) +{ + return ExploreFile(dir.absolutePath()); +} + + OrganizerCore::OrganizerCore(const QSettings &initSettings) : m_UserInterface(nullptr) , m_PluginContainer(nullptr) @@ -1220,76 +1305,13 @@ QStringList OrganizerCore::modsSortedByProfilePriority() const return res; } - -bool OrganizerCore::getFileExecutionContext( - QWidget* parent, const QFileInfo &targetInfo, - QFileInfo &binaryInfo, QString &arguments, FileExecutionTypes& type) -{ - QString extension = targetInfo.suffix(); - if ((extension.compare("cmd", Qt::CaseInsensitive) == 0) || - (extension.compare("com", Qt::CaseInsensitive) == 0) || - (extension.compare("bat", Qt::CaseInsensitive) == 0)) { - binaryInfo = QFileInfo("C:\\Windows\\System32\\cmd.exe"); - arguments = QString("/C \"%1\"").arg(QDir::toNativeSeparators(targetInfo.absoluteFilePath())); - type = FileExecutionTypes::executable; - return true; - } else if (extension.compare("exe", Qt::CaseInsensitive) == 0) { - binaryInfo = targetInfo; - type = FileExecutionTypes::executable; - return true; - } else if (extension.compare("jar", Qt::CaseInsensitive) == 0) { - // types that need to be injected into - std::wstring targetPathW = ToWString(targetInfo.absoluteFilePath()); - QString binaryPath; - - { // try to find java automatically - WCHAR buffer[MAX_PATH]; - if (::FindExecutableW(targetPathW.c_str(), nullptr, buffer) > (HINSTANCE)32) { - DWORD binaryType = 0UL; - if (!::GetBinaryTypeW(buffer, &binaryType)) { - qDebug("failed to determine binary type of \"%ls\": %lu", buffer, ::GetLastError()); - } else if (binaryType == SCS_32BIT_BINARY) { - binaryPath = ToQString(buffer); - } - } - } - if (binaryPath.isEmpty() && (extension == "jar")) { - // second attempt: look to the registry - QSettings javaReg("HKEY_LOCAL_MACHINE\\Software\\JavaSoft\\Java Runtime Environment", QSettings::NativeFormat); - if (javaReg.contains("CurrentVersion")) { - QString currentVersion = javaReg.value("CurrentVersion").toString(); - binaryPath = javaReg.value(QString("%1/JavaHome").arg(currentVersion)).toString().append("\\bin\\javaw.exe"); - } - } - if (binaryPath.isEmpty()) { - binaryPath = QFileDialog::getOpenFileName( - parent, QObject::tr("Select binary"), QString(), QObject::tr("Binary") + " (*.exe)"); - } - if (binaryPath.isEmpty()) { - return false; - } - binaryInfo = QFileInfo(binaryPath); - if (extension == "jar") { - arguments = QString("-jar \"%1\"").arg(QDir::toNativeSeparators(targetInfo.absoluteFilePath())); - } else { - arguments = QString("\"%1\"").arg(QDir::toNativeSeparators(targetInfo.absoluteFilePath())); - } - - type = FileExecutionTypes::executable; - return true; - } else { - type = FileExecutionTypes::other; - return true; - } -} - bool OrganizerCore::executeFile(QWidget* parent, const QFileInfo& targetInfo) { QFileInfo binaryInfo; QString arguments; FileExecutionTypes type; - if (!getFileExecutionContext(parent, targetInfo, binaryInfo, arguments, type)) { + if (!GetFileExecutionContext(parent, targetInfo, binaryInfo, arguments, type)) { return false; } |
