3 /***************************************************************************
4 track.cpp - description
7 copyright : (C) 2001 by Shaun Jackman (sjackman@debian.org)
8 modify by : Andres Oton
9 email : andres.oton@gmail.com
10 ***************************************************************************/
12 /***************************************************************************
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 ***************************************************************************/
21 static const char* rcsid
__attribute__((unused
)) =
22 "$Id: track.cpp,v 1.10.2.8 2005/07/05 00:25:09 acejones Exp $";
26 #include "njbmediadevice.h"
29 #include <q3ptrlist.h>
34 /* ------------------------------------------------------------------------ */
35 NjbTrack::NjbTrack( njb_songid_t
* song
)
38 njb_songid_frame_t
* frame
;
42 MetaBundle
*bundle
= new MetaBundle();
43 frame
= NJB_Songid_Findframe( song
, FR_SIZE
);
44 if ( frame
->type
== NJB_TYPE_UINT32
)
45 bundle
->setFilesize( frame
->data
.u_int32_val
);
48 bundle
->setFilesize( 0 );
49 error() << " Unexpected frame type:" << frame
->type
<< endl
;
52 frame
= NJB_Songid_Findframe( song
, FR_LENGTH
);
53 if ( frame
->type
== NJB_TYPE_UINT16
)
54 bundle
->setLength( frame
->data
.u_int16_val
);
57 bundle
->setLength( 0 );
58 error() << " Unexpected frame type:" << frame
->type
<< endl
;
61 frame
= NJB_Songid_Findframe( song
, FR_GENRE
);
64 bundle
->setGenre( AtomicString( frame
->data
.strval
) );
67 frame
= NJB_Songid_Findframe( song
, FR_ARTIST
);
70 QString artist
= frame
->data
.strval
;
71 artist
.replace( QRegExp( "/" ), "-" );
72 bundle
->setArtist( artist
);
75 bundle
->setArtist( i18n("Unknown artist") );
77 frame
= NJB_Songid_Findframe( song
, FR_ALBUM
);
80 QString album
= frame
->data
.strval
;
81 album
.replace( QRegExp( "/" ), "-" );
82 bundle
->setAlbum( album
);
85 bundle
->setAlbum( i18n("Unknown album") );
87 frame
= NJB_Songid_Findframe( song
, FR_TITLE
);
90 QString title
= frame
->data
.strval
;
91 title
.replace( QRegExp( "/"), "-");
92 bundle
->setTitle( title
);
95 bundle
->setTitle( i18n("Unknown title") );
97 frame
= NJB_Songid_Findframe( song
, FR_TRACK
);
100 switch ( frame
->type
)
102 case NJB_TYPE_UINT16
:
103 bundle
->setTrack( frame
->data
.u_int16_val
);
105 case NJB_TYPE_UINT32
:
106 bundle
->setTrack( frame
->data
.u_int32_val
);
108 case NJB_TYPE_STRING
:
109 bundle
->setTrack( QString(frame
->data
.strval
).toUInt() );
112 bundle
->setTrack( 0 );
117 frame
= NJB_Songid_Findframe( song
, FR_CODEC
);
120 codec
= QByteArray( frame
->data
.strval
).toLower();
122 bundle
->setFileType( MetaBundle::mp3
);
123 else if (codec
== "wma" )
124 bundle
->setFileType( MetaBundle::wma
);
126 bundle
->setFileType( MetaBundle::other
); // It's a wav...
130 bundle
->setFileType( MetaBundle::mp3
); //Assumed...
131 codec
= "mp3"; // Used for the next frame as a fallback
134 frame
= NJB_Songid_Findframe( song
, FR_FNAME
);
138 //bundle->setUrl( KUrl( frame->data.strval ) );
139 filename
= frame
->data
.strval
;
142 if( filename
.isEmpty() )
144 //bundle->setUrl( bundle->artist() + " - " + bundle->title() + '.' + codec );
145 filename
= bundle
->artist() + " - " + bundle
->title() + '.' + codec
;
148 bundle
->setPath( filename
);
150 frame
= NJB_Songid_Findframe( song
, FR_YEAR
);
153 switch ( frame
->type
)
155 case NJB_TYPE_UINT16
:
156 bundle
->setYear( frame
->data
.u_int16_val
);
158 case NJB_TYPE_UINT32
:
159 bundle
->setYear( frame
->data
.u_int32_val
);
161 case NJB_TYPE_STRING
:
162 bundle
->setYear( QString( frame
->data
.strval
).toInt() );
165 bundle
->setYear( 0 );
168 this->setBundle( *bundle
);
171 NjbTrack::~NjbTrack()
173 ItemList
.setAutoDelete( true );
174 while( ItemList
.count() > 0 )
176 delete ItemList
.first();
181 NjbTrack::writeToSongid( njb_songid_t
*songid
)
183 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Filename( m_bundle
.filename().toUtf8() ) );
184 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Filesize( m_bundle
.filesize() ) );
185 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Codec( "mp3" ) ); //for now
186 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Title( m_bundle
.title().toUtf8() ) );
187 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Album( m_bundle
.album().ptr()->utf8() ) );
188 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Genre( m_bundle
.genre().ptr()->utf8() ) );
189 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Artist( m_bundle
.artist().ptr()->utf8() ) );
190 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Length( m_bundle
.length() ) );
191 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Tracknum( m_bundle
.track() ) );
192 NJB_Songid_Addframe( songid
, NJB_Songid_Frame_New_Year( m_bundle
.year() ) );
196 NjbTrack::newSongid()
198 njb_songid_t
*songid
= new njb_songid_t
;
199 writeToSongid( songid
);
204 NjbTrack::setBundle( MetaBundle
&bundle
)
206 if( bundle
.title().isEmpty() )
207 bundle
.setTitle( i18n( "Unknown title" ) );
208 if( bundle
.artist().isEmpty() )
209 bundle
.setArtist( i18n( "Unknown artist" ) );
210 if( bundle
.album().isEmpty() )
211 bundle
.setAlbum( i18n( "Unknown album" ) );
212 if( bundle
.genre().isEmpty() )
213 bundle
.setGenre( i18n( "Unknown genre" ) );
219 NjbTrack::addItem( const NjbMediaItem
*item
)
221 ItemList
.append( item
);
225 NjbTrack::removeItem( const NjbMediaItem
*item
)
227 ItemList
.remove( item
);
228 debug() << "Removed item\n";
229 return ItemList
.isEmpty();
232 /* ------------------------------------------------------------------------ */
233 trackValueList::iterator
234 trackValueList::findTrackByName( const QString
& _filename
)
236 trackValueList::iterator it
;
237 for( it
= begin(); it
!= end(); it
++)
238 if( (*it
)->bundle()->url().path() == _filename
)
243 /* ------------------------------------------------------------------------ */
244 trackValueList::iterator
245 trackValueList::findTrackById( unsigned _id
)
247 trackValueList::iterator it
;
248 for( it
= begin(); it
!= end(); it
++)
249 if( (*it
)->id() == _id
)
254 /* ------------------------------------------------------------------------ */
255 trackValueList::const_iterator
256 trackValueList::findTrackById( unsigned _id
) const
258 trackValueList::const_iterator it
;
259 for( it
= begin(); it
!= end(); it
++)
260 if( (*it
)->id() == _id
)
265 /* ------------------------------------------------------------------------ */
266 /** Transfer musical info from the njb to local structures */
268 trackValueList::readFromDevice( void )
272 // Don't get extended metadatas
274 NJB_Get_Extended_Tags(NjbMediaDevice::theNjb(), 0);
275 NJB_Reset_Get_Track_Tag( NjbMediaDevice::theNjb());
276 while( njb_songid_t
* song
= NJB_Get_Track_Tag( NjbMediaDevice::theNjb()))
278 NjbTrack
*track
= new NjbTrack( song
);
280 NJB_Songid_Destroy( song
);
284 debug() << ": " << i
<< " jukebox tracks loaded from device." << endl
;