-
Notifications
You must be signed in to change notification settings - Fork 2
/
opusfilehandler.cpp
94 lines (69 loc) · 2.53 KB
/
opusfilehandler.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
#include "opusfilehandler.h"
#include "taglibutils_p.h"
#include <QSaveFile>
#include <opusfile.h>
OpusFileHandler::OpusFileHandler()
{
}
OpusFileHandler::~OpusFileHandler()
{
}
FileHandlerInterface::Status OpusFileHandler::setFile(const QString filePath)
{
m_file = filePath;
return SUCCESS;
}
// seems same as the vorbis one...
ChapterItem *OpusFileHandler::createChapterTree() const
{
if (m_file.isEmpty()) return nullptr;
TagLib::Ogg::Opus::File file(m_file.toLocal8Bit().data());
TagLib::Ogg::XiphComment * tags = file.tag();
return TagLibUtils::loadFromXiphComment(tags);
}
FileHandlerInterface::ChapterFeatures OpusFileHandler::chapterFeatures() const
{
return ChapterFeatures(StartTimeMs | Title);
}
FileHandlerInterface::Status OpusFileHandler::importFromFile()
{
return SUCCESS;
}
FileHandlerInterface::Status OpusFileHandler::writeToFile(ChapterItem *chapterRoot)
{
TagLib::Ogg::Opus::File file(m_file.toLocal8Bit().data());
TagLib::Ogg::XiphComment * tags = file.tag();
Q_CHECK_PTR(tags);
TagLibUtils::saveToXiphComment(chapterRoot, tags);
return file.save() ? SUCCESS : FILE_STAT_ERROR;
}
FileHandlerInterface::Status OpusFileHandler::exportToFile(ChapterItem *chapterRoot)
{
QSaveFile sf(m_file);
sf.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
QStandardItem * item = chapterRoot;
ChapterItem * chapterItem = static_cast<ChapterItem *>(item);
if (chapterItem) {
int currentChapterNum = 1; // start from 1.
ChapterItem::forEach(chapterItem, [&](const ChapterItem * currentItem) {
if (currentItem->hasChildren()) return;
// write time and title
std::string timeStrLine = TagLibUtils::ogmChapterKey(currentChapterNum, TagLibUtils::OgmChapterTime);
timeStrLine += "=";
timeStrLine += TagLibUtils::ogmTimeStr(currentItem->data(ChapterStartTimeMs).toInt());
QByteArray ba;
ba.append(timeStrLine.data(), timeStrLine.length());
ba.append('\n');
std::string titleStrLine = TagLibUtils::ogmChapterKey(currentChapterNum, TagLibUtils::OgmChapterName);
titleStrLine += "=";
QByteArray ba2;
ba2.append(titleStrLine.data(), titleStrLine.length());
ba2.append(currentItem->data(ChapterTitle).toString().toUtf8());
ba2.append('\n');
sf.write(ba);
sf.write(ba2);
currentChapterNum++;
});
}
return sf.commit() ? SUCCESS : FILE_STAT_ERROR;
}