OSDN Git Service

[denncoCreator] minor improvements
[dennco/denncoCreator.git] / Source / utils / dcskeltoncreatorutil.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 Oct-1, 2012.
18 //
19 #include "dcskeltoncreatorutil.h"
20
21 #include "utils/dcutil.h"
22
23 #include <QFile>
24 #include <QTextStream>
25
26 static QString SKELTON_PROPERTY_XML =
27         "<dennco>\n"
28         "<TickIntervalSec>1.0</TickIntervalSec>\n"
29         "<UIPath>/ui/index.html</UIPath>\n"
30         "<EnableHTTPServer>no</EnableHTTPServer>\n"
31         "<EnableSerialServer>no</EnableSerialServer>\n"
32         "</dennco>";
33
34 static QString SKELTON_UI_TOP =
35         "<html>\n"
36         "<head>\n"
37         "<title></title>\n"
38         "<script type=\"text/javascript\">\n"
39         "var timerId = setInterval(\"updateOutput()\", 500);\n"
40         "function updateOutput()\n"
41         "{\n"
42         "    //function for getting updated data from cells\n"
43         "}\n"
44         "\n"
45         "function stopTimer()\n"
46         "{\n"
47         "    clearInterval(timerId);\n"
48         "}\n"
49         "</script>\n"
50         "</head>\n"
51         "<body onunload=\"stopTimer()\">\n"
52         "</body>\n"
53         "</html>\n";
54
55 static QString SKELTON_CELLCODE =
56         "// Function: doInit\n"
57         "// This function will be called once when a cell is instantiated.\n"
58         "//\n"
59         "function doInit()\n"
60         "{\n"
61         "}\n"
62         "\n"
63         "// Function: doTick\n"
64         "// This function will be called once every tick interval time.\n"
65         "// The parameter time holds the elasped time from the beginning of the container in millisecond.\n"
66         "function doTick(time)\n"
67         "{\n"
68         "}\n"
69         "\n"
70         "// Function: doDestroy\n"
71         "// This function will be called once just before the cell instance is destroyed.\n"
72         "//\n"
73         "function doDestroy()\n"
74         "{\n"
75         "}\n"
76         "\n";
77
78 static QString SKELTOM_CUSTOMSCRIPT =
79         "// Script below will be called once when a cell is instantiated.\n"
80         "// When a cell has a attached cell code class, custom script here will be called first and \n"
81         "// doInit function in cell code class follows. \n"
82         "// You can define functions here, too.\n"
83         "// In the case a cell doesn't have a cell code class attached, you can define doInit, \n"
84         "// doTick and doDestroy functions here then they work jast as like functions defined in \n"
85         "// cell code class does.\n\n";
86
87
88 bool DCSkeltonCreatorUtil::createNewContent(const QString &contentRootPath)
89 {
90     if (QFileInfo(contentRootPath).exists())
91     {
92         QMessageBox msgBox;
93         msgBox.setText(tr("ERROR! The file already exist"));
94         return false;
95     }
96
97     QDir dir;
98     if (!dir.mkdir(contentRootPath))
99     {
100         QMessageBox msgBox;
101         msgBox.setText(tr("ERROR! Failed to create a directory"));
102         return false;
103     }
104
105     QString conteinerRoot = contentRootPath + "/Container";
106     if (!dir.mkdir(conteinerRoot))
107     {
108         QMessageBox msgBox;
109         msgBox.setText(tr("ERROR! Failed to create a directory"));
110         DCUtil::removeDirRecursive(contentRootPath);
111         return false;
112     }
113
114     QFile propertyFile(contentRootPath + "/property.xml");
115     propertyFile.open(QIODevice::WriteOnly);
116     QTextStream out(&propertyFile);
117     out << SKELTON_PROPERTY_XML;
118     propertyFile.close();
119
120     QString uiRoot = contentRootPath + "/ui";
121     if (!dir.mkdir(uiRoot))
122     {
123         QMessageBox msgBox;
124         msgBox.setText(tr("ERROR! Failed to create a directory"));
125         DCUtil::removeDirRecursive(contentRootPath);
126         return false;
127     }
128
129
130     QFile uiTopPage(uiRoot + "/index.html");
131     uiTopPage.open(QIODevice::WriteOnly);
132     QTextStream out2(&uiTopPage);
133     out2 << SKELTON_UI_TOP;
134     uiTopPage.close();
135
136     return true;
137 }
138
139 QString DCSkeltonCreatorUtil::getDefaultCellCodeClassScript()
140 {
141     return SKELTON_CELLCODE;
142 }
143
144 QString DCSkeltonCreatorUtil::getDefaultCustomScript()
145 {
146     return SKELTOM_CUSTOMSCRIPT;
147 }