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