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 
22 
23 #ifndef ABSTRACT_XIPH_FRAME_H_
24 #define ABSTRACT_XIPH_FRAME_H_
25 
26 #include "Utils/Tagging/AbstractFrame.h"
27 #include <QString>
28 
29 #include "taglib/tag.h"
30 #include "taglib/xiphcomment.h"
31 #include "taglib/tstring.h"
32 #include "taglib/tstringlist.h"
33 
34 namespace Xiph
35 {
36  template<typename Model_t>
37  class XiphFrame :
38  protected Tagging::AbstractFrame<TagLib::Ogg::XiphComment>
39  {
40  protected:
41  virtual bool map_tag_to_model(Model_t& model)=0;
42  virtual bool map_model_to_tag(const Model_t& model)=0;
43 
44  // those methods are usually called by the implementations
45  // in order to retrieve and write back the data easily
46  bool value(TagLib::String& str) const
47  {
48  TagLib::Ogg::XiphComment* tag = this->tag();
49  const TagLib::Ogg::FieldListMap& map = tag->fieldListMap();
50  TagLib::Ogg::FieldListMap::ConstIterator it = map.find( this->tag_key() );
51  if(it == map.end()){
52  str = TagLib::String();
53  return false;
54  }
55 
56  str = it->second.front();
57  return true;
58  }
59 
60  void set_value(const TagLib::String& value)
61  {
62  TagLib::Ogg::XiphComment* tag = this->tag();
63  tag->addField(this->tag_key(), value, true);
64  }
65 
66  void set_value(const QString& value)
67  {
68  TagLib::String str = this->convert_string(value);
69  set_value(str);
70  }
71 
72  public:
73  XiphFrame(TagLib::Ogg::XiphComment* tag, const QString& identifier) :
75 
76  virtual ~XiphFrame() = default;
77 
78  bool read(Model_t& model)
79  {
80  if(!this->tag()){
81  return false;
82  }
83 
84  bool success = map_tag_to_model(model);
85 
86  return success;
87  }
88 
89  bool write(const Model_t& model)
90  {
91  TagLib::Ogg::XiphComment* tag = this->tag();
92  if(!tag) {
93  return false;
94  }
95 
96  TagLib::String key = this->tag_key();
97  if(!key.isEmpty())
98  {
99  tag->removeField( this->tag_key() );
100  }
101 
102  return map_model_to_tag(model);
103  }
104 
105  virtual bool is_frame_found() const
106  {
107  if(this->tag_key().isEmpty())
108  {
109  return false;
110  }
111 
112  return this->tag()->contains("METADTA_BLOCK_PICTURE");
113  }
114  };
115 }
116 
117 #endif // ABSTRACTFRAME_H
Tagging::AbstractFrame
Definition: AbstractFrame.h:57
Xiph::XiphFrame
Definition: XiphFrame.h:39