|
| 1 | +#include "OverrideMapUpdaterClass.h" |
| 2 | + |
| 3 | +// Experiment with concepts needed, might eventually morph into actual behavior |
| 4 | +OverrideMapUpdater::OverrideMapUpdater(void) |
| 5 | +{ |
| 6 | + gNppMetaInfo.populate(); |
| 7 | + |
| 8 | + // 1. Pick correct file: "cfg" directory (Settings/Cloud/AppData) or "app" directory, whichever exists |
| 9 | + _wsOverMapPath = gNppMetaInfo.dir.cfgFunctionList + L"\\overrideMap.xml"; |
| 10 | + _wsAppOverMapPath = gNppMetaInfo.dir.app + L"\\functionList\\overrideMap.xml"; |
| 11 | + bool needWriteToAppDir = false; |
| 12 | + bool needToCreateFile = false; |
| 13 | + |
| 14 | + if (!PathFileExists(_wsOverMapPath.c_str())) { |
| 15 | + // since .cfgFunctionList\overrideMap.xml doesn't exist, try to copy from app dir instead |
| 16 | + if (PathFileExists(_wsAppOverMapPath.c_str())) { |
| 17 | + // check if writeable |
| 18 | + if (pcjHelper::is_dir_writable(gNppMetaInfo.dir.cfgFunctionList)) { |
| 19 | + // if writable, copy the app version to the cfg location |
| 20 | + CopyFile(_wsAppOverMapPath.c_str(), _wsOverMapPath.c_str(), TRUE); |
| 21 | + } |
| 22 | + else { |
| 23 | + // if not writable, then need to write into the app directory instead |
| 24 | + needWriteToAppDir = true; |
| 25 | + } |
| 26 | + } |
| 27 | + else { |
| 28 | + // if neither XML exists, need to create one of them |
| 29 | + needToCreateFile = true; |
| 30 | + // pick the right one based on writability of cfg directory |
| 31 | + needWriteToAppDir = !pcjHelper::is_dir_writable(gNppMetaInfo.dir.cfgFunctionList); |
| 32 | + } |
| 33 | + |
| 34 | + // change the destination path if it needs to be in the app directory |
| 35 | + if (needWriteToAppDir) _wsOverMapPath = _wsAppOverMapPath; |
| 36 | + } |
| 37 | + |
| 38 | + // 2: need XML object |
| 39 | + pOverrideMapXML = new tinyxml2::XMLDocument; // create new one (old is discarded, if it exists) |
| 40 | + if (needToCreateFile) { |
| 41 | + // create the basic structure |
| 42 | + pRoot = pOverrideMapXML->NewElement("NotepadPlus"); |
| 43 | + pOverrideMapXML->InsertFirstChild(pRoot); |
| 44 | + |
| 45 | + pFunctionList = pRoot->InsertNewChildElement("functionList"); |
| 46 | + pAssociationMap = pFunctionList->InsertNewChildElement("associationMap"); |
| 47 | + |
| 48 | + pAssociationMap->InsertEndChild( |
| 49 | + pOverrideMapXML->NewComment("\r\n" |
| 50 | + "\t\t\tSee https://github.com/notepad-plus-plus/notepad-plus-plus/blob/master/PowerEditor/installer/functionList/overrideMap.xml for the \"official\" default for this file\r\n" |
| 51 | + "\t\t\t\tincluding the default id-vs-langID values\r\n" |
| 52 | + "\r\n" |
| 53 | + "\t\t\tEach functionlist parse rule links to a language ID(\"langID\") or a UDL name.\r\n" |
| 54 | + "\t\t\tExamples:\r\n" |
| 55 | + "\t\t\t\t<association id=\"my_perl.xml\" langID=\"21\" />\r\n" |
| 56 | + "\t\t\t\t<association id=\"nppexec.xml\" userDefinedLangName=\"NppExec\" />\r\n" |
| 57 | + ) |
| 58 | + ); |
| 59 | + pAssociationMap->InsertEndChild( |
| 60 | + pOverrideMapXML->NewComment(" ==================== User Defined Languages ============================ ") |
| 61 | + ); |
| 62 | + |
| 63 | + tinyxml2::XMLElement* pAssoc = pAssociationMap->InsertNewChildElement("association"); |
| 64 | + pAssoc->SetAttribute("id", "nppexec.xml"); |
| 65 | + pAssoc->SetAttribute("userDefinedLangName", "NppExec"); |
| 66 | + } |
| 67 | + else { |
| 68 | + // TODO: !!! NEED TO ADD ERROR CHECKING !!! |
| 69 | + // load file and extract the main elements |
| 70 | + /*tinyxml2::XMLError eResult =*/ pOverrideMapXML->LoadFile(sOverMapPath().c_str()); |
| 71 | + pRoot = pOverrideMapXML->FirstChildElement("NotepadPlus"); |
| 72 | + pFunctionList = pRoot->FirstChildElement("functionList"); |
| 73 | + pAssociationMap = pFunctionList->FirstChildElement("associationMap"); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +bool OverrideMapUpdater::experiment(void) |
| 78 | +{ |
| 79 | + return false; |
| 80 | +} |
0 commit comments