summaryrefslogtreecommitdiff
path: root/src/organizercore.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/organizercore.cpp')
-rw-r--r--src/organizercore.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/organizercore.cpp b/src/organizercore.cpp
index 8212d248..bdaf4ffc 100644
--- a/src/organizercore.cpp
+++ b/src/organizercore.cpp
@@ -1220,6 +1220,102 @@ 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)) {
+ return false;
+ }
+
+ switch (type)
+ {
+ case FileExecutionTypes::executable: {
+ spawnBinaryDirect(
+ binaryInfo, arguments, currentProfile()->name(),
+ targetInfo.absolutePath(), "", "");
+
+ return true;
+ }
+
+ case FileExecutionTypes::other: {
+ ::ShellExecuteW(nullptr, L"open",
+ ToWString(targetInfo.absoluteFilePath()).c_str(),
+ nullptr, nullptr, SW_SHOWNORMAL);
+
+ return true;
+ }
+ }
+
+ // nop
+ return false;
+}
+
void OrganizerCore::spawnBinary(const QFileInfo &binary,
const QString &arguments,
const QDir &currentDirectory,