-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_registry.cpp
More file actions
210 lines (171 loc) · 5.49 KB
/
Copy pathlanguage_registry.cpp
File metadata and controls
210 lines (171 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "language_registry.h"
#include <QStandardPaths>
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QDirIterator>
#include <QProcess>
#include <QDebug>
#include <QCoreApplication>
LanguageRegistry::LanguageRegistry(QObject *parent) : QObject(parent) {
m_userConfigPath = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation)
+ "/languages";
}
void LanguageRegistry::initialize() {
qDebug() << "Initializing Language Registry...";
loadBuiltinDefaults();
// System paths
QStringList systemPaths = {
"/usr/share/codehour/languages",
"/usr/local/share/codehour/languages",
QCoreApplication::applicationDirPath() + "/languages"
};
for (const QString &path : systemPaths) {
loadFromDirectory(path);
}
// User configs
QDir().mkpath(m_userConfigPath);
loadFromDirectory(m_userConfigPath);
qDebug() << "Available languages:" << availableLanguages();
}
void LanguageRegistry::loadBuiltinDefaults() {
auto addBuiltin = [this](const QJsonObject &json, const QString &id) {
LanguageConfig config = LanguageConfig::fromJson(json, id);
config.isBuiltin = true;
m_languages[id] = config;
};
addBuiltin(builtinPython(), "python");
addBuiltin(builtinC(), "c");
addBuiltin(builtinCpp(), "cpp");
addBuiltin(builtinJavascript(), "javascript");
}
QJsonObject LanguageRegistry::builtinPython() {
return QJsonObject{
{"name", "Python"},
{"extension", ".py"},
{"sourceFile", "solution.py"},
{"compiled", false},
{"isEmbedded", true},
{"timeout", 5000},
{"template", "# Your code here\n"}
};
}
QJsonObject LanguageRegistry::builtinC() {
return QJsonObject{
{"name", "C"},
{"extension", ".c"},
{"sourceFile", "solution.c"},
{"compiled", true},
{"isEmbedded", true},
{"timeout", 5000},
{"template", "#include <stdio.h>\n\n// Your code here\n"}
};
}
QJsonObject LanguageRegistry::builtinCpp() {
return QJsonObject{
{"name", "C++"},
{"extension", ".cpp"},
{"sourceFile", "solution.cpp"},
{"compiled", true},
{"isEmbedded", true},
{"timeout", 5000},
{"template", "#include <iostream>\n\n// Your code here\n"}
};
}
QJsonObject LanguageRegistry::builtinJavascript() {
return QJsonObject{
{"name", "Javascript"},
{"extension", ".js"},
{"sourceFile", "solution.js"},
{"compiled", false},
{"isEmbedded", true},
{"timeout", 5000},
{"template", "// Your code here\n"}
};
}
void LanguageRegistry::loadFromDirectory(const QString &path) {
QDir dir(path);
if (!dir.exists()) return;
QDirIterator it(path, {"*.json"}, QDir::Files);
while (it.hasNext()) {
loadFromFile(it.next());
}
}
void LanguageRegistry::loadFromFile(const QString &filePath) {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) return;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error);
file.close();
if (error.error != QJsonParseError::NoError) {
qDebug() << "JSON error in" << filePath << ":" << error.errorString();
return;
}
QString id = QFileInfo(filePath).baseName();
LanguageConfig config = LanguageConfig::fromJson(doc.object(), id);
config.configPath = filePath;
if (config.isValid()) {
m_languages[config.id] = config;
qDebug() << "Loaded language:" << config.name;
}
}
LanguageConfig LanguageRegistry::getConfig(const QString &id) const {
return m_languages.value(id, LanguageConfig{});
}
bool LanguageRegistry::hasLanguage(const QString &id) const {
return m_languages.contains(id);
}
QStringList LanguageRegistry::allLanguages() const {
return m_languages.keys();
}
QStringList LanguageRegistry::availableLanguages() const {
QStringList available;
for (auto it = m_languages.begin(); it != m_languages.end(); ++it) {
if (isLanguageAvailable(it.key())) {
available << it.key();
}
}
return available;
}
bool LanguageRegistry::isLanguageAvailable(const QString &id) const {
if (!m_languages.contains(id)) return false;
// We only support embedded compilers now.
return m_languages[id].isEmbedded;
}
bool LanguageRegistry::addLanguage(const LanguageConfig &config, bool save) {
if (!config.isValid()) return false;
m_languages[config.id] = config;
if (save) {
saveConfig(config);
}
emit languagesChanged();
return true;
}
bool LanguageRegistry::saveConfig(const LanguageConfig &config) {
QString filePath = m_userConfigPath + "/" + config.id + ".json";
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) return false;
QJsonDocument doc(config.toJson());
file.write(doc.toJson(QJsonDocument::Indented));
file.close();
return true;
}
bool LanguageRegistry::removeLanguage(const QString &id) {
if (!m_languages.contains(id)) return false;
const LanguageConfig &config = m_languages[id];
if (config.isBuiltin && config.configPath.isEmpty()) return false;
if (!config.configPath.isEmpty()) {
QFile::remove(config.configPath);
}
m_languages.remove(id);
emit languagesChanged();
return true;
}
QString LanguageRegistry::userConfigPath() const {
return m_userConfigPath;
}
void LanguageRegistry::reload() {
m_languages.clear();
initialize();
emit languagesChanged();
}