Sayonara Player
MP4Frame.h
1 /* MP4Frame.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_MP4_FRAME_H_
24 #define ABSTRACT_MP4_FRAME_H_
25 
26 #include "Helper/Tagging/AbstractFrame.h"
27 #include <QString>
28 #include <taglib/tag.h>
29 #include <taglib/tstring.h>
30 #include <taglib/tstringlist.h>
31 #include <taglib/mp4tag.h>
32 
33 namespace MP4
34 {
35  template<typename Model_t>
36  class MP4Frame :
37  protected Tagging::AbstractFrame<TagLib::MP4::Tag>
38  {
39  protected:
40  TagLib::MP4::ItemListMap::ConstIterator find_key(const TagLib::MP4::ItemListMap& ilm) const
41  {
42  for(TagLib::MP4::ItemListMap::ConstIterator it=ilm.begin(); it!=ilm.end(); it++){
43  QString key = this->key();
44  if( this->cvt_string(it->first).compare(key, Qt::CaseInsensitive) == 0){
45  return it;
46  }
47  }
48 
49  return ilm.end();
50  }
51 
52  virtual bool map_tag_to_model(Model_t& model)=0;
53  virtual bool map_model_to_tag(const Model_t& model)=0;
54 
55 
56  public:
57  MP4Frame(TagLib::Tag* tag, const QString& identifier) :
59 
60  virtual ~MP4Frame() {}
61 
62  bool read(Model_t& model)
63  {
64  TagLib::MP4::Tag* key = this->tag();
65  if(!key) {
66  return false;
67  }
68 
69  const TagLib::MP4::ItemListMap& ilm = key->itemListMap();
70 
71  bool found = (find_key(ilm) != ilm.end());
72  if(!found){
73  return false;
74  }
75 
76  bool success = map_tag_to_model(model);
77 
78  return success;
79  }
80 
81  bool write(const Model_t& model)
82  {
83  TagLib::MP4::Tag* tag = this->tag();
84  if(!tag) {
85  return false;
86  }
87 
88  TagLib::MP4::ItemListMap& ilm = tag->itemListMap();
89 
90  auto itcopy=ilm.begin();
91  for(auto it=ilm.begin(); it!=ilm.end(); it++)
92  {
93  QString key = this->key();
94  if( this->cvt_string(it->first).compare(key, Qt::CaseInsensitive) == 0)
95  {
96  ilm.erase(it);
97  it = itcopy;
98  }
99 
100  else{
101  itcopy = it;
102  }
103  }
104 
105  return map_model_to_tag(model);
106  }
107  };
108 }
109 
110 #endif // ABSTRACT_MP4_FRAME_H_
Definition: AlbumArtist.h:29
Definition: MP4Frame.h:36
Definition: AbstractFrame.h:57