1/*
2 * Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#include "projecttemplate.h"
18#include <QFile>
19#include <QMessageBox>
20#include "mainwindow.h"
21#include "settings.h"
22
23ProjectTemplate::ProjectTemplate(QObject *parent) : QObject(parent)
24{
25
26}
27
28int ProjectTemplate::unitCount()
29{
30 if (!mIni || mVersion<=0)
31 return 0;
32 return mIni->GetLongValue("Project","UnitCount",0);
33}
34
35PTemplateUnit ProjectTemplate::unit(int index)
36{
37 if (!mIni || mVersion<=0)
38 return PTemplateUnit();
39 QString section = QString("Unit%1").arg(index);
40 PTemplateUnit unit = std::make_shared<TemplateUnit>();
41 unit->Source = fromByteArray(mIni->GetValue(toByteArray(section), "Source", ""));
42 unit->Target = fromByteArray(mIni->GetValue(toByteArray(section), "Target", ""));
43 unit->CText = fromByteArray(mIni->GetValue(toByteArray(section), "C", ""));
44 unit->CppText = fromByteArray(mIni->GetValue(toByteArray(section), "Cpp", ""));
45 if (unit->CppText.isEmpty())
46 unit->CppText = unit->CText;
47
48 unit->CName = fromByteArray(mIni->GetValue(toByteArray(section), "CName", ""));
49 unit->CppName = fromByteArray(mIni->GetValue(toByteArray(section), "CppName", ""));
50 if (unit->CppName.isEmpty())
51 unit->CppName = unit->CName;
52 return unit;
53}
54
55void ProjectTemplate::readTemplateFile(const QString &fileName)
56{
57 if (mIni)
58 mIni=nullptr;
59 QFile file(fileName);
60 if (file.open(QFile::ReadOnly)) {
61 mFileName = fileName;
62 mIni = std::make_shared<SimpleIni>();
63 QByteArray data = file.readAll();
64 if (mIni->LoadData(data.toStdString()) != SI_OK) {
65 QMessageBox::critical(pMainWindow,
66 tr("Read failed."),
67 tr("Can't read template file '%1'.").arg(fileName),
68 QMessageBox::Ok);
69 return;
70 }
71 } else {
72 QMessageBox::critical(pMainWindow,
73 tr("Can't Open Template"),
74 tr("Can't open template file '%1' for read.").arg(fileName),
75 QMessageBox::Ok);
76 return;
77 }
78
79 mVersion = mIni->GetLongValue("Template", "Ver", 0);
80 if (mVersion<=0) {
81 QMessageBox::critical(pMainWindow,
82 tr("Old version template"),
83 tr("Template file '%1' has version '%2', which is unsupported.")
84 .arg(fileName)
85 .arg(mVersion),
86 QMessageBox::Ok);
87 return;
88 }
89
90 QString lang = pSettings->environment().language();
91 // template info
92 mIcon = fromByteArray(mIni->GetValue("Template", "Icon", ""));
93 if (!lang.isEmpty()) {
94 mCategory = fromByteArray(mIni->GetValue("Template", QString("Category[%1]").arg(lang).toUtf8(), ""));
95 mName = fromByteArray(mIni->GetValue("Template", QString("Name[%1]").arg(lang).toUtf8(), ""));
96 mDescription = fromByteArray(mIni->GetValue("Template", QString("Description[%1]").arg(lang).toUtf8(), ""));
97 }
98 if (mCategory.isEmpty())
99 mCategory = fromByteArray(mIni->GetValue("Template", "Category", ""));
100 if (mName.isEmpty())
101 mName = fromByteArray(mIni->GetValue("Template", "Name", ""));
102 if (mDescription.isEmpty())
103 mDescription = fromByteArray(mIni->GetValue("Template", "Description", ""));
104
105 mOptions.icon = mIni->GetValue("Project", "Icon", "");
106 mOptions.type = static_cast<ProjectType>(mIni->GetLongValue("Project", "Type", 0)); // default = gui
107 mOptions.objFiles = fromByteArray(mIni->GetValue("Project", "ObjFiles", "")).split(";",
108#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
109 Qt::SkipEmptyParts
110#else
111 QString::SkipEmptyParts
112#endif
113 );
114 mOptions.includeDirs = fromByteArray(mIni->GetValue("Project", "Includes", "")).split(";",
115#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
116 Qt::SkipEmptyParts
117#else
118 QString::SkipEmptyParts
119#endif
120 );
121 mOptions.binDirs = fromByteArray(mIni->GetValue("Project", "Bins", "")).split(";",
122#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
123 Qt::SkipEmptyParts
124#else
125 QString::SkipEmptyParts
126#endif
127 );
128
129 mOptions.libDirs = fromByteArray(mIni->GetValue("Project", "Libs", "")).split(";",
130#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
131 Qt::SkipEmptyParts
132#else
133 QString::SkipEmptyParts
134#endif
135 );
136
137 mOptions.resourceIncludes = fromByteArray(mIni->GetValue("Project", "ResourceIncludes", "")).split(";",
138#if QT_VERSION >= QT_VERSION_CHECK(5,15,0)
139 Qt::SkipEmptyParts
140#else
141 QString::SkipEmptyParts
142#endif
143 );
144 mOptions.compilerCmd = fromByteArray(mIni->GetValue("Project", "Compiler", ""));
145 mOptions.cppCompilerCmd = fromByteArray(mIni->GetValue("Project", "CppCompiler", ""));
146 mOptions.linkerCmd = fromByteArray(mIni->GetValue("Project", "Linker",""));
147 mOptions.isCpp = mIni->GetBoolValue("Project", "IsCpp", false);
148 mOptions.includeVersionInfo = mIni->GetBoolValue("Project", "IncludeVersionInfo", false);
149 mOptions.supportXPThemes = mIni->GetBoolValue("Project", "SupportXPThemes", false);
150 mOptions.exeOutput = fromByteArray(mIni->GetValue("Project", "ExeOutput", ""));
151 mOptions.objectOutput = fromByteArray(mIni->GetValue("Project", "ObjectOutput", ""));
152 mOptions.logOutput = fromByteArray(mIni->GetValue("Project", "LogOutput", ""));
153 mOptions.execEncoding = mIni->GetValue("Project","ExecEncoding", ENCODING_SYSTEM_DEFAULT);
154
155 mOptions.staticLink = mIni->GetBoolValue("Project", "StaticLink",true);
156 mOptions.addCharset = mIni->GetBoolValue("Project", "AddCharset",true);
157 bool useUTF8 = mIni->GetBoolValue("Project", "UseUTF8", false);
158 if (useUTF8) {
159 mOptions.encoding = fromByteArray(mIni->GetValue("Project","Encoding", ENCODING_UTF8));
160 } else {
161 mOptions.encoding = fromByteArray(mIni->GetValue("Project","Encoding", ENCODING_AUTO_DETECT));
162 }
163 mOptions.modelType = (ProjectModelType)mIni->GetLongValue("Project", "ModelType", (int)ProjectModelType::FileSystem);
164
165}
166
167bool ProjectTemplate::save()
168{
169 if (mIni) {
170 return mIni->SaveFile(toByteArray(mFileName)) == SI_OK ;
171 }
172 return false;
173}
174
175const QString &ProjectTemplate::category() const
176{
177 return mCategory;
178}
179
180void ProjectTemplate::setCategory(const QString &newCategory)
181{
182 mCategory = newCategory;
183}
184
185const QString &ProjectTemplate::description() const
186{
187 return mDescription;
188}
189
190void ProjectTemplate::setDescription(const QString &newDescription)
191{
192 mDescription = newDescription;
193}
194
195const QString &ProjectTemplate::fileName() const
196{
197 return mFileName;
198}
199
200void ProjectTemplate::setFileName(const QString &newFileName)
201{
202 mFileName = newFileName;
203}
204
205const QString ProjectTemplate::folder() const
206{
207 return extractFileDir(mFileName);
208}
209
210const QString &ProjectTemplate::icon() const
211{
212 return mIcon;
213}
214
215void ProjectTemplate::setIcon(const QString &newIcon)
216{
217 mIcon = newIcon;
218}
219
220const QString &ProjectTemplate::name() const
221{
222 return mName;
223}
224
225void ProjectTemplate::setName(const QString &newName)
226{
227 mName = newName;
228}
229
230const ProjectOptions &ProjectTemplate::options() const
231{
232 return mOptions;
233}
234
235void ProjectTemplate::setOptions(const ProjectOptions &newOptions)
236{
237 mOptions = newOptions;
238}
239
240int ProjectTemplate::version() const
241{
242 return mVersion;
243}
244
245void ProjectTemplate::setVersion(int newVersion)
246{
247 mVersion = newVersion;
248}
249
250