OSDN Git Service

[denncoCreator] Implementing save functionality. The work is still in progress.
[dennco/denncoCreator.git] / Source / dccontent.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 //
17 //  Created by tkawata on Sep-30, 2012.
18 //
19 #include "dccontent.h"
20
21 #include "dccontainer.h"
22 #include "DNContainerBuilder.h"
23 #include "DNGlobal.h"
24 #include "DNXML.h"
25 #include "DNXMLElement.h"
26 #include "DNUtils.h"
27 #include "dccontainersaver.h"
28
29 #include <stdlib.h>
30 #include <sstream>
31 #include <iostream>
32
33 DCContent::DCContent(DCCreator *creator, std::string contentPath) :
34     d_creator(creator), d_valid(false), d_container(0)
35 {
36     d_container = (DCContainer*) TKContainer::createContainer();
37     d_container->setContent(this);
38
39     dnGlobal()->updateRunningStatus(DNGlobal::STOPPED);
40     dnGlobal()->resetErrorStatus();
41
42     std::string containerRoot = contentPath;
43     containerRoot.append("/Container");
44
45     bool succeeded = false;
46     succeeded = parseSettingFile(contentPath.c_str());
47     if (!succeeded || !dnGlobal()->isErrorStatusNormal())
48     {
49         dnNotifyError("property.xml parse error", "failed to parse setting file /property.xml");
50         return;
51     }
52
53     std::string dataStorePath = containerRoot;
54     dataStorePath.append("/data.db");
55
56     succeeded = d_container->setDataStore(dataStorePath.c_str());
57     if (!succeeded)
58     {
59         dnNotifyError("Initialization failed","Failed to the setup data store");
60         return;
61     }
62
63     succeeded = parseContainerFile(containerRoot.c_str());
64     if (!succeeded)
65     {
66         dnNotifyError("Initialization failed", "Failed to parse container file");
67         return;
68     }
69
70     d_valid = true;
71 }
72
73 DCContent::~DCContent()
74 {
75     d_valid = false;
76     if (d_container)
77     {
78         delete d_container;
79         d_container = 0;
80     }
81 }
82
83 bool DCContent::parseSettingFile(const char *contentRoot)
84 {
85     bool valid = false;
86
87     DNXML *xml = DNXML::createXMLFromFile(contentRoot, "property.xml");
88     if (xml)
89     {
90         valid = true;
91         DNXMLElement *element = xml->getRoot();
92         if (!element)
93         {
94             valid = false;
95             std::string message = "Failed to load property.xml file";
96             dnNotifyError("Initialization failed",message);
97         }
98
99         if (valid && element->name != "dennco")
100         {
101             valid = false;
102             std::string message = "First element of property.xml should be <dennco>";
103             dnNotifyError("Initialization failed",message);
104         }
105
106         if (valid)
107         {
108             DNXMLElement *e = element->inner;
109             while(e)
110             {
111                 std::string pname = upperString(e->name);
112
113                 if (pname == "TICKINTERVALSEC")
114                 {
115                     std::istringstream is(e->text);
116                     float t = 0.0;
117                     is >> t;
118                     if (t>0)
119                     {
120                         //TODO
121                     }
122                     else
123                     {
124                         valid = false;
125                         std::string message = "Error in property.xml. TickIntervalSec is not configured properly.";
126                         dnNotifyError("Initialization failed",message);
127                     }
128                 }
129                 else if (pname == "UIPATH")
130                 {
131                     // TODO
132                 }
133                 else if (pname == "ENABLEHTTPSERVER")
134                 {
135
136                 }
137                 else if (pname == "ENABLESERIALSERVER")
138                 {
139                     if ( upperString(e->text) == "YES")
140                     {
141                         //TODO
142                     }
143                     else
144                     {
145                         //TODO
146                     }
147                 }
148                 else if (pname == "SERIALSPEED")
149                 {
150                     //TODO
151                 }
152                 e = e->next;
153             }
154         }
155
156         delete xml;
157     }
158     return valid;
159 }
160
161 bool DCContent::parseContainerFile(const char *containerRoot)
162 {
163     DNContainerBuilder builder(d_container);
164     d_container->beganBuildContainer();
165     bool r = builder.buildContainerFromXHTML(containerRoot);
166     d_container->endedBuildContainer();
167
168     return r;
169 }
170
171 bool DCContent::saveAll(const QString& containerRoot)
172 {
173     DCContainerSaver saver(d_container);
174     return saver.saveAll(containerRoot);
175 }