-
Notifications
You must be signed in to change notification settings - Fork 2
/
filehandlermanager.cpp
145 lines (110 loc) · 4.5 KB
/
filehandlermanager.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
#include "filehandlermanager.h"
#include "mpegfilehandler.h"
#include "vorbisfilehandler.h"
#include "opusfilehandler.h"
#include "mp4filehandler.h"
#include "cuefilehandler.h"
#include "ytdlfilehandler.h"
#include <QMultiMap>
#include <QMultiHash>
class FileHandlerManagerPrivate
{
public:
explicit FileHandlerManagerPrivate(FileHandlerManager *qq);
~FileHandlerManagerPrivate();
QMap<QString, std::function<FileHandlerInterface*()>> m_handlerIdMap; // <HandlerName, creator>
QMap<QString, QString> m_readerMap; // <MimeTypeStr, HandlerName>
QMap<QString, QString> m_writerMap; // <MimeTypeStr, HandlerName>
QMap<QString, QString> m_textImporterMap; // <TxtTypeIdentifier, HandlerName>
QMap<QString, QPair<QString, QString> > m_exporterMap; // <SuffixStr, <HandlerName, Copywriting> >
FileHandlerManager *q_ptr;
};
FileHandlerManagerPrivate::FileHandlerManagerPrivate(FileHandlerManager *qq)
: q_ptr(qq)
{
}
FileHandlerManagerPrivate::~FileHandlerManagerPrivate()
{
}
// ------------------------- Item Get Border Line -------------------------
FileHandlerManager::FileHandlerManager(QObject *parent)
: QObject(parent)
, d_ptr(new FileHandlerManagerPrivate(this))
{
registerFileHandler<MpegFileHandler>();
registerFileHandler<VorbisFileHandler>();
registerFileHandler<OpusFileHandler>();
registerFileHandler<Mp4FileHandler>();
registerFileHandler<CueFileHandler>();
registerFileHandler<YtdlFileHandler>();
registerMimeTypeHandler<MpegFileHandler>("audio/mpeg", true, true);
registerMimeTypeHandler<VorbisFileHandler>("audio/x-vorbis+ogg", true, true);
registerMimeTypeHandler<OpusFileHandler>("audio/x-opus+ogg", true, true);
registerMimeTypeHandler<Mp4FileHandler>("audio/mp4", true, true);
registerMimeTypeHandler<CueFileHandler>("application/x-cue", true, true);
registerMimeTypeHandler<YtdlFileHandler>("application/json", true, false);
registerExporter<OpusFileHandler>("*.ogm.txt", tr("OGM Style TXT Files"));
registerExporter<CueFileHandler>("*.cue", tr("CUE Files"));
registerExporter<YtdlFileHandler>("*.info.json", tr("youtube-dl Style Json Files"));
}
FileHandlerManager::~FileHandlerManager()
{
}
void FileHandlerManager::insertFileHandler(const QString & handlerId, std::function<FileHandlerInterface *()> fn)
{
Q_D(FileHandlerManager);
d->m_handlerIdMap[handlerId] = fn;
}
FileHandlerManager *FileHandlerManager::instance()
{
static FileHandlerManager manager;
return &manager;
}
void FileHandlerManager::insertMimeTypeHandler(const QString &mimeTypeStr, const QString &handlerId, bool asReader, bool asWriter)
{
Q_D(FileHandlerManager);
if (asReader) d->m_readerMap[mimeTypeStr] = handlerId;
if (asWriter) d->m_writerMap[mimeTypeStr] = handlerId;
}
void FileHandlerManager::insertExporter(const QString &suffix, const QString & userFilterCopywriting,
const QString &handlerId)
{
Q_D(FileHandlerManager);
d->m_exporterMap[suffix] = qMakePair(handlerId, userFilterCopywriting);
}
FileHandlerInterface *FileHandlerManager::createReadHandlerByMimeType(const QMimeType &mimeType) const
{
Q_D(const FileHandlerManager);
for (const QString & mimeTypeStr : d->m_readerMap.keys()) {
if (mimeType.inherits(mimeTypeStr)) {
return d->m_handlerIdMap[d->m_readerMap[mimeTypeStr]]();
}
}
return nullptr;
}
FileHandlerInterface *FileHandlerManager::createWriteHandlerByMimeType(const QMimeType &mimeType) const
{
Q_D(const FileHandlerManager);
for (const QString & mimeTypeStr : d->m_writerMap.keys()) {
if (mimeType.inherits(mimeTypeStr)) {
return d->m_handlerIdMap[d->m_writerMap[mimeTypeStr]]();
}
}
return nullptr;
}
FileHandlerInterface *FileHandlerManager::createExportHandlerBySuffix(const QString &suffix) const
{
Q_D(const FileHandlerManager);
return d->m_handlerIdMap[d->m_exporterMap[suffix].first]();
}
QList<QPair<QString, QString> > FileHandlerManager::exporterList() const
{
Q_D(const FileHandlerManager);
QList<QPair<QString, QString> > result;
for (const QString & suffix : d->m_exporterMap.keys()) {
result.append(qMakePair(suffix,
QString("%1 (%2)").arg(d->m_exporterMap[suffix].second,
suffix)));
}
return result;
}