Sayonara Player
XiphFrame.h
1 /* XiphFrame.h */
2 
3 /* Copyright (C) 2011-2020 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_XIPH_FRAME_H
22 #define SAYONARA_ABSTRACT_XIPH_FRAME_H
23 
24 #include "Utils/Tagging/AbstractFrame.h"
25 
26 #include <QString>
27 
28 #include <taglib/tag.h>
29 #include <taglib/xiphcomment.h>
30 #include <taglib/tstring.h>
31 #include <taglib/tstringlist.h>
32 
33 #include <optional>
34 
35 namespace Xiph
36 {
37  template<typename Model_t>
38  class XiphFrame :
39  protected Tagging::AbstractFrame<TagLib::Ogg::XiphComment>
40  {
41  protected:
42  virtual std::optional<Model_t> mapTagToData() const = 0;
43  virtual void mapDataToTag(const Model_t& model) = 0;
44 
45  std::optional<TagLib::String> stringData() const
46  {
47  const auto& map = tag()->fieldListMap();
48  const auto it = map.find(tagKey());
49  return (it == map.end())
50  ? std::optional<TagLib::String>{}
51  : std::optional(it->second.front());
52  }
53 
54  void setStringData(const TagLib::String& value)
55  {
56  if(tag())
57  {
58  tag()->addField(tagKey(), value, true);
59  }
60  }
61 
62  void setStringData(const QString& value)
63  {
64  setStringData(Tagging::convertString(value));
65  }
66 
67  public:
68  XiphFrame(TagLib::Ogg::XiphComment* tag, const QString& identifier) :
70 
71  virtual ~XiphFrame() = default;
72 
73  bool read(Model_t& model) const
74  {
75  const auto data = (tag() != nullptr)
76  ? mapTagToData()
77  : std::nullopt;
78 
79  if(data.has_value())
80  {
81  model = data.value();
82  }
83 
84  return data.has_value();
85  }
86 
87  bool write(const Model_t& model)
88  {
89  if(!tag())
90  {
91  return false;
92  }
93 
94  if(!tagKey().isEmpty())
95  {
96  tag()->removeFields(tagKey());
97  }
98 
99  mapDataToTag(model);
100 
101  return true;
102  }
103 
104  virtual bool isFrameAvailable() const
105  {
106  return (!tagKey().isEmpty() && tag()->contains("METADATA_BLOCK_PICTURE"));
107  }
108  };
109 }
110 
111 #endif // SAYONARA_ABSTRACT_XIPH_FRAME_H
Tagging::AbstractFrame
Definition: AbstractFrame.h:54
Xiph::XiphFrame
Definition: XiphFrame.h:40