-
Notifications
You must be signed in to change notification settings - Fork 0
/
artefact_manager.cpp
86 lines (78 loc) · 2.4 KB
/
artefact_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
#include "artefact_manager.h"
#include "markdown_artefact.h"
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QSharedPointer>
ArtefactManager::ArtefactManager(QObject *parent) : QObject(parent) {}
void ArtefactManager::loadArtefact(const QUrl &fileUrl) {
qInfo() << "loading file " << fileUrl;
if (isValidArtefactURL(fileUrl)) {
QString extension = getExtension(fileUrl);
if (extension == "md") {
QSharedPointer<MarkdownArtefact> artefact =
QSharedPointer<MarkdownArtefact>::create(fileUrl);
artefact_dict[fileUrl] = artefact;
} else {
qInfo() << "invalid file extension: " << extension;
// TODO: emit error
return;
}
} else {
qInfo() << "invalid URL: " << fileUrl;
// TODO: emit error
return;
}
}
bool ArtefactManager::isValidArtefactURL(const QUrl &fileUrl) {
if (isValidFile(fileUrl)) {
QString extension = getExtension(fileUrl);
return isValidArtefactType(extension);
} else {
return false;
}
}
QString ArtefactManager::getExtension(const QUrl &fileUrl) {
QFileInfo fileInfo = QFileInfo(fileUrl.path());
QString extension = fileInfo.suffix();
return extension;
}
bool ArtefactManager::isValidArtefactType(const QString extension) {
// TODO: more generic artefact type handling
return extension == "md";
}
void ArtefactManager::getArtefact(const QUrl &fileUrl) {
if (not artefact_dict.contains(fileUrl)) {
loadArtefact(fileUrl);
}
if (artefact_dict.contains(fileUrl)) {
emit artefactLoaded(fileUrl, artefact_dict[fileUrl]);
} else {
// TODO: emit error
}
}
QUrl ArtefactManager::createArtefact(const QString extension) {
qInfo() << "creating new artefact with extension: " << extension;
if (extension == "md") {
QSharedPointer<MarkdownArtefact> artefact =
QSharedPointer<MarkdownArtefact>::create();
const QUrl fileUrl = artefact->url();
artefact_dict[fileUrl] = artefact;
qInfo() << "markdown artefact with url: " << fileUrl << " created.";
return fileUrl;
} else {
qInfo() << "invalid file extension: " << extension;
// TODO: emit error
return QUrl();
}
}
bool ArtefactManager::isValidFile(const QUrl &fileUrl) {
if (fileUrl.isLocalFile() or fileUrl.isRelative()) {
QFileInfo fileInfo = QFileInfo(fileUrl.path());
QDir directory = QDir(fileInfo.canonicalPath());
if (directory.exists()) {
return true;
}
}
return false;
}