The rest of the qbytearray port--Most of this is in code thats not enabled/ported...
[amarok.git] / src / mediadevice / njb / track.cpp
blob47f5913d365d685d4146343a50d9053995a63040
1 //Added by qt3to4:
2 #include <QByteArray>
3 /***************************************************************************
4 track.cpp - description
5 -------------------
6 begin : 2001-10-13
7 copyright : (C) 2001 by Shaun Jackman (sjackman@debian.org)
8 modify by : Andres Oton
9 email : andres.oton@gmail.com
10 ***************************************************************************/
12 /***************************************************************************
13 * *
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. *
18 * *
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 $";
24 #include "debug.h"
25 #include "track.h"
26 #include "njbmediadevice.h"
28 #include <QRegExp>
29 #include <q3ptrlist.h>
31 #include <klocale.h>
34 /* ------------------------------------------------------------------------ */
35 NjbTrack::NjbTrack( njb_songid_t* song)
38 njb_songid_frame_t* frame;
40 m_id = song->trid;
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 );
46 else
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 );
55 else
57 bundle->setLength( 0 );
58 error() << " Unexpected frame type:" << frame->type << endl;
61 frame = NJB_Songid_Findframe( song, FR_GENRE );
62 if( frame )
64 bundle->setGenre( AtomicString( frame->data.strval ) );
67 frame = NJB_Songid_Findframe( song, FR_ARTIST );
68 if( frame )
70 QString artist = frame->data.strval;
71 artist.replace( QRegExp( "/" ), "-" );
72 bundle->setArtist( artist );
74 else
75 bundle->setArtist( i18n("Unknown artist") );
77 frame = NJB_Songid_Findframe( song, FR_ALBUM );
78 if( frame)
80 QString album = frame->data.strval;
81 album.replace( QRegExp( "/" ), "-" );
82 bundle->setAlbum( album );
84 else
85 bundle->setAlbum( i18n("Unknown album") );
87 frame = NJB_Songid_Findframe( song, FR_TITLE);
88 if( frame )
90 QString title = frame->data.strval;
91 title.replace( QRegExp( "/"), "-");
92 bundle->setTitle( title );
94 else
95 bundle->setTitle( i18n("Unknown title") );
97 frame = NJB_Songid_Findframe( song, FR_TRACK );
98 if( frame )
100 switch ( frame->type )
102 case NJB_TYPE_UINT16:
103 bundle->setTrack( frame->data.u_int16_val );
104 break;
105 case NJB_TYPE_UINT32:
106 bundle->setTrack( frame->data.u_int32_val );
107 break;
108 case NJB_TYPE_STRING:
109 bundle->setTrack( QString(frame->data.strval).toUInt() );
110 break;
111 default:
112 bundle->setTrack( 0 );
116 QString codec;
117 frame = NJB_Songid_Findframe( song, FR_CODEC);
118 if( frame )
120 codec = QByteArray( frame->data.strval).toLower();
121 if( codec == "mp3" )
122 bundle->setFileType( MetaBundle::mp3 );
123 else if (codec == "wma" )
124 bundle->setFileType( MetaBundle::wma );
125 else
126 bundle->setFileType( MetaBundle::other ); // It's a wav...
128 else
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 );
135 QString filename;
136 if( frame )
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 );
151 if( frame )
153 switch ( frame->type )
155 case NJB_TYPE_UINT16:
156 bundle->setYear( frame->data.u_int16_val );
157 break;
158 case NJB_TYPE_UINT32:
159 bundle->setYear( frame->data.u_int32_val );
160 break;
161 case NJB_TYPE_STRING:
162 bundle->setYear( QString( frame->data.strval ).toInt() );
163 break;
164 default:
165 bundle->setYear( 0 );
168 this->setBundle( *bundle );
171 NjbTrack::~NjbTrack()
173 ItemList.setAutoDelete( true );
174 while( ItemList.count() > 0 )
176 delete ItemList.first();
180 void
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() ) );
195 njb_songid_t *
196 NjbTrack::newSongid()
198 njb_songid_t *songid = new njb_songid_t;
199 writeToSongid( songid );
200 return songid;
203 void
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" ) );
215 m_bundle = bundle;
218 void
219 NjbTrack::addItem( const NjbMediaItem *item )
221 ItemList.append( item );
224 bool
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)
239 break;
240 return it;
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)
250 break;
251 return it;
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)
261 break;
262 return it;
265 /* ------------------------------------------------------------------------ */
266 /** Transfer musical info from the njb to local structures */
268 trackValueList::readFromDevice( void )
270 int i = 0;
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 );
279 append( track );
280 NJB_Songid_Destroy( song );
282 ++i;
284 debug() << ": " << i << " jukebox tracks loaded from device." << endl;
286 return NJB_SUCCESS;