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"
33 #include <QDomDocument>
38 DCContainerSaver::DCContainerSaver(DCContainer *container) : d_container(container)
43 DCContainerSaver::~DCContainerSaver()
48 bool DCContainerSaver::saveAll(const QString& containerRootPath)
50 const QMap<QString,DCVCPage*> *pages = d_container->getScene()->getPages();
51 QMapIterator<QString, DCVCPage*> i(*pages);
56 DCVCPage *page = i.value();
57 if (!saveForPage(containerRootPath, page))
65 bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *page)
67 QString pageFilePath = containerRootPath;
68 pageFilePath.append(page->getLocationPath());
70 QDomDocument doc("html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");
71 doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
73 QDomElement root = doc.createElementNS("http://www.w3.org/1999/xhtml","html");
74 root.setAttribute("lang", "en");
75 QDomElement body = doc.createElement("body");
76 root.appendChild(body);
78 const QList<DCVPageComponent*>* vcells = page->getCells();
79 for (int i = 0; i < vcells->length(); i++)
81 DCCell *cell = vcells->at(i)->getOwnerCell();
82 QString cellName = QString::fromStdString(cell->getName());
84 QDomElement aCellTag = doc.createElement("a");
85 aCellTag.setAttribute("define", "cell");
86 aCellTag.setAttribute("name", cellName);
88 QDomElement aCellCodeTag = doc.createElement("a");
89 aCellCodeTag.setAttribute("parameter", "cellcode");
90 if (cell->getCellCode() && cell->getCellCode() != d_container->getEmptyCellCodeClass())
92 aCellCodeTag.setAttribute("href", QString::fromStdString(cell->getCellCode()->getName()));
96 aCellCodeTag.setAttribute("type", cell->getType());
98 aCellTag.appendChild(aCellCodeTag);
99 DCAxon *axon = cell->getAxon();
100 for (int j = 0; j < axon->getNumberOfTerminals(); j++)
102 DCAxonTerminal *terminal = axon->getTerminalAt(j);
103 DCReceptor *receptor = dynamic_cast<DCReceptor*>(terminal->getTarget());
106 DCCell *targetCell = dynamic_cast<DCCell*>(receptor->getOwnerCell());
109 QString connectionPath = QString::fromStdString(targetCell->getLocation());
110 connectionPath.append("#");
111 connectionPath.append(QString::fromStdString(targetCell->getName()));
112 QString receptorName = QString::fromStdString(targetCell->getReceptorName(receptor));
114 QDomElement aConnectionTag = doc.createElement("a");
115 aConnectionTag.setAttribute("parameter", "connection");
116 aConnectionTag.setAttribute("href", connectionPath);
117 aConnectionTag.setAttribute("receptor", receptorName);
118 aCellTag.appendChild(aConnectionTag);
122 QDomElement preScriptTag = doc.createElement("pre");
123 preScriptTag.setAttribute("parameter", "script");
124 QDomCDATASection scriptCData = doc.createCDATASection(d_container->readCustomScriptFromWorkFile(cell));
125 preScriptTag.appendChild(scriptCData);
126 aCellTag.appendChild(preScriptTag);
127 body.appendChild(aCellTag);
130 const QList<DCVPageComponent*>* vcellcodeclasses = page->getCellCodeClasses();
131 for (int i = 0; i < vcellcodeclasses->length(); i++)
133 DCCellCode *cellcodeclass = dynamic_cast<DCVCCellCode*>(vcellcodeclasses->at(i))->getOwnerCellCodeClass();
134 QString cellCodeClassName = QString::fromStdString(cellcodeclass->getName());
135 if (cellCodeClassName.indexOf("#") >= 0)
136 cellCodeClassName = cellCodeClassName.mid(cellCodeClassName.indexOf("#")+1);
138 QDomElement aCellCodeTag = doc.createElement("a");
139 aCellCodeTag.setAttribute("define", "cellcode");
140 aCellCodeTag.setAttribute("name", cellCodeClassName);
141 aCellCodeTag.setAttribute("type", QString::fromStdString(cellcodeclass->getCellAPIName()));
142 QDomElement preScriptTag = doc.createElement("pre");
143 preScriptTag.setAttribute("parameter", "script");
144 QDomCDATASection scriptCData = doc.createCDATASection(d_container->readCellCodeScriptFromFile(cellcodeclass));
145 preScriptTag.appendChild(scriptCData);
146 aCellCodeTag.appendChild(preScriptTag);
147 body.appendChild(aCellCodeTag);
150 doc.appendChild(root);
152 QFile file(pageFilePath);
153 QFileInfo pathInfo = QFileInfo(pageFilePath);
154 QDir dir = pathInfo.dir();
156 dir.mkpath(dir.absolutePath());
159 if (file.open(QFile::WriteOnly | QFile::Truncate))
161 QTextStream out(&file);
168 r = d_container->getScene()->saveSceneForPage(containerRootPath, page);