Revert previous commit, was incorrect
[amarok.git] / src / meta / file / File.cpp
blob25706aaa8bb123baaebd439973c693348919e1e5
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 "File.h"
20 #include "File_p.h"
22 #include "debug.h"
23 #include "Meta.h"
24 #include "meta/EditCapability.h"
25 #include "MetaUtility.h"
27 #include <QFile>
28 #include <QPointer>
29 #include <QString>
31 #include <kfilemetainfo.h>
32 #include <kfilemetainfoitem.h>
34 using namespace MetaFile;
36 class EditCapabilityImpl : public Meta::EditCapability
38 Q_OBJECT
39 public:
40 EditCapabilityImpl( MetaFile::Track *track )
41 : Meta::EditCapability()
42 , m_track( track ) {}
44 virtual bool isEditable() const { return m_track->isEditable(); }
45 virtual void setAlbum( const QString &newAlbum ) { m_track->setAlbum( newAlbum ); }
46 virtual void setArtist( const QString &newArtist ) { m_track->setArtist( newArtist ); }
47 virtual void setComposer( const QString &newComposer ) { m_track->setComposer( newComposer ); }
48 virtual void setGenre( const QString &newGenre ) { m_track->setGenre( newGenre ); }
49 virtual void setYear( const QString &newYear ) { m_track->setYear( newYear ); }
50 virtual void setTitle( const QString &newTitle ) { m_track->setTitle( newTitle ); }
51 virtual void setComment( const QString &newComment ) { m_track->setComment( newComment ); }
52 virtual void setTrackNumber( int newTrackNumber ) { m_track->setTrackNumber( newTrackNumber ); }
53 virtual void setDiscNumber( int newDiscNumber ) { m_track->setDiscNumber( newDiscNumber ); }
54 virtual void beginMetaDataUpdate() { m_track->beginMetaDataUpdate(); }
55 virtual void endMetaDataUpdate() { m_track->endMetaDataUpdate(); }
56 virtual void abortMetaDataUpdate() { m_track->abortMetaDataUpdate(); }
58 private:
59 KSharedPtr<MetaFile::Track> m_track;
62 Track::Track( const KUrl &url )
63 : Meta::Track()
64 , d( new Track::Private( this ) )
66 d->url = url;
67 d->metaInfo = KFileMetaInfo( url.path() );
68 d->album = Meta::AlbumPtr( new MetaFile::FileAlbum( QPointer<MetaFile::Track::Private>( d ) ) );
69 d->artist = Meta::ArtistPtr( new MetaFile::FileArtist( QPointer<MetaFile::Track::Private>( d ) ) );
70 d->genre = Meta::GenrePtr( new MetaFile::FileGenre( QPointer<MetaFile::Track::Private>( d ) ) );
71 d->composer = Meta::ComposerPtr( new MetaFile::FileComposer( QPointer<MetaFile::Track::Private>( d ) ) );
72 d->year = Meta::YearPtr( new MetaFile::FileYear( QPointer<MetaFile::Track::Private>( d ) ) );
75 Track::~Track()
77 delete d;
80 QString
81 Track::name() const
83 KFileMetaInfoItem item = d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::TITLE ) );
84 if( item.isValid() )
85 return item.value().toString();
86 else
87 return QString();
90 QString
91 Track::prettyName() const
93 return name();
96 QString
97 Track::fullPrettyName() const
99 return name();
102 QString
103 Track::sortableName() const
105 return name();
109 KUrl
110 Track::playableUrl() const
112 return d->url;
115 QString
116 Track::prettyUrl() const
118 return d->url.path();
121 QString
122 Track::url() const
124 return d->url.url();
127 bool
128 Track::isPlayable() const
130 //simple implementation, check internet connectivity or ping server?
131 return true;
134 bool
135 Track::isEditable() const
137 //not this probably needs more work on *nix
138 return QFile::permissions( d->url.path() ) & QFile::WriteUser;
141 Meta::AlbumPtr
142 Track::album() const
144 return d->album;
147 Meta::ArtistPtr
148 Track::artist() const
150 return d->artist;
153 Meta::GenrePtr
154 Track::genre() const
156 return d->genre;
159 Meta::ComposerPtr
160 Track::composer() const
162 return d->composer;
165 Meta::YearPtr
166 Track::year() const
168 return d->year;
171 void
172 Track::setAlbum( const QString &newAlbum )
174 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::ALBUM ) ).setValue( newAlbum );
175 if( !d->batchUpdate )
177 d->metaInfo.applyChanges();
178 notifyObservers();
182 void
183 Track::setArtist( const QString& newArtist )
185 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::ARTIST ) ).setValue( newArtist );
186 if( !d->batchUpdate )
188 d->metaInfo.applyChanges();
189 notifyObservers();
193 void
194 Track::setGenre( const QString& newGenre )
196 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::GENRE ) ).setValue( newGenre );
197 if( !d->batchUpdate )
199 d->metaInfo.applyChanges();
200 notifyObservers();
204 void
205 Track::setComposer( const QString& newComposer )
207 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::COMPOSER ) ).setValue( newComposer );
208 if( !d->batchUpdate )
210 d->metaInfo.applyChanges();
211 notifyObservers();
215 void
216 Track::setYear( const QString& newYear )
218 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::YEAR ) ).setValue( newYear );
219 if( !d->batchUpdate )
221 d->metaInfo.applyChanges();
222 notifyObservers();
226 void
227 Track::setTitle( const QString &newTitle )
229 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::TITLE ) ).setValue( newTitle );
230 if( !d->batchUpdate )
232 d->metaInfo.applyChanges();
233 notifyObservers();
237 QString
238 Track::comment() const
240 KFileMetaInfoItem item = d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::COMMENT ) );
241 if( item.isValid() )
242 return item.value().toString();
243 else
244 return QString();
247 void
248 Track::setComment( const QString& newComment )
250 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::COMMENT ) ).setValue( newComment );
251 if( !d->batchUpdate )
253 d->metaInfo.applyChanges();
254 notifyObservers();
258 double
259 Track::score() const
261 return 0.0;
264 void
265 Track::setScore( double newScore )
267 Q_UNUSED( newScore )
271 Track::rating() const
273 return 0;
276 void
277 Track::setRating( int newRating )
279 Q_UNUSED( newRating )
283 Track::trackNumber() const
285 KFileMetaInfoItem item = d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::TRACKNUMBER ) );
286 if( item.isValid() )
287 return item.value().toInt();
288 else
289 return 0;
292 void
293 Track::setTrackNumber( int newTrackNumber )
295 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::TRACKNUMBER ) ).setValue( newTrackNumber );
296 if( !d->batchUpdate )
298 d->metaInfo.applyChanges();
299 notifyObservers();
304 Track::discNumber() const
306 KFileMetaInfoItem item = d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::DISCNUMBER ) );
307 if( item.isValid() )
308 return item.value().toInt();
309 else
310 return 0;
313 void
314 Track::setDiscNumber( int newDiscNumber )
316 d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::DISCNUMBER ) ).setValue( newDiscNumber );
317 if( !d->batchUpdate )
319 d->metaInfo.applyChanges();
320 notifyObservers();
325 Track::length() const
327 KFileMetaInfoItem item = d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::LENGTH ) );
328 if( item.isValid() )
329 return item.value().toInt();
330 else
331 return 0;
335 Track::filesize() const
337 KFileMetaInfoItem item = d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::FILESIZE ) );
338 if( item.isValid() )
339 return item.value().toInt();
340 else
341 return 0;
345 Track::sampleRate() const
347 KFileMetaInfoItem item = d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::SAMPLERATE ) );
348 if( item.isValid() )
349 return item.value().toInt();
350 else
351 return 0;
355 Track::bitrate() const
357 KFileMetaInfoItem item = d->metaInfo.item( Meta::Field::xesamPrettyToFullFieldName( Meta::Field::BITRATE ) );
358 if( item.isValid() )
359 return item.value().toInt();
360 else
361 return 0;
364 uint
365 Track::lastPlayed() const
367 return 0;
371 Track::playCount() const
373 return 0;
376 QString
377 Track::type() const
379 return "";
382 void
383 Track::beginMetaDataUpdate()
385 d->batchUpdate = true;
388 void
389 Track::endMetaDataUpdate()
391 d->metaInfo.applyChanges();
392 d->batchUpdate = false;
393 notifyObservers();
398 void
399 Track::abortMetaDataUpdate()
401 //KFileMetaInfo does not have a method to reset the items
402 d->metaInfo = KFileMetaInfo( d->url.path() );
403 d->batchUpdate = false;
406 void
407 Track::finishedPlaying( double playedFraction )
409 Q_UNUSED( playedFraction );
410 //TODO
413 bool
414 Track::inCollection() const
416 return false;
419 Collection*
420 Track::collection() const
422 return 0;
425 bool
426 Track::hasCapabilityInterface( Meta::Capability::Type type ) const
428 return type == Meta::Capability::Editable;
431 Meta::Capability*
432 Track::asCapabilityInterface( Meta::Capability::Type type )
434 switch( type )
436 case Meta::Capability::Editable:
437 return new EditCapabilityImpl( this );
438 break;
439 default:
440 return 0;
444 #include "File.moc"