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., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 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:
PopularimeterFramePrivate()36 PopularimeterFramePrivate() : rating(0), counter(0) {}
37 String email;
38 int rating;
39 unsigned int counter;
40 };
41
42 ////////////////////////////////////////////////////////////////////////////////
43 // public members
44 ////////////////////////////////////////////////////////////////////////////////
45
PopularimeterFrame()46 PopularimeterFrame::PopularimeterFrame() :
47 Frame("POPM"),
48 d(new PopularimeterFramePrivate())
49 {
50 }
51
PopularimeterFrame(const ByteVector & data)52 PopularimeterFrame::PopularimeterFrame(const ByteVector &data) :
53 Frame(data),
54 d(new PopularimeterFramePrivate())
55 {
56 setData(data);
57 }
58
~PopularimeterFrame()59 PopularimeterFrame::~PopularimeterFrame()
60 {
61 delete d;
62 }
63
toString() const64 String PopularimeterFrame::toString() const
65 {
66 return d->email + " rating=" + String::number(d->rating) + " counter=" + String::number(d->counter);
67 }
68
email() const69 String PopularimeterFrame::email() const
70 {
71 return d->email;
72 }
73
setEmail(const String & s)74 void PopularimeterFrame::setEmail(const String &s)
75 {
76 d->email = s;
77 }
78
rating() const79 int PopularimeterFrame::rating() const
80 {
81 return d->rating;
82 }
83
setRating(int s)84 void PopularimeterFrame::setRating(int s)
85 {
86 d->rating = s;
87 }
88
counter() const89 unsigned int PopularimeterFrame::counter() const
90 {
91 return d->counter;
92 }
93
setCounter(unsigned int s)94 void PopularimeterFrame::setCounter(unsigned int s)
95 {
96 d->counter = s;
97 }
98
99 ////////////////////////////////////////////////////////////////////////////////
100 // protected members
101 ////////////////////////////////////////////////////////////////////////////////
102
parseFields(const ByteVector & data)103 void PopularimeterFrame::parseFields(const ByteVector &data)
104 {
105 int pos = 0, size = int(data.size());
106
107 d->email = readStringField(data, String::Latin1, &pos);
108
109 d->rating = 0;
110 d->counter = 0;
111 if(pos < size) {
112 d->rating = (unsigned char)(data[pos++]);
113 if(pos < size) {
114 d->counter = data.toUInt(static_cast<unsigned int>(pos));
115 }
116 }
117 }
118
renderFields() const119 ByteVector PopularimeterFrame::renderFields() const
120 {
121 ByteVector data;
122
123 data.append(d->email.data(String::Latin1));
124 data.append(textDelimiter(String::Latin1));
125 data.append(char(d->rating));
126 data.append(ByteVector::fromUInt(d->counter));
127
128 return data;
129 }
130
131 ////////////////////////////////////////////////////////////////////////////////
132 // private members
133 ////////////////////////////////////////////////////////////////////////////////
134
PopularimeterFrame(const ByteVector & data,Header * h)135 PopularimeterFrame::PopularimeterFrame(const ByteVector &data, Header *h) :
136 Frame(h),
137 d(new PopularimeterFramePrivate())
138 {
139 parseFields(fieldData(data));
140 }
141