Sayonara Player
Loading...
Searching...
No Matches
MP4Frame.h
1/* MP4Frame.h */
2
3/* Copyright (C) 2011-2024 Michael Lugmair (Lucio Carreras)
4 *
5 * This file is part of sayonara player
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef SAYONARA_ABSTRACT_MP4_FRAME_H
22#define SAYONARA_ABSTRACT_MP4_FRAME_H
23
24#include "Utils/Tagging/AbstractFrame.h"
25#include "Utils/Algorithm.h"
26
27#include <QString>
28
29#include <taglib/tag.h>
30#include <taglib/tstringlist.h>
31#include <taglib/mp4tag.h>
32#include <taglib/tpropertymap.h>
33
34#include <algorithm>
35#include <optional>
36
37namespace MP4
38{
39 bool isFrameAvailable(TagLib::MP4::Tag* tag, const QByteArray& key, const QByteArray& propertyMapKey);
40 TagLib::MP4::ItemMap::ConstIterator findKey(const QByteArray& key, const TagLib::MP4::ItemMap& itemMap);
41 void eraseAllFromTag(TagLib::MP4::Tag* tag, const QString& key);
42
43 template<typename Model_t>
44 class MP4Frame :
45 protected Tagging::AbstractFrame<TagLib::MP4::Tag>
46 {
47 protected:
48 virtual std::optional<Model_t> mapItemToData(const TagLib::MP4::Item& item) const = 0;
49 virtual std::optional<TagLib::MP4::Item> mapDataToItem(const Model_t& model) = 0;
50
51 public:
52 MP4Frame(TagLib::MP4::Tag* tag, const QByteArray& identifier) :
54
55 ~MP4Frame() override = default;
56
57 bool read(Model_t& data) const
58 {
59 if(tag())
60 {
61 if(const auto maybeData = findDataInItemMap(); maybeData.has_value())
62 {
63 data = maybeData.value();
64 return true;
65 }
66
67 if(const auto maybeData = findDataInPropertyMap(); maybeData.has_value())
68 {
69 data = maybeData.value();
70 return true;
71 }
72 }
73
74 return false;
75 }
76
77 bool write(const Model_t& data)
78 {
79 if(!tag())
80 {
81 return false;
82 }
83
84 eraseAllFromTag(tag(), key());
85
86 const auto item = mapDataToItem(data);
87 if(item.has_value() && item.value().isValid())
88 {
89 tag()->setItem(tagKey(), *item);
90 }
91
92 return item.has_value();
93 }
94
95 [[nodiscard]] bool isFrameAvailable() const
96 {
97 return MP4::isFrameAvailable(tag(), key(), propertyMapKey());
98 }
99
100 private:
101 std::optional<Model_t> findDataInItemMap() const
102 {
103 const auto& itemMap = tag()->itemMap();
104
105 if(const auto it = findKey(key(), itemMap); (it != itemMap.end() && it->second.isValid()))
106 {
107 const auto optionalData = mapItemToData(it->second);
108 if(optionalData.has_value())
109 {
110 return optionalData;
111 }
112 }
113
114 return std::nullopt;
115 }
116
117 virtual std::optional<Model_t> findDataInPropertyMap() const { return std::nullopt; };
118
119 [[nodiscard]] virtual QByteArray propertyMapKey() const { return {}; };
120 };
121}
122
123#endif // SAYONARA_ABSTRACT_MP4_FRAME_H
Definition: MP4Frame.h:46
Definition: AbstractFrame.h:54