/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.
This file is part of Mod Organizer.
Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Mod Organizer. If not, see .
*/
#include "editexecutablesdialog.h"
#include "ui_editexecutablesdialog.h"
#include "filedialogmemory.h"
#include
#include
#include
using namespace MOBase;
using namespace MOShared;
EditExecutablesDialog::EditExecutablesDialog(const ExecutablesList &executablesList, QWidget *parent)
: TutorableDialog("EditExecutables", parent),
ui(new Ui::EditExecutablesDialog), m_ExecutablesList(executablesList)
{
ui->setupUi(this);
refreshExecutablesWidget();
}
EditExecutablesDialog::~EditExecutablesDialog()
{
delete ui;
}
ExecutablesList EditExecutablesDialog::getExecutablesList() const
{
ExecutablesList newList;
for (int i = 0; i < ui->executablesListBox->count(); ++i) {
newList.addExecutable(ui->executablesListBox->item(i)->data(Qt::UserRole).value());
}
return newList;
}
void EditExecutablesDialog::refreshExecutablesWidget()
{
QListWidget *executablesWidget = findChild("executablesListBox");
executablesWidget->clear();
std::vector::const_iterator current, end;
m_ExecutablesList.getExecutables(current, end);
for(; current != end; ++current) {
QListWidgetItem *newItem = new QListWidgetItem(current->m_Title);
QVariant temp;
temp.setValue(*current);
newItem->setData(Qt::UserRole, temp);
newItem->setTextColor(current->m_Custom ? QColor(Qt::black) : QColor(Qt::darkGray));
executablesWidget->addItem(newItem);
}
QPushButton *addButton = findChild("addButton");
QPushButton *removeButton = findChild("removeButton");
addButton->setEnabled(false);
removeButton->setEnabled(false);
}
void EditExecutablesDialog::on_binaryEdit_textChanged(const QString &arg1)
{
QPushButton *addButton = findChild("addButton");
// QPushButton *removeButton = findChild("removeButton");
QFileInfo fileInfo(arg1);
addButton->setEnabled(fileInfo.exists() && fileInfo.isFile());
}
void EditExecutablesDialog::resetInput()
{
ui->binaryEdit->setText("");
ui->titleEdit->setText("");
ui->workingDirEdit->clear();
ui->argumentsEdit->setText("");
ui->closeCheckBox->setChecked(false);
}
void EditExecutablesDialog::on_addButton_clicked()
{
QLineEdit *titleEdit = findChild("titleEdit");
QLineEdit *binaryEdit = findChild("binaryEdit");
QLineEdit *argumentsEdit = findChild("argumentsEdit");
QLineEdit *workingDirEdit = findChild("workingDirEdit");
QCheckBox *closeCheckBox = findChild("closeCheckBox");
m_ExecutablesList.addExecutable(titleEdit->text(), QDir::fromNativeSeparators(binaryEdit->text()),
argumentsEdit->text(), QDir::fromNativeSeparators(workingDirEdit->text()),
(closeCheckBox->checkState() == Qt::Checked) ? DEFAULT_CLOSE : DEFAULT_STAY,
ui->overwriteAppIDBox->isChecked() ? ui->appIDOverwriteEdit->text() : "",
true, false);
resetInput();
refreshExecutablesWidget();
}
void EditExecutablesDialog::on_browseButton_clicked()
{
QString binaryName = FileDialogMemory::getOpenFileName("editExecutableBinary", this,
tr("Select a binary"), QString(), tr("Executable (%1)").arg("*.exe *.bat *.jar"));
if (binaryName.endsWith(".jar", Qt::CaseInsensitive)) {
QString binaryPath;
{ // try to find java automatically
std::wstring binaryNameW = ToWString(binaryName);
WCHAR buffer[MAX_PATH];
if (::FindExecutableW(binaryNameW.c_str(), NULL, buffer) > (HINSTANCE)32) {
DWORD binaryType = 0UL;
if (!::GetBinaryTypeW(binaryNameW.c_str(), &binaryType)) {
qDebug("failed to determine binary type: %lu", ::GetLastError());
} else if (binaryType == SCS_32BIT_BINARY) {
binaryPath = ToQString(buffer);
}
}
}
if (binaryPath.isEmpty()) {
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()) {
QMessageBox::information(this, tr("Java (32-bit) required"),
tr("MO requires 32-bit java to run this application. If you already have it installed, select javaw.exe "
"from that installation as the binary."));
} else {
ui->binaryEdit->setText(binaryPath);
}
ui->workingDirEdit->setText(QDir::toNativeSeparators(QFileInfo(binaryName).absolutePath()));
ui->argumentsEdit->setText("-jar \"" + QDir::toNativeSeparators(binaryName) + "\"");
} else {
ui->binaryEdit->setText(QDir::toNativeSeparators(binaryName));
}
}
void EditExecutablesDialog::on_browseDirButton_clicked()
{
QString dirName = FileDialogMemory::getExistingDirectory("editExecutableDirectory", this,
tr("Select a directory"));
ui->workingDirEdit->setText(dirName);
}
void EditExecutablesDialog::on_removeButton_clicked()
{
// QLineEdit *binaryEdit = findChild("binaryEdit");
if (QMessageBox::question(this, tr("Confirm"), tr("Really remove \"%1\" from executables?").arg(ui->titleEdit->text()),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
m_ExecutablesList.remove(ui->titleEdit->text());
}
resetInput();
refreshExecutablesWidget();
}
void EditExecutablesDialog::on_titleEdit_textChanged(const QString &arg1)
{
QPushButton *addButton = findChild("addButton");
QPushButton *removeButton = findChild("removeButton");
QListWidget *executablesWidget = findChild("executablesListBox");
QList existingItems = executablesWidget->findItems(arg1, Qt::MatchFixedString);
addButton->setEnabled(arg1.length() != 0);
if (existingItems.count() == 0) {
addButton->setText(tr("Add"));
removeButton->setEnabled(false);
} else {
// existing item. is it a custom one?
addButton->setText(tr("Modify"));
removeButton->setEnabled(true);
}
}
void EditExecutablesDialog::on_executablesListBox_itemClicked(QListWidgetItem *item)
{
QLineEdit *titleEdit = findChild("titleEdit");
QLineEdit *binaryEdit = findChild("binaryEdit");
QLineEdit *argumentsEdit = findChild("argumentsEdit");
QLineEdit *workingDirEdit = findChild("workingDirEdit");
QPushButton *removeButton = findChild("removeButton");
QCheckBox *closeCheckBox = findChild("closeCheckBox");
const Executable &selectedExecutable = item->data(Qt::UserRole).value();
titleEdit->setText(selectedExecutable.m_Title);
binaryEdit->setText(QDir::toNativeSeparators(selectedExecutable.m_BinaryInfo.absoluteFilePath()));
argumentsEdit->setText(selectedExecutable.m_Arguments);
workingDirEdit->setText(QDir::toNativeSeparators(selectedExecutable.m_WorkingDirectory));
closeCheckBox->setChecked(selectedExecutable.m_CloseMO == DEFAULT_CLOSE);
if (selectedExecutable.m_CloseMO == NEVER_CLOSE) {
closeCheckBox->setEnabled(false);
closeCheckBox->setToolTip(tr("MO must be kept running or this application will not work correctly."));
} else {
closeCheckBox->setEnabled(true);
closeCheckBox->setToolTip(tr("If checked, MO will be closed once the specified executable is run."));
}
removeButton->setEnabled(selectedExecutable.m_Custom);
ui->overwriteAppIDBox->setChecked(selectedExecutable.m_SteamAppID != 0);
if (selectedExecutable.m_SteamAppID != 0) {
ui->appIDOverwriteEdit->setText(selectedExecutable.m_SteamAppID);
}
}
void EditExecutablesDialog::on_overwriteAppIDBox_toggled(bool checked)
{
ui->appIDOverwriteEdit->setEnabled(checked);
}