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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#include "config.h"
#include <zen/file_access.h>
#include <zen/process_exec.h>
#include <zenxml/xml.h>
#include <wx/uilocale.h>
#include "../ffs_paths.h"
#include "../localization.h"
using namespace zen;
using namespace rts;
//-------------------------------------------------------------------------------------------------------------------------------
const int XML_FORMAT_RTS_CFG = 2; //2020-04-14
//-------------------------------------------------------------------------------------------------------------------------------
namespace zen
{
template <> inline
bool readText(const std::string& input, wxLanguage& value)
{
if (const wxLanguageInfo* lngInfo = wxUILocale::FindLanguageInfo(utfTo<wxString>(input)))
{
value = static_cast<wxLanguage>(lngInfo->Language);
return true;
}
return false;
}
}
namespace
{
std::string getConfigType(const XmlDoc& doc)
{
if (doc.root().getName() == "FreeFileSync")
{
std::string type;
if (doc.root().getAttribute("XmlType", type))
return type;
}
return {};
}
void readConfig(const XmlIn& in, XmlRealConfig& cfg, int formatVer)
{
in["Directories"](cfg.directories);
in["Delay" ](cfg.delay);
in["Commandline"](cfg.commandline);
//TODO: remove if clause after migration! 2020-04-14
if (formatVer < 2)
if (startsWithAsciiNoCase(cfg.commandline, "cmd /c ") ||
startsWithAsciiNoCase(cfg.commandline, "cmd.exe /c "))
cfg.commandline = afterFirst(cfg.commandline, Zstr("/c "), IfNotFoundReturn::all);
}
void writeConfig(const XmlRealConfig& cfg, XmlOut& out)
{
out["Directories"](cfg.directories);
out["Delay" ](cfg.delay);
out["Commandline"](cfg.commandline);
}
}
std::pair<XmlRealConfig, std::wstring /*warningMsg*/> rts::readConfig(const Zstring& filePath) //throw FileError
{
XmlDoc doc = loadXml(filePath); //throw FileError
if (getConfigType(doc) != "REAL")
throw FileError(replaceCpy(_("File %x does not contain a valid configuration."), L"%x", fmtPath(filePath)));
int formatVer = 0;
/*bool success =*/ doc.root().getAttribute("XmlFormat", formatVer);
XmlIn in(doc);
XmlRealConfig cfg;
::readConfig(in, cfg, formatVer);
std::wstring warningMsg;
if (const std::wstring& errors = in.getErrors();
!errors.empty())
warningMsg = replaceCpy(_("Configuration file %x is incomplete. The missing elements have been set to their default values."), L"%x", fmtPath(filePath)) + L"\n\n" +
_("The following XML elements could not be read:") + L'\n' + errors;
else //(try to) migrate old configuration automatically
if (formatVer < XML_FORMAT_RTS_CFG)
try
{
rts::writeConfig(cfg, filePath); //throw FileError
}
catch (const FileError& e) { warningMsg = e.toString(); }
return {cfg, warningMsg};
}
void rts::writeConfig(const XmlRealConfig& cfg, const Zstring& filePath) //throw FileError
{
XmlDoc doc("FreeFileSync");
doc.root().setAttribute("XmlType", "REAL");
doc.root().setAttribute("XmlFormat", XML_FORMAT_RTS_CFG);
XmlOut out(doc);
::writeConfig(cfg, out);
saveXml(doc, filePath); //throw FileError
}
std::pair<XmlRealConfig, std::wstring /*warningMsg*/> rts::readRealOrBatchConfig(const Zstring& filePath) //throw FileError
{
XmlDoc doc = loadXml(filePath); //throw FileError
//quick exit if file is not an FFS XML
//convert batch config to RealTimeSync config
if (getConfigType(doc) == "BATCH")
{
XmlIn in(doc);
//read folder pairs
std::set<Zstring, LessNativePath> uniqueFolders;
in["FolderPairs"].visitChildren([&](const XmlIn& inPair)
{
assert(*inPair.getName() == "Pair");
Zstring folderPathPhraseLeft;
Zstring folderPathPhraseRight;
inPair["Left" ](folderPathPhraseLeft);
inPair["Right"](folderPathPhraseRight);
uniqueFolders.insert(folderPathPhraseLeft);
uniqueFolders.insert(folderPathPhraseRight);
});
if (const std::wstring& errors = in.getErrors();
!errors.empty())
throw FileError(replaceCpy(_("File %x does not contain a valid configuration."), L"%x", fmtPath(filePath)),
_("The following XML elements could not be read:") + L'\n' + errors);
//---------------------------------------------------------------------------------------
std::erase_if(uniqueFolders, [](const Zstring& str) { return trimCpy(str).empty(); });
std::wstring warningMsg;
const Zstring ffsLaunchPath = [&]() -> Zstring
{
try
{
return fff::getFreeFileSyncLauncherPath(); //throw FileError
}
catch (const FileError& e)
{
warningMsg = e.toString();
return Zstr("FreeFileSync"); //fallback: at least give some hint...
}
}();
XmlRealConfig cfg
{
.directories = {uniqueFolders.begin(), uniqueFolders.end()},
.commandline = escapeCommandArg(ffsLaunchPath) + Zstr(' ') + escapeCommandArg(filePath),
};
return {cfg, warningMsg};
}
else
return readConfig(filePath); //throw FileError
}
wxLanguage rts::getProgramLanguage() //throw FileError
{
const Zstring& filePath = appendPath(fff::getConfigDirPath(), Zstr("GlobalSettings.xml"));
XmlDoc doc;
try
{
doc = loadXml(filePath); //throw FileError
}
catch (FileError&)
{
if (!itemExists(filePath)) //throw FileError
return fff::getDefaultLanguage();
throw;
}
if (getConfigType(doc) != "GLOBAL")
throw FileError(replaceCpy(_("File %x does not contain a valid configuration."), L"%x", fmtPath(filePath)));
XmlIn in(doc);
wxLanguage lng = wxLANGUAGE_UNKNOWN;
in["Language"].attribute("Code", lng);
if (const std::wstring& errors = in.getErrors();
!errors.empty())
throw FileError(replaceCpy(_("File %x does not contain a valid configuration."), L"%x", fmtPath(filePath)),
_("The following XML elements could not be read:") + L'\n' + errors);
return lng;
}
|