OSDN Git Service

[denncoCreator] minor improvements for window titles and application close behavior.
[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 #include "uieditor/dcuieditor.h"
29 #include "propertyeditor/dcpropertyeditor.h"
30
31 #include <stdlib.h>
32 #include <sstream>
33 #include <iostream>
34
35 DCContent::DCContent(DCCreator *creator, std::string contentPath) :
36     d_creator(creator), d_valid(false), d_container(0)
37 {
38     d_contentRootPath = QString::fromStdString(contentPath);
39     d_container = (DCContainer*) TKContainer::createContainer();
40     d_container->setContent(this);
41
42     dnGlobal()->updateRunningStatus(DNGlobal::STOPPED);
43     dnGlobal()->resetErrorStatus();
44
45     std::string containerRoot = contentPath;
46     containerRoot.append("/Container");
47
48     bool succeeded = false;
49     succeeded = parseSettingFile(contentPath.c_str());
50     if (!succeeded || !dnGlobal()->isErrorStatusNormal())
51     {
52         dnNotifyError("property.xml parse error", "failed to parse setting file /property.xml");
53         return;
54     }
55
56     std::string dataStorePath = containerRoot;
57     dataStorePath.append("/data.db");
58
59     succeeded = d_container->setDataStore(dataStorePath.c_str());
60     if (!succeeded)
61     {
62         dnNotifyError("Initialization failed","Failed to the setup data store");
63         return;
64     }
65
66     succeeded = parseContainerFile(containerRoot.c_str());
67     if (!succeeded)
68     {
69         dnNotifyError("Initialization failed", "Failed to parse container file");
70         return;
71     }
72
73     d_valid = true;
74 }
75
76 DCContent::~DCContent()
77 {
78     d_valid = false;
79     if (d_container)
80     {
81         delete d_container;
82         d_container = 0;
83     }
84 }
85
86 bool DCContent::getIsModified() const
87 {
88     if (d_valid && d_container && d_container->getIsModified())
89     {
90         return true;
91     }
92
93     if (DCPropertyEditor::getEditor()->isVisible())
94         DCPropertyEditor::getEditor()->close();
95
96     if (DCUIEditor::getEditor()->isVisible())
97         DCUIEditor::getEditor()->close();
98
99     return false;
100 }
101
102 bool DCContent::parseSettingFile(const char *contentRoot)
103 {
104     bool valid = false;
105
106     DNXML *xml = DNXML::createXMLFromFile(contentRoot, "property.xml");
107     if (xml)
108     {
109         valid = true;
110         DNXMLElement *element = xml->getRoot();
111         if (!element)
112         {
113             valid = false;
114             std::string message = "Failed to load property.xml file";
115             dnNotifyError("Initialization failed",message);
116         }
117
118         if (valid && element->name != "dennco")
119         {
120             valid = false;
121             std::string message = "First element of property.xml should be <dennco>";
122             dnNotifyError("Initialization failed",message);
123         }
124
125         if (valid)
126         {
127             DNXMLElement *e = element->inner;
128             while(e)
129             {
130                 std::string pname = upperString(e->name);
131
132                 if (pname == "TICKINTERVALSEC")
133                 {
134                     std::istringstream is(e->text);
135                     float t = 0.0;
136                     is >> t;
137                     if (t>0)
138                     {
139                         //TODO
140                     }
141                     else
142                     {
143                         valid = false;
144                         std::string message = "Error in property.xml. TickIntervalSec is not configured properly.";
145                         dnNotifyError("Initialization failed",message);
146                     }
147                 }
148                 else if (pname == "UIPATH")
149                 {
150                     // TODO
151                 }
152                 else if (pname == "ENABLEHTTPSERVER")
153                 {
154
155                 }
156                 else if (pname == "ENABLESERIALSERVER")
157                 {
158                     if ( upperString(e->text) == "YES")
159                     {
160                         //TODO
161                     }
162                     else
163                     {
164                         //TODO
165                     }
166                 }
167                 else if (pname == "SERIALSPEED")
168                 {
169                     //TODO
170                 }
171                 e = e->next;
172             }
173         }
174
175         delete xml;
176     }
177     return valid;
178 }
179
180 bool DCContent::parseContainerFile(const char *containerRoot)
181 {
182     DNContainerBuilder builder(d_container);
183     d_container->beganBuildContainer();
184     bool r = builder.buildContainerFromXHTML(containerRoot);
185     d_container->endedBuildContainer();
186
187     return r;
188 }
189
190 bool DCContent::saveAll(const QString& contentRoot)
191 {
192     DCContainerSaver containerSaver(d_container);
193
194     QString containerRoot = contentRoot + "/Container";
195     return containerSaver.saveAll(containerRoot);
196 }
197
198 bool DCContent::saveForPage(const QString &contentRoot, DCVCPage *page)
199 {
200     DCContainerSaver containerSaver(d_container);
201
202     QString containerRoot = contentRoot + "/Container";
203     return containerSaver.saveForPage(containerRoot, page);
204 }