[1397] | 1 | #include "gsProfile.h"
|
---|
[2288] | 2 | #include "FilePath.h"
|
---|
[1397] | 3 |
|
---|
| 4 | #include <stdio.h>
|
---|
| 5 |
|
---|
[1475] | 6 | gsProfile::gsProfile(installManager &manager, string fileName)
|
---|
| 7 | : installAgent(manager)
|
---|
[2288] | 8 | { FilePath path(fileName);
|
---|
| 9 |
|
---|
| 10 | this->fileName = fileName;
|
---|
| 11 | if (path.exists() == false)
|
---|
| 12 | { this->logAction("ProfileCreate", "", "", "");
|
---|
| 13 | }
|
---|
[1397] | 14 | }
|
---|
| 15 |
|
---|
[1475] | 16 | void gsProfile::logAction(string action, string sectionName, string item,
|
---|
| 17 | string value)
|
---|
[1498] | 18 | { unInstallCommand command(action);
|
---|
| 19 | command.addParameter(this->fileName);
|
---|
| 20 | command.addParameter(sectionName);
|
---|
| 21 | command.addParameter(item);
|
---|
| 22 | command.addParameter(value);
|
---|
| 23 | manager->storeCommand(command);
|
---|
[1475] | 24 | }
|
---|
| 25 |
|
---|
| 26 | bool gsProfile::undoAction(string actionName, stringArray ¶ms)
|
---|
[2288] | 27 | { if (actionName == "ProfileCreate")
|
---|
| 28 | { if (this->fileName == params[0])
|
---|
[2534] | 29 | { DeleteFile(params[0].c_str());
|
---|
| 30 | return true;
|
---|
[2288] | 31 | }
|
---|
| 32 | }
|
---|
| 33 | else if (actionName == "ProfileAddListMember")
|
---|
| 34 | { this->removeListMember(params[0], params[1], params[2]);
|
---|
[1475] | 35 | return true;
|
---|
| 36 | }
|
---|
[2288] | 37 | else if (actionName == "ProfileSetItem")
|
---|
| 38 | { this->removeString(params[1], params[2]);
|
---|
| 39 | return true;
|
---|
| 40 | }
|
---|
[1475] | 41 | return false;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | bool gsProfile::ensureListMember(string sectionName, string listName,
|
---|
| 45 | string listMember)
|
---|
[1397] | 46 | { char buffer[256] = "\0";
|
---|
| 47 | stringArray *members;
|
---|
[1475] | 48 | bool reply = true;
|
---|
[1397] | 49 |
|
---|
[1475] | 50 | if (GetPrivateProfileString(sectionName.c_str(), listName.c_str(), "",
|
---|
| 51 | buffer, 256, this->fileName.c_str()) <= 0)
|
---|
[1397] | 52 | { buffer[0] = '\0';
|
---|
| 53 | }
|
---|
| 54 | // add if needsbe
|
---|
| 55 | members = new stringArray(buffer, ";");
|
---|
| 56 | if (members->includes(listMember) == false)
|
---|
| 57 | { members->add(listMember);
|
---|
| 58 | members->writeToCString(buffer, ";", 256);
|
---|
[1475] | 59 |
|
---|
| 60 | reply = this->writeString(sectionName, listName, buffer);
|
---|
| 61 | if (reply)
|
---|
| 62 | { this->logAction("ProfileAddListMember", sectionName, listName, listMember);
|
---|
| 63 | }
|
---|
[1397] | 64 | }
|
---|
| 65 | delete members;
|
---|
[1475] | 66 | return reply;
|
---|
[1397] | 67 | }
|
---|
| 68 |
|
---|
[1475] | 69 | bool gsProfile::addListMember(string sectionName, string listName, string listMember)
|
---|
[1397] | 70 | { char buffer[256];
|
---|
| 71 | stringArray *members;
|
---|
| 72 |
|
---|
[1475] | 73 | GetPrivateProfileString(sectionName.c_str(), listName.c_str(), "",
|
---|
| 74 | buffer, 256, this->fileName.c_str());
|
---|
[1397] | 75 | // straightforward, unthinking, add
|
---|
| 76 | members = new stringArray(buffer, ";");
|
---|
| 77 | members->add(listMember);
|
---|
| 78 | members->writeToCString(buffer, ";", 256);
|
---|
| 79 | delete members;
|
---|
[1475] | 80 | return this->writeString(sectionName, listName, buffer);
|
---|
[1397] | 81 | }
|
---|
| 82 |
|
---|
[1475] | 83 | bool gsProfile::removeListMember(string sectionName, string listName, string listMember)
|
---|
[1397] | 84 | { char buffer[256];
|
---|
| 85 | stringArray *members;
|
---|
| 86 |
|
---|
[1475] | 87 | GetPrivateProfileString(sectionName.c_str(), listName.c_str(), "",
|
---|
| 88 | buffer, 256, this->fileName.c_str());
|
---|
[1397] | 89 | // straightforward, unthinking, removal - if it isn't there, we won't need to
|
---|
| 90 | // worry!
|
---|
| 91 | members = new stringArray(buffer, ";");
|
---|
| 92 | members->remove(listMember);
|
---|
| 93 | members->writeToCString(buffer, ";", 256);
|
---|
| 94 | delete members;
|
---|
| 95 |
|
---|
[1475] | 96 | return this->writeString(sectionName, listName, buffer);
|
---|
[1397] | 97 | }
|
---|
| 98 |
|
---|
[1475] | 99 | bool gsProfile::writeString(string sectionName, string itemName, string itemValue)
|
---|
| 100 | { if (!WritePrivateProfileString( sectionName.c_str(), itemName.c_str(),
|
---|
| 101 | itemValue.c_str(), this->fileName.c_str()))
|
---|
[2534] | 102 | { DWORD error = GetLastError();
|
---|
| 103 | /* char buffer[201];
|
---|
| 104 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, buffer, 200, NULL);
|
---|
| 105 | MessageBox(0, buffer, "Write error", MB_OK);
|
---|
| 106 | */
|
---|
| 107 |
|
---|
[1541] | 108 | return false;
|
---|
[1397] | 109 | }
|
---|
[2288] | 110 | this->logAction("ProfileSetItem", sectionName, itemName, itemValue);
|
---|
[1397] | 111 | return true;
|
---|
[2288] | 112 | }
|
---|
| 113 |
|
---|
| 114 | bool gsProfile::removeString(string sectionName, string itemName)
|
---|
| 115 | { if (!WritePrivateProfileString( sectionName.c_str(), itemName.c_str(),
|
---|
| 116 | NULL, this->fileName.c_str()))
|
---|
[2534] | 117 | { DWORD error = GetLastError();
|
---|
| 118 | if (error != 3)
|
---|
| 119 | { char buffer[201];
|
---|
| 120 |
|
---|
| 121 | MessageBox(0, "Failed", itemName.c_str(), MB_OK);
|
---|
| 122 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, 0, 0, buffer, 200, NULL);
|
---|
| 123 | MessageBox(0, buffer, "Write error", MB_OK);
|
---|
| 124 | return false;
|
---|
| 125 | }
|
---|
| 126 | WritePrivateProfileString(NULL, NULL, NULL, this->fileName.c_str());
|
---|
| 127 | MessageBox(0, this->fileName.c_str(), sectionName.c_str(), MB_OK);
|
---|
[2288] | 128 | }
|
---|
| 129 | return true;
|
---|
[1397] | 130 | } |
---|