1 /***************************************************************************
2 copyright : (C) 2004 by Allan Sandfeld Jensen
3 email : kde@carewolf.com
4 ***************************************************************************/
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. *
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. *
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 *
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 ***************************************************************************/
26 #include <tbytevectorlist.h>
31 using namespace TagLib;
34 class APE::Item::ItemPrivate
37 ItemPrivate() : type(Text), readOnly(false) {}
51 APE::Item::Item(const String &key, const String &value)
55 d->text.append(value);
58 APE::Item::Item(const String &key, const StringList &values)
65 APE::Item::Item(const Item &item)
67 d = new ItemPrivate(*item.d);
75 Item &APE::Item::operator=(const Item &item)
78 d = new ItemPrivate(*item.d);
82 void APE::Item::setReadOnly(bool readOnly)
84 d->readOnly = readOnly;
87 bool APE::Item::isReadOnly() const
92 void APE::Item::setType(APE::Item::ItemTypes val)
97 APE::Item::ItemTypes APE::Item::type() const
102 String APE::Item::key() const
107 ByteVector APE::Item::value() const
109 // This seems incorrect as it won't be actually rendering the value to keep it
115 void APE::Item::setKey(const String &key)
120 void APE::Item::setValue(const String &value)
125 void APE::Item::setValues(const StringList &value)
130 void APE::Item::appendValue(const String &value)
132 d->text.append(value);
135 void APE::Item::appendValues(const StringList &values)
137 d->text.append(values);
140 int APE::Item::size() const
142 return 8 + d->key.size() + 1 + d->value.size();
145 StringList APE::Item::toStringList() const
150 StringList APE::Item::values() const
155 String APE::Item::toString() const
157 return isEmpty() ? String::null : d->text.front();
160 bool APE::Item::isEmpty() const
165 if(d->text.isEmpty())
167 if(d->text.size() == 1 && d->text.front().isEmpty())
171 return d->value.isEmpty();
177 void APE::Item::parse(const ByteVector &data)
179 // 11 bytes is the minimum size for an APE item
181 if(data.size() < 11) {
182 debug("APE::Item::parse() -- no data in item");
186 uint valueLength = data.mid(0, 4).toUInt(false);
187 uint flags = data.mid(4, 4).toUInt(false);
189 d->key = String(data.mid(8), String::UTF8);
191 d->value = data.mid(8 + d->key.size() + 1, valueLength);
193 setReadOnly(flags & 1);
194 setType(ItemTypes((flags >> 1) & 3));
197 d->text = StringList(ByteVectorList::split(d->value, '\0'), String::UTF8);
200 ByteVector APE::Item::render() const
203 TagLib::uint flags = ((d->readOnly) ? 1 : 0) | (d->type << 1);
209 if(d->type == Text) {
210 StringList::ConstIterator it = d->text.begin();
212 value.append(it->data(String::UTF8));
214 for(; it != d->text.end(); ++it) {
216 value.append(it->data(String::UTF8));
221 value.append(d->value);
223 data.append(ByteVector::fromUInt(value.size(), false));
224 data.append(ByteVector::fromUInt(flags, false));
225 data.append(d->key.data(String::UTF8));
226 data.append(ByteVector('\0'));