Sayonara Player
ID3v2Frame.h
1 /* AbstractFrame.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 #ifndef ID3V2_FRAME_H_
22 #define ID3V2_FRAME_H_
23 
24 #include "Utils/Tagging/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 
37 namespace ID3v2
38 {
39  template<typename ModelType_t, typename FrameType_t>
46  class ID3v2Frame :
47  protected Tagging::AbstractFrame<TagLib::ID3v2::Tag>
48  {
49  protected:
50  FrameType_t* mFrame=nullptr;
51 
52  protected:
53 
60  virtual TagLib::ID3v2::Frame* create_id3v2_frame()=0;
61 
67  virtual void map_model_to_frame(const ModelType_t& model, FrameType_t* frame)=0;
68  virtual void map_frame_to_model(const FrameType_t* frame, ModelType_t& model)=0;
69 
70 
71  public:
72  // constructor
73  ID3v2Frame(TagLib::ID3v2::Tag* tag, const char* four) :
74  Tagging::AbstractFrame<TagLib::ID3v2::Tag>(tag, four)
75  {
76  // map, containing [four][frame list]
77  TagLib::ByteVector vec(four, 4);
78  TagLib::ID3v2::FrameListMap map = tag->frameListMap();
79  TagLib::ID3v2::FrameList frame_list = map[vec];
80  if(!frame_list.isEmpty())
81  {
82  mFrame = dynamic_cast<FrameType_t*>(frame_list.front());
83  }
84  }
85 
86  // destructor
87  virtual ~ID3v2Frame() = default;
88 
89 
90  //
96  virtual bool read(ModelType_t& data)
97  {
98  if(!mFrame){
99  return false;
100  }
101 
102  map_frame_to_model(mFrame, data);
103 
104  return true;
105  }
106 
107 
108  //
114  virtual bool write(const ModelType_t& data_model)
115  {
116  TagLib::ID3v2::Tag* tag = this->tag();
117  if(!tag){
118  return false;
119  }
120 
121  bool created = false;
122  if(!mFrame)
123  {
124  mFrame = dynamic_cast<FrameType_t*>(create_id3v2_frame());
125  if(!mFrame){
126  return false;
127  }
128 
129  created = true;
130  }
131 
132  map_model_to_frame(data_model, mFrame);
133 
134  if(created)
135  {
136  // after that, no need to delete mFrame
137  tag->addFrame(mFrame);
138  }
139 
140  return true;
141  }
142 
143 
148  bool is_frame_found() const
149  {
150  return (mFrame != nullptr);
151  }
152 
153  FrameType_t* frame()
154  {
155  return mFrame;
156  }
157  };
158 }
159 
160 #endif // ABSTRACTFRAME_H
Tagging::AbstractFrame
Definition: AbstractFrame.h:57
ID3v2::ID3v2Frame::map_model_to_frame
virtual void map_model_to_frame(const ModelType_t &model, FrameType_t *frame)=0
map_model_to_frame maps the model to the frame and vice versa so the frame knows how to get/set data
ID3v2::ID3v2Frame
The AbstractFrame class for example AbstractFrame<Discnumber, TagLib::ID3v2::TextIdentificationFrame>
Definition: ID3v2Frame.h:48
ID3v2::ID3v2Frame::is_frame_found
bool is_frame_found() const
if the frame was found when called read()
Definition: ID3v2Frame.h:148
ID3v2::ID3v2Frame::write
virtual bool write(const ModelType_t &data_model)
insert the _data_model into the frame
Definition: ID3v2Frame.h:114
ID3v2::ID3v2Frame::read
virtual bool read(ModelType_t &data)
sets the _data_model by reading from the frame
Definition: ID3v2Frame.h:96
Tagging
The GUI_TagEdit class.
Definition: GenreFetcher.h:34
ID3v2::ID3v2Frame::create_id3v2_frame
virtual TagLib::ID3v2::Frame * create_id3v2_frame()=0
create_id3v2_frame creates new id3v2 frame if there's no frame we have to create it manually every su...
ID3v2
ID3v2Frame namespace.
Definition: AlbumArtist.h:27