-
Notifications
You must be signed in to change notification settings - Fork 2
/
ytdlfilehandler.cpp
114 lines (88 loc) · 3.28 KB
/
ytdlfilehandler.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
#include "ytdlfilehandler.h"
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QSaveFile>
YtdlFileHandler::YtdlFileHandler()
{
}
YtdlFileHandler::~YtdlFileHandler()
{
}
FileHandlerInterface::Status YtdlFileHandler::setFile(const QString filePath)
{
m_file = filePath;
return SUCCESS;
}
ChapterItem *YtdlFileHandler::createChapterTree() const
{
QFile jsonFile(m_file);
jsonFile.open(QIODevice::ReadOnly | QIODevice::Text);
ChapterItem * tocItem = new ChapterItem("pseudoTOC");
tocItem->setColumnCount(3);
QJsonDocument && json = QJsonDocument::fromJson(jsonFile.readAll());
QJsonObject && jsonObj = json.object();
// if (jsonObj.contains("duration")) {
// m_duration = jsonObj.value("duration").toInt() * 1000;
// }
if (jsonObj.contains("chapters")) {
QJsonArray && chapterArr = jsonObj.value("chapters").toArray();
for (const QJsonValueRef & chapter : chapterArr) {
QJsonObject && chapterObj = chapter.toObject();
ChapterItem * chapterItem = new ChapterItem();
chapterItem->setColumnCount(3);
if (chapterObj.contains("title")) {
chapterItem->setItemProperty(ChapterTitle, chapterObj.value("title").toString());
}
if (chapterObj.contains("start_time")) {
chapterItem->setItemProperty(ChapterStartTimeMs, chapterObj.value("start_time").toInt() * 1000);
}
if (chapterObj.contains("end_time")) {
chapterItem->setItemProperty(ChapterEndTimeMs, chapterObj.value("end_time").toInt() * 1000);
}
tocItem->appendRow(chapterItem);
}
}
jsonFile.close();
return tocItem;
}
FileHandlerInterface::ChapterFeatures YtdlFileHandler::chapterFeatures() const
{
return ChapterFeatures(StartTimeMs | EndTimeMs | Title);
}
FileHandlerInterface::Status YtdlFileHandler::importFromFile()
{
return SUCCESS;
}
FileHandlerInterface::Status YtdlFileHandler::writeToFile(ChapterItem *chapterRoot)
{
QSaveFile sf(m_file);
sf.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
sf.fileName();
QJsonObject parentObj;
QJsonArray chapterArr;
ChapterItem::forEach(chapterRoot, [&](const ChapterItem * currentItem) {
if (currentItem->hasChildren()) return;
QJsonObject chapterObj;
chapterObj.insert("title", QJsonValue::fromVariant(currentItem->data(ChapterTitle)));
chapterObj.insert("start_time", QJsonValue(currentItem->data(ChapterStartTimeMs).toInt() / 1000));
QVariant && endTimeMs = currentItem->data(ChapterEndTimeMs);
if (endTimeMs.isValid()) {
chapterObj.insert("end_time", QJsonValue(endTimeMs.toInt() / 1000));
}
chapterArr.append(QJsonValue(chapterObj));
});
parentObj.insert("chapters", QJsonValue(chapterArr));
QJsonDocument json(parentObj);
QTextStream out(&sf);
out.setCodec("UTF-8");
out << json.toJson();
out.flush();
sf.commit();
return SUCCESS;
}
FileHandlerInterface::Status YtdlFileHandler::exportToFile(ChapterItem *chapterRoot)
{
return writeToFile(chapterRoot);
}