Import spoiler.xml as spoiler.xml and overwrite existing (#2784)

This commit is contained in:
tritoch 2017-06-28 21:54:09 -05:00 committed by Zach H
parent bcda502b46
commit 5abfda49be

View file

@ -1027,14 +1027,14 @@ void MainWindow::actAddCustomSet()
if (!dialog.exec())
return;
QString fileName = dialog.selectedFiles().at(0);
QString fullFilePath = dialog.selectedFiles().at(0);
if (!QFile::exists(fileName)) {
if (!QFile::exists(fullFilePath)) {
QMessageBox::warning(this, tr("Load sets/cards"), tr("Selected file cannot be found."));
return;
}
if (QFileInfo(fileName).suffix() != "xml") { // fileName = *.xml
if (QFileInfo(fullFilePath).suffix() != "xml") { // fileName = *.xml
QMessageBox::warning(this, tr("Load sets/cards"), tr("You can only import XML databases at this time."));
return;
}
@ -1042,15 +1042,38 @@ void MainWindow::actAddCustomSet()
QDir dir = settingsCache->getCustomCardDatabasePath();
int nextPrefix = getNextCustomSetPrefix(dir);
bool res = QFile::copy(
fileName, dir.absolutePath() + "/" + (nextPrefix > 9 ? "" : "0") +
QString::number(nextPrefix) + "." + QFileInfo(fileName).fileName()
);
bool res = false;
if (res) {
QString fileName = QFileInfo(fullFilePath).fileName();
if (fileName.compare("spoiler.xml", Qt::CaseInsensitive) == 0)
{
/*
* If the file being added is "spoiler.xml"
* then we'll want to overwrite the old version
* and replace it with the new one
*/
if (QFile::exists(dir.absolutePath() + "/spoiler.xml"))
{
QFile::remove(dir.absolutePath() + "/spoiler.xml");
}
res = QFile::copy(fullFilePath, dir.absolutePath() + "/spoiler.xml");
}
else
{
res = QFile::copy(
fullFilePath,
dir.absolutePath() + "/" + (nextPrefix > 9 ? "" : "0") + QString::number(nextPrefix) + "." + fileName
);
}
if (res)
{
QMessageBox::information(this, tr("Load sets/cards"), tr("The new sets/cards have been added successfully.\nCockatrice will now reload the card database."));
QtConcurrent::run(db, &CardDatabase::loadCardDatabases);
} else {
}
else
{
QMessageBox::warning(this, tr("Load sets/cards"), tr("Sets/cards failed to import."));
}
}