Skip to content

Commit 274896d

Browse files
committed
CollectionInterface: OverrideMapUpdater has a constructor with properties for the needed paths and for the initial XML
1 parent f0b1ab7 commit 274896d

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#pragma once
2+
#include <string>
3+
#include <vector>
4+
#include <map>
5+
#include <stdexcept>
6+
#include <windows.h>
7+
#include <wininet.h>
8+
#include <pathcch.h>
9+
#include <shlwapi.h>
10+
#include "PluginDefinition.h"
11+
#include "NppMetaClass.h"
12+
#include "tinyxml2.h"
13+
#include "pcjHelper.h"
14+
15+
class OverrideMapUpdater {
16+
public:
17+
// Instantiate OverrideMapUpdater object
18+
OverrideMapUpdater(void);
19+
20+
// Experiment with concepts needed, might eventually morph into actual behavior
21+
bool experiment(void);
22+
23+
// getters
24+
std::wstring wsOverMapPath(void) { return _wsOverMapPath; }
25+
std::string sOverMapPath(void) { return pcjHelper::wstring_to_utf8(_wsOverMapPath); }
26+
27+
private:
28+
// properties
29+
30+
// paths
31+
std::wstring _wsOverMapPath; // <cfg>\functionList\overrideMap.xml path, or <exe> version
32+
std::wstring _wsAppOverMapPath; // <exe>\functionList\overrideMap.xml path
33+
34+
// tinyxml2
35+
tinyxml2::XMLDocument* pOverrideMapXML;
36+
tinyxml2::XMLElement* pRoot, * pFunctionList, * pAssociationMap;
37+
};

0 commit comments

Comments
 (0)