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