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., 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 "tag.h"
27 #include "tstringlist.h"
28 #include "tpropertymap.h"
29 
30 using namespace TagLib;
31 
32 class Tag::TagPrivate
33 {
34 
35 };
36 
37 Tag::Tag()
38 {
39 
40 }
41 
42 Tag::~Tag()
43 {
44 
45 }
46 
47 bool Tag::isEmpty() const
48 {
49   return (title().isEmpty() &&
50           artist().isEmpty() &&
51           album().isEmpty() &&
52           comment().isEmpty() &&
53           genre().isEmpty() &&
54           year() == 0 &&
55           track() == 0);
56 }
57 
58 PropertyMap Tag::properties() const
59 {
60   PropertyMap map;
61   if(!(title().isEmpty()))
62     map["TITLE"].append(title());
63   if(!(artist().isEmpty()))
64     map["ARTIST"].append(artist());
65   if(!(album().isEmpty()))
66     map["ALBUM"].append(album());
67   if(!(comment().isEmpty()))
68     map["COMMENT"].append(comment());
69   if(!(genre().isEmpty()))
70     map["GENRE"].append(genre());
71   if(!(year() == 0))
72     map["DATE"].append(String::number(year()));
73   if(!(track() == 0))
74     map["TRACKNUMBER"].append(String::number(track()));
75   return map;
76 }
77 
78 void Tag::removeUnsupportedProperties(const StringList&)
79 {
80 }
81 
82 PropertyMap Tag::setProperties(const PropertyMap &origProps)
83 {
84   PropertyMap properties(origProps);
85   properties.removeEmpty();
86   StringList oneValueSet;
87   // can this be simplified by using some preprocessor defines / function pointers?
88   if(properties.contains("TITLE")) {
89     setTitle(properties["TITLE"].front());
90     oneValueSet.append("TITLE");
91   } else
92     setTitle(String());
93 
94   if(properties.contains("ARTIST")) {
95     setArtist(properties["ARTIST"].front());
96     oneValueSet.append("ARTIST");
97   } else
98     setArtist(String());
99 
100   if(properties.contains("ALBUM")) {
101     setAlbum(properties["ALBUM"].front());
102     oneValueSet.append("ALBUM");
103   } else
104     setAlbum(String());
105 
106   if(properties.contains("COMMENT")) {
107     setComment(properties["COMMENT"].front());
108     oneValueSet.append("COMMENT");
109   } else
110     setComment(String());
111 
112   if(properties.contains("GENRE")) {
113     setGenre(properties["GENRE"].front());
114     oneValueSet.append("GENRE");
115   } else
116     setGenre(String());
117 
118   if(properties.contains("DATE")) {
119     bool ok;
120     int date = properties["DATE"].front().toInt(&ok);
121     if(ok) {
122       setYear(date);
123       oneValueSet.append("DATE");
124     } else
125       setYear(0);
126   }
127   else
128     setYear(0);
129 
130   if(properties.contains("TRACKNUMBER")) {
131     bool ok;
132     int track = properties["TRACKNUMBER"].front().toInt(&ok);
133     if(ok) {
134       setTrack(track);
135       oneValueSet.append("TRACKNUMBER");
136     } else
137       setTrack(0);
138   }
139   else
140     setTrack(0);
141 
142   // for each tag that has been set above, remove the first entry in the corresponding
143   // value list. The others will be returned as unsupported by this format.
144   for(StringList::ConstIterator it = oneValueSet.begin(); it != oneValueSet.end(); ++it) {
145     if(properties[*it].size() == 1)
146       properties.erase(*it);
147     else
148       properties[*it].erase( properties[*it].begin() );
149   }
150   return properties;
151 }
152 
153 void Tag::duplicate(const Tag *source, Tag *target, bool overwrite) // static
154 {
155   if(overwrite) {
156     target->setTitle(source->title());
157     target->setArtist(source->artist());
158     target->setAlbum(source->album());
159     target->setComment(source->comment());
160     target->setGenre(source->genre());
161     target->setYear(source->year());
162     target->setTrack(source->track());
163   }
164   else {
165     if(target->title().isEmpty())
166       target->setTitle(source->title());
167     if(target->artist().isEmpty())
168       target->setArtist(source->artist());
169     if(target->album().isEmpty())
170       target->setAlbum(source->album());
171     if(target->comment().isEmpty())
172       target->setComment(source->comment());
173     if(target->genre().isEmpty())
174       target->setGenre(source->genre());
175     if(target->year() <= 0)
176       target->setYear(source->year());
177     if(target->track() <= 0)
178       target->setTrack(source->track());
179   }
180 }
181