Version bump
[someplayer] / src / taglib / mpeg / id3v2 / frames / popularimeterframe.cpp
1 /***************************************************************************
2     copyright            : (C) 2008 by Lukas Lalinsky
3     email                : lalinsky@gmail.com
4  ***************************************************************************/
5
6 /***************************************************************************
7  *   This library is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU Lesser General Public License version   *
9  *   2.1 as published by the Free Software Foundation.                     *
10  *                                                                         *
11  *   This library is distributed in the hope that it will be useful, but   *
12  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
14  *   Lesser General Public License for more details.                       *
15  *                                                                         *
16  *   You should have received a copy of the GNU Lesser General Public      *
17  *   License along with this library; if not, write to the Free Software   *
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
19  *   USA                                                                   *
20  *                                                                         *
21  *   Alternatively, this file is available under the Mozilla Public        *
22  *   License Version 1.1.  You may obtain a copy of the License at         *
23  *   http://www.mozilla.org/MPL/                                           *
24  ***************************************************************************/
25
26 #include <tdebug.h>
27
28 #include "popularimeterframe.h"
29
30 using namespace TagLib;
31 using namespace ID3v2;
32
33 class PopularimeterFrame::PopularimeterFramePrivate
34 {
35 public:
36   PopularimeterFramePrivate() : rating(0), counter(0) {}
37   String email;
38   int rating;
39   TagLib::uint counter;
40 };
41
42 ////////////////////////////////////////////////////////////////////////////////
43 // public members
44 ////////////////////////////////////////////////////////////////////////////////
45
46 PopularimeterFrame::PopularimeterFrame() : Frame("POPM")
47 {
48   d = new PopularimeterFramePrivate;
49 }
50
51 PopularimeterFrame::PopularimeterFrame(const ByteVector &data) : Frame(data)
52 {
53   d = new PopularimeterFramePrivate;
54   setData(data);
55 }
56
57 PopularimeterFrame::~PopularimeterFrame()
58 {
59   delete d;
60 }
61
62 String PopularimeterFrame::toString() const
63 {
64   return d->email + " rating=" + String::number(d->rating) + " counter=" + String::number(d->counter);
65 }
66
67 String PopularimeterFrame::email() const
68 {
69   return d->email;
70 }
71
72 void PopularimeterFrame::setEmail(const String &s)
73 {
74   d->email = s;
75 }
76
77 int PopularimeterFrame::rating() const
78 {
79   return d->rating;
80 }
81
82 void PopularimeterFrame::setRating(int s)
83 {
84   d->rating = s;
85 }
86
87 TagLib::uint PopularimeterFrame::counter() const
88 {
89   return d->counter;
90 }
91
92 void PopularimeterFrame::setCounter(TagLib::uint s)
93 {
94   d->counter = s;
95 }
96
97 ////////////////////////////////////////////////////////////////////////////////
98 // protected members
99 ////////////////////////////////////////////////////////////////////////////////
100
101 void PopularimeterFrame::parseFields(const ByteVector &data)
102 {
103   int pos = 0, size = int(data.size());
104
105   d->email = readStringField(data, String::Latin1, &pos);
106
107   d->rating = 0;
108   d->counter = 0;
109   if(pos < size) {
110     d->rating = (unsigned char)(data[pos++]);
111     if(pos < size) {
112       d->counter = data.mid(pos, 4).toUInt();
113     }
114   }
115 }
116
117 ByteVector PopularimeterFrame::renderFields() const
118 {
119   ByteVector data;
120
121   data.append(d->email.data(String::Latin1));
122   data.append(textDelimiter(String::Latin1));
123   data.append(char(d->rating));
124   data.append(ByteVector::fromUInt(d->counter));
125
126   return data;
127 }
128
129 ////////////////////////////////////////////////////////////////////////////////
130 // private members
131 ////////////////////////////////////////////////////////////////////////////////
132
133 PopularimeterFrame::PopularimeterFrame(const ByteVector &data, Header *h) : Frame(h)
134 {
135   d = new PopularimeterFramePrivate;
136   parseFields(fieldData(data));
137 }