1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/***************************************************************************
copyright : (C) 2002 - 2008 by Scott Wheeler
email : wheeler@kde.org
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include "tagunion.h"
#include <array>
#include "tstringlist.h"
#include "tpropertymap.h"
using namespace TagLib;
#define stringUnion(method) \
do { \
if(tag(0) && !tag(0)->method().isEmpty()) \
return tag(0)->method(); \
if(tag(1) && !tag(1)->method().isEmpty()) \
return tag(1)->method(); \
if(tag(2) && !tag(2)->method().isEmpty()) \
return tag(2)->method(); \
return String(); \
} while(0)
#define numberUnion(method) \
do { \
if(tag(0) && tag(0)->method() > 0) \
return tag(0)->method(); \
if(tag(1) && tag(1)->method() > 0) \
return tag(1)->method(); \
if(tag(2) && tag(2)->method() > 0) \
return tag(2)->method(); \
return 0; \
} while(0)
#define setUnion(method, value) \
do { \
if(tag(0)) \
tag(0)->set##method(value); \
if(tag(1)) \
tag(1)->set##method(value); \
if(tag(2)) \
tag(2)->set##method(value); \
} while(0)
class TagUnion::TagUnionPrivate
{
public:
TagUnionPrivate() = default;
~TagUnionPrivate()
{
for(Tag *tag : tags)
delete tag;
}
TagUnionPrivate(const TagUnionPrivate &) = delete;
TagUnionPrivate &operator=(const TagUnionPrivate &) = delete;
std::array<Tag *, 3> tags { nullptr, nullptr, nullptr };
};
TagUnion::TagUnion(Tag *first, Tag *second, Tag *third) :
d(std::make_unique<TagUnionPrivate>())
{
d->tags = { first, second, third };
}
TagUnion::~TagUnion() = default;
Tag *TagUnion::operator[](int index) const
{
return tag(index);
}
Tag *TagUnion::tag(int index) const
{
return d->tags[index];
}
void TagUnion::set(int index, Tag *tag)
{
delete d->tags[index];
d->tags[index] = tag;
}
PropertyMap TagUnion::properties() const
{
auto it = std::find_if(d->tags.cbegin(), d->tags.cend(), [](const Tag *t) {
return t && !t->isEmpty();
});
return it != d->tags.cend() ? (*it)->properties() : PropertyMap();
}
void TagUnion::removeUnsupportedProperties(const StringList &unsupported)
{
for(const auto &t : d->tags) {
if(t) {
t->removeUnsupportedProperties(unsupported);
}
}
}
StringList TagUnion::complexPropertyKeys() const
{
for(const auto &t : d->tags) {
if(t) {
if(const StringList keys = t->complexPropertyKeys(); !keys.isEmpty()) {
return keys;
}
}
}
return StringList();
}
List<VariantMap> TagUnion::complexProperties(const String &key) const
{
for(const auto &t : d->tags) {
if(t) {
if(const List<VariantMap> props = t->complexProperties(key);
!props.isEmpty()) {
return props;
}
}
}
return List<VariantMap>();
}
bool TagUnion::setComplexProperties(const String &key, const List<VariantMap> &value)
{
bool combinedResult = false;
for(const auto &t : d->tags) {
if(t) {
if(t->setComplexProperties(key, value)) {
combinedResult = true;
}
}
}
return combinedResult;
}
String TagUnion::title() const
{
stringUnion(title);
}
String TagUnion::artist() const
{
stringUnion(artist);
}
String TagUnion::album() const
{
stringUnion(album);
}
String TagUnion::comment() const
{
stringUnion(comment);
}
String TagUnion::genre() const
{
stringUnion(genre);
}
unsigned int TagUnion::year() const
{
numberUnion(year);
}
unsigned int TagUnion::track() const
{
numberUnion(track);
}
void TagUnion::setTitle(const String &s)
{
setUnion(Title, s);
}
void TagUnion::setArtist(const String &s)
{
setUnion(Artist, s);
}
void TagUnion::setAlbum(const String &s)
{
setUnion(Album, s);
}
void TagUnion::setComment(const String &s)
{
setUnion(Comment, s);
}
void TagUnion::setGenre(const String &s)
{
setUnion(Genre, s);
}
void TagUnion::setYear(unsigned int i)
{
setUnion(Year, i);
}
void TagUnion::setTrack(unsigned int i)
{
setUnion(Track, i);
}
bool TagUnion::isEmpty() const
{
return std::none_of(d->tags.begin(), d->tags.end(), [](auto t) { return t && !t->isEmpty(); });
}
|