pollymc/logic/tools/MCEditTool.cpp

125 lines
3.0 KiB
C++
Raw Normal View History

#include "MCEditTool.h"
#include <QDir>
#include <QProcess>
2014-02-22 00:43:12 +05:30
#include <QDesktopServices>
#include <QUrl>
2015-01-28 03:01:07 +05:30
// FIXME: mixing logic and UI!!!!
#include <QInputDialog>
2015-02-09 06:21:14 +05:30
#include <QApplication>
2015-02-09 06:21:14 +05:30
#include "settings/SettingsObject.h"
#include "BaseInstance.h"
#include "minecraft/MinecraftInstance.h"
2015-02-09 06:21:14 +05:30
MCEditTool::MCEditTool(SettingsObjectPtr settings, InstancePtr instance, QObject *parent)
: BaseDetachedTool(settings, instance, parent)
{
}
2015-01-28 03:01:07 +05:30
QString MCEditTool::getSave() const
{
// FIXME: mixing logic and UI!!!!
auto mcInstance = std::dynamic_pointer_cast<MinecraftInstance>(m_instance);
if(!mcInstance)
{
return QString();
}
QDir saves(mcInstance->minecraftRoot() + "/saves");
QStringList worlds = saves.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
QMutableListIterator<QString> it(worlds);
while (it.hasNext())
{
it.next();
if (!QDir(saves.absoluteFilePath(it.value())).exists("level.dat"))
{
it.remove();
}
}
bool ok = true;
2015-02-09 06:21:14 +05:30
const QString save = QInputDialog::getItem(QApplication::activeWindow(), tr("MCEdit"), tr("Choose which world to open:"),
2015-01-28 03:01:07 +05:30
worlds, 0, false, &ok);
if (ok)
{
return saves.absoluteFilePath(save);
}
return QString();
}
void MCEditTool::runImpl()
{
2015-02-09 06:21:14 +05:30
const QString mceditPath = globalSettings->get("MCEditPath").toString();
const QString save = getSave();
if (save.isNull())
{
return;
}
2014-02-22 00:43:12 +05:30
#ifdef Q_OS_OSX
QProcess *process = new QProcess();
connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), process, SLOT(deleteLater()));
process->setProgram(mceditPath);
process->setArguments(QStringList() << save);
process->start();
#else
QDir mceditDir(mceditPath);
QString program;
#ifdef Q_OS_LINUX
if (mceditDir.exists("mcedit.py"))
{
program = mceditDir.absoluteFilePath("mcedit.py");
}
else if (mceditDir.exists("mcedit.pyc"))
{
program = mceditDir.absoluteFilePath("mcedit.pyc");
}
#elif defined(Q_OS_WIN32)
if (mceditDir.exists("mcedit.exe"))
{
program = mceditDir.absoluteFilePath("mcedit.exe");
}
else if (mceditDir.exists("mcedit2.exe"))
{
program = mceditDir.absoluteFilePath("mcedit2.exe");
}
#endif
if(program.size())
{
QProcess::startDetached(program, QStringList() << save, mceditPath);
}
2014-02-22 00:43:12 +05:30
#endif
}
2015-02-09 06:21:14 +05:30
void MCEditFactory::registerSettings(SettingsObjectPtr settings)
{
settings->registerSetting("MCEditPath");
2015-02-09 06:21:14 +05:30
globalSettings = settings;
}
2014-03-30 23:41:05 +05:30
BaseExternalTool *MCEditFactory::createTool(InstancePtr instance, QObject *parent)
{
2015-02-09 06:21:14 +05:30
return new MCEditTool(globalSettings, instance, parent);
}
bool MCEditFactory::check(QString *error)
{
2015-02-09 06:21:14 +05:30
return check(globalSettings->get("MCEditPath").toString(), error);
}
bool MCEditFactory::check(const QString &path, QString *error)
{
if (path.isEmpty())
{
*error = QObject::tr("Path is empty");
return false;
}
const QDir dir(path);
if (!dir.exists())
{
*error = QObject::tr("Path does not exist");
return false;
}
if (!dir.exists("mcedit.pyc") && !dir.exists("mcedit.py") && !dir.exists("mcedit.exe") && !dir.exists("Contents") && !dir.exists("mcedit2.exe"))
{
2014-02-22 00:43:12 +05:30
*error = QObject::tr("Path does not seem to be a MCEdit path");
return false;
}
return true;
}