summaryrefslogtreecommitdiff
path: root/src/profile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/profile.cpp')
-rw-r--r--src/profile.cpp75
1 files changed, 55 insertions, 20 deletions
diff --git a/src/profile.cpp b/src/profile.cpp
index 002c93c0..62ac943e 100644
--- a/src/profile.cpp
+++ b/src/profile.cpp
@@ -25,6 +25,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
#include "modinfo.h"
#include <utility.h>
#include <util.h>
+#include <error_report.h>
#include <appconfig.h>
#include <QMessageBox>
#include <QApplication>
@@ -47,14 +48,19 @@ Profile::Profile(const QString &name, bool useDefaultSettings)
QString profilesDir = QDir::fromNativeSeparators(ToQString(GameInfo::instance().getProfilesDir()));
QDir profileBase(profilesDir);
- if (!profileBase.exists() || !profileBase.mkdir(name)) {
- throw std::runtime_error(QObject::tr("failed to create %1").arg(name).toUtf8().constData());
+ QString fixedName = name;
+ if (!fixDirectoryName(fixedName)) {
+ throw MyException(tr("invalid profile name %1").arg(name));
}
- QString fullPath = profilesDir + "/" + name;
+
+ if (!profileBase.exists() || !profileBase.mkdir(fixedName)) {
+ throw MyException(tr("failed to create %1").arg(fixedName).toUtf8().constData());
+ }
+ QString fullPath = profilesDir + "/" + fixedName;
m_Directory = QDir(fullPath);
QFile modList(m_Directory.filePath("modlist.txt"));
if (!modList.open(QIODevice::ReadWrite)) {
- profileBase.rmdir(name);
+ profileBase.rmdir(fixedName);
throw std::runtime_error(QObject::tr("failed to create %1").arg(m_Directory.filePath("modlist.txt")).toUtf8().constData());
}
modList.close();
@@ -63,7 +69,7 @@ Profile::Profile(const QString &name, bool useDefaultSettings)
GameInfo::instance().createProfile(ToWString(fullPath), useDefaultSettings);
} catch (...) {
// clean up in case of an error
- removeDir(profileBase.absoluteFilePath(name));
+ removeDir(profileBase.absoluteFilePath(fixedName));
throw;
}
initTimer();
@@ -133,11 +139,16 @@ void Profile::cancelWriteModlist() const
void Profile::writeModlistNow() const
{
m_SaveTimer->stop();
+ if (!m_Directory.exists()) return;
#pragma message("right now, this is doing unnecessary saves. Need a flag that says that mod priority, enabled-state or name of a mod has changed")
QString fileName = getModlistFileName();
QFile file(fileName);
- file.open(QIODevice::WriteOnly);
+ if (!file.open(QIODevice::WriteOnly)) {
+ reportError(tr("failed to open \"%1\" for writing").arg(fileName));
+ return;
+ }
+
file.resize(0);
file.write(QString("# This file was automatically generated by Mod Organizer.\r\n").toUtf8());
if (m_ModStatus.empty()) {
@@ -168,9 +179,7 @@ void Profile::writeModlistNow() const
void Profile::createTweakedIniFile()
{
- QFileInfo iniInfo(getIniFileName());
-
- QString tweakedIni = iniInfo.absolutePath() + "/initweaks.ini";
+ QString tweakedIni = m_Directory.absoluteFilePath("initweaks.ini");
// QFile iniFile(tweakedIni);
// workaround: the fallout nv launcher seems to mark the file read-only. crazy...
@@ -189,6 +198,28 @@ void Profile::createTweakedIniFile()
mergeTweaks(modInfo, tweakedIni);
}
}
+
+
+ bool error = false;
+ if (!::WritePrivateProfileStringW(L"Archive", L"bInvalidateOlderFiles", L"1", ToWString(tweakedIni).c_str())) {
+ error = true;
+ }
+
+ if (localSavesEnabled()) {
+ if (!::WritePrivateProfileStringW(L"General", L"bUseMyGamesDirectory", L"1", ToWString(tweakedIni).c_str())) {
+ error = true;
+ }
+
+ if (!::WritePrivateProfileStringW(L"General", L"SLocalSavePath",
+ AppConfig::localSavePlaceholder(),
+ ToWString(tweakedIni).c_str())) {
+ error = true;
+ }
+ }
+
+ if (error) {
+ reportError(tr("failed to create tweaked ini: %1").arg(getCurrentErrorStringA().c_str()));
+ }
}
@@ -427,6 +458,14 @@ Profile Profile::createFrom(const QString &name, const Profile &reference)
}
+Profile *Profile::createPtrFrom(const QString &name, const Profile &reference)
+{
+ QString profileDirectory = QDir::fromNativeSeparators(ToQString(GameInfo::instance().getProfilesDir())).append("/").append(name);
+ reference.copyFilesTo(profileDirectory);
+ return new Profile(QDir(profileDirectory));
+}
+
+
void Profile::copyFilesTo(QString &target) const
{
copyDir(m_Directory.absolutePath(), target, false);
@@ -499,16 +538,6 @@ void Profile::mergeTweaks(ModInfo::Ptr modInfo, const QString &tweakedIni) const
iter != iniTweaks.end(); ++iter) {
mergeTweak(*iter, tweakedIni);
}
-
- ::WritePrivateProfileStringW(L"Archive", L"bInvalidateOlderFiles", L"1", ToWString(tweakedIni).c_str());
-
- if (localSavesEnabled()) {
- ::WritePrivateProfileStringW(L"General", L"bUseMyGamesDirectory", L"1", ToWString(tweakedIni).c_str());
-
- ::WritePrivateProfileStringW(L"General", L"SLocalSavePath",
- AppConfig::localSavePlaceholder(),
- ToWString(tweakedIni).c_str());
- }
}
@@ -659,7 +688,6 @@ QString Profile::getPluginsFileName() const
return QDir::cleanPath(m_Directory.absoluteFilePath("plugins.txt"));
}
-
QString Profile::getLoadOrderFileName() const
{
return QDir::cleanPath(m_Directory.absoluteFilePath("loadorder.txt"));
@@ -693,3 +721,10 @@ QString Profile::getPath() const
{
return QDir::cleanPath(m_Directory.absolutePath());
}
+
+void Profile::rename(const QString &newName)
+{
+ QDir profileDir(QDir::fromNativeSeparators(ToQString(GameInfo::instance().getProfilesDir())));
+ profileDir.rename(getName(), newName);
+ m_Directory = profileDir.absoluteFilePath(newName);
+}