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