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, const CStdString& strPath2)
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 while (m_headofdoc && details.m_iEpisode != episode)
76 m_headofdoc = strstr(m_headofdoc+1,"<episodedetails");
77 bNfo = GetDetails(details);
80 if (details.m_iEpisode != episode)
85 if (infos == 1) // still allow differing nfo/file numbers for single ep nfo's
86 bNfo = GetDetails(details);
91 vector<ScraperPtr> vecScrapers;
93 // add selected scraper - first proirity
95 vecScrapers.push_back(m_info);
97 // Add all scrapers except selected and default
99 CAddonMgr::Get().GetAddons(m_type,addons);
101 for (unsigned i = 0; i < addons.size(); ++i)
103 ScraperPtr scraper = boost::dynamic_pointer_cast<CScraper>(addons[i]);
105 // skip if scraper requires settings and there's nothing set yet
106 if (scraper->RequiresSettings() && !scraper->HasUserSettings())
109 if( (!m_info || m_info->ID() != scraper->ID()) && (!defaultScraper || defaultScraper->ID() != scraper->ID()) )
110 vecScrapers.push_back(scraper);
113 // add default scraper - not user selectable so it's last priority
114 if( defaultScraper && (!m_info || m_info->ID() != defaultScraper->ID()) &&
115 ( !defaultScraper->RequiresSettings() || defaultScraper->HasUserSettings() ) )
116 vecScrapers.push_back(defaultScraper);
120 for (unsigned int i=0;i<vecScrapers.size();++i)
121 if ((res = Scrape(vecScrapers[i])) == 0 || res == 2)
127 return m_scurl.m_url.empty() ? FULL_NFO : COMBINED_NFO;
128 return m_scurl.m_url.empty() ? NO_NFO : URL_NFO;
131 // return value: 0 - success; 1 - no result; skip; 2 - error
132 int CNfoFile::Scrape(ScraperPtr& scraper)
134 if (scraper->IsNoop())
136 m_scurl = CScraperUrl();
139 if (scraper->Type() != m_type)
141 scraper->ClearCache();
145 m_scurl = scraper->NfoUrl(m_doc);
147 catch (const CScraperError &sce)
149 CVideoInfoDownloader::ShowErrorDialog(sce);
154 if (!m_scurl.m_url.empty())
155 SetScraperInfo(scraper);
156 return m_scurl.m_url.empty() ? 1 : 0;
159 int CNfoFile::Load(const CStdString& strFile)
163 if (file.Open(strFile))
165 int size = (int)file.GetLength();
168 m_doc = new char[size+1];
173 CLog::Log(LOGERROR, "%s: Exception while creating file buffer",__FUNCTION__);
181 file.Read(m_doc, size);
189 void CNfoFile::Close()