Revert previous commit, was incorrect
[amarok.git] / src / meta / MetaUtility.cpp
blob68943f36cfa5e107de08c91c253c385da7200a01
1 /*
2 Copyright (C) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "MetaUtility.h"
21 #include "Meta.h"
22 #include "meta/Capability.h"
23 #include "meta/EditCapability.h"
25 #include <QChar>
27 static const QString XESAM_ALBUM = "http://freedesktop.org/standards/xesam/1.0/core#album";
28 static const QString XESAM_ARTIST = "http://freedesktop.org/standards/xesam/1.0/core#author";
29 static const QString XESAM_BITRATE = "http://freedesktop.org/standards/xesam/1.0/core#audioBitrate";
30 static const QString XESAM_BPM = "http://freedesktop.org/standards/xesam/1.0/core#audioBPM";
31 static const QString XESAM_CODEC = "http://freedesktop.org/standards/xesam/1.0/core#audioCodec";
32 static const QString XESAM_COMMENT = "http://freedesktop.org/standards/xesam/1.0/core#comment";
33 static const QString XESAM_COMPOSER = "http://freedesktop.org/standards/xesam/1.0/core#composer";
34 static const QString XESAM_DISCNUMBER = "http://freedesktop.org/standards/xesam/1.0/core#discNumber";
35 static const QString XESAM_FILESIZE = "http://freedesktop.org/standards/xesam/1.0/core#size";
36 static const QString XESAM_GENRE = "http://freedesktop.org/standards/xesam/1.0/core#genre";
37 static const QString XESAM_LENGTH = "http://freedesktop.org/standards/xesam/1.0/core#mediaDuration";
38 static const QString XESAM_RATING = "http://freedesktop.org/standards/xesam/1.0/core#userRating";
39 static const QString XESAM_SAMPLERATE = "http://freedesktop.org/standards/xesam/1.0/core#audioSampleRate";
40 static const QString XESAM_TITLE = "http://freedesktop.org/standards/xesam/1.0/core#title";
41 static const QString XESAM_TRACKNUMBER = "http://freedesktop.org/standards/xesam/1.0/core#trackNumber";
42 static const QString XESAM_URL = "http://freedesktop.org/standards/xesam/1.0/core#url";
43 static const QString XESAM_YEAR = "http://freedesktop.org/standards/xesam/1.0/core#contentCreated";
45 static const QString XESAM_SCORE = "http://freedesktop.org/standards/xesam/1.0/core#autoRating";
46 static const QString XESAM_PLAYCOUNT = "http://freedesktop.org/standards/xesam/1.0/core#useCount";
47 static const QString XESAM_FIRST_PLAYED = "http://freedesktop.org/standards/xesam/1.0/core#firstUsed";
48 static const QString XESAM_LAST_PLAYED = "http://freedesktop.org/standards/xesam/1.0/core#lastUsed";
50 //static bool conversionMapsInitialised = false;
52 QVariantMap
53 Meta::Field::mapFromTrack( const Meta::Track *track )
55 //note: track does not support bpm, first_played yet
56 QVariantMap map;
57 if( !track )
58 return map;
60 if( track->name().isEmpty() )
61 map.insert( Meta::Field::TITLE, QVariant( track->prettyName() ) );
62 else
63 map.insert( Meta::Field::TITLE, QVariant( track->name() ) );
64 if( !track->artist()->name().isEmpty() )
65 map.insert( Meta::Field::ARTIST, QVariant( track->artist()->name() ) );
66 if( !track->album()->name().isEmpty() )
67 map.insert( Meta::Field::ALBUM, QVariant( track->album()->name() ) );
68 if( track->filesize() )
69 map.insert( Meta::Field::FILESIZE, QVariant( track->filesize() ) );
70 if( !track->genre()->name().isEmpty() )
71 map.insert( Meta::Field::GENRE, QVariant( track->genre()->name() ) );
72 if( !track->composer()->name().isEmpty() )
73 map.insert( Meta::Field::COMPOSER, QVariant( track->composer()->name() ) );
74 if( !track->year()->name().isEmpty() )
75 map.insert( Meta::Field::YEAR, QVariant( track->year()->name() ) );
76 if( !track->comment().isEmpty() )
77 map.insert( Meta::Field::COMMENT, QVariant( track->comment() ) );
78 if( track->trackNumber() )
79 map.insert( Meta::Field::TRACKNUMBER, QVariant( track->trackNumber() ) );
80 if( track->discNumber() )
81 map.insert( Meta::Field::DISCNUMBER, QVariant( track->discNumber() ) );
82 if( track->bitrate() )
83 map.insert( Meta::Field::BITRATE, QVariant( track->bitrate() ) );
84 if( track->length() )
85 map.insert( Meta::Field::LENGTH, QVariant( track->length() ) );
86 if( track->sampleRate() )
87 map.insert( Meta::Field::SAMPLERATE, QVariant( track->sampleRate() ) );
89 map.insert( Meta::Field::URL, QVariant( track->prettyUrl() ) );
90 map.insert( Meta::Field::RATING, QVariant( track->rating() ) );
91 map.insert( Meta::Field::SCORE, QVariant( track->score() ) );
92 map.insert( Meta::Field::PLAYCOUNT, QVariant( track->playCount() ) );
93 map.insert( Meta::Field::LAST_PLAYED, QVariant( track->lastPlayed() ) );
95 return map;
99 void
100 Meta::Field::updateTrack( Meta::Track *track, const QVariantMap &metadata )
102 if( !track || !track->hasCapabilityInterface( Meta::Capability::Editable ) )
103 return;
105 Meta::EditCapability *ec = track->as<Meta::EditCapability>();
106 if( !ec || !ec->isEditable() )
107 return;
108 ec->beginMetaDataUpdate();
109 QString title = metadata.contains( Meta::Field::TITLE ) ?
110 metadata.value( Meta::Field::TITLE ).toString() : QString();
111 ec->setTitle( title );
112 QString comment = metadata.contains( Meta::Field::COMMENT ) ?
113 metadata.value( Meta::Field::COMMENT ).toString() : QString();
114 ec->setComment( comment );
115 int tracknr = metadata.contains( Meta::Field::TRACKNUMBER ) ?
116 metadata.value( Meta::Field::TRACKNUMBER ).toInt() : 0;
117 ec->setTrackNumber( tracknr );
118 int discnr = metadata.contains( Meta::Field::DISCNUMBER ) ?
119 metadata.value( Meta::Field::DISCNUMBER ).toInt() : 0;
120 ec->setDiscNumber( discnr );
121 QString artist = metadata.contains( Meta::Field::ARTIST ) ?
122 metadata.value( Meta::Field::ARTIST ).toString() : QString();
123 ec->setArtist( artist );
124 QString album = metadata.contains( Meta::Field::ALBUM ) ?
125 metadata.value( Meta::Field::ALBUM ).toString() : QString();
126 ec->setAlbum( album );
127 QString genre = metadata.contains( Meta::Field::GENRE ) ?
128 metadata.value( Meta::Field::GENRE ).toString() : QString();
129 ec->setGenre( genre );
130 QString composer = metadata.contains( Meta::Field::COMPOSER ) ?
131 metadata.value( Meta::Field::COMPOSER ).toString() : QString();
132 ec->setComposer( composer );
133 QString year = metadata.contains( Meta::Field::YEAR ) ?
134 metadata.value( Meta::Field::YEAR ).toString() : QString();
135 ec->setYear( year );
137 ec->endMetaDataUpdate();
140 QString
141 Meta::Field::xesamPrettyToFullFieldName( const QString &name )
143 if( name == Meta::Field::ARTIST )
144 return XESAM_ARTIST;
145 else if( name == Meta::Field::ALBUM )
146 return XESAM_ALBUM;
147 else if( name == Meta::Field::BITRATE )
148 return XESAM_BITRATE;
149 else if( name == Meta::Field::BPM )
150 return XESAM_BPM;
151 else if( name == Meta::Field::CODEC )
152 return XESAM_CODEC;
153 else if( name == Meta::Field::COMMENT )
154 return XESAM_COMMENT;
155 else if( name == Meta::Field::COMPOSER )
156 return XESAM_COMPOSER;
157 else if( name == Meta::Field::DISCNUMBER )
158 return XESAM_DISCNUMBER;
159 else if( name == Meta::Field::FILESIZE )
160 return XESAM_FILESIZE;
161 else if( name == Meta::Field::GENRE )
162 return XESAM_GENRE;
163 else if( name == Meta::Field::LENGTH )
164 return XESAM_LENGTH;
165 else if( name == Meta::Field::RATING )
166 return XESAM_RATING;
167 else if( name == Meta::Field::SAMPLERATE )
168 return XESAM_SAMPLERATE;
169 else if( name == Meta::Field::TITLE )
170 return XESAM_TITLE;
171 else if( name == Meta::Field::TRACKNUMBER )
172 return XESAM_TRACKNUMBER;
173 else if( name == Meta::Field::URL )
174 return XESAM_URL;
175 else if( name == Meta::Field::YEAR )
176 return XESAM_YEAR;
177 else if( name==Meta::Field::SCORE )
178 return XESAM_SCORE;
179 else if( name==Meta::Field::PLAYCOUNT )
180 return XESAM_PLAYCOUNT;
181 else if( name==Meta::Field::FIRST_PLAYED )
182 return XESAM_FIRST_PLAYED;
183 else if( name==Meta::Field::LAST_PLAYED )
184 return XESAM_LAST_PLAYED;
185 else
186 return "xesamPrettyToFullName: unknown name " + name;
189 QString
190 Meta::Field::xesamFullToPrettyFieldName( const QString &name )
192 if( name == XESAM_ARTIST )
193 return Meta::Field::ARTIST;
194 else if( name == XESAM_ALBUM )
195 return Meta::Field::ALBUM;
196 else if( name == XESAM_BITRATE )
197 return Meta::Field::BITRATE;
198 else if( name == XESAM_BPM )
199 return Meta::Field::BPM;
200 else if( name == XESAM_CODEC )
201 return Meta::Field::CODEC;
202 else if( name == XESAM_COMMENT )
203 return Meta::Field::COMMENT;
204 else if( name == XESAM_COMPOSER )
205 return Meta::Field::COMPOSER;
206 else if( name == XESAM_DISCNUMBER )
207 return Meta::Field::DISCNUMBER;
208 else if( name == XESAM_FILESIZE )
209 return Meta::Field::FILESIZE;
210 else if( name == XESAM_GENRE )
211 return Meta::Field::GENRE;
212 else if( name == XESAM_LENGTH )
213 return Meta::Field::LENGTH;
214 else if( name == XESAM_RATING )
215 return Meta::Field::RATING;
216 else if( name == XESAM_SAMPLERATE )
217 return Meta::Field::SAMPLERATE;
218 else if( name == XESAM_TITLE )
219 return Meta::Field::TITLE;
220 else if( name == XESAM_TRACKNUMBER )
221 return Meta::Field::TRACKNUMBER;
222 else if( name == XESAM_URL )
223 return Meta::Field::URL;
224 else if( name == XESAM_YEAR )
225 return Meta::Field::YEAR;
226 else if( name == XESAM_SCORE )
227 return Meta::Field::SCORE;
228 else if( name == XESAM_PLAYCOUNT )
229 return Meta::Field::PLAYCOUNT;
230 else if( name == XESAM_FIRST_PLAYED )
231 return Meta::Field::FIRST_PLAYED;
232 else if( name == XESAM_LAST_PLAYED )
233 return Meta::Field::LAST_PLAYED;
234 else
235 return "xesamFullToPrettyName: unknown name " + name;
239 QString
240 Meta::msToPrettyTime( int ms )
242 return Meta::secToPrettyTime( ms / 1000 );
245 QString
246 Meta::secToPrettyTime( int seconds )
248 int minutes = ( seconds / 60 ) % 60;
249 int hours = seconds / 3600;
250 QString s = QChar( ':' );
251 s.append( ( seconds % 60 ) < 10 ? QString( "0%1" ).arg( seconds % 60 ) : QString::number( seconds % 60 ) ); //seconds
253 if( hours )
255 s.prepend( minutes < 10 ? QString( "0%1" ).arg( minutes ) : QString::number( minutes ) );
256 s.prepend( ':' );
258 else
260 s.prepend( QString::number( minutes ) );
261 return s;
264 //don't zeroPad the last one, as it can be greater than 2 digits
265 s.prepend( QString::number( hours ) );
267 return s;