summaryrefslogtreecommitdiff
path: root/library/processXml.cpp
blob: 80f284c24d6d9ae25146bcaa955996b540fc103a (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#include "processXml.h"
#include <wx/filefn.h>
#include <wx/ffile.h>
#include "globalFunctions.h"

using namespace globalFunctions;
using namespace xmlAccess;


XmlType xmlAccess::getXmlType(const wxString& filename)
{
    if (!wxFileExists(filename))
        return XML_OTHER;

    //workaround to get a FILE* from a unicode filename
    wxFFile configFile(filename, wxT("rb"));
    if (!configFile.IsOpened())
        return XML_OTHER;

    FILE* inputFile = configFile.fp();

    TiXmlDocument doc;
    if (!doc.LoadFile(inputFile)) //fails if inputFile is no proper XML
        return XML_OTHER;

    TiXmlElement* root = doc.RootElement();

    if (!root || (root->ValueStr() != string("FreeFileSync"))) //check for FFS configuration xml
        return XML_OTHER;

    const char* cfgType = root->Attribute("XmlType");
    if (!cfgType)
        return XML_OTHER;

    if (string(cfgType) == "BATCH")
        return XML_BATCH_CONFIG;
    else if (string(cfgType) == "GUI")
        return XML_GUI_CONFIG;
    else
        return XML_OTHER;
}


XmlInput::XmlInput(const wxString& fileName, const XmlType type) :
        loadSuccess(false)
{
    if (!wxFileExists(fileName)) //avoid wxWidgets error message when wxFFile receives not existing file
        return;

    //workaround to get a FILE* from a unicode filename
    wxFFile dummyFile(fileName, wxT("rb"));
    if (dummyFile.IsOpened())
    {
        FILE* inputFile = dummyFile.fp();

        TiXmlBase::SetCondenseWhiteSpace(false); //do not condense whitespace characters

        if (doc.LoadFile(inputFile)) //load XML; fails if inputFile is no proper XML
        {
            TiXmlElement* root = doc.RootElement();

            if (root && (root->ValueStr() == string("FreeFileSync"))) //check for FFS configuration xml
            {
                const char* cfgType = root->Attribute("XmlType");
                if (cfgType)
                {
                    if (type == XML_GUI_CONFIG)
                        loadSuccess = string(cfgType) == "GUI";
                    else if (type == XML_BATCH_CONFIG)
                        loadSuccess = string(cfgType) == "BATCH";
                }
            }
        }
    }
}


bool XmlInput::readXmlElementValue(string& output, const TiXmlElement* parent, const string& name)
{
    if (parent)
    {
        const TiXmlElement* child = parent->FirstChildElement(name);
        if (child)
        {
            const char* text = child->GetText();
            if (text) //may be NULL!!
                output = text;
            else
                output.clear();
            return true;
        }
    }

    return false;
}


bool XmlInput::readXmlElementValue(int& output, const TiXmlElement* parent, const string& name)
{
    string temp;
    if (readXmlElementValue(temp, parent, name))
    {
        output = stringToInt(temp);
        return true;
    }
    else
        return false;
}


bool XmlInput::readXmlElementValue(CompareVariant& output, const TiXmlElement* parent, const string& name)
{
    int dummy = 0;
    if (readXmlElementValue(dummy, parent, name))
    {
        output = CompareVariant(dummy);
        return true;
    }
    else
        return false;
}


bool XmlInput::readXmlElementValue(SyncDirection& output, const TiXmlElement* parent, const string& name)
{
    string dummy;
    if (readXmlElementValue(dummy, parent, name))
    {
        if (dummy == "left")
            output = SYNC_DIR_LEFT;
        else if (dummy == "right")
            output = SYNC_DIR_RIGHT;
        else //treat all other input as "none"
            output = SYNC_DIR_NONE;

        return true;
    }
    else
        return false;
}


bool XmlInput::readXmlElementValue(bool& output, const TiXmlElement* parent, const string& name)
{
    string dummy;
    if (readXmlElementValue(dummy, parent, name))
    {
        output = (dummy == "true");
        return true;
    }
    else
        return false;
}


bool XmlInput::readXmlMainConfig(XmlMainConfig& outputCfg)
{
    TiXmlElement* root = doc.RootElement();
    if (!root) return false;

    TiXmlHandle hRoot(root);

    TiXmlElement* cmpSettings  = hRoot.FirstChild("MainConfig").FirstChild("Comparison").ToElement();
    TiXmlElement* syncConfig   = hRoot.FirstChild("MainConfig").FirstChild("Synchronization").FirstChild("Directions").ToElement();
    TiXmlElement* miscSettings = hRoot.FirstChild("MainConfig").FirstChild("Miscellaneous").ToElement();
    TiXmlElement* filter       = TiXmlHandle(miscSettings).FirstChild("Filter").ToElement();

    if (!cmpSettings || !syncConfig || !miscSettings || !filter)
        return false;

    string tempString;
//###########################################################
    //read compare variant
    if (!readXmlElementValue(outputCfg.cfg.compareVar, cmpSettings, "Variant")) return false;

    //read folder pair(s)
    TiXmlElement* folderPair = TiXmlHandle(cmpSettings).FirstChild("Folders").FirstChild("Pair").ToElement();

    //read folder pairs
    while (folderPair)
    {
        FolderPair newPair;

        if (!readXmlElementValue(tempString, folderPair, "Left")) return false;
        newPair.leftDirectory = wxString::FromUTF8(tempString.c_str());

        if (!readXmlElementValue(tempString, folderPair, "Right")) return false;
        newPair.rightDirectory = wxString::FromUTF8(tempString.c_str());

        outputCfg.directoryPairs.push_back(newPair);
        folderPair = folderPair->NextSiblingElement();
    }

//###########################################################
    //read sync configuration
    if (!readXmlElementValue(outputCfg.cfg.syncConfiguration.exLeftSideOnly, syncConfig, "LeftOnly"))   return false;
    if (!readXmlElementValue(outputCfg.cfg.syncConfiguration.exRightSideOnly, syncConfig, "RightOnly")) return false;
    if (!readXmlElementValue(outputCfg.cfg.syncConfiguration.leftNewer, syncConfig, "LeftNewer"))       return false;
    if (!readXmlElementValue(outputCfg.cfg.syncConfiguration.rightNewer, syncConfig, "RightNewer"))     return false;
    if (!readXmlElementValue(outputCfg.cfg.syncConfiguration.different, syncConfig, "Different"))       return false;
//###########################################################
    //read filter settings
    if (!readXmlElementValue(outputCfg.cfg.filterIsActive, filter, "Active")) return false;

    if (!readXmlElementValue(tempString, filter, "Include"))  return false;
    outputCfg.cfg.includeFilter = wxString::FromUTF8(tempString.c_str());

    if (!readXmlElementValue(tempString, filter, "Exclude"))  return false;
    outputCfg.cfg.excludeFilter = wxString::FromUTF8(tempString.c_str());
//###########################################################
    //other
    if (!readXmlElementValue(outputCfg.cfg.useRecycleBin, miscSettings, "Recycler"))   return false;
    if (!readXmlElementValue(outputCfg.cfg.continueOnError, miscSettings, "Continue")) return false;

    return true;
}


bool XmlInput::readXmlGuiConfig(XmlGuiConfig& outputCfg)
{
    TiXmlElement* root = doc.RootElement();
    if (!root) return false;

    TiXmlHandle hRoot(root);

    //read GUI layout
    TiXmlElement* mainWindow = hRoot.FirstChild("GuiConfig").FirstChild("Windows").FirstChild("Main").ToElement();
    if (mainWindow)
    {
        if (!readXmlElementValue(outputCfg.hideFilteredElements, mainWindow, "HideFiltered"))
            outputCfg.hideFilteredElements = false;

        //read application window size and position
        if (!readXmlElementValue(outputCfg.widthNotMaximized, mainWindow, "Width"))
            outputCfg.widthNotMaximized = -1;
        if (!readXmlElementValue(outputCfg.heightNotMaximized, mainWindow, "Height"))
            outputCfg.heightNotMaximized = -1;
        if (!readXmlElementValue(outputCfg.posXNotMaximized, mainWindow, "PosX"))
            outputCfg.posXNotMaximized = -1;
        if (!readXmlElementValue(outputCfg.posYNotMaximized, mainWindow, "PosY"))
            outputCfg.posYNotMaximized = -1;
        if (!readXmlElementValue(outputCfg.isMaximized, mainWindow, "Maximized"))
            outputCfg.isMaximized = false;

//###########################################################
        //read column widths
        TiXmlElement* leftColumn = TiXmlHandle(mainWindow).FirstChild("LeftColumns").FirstChild("Width").ToElement();
        while (leftColumn)
        {
            const char* width = leftColumn->GetText();
            if (width) //may be NULL!!
                outputCfg.columnWidthLeft.push_back(stringToInt(width));
            else
                break;

            leftColumn = leftColumn->NextSiblingElement();
        }

        TiXmlElement* rightColumn = TiXmlHandle(mainWindow).FirstChild("RightColumns").FirstChild("Width").ToElement();
        while (rightColumn)
        {
            const char* width = rightColumn->GetText();
            if (width) //may be NULL!!
                outputCfg.columnWidthRight.push_back(stringToInt(width));
            else
                break;

            rightColumn = rightColumn->NextSiblingElement();
        }
    }

    return true;
}


bool XmlInput::readXmlBatchConfig(XmlBatchConfig& outputCfg)
{
    TiXmlElement* root = doc.RootElement();
    if (!root) return false;

    TiXmlHandle hRoot(root);

    //read batch settings
    TiXmlElement* batchConfig = hRoot.FirstChild("BatchConfig").ToElement();
    if (batchConfig)
    {
        //read application window size and position
        if (!readXmlElementValue(outputCfg.silent, batchConfig, "Silent"))
            outputCfg.silent = false;
    }

    return true;
}


XmlOutput::XmlOutput(const wxString& fileName, const XmlType type) :
        m_fileName(fileName)
{
    TiXmlBase::SetCondenseWhiteSpace(false); //do not condense whitespace characters

    TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", ""); //delete won't be necessary later; ownership passed to TiXmlDocument!
    doc.LinkEndChild(decl);

    TiXmlElement* root = new TiXmlElement("FreeFileSync");
    if (type == XML_GUI_CONFIG)
        root->SetAttribute("XmlType", "GUI"); //xml configuration type
    else if (type == XML_BATCH_CONFIG)
        root->SetAttribute("XmlType", "BATCH");
    else
        assert(false);
    doc.LinkEndChild(root);
}


bool XmlOutput::writeToFile()
{
    //workaround to get a FILE* from a unicode filename
    wxFFile dummyFile(m_fileName, wxT("wb"));
    if (!dummyFile.IsOpened())
        return false;

    FILE* outputFile = dummyFile.fp();

    return doc.SaveFile(outputFile); //save XML
}


void XmlOutput::addXmlElement(TiXmlElement* parent, const string& name, const string& value)
{
    if (parent)
    {
        TiXmlElement* subElement = new TiXmlElement(name);
        parent->LinkEndChild(subElement);
        subElement->LinkEndChild(new TiXmlText(value));
    }
}


void XmlOutput::addXmlElement(TiXmlElement* parent, const string& name, const int value)
{
    addXmlElement(parent, name, numberToString(value));
}


void XmlOutput::addXmlElement(TiXmlElement* parent, const string& name, const SyncDirection value)
{
    if (value == SYNC_DIR_LEFT)
        addXmlElement(parent, name, string("left"));
    else if (value == SYNC_DIR_RIGHT)
        addXmlElement(parent, name, string("right"));
    else if (value == SYNC_DIR_NONE)
        addXmlElement(parent, name, string("none"));
    else
        assert(false);
}


void XmlOutput::addXmlElement(TiXmlElement* parent, const string& name, const bool value)
{
    if (value)
        addXmlElement(parent, name, string("true"));
    else
        addXmlElement(parent, name, string("false"));
}


bool XmlOutput::writeXmlMainConfig(const XmlMainConfig& inputCfg)
{
    TiXmlElement* root = doc.RootElement();
    if (!root) return false;

    TiXmlElement* settings = new TiXmlElement("MainConfig");
    root->LinkEndChild(settings);

//###########################################################
    TiXmlElement* cmpSettings = new TiXmlElement("Comparison");
    settings->LinkEndChild(cmpSettings);

    //write compare algorithm
    addXmlElement(cmpSettings, "Variant", inputCfg.cfg.compareVar);

    //write folder pair(s)
    TiXmlElement* folders = new TiXmlElement("Folders");
    cmpSettings->LinkEndChild(folders);

    //write folder pairs
    for (vector<FolderPair>::const_iterator i = inputCfg.directoryPairs.begin(); i != inputCfg.directoryPairs.end(); ++i)
    {
        TiXmlElement* folderPair = new TiXmlElement("Pair");
        folders->LinkEndChild(folderPair);

        addXmlElement(folderPair, "Left", string((i->leftDirectory).ToUTF8()));
        addXmlElement(folderPair, "Right", string((i->rightDirectory).ToUTF8()));
    }

//###########################################################
    TiXmlElement* syncSettings = new TiXmlElement("Synchronization");
    settings->LinkEndChild(syncSettings);

    //write sync configuration
    TiXmlElement* syncConfig = new TiXmlElement("Directions");
    syncSettings->LinkEndChild(syncConfig);

    addXmlElement(syncConfig, "LeftOnly",   inputCfg.cfg.syncConfiguration.exLeftSideOnly);
    addXmlElement(syncConfig, "RightOnly",  inputCfg.cfg.syncConfiguration.exRightSideOnly);
    addXmlElement(syncConfig, "LeftNewer",  inputCfg.cfg.syncConfiguration.leftNewer);
    addXmlElement(syncConfig, "RightNewer", inputCfg.cfg.syncConfiguration.rightNewer);
    addXmlElement(syncConfig, "Different",  inputCfg.cfg.syncConfiguration.different);

//###########################################################
    TiXmlElement* miscSettings = new TiXmlElement("Miscellaneous");
    settings->LinkEndChild(miscSettings);

    //write filter settings
    TiXmlElement* filter = new TiXmlElement("Filter");
    miscSettings->LinkEndChild(filter);

    addXmlElement(filter, "Active", inputCfg.cfg.filterIsActive);
    addXmlElement(filter, "Include", string((inputCfg.cfg.includeFilter).ToUTF8()));
    addXmlElement(filter, "Exclude", string((inputCfg.cfg.excludeFilter).ToUTF8()));

    //other
    addXmlElement(miscSettings, "Recycler", inputCfg.cfg.useRecycleBin);
    addXmlElement(miscSettings, "Continue", inputCfg.cfg.continueOnError);
//###########################################################

    return true;
}


bool XmlOutput::writeXmlGuiConfig(const XmlGuiConfig& inputCfg)
{
    TiXmlElement* root = doc.RootElement();
    if (!root) return false;

    TiXmlElement* guiLayout = new TiXmlElement("GuiConfig");
    root->LinkEndChild(guiLayout);

    TiXmlElement* windows = new TiXmlElement("Windows");
    guiLayout->LinkEndChild(windows);

    TiXmlElement* mainWindow = new TiXmlElement("Main");
    windows->LinkEndChild(mainWindow);

    addXmlElement(mainWindow, "HideFiltered", inputCfg.hideFilteredElements);

    //window size
    addXmlElement(mainWindow, "Width", inputCfg.widthNotMaximized);
    addXmlElement(mainWindow, "Height", inputCfg.heightNotMaximized);

    //window position
    addXmlElement(mainWindow, "PosX", inputCfg.posXNotMaximized);
    addXmlElement(mainWindow, "PosY", inputCfg.posYNotMaximized);
    addXmlElement(mainWindow, "Maximized", inputCfg.isMaximized);

    //write column sizes
    TiXmlElement* leftColumn = new TiXmlElement("LeftColumns");
    mainWindow->LinkEndChild(leftColumn);

    for (unsigned int i = 0; i < inputCfg.columnWidthLeft.size(); ++i)
        addXmlElement(leftColumn, "Width", inputCfg.columnWidthLeft[i]);

    TiXmlElement* rightColumn = new TiXmlElement("RightColumns");
    mainWindow->LinkEndChild(rightColumn);

    for (unsigned int i = 0; i < inputCfg.columnWidthRight.size(); ++i)
        addXmlElement(rightColumn, "Width", inputCfg.columnWidthRight[i]);

    return true;
}


bool XmlOutput::writeXmlBatchConfig(const XmlBatchConfig& inputCfg)
{
    TiXmlElement* root = doc.RootElement();
    if (!root) return false;

    TiXmlElement* batchConfig = new TiXmlElement("BatchConfig");
    root->LinkEndChild(batchConfig);

    addXmlElement(batchConfig, "Silent", inputCfg.silent);

    return true;
}
bgstack15