d9b97f6dd0b4a08fb1e2b7f494c2bc2df6120bec
[vuplus_xbmc] / xbmc / video / VideoInfoTag.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "VideoInfoTag.h"
22 #include "utils/XMLUtils.h"
23 #include "guilib/LocalizeStrings.h"
24 #include "settings/AdvancedSettings.h"
25 #include "utils/log.h"
26 #include "utils/StringUtils.h"
27 #include "utils/Variant.h"
28 #include "TextureCache.h"
29 #include "filesystem/File.h"
30
31 #include <sstream>
32
33 using namespace std;
34
35 void CVideoInfoTag::Reset()
36 {
37   m_director.clear();
38   m_writingCredits.clear();
39   m_genre.clear();
40   m_country.clear();
41   m_strTagLine.clear();
42   m_strPlotOutline.clear();
43   m_strPlot.clear();
44   m_strPictureURL.Clear();
45   m_strTitle.clear();
46   m_strShowTitle.clear();
47   m_strOriginalTitle.clear();
48   m_strSortTitle.clear();
49   m_strVotes.clear();
50   m_cast.clear();
51   m_strSet.clear();
52   m_iSetId = -1;
53   m_tags.clear();
54   m_strFile.clear();
55   m_strPath.clear();
56   m_strIMDBNumber.clear();
57   m_strMPAARating.clear();
58   m_strFileNameAndPath.clear();
59   m_premiered.Reset();
60   m_strStatus.clear();
61   m_strProductionCode.clear();
62   m_firstAired.Reset();
63   m_studio.clear();
64   m_strAlbum.clear();
65   m_artist.clear();
66   m_strTrailer.clear();
67   m_iTop250 = 0;
68   m_iYear = 0;
69   m_iSeason = -1;
70   m_iEpisode = -1;
71   m_strUniqueId.clear();
72   m_iSpecialSortSeason = -1;
73   m_iSpecialSortEpisode = -1;
74   m_fRating = 0.0f;
75   m_iDbId = -1;
76   m_iFileId = -1;
77   m_iBookmarkId = -1;
78   m_iTrack = -1;
79   m_fanart.m_xml.clear();
80   m_duration = 0;
81   m_lastPlayed.Reset();
82   m_showLink.clear();
83   m_streamDetails.Reset();
84   m_playCount = 0;
85   m_fEpBookmark = 0;
86   m_basePath.clear();
87   m_parentPathID = -1;
88   m_resumePoint.Reset();
89   m_resumePoint.type = CBookmark::RESUME;
90   m_iIdShow = -1;
91   m_iIdSeason = -1;
92   m_strShowPath.clear();
93   m_dateAdded.Reset();
94   m_type.clear();
95 }
96
97 bool CVideoInfoTag::Save(TiXmlNode *node, const CStdString &tag, bool savePathInfo, const TiXmlElement *additionalNode)
98 {
99   if (!node) return false;
100
101   // we start with a <tag> tag
102   TiXmlElement movieElement(tag.c_str());
103   TiXmlNode *movie = node->InsertEndChild(movieElement);
104
105   if (!movie) return false;
106
107   XMLUtils::SetString(movie, "title", m_strTitle);
108   if (!m_strOriginalTitle.IsEmpty())
109     XMLUtils::SetString(movie, "originaltitle", m_strOriginalTitle);
110   if (!m_strShowTitle.IsEmpty())
111     XMLUtils::SetString(movie, "showtitle", m_strShowTitle);
112   if (!m_strSortTitle.IsEmpty())
113     XMLUtils::SetString(movie, "sorttitle", m_strSortTitle);
114   XMLUtils::SetFloat(movie, "rating", m_fRating);
115   XMLUtils::SetFloat(movie, "epbookmark", m_fEpBookmark);
116   XMLUtils::SetInt(movie, "year", m_iYear);
117   XMLUtils::SetInt(movie, "top250", m_iTop250);
118   if (tag == "episodedetails" || tag == "tvshow")
119   {
120     XMLUtils::SetInt(movie, "season", m_iSeason);
121     XMLUtils::SetInt(movie, "episode", m_iEpisode);
122     XMLUtils::SetString(movie, "uniqueid", m_strUniqueId);
123     XMLUtils::SetInt(movie, "displayseason",m_iSpecialSortSeason);
124     XMLUtils::SetInt(movie, "displayepisode",m_iSpecialSortEpisode);
125   }
126   if (tag == "musicvideo")
127   {
128     XMLUtils::SetInt(movie, "track", m_iTrack);
129     XMLUtils::SetString(movie, "album", m_strAlbum);
130   }
131   XMLUtils::SetString(movie, "votes", m_strVotes);
132   XMLUtils::SetString(movie, "outline", m_strPlotOutline);
133   XMLUtils::SetString(movie, "plot", m_strPlot);
134   XMLUtils::SetString(movie, "tagline", m_strTagLine);
135   XMLUtils::SetInt(movie, "runtime", GetDuration() / 60);
136   if (!m_strPictureURL.m_xml.empty())
137   {
138     CXBMCTinyXML doc;
139     doc.Parse(m_strPictureURL.m_xml);
140     const TiXmlNode* thumb = doc.FirstChild("thumb");
141     while (thumb)
142     {
143       movie->InsertEndChild(*thumb);
144       thumb = thumb->NextSibling("thumb");
145     }
146   }
147   if (m_fanart.m_xml.size())
148   {
149     CXBMCTinyXML doc;
150     doc.Parse(m_fanart.m_xml);
151     movie->InsertEndChild(*doc.RootElement());
152   }
153   XMLUtils::SetString(movie, "mpaa", m_strMPAARating);
154   XMLUtils::SetInt(movie, "playcount", m_playCount);
155   XMLUtils::SetDate(movie, "lastplayed", m_lastPlayed);
156   if (savePathInfo)
157   {
158     XMLUtils::SetString(movie, "file", m_strFile);
159     XMLUtils::SetString(movie, "path", m_strPath);
160     XMLUtils::SetString(movie, "filenameandpath", m_strFileNameAndPath);
161     XMLUtils::SetString(movie, "basepath", m_basePath);
162   }
163   if (!m_strEpisodeGuide.IsEmpty())
164   {
165     CXBMCTinyXML doc;
166     doc.Parse(m_strEpisodeGuide);
167     if (doc.RootElement())
168       movie->InsertEndChild(*doc.RootElement());
169     else
170       XMLUtils::SetString(movie, "episodeguide", m_strEpisodeGuide);
171   }
172
173   XMLUtils::SetString(movie, "id", m_strIMDBNumber);
174   XMLUtils::SetStringArray(movie, "genre", m_genre);
175   XMLUtils::SetStringArray(movie, "country", m_country);
176   XMLUtils::SetString(movie, "set", m_strSet);
177   XMLUtils::SetStringArray(movie, "tag", m_tags);
178   XMLUtils::SetStringArray(movie, "credits", m_writingCredits);
179   XMLUtils::SetStringArray(movie, "director", m_director);
180   XMLUtils::SetDate(movie, "premiered", m_premiered);
181   XMLUtils::SetString(movie, "status", m_strStatus);
182   XMLUtils::SetString(movie, "code", m_strProductionCode);
183   XMLUtils::SetDate(movie, "aired", m_firstAired);
184   XMLUtils::SetStringArray(movie, "studio", m_studio);
185   XMLUtils::SetString(movie, "trailer", m_strTrailer);
186
187   if (m_streamDetails.HasItems())
188   {
189     // it goes fileinfo/streamdetails/[video|audio|subtitle]
190     TiXmlElement fileinfo("fileinfo");
191     TiXmlElement streamdetails("streamdetails");
192     for (int iStream=1; iStream<=m_streamDetails.GetVideoStreamCount(); iStream++)
193     {
194       TiXmlElement stream("video");
195       XMLUtils::SetString(&stream, "codec", m_streamDetails.GetVideoCodec(iStream));
196       XMLUtils::SetFloat(&stream, "aspect", m_streamDetails.GetVideoAspect(iStream));
197       XMLUtils::SetInt(&stream, "width", m_streamDetails.GetVideoWidth(iStream));
198       XMLUtils::SetInt(&stream, "height", m_streamDetails.GetVideoHeight(iStream));
199       XMLUtils::SetInt(&stream, "durationinseconds", m_streamDetails.GetVideoDuration(iStream));
200       streamdetails.InsertEndChild(stream);
201     }
202     for (int iStream=1; iStream<=m_streamDetails.GetAudioStreamCount(); iStream++)
203     {
204       TiXmlElement stream("audio");
205       XMLUtils::SetString(&stream, "codec", m_streamDetails.GetAudioCodec(iStream));
206       XMLUtils::SetString(&stream, "language", m_streamDetails.GetAudioLanguage(iStream));
207       XMLUtils::SetInt(&stream, "channels", m_streamDetails.GetAudioChannels(iStream));
208       streamdetails.InsertEndChild(stream);
209     }
210     for (int iStream=1; iStream<=m_streamDetails.GetSubtitleStreamCount(); iStream++)
211     {
212       TiXmlElement stream("subtitle");
213       XMLUtils::SetString(&stream, "language", m_streamDetails.GetSubtitleLanguage(iStream));
214       streamdetails.InsertEndChild(stream);
215     }
216     fileinfo.InsertEndChild(streamdetails);
217     movie->InsertEndChild(fileinfo);
218   }  /* if has stream details */
219
220   // cast
221   for (iCast it = m_cast.begin(); it != m_cast.end(); ++it)
222   {
223     // add a <actor> tag
224     TiXmlElement cast("actor");
225     TiXmlNode *node = movie->InsertEndChild(cast);
226     XMLUtils::SetString(node, "name", it->strName);
227     XMLUtils::SetString(node, "role", it->strRole);
228     XMLUtils::SetInt(node, "order", it->order);
229     XMLUtils::SetString(node, "thumb", it->thumbUrl.GetFirstThumb().m_url);
230   }
231   XMLUtils::SetStringArray(movie, "artist", m_artist);
232   XMLUtils::SetStringArray(movie, "showlink", m_showLink);
233  
234   TiXmlElement resume("resume");
235   XMLUtils::SetFloat(&resume, "position", (float)m_resumePoint.timeInSeconds);
236   XMLUtils::SetFloat(&resume, "total", (float)m_resumePoint.totalTimeInSeconds);
237   movie->InsertEndChild(resume);
238
239   XMLUtils::SetString(movie, "dateadded", m_dateAdded.GetAsDBDateTime());
240
241   if (additionalNode)
242     movie->InsertEndChild(*additionalNode);
243
244   return true;
245 }
246
247 bool CVideoInfoTag::Load(const TiXmlElement *element, bool append, bool prioritise)
248 {
249   if (!element)
250     return false;
251   if (!append)
252     Reset();
253   ParseNative(element, prioritise);
254   return true;
255 }
256
257 void CVideoInfoTag::Archive(CArchive& ar)
258 {
259   if (ar.IsStoring())
260   {
261     ar << m_director;
262     ar << m_writingCredits;
263     ar << m_genre;
264     ar << m_country;
265     ar << m_strTagLine;
266     ar << m_strPlotOutline;
267     ar << m_strPlot;
268     ar << m_strPictureURL.m_spoof;
269     ar << m_strPictureURL.m_xml;
270     ar << m_fanart.m_xml;
271     ar << m_strTitle;
272     ar << m_strSortTitle;
273     ar << m_strVotes;
274     ar << m_studio;
275     ar << m_strTrailer;
276     ar << (int)m_cast.size();
277     for (unsigned int i=0;i<m_cast.size();++i)
278     {
279       ar << m_cast[i].strName;
280       ar << m_cast[i].strRole;
281       ar << m_cast[i].order;
282       ar << m_cast[i].thumb;
283       ar << m_cast[i].thumbUrl.m_xml;
284     }
285
286     ar << m_strSet;
287     ar << m_iSetId;
288     ar << m_tags;
289     ar << m_duration;
290     ar << m_strFile;
291     ar << m_strPath;
292     ar << m_strIMDBNumber;
293     ar << m_strMPAARating;
294     ar << m_strFileNameAndPath;
295     ar << m_strOriginalTitle;
296     ar << m_strEpisodeGuide;
297     ar << m_premiered;
298     ar << m_strStatus;
299     ar << m_strProductionCode;
300     ar << m_firstAired;
301     ar << m_strShowTitle;
302     ar << m_strAlbum;
303     ar << m_artist;
304     ar << m_playCount;
305     ar << m_lastPlayed;
306     ar << m_iTop250;
307     ar << m_iYear;
308     ar << m_iSeason;
309     ar << m_iEpisode;
310     ar << m_strUniqueId;
311     ar << m_fRating;
312     ar << m_iDbId;
313     ar << m_iFileId;
314     ar << m_iSpecialSortSeason;
315     ar << m_iSpecialSortEpisode;
316     ar << m_iBookmarkId;
317     ar << m_iTrack;
318     ar << dynamic_cast<IArchivable&>(m_streamDetails);
319     ar << m_showLink;
320     ar << m_fEpBookmark;
321     ar << m_basePath;
322     ar << m_parentPathID;
323     ar << m_resumePoint.timeInSeconds;
324     ar << m_resumePoint.totalTimeInSeconds;
325     ar << m_iIdShow;
326     ar << m_strShowPath;
327     ar << m_dateAdded.GetAsDBDateTime();
328     ar << m_type;
329     ar << m_iIdSeason;
330   }
331   else
332   {
333     ar >> m_director;
334     ar >> m_writingCredits;
335     ar >> m_genre;
336     ar >> m_country;
337     ar >> m_strTagLine;
338     ar >> m_strPlotOutline;
339     ar >> m_strPlot;
340     ar >> m_strPictureURL.m_spoof;
341     ar >> m_strPictureURL.m_xml;
342     ar >> m_fanart.m_xml;
343     ar >> m_strTitle;
344     ar >> m_strSortTitle;
345     ar >> m_strVotes;
346     ar >> m_studio;
347     ar >> m_strTrailer;
348     int iCastSize;
349     ar >> iCastSize;
350     m_cast.reserve(iCastSize);
351     for (int i=0;i<iCastSize;++i)
352     {
353       SActorInfo info;
354       ar >> info.strName;
355       ar >> info.strRole;
356       ar >> info.order;
357       ar >> info.thumb;
358       CStdString strXml;
359       ar >> strXml;
360       info.thumbUrl.ParseString(strXml);
361       m_cast.push_back(info);
362     }
363
364     ar >> m_strSet;
365     ar >> m_iSetId;
366     ar >> m_tags;
367     ar >> m_duration;
368     ar >> m_strFile;
369     ar >> m_strPath;
370     ar >> m_strIMDBNumber;
371     ar >> m_strMPAARating;
372     ar >> m_strFileNameAndPath;
373     ar >> m_strOriginalTitle;
374     ar >> m_strEpisodeGuide;
375     ar >> m_premiered;
376     ar >> m_strStatus;
377     ar >> m_strProductionCode;
378     ar >> m_firstAired;
379     ar >> m_strShowTitle;
380     ar >> m_strAlbum;
381     ar >> m_artist;
382     ar >> m_playCount;
383     ar >> m_lastPlayed;
384     ar >> m_iTop250;
385     ar >> m_iYear;
386     ar >> m_iSeason;
387     ar >> m_iEpisode;
388     ar >> m_strUniqueId;
389     ar >> m_fRating;
390     ar >> m_iDbId;
391     ar >> m_iFileId;
392     ar >> m_iSpecialSortSeason;
393     ar >> m_iSpecialSortEpisode;
394     ar >> m_iBookmarkId;
395     ar >> m_iTrack;
396     ar >> dynamic_cast<IArchivable&>(m_streamDetails);
397     ar >> m_showLink;
398     ar >> m_fEpBookmark;
399     ar >> m_basePath;
400     ar >> m_parentPathID;
401     ar >> m_resumePoint.timeInSeconds;
402     ar >> m_resumePoint.totalTimeInSeconds;
403     ar >> m_iIdShow;
404     ar >> m_strShowPath;
405
406     CStdString dateAdded;
407     ar >> dateAdded;
408     m_dateAdded.SetFromDBDateTime(dateAdded);
409     ar >> m_type;
410     ar >> m_iIdSeason;
411   }
412 }
413
414 void CVideoInfoTag::Serialize(CVariant& value) const
415 {
416   value["director"] = m_director;
417   value["writer"] = m_writingCredits;
418   value["genre"] = m_genre;
419   value["country"] = m_country;
420   value["tagline"] = m_strTagLine;
421   value["plotoutline"] = m_strPlotOutline;
422   value["plot"] = m_strPlot;
423   value["title"] = m_strTitle;
424   value["votes"] = m_strVotes;
425   value["studio"] = m_studio;
426   value["trailer"] = m_strTrailer;
427   value["cast"] = CVariant(CVariant::VariantTypeArray);
428   for (unsigned int i = 0; i < m_cast.size(); ++i)
429   {
430     CVariant actor;
431     actor["name"] = m_cast[i].strName;
432     actor["role"] = m_cast[i].strRole;
433     actor["order"] = m_cast[i].order;
434     if (!m_cast[i].thumb.IsEmpty())
435       actor["thumbnail"] = CTextureCache::GetWrappedImageURL(m_cast[i].thumb);
436     value["cast"].push_back(actor);
437   }
438   value["set"] = m_strSet;
439   value["setid"] = m_iSetId;
440   value["tag"] = m_tags;
441   value["runtime"] = GetDuration();
442   value["file"] = m_strFile;
443   value["path"] = m_strPath;
444   value["imdbnumber"] = m_strIMDBNumber;
445   value["mpaa"] = m_strMPAARating;
446   value["filenameandpath"] = m_strFileNameAndPath;
447   value["originaltitle"] = m_strOriginalTitle;
448   value["sorttitle"] = m_strSortTitle;
449   value["episodeguide"] = m_strEpisodeGuide;
450   value["premiered"] = m_premiered.IsValid() ? m_premiered.GetAsDBDate() : StringUtils::EmptyString;
451   value["status"] = m_strStatus;
452   value["productioncode"] = m_strProductionCode;
453   value["firstaired"] = m_firstAired.IsValid() ? m_firstAired.GetAsDBDate() : StringUtils::EmptyString;
454   value["showtitle"] = m_strShowTitle;
455   value["album"] = m_strAlbum;
456   value["artist"] = m_artist;
457   value["playcount"] = m_playCount;
458   value["lastplayed"] = m_lastPlayed.IsValid() ? m_lastPlayed.GetAsDBDateTime() : StringUtils::EmptyString;
459   value["top250"] = m_iTop250;
460   value["year"] = m_iYear;
461   value["season"] = m_iSeason;
462   value["episode"] = m_iEpisode;
463   value["uniqueid"]["unknown"] = m_strUniqueId;
464   value["rating"] = m_fRating;
465   value["dbid"] = m_iDbId;
466   value["fileid"] = m_iFileId;
467   value["track"] = m_iTrack;
468   value["showlink"] = m_showLink;
469   m_streamDetails.Serialize(value["streamdetails"]);
470   CVariant resume = CVariant(CVariant::VariantTypeObject);
471   resume["position"] = (float)m_resumePoint.timeInSeconds;
472   resume["total"] = (float)m_resumePoint.totalTimeInSeconds;
473   value["resume"] = resume;
474   value["tvshowid"] = m_iIdShow;
475   value["tvshowpath"] = m_strShowPath;
476   value["dateadded"] = m_dateAdded.IsValid() ? m_dateAdded.GetAsDBDateTime() : StringUtils::EmptyString;
477   value["type"] = m_type;
478   value["seasonid"] = m_iIdSeason;
479 }
480
481 void CVideoInfoTag::ToSortable(SortItem& sortable, Field field) const
482 {
483   switch (field)
484   {
485   case FieldDirector:                 sortable[FieldDirector] = m_director; break;
486   case FieldWriter:                   sortable[FieldWriter] = m_writingCredits; break;
487   case FieldGenre:                    sortable[FieldGenre] = m_genre; break;
488   case FieldCountry:                  sortable[FieldCountry] = m_country; break;
489   case FieldTagline:                  sortable[FieldTagline] = m_strTagLine; break;
490   case FieldPlotOutline:              sortable[FieldPlotOutline] = m_strPlotOutline; break;
491   case FieldPlot:                     sortable[FieldPlot] = m_strPlot; break;
492   case FieldTitle:                    sortable[FieldTitle] = m_strTitle; break;
493   case FieldVotes:                    sortable[FieldVotes] = m_strVotes; break;
494   case FieldStudio:                   sortable[FieldStudio] = m_studio; break;
495   case FieldTrailer:                  sortable[FieldTrailer] = m_strTrailer; break;
496   case FieldSet:                      sortable[FieldSet] = m_strSet; break;
497   case FieldTime:                     sortable[FieldTime] = GetDuration(); break;
498   case FieldFilename:                 sortable[FieldFilename] = m_strFile; break;
499   case FieldMPAA:                     sortable[FieldMPAA] = m_strMPAARating; break;
500   case FieldPath:                     sortable[FieldPath] = m_strFileNameAndPath; break;
501   case FieldSortTitle:                sortable[FieldSortTitle] = m_strSortTitle; break;
502   case FieldTvShowStatus:             sortable[FieldTvShowStatus] = m_strStatus; break;
503   case FieldProductionCode:           sortable[FieldProductionCode] = m_strProductionCode; break;
504   case FieldAirDate:                  sortable[FieldAirDate] = m_firstAired.IsValid() ? m_firstAired.GetAsDBDate() : (m_premiered.IsValid() ? m_premiered.GetAsDBDate() : StringUtils::EmptyString); break;
505   case FieldTvShowTitle:              sortable[FieldTvShowTitle] = m_strShowTitle; break;
506   case FieldAlbum:                    sortable[FieldAlbum] = m_strAlbum; break;
507   case FieldArtist:                   sortable[FieldArtist] = m_artist; break;
508   case FieldPlaycount:                sortable[FieldPlaycount] = m_playCount; break;
509   case FieldLastPlayed:               sortable[FieldLastPlayed] = m_lastPlayed.IsValid() ? m_lastPlayed.GetAsDBDateTime() : StringUtils::EmptyString; break;
510   case FieldTop250:                   sortable[FieldTop250] = m_iTop250; break;
511   case FieldYear:                     sortable[FieldYear] = m_iYear; break;
512   case FieldSeason:                   sortable[FieldSeason] = m_iSeason; break;
513   case FieldEpisodeNumber:            sortable[FieldEpisodeNumber] = m_iEpisode; break;
514   case FieldEpisodeNumberSpecialSort: sortable[FieldEpisodeNumberSpecialSort] = m_iSpecialSortEpisode; break;
515   case FieldSeasonSpecialSort:        sortable[FieldSeasonSpecialSort] = m_iSpecialSortSeason; break;
516   case FieldRating:                   sortable[FieldRating] = m_fRating; break;
517   case FieldId:                       sortable[FieldId] = m_iDbId; break;
518   case FieldTrackNumber:              sortable[FieldTrackNumber] = m_iTrack; break;
519   case FieldTag:                      sortable[FieldTag] = m_tags; break;
520
521   case FieldVideoResolution:          sortable[FieldVideoResolution] = m_streamDetails.GetVideoHeight(); break;
522   case FieldVideoAspectRatio:         sortable[FieldVideoAspectRatio] = m_streamDetails.GetVideoAspect(); break;
523   case FieldVideoCodec:               sortable[FieldVideoCodec] = m_streamDetails.GetVideoCodec(); break;
524
525   case FieldAudioChannels:            sortable[FieldAudioChannels] = m_streamDetails.GetAudioChannels(); break;
526   case FieldAudioCodec:               sortable[FieldAudioCodec] = m_streamDetails.GetAudioCodec(); break;
527   case FieldAudioLanguage:            sortable[FieldAudioLanguage] = m_streamDetails.GetAudioLanguage(); break;
528
529   case FieldSubtitleLanguage:         sortable[FieldSubtitleLanguage] = m_streamDetails.GetSubtitleLanguage(); break;
530
531   case FieldInProgress:               sortable[FieldInProgress] = m_resumePoint.IsPartWay(); break;
532   case FieldDateAdded:                sortable[FieldDateAdded] = m_dateAdded.IsValid() ? m_dateAdded.GetAsDBDateTime() : StringUtils::EmptyString; break;
533   case FieldMediaType:                sortable[FieldMediaType] = DatabaseUtils::MediaTypeFromString(m_type); break;
534   default: break;
535   }
536 }
537
538 const CStdString CVideoInfoTag::GetCast(bool bIncludeRole /*= false*/) const
539 {
540   CStdString strLabel;
541   for (iCast it = m_cast.begin(); it != m_cast.end(); ++it)
542   {
543     CStdString character;
544     if (it->strRole.IsEmpty() || !bIncludeRole)
545       character.Format("%s\n", it->strName.c_str());
546     else
547       character.Format("%s %s %s\n", it->strName.c_str(), g_localizeStrings.Get(20347).c_str(), it->strRole.c_str());
548     strLabel += character;
549   }
550   return strLabel.TrimRight("\n");
551 }
552
553 void CVideoInfoTag::ParseNative(const TiXmlElement* movie, bool prioritise)
554 {
555   XMLUtils::GetString(movie, "title", m_strTitle);
556   XMLUtils::GetString(movie, "originaltitle", m_strOriginalTitle);
557   XMLUtils::GetString(movie, "showtitle", m_strShowTitle);
558   XMLUtils::GetString(movie, "sorttitle", m_strSortTitle);
559   XMLUtils::GetFloat(movie, "rating", m_fRating);
560   XMLUtils::GetFloat(movie, "epbookmark", m_fEpBookmark);
561   int max_value = 10;
562   const TiXmlElement* rElement = movie->FirstChildElement("rating");
563   if (rElement && (rElement->QueryIntAttribute("max", &max_value) == TIXML_SUCCESS) && max_value>=1)
564   {
565     m_fRating = m_fRating / max_value * 10; // Normalise the Movie Rating to between 1 and 10
566   }
567   XMLUtils::GetInt(movie, "year", m_iYear);
568   XMLUtils::GetInt(movie, "top250", m_iTop250);
569   XMLUtils::GetInt(movie, "season", m_iSeason);
570   XMLUtils::GetInt(movie, "episode", m_iEpisode);
571   XMLUtils::GetInt(movie, "track", m_iTrack);
572   XMLUtils::GetString(movie, "uniqueid", m_strUniqueId);
573   XMLUtils::GetInt(movie, "displayseason", m_iSpecialSortSeason);
574   XMLUtils::GetInt(movie, "displayepisode", m_iSpecialSortEpisode);
575   int after=0;
576   XMLUtils::GetInt(movie, "displayafterseason",after);
577   if (after > 0)
578   {
579     m_iSpecialSortSeason = after;
580     m_iSpecialSortEpisode = 0x1000; // should be more than any realistic episode number
581   }
582   XMLUtils::GetString(movie, "votes", m_strVotes);
583   XMLUtils::GetString(movie, "outline", m_strPlotOutline);
584   XMLUtils::GetString(movie, "plot", m_strPlot);
585   XMLUtils::GetString(movie, "tagline", m_strTagLine);
586   CStdString runtime;
587   if (XMLUtils::GetString(movie, "runtime", runtime) && !runtime.empty())
588     m_duration = GetDurationFromMinuteString(runtime);
589   XMLUtils::GetString(movie, "mpaa", m_strMPAARating);
590   XMLUtils::GetInt(movie, "playcount", m_playCount);
591   XMLUtils::GetDate(movie, "lastplayed", m_lastPlayed);
592   XMLUtils::GetString(movie, "file", m_strFile);
593   XMLUtils::GetString(movie, "path", m_strPath);
594   XMLUtils::GetString(movie, "id", m_strIMDBNumber);
595   XMLUtils::GetString(movie, "filenameandpath", m_strFileNameAndPath);
596   XMLUtils::GetDate(movie, "premiered", m_premiered);
597   XMLUtils::GetString(movie, "status", m_strStatus);
598   XMLUtils::GetString(movie, "code", m_strProductionCode);
599   XMLUtils::GetDate(movie, "aired", m_firstAired);
600   XMLUtils::GetString(movie, "album", m_strAlbum);
601   XMLUtils::GetString(movie, "trailer", m_strTrailer);
602   XMLUtils::GetString(movie, "basepath", m_basePath);
603
604   size_t iThumbCount = m_strPictureURL.m_url.size();
605   CStdString xmlAdd = m_strPictureURL.m_xml;
606
607   const TiXmlElement* thumb = movie->FirstChildElement("thumb");
608   while (thumb)
609   {
610     m_strPictureURL.ParseElement(thumb);
611     if (prioritise)
612     {
613       CStdString temp;
614       temp << *thumb;
615       xmlAdd = temp+xmlAdd;
616     }
617     thumb = thumb->NextSiblingElement("thumb");
618   }
619
620   // prioritise thumbs from nfos
621   if (prioritise && iThumbCount && iThumbCount != m_strPictureURL.m_url.size())
622   {
623     rotate(m_strPictureURL.m_url.begin(),
624            m_strPictureURL.m_url.begin()+iThumbCount, 
625            m_strPictureURL.m_url.end());
626     m_strPictureURL.m_xml = xmlAdd;
627   }
628
629   XMLUtils::GetStringArray(movie, "genre", m_genre, prioritise, g_advancedSettings.m_videoItemSeparator);
630   XMLUtils::GetStringArray(movie, "country", m_country, prioritise, g_advancedSettings.m_videoItemSeparator);
631   XMLUtils::GetStringArray(movie, "credits", m_writingCredits, prioritise, g_advancedSettings.m_videoItemSeparator);
632   XMLUtils::GetStringArray(movie, "director", m_director, prioritise, g_advancedSettings.m_videoItemSeparator);
633   XMLUtils::GetStringArray(movie, "showlink", m_showLink, prioritise, g_advancedSettings.m_videoItemSeparator);
634
635   // cast
636   const TiXmlElement* node = movie->FirstChildElement("actor");
637   if (node && node->FirstChild() && prioritise)
638     m_cast.clear();
639   while (node)
640   {
641     const TiXmlNode *actor = node->FirstChild("name");
642     if (actor && actor->FirstChild())
643     {
644       SActorInfo info;
645       info.strName = actor->FirstChild()->Value();
646       XMLUtils::GetString(node, "role", info.strRole);
647       XMLUtils::GetInt(node, "order", info.order);
648       const TiXmlElement* thumb = node->FirstChildElement("thumb");
649       while (thumb)
650       {
651         info.thumbUrl.ParseElement(thumb);
652         thumb = thumb->NextSiblingElement("thumb");
653       }
654       const char* clear=node->Attribute("clear");
655       if (clear && stricmp(clear,"true"))
656         m_cast.clear();
657       m_cast.push_back(info);
658     }
659     node = node->NextSiblingElement("actor");
660   }
661
662   XMLUtils::GetString(movie, "set", m_strSet);
663   XMLUtils::GetStringArray(movie, "tag", m_tags, prioritise, g_advancedSettings.m_videoItemSeparator);
664   XMLUtils::GetStringArray(movie, "studio", m_studio, prioritise, g_advancedSettings.m_videoItemSeparator);
665   // artists
666   node = movie->FirstChildElement("artist");
667   if (node && node->FirstChild() && prioritise)
668     m_artist.clear();
669   while (node)
670   {
671     const TiXmlNode* pNode = node->FirstChild("name");
672     const char* pValue=NULL;
673     if (pNode && pNode->FirstChild())
674       pValue = pNode->FirstChild()->Value();
675     else if (node->FirstChild())
676       pValue = node->FirstChild()->Value();
677     if (pValue)
678     {
679       const char* clear=node->Attribute("clear");
680       if (clear && stricmp(clear,"true")==0)
681         m_artist.clear();
682       vector<string> artists = StringUtils::Split(pValue, g_advancedSettings.m_videoItemSeparator);
683       m_artist.insert(m_artist.end(), artists.begin(), artists.end());
684     }
685     node = node->NextSiblingElement("artist");
686   }
687
688   node = movie->FirstChildElement("fileinfo");
689   if (node)
690   {
691     // Try to pull from fileinfo/streamdetails/[video|audio|subtitle]
692     const TiXmlNode *nodeStreamDetails = node->FirstChild("streamdetails");
693     if (nodeStreamDetails)
694     {
695       const TiXmlNode *nodeDetail = NULL;
696       while ((nodeDetail = nodeStreamDetails->IterateChildren("audio", nodeDetail)))
697       {
698         CStreamDetailAudio *p = new CStreamDetailAudio();
699         XMLUtils::GetString(nodeDetail, "codec", p->m_strCodec);
700         XMLUtils::GetString(nodeDetail, "language", p->m_strLanguage);
701         XMLUtils::GetInt(nodeDetail, "channels", p->m_iChannels);
702         p->m_strCodec.MakeLower();
703         p->m_strLanguage.MakeLower();
704         m_streamDetails.AddStream(p);
705       }
706       nodeDetail = NULL;
707       while ((nodeDetail = nodeStreamDetails->IterateChildren("video", nodeDetail)))
708       {
709         CStreamDetailVideo *p = new CStreamDetailVideo();
710         XMLUtils::GetString(nodeDetail, "codec", p->m_strCodec);
711         XMLUtils::GetFloat(nodeDetail, "aspect", p->m_fAspect);
712         XMLUtils::GetInt(nodeDetail, "width", p->m_iWidth);
713         XMLUtils::GetInt(nodeDetail, "height", p->m_iHeight);
714         XMLUtils::GetInt(nodeDetail, "durationinseconds", p->m_iDuration);
715         p->m_strCodec.MakeLower();
716         m_streamDetails.AddStream(p);
717       }
718       nodeDetail = NULL;
719       while ((nodeDetail = nodeStreamDetails->IterateChildren("subtitle", nodeDetail)))
720       {
721         CStreamDetailSubtitle *p = new CStreamDetailSubtitle();
722         XMLUtils::GetString(nodeDetail, "language", p->m_strLanguage);
723         p->m_strLanguage.MakeLower();
724         m_streamDetails.AddStream(p);
725       }
726     }
727     m_streamDetails.DetermineBestStreams();
728   }  /* if fileinfo */
729
730   const TiXmlElement *epguide = movie->FirstChildElement("episodeguide");
731   if (epguide)
732   {
733     // DEPRECIATE ME - support for old XML-encoded <episodeguide> blocks.
734     if (epguide->FirstChild() && strnicmp("<episodeguide", epguide->FirstChild()->Value(), 13) == 0)
735       m_strEpisodeGuide = epguide->FirstChild()->Value();
736     else
737     {
738       stringstream stream;
739       stream << *epguide;
740       m_strEpisodeGuide = stream.str();
741     }
742   }
743
744   // fanart
745   const TiXmlElement *fanart = movie->FirstChildElement("fanart");
746   if (fanart)
747   {
748     // we prioritise mixed-mode nfo's with fanart set
749     if (prioritise)
750     {
751       CStdString temp;
752       temp << *fanart;
753       m_fanart.m_xml = temp+m_fanart.m_xml;
754     }
755     else
756       m_fanart.m_xml << *fanart;
757     m_fanart.Unpack();
758   }
759
760   // resumePoint
761   const TiXmlNode *resume = movie->FirstChild("resume");
762   if (resume)
763   {
764     XMLUtils::GetDouble(resume, "position", m_resumePoint.timeInSeconds);
765     XMLUtils::GetDouble(resume, "total", m_resumePoint.totalTimeInSeconds);
766   }
767
768   // dateAdded
769   CStdString dateAdded;
770   XMLUtils::GetString(movie, "dateadded", dateAdded);
771   m_dateAdded.SetFromDBDateTime(dateAdded);
772 }
773
774 bool CVideoInfoTag::HasStreamDetails() const
775 {
776   return m_streamDetails.HasItems();
777 }
778
779 bool CVideoInfoTag::IsEmpty() const
780 {
781   return (m_strTitle.IsEmpty() &&
782           m_strFile.IsEmpty() &&
783           m_strPath.IsEmpty());
784 }
785
786 unsigned int CVideoInfoTag::GetDuration() const
787 {
788   /*
789    Prefer the duration from the stream if it isn't too
790    small (60%) compared to the duration from the tag.
791    */
792   unsigned int duration = m_streamDetails.GetVideoDuration();
793   if (duration > m_duration * 0.6)
794     return duration;
795
796   return m_duration;
797 }
798
799 unsigned int CVideoInfoTag::GetDurationFromMinuteString(const std::string &runtime)
800 {
801   unsigned int duration = (unsigned int)str2uint64(runtime);
802   if (!duration)
803   { // failed for some reason, or zero
804     duration = strtoul(runtime.c_str(), NULL, 10);
805     CLog::Log(LOGWARNING, "%s <runtime> should be in minutes. Interpreting '%s' as %u minutes", __FUNCTION__, runtime.c_str(), duration);
806   }
807   return duration*60;
808 }