00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #include "upnpcdsmusic.h"
00012 #include "httprequest.h"
00013 #include <qfileinfo.h>
00014 #include <limits.h>
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 UPnpCDSRootInfo UPnpCDSMusic::g_RootNodes[] =
00044 {
00045 { "All Music",
00046 "*",
00047 "SELECT song_id as id, "
00048 "name, "
00049 "1 as children "
00050 "FROM music_songs song "
00051 "%1 "
00052 "ORDER BY name",
00053 "" },
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 { "By Album",
00069 "song.album_id",
00070 "SELECT a.album_id as id, "
00071 "a.album_name as name, "
00072 "count( song.album_id ) as children "
00073 "FROM music_songs song join music_albums a on a.album_id = song.album_id "
00074 "%1 "
00075 "GROUP BY a.album_id "
00076 "ORDER BY a.album_name",
00077 "WHERE song.album_id=:KEY" },
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 };
00105
00106 int UPnpCDSMusic::g_nRootCount = sizeof( g_RootNodes ) / sizeof( UPnpCDSRootInfo );
00107
00109
00111
00112 UPnpCDSRootInfo *UPnpCDSMusic::GetRootInfo( int nIdx )
00113 {
00114 if ((nIdx >=0 ) && ( nIdx < g_nRootCount ))
00115 return &(g_RootNodes[ nIdx ]);
00116
00117 return NULL;
00118 }
00119
00121
00123
00124 int UPnpCDSMusic::GetRootCount()
00125 {
00126 return g_nRootCount;
00127 }
00128
00130
00132
00133 QString UPnpCDSMusic::GetTableName( QString sColumn )
00134 {
00135 return "music_songs song";
00136 }
00137
00139
00141
00142 QString UPnpCDSMusic::GetItemListSQL( QString )
00143 {
00144 return "SELECT song.song_id as intid, artist.artist_name as artist, " \
00145 "album.album_name as album, song.name as title, " \
00146 "genre.genre, song.year, song.track as tracknum, " \
00147 "song.description, song.filename, song.length " \
00148 "FROM music_songs song " \
00149 " join music_artists artist on artist.artist_id = song.artist_id " \
00150 " join music_albums album on album.album_id = song.album_id " \
00151 " join music_genres genre on genre.genre_id = song.genre_id ";
00152 }
00153
00155
00157
00158 void UPnpCDSMusic::BuildItemQuery( MSqlQuery &query, const QStringMap &mapParams )
00159 {
00160 int nId = mapParams[ "Id" ].toInt();
00161
00162 QString sSQL = QString( "%1 WHERE song.song_id=:ID " )
00163 .arg( GetItemListSQL() );
00164
00165 query.prepare( sSQL );
00166
00167 query.bindValue(":ID" , (int)nId );
00168 }
00169
00171
00173
00174 bool UPnpCDSMusic::IsBrowseRequestForUs( UPnpCDSRequest *pRequest )
00175 {
00176
00177
00178
00179
00180
00181
00182 if (pRequest->m_sContainerID == "7")
00183 {
00184 pRequest->m_sObjectId = "Music";
00185
00186 VERBOSE( VB_UPNP, "UPnpCDSMusic::IsBrowseRequestForUs - Yes, ContainerId == 7" );
00187
00188 return true;
00189 }
00190
00191 if ((pRequest->m_sObjectId == "") && (pRequest->m_sContainerID != ""))
00192 pRequest->m_sObjectId = pRequest->m_sContainerID;
00193
00194 VERBOSE( VB_UPNP, "UPnpCDSMusic::IsBrowseRequestForUs - Not sure... Calling base class." );
00195
00196 return UPnpCDSExtension::IsBrowseRequestForUs( pRequest );
00197 }
00198
00200
00202
00203 bool UPnpCDSMusic::IsSearchRequestForUs( UPnpCDSRequest *pRequest )
00204 {
00205
00206
00207
00208
00209
00210
00211 if (pRequest->m_sContainerID == "7")
00212 {
00213 pRequest->m_sObjectId = "Music/1";
00214 pRequest->m_sSearchCriteria = "object.container.album.musicAlbum";
00215 pRequest->m_sSearchList.append( pRequest->m_sSearchCriteria );
00216
00217 VERBOSE( VB_UPNP, "UPnpCDSMusic::IsSearchRequestForUs... Yes." );
00218
00219 return true;
00220 }
00221
00222 if (pRequest->m_sContainerID == "4")
00223 {
00224 pRequest->m_sObjectId = "Music";
00225 pRequest->m_sSearchCriteria = "object.item.audioItem.musicTrack";
00226 pRequest->m_sSearchList.append( pRequest->m_sSearchCriteria );
00227
00228 VERBOSE( VB_UPNP, "UPnpCDSMusic::IsSearchRequestForUs... Yes." );
00229
00230 return true;
00231 }
00232
00233 if ((pRequest->m_sObjectId == "") && (pRequest->m_sContainerID != ""))
00234 pRequest->m_sObjectId = pRequest->m_sContainerID;
00235
00236 VERBOSE( VB_UPNP, "UPnpCDSMusic::IsSearchRequestForUs... Don't know, calling base class." );
00237
00238 return UPnpCDSExtension::IsSearchRequestForUs( pRequest );
00239 }
00240
00242
00244
00245 void UPnpCDSMusic::AddItem( const QString &sObjectId,
00246 UPnpCDSExtensionResults *pResults,
00247 bool bAddRef,
00248 MSqlQuery &query )
00249 {
00250 QString sName;
00251
00252 int nId = query.value( 0).toInt();
00253 QString sArtist = QString::fromUtf8(query.value( 1).toString());
00254 QString sAlbum = QString::fromUtf8(query.value( 2).toString());
00255 QString sTitle = QString::fromUtf8(query.value( 3).toString());
00256 QString sGenre = QString::fromUtf8(query.value( 4).toString());
00257
00258 int nTrackNum = query.value( 6).toInt();
00259 QString sDescription = QString::fromUtf8(query.value( 7).toString());
00260 QString sFileName = QString::fromUtf8(query.value( 8).toString());
00261 uint nLength = query.value( 9).toInt();
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 sName = sTitle;
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 QString sServerIp = gContext->GetSetting( "BackendServerIp" );
00289 QString sPort = gContext->GetSetting( "BackendStatusPort" );
00290
00291
00292
00293
00294
00295 QString sURIBase = QString( "http://%1:%2/Myth/" )
00296 .arg( sServerIp )
00297 .arg( sPort );
00298
00299 QString sURIParams = QString( "?Id=%1" )
00300 .arg( nId );
00301
00302
00303 QString sId = QString( "%1/item%2")
00304 .arg( sObjectId )
00305 .arg( sURIParams );
00306
00307 CDSObject *pItem = CDSObject::CreateMusicTrack( sId,
00308 sName,
00309 sObjectId );
00310 pItem->m_bRestricted = true;
00311 pItem->m_bSearchable = true;
00312 pItem->m_sWriteStatus = "NOT_WRITABLE";
00313
00314 if ( bAddRef )
00315 {
00316 QString sRefId = QString( "%1/0/item%2")
00317 .arg( m_sExtensionId )
00318 .arg( sURIParams );
00319
00320 pItem->SetPropValue( "refID", sRefId );
00321 }
00322
00323 pItem->SetPropValue( "genre" , sGenre );
00324 pItem->SetPropValue( "description" , sTitle );
00325 pItem->SetPropValue( "longDescription" , sDescription);
00326
00327 pItem->SetPropValue( "artist" , sArtist );
00328 pItem->SetPropValue( "album" , sAlbum );
00329 pItem->SetPropValue( "originalTrackNumber" , QString::number( nTrackNum ));
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 pResults->Add( pItem );
00348
00349
00350
00351
00352
00353 QFileInfo fInfo( sFileName );
00354
00355 QString sMimeType = HTTPRequest::GetMimeType( fInfo.extension( FALSE ));
00356 QString sProtocol = QString( "http-get:*:%1:DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000" ).arg( sMimeType );
00357 QString sURI = QString( "%1GetMusic%2").arg( sURIBase )
00358 .arg( sURIParams );
00359
00360 Resource *pRes = pItem->AddResource( sProtocol, sURI );
00361
00362 nLength /= 1000;
00363
00364 QString sDur;
00365
00366 sDur.sprintf("%02d:%02d:%02d",
00367 (nLength / 3600) % 24,
00368 (nLength / 60) % 60,
00369 nLength % 60);
00370
00371 pRes->AddAttribute( "duration" , sDur );
00372 }
00373