1 // Copyright (c) 2012 Dennco Project
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.
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.
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/>.
17 // Created by tkawata on Oct-2, 2012.
19 #include "dccontainersaver.h"
21 #include "dccontainer.h"
24 #include "dcvccellcode.h"
26 #include "dccellcode.h"
27 #include "dcreceptor.h"
29 #include "dcaxonterminal.h"
30 #include "dcvpagecomponent.h"
32 #include "utils/dcutil.h"
34 #include <QDomDocument>
38 #include <QCryptographicHash>
41 static QDomDocument* constructDocumentForPage(DCContainer *container, const DCVCPage *page);
43 DCContainerSaver::DCContainerSaver(DCContainer *container) : d_container(container)
48 DCContainerSaver::~DCContainerSaver()
53 QString DCContainerSaver::calculateHashForPage(const DCVCPage *page)
55 QCryptographicHash hash(QCryptographicHash::Md5);
56 QDomDocument *pDoc1 = constructDocumentForPage(d_container, page);
59 hash.addData(pDoc1->toString(-1).toLocal8Bit());
64 QDomElement doc2Root = doc2.createElement("denncoCreator");
65 page->saveAttributesToXML(&doc2, &doc2Root);
66 doc2.appendChild(doc2Root);
67 hash.addData(doc2.toString(-1).toLocal8Bit());
69 return QString(hash.result().toHex());
72 bool DCContainerSaver::saveAll(const QString& containerRootPath)
74 const QMap<QString,DCVCPage*> *pages = d_container->getScene()->getPages();
75 QMapIterator<QString, DCVCPage*> i(*pages);
80 DCVCPage *page = i.value();
81 if (!saveForPage(containerRootPath, page))
89 bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *page)
91 QString pageFilePath = containerRootPath;
92 pageFilePath.append(page->getLocationPath());
94 QDomDocument *doc = constructDocumentForPage(d_container, page);
98 QFile file(pageFilePath);
99 QFileInfo pathInfo = QFileInfo(pageFilePath);
100 QDir dir = pathInfo.dir();
102 dir.mkpath(dir.absolutePath());
105 if (file.open(QFile::WriteOnly | QFile::Truncate))
107 QTextStream out(&file);
117 r = d_container->getScene()->saveSceneForPage(containerRootPath, page);
122 page->updateSavedState(d_container);
128 QDomDocument* constructDocumentForPage(DCContainer *conteinr, const DCVCPage *page)
130 QDomDocument *doc = new QDomDocument("html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");
131 doc->createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
133 QDomElement root = doc->createElementNS("http://www.w3.org/1999/xhtml","html");
134 root.setAttribute("lang", "en");
135 QDomElement body = doc->createElement("body");
136 root.appendChild(body);
138 const QList<DCVPageComponent*>* vcells = page->getCells();
139 for (int i = 0; i < vcells->length(); i++)
141 DCCell *cell = vcells->at(i)->getOwnerCell();
142 QString cellName = QString::fromStdString(cell->getName());
144 QDomElement aCellTag = doc->createElement("a");
145 aCellTag.setAttribute("define", "cell");
146 aCellTag.setAttribute("name", cellName);
148 QDomElement aCellCodeTag = doc->createElement("a");
149 aCellCodeTag.setAttribute("parameter", "cellcode");
150 if (cell->getIsCellCodeAssgined())
152 aCellCodeTag.setAttribute("href", QString::fromStdString(cell->getCellCode()->getFQNName()));
156 aCellCodeTag.setAttribute("type", cell->getType());
158 aCellTag.appendChild(aCellCodeTag);
159 DCAxon *axon = cell->getAxon();
160 for (int j = 0; j < axon->getNumberOfTerminals(); j++)
162 DCAxonTerminal *terminal = axon->getTerminalAt(j);
163 DCReceptor *receptor = terminal->getTarget();
166 DCCell *targetCell = receptor->getOwnerCell();
169 QString connectionPath = QString::fromStdString(targetCell->getLocation());
170 connectionPath.append("#");
171 connectionPath.append(QString::fromStdString(targetCell->getName()));
172 QString receptorName = QString::fromStdString(targetCell->getReceptorName(receptor));
174 QDomElement aConnectionTag = doc->createElement("a");
175 aConnectionTag.setAttribute("parameter", "connection");
176 aConnectionTag.setAttribute("href", connectionPath);
177 aConnectionTag.setAttribute("receptor", receptorName);
178 aCellTag.appendChild(aConnectionTag);
182 QDomElement preScriptTag = doc->createElement("pre");
183 preScriptTag.setAttribute("parameter", "script");
184 QDomCDATASection scriptCData = doc->createCDATASection(conteinr->readCustomScriptFromWorkFile(cell));
185 preScriptTag.appendChild(scriptCData);
186 aCellTag.appendChild(preScriptTag);
187 body.appendChild(aCellTag);
190 const QList<DCVPageComponent*>* vcellcodeclasses = page->getCellCodeClasses();
191 for (int i = 0; i < vcellcodeclasses->length(); i++)
193 DCCellCode *cellcodeclass = dynamic_cast<DCVCCellCode*>(vcellcodeclasses->at(i))->getOwnerCellCodeClass();
194 QString cellCodeClassName = DCUtil::getNameFromFQNPath(QString::fromStdString(cellcodeclass->getFQNName()));
196 QDomElement aCellCodeTag = doc->createElement("a");
197 aCellCodeTag.setAttribute("define", "cellcode");
198 aCellCodeTag.setAttribute("name", cellCodeClassName);
199 aCellCodeTag.setAttribute("type", QString::fromStdString(cellcodeclass->getCellAPIName()));
200 QDomElement preScriptTag = doc->createElement("pre");
201 preScriptTag.setAttribute("parameter", "script");
202 QDomCDATASection scriptCData = doc->createCDATASection(conteinr->readCellCodeScriptFromFile(cellcodeclass));
203 preScriptTag.appendChild(scriptCData);
204 aCellCodeTag.appendChild(preScriptTag);
205 body.appendChild(aCellCodeTag);
208 doc->appendChild(root);