Implement instance creation.
This commit is contained in:
parent
ff3078b3a6
commit
1626fa013c
@ -147,7 +147,50 @@ void MainWindow::instanceActivated ( QModelIndex index )
|
|||||||
void MainWindow::on_actionAddInstance_triggered()
|
void MainWindow::on_actionAddInstance_triggered()
|
||||||
{
|
{
|
||||||
NewInstanceDialog *newInstDlg = new NewInstanceDialog ( this );
|
NewInstanceDialog *newInstDlg = new NewInstanceDialog ( this );
|
||||||
newInstDlg->exec();
|
if (newInstDlg->exec())
|
||||||
|
{
|
||||||
|
Instance *newInstance = NULL;
|
||||||
|
|
||||||
|
QString instDirName = DirNameFromString(newInstDlg->instName());
|
||||||
|
QString instDir = PathCombine(globalSettings->get("InstanceDir").toString(),
|
||||||
|
instDirName);
|
||||||
|
|
||||||
|
InstanceLoader::InstTypeError error = InstanceLoader::get().
|
||||||
|
createInstance(newInstance, newInstDlg->selectedType(), instDir);
|
||||||
|
|
||||||
|
if (error == InstanceLoader::NoError)
|
||||||
|
{
|
||||||
|
newInstance->setName(newInstDlg->instName());
|
||||||
|
instList.add(InstancePtr(newInstance));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
QString errorMsg = QString("Failed to create instance %1: ").
|
||||||
|
arg(instDirName);
|
||||||
|
|
||||||
|
switch (error)
|
||||||
|
{
|
||||||
|
case InstanceLoader::TypeNotRegistered:
|
||||||
|
errorMsg += "Instance type not found.";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case InstanceLoader::InstExists:
|
||||||
|
errorMsg += "An instance with the given directory name already exists.";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case InstanceLoader::CantCreateDir:
|
||||||
|
errorMsg += "Failed to create the instance directory.";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
errorMsg += QString("Unknown instance loader error %1").
|
||||||
|
arg(error);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
QMessageBox::warning(this, "Error", errorMsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::on_actionChangeInstGroup_triggered()
|
void MainWindow::on_actionChangeInstGroup_triggered()
|
||||||
|
@ -74,6 +74,8 @@ void NewInstanceDialog::updateSelectedType()
|
|||||||
{
|
{
|
||||||
if (!m_selectedType->versionList()->isLoaded())
|
if (!m_selectedType->versionList()->isLoaded())
|
||||||
loadVersionList();
|
loadVersionList();
|
||||||
|
|
||||||
|
setSelectedVersion(m_selectedType->versionList()->getLatestStable());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,6 +114,27 @@ void NewInstanceDialog::loadVersionList()
|
|||||||
setSelectedVersion(m_selectedType->versionList()->getLatestStable());
|
setSelectedVersion(m_selectedType->versionList()->getLatestStable());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString NewInstanceDialog::instName() const
|
||||||
|
{
|
||||||
|
return ui->instNameTextBox->text();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString NewInstanceDialog::iconKey() const
|
||||||
|
{
|
||||||
|
// TODO: Implement icon stuff.
|
||||||
|
return "default";
|
||||||
|
}
|
||||||
|
|
||||||
|
const InstanceTypeInterface *NewInstanceDialog::selectedType() const
|
||||||
|
{
|
||||||
|
return m_selectedType;
|
||||||
|
}
|
||||||
|
|
||||||
|
const InstVersion *NewInstanceDialog::selectedVersion() const
|
||||||
|
{
|
||||||
|
return m_selectedVersion;
|
||||||
|
}
|
||||||
|
|
||||||
void NewInstanceDialog::on_btnChangeVersion_clicked()
|
void NewInstanceDialog::on_btnChangeVersion_clicked()
|
||||||
{
|
{
|
||||||
if (m_selectedType)
|
if (m_selectedType)
|
||||||
|
@ -41,6 +41,11 @@ public:
|
|||||||
|
|
||||||
void loadVersionList();
|
void loadVersionList();
|
||||||
|
|
||||||
|
QString instName() const;
|
||||||
|
QString iconKey() const;
|
||||||
|
const InstanceTypeInterface *selectedType() const;
|
||||||
|
const InstVersion *selectedVersion() const;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_btnChangeVersion_clicked();
|
void on_btnChangeVersion_clicked();
|
||||||
|
|
||||||
|
@ -25,4 +25,8 @@ LIBUTIL_EXPORT QString PathCombine(QString path1, QString path2, QString path3);
|
|||||||
|
|
||||||
LIBUTIL_EXPORT QString AbsolutePath(QString path);
|
LIBUTIL_EXPORT QString AbsolutePath(QString path);
|
||||||
|
|
||||||
|
LIBUTIL_EXPORT QString RemoveInvalidFilenameChars(QString string, QChar replaceWith = '-');
|
||||||
|
|
||||||
|
LIBUTIL_EXPORT QString DirNameFromString(QString string, QString inDir = ".");
|
||||||
|
|
||||||
#endif // PATHUTILS_H
|
#endif // PATHUTILS_H
|
||||||
|
@ -35,3 +35,33 @@ QString AbsolutePath(QString path)
|
|||||||
{
|
{
|
||||||
return QFileInfo(path).absolutePath();
|
return QFileInfo(path).absolutePath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString badFilenameChars = "\"\\/?<>:*|!";
|
||||||
|
|
||||||
|
QString RemoveInvalidFilenameChars(QString string, QChar replaceWith)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < string.length(); i++)
|
||||||
|
{
|
||||||
|
if (badFilenameChars.contains(string[i]))
|
||||||
|
{
|
||||||
|
string[i] = replaceWith;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DirNameFromString(QString string, QString inDir)
|
||||||
|
{
|
||||||
|
int num = 0;
|
||||||
|
QString dirName = RemoveInvalidFilenameChars(string, '-');
|
||||||
|
while (QFileInfo(PathCombine(inDir, dirName)).exists())
|
||||||
|
{
|
||||||
|
num++;
|
||||||
|
dirName = RemoveInvalidFilenameChars(dirName, '-') + QString::number(num);
|
||||||
|
|
||||||
|
// If it's over 9000
|
||||||
|
if (num > 9000)
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return dirName;
|
||||||
|
}
|
||||||
|
@ -35,17 +35,16 @@ InstVersionList *StdInstanceType::versionList() const
|
|||||||
InstanceLoader::InstTypeError StdInstanceType::createInstance(Instance *&inst,
|
InstanceLoader::InstTypeError StdInstanceType::createInstance(Instance *&inst,
|
||||||
const QString &instDir) const
|
const QString &instDir) const
|
||||||
{
|
{
|
||||||
QFileInfo rootDir(instDir);
|
QDir rootDir(instDir);
|
||||||
|
|
||||||
if (!rootDir.exists() && !QDir().mkdir(rootDir.path()))
|
qDebug(instDir.toUtf8());
|
||||||
|
if (!rootDir.exists() && !rootDir.mkpath("."))
|
||||||
{
|
{
|
||||||
return InstanceLoader::CantCreateDir;
|
return InstanceLoader::CantCreateDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
StdInstance *stdInst = new StdInstance(instDir, this);
|
StdInstance *stdInst = new StdInstance(instDir, this);
|
||||||
|
|
||||||
// TODO: Verify that the instance is valid.
|
|
||||||
|
|
||||||
inst = stdInst;
|
inst = stdInst;
|
||||||
|
|
||||||
return InstanceLoader::NoError;
|
return InstanceLoader::NoError;
|
||||||
|
Loading…
Reference in New Issue
Block a user