2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
20 // NfoFile.cpp: implementation of the CNfoFile class.
22 //////////////////////////////////////////////////////////////////////
25 #include "video/VideoInfoDownloader.h"
26 #include "addons/AddonManager.h"
27 #include "filesystem/File.h"
29 #include "music/Album.h"
30 #include "music/Artist.h"
31 #include "settings/Settings.h"
32 #include "utils/log.h"
37 using namespace XFILE
;
38 using namespace ADDON
;
40 CNfoFile::NFOResult
CNfoFile::Create(const CStdString
& strPath
, const ScraperPtr
& info
, int episode
)
42 m_info
= info
; // assume we can use these settings
43 m_type
= ScraperTypeFromContent(info
->Content());
44 if (FAILED(Load(strPath
)))
51 ScraperPtr defaultScraper
;
52 if (CAddonMgr::Get().GetDefault(m_type
, addon
))
53 defaultScraper
= boost::dynamic_pointer_cast
<CScraper
>(addon
);
55 if (m_type
== ADDON_SCRAPER_ALBUMS
)
58 bNfo
= GetDetails(album
);
60 else if (m_type
== ADDON_SCRAPER_ARTISTS
)
63 bNfo
= GetDetails(artist
);
65 else if (m_type
== ADDON_SCRAPER_TVSHOWS
|| m_type
== ADDON_SCRAPER_MOVIES
|| m_type
== ADDON_SCRAPER_MUSICVIDEOS
)
67 // first check if it's an XML file with the info we need
68 CVideoInfoTag details
;
69 bNfo
= GetDetails(details
);
70 if (episode
> -1 && bNfo
&& m_type
== ADDON_SCRAPER_TVSHOWS
)
73 m_headofdoc
= strstr(m_headofdoc
,"<episodedetails");
74 bNfo
= GetDetails(details
);
75 while (m_headofdoc
&& details
.m_iEpisode
!= episode
)
77 m_headofdoc
= strstr(m_headofdoc
+1,"<episodedetails");
78 bNfo
= GetDetails(details
);
81 if (details
.m_iEpisode
!= episode
)
86 if (infos
== 1) // still allow differing nfo/file numbers for single ep nfo's
87 bNfo
= GetDetails(details
);
92 vector
<ScraperPtr
> vecScrapers
;
94 // add selected scraper - first proirity
96 vecScrapers
.push_back(m_info
);
98 // Add all scrapers except selected and default
100 CAddonMgr::Get().GetAddons(m_type
,addons
);
102 for (unsigned i
= 0; i
< addons
.size(); ++i
)
104 ScraperPtr scraper
= boost::dynamic_pointer_cast
<CScraper
>(addons
[i
]);
106 // skip if scraper requires settings and there's nothing set yet
107 if (scraper
->RequiresSettings() && !scraper
->HasUserSettings())
110 if( (!m_info
|| m_info
->ID() != scraper
->ID()) && (!defaultScraper
|| defaultScraper
->ID() != scraper
->ID()) )
111 vecScrapers
.push_back(scraper
);
114 // add default scraper - not user selectable so it's last priority
115 if( defaultScraper
&& (!m_info
|| m_info
->ID() != defaultScraper
->ID()) &&
116 ( !defaultScraper
->RequiresSettings() || defaultScraper
->HasUserSettings() ) )
117 vecScrapers
.push_back(defaultScraper
);
121 for (unsigned int i
=0;i
<vecScrapers
.size();++i
)
122 if ((res
= Scrape(vecScrapers
[i
])) == 0 || res
== 2)
128 return m_scurl
.m_url
.empty() ? FULL_NFO
: COMBINED_NFO
;
129 return m_scurl
.m_url
.empty() ? NO_NFO
: URL_NFO
;
132 // return value: 0 - success; 1 - no result; skip; 2 - error
133 int CNfoFile::Scrape(ScraperPtr
& scraper
)
135 if (scraper
->IsNoop())
137 m_scurl
= CScraperUrl();
140 if (scraper
->Type() != m_type
)
142 scraper
->ClearCache();
146 m_scurl
= scraper
->NfoUrl(m_doc
);
148 catch (const CScraperError
&sce
)
150 CVideoInfoDownloader::ShowErrorDialog(sce
);
155 if (!m_scurl
.m_url
.empty())
156 SetScraperInfo(scraper
);
157 return m_scurl
.m_url
.empty() ? 1 : 0;
160 int CNfoFile::Load(const CStdString
& strFile
)
164 if (file
.Open(strFile
))
166 int size
= (int)file
.GetLength();
169 m_doc
= new char[size
+1];
174 CLog::Log(LOGERROR
, "%s: Exception while creating file buffer",__FUNCTION__
);
182 file
.Read(m_doc
, size
);
190 void CNfoFile::Close()