#include "gsProfile.h" #include "FilePath.h" #include gsProfile::gsProfile(installManager &manager, string fileName) : installAgent(manager) { FilePath path(fileName); this->fileName = fileName; if (path.exists() == false) { this->logAction("ProfileCreate", "", "", ""); } } void gsProfile::logAction(string action, string sectionName, string item, string value) { unInstallCommand command(action); command.addParameter(this->fileName); command.addParameter(sectionName); command.addParameter(item); command.addParameter(value); manager->storeCommand(command); } bool gsProfile::undoAction(string actionName, stringArray ¶ms) { if (actionName == "ProfileCreate") { if (this->fileName == params[0]) { DeleteFile(params[0].c_str()); return true; } } else if (actionName == "ProfileAddListMember") { this->removeListMember(params[0], params[1], params[2]); return true; } else if (actionName == "ProfileSetItem") { this->removeString(params[1], params[2]); return true; } return false; } bool gsProfile::ensureListMember(string sectionName, string listName, string listMember) { char buffer[256] = "\0"; stringArray *members; bool reply = true; if (GetPrivateProfileString(sectionName.c_str(), listName.c_str(), "", buffer, 256, this->fileName.c_str()) <= 0) { buffer[0] = '\0'; } // add if needsbe members = new stringArray(buffer, ";"); if (members->includes(listMember) == false) { members->add(listMember); members->writeToCString(buffer, ";", 256); reply = this->writeString(sectionName, listName, buffer); if (reply) { this->logAction("ProfileAddListMember", sectionName, listName, listMember); } } delete members; return reply; } bool gsProfile::addListMember(string sectionName, string listName, string listMember) { char buffer[256]; stringArray *members; GetPrivateProfileString(sectionName.c_str(), listName.c_str(), "", buffer, 256, this->fileName.c_str()); // straightforward, unthinking, add members = new stringArray(buffer, ";"); members->add(listMember); members->writeToCString(buffer, ";", 256); delete members; return this->writeString(sectionName, listName, buffer); } bool gsProfile::removeListMember(string sectionName, string listName, string listMember) { char buffer[256]; stringArray *members; GetPrivateProfileString(sectionName.c_str(), listName.c_str(), "", buffer, 256, this->fileName.c_str()); // straightforward, unthinking, removal - if it isn't there, we won't need to // worry! members = new stringArray(buffer, ";"); members->remove(listMember); members->writeToCString(buffer, ";", 256); delete members; return this->writeString(sectionName, listName, buffer); } bool gsProfile::writeString(string sectionName, string itemName, string itemValue) { if (!WritePrivateProfileString( sectionName.c_str(), itemName.c_str(), itemValue.c_str(), this->fileName.c_str())) { DWORD error = GetLastError();/* char buffer[201]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, buffer, 200, NULL); MessageBox(0, buffer, "Write error", MB_OK); */ return false; } this->logAction("ProfileSetItem", sectionName, itemName, itemValue); return true; } bool gsProfile::removeString(string sectionName, string itemName) { if (!WritePrivateProfileString( sectionName.c_str(), itemName.c_str(), NULL, this->fileName.c_str())) { DWORD error = GetLastError(); if (error != 3) { char buffer[201]; MessageBox(0, "Failed", itemName.c_str(), MB_OK); FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, buffer, 200, NULL); MessageBox(0, buffer, "Write error", MB_OK); return false; } WritePrivateProfileString(NULL, NULL, NULL, this->fileName.c_str()); MessageBox(0, this->fileName.c_str(), sectionName.c_str(), MB_OK); } return true; }