Sayonara Player
AbstractFrame.h
1 /* AbstractFrame.h */
2 
3 /* Copyright (C) 2011-2016 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 ABSTRACTFRAME_H
24 #define ABSTRACTFRAME_H
25 
26 #include <taglib/fileref.h>
27 #include <taglib/mpegfile.h>
28 #include <taglib/id3v2tag.h>
29 #include <taglib/id3v2frame.h>
30 
31 #include <QString>
32 #include "Helper/Logger/Logger.h"
33 
38 namespace ID3v2Frame
39 {
40  template<typename ModelType_t, typename FrameType_t>
47  class AbstractFrame {
48 
49  protected:
50  const char* _four=nullptr;
51  TagLib::ID3v2::Tag* _tag=nullptr;
52  ModelType_t _data_model;
53  FrameType_t* _frame=nullptr;
54 
55 
56  protected:
57 
64  virtual TagLib::ID3v2::Frame* create_id3v2_frame()=0;
65 
71  virtual void map_model_to_frame()=0;
72  virtual void map_frame_to_model()=0;
73 
74 
75  public:
76  // constructor
77  AbstractFrame(TagLib::FileRef* file_ref, const char* four) :
78  _four(four),
79  _tag(nullptr),
80  _frame(nullptr)
81  {
82  TagLib::MPEG::File* file_mp3;
83  TagLib::ID3v2::FrameListMap map;
84  TagLib::ID3v2::FrameList frame_list;
85 
86  file_mp3 = dynamic_cast<TagLib::MPEG::File*>(file_ref->file());
87  if(!file_mp3) {
88  return;
89  }
90 
91  _tag = file_mp3->ID3v2Tag();
92  if(!_tag){
93  return;
94  }
95 
96  // map, containing [four][frame list]
97  map = _tag->frameListMap();
98  frame_list = map[_four];
99  if(!frame_list.isEmpty()) {
100  _frame = dynamic_cast<FrameType_t*> (frame_list.front());
101  }
102  }
103 
104  // destructor
105  virtual ~AbstractFrame(){
106 
107  }
108 
109 
110  //
116  virtual bool read(ModelType_t& data){
117  if(!_frame){
118  return false;
119  }
120 
121  map_frame_to_model();
122  data = _data_model;
123 
124  return true;
125  }
126 
127 
128  //
134  virtual bool write(const ModelType_t& data_model){
135 
136  bool created = false;
137 
138  if(!_tag){
139  return false;
140  }
141 
142  if(!_frame){
143 
144  _frame = dynamic_cast<FrameType_t*>(create_id3v2_frame());
145 
146  if(!_frame){
147  return false;
148  }
149  created = true;
150  }
151 
152  _data_model = data_model;
153 
155 
156  if(created){
157  // after that, no need to delete _frame
158  _tag->addFrame(_frame);
159  }
160 
161  return true;
162  }
163 
164 
169  bool is_frame_found() const {
170  return (_frame != nullptr);
171  }
172  };
173 }
174 #endif // ABSTRACTFRAME_H
virtual bool read(ModelType_t &data)
sets the _data_model by reading from the frame
Definition: AbstractFrame.h:116
virtual bool write(const ModelType_t &data_model)
insert the _data_model into the frame
Definition: AbstractFrame.h:134
bool is_frame_found() const
if the frame was found when called read()
Definition: AbstractFrame.h:169
ID3v2Frame namespace.
Definition: AbstractFrame.h:38
The AbstractFrame class for example AbstractFrame<Discnumber, TagLib::ID3v2::TextIdentificationFrame>...
Definition: AbstractFrame.h:47
virtual void map_model_to_frame()=0
map_model_to_frame maps the model to the frame and vice versa so the frame knows how to get/set data ...
virtual TagLib::ID3v2::Frame * create_id3v2_frame()=0
create_id3v2_frame creates new id3v2 frame if there&#39;s no frame we have to create it manually every su...