diff --git a/apps/launcher/datafilespage.cpp b/apps/launcher/datafilespage.cpp index 06e9ff3f8..fa8375f3d 100644 --- a/apps/launcher/datafilespage.cpp +++ b/apps/launcher/datafilespage.cpp @@ -369,10 +369,10 @@ void DataFilesPage::masterSelectionChanged(const QItemSelection &selected, const QString masterstr; // Create a QStringList containing all the masters - const QList selectedMasters = mMastersWidget->selectedItems(); + const QStringList masterList = selectedMasters(); - foreach (const QTableWidgetItem *item, selectedMasters) { - masters.append(item->data(Qt::DisplayRole).toString()); + foreach (const QString ¤tMaster, masterList) { + masters.append(currentMaster); } masters.sort(); @@ -488,6 +488,18 @@ void DataFilesPage::setCheckstate(QModelIndex index) } } +const QStringList DataFilesPage::selectedMasters() +{ + QStringList masters; + const QList selectedMasters = mMastersWidget->selectedItems(); + + foreach (const QTableWidgetItem *item, selectedMasters) { + masters.append(item->data(Qt::DisplayRole).toString()); + } + + return masters; +} + const QStringList DataFilesPage::checkedPlugins() { QStringList checkedItems; @@ -611,12 +623,12 @@ void DataFilesPage::writeConfig(QString profile) mLauncherConfig->remove(""); // Clear the subgroup // First write the masters to the config - const QList selectedMasters = mMastersWidget->selectedItems(); + const QStringList masterList = selectedMasters(); // We don't use foreach because we need i - for (int i = 0; i < selectedMasters.size(); ++i) { - const QTableWidgetItem *item = selectedMasters.at(i); - mLauncherConfig->setValue(QString("Master%0").arg(i), item->data(Qt::DisplayRole).toString()); + for (int i = 0; i < masterList.size(); ++i) { + const QString master = masterList.at(i); + mLauncherConfig->setValue(QString("Master%0").arg(i), master); } // Now write all checked plugins diff --git a/apps/launcher/datafilespage.hpp b/apps/launcher/datafilespage.hpp index 2aebc0160..9127797ac 100644 --- a/apps/launcher/datafilespage.hpp +++ b/apps/launcher/datafilespage.hpp @@ -27,6 +27,7 @@ public: QSettings *mLauncherConfig; const QStringList checkedPlugins(); + const QStringList selectedMasters(); void setupConfig(); void readConfig(); void writeConfig(QString profile = QString()); diff --git a/apps/launcher/maindialog.cpp b/apps/launcher/maindialog.cpp index d0ca35b61..464dca03a 100644 --- a/apps/launcher/maindialog.cpp +++ b/apps/launcher/maindialog.cpp @@ -140,6 +140,58 @@ void MainDialog::closeEvent(QCloseEvent *event) qDebug() << "Close event"; mDataFilesPage->writeConfig(); mDataFilesPage->mLauncherConfig->sync(); + + // Write to the openmw.cfg + QString dataPath = mGameConfig->value("data").toString(); + dataPath.append("/"); + + QStringList dataFiles = mDataFilesPage->selectedMasters(); + dataFiles.append(mDataFilesPage->checkedPlugins()); + + qDebug() << "Writing to openmw.cfg"; + + // Open the config as a QFile + QFile file(mGameConfig->fileName()); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + // File cannot be opened or created TODO: throw error + } + + QTextStream in(&file); + QByteArray buffer; + + // Remove all previous master/plugin entries from config + while (!in.atEnd()) { + QString line = in.readLine(); + if (!line.contains("master") && !line.contains("plugin")) { + buffer += line += "\n"; + } + } + + file.close(); + + // Now we write back the other config entries + if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) { + // File cannot be opened or created TODO: throw error + } + + file.write(buffer); + QTextStream out(&file); + + // Write the list of game files to the config + foreach (const QString ¤tFile, dataFiles) { + QFileInfo dataFile = QFileInfo(QString(currentFile).prepend(dataPath)); + + if (dataFile.exists()) { + if (currentFile.endsWith(QString(".esm"), Qt::CaseInsensitive)) { + out << "master=" << currentFile << endl; + } else if (currentFile.endsWith(QString(".esp"), Qt::CaseInsensitive)) { + out << "plugin=" << currentFile << endl; + } + } + } + + + file.close(); event->accept(); }