File: Metadata.cpp

package info (click to toggle)
gm-assistant 1.2.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,116 kB
  • sloc: cpp: 7,281; makefile: 3
file content (135 lines) | stat: -rw-r--r-- 4,162 bytes parent folder | download | duplicates (4)
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
/*************************************************************************
* Copyright © 2013-2020 Vincent Prat & Simon Nicolas
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*************************************************************************/

#include "Metadata.h"
#include <ctime>
#include <sstream>
#include <Poco/DOM/Text.h>

using namespace std;

Metadata::Metadata()
{
}

void Metadata::fromXML(const Poco::XML::Element *root)
{
    Poco::XML::Element *element = root->getChildElement("title");
    if (element)
    {
        setTitle(element->getAttribute("value"));
    }
    element = root->getChildElement("author");
    if (element)
    {
        setAuthor(element->getAttribute("value"));
    }
    element = root->getChildElement("creation");
    if (element)
    {
        setCreationDate(element->getAttribute("date"));
    }
    element = root->getChildElement("description");
    if (element)
    {
        setDescription(element->innerText());
    }
    element = root->getChildElement("rpg");
    if (element)
    {
        setRpg(element->getAttribute("value"));
    }
    element = root->getChildElement("players");
    if (element)
    {
        setPlayers(element->getAttribute("value"));
    }
    element = root->getChildElement("game");
    if (element)
    {
        setGameDate(element->getAttribute("date"));
    }
}

void Metadata::toXML(Poco::XML::Element *root) const
{
    using namespace Poco::XML;

    Document *document = root->ownerDocument();
    Element *tmp = document->createElement("title");
    root->appendChild(tmp);
    tmp->setAttribute("value", sTitle);
    tmp = document->createElement("author");
    root->appendChild(tmp);
    tmp->setAttribute("value", sAuthor);
    tmp = document->createElement("creation");
    root->appendChild(tmp);
    stringstream bufCreation;
    bufCreation << dCreation.day() << "/" << dCreation.month() << "/" << dCreation.year();
    tmp->setAttribute("date", bufCreation.str());
    tmp = document->createElement("description");
    root->appendChild(tmp);
    tmp->appendChild(document->createTextNode(sDescription));
    tmp = document->createElement("rpg");
    root->appendChild(tmp);
    tmp->setAttribute("value", sRpg);
    tmp = document->createElement("players");
    root->appendChild(tmp);
    tmp->setAttribute("value", sPlayers);
    tmp = document->createElement("game");
    root->appendChild(tmp);
    stringstream bufGame;
    bufGame << dGame.day() << "/" << dGame.month() << "/" << dGame.year();
    tmp->setAttribute("date", bufGame.str());
}

bool Metadata::operator!=(const Metadata &metadata) const
{
    return (sTitle != metadata.sTitle || sAuthor != metadata.sAuthor || dCreation != metadata.dCreation || sDescription != metadata.sDescription || sRpg != metadata.sRpg || sPlayers != metadata.sPlayers || dGame != metadata.dGame);
}

// Date methods

Metadata::Date::Date()
{
    time_t currentTime;
    time(&currentTime);
    struct tm *time_info = localtime(&currentTime);
    iDay = time_info->tm_mday;
    iMonth = time_info->tm_mon + 1;
    iYear = time_info->tm_year + 1900;
}

Metadata::Date::Date(int day, int month, int year): iDay(day), iMonth(month), iYear(year)
{
}

Metadata::Date::Date(const string &date)
{
    istringstream buf(date);
    buf >> iDay;
    buf.ignore(1);
    buf >> iMonth;
    buf.ignore(1);
    buf >> iYear;
}

bool Metadata::Date::operator!=(const Date &date) const
{
    return (iDay != date.iDay || iMonth != date.iMonth || iYear != date.iYear);
}