00001
00002
00003
00004
00005
00006
00007
00008
00010
00011 #include "upnpcdstv.h"
00012 #include "httprequest.h"
00013 #include <qfileinfo.h>
00014 #include <qregexp.h>
00015 #include <qurl.h>
00016 #include <limits.h>
00017 #include "util.h"
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 UPnpCDSRootInfo UPnpCDSTv::g_RootNodes[] =
00038 {
00039 { "All Recordings",
00040 "*",
00041 "SELECT 0 as key, "
00042 "CONCAT( title, ': ', subtitle) as name, "
00043 "1 as children "
00044 "FROM recorded "
00045 "%1 "
00046 "ORDER BY starttime DESC",
00047 "" },
00048
00049 { "By Title",
00050 "title",
00051 "SELECT title as id, "
00052 "title as name, "
00053 "count( title ) as children "
00054 "FROM recorded "
00055 "%1 "
00056 "GROUP BY title "
00057 "ORDER BY title",
00058 "WHERE title=:KEY" },
00059
00060 { "By Genre",
00061 "category",
00062 "SELECT category as id, "
00063 "category as name, "
00064 "count( category ) as children "
00065 "FROM recorded "
00066 "%1 "
00067 "GROUP BY category "
00068 "ORDER BY category",
00069 "WHERE category=:KEY" },
00070
00071 { "By Date",
00072 "DATE_FORMAT(starttime, '%Y-%m-%d')",
00073 "SELECT DATE_FORMAT(starttime, '%Y-%m-%d') as id, "
00074 "DATE_FORMAT(starttime, '%Y-%m-%d %W') as name, "
00075 "count( DATE_FORMAT(starttime, '%Y-%m-%d %W') ) as children "
00076 "FROM recorded "
00077 "%1 "
00078 "GROUP BY name "
00079 "ORDER BY starttime DESC",
00080 "WHERE DATE_FORMAT(starttime, '%Y-%m-%d') =:KEY" },
00081
00082 { "By Channel",
00083 "chanid",
00084 "SELECT channel.chanid as id, "
00085 "CONCAT(channel.channum, ' ', channel.callsign) as name, "
00086 "count( channum ) as children "
00087 "FROM channel "
00088 "INNER JOIN recorded ON channel.chanid = recorded.chanid "
00089 "%1 "
00090 "GROUP BY name "
00091 "ORDER BY channel.chanid",
00092 "WHERE channel.chanid=:KEY" },
00093
00094
00095 { "By Group",
00096 "recgroup",
00097 "SELECT recgroup as id, "
00098 "recgroup as name, count( recgroup ) as children "
00099 "FROM recorded "
00100 "%1 "
00101 "GROUP BY recgroup "
00102 "ORDER BY recgroup",
00103 "WHERE recgroup=:KEY" }
00104 };
00105
00106 int UPnpCDSTv::g_nRootCount = sizeof( g_RootNodes ) / sizeof( UPnpCDSRootInfo );
00107
00109
00111
00112 UPnpCDSRootInfo *UPnpCDSTv::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 UPnpCDSTv::GetRootCount()
00125 {
00126 return g_nRootCount;
00127 }
00128
00130
00132
00133 QString UPnpCDSTv::GetTableName( QString )
00134 {
00135 return "recorded";
00136 }
00137
00139
00141
00142 QString UPnpCDSTv::GetItemListSQL( QString )
00143 {
00144 return "SELECT chanid, starttime, endtime, title, " \
00145 "subtitle, description, category, " \
00146 "hostname, recgroup, filesize, " \
00147 "basename, progstart, progend " \
00148 "FROM recorded ";
00149 }
00150
00152
00154
00155 void UPnpCDSTv::BuildItemQuery( MSqlQuery &query, const QStringMap &mapParams )
00156 {
00157 int nChanId = mapParams[ "ChanId" ].toInt();
00158 QString sStartTime = mapParams[ "StartTime" ];
00159
00160 QString sSQL = QString( "%1 WHERE chanid=:CHANID and starttime=:STARTTIME " )
00161 .arg( GetItemListSQL() );
00162
00163 query.prepare( sSQL );
00164
00165 query.bindValue(":CHANID" , (int)nChanId );
00166 query.bindValue(":STARTTIME", sStartTime );
00167 }
00168
00170
00172
00173 bool UPnpCDSTv::IsBrowseRequestForUs( UPnpCDSRequest *pRequest )
00174 {
00175
00176
00177
00178
00179
00180
00181 if (( pRequest->m_sObjectId == "13") &&
00182 ( gContext->GetSetting("UPnP/WMPSource") != "1") )
00183 {
00184 pRequest->m_sObjectId = "RecTv/0";
00185
00186 VERBOSE( VB_UPNP, "UPnpCDSTv::IsBrowseRequestForUs - Yes, ObjectId == 13" );
00187 return true;
00188 }
00189
00190 VERBOSE( VB_UPNP, "UPnpCDSTv::IsBrowseRequestForUs - Not sure... Calling base class." );
00191
00192 return UPnpCDSExtension::IsBrowseRequestForUs( pRequest );
00193 }
00194
00196
00198
00199 bool UPnpCDSTv::IsSearchRequestForUs( UPnpCDSRequest *pRequest )
00200 {
00201
00202
00203
00204
00205
00206
00207
00208
00209 if ((pRequest->m_sObjectId == "") && (pRequest->m_sContainerID != ""))
00210 pRequest->m_sObjectId = pRequest->m_sContainerID;
00211
00212
00213
00214 bool bOurs = UPnpCDSExtension::IsSearchRequestForUs( pRequest );
00215
00216
00217
00218
00219
00220 if ( bOurs && ( pRequest->m_sObjectId == "0" ))
00221 {
00222 if ( gContext->GetSetting("UPnP/WMPSource") != "1")
00223 {
00224 pRequest->m_sObjectId = "RecTv/0";
00225 pRequest->m_sParentId = "8";
00226 }
00227 else
00228 bOurs = false;
00229 }
00230
00231 return bOurs;
00232 }
00233
00235
00237
00238 void UPnpCDSTv::AddItem( const QString &sObjectId,
00239 UPnpCDSExtensionResults *pResults,
00240 bool bAddRef,
00241 MSqlQuery &query )
00242 {
00243 int nChanid = query.value( 0).toInt();
00244 QDateTime dtStartTime = query.value( 1).toDateTime();
00245 QDateTime dtEndTime = query.value( 2).toDateTime();
00246 QString sTitle = QString::fromUtf8(query.value( 3).toString());
00247 QString sSubtitle = QString::fromUtf8(query.value( 4).toString());
00248 QString sDescription = QString::fromUtf8(query.value( 5).toString());
00249 QString sCategory = query.value( 6).toString();
00250 QString sHostName = query.value( 7).toString();
00251 QString sRecGroup = QString::fromUtf8(query.value( 8).toString());
00252 long long nFileSize = stringToLongLong( query.value( 9).toString() );
00253 QString sBaseName = query.value(10).toString();
00254
00255 QDateTime dtProgStart = query.value(11).toDateTime();
00256 QDateTime dtProgEnd = query.value(12).toDateTime();
00257
00258
00259
00260
00261
00262 if (!m_mapBackendIp.contains( sHostName ))
00263 m_mapBackendIp[ sHostName ] = gContext->GetSettingOnHost( "BackendServerIp", sHostName);
00264
00265 if (!m_mapBackendPort.contains( sHostName ))
00266 m_mapBackendPort[ sHostName ] = gContext->GetSettingOnHost("BackendStatusPort", sHostName);
00267
00268
00269
00270
00271
00272 QString sName = sTitle + ": " + sSubtitle;
00273
00274 QString sURIBase = QString( "http://%1:%2/Myth/" )
00275 .arg( m_mapBackendIp [ sHostName ] )
00276 .arg( m_mapBackendPort[ sHostName ] );
00277
00278 QString sURIParams = QString( "?ChanId=%1&StartTime=%2" )
00279 .arg( nChanid )
00280 .arg( dtStartTime.toString(Qt::ISODate));
00281
00282 QString sId = QString( "%1/item%2")
00283 .arg( sObjectId )
00284 .arg( sURIParams );
00285
00286 CDSObject *pItem = CDSObject::CreateVideoItem( sId,
00287 sName,
00288 sObjectId );
00289 pItem->m_bRestricted = false;
00290 pItem->m_bSearchable = true;
00291 pItem->m_sWriteStatus = "WRITABLE";
00292
00293 if ( bAddRef )
00294 {
00295 QString sRefId = QString( "%1/0/item%2")
00296 .arg( m_sExtensionId )
00297 .arg( sURIParams );
00298
00299 pItem->SetPropValue( "refID", sRefId );
00300 }
00301
00302 pItem->SetPropValue( "genre" , sCategory );
00303 pItem->SetPropValue( "longDescription", sDescription );
00304 pItem->SetPropValue( "description" , sSubtitle );
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 pItem->SetPropValue( "creator" , "[Unknown Author]" );
00321 pItem->SetPropValue( "artist" , "[Unknown Author]" );
00322 pItem->SetPropValue( "album" , "[Unknown Series]" );
00323 pItem->SetPropValue( "actor" , "[Unknown Author]" );
00324
00325 pResults->Add( pItem );
00326
00327
00328
00329
00330
00331 QFileInfo fInfo( sBaseName );
00332
00333 QString sMimeType = HTTPRequest::GetMimeType( fInfo.extension( FALSE ));
00334
00335 QString sProtocol = QString( "http-get:*:%1:DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01500000000000000000000000000000" ).arg( sMimeType );
00336 QString sURI = QString( "%1GetRecording%2").arg( sURIBase )
00337 .arg( sURIParams );
00338
00339 Resource *pRes = pItem->AddResource( sProtocol, sURI );
00340
00341 uint uiStart = dtProgStart.toTime_t();
00342 uint uiEnd = dtProgEnd.toTime_t();
00343 uint uiDur = uiEnd - uiStart;
00344
00345 QString sDur;
00346
00347 sDur.sprintf("%02d:%02d:%02d",
00348 (uiDur / 3600) % 24,
00349 (uiDur / 60) % 60,
00350 uiDur % 60);
00351
00352
00353 pRes->AddAttribute( "duration" , sDur );
00354 pRes->AddAttribute( "size" , longLongToString( nFileSize) );
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380 }
00381