OSDN Git Service

[denncoCreator] fixed a crash bug.
[dennco/denncoCreator.git] / Source / dccontainersaver.cpp
index 49cf023..8691b46 100644 (file)
 #include <QMap>
 #include <QFile>
 #include <QDir>
+#include <QCryptographicHash>
+#include <QByteArray>
+
+static QDomDocument* constructDocumentForPage(DCContainer *container, const DCVCPage *page);
 
 DCContainerSaver::DCContainerSaver(DCContainer *container) : d_container(container)
 {
@@ -46,6 +50,25 @@ DCContainerSaver::~DCContainerSaver()
 
 }
 
+QString DCContainerSaver::calculateHashForPage(const DCVCPage *page)
+{
+    QCryptographicHash hash(QCryptographicHash::Md5);
+    QDomDocument *pDoc1 = constructDocumentForPage(d_container, page);
+    if (pDoc1)
+    {
+        hash.addData(pDoc1->toString(-1).toLocal8Bit());
+        delete pDoc1;
+    }
+
+    QDomDocument doc2;
+    QDomElement doc2Root = doc2.createElement("denncoCreator");
+    page->saveAttributesToXML(&doc2, &doc2Root);
+    doc2.appendChild(doc2Root);
+    hash.addData(doc2.toString(-1).toLocal8Bit());
+
+    return QString(hash.result().toHex());
+}
+
 bool DCContainerSaver::saveAll(const QString& containerRootPath)
 {
     const QMap<QString,DCVCPage*> *pages = d_container->getScene()->getPages();
@@ -68,12 +91,48 @@ bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *p
     QString pageFilePath = containerRootPath;
     pageFilePath.append(page->getLocationPath());
 
-    QDomDocument doc("html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");
-    doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
+    QDomDocument *doc = constructDocumentForPage(d_container, page);
+    if (!doc)
+        return false;
+
+    QFile file(pageFilePath);
+    QFileInfo pathInfo = QFileInfo(pageFilePath);
+    QDir dir = pathInfo.dir();
+    if (!dir.exists())
+        dir.mkpath(dir.absolutePath());
+
+    bool r = false;
+    if (file.open(QFile::WriteOnly | QFile::Truncate))
+    {
+        QTextStream out(&file);
+        doc->save(out, 4);
+        r = true;
+    }
+    file.close();
+
+    delete doc;
+
+    if (r)
+    {
+        r = d_container->getScene()->saveSceneForPage(containerRootPath, page);
+    }
+
+    if (r)
+    {
+        page->updateSavedState(d_container);
+    }
+
+    return r;
+}
+
+QDomDocument* constructDocumentForPage(DCContainer *conteinr, const DCVCPage *page)
+{
+    QDomDocument *doc = new QDomDocument("html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");
+    doc->createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
 
-    QDomElement root = doc.createElementNS("http://www.w3.org/1999/xhtml","html");
+    QDomElement root = doc->createElementNS("http://www.w3.org/1999/xhtml","html");
     root.setAttribute("lang", "en");
-    QDomElement body = doc.createElement("body");
+    QDomElement body = doc->createElement("body");
     root.appendChild(body);
 
     const QList<DCVPageComponent*>* vcells = page->getCells();
@@ -82,13 +141,13 @@ bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *p
         DCCell *cell = vcells->at(i)->getOwnerCell();
         QString cellName = QString::fromStdString(cell->getName());
 
-        QDomElement aCellTag = doc.createElement("a");
+        QDomElement aCellTag = doc->createElement("a");
         aCellTag.setAttribute("define", "cell");
         aCellTag.setAttribute("name", cellName);
 
-        QDomElement aCellCodeTag = doc.createElement("a");
+        QDomElement aCellCodeTag = doc->createElement("a");
         aCellCodeTag.setAttribute("parameter", "cellcode");
-        if (cell->getCellCode() && cell->getCellCode() != d_container->getEmptyCellCodeClass())
+        if (cell->getIsCellCodeAssgined())
         {
             aCellCodeTag.setAttribute("href", QString::fromStdString(cell->getCellCode()->getFQNName()));
         }
@@ -101,10 +160,10 @@ bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *p
         for (int j = 0; j < axon->getNumberOfTerminals(); j++)
         {
             DCAxonTerminal *terminal = axon->getTerminalAt(j);
-            DCReceptor *receptor = dynamic_cast<DCReceptor*>(terminal->getTarget());
+            DCReceptor *receptor = terminal->getTarget();
             if (receptor)
             {
-                DCCell *targetCell = dynamic_cast<DCCell*>(receptor->getOwnerCell());
+                DCCell *targetCell = receptor->getOwnerCell();
                 if (targetCell)
                 {
                     QString connectionPath = QString::fromStdString(targetCell->getLocation());
@@ -112,7 +171,7 @@ bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *p
                     connectionPath.append(QString::fromStdString(targetCell->getName()));
                     QString receptorName = QString::fromStdString(targetCell->getReceptorName(receptor));
 
-                    QDomElement aConnectionTag = doc.createElement("a");
+                    QDomElement aConnectionTag = doc->createElement("a");
                     aConnectionTag.setAttribute("parameter", "connection");
                     aConnectionTag.setAttribute("href", connectionPath);
                     aConnectionTag.setAttribute("receptor", receptorName);
@@ -120,9 +179,9 @@ bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *p
                 }
             }
         }
-        QDomElement preScriptTag = doc.createElement("pre");
+        QDomElement preScriptTag = doc->createElement("pre");
         preScriptTag.setAttribute("parameter", "script");
-        QDomCDATASection scriptCData = doc.createCDATASection(d_container->readCustomScriptFromWorkFile(cell));
+        QDomCDATASection scriptCData = doc->createCDATASection(conteinr->readCustomScriptFromWorkFile(cell));
         preScriptTag.appendChild(scriptCData);
         aCellTag.appendChild(preScriptTag);
         body.appendChild(aCellTag);
@@ -134,37 +193,19 @@ bool DCContainerSaver::saveForPage(const QString& containerRootPath, DCVCPage *p
         DCCellCode *cellcodeclass = dynamic_cast<DCVCCellCode*>(vcellcodeclasses->at(i))->getOwnerCellCodeClass();
         QString cellCodeClassName = DCUtil::getNameFromFQNPath(QString::fromStdString(cellcodeclass->getFQNName()));
 
-        QDomElement aCellCodeTag = doc.createElement("a");
+        QDomElement aCellCodeTag = doc->createElement("a");
         aCellCodeTag.setAttribute("define", "cellcode");
         aCellCodeTag.setAttribute("name", cellCodeClassName);
         aCellCodeTag.setAttribute("type", QString::fromStdString(cellcodeclass->getCellAPIName()));
-        QDomElement preScriptTag = doc.createElement("pre");
+        QDomElement preScriptTag = doc->createElement("pre");
         preScriptTag.setAttribute("parameter", "script");
-        QDomCDATASection scriptCData = doc.createCDATASection(d_container->readCellCodeScriptFromFile(cellcodeclass));
+        QDomCDATASection scriptCData = doc->createCDATASection(conteinr->readCellCodeScriptFromFile(cellcodeclass));
         preScriptTag.appendChild(scriptCData);
         aCellCodeTag.appendChild(preScriptTag);
         body.appendChild(aCellCodeTag);
     }
 
-    doc.appendChild(root);
+    doc->appendChild(root);
 
-    QFile file(pageFilePath);
-    QFileInfo pathInfo = QFileInfo(pageFilePath);
-    QDir dir = pathInfo.dir();
-    if (!dir.exists())
-        dir.mkpath(dir.absolutePath());
-
-    bool r = false;
-    if (file.open(QFile::WriteOnly | QFile::Truncate))
-    {
-        QTextStream out(&file);
-        doc.save(out, 4);
-        r = true;
-    }
-
-    if (r)
-    {
-        r = d_container->getScene()->saveSceneForPage(containerRootPath, page);
-    }
-    return r;
+    return doc;
 }