Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / mpeg / id3v2 / frames / unsynchronizedlyricsframe.cpp
1 /***************************************************************************
2     copyright            : (C) 2002 - 2008 by Scott Wheeler
3     email                : wheeler@kde.org
4     copyright            : (C) 2006 by Urs Fleisch
5     email                : ufleisch@users.sourceforge.net
6  ***************************************************************************/
7
8 /***************************************************************************
9  *   This library is free software; you can redistribute it and/or modify  *
10  *   it  under the terms of the GNU Lesser General Public License version  *
11  *   2.1 as published by the Free Software Foundation.                     *
12  *                                                                         *
13  *   This library is distributed in the hope that it will be useful, but   *
14  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
16  *   Lesser General Public License for more details.                       *
17  *                                                                         *
18  *   You should have received a copy of the GNU Lesser General Public      *
19  *   License along with this library; if not, write to the Free Software   *
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
21  *   USA                                                                   *
22  *                                                                         *
23  *   Alternatively, this file is available under the Mozilla Public        *
24  *   License Version 1.1.  You may obtain a copy of the License at         *
25  *   http://www.mozilla.org/MPL/                                           *
26  ***************************************************************************/
27
28 #include "unsynchronizedlyricsframe.h"
29 #include <tbytevectorlist.h>
30 #include <tdebug.h>
31
32 using namespace TagLib;
33 using namespace ID3v2;
34
35 class UnsynchronizedLyricsFrame::UnsynchronizedLyricsFramePrivate
36 {
37 public:
38   UnsynchronizedLyricsFramePrivate() : textEncoding(String::Latin1) {}
39   String::Type textEncoding;
40   ByteVector language;
41   String description;
42   String text;
43 };
44
45 ////////////////////////////////////////////////////////////////////////////////
46 // public members
47 ////////////////////////////////////////////////////////////////////////////////
48
49 UnsynchronizedLyricsFrame::UnsynchronizedLyricsFrame(String::Type encoding) :
50   Frame("USLT")
51 {
52   d = new UnsynchronizedLyricsFramePrivate;
53   d->textEncoding = encoding;
54 }
55
56 UnsynchronizedLyricsFrame::UnsynchronizedLyricsFrame(const ByteVector &data) :
57   Frame(data)
58 {
59   d = new UnsynchronizedLyricsFramePrivate;
60   setData(data);
61 }
62
63 UnsynchronizedLyricsFrame::~UnsynchronizedLyricsFrame()
64 {
65   delete d;
66 }
67
68 String UnsynchronizedLyricsFrame::toString() const
69 {
70   return d->text;
71 }
72
73 ByteVector UnsynchronizedLyricsFrame::language() const
74 {
75   return d->language;
76 }
77
78 String UnsynchronizedLyricsFrame::description() const
79 {
80   return d->description;
81 }
82
83 String UnsynchronizedLyricsFrame::text() const
84 {
85   return d->text;
86 }
87
88 void UnsynchronizedLyricsFrame::setLanguage(const ByteVector &languageEncoding)
89 {
90   d->language = languageEncoding.mid(0, 3);
91 }
92
93 void UnsynchronizedLyricsFrame::setDescription(const String &s)
94 {
95   d->description = s;
96 }
97
98 void UnsynchronizedLyricsFrame::setText(const String &s)
99 {
100   d->text = s;
101 }
102
103
104 String::Type UnsynchronizedLyricsFrame::textEncoding() const
105 {
106   return d->textEncoding;
107 }
108
109 void UnsynchronizedLyricsFrame::setTextEncoding(String::Type encoding)
110 {
111   d->textEncoding = encoding;
112 }
113
114 ////////////////////////////////////////////////////////////////////////////////
115 // protected members
116 ////////////////////////////////////////////////////////////////////////////////
117
118 void UnsynchronizedLyricsFrame::parseFields(const ByteVector &data)
119 {
120   if(data.size() < 5) {
121     debug("An unsynchronized lyrics frame must contain at least 5 bytes.");
122     return;
123   }
124
125   d->textEncoding = String::Type(data[0]);
126   d->language = data.mid(1, 3);
127
128   int byteAlign
129     = d->textEncoding == String::Latin1 || d->textEncoding == String::UTF8 ? 1 : 2;
130
131   ByteVectorList l =
132     ByteVectorList::split(data.mid(4), textDelimiter(d->textEncoding), byteAlign, 2);
133
134   if(l.size() == 2) {
135     d->description = String(l.front(), d->textEncoding);
136     d->text = String(l.back(), d->textEncoding);
137   }
138 }
139
140 ByteVector UnsynchronizedLyricsFrame::renderFields() const
141 {
142   ByteVector v;
143
144   v.append(char(d->textEncoding));
145   v.append(d->language.size() == 3 ? d->language : "XXX");
146   v.append(d->description.data(d->textEncoding));
147   v.append(textDelimiter(d->textEncoding));
148   v.append(d->text.data(d->textEncoding));
149
150   return v;
151 }
152
153 ////////////////////////////////////////////////////////////////////////////////
154 // private members
155 ////////////////////////////////////////////////////////////////////////////////
156
157 UnsynchronizedLyricsFrame::UnsynchronizedLyricsFrame(const ByteVector &data, Header *h)
158   : Frame(h)
159 {
160   d = new UnsynchronizedLyricsFramePrivate();
161   parseFields(fieldData(data));
162 }