Added TagLib (with AUTORS and COPYING files)
[someplayer] / src / taglib / tagunion.cpp
1 /***************************************************************************
2     copyright            : (C) 2002 - 2008 by Scott Wheeler
3     email                : wheeler@kde.org
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 "tagunion.h"
27
28 using namespace TagLib;
29
30 #define stringUnion(method)                                          \
31   if(tag(0) && !tag(0)->method().isEmpty())                          \
32     return tag(0)->method();                                         \
33   if(tag(1) && !tag(1)->method().isEmpty())                          \
34     return tag(1)->method();                                         \
35   if(tag(2) && !tag(2)->method().isEmpty())                          \
36     return tag(2)->method();                                         \
37   return String::null                                                \
38
39 #define numberUnion(method)                                          \
40   if(tag(0) && tag(0)->method() > 0)                                 \
41     return tag(0)->method();                                         \
42   if(tag(1) && tag(1)->method() > 0)                                 \
43     return tag(1)->method();                                         \
44   if(tag(2) && tag(2)->method() > 0)                                 \
45     return tag(2)->method();                                         \
46   return 0
47
48 #define setUnion(method, value)                                      \
49   if(tag(0))                                                         \
50     tag(0)->set##method(value);                                      \
51   if(tag(1))                                                         \
52     tag(1)->set##method(value);                                      \
53   if(tag(2))                                                         \
54     tag(2)->set##method(value);                                      \
55
56 class TagUnion::TagUnionPrivate
57 {
58 public:
59   TagUnionPrivate() : tags(3, static_cast<Tag *>(0))
60   {
61
62   }
63
64   ~TagUnionPrivate()
65   {
66     delete tags[0];
67     delete tags[1];
68     delete tags[2];
69   }
70
71   std::vector<Tag *> tags;
72 };
73
74 TagUnion::TagUnion(Tag *first, Tag *second, Tag *third)
75 {
76   d = new TagUnionPrivate;
77
78   d->tags[0] = first;
79   d->tags[1] = second;
80   d->tags[2] = third;
81 }
82
83 TagUnion::~TagUnion()
84 {
85   delete d;
86 }
87
88 Tag *TagUnion::operator[](int index) const
89 {
90   return tag(index);
91 }
92
93 Tag *TagUnion::tag(int index) const
94 {
95   return d->tags[index];
96 }
97
98 void TagUnion::set(int index, Tag *tag)
99 {
100   delete d->tags[index];
101   d->tags[index] = tag;
102 }
103
104 String TagUnion::title() const
105 {
106   stringUnion(title);
107 }
108
109 String TagUnion::artist() const
110 {
111   stringUnion(artist);
112 }
113
114 String TagUnion::album() const
115 {
116   stringUnion(album);
117 }
118
119 String TagUnion::comment() const
120 {
121   stringUnion(comment);
122 }
123
124 String TagUnion::genre() const
125 {
126   stringUnion(genre);
127 }
128
129 TagLib::uint TagUnion::year() const
130 {
131   numberUnion(year);
132 }
133
134 TagLib::uint TagUnion::track() const
135 {
136   numberUnion(track);
137 }
138
139 void TagUnion::setTitle(const String &s)
140 {
141   setUnion(Title, s);
142 }
143
144 void TagUnion::setArtist(const String &s)
145 {
146   setUnion(Artist, s);
147 }
148
149 void TagUnion::setAlbum(const String &s)
150 {
151   setUnion(Album, s);
152 }
153
154 void TagUnion::setComment(const String &s)
155 {
156   setUnion(Comment, s);
157 }
158
159 void TagUnion::setGenre(const String &s)
160 {
161   setUnion(Genre, s);
162 }
163
164 void TagUnion::setYear(uint i)
165 {
166   setUnion(Year, i);
167 }
168
169 void TagUnion::setTrack(uint i)
170 {
171   setUnion(Track, i);
172 }
173
174 bool TagUnion::isEmpty() const
175 {
176   if(d->tags[0] && !d->tags[0]->isEmpty())
177     return false;
178   if(d->tags[1] && !d->tags[1]->isEmpty())
179     return false;
180   if(d->tags[2] && !d->tags[2]->isEmpty())
181     return false;
182
183   return true;
184 }
185