-
Notifications
You must be signed in to change notification settings - Fork 71
/
file_manager.cpp
343 lines (285 loc) · 9.61 KB
/
file_manager.cpp
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "file_manager/file_manager.h"
#include "core/log.h"
#include "netlist/gate_library/gate_library_manager.h"
#include "netlist/netlist.h"
#include "netlist/netlist_factory.h"
#include "netlist/persistent/netlist_serializer.h"
#include "gui_globals.h"
#include <QFile>
#include <QFileInfo>
#include <QFileSystemWatcher>
#include <QInputDialog>
#include <QMessageBox>
#include <QTextStream>
file_manager::file_manager(QObject* parent) : QObject(parent), m_file_watcher(new QFileSystemWatcher(this)), m_file_open(false)
{
connect(m_file_watcher, &QFileSystemWatcher::fileChanged, this, &file_manager::handle_file_changed);
connect(m_file_watcher, &QFileSystemWatcher::directoryChanged, this, &file_manager::handle_directory_changed);
m_timer = new QTimer(this);
connect(m_timer, &QTimer::timeout, this, &file_manager::autosave);
}
file_manager* file_manager::get_instance()
{
static file_manager manager;
return &manager;
}
void file_manager::handle_program_arguments(const program_arguments& args)
{
if (args.is_option_set("--input-file"))
{
auto file_name = hal::path(args.get_parameter("--input-file"));
log_info("gui", "GUI started with file {}.", file_name.string());
open_file(QString::fromStdString(file_name.string()));
}
}
bool file_manager::is_document_open()
{
return m_file_open;
}
void file_manager::autosave()
{
log_info("gui", "saving a backup in case something goes wrong...");
netlist_serializer::serialize_to_file(g_netlist, m_shadow_file_name.toStdString());
}
QString file_manager::file_name() const
{
if (m_file_open)
return m_file_name;
return QString();
}
void file_manager::file_successfully_loaded(QString file_name)
{
// autosave every 60s
m_timer->start(60 * 1000);
m_file_name = file_name;
m_shadow_file_name = get_shadow_file(file_name);
m_file_watcher->addPath(m_file_name);
m_file_open = true;
update_recent_files(m_file_name);
g_python_context = std::make_unique<python_context>(); // TODO HANDLE PYTHON CONTEXT SEPARATELY
Q_EMIT file_opened(m_file_name);
}
QString file_manager::get_shadow_file(QString file)
{
QString shadow_file_name;
if (file.contains('/'))
{
shadow_file_name = file.left(file.lastIndexOf('/') + 1) + "~" + file.right(file.size() - file.lastIndexOf('/') - 1);
}
else
{
shadow_file_name = "~" + file;
}
return shadow_file_name.left(shadow_file_name.lastIndexOf('.')) + ".hal";
}
void file_manager::open_file(QString file_name)
{
QString logical_file_name = file_name;
if (g_netlist)
{
// TODO HANDLE THIS CASE
return;
}
if (file_name.isEmpty())
{
std::string error_message("Unable to open file. File name is empty");
log_error("gui", "{}", error_message);
display_error_message(QString::fromStdString(error_message));
return;
}
if (!file_name.endsWith(".hal"))
{
QString hal_file_name = file_name.left(file_name.lastIndexOf('.')) + ".hal";
if (QFileInfo::exists(hal_file_name) && QFileInfo(hal_file_name).isFile())
{
if (QMessageBox::question(nullptr, "Resume previous work", "A .hal file exists for the selected netlist! Load that file instead?", QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
{
file_name = hal_file_name;
logical_file_name = hal_file_name;
}
}
}
log_manager& lm = log_manager::get_instance();
hal::path log_path = file_name.toStdString();
lm.set_file_name(hal::path(log_path.replace_extension(".log")));
if (file_name.endsWith(".hal"))
{
QString shadow_file_name = get_shadow_file(file_name);
if (QFileInfo::exists(shadow_file_name) && QFileInfo(shadow_file_name).isFile())
{
QString message =
"It seems that HAL crashed during your last session. Last autosave was on " + QFileInfo(shadow_file_name).lastModified().toString("dd.MM.yyyy hh:mm") + ". Restore that state?";
if (QMessageBox::question(nullptr, "HAL did not exit cleanly", message, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes)
{
// logical_file_name is not changed
file_name = shadow_file_name;
}
}
}
QFile file(file_name);
if (!file.open(QFile::ReadOnly))
{
std::string error_message("Unable to open file" + file_name.toStdString());
log_error("gui", "Unable to open file '{}'", error_message);
display_error_message(QString::fromStdString(error_message));
return;
}
if (file_name.endsWith(".hal"))
{
std::shared_ptr<netlist> netlist = netlist_factory::load_netlist(file_name.toStdString());
if (netlist)
{
g_netlist = netlist;
file_successfully_loaded(logical_file_name);
}
else
{
std::string error_message("Failed to create netlist from .hal file");
log_error("gui", "{}", error_message);
display_error_message(QString::fromStdString(error_message));
}
return;
}
QString language = "";
if (file_name.endsWith(".v"))
language = "verilog";
else if (file_name.endsWith(".vhd") || file_name.endsWith(".vhdl"))
language = "vhdl";
if (language == "")
{
std::string error_message("Unable to determine file language. Files have to end with .v, .vhd or .vhdl");
log_error("gui", "{}", error_message);
display_error_message(QString::fromStdString(error_message));
return;
}
QList<QPair<std::string, std::shared_ptr<netlist>>> list;
for (const auto& key : gate_library_manager::get_gate_libraries())
{
std::string lib = key.first;
log_info("gui", "Trying to use gate library '{}'...", lib);
std::shared_ptr<netlist> netlist = netlist_factory::load_netlist(file_name.toStdString(), language.toStdString(), lib);
if (netlist)
{
list.append(QPair(lib, netlist));
}
else
{
log_info("gui", "Failed");
}
}
if (list.isEmpty())
{
std::string error_message("Unable to find a compatible gate library. Deserialization failed!");
log_error("gui", "{}", error_message);
display_error_message(QString::fromStdString(error_message));
return;
}
if (list.length() == 1)
{
log_info("gui", "One compatible gate library found.");
g_netlist = list.at(0).second;
}
else
{
log_info("gui", "{} compatible gate libraries found. User has to select one.", list.length());
QInputDialog dialog;
QStringList libs;
for (auto& element : list)
libs.append(QString::fromStdString(element.first));
dialog.setComboBoxItems(libs);
dialog.setWindowTitle("Select gate library");
dialog.setLabelText("The specified file can be processed with more than one gate library. Please select which library should be used:");
if (dialog.exec())
{
std::string selection = dialog.textValue().toStdString();
for (auto& element : list)
{
if (element.first == selection)
g_netlist = element.second;
}
}
else
return;
}
file_successfully_loaded(logical_file_name);
}
void file_manager::close_file()
{
if (!m_file_open)
return;
m_timer->stop();
// TODO CHECK DIRTY AND TRIGGER SAVE ROUTINE
m_file_watcher->removePath(m_file_name);
m_file_name = "";
m_file_open = false;
if (QFileInfo::exists(m_shadow_file_name) && QFileInfo(m_shadow_file_name).isFile())
{
QFile(m_shadow_file_name).remove();
}
Q_EMIT file_closed();
}
void file_manager::handle_file_changed(const QString& path)
{
Q_EMIT file_changed(path);
}
void file_manager::handle_directory_changed(const QString& path)
{
Q_EMIT file_directory_changed(path);
}
void file_manager::update_recent_files(const QString& file)
{
QStringList list;
g_gui_state.beginReadArray("recent_files");
for (int i = 0; i < 14; ++i)
{
g_gui_state.setArrayIndex(i);
QString state_file = g_gui_state.value("file").toString();
if (state_file.isEmpty())
continue;
list.append(g_gui_state.value("file").toString());
}
g_gui_state.endArray();
int index = list.indexOf(file);
if (index == -1)
list.prepend(file);
else
list.move(index, 0);
g_gui_state.beginGroup("recent_files");
g_gui_state.remove("");
g_gui_state.endGroup();
g_gui_state.beginWriteArray("recent_files");
int i = 0;
std::vector<hal::path> files;
for (QString& string : list)
{
hal::path file_path(string.toStdString());
bool skip = false;
for (const auto& other : files)
{
if (hal::fs::equivalent(file_path, other))
{
skip = true;
break;
}
}
if (!skip)
{
g_gui_state.setArrayIndex(i);
g_gui_state.setValue("file", string);
++i;
if (i == 14)
break;
}
}
g_gui_state.endArray();
}
void file_manager::display_error_message(QString error_message)
{
QMessageBox msgBox;
msgBox.setText("Error");
msgBox.setInformativeText(error_message);
msgBox.setStyleSheet("QLabel{min-width: 600px;}");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}