MythTV  0.28pre
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
video.cpp
Go to the documentation of this file.
1 // Program Name: video.cpp
3 // Created : Apr. 21, 2011
4 //
5 // Copyright (c) 2011 Robert McNamara <rmcnamara@mythtv.org>
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //
25 
26 #include <QList>
27 #include <QFile>
28 #include <QMutex>
29 
30 #include <math.h>
31 
32 #include "video.h"
33 
34 #include "videometadata.h"
35 #include "metadatafactory.h"
36 #include "bluraymetadata.h"
37 
38 #include "compat.h"
39 #include "mythversion.h"
40 #include "mythcorecontext.h"
41 #include "storagegroup.h"
42 #include "remotefile.h"
43 #include "globals.h"
44 #include "mythdate.h"
45 #include "serviceUtil.h"
46 #include "mythmiscutil.h"
47 
49 //
51 
53  const QString &Sort,
54  bool bDescending,
55  int nStartIndex,
56  int nCount )
57 {
59 
60  QString sql = "";
61  if (!Folder.isEmpty())
62  sql.append(" WHERE filename LIKE '" + Folder + "%'");
63 
64  sql.append(" ORDER BY ");
65  QString sort = QString(Sort.toLower());
66  if (sort == "added")
67  sql.append("insertdate");
68  else if (sort == "released")
69  sql.append("releasedate");
70  else
71  sql.append("intid");
72 
73  if (bDescending)
74  sql += " DESC";
76 
77  std::vector<VideoMetadataListManager::VideoMetadataPtr> videos(videolist.begin(), videolist.end());
78 
79  // ----------------------------------------------------------------------
80  // Build Response
81  // ----------------------------------------------------------------------
82 
83  DTC::VideoMetadataInfoList *pVideoMetadataInfos = new DTC::VideoMetadataInfoList();
84 
85  nStartIndex = min( nStartIndex, (int)videos.size() );
86  nCount = (nCount > 0) ? min( nCount, (int)videos.size() ) : videos.size();
87  int nEndIndex = min((nStartIndex + nCount), (int)videos.size() );
88 
89  for( int n = nStartIndex; n < nEndIndex; n++ )
90  {
91  DTC::VideoMetadataInfo *pVideoMetadataInfo = pVideoMetadataInfos->AddNewVideoMetadataInfo();
92 
94 
95  if (metadata)
96  FillVideoMetadataInfo ( pVideoMetadataInfo, metadata, true );
97  }
98 
99  int curPage = 0, totalPages = 0;
100  if (nCount == 0)
101  totalPages = 1;
102  else
103  totalPages = (int)ceil((float)videos.size() / nCount);
104 
105  if (totalPages == 1)
106  curPage = 1;
107  else
108  {
109  curPage = (int)ceil((float)nStartIndex / nCount) + 1;
110  }
111 
112  pVideoMetadataInfos->setStartIndex ( nStartIndex );
113  pVideoMetadataInfos->setCount ( nCount );
114  pVideoMetadataInfos->setCurrentPage ( curPage );
115  pVideoMetadataInfos->setTotalPages ( totalPages );
116  pVideoMetadataInfos->setTotalAvailable( videos.size() );
117  pVideoMetadataInfos->setAsOf ( MythDate::current() );
118  pVideoMetadataInfos->setVersion ( MYTH_BINARY_VERSION );
119  pVideoMetadataInfos->setProtoVer ( MYTH_PROTO_VERSION );
120 
121  return pVideoMetadataInfos;
122 }
123 
125 //
127 
129 {
132 
133  if ( !metadata )
134  throw( QString( "No metadata found for selected ID!." ));
135 
136  DTC::VideoMetadataInfo *pVideoMetadataInfo = new DTC::VideoMetadataInfo();
137 
138  FillVideoMetadataInfo ( pVideoMetadataInfo, metadata, true );
139 
140  return pVideoMetadataInfo;
141 }
142 
144 //
146 
148 {
152  mlm->setList(videolist);
153  VideoMetadataListManager::VideoMetadataPtr metadata = mlm->byFilename(FileName);
154 
155  if ( !metadata )
156  throw( QString( "No metadata found for selected filename!." ));
157 
158  DTC::VideoMetadataInfo *pVideoMetadataInfo = new DTC::VideoMetadataInfo();
159 
160  FillVideoMetadataInfo ( pVideoMetadataInfo, metadata, true );
161 
162  delete mlm;
163 
164  return pVideoMetadataInfo;
165 }
166 
168 //
170 
172  const QString &Subtitle,
173  const QString &Inetref,
174  int Season,
175  int Episode,
176  const QString &GrabberType,
177  bool AllowGeneric )
178 {
179  DTC::VideoLookupList *pVideoLookups = new DTC::VideoLookupList();
180 
181  MetadataLookupList list;
182 
183  MetadataFactory *factory = new MetadataFactory(NULL);
184 
185  if (factory)
186  list = factory->SynchronousLookup(Title, Subtitle,
187  Inetref, Season, Episode,
188  GrabberType, AllowGeneric);
189 
190  if ( !list.size() )
191  return pVideoLookups;
192 
193  //MetadataLookupList is a reference counted list.
194  //it will delete all its content at its end of life
195  for( int n = 0; n < list.size(); n++ )
196  {
197  DTC::VideoLookup *pVideoLookup = pVideoLookups->AddNewVideoLookup();
198 
199  MetadataLookup *lookup = list[n];
200 
201  if (lookup)
202  {
203  pVideoLookup->setTitle(lookup->GetTitle());
204  pVideoLookup->setSubTitle(lookup->GetSubtitle());
205  pVideoLookup->setSeason(lookup->GetSeason());
206  pVideoLookup->setEpisode(lookup->GetEpisode());
207  pVideoLookup->setYear(lookup->GetYear());
208  pVideoLookup->setTagline(lookup->GetTagline());
209  pVideoLookup->setDescription(lookup->GetDescription());
210  pVideoLookup->setCertification(lookup->GetCertification());
211  pVideoLookup->setInetref(lookup->GetInetref());
212  pVideoLookup->setCollectionref(lookup->GetCollectionref());
213  pVideoLookup->setHomePage(lookup->GetHomepage());
214  pVideoLookup->setReleaseDate(
215  QDateTime(lookup->GetReleaseDate(),
216  QTime(0,0),Qt::LocalTime).toUTC());
217  pVideoLookup->setUserRating(lookup->GetUserRating());
218  pVideoLookup->setLength(lookup->GetRuntime());
219  pVideoLookup->setLanguage(lookup->GetLanguage());
220  pVideoLookup->setCountries(lookup->GetCountries());
221  pVideoLookup->setPopularity(lookup->GetPopularity());
222  pVideoLookup->setBudget(lookup->GetBudget());
223  pVideoLookup->setRevenue(lookup->GetRevenue());
224  pVideoLookup->setIMDB(lookup->GetIMDB());
225  pVideoLookup->setTMSRef(lookup->GetTMSref());
226 
227  ArtworkList coverartlist = lookup->GetArtwork(kArtworkCoverart);
228  ArtworkList::iterator c;
229  for (c = coverartlist.begin(); c != coverartlist.end(); ++c)
230  {
231  DTC::ArtworkItem *art = pVideoLookup->AddNewArtwork();
232  art->setType("coverart");
233  art->setUrl((*c).url);
234  art->setThumbnail((*c).thumbnail);
235  art->setWidth((*c).width);
236  art->setHeight((*c).height);
237  }
238  ArtworkList fanartlist = lookup->GetArtwork(kArtworkFanart);
239  ArtworkList::iterator f;
240  for (f = fanartlist.begin(); f != fanartlist.end(); ++f)
241  {
242  DTC::ArtworkItem *art = pVideoLookup->AddNewArtwork();
243  art->setType("fanart");
244  art->setUrl((*f).url);
245  art->setThumbnail((*f).thumbnail);
246  art->setWidth((*f).width);
247  art->setHeight((*f).height);
248  }
249  ArtworkList bannerlist = lookup->GetArtwork(kArtworkBanner);
250  ArtworkList::iterator b;
251  for (b = bannerlist.begin(); b != bannerlist.end(); ++b)
252  {
253  DTC::ArtworkItem *art = pVideoLookup->AddNewArtwork();
254  art->setType("banner");
255  art->setUrl((*b).url);
256  art->setThumbnail((*b).thumbnail);
257  art->setWidth((*b).width);
258  art->setHeight((*b).height);
259  }
260  ArtworkList screenshotlist = lookup->GetArtwork(kArtworkScreenshot);
261  ArtworkList::iterator s;
262  for (s = screenshotlist.begin(); s != screenshotlist.end(); ++s)
263  {
264  DTC::ArtworkItem *art = pVideoLookup->AddNewArtwork();
265  art->setType("screenshot");
266  art->setUrl((*s).url);
267  art->setThumbnail((*s).thumbnail);
268  art->setWidth((*s).width);
269  art->setHeight((*s).height);
270  }
271  }
272  }
273 
274  pVideoLookups->setCount ( list.count() );
275  pVideoLookups->setAsOf ( MythDate::current() );
276  pVideoLookups->setVersion ( MYTH_BINARY_VERSION );
277  pVideoLookups->setProtoVer ( MYTH_PROTO_VERSION );
278 
279  delete factory;
280 
281  return pVideoLookups;
282 }
283 
285 //
287 
289 {
290  bool bResult = false;
291 
295  mlm->setList(videolist);
297 
298  if (metadata)
299  bResult = metadata->DeleteFromDatabase();
300 
301  delete mlm;
302 
303  return bResult;
304 }
305 
307 //
309 
310 bool Video::AddVideo( const QString &sFileName,
311  const QString &sHostName )
312 {
313  if ( sHostName.isEmpty() )
314  throw( QString( "Host not provided! Local storage is deprecated and "
315  "is not supported by the API." ));
316 
317  if ( sFileName.isEmpty() ||
318  (sFileName.contains("/../")) ||
319  (sFileName.startsWith("../")) )
320  {
321  throw( QString( "Filename not provided, or fails sanity checks!" ));
322  }
323 
324  StorageGroup sgroup("Videos", sHostName);
325 
326  QString fullname = sgroup.FindFile(sFileName);
327 
328  if ( !QFile::exists(fullname) )
329  throw( QString( "Provided filename does not exist!" ));
330 
331  QString hash = FileHash(fullname);
332 
333  if (hash == "NULL")
334  {
335  LOG(VB_GENERAL, LOG_ERR, "Video Hash Failed. Unless this is a DVD or "
336  "Blu-ray, something has probably gone wrong.");
337  hash = "";
338  }
339 
340  VideoMetadata newFile(sFileName, hash,
346  VideoMetadata::FilenameToMeta(sFileName, 1),
347  VideoMetadata::FilenameToMeta(sFileName, 4),
348  QString(), VIDEO_YEAR_DEFAULT,
349  QDate::fromString("0000-00-00","YYYY-MM-DD"),
350  VIDEO_INETREF_DEFAULT, 0, QString(),
352  0.0, VIDEO_RATING_DEFAULT, 0, 0,
353  VideoMetadata::FilenameToMeta(sFileName, 2).toInt(),
354  VideoMetadata::FilenameToMeta(sFileName, 3).toInt(),
355  MythDate::current().date(), 0,
357 
358  newFile.SetHost(sHostName);
359  newFile.SaveToDatabase();
360 
361  return true;
362 }
363 
365 //
367 
369  bool bWatched )
370 {
371  bool bResut = false;
374 
375  if ( metadata )
376  {
377  metadata->SetWatched(bWatched);
378  metadata->UpdateDatabase();
379  bResut = true;
380  }
381 
382  return bResut;
383 }
384 
386 //
388 
389 DTC::BlurayInfo* Video::GetBluray( const QString &sPath )
390 {
391  QString path = sPath;
392 
393  if (sPath.isEmpty())
394  path = gCoreContext->GetSetting( "BluRayMountpoint", "/media/cdrom");
395 
396  LOG(VB_GENERAL, LOG_NOTICE,
397  QString("Parsing Blu-ray at path: %1 ").arg(path));
398 
399  BlurayMetadata *bdmeta = new BlurayMetadata(path);
400 
401  if ( !bdmeta )
402  throw( QString( "Unable to open Blu-ray Metadata Parser!" ));
403 
404  if ( !bdmeta->OpenDisc() )
405  throw( QString( "Unable to open Blu-ray Disc/Path!" ));
406 
407  if ( !bdmeta->ParseDisc() )
408  throw( QString( "Unable to parse metadata from Blu-ray Disc/Path!" ));
409 
410  DTC::BlurayInfo *pBlurayInfo = new DTC::BlurayInfo();
411 
412  pBlurayInfo->setPath(path);
413  pBlurayInfo->setTitle(bdmeta->GetTitle());
414  pBlurayInfo->setAltTitle(bdmeta->GetAlternateTitle());
415  pBlurayInfo->setDiscLang(bdmeta->GetDiscLanguage());
416  pBlurayInfo->setDiscNum(bdmeta->GetCurrentDiscNumber());
417  pBlurayInfo->setTotalDiscNum(bdmeta->GetTotalDiscNumber());
418  pBlurayInfo->setTitleCount(bdmeta->GetTitleCount());
419  pBlurayInfo->setThumbCount(bdmeta->GetThumbnailCount());
420  pBlurayInfo->setTopMenuSupported(bdmeta->GetTopMenuSupported());
421  pBlurayInfo->setFirstPlaySupported(bdmeta->GetFirstPlaySupported());
422  pBlurayInfo->setNumHDMVTitles((uint)bdmeta->GetNumHDMVTitles());
423  pBlurayInfo->setNumBDJTitles((uint)bdmeta->GetNumBDJTitles());
424  pBlurayInfo->setNumUnsupportedTitles((uint)bdmeta->GetNumUnsupportedTitles());
425  pBlurayInfo->setAACSDetected(bdmeta->GetAACSDetected());
426  pBlurayInfo->setLibAACSDetected(bdmeta->GetLibAACSDetected());
427  pBlurayInfo->setAACSHandled(bdmeta->GetAACSHandled());
428  pBlurayInfo->setBDPlusDetected(bdmeta->GetBDPlusDetected());
429  pBlurayInfo->setLibBDPlusDetected(bdmeta->GetLibBDPlusDetected());
430  pBlurayInfo->setBDPlusHandled(bdmeta->GetBDPlusHandled());
431 
432  QStringList thumbs = bdmeta->GetThumbnails();
433  if (thumbs.size())
434  pBlurayInfo->setThumbPath(thumbs.at(0));
435 
436  delete bdmeta;
437 
438  return pBlurayInfo;
439 }
440 
442 //
QString FileHash(QString filename)
QStringList GetCountries() const
uint GetRevenue() const
QString GetSubtitle() const
uint GetTotalDiscNumber(void)
const QString VIDEO_SCREENSHOT_DEFAULT
Definition: globals.cpp:27
uint32_t GetNumHDMVTitles(void)
static void loadAllFromDatabase(metadata_list &items, const QString &sql="")
QString GetCollectionref() const
QString GetAlternateTitle(void)
DTC::VideoMetadataInfo * GetVideo(int Id)
Definition: video.cpp:128
DTC::VideoLookupList * LookupVideo(const QString &Title, const QString &Subtitle, const QString &Inetref, int Season, int Episode, const QString &GrabberType, bool AllowGeneric)
Definition: video.cpp:171
unsigned int uint
Definition: compat.h:135
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
ArtworkList GetArtwork(VideoArtworkType type) const
bool GetTopMenuSupported(void)
VideoLookup * AddNewVideoLookup()
QString GetTitle() const
QString GetCertification() const
bool ParseDisc(void)
uint GetEpisode() const
unsigned char b
Definition: ParseText.cpp:339
VideoMetadataPtr byID(unsigned int db_id) const
void setList(metadata_list &list)
const QString VIDEO_PLOT_DEFAULT
Definition: globals.cpp:32
bool AddVideo(const QString &FileName, const QString &HostName)
Definition: video.cpp:310
float GetUserRating() const
const QString VIDEO_TRAILER_DEFAULT
Definition: globals.cpp:26
bool GetAACSHandled(void)
uint GetSeason() const
bool GetBDPlusDetected(void)
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QString GetSetting(const QString &key, const QString &defaultval="")
uint32_t GetNumBDJTitles(void)
DTC::VideoMetadataInfoList * GetVideoList(const QString &Folder, const QString &Sort, bool Descending, int StartIndex, int Count)
Definition: video.cpp:52
MetadataLookupList SynchronousLookup(QString title, QString subtitle, QString inetref, int season, int episode, QString grabber, bool allowgeneric=false)
bool OpenDisc(void)
QString GetDiscLanguage(void)
static QString FilenameToMeta(const QString &file_name, int position)
QString GetTMSref() const
QString GetIMDB() const
bool GetFirstPlaySupported(void)
bool GetAACSDetected(void)
QString GetTitle(void)
int c
Definition: minilzo.cpp:2402
return s
Definition: minilzo.cpp:2409
QString GetLanguage() const
QStringList GetThumbnails(void)
QList< ArtworkInfo > ArtworkList
void SetHost(const QString &host)
GrabberType
std::list< VideoMetadataPtr > metadata_list
ArtworkItem * AddNewArtwork()
DTC::BlurayInfo * GetBluray(const QString &Path)
Definition: video.cpp:389
void FillVideoMetadataInfo(DTC::VideoMetadataInfo *pVideoMetadataInfo, VideoMetadataListManager::VideoMetadataPtr pMetadata, bool bDetails)
const QString VIDEO_INETREF_DEFAULT
Definition: globals.cpp:24
QString FindFile(const QString &filename)
DTC::VideoMetadataInfo * GetVideoByFileName(const QString &FileName)
Definition: video.cpp:147
VideoMetadataPtr byFilename(const QString &file_name) const
QString GetDescription() const
uint32_t GetNumUnsupportedTitles(void)
bool RemoveVideoFromDB(int Id)
Definition: video.cpp:288
uint GetYear() const
const QString VIDEO_COVERFILE_DEFAULT
Definition: globals.cpp:25
uint GetTitleCount(void)
uint GetBudget() const
bool UpdateVideoWatchedStatus(int Id, bool Watched)
Definition: video.cpp:368
uint GetCurrentDiscNumber(void)
const QString VIDEO_RATING_DEFAULT
Definition: globals.cpp:30
QString GetInetref() const
typedef int
Definition: lzoconf.h:279
uint GetThumbnailCount(void)
QString GetTagline() const
const QString VIDEO_FANART_DEFAULT
Definition: globals.cpp:29
bool GetLibBDPlusDetected(void)
QDate GetReleaseDate() const
const QString VIDEO_DIRECTOR_DEFAULT
Definition: globals.cpp:23
static VideoMetadataPtr loadOneFromDatabase(uint id)
QString GetHomepage() const
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:34
uint GetPopularity() const
bool GetLibAACSDetected(void)
uint GetRuntime() const
bool GetBDPlusHandled(void)
const QString VIDEO_BANNER_DEFAULT
Definition: globals.cpp:28