1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved *
// **************************************************************************
#include "xml_proc.h"
#include <zen/file_handling.h>
#include <wx/filefn.h>
#include <wx+/string_conv.h>
#include "../lib/process_xml.h"
#include "../lib/ffs_paths.h"
using namespace zen;
using namespace xmlAccess;
namespace
{
void readConfig(const XmlIn& in, XmlRealConfig& config)
{
in["Directories"](config.directories);
in["Delay" ](config.delay);
in["Commandline"](config.commandline);
}
bool isXmlTypeRTS(const XmlDoc& doc) //throw()
{
if (doc.root().getNameAs<std::string>() == "FreeFileSync")
{
std::string type;
if (doc.root().getAttribute("XmlType", type))
return type == "REAL";
}
return false;
}
}
void xmlAccess::readConfig(const Zstring& filename, XmlRealConfig& config, std::wstring& warningMsg) //throw FileError
{
XmlDoc doc = loadXmlDocument(filename); //throw FileError
if (!isXmlTypeRTS(doc))
throw FileError(replaceCpy(_("File %x does not contain a valid configuration."), L"%x", fmtFileName(filename)));
XmlIn in(doc);
::readConfig(in, config);
try
{
checkForMappingErrors(in, filename); //throw FileError
}
catch (const FileError& e)
{
warningMsg = e.toString();
}
}
namespace
{
void writeConfig(const XmlRealConfig& config, XmlOut& out)
{
out["Directories"](config.directories);
out["Delay" ](config.delay);
out["Commandline"](config.commandline);
}
}
void xmlAccess::writeConfig(const XmlRealConfig& config, const Zstring& filename) //throw FileError
{
XmlDoc doc("FreeFileSync");
doc.root().setAttribute("XmlType", "REAL");
XmlOut out(doc);
::writeConfig(config, out);
saveXmlDocument(doc, filename); //throw FileError
}
namespace
{
xmlAccess::XmlRealConfig convertBatchToReal(const xmlAccess::XmlBatchConfig& batchCfg, const Zstring& filename)
{
std::set<Zstring, LessFilename> uniqueFolders;
//add main folders
uniqueFolders.insert(batchCfg.mainCfg.firstPair.dirnamePhraseLeft);
uniqueFolders.insert(batchCfg.mainCfg.firstPair.dirnamePhraseRight);
//additional folders
for (const FolderPairEnh& fp : batchCfg.mainCfg.additionalPairs)
{
uniqueFolders.insert(fp.dirnamePhraseLeft);
uniqueFolders.insert(fp.dirnamePhraseRight);
}
uniqueFolders.erase(Zstring());
xmlAccess::XmlRealConfig output;
output.directories.assign(uniqueFolders.begin(), uniqueFolders.end());
output.commandline = Zstr("\"") + zen::getFreeFileSyncLauncher() + Zstr("\" \"") + filename + Zstr("\"");
return output;
}
}
void xmlAccess::readRealOrBatchConfig(const Zstring& filename, xmlAccess::XmlRealConfig& config, std::wstring& warningMsg) //throw FileError
{
using namespace xmlAccess;
if (getXmlType(filename) != XML_TYPE_BATCH) //throw FileError
return readConfig(filename, config, warningMsg); //throw FileError
//convert batch config to RealtimeSync config
XmlBatchConfig batchCfg;
readConfig(filename, batchCfg, warningMsg); //throw FileError
//<- redirect batch config warnings
config = convertBatchToReal(batchCfg, filename);
}
int xmlAccess::getProgramLanguage()
{
xmlAccess::XmlGlobalSettings settings;
std::wstring warningMsg;
try
{
xmlAccess::readConfig(getGlobalConfigFile(), settings, warningMsg); //throw FileError
}
catch (const FileError&) {} //user default language if error occurred
return settings.programLanguage;
}
|