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/>.
22 #include "GUIUserMessages.h"
23 #include "Application.h"
24 #include "GUIDialogSubtitles.h"
25 #include "addons/AddonManager.h"
26 #include "cores/IPlayer.h"
27 #include "dialogs/GUIDialogKaiToast.h"
28 #include "filesystem/AddonsDirectory.h"
29 #include "filesystem/File.h"
30 #include "filesystem/PluginDirectory.h"
31 #include "filesystem/SpecialProtocol.h"
32 #include "guilib/GUIImage.h"
33 #include "settings/MediaSettings.h"
34 #include "settings/Settings.h"
35 #include "settings/VideoSettings.h"
36 #include "settings/lib/Setting.h"
37 #include "utils/JobManager.h"
38 #include "utils/LangCodeExpander.h"
39 #include "utils/log.h"
40 #include "utils/StringUtils.h"
41 #include "utils/URIUtils.h"
45 using namespace ADDON;
46 using namespace XFILE;
48 #define CONTROL_NAMELABEL 100
49 #define CONTROL_NAMELOGO 110
50 #define CONTROL_SUBLIST 120
51 #define CONTROL_SUBSEXIST 130
52 #define CONTROL_SUBSTATUS 140
53 #define CONTROL_SERVICELIST 150
55 /*! \brief simple job to retrieve a directory and store a string (language)
57 class CSubtitlesJob: public CJob
60 CSubtitlesJob(const CURL &url, const std::string &language) : m_url(url), m_language(language)
62 m_items = new CFileItemList;
64 virtual ~CSubtitlesJob()
70 CDirectory::GetDirectory(m_url.Get(), *m_items);
73 virtual bool operator==(const CJob *job) const
75 if (strcmp(job->GetType(),GetType()) == 0)
77 const CSubtitlesJob* rjob = dynamic_cast<const CSubtitlesJob*>(job);
80 return m_url.Get() == rjob->m_url.Get() &&
81 m_language == rjob->m_language;
86 const CFileItemList *GetItems() const { return m_items; }
87 const CURL &GetURL() const { return m_url; }
88 const std::string &GetLanguage() const { return m_language; }
91 CFileItemList *m_items;
92 std::string m_language;
95 CGUIDialogSubtitles::CGUIDialogSubtitles(void)
96 : CGUIDialog(WINDOW_DIALOG_SUBTITLES, "DialogSubtitles.xml")
98 m_loadType = KEEP_IN_MEMORY;
99 m_subtitles = new CFileItemList;
100 m_serviceItems = new CFileItemList;
101 m_pausedOnRun = false;
102 m_updateSubsList = false;
105 CGUIDialogSubtitles::~CGUIDialogSubtitles(void)
109 delete m_serviceItems;
112 bool CGUIDialogSubtitles::OnMessage(CGUIMessage& message)
114 if (message.GetMessage() == GUI_MSG_CLICKED)
116 int iControl = message.GetSenderId();
118 if (iControl == CONTROL_SUBLIST)
120 CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_SUBLIST);
123 int item = msg.GetParam1();
124 if (item >= 0 && item < m_subtitles->Size())
125 Download(*m_subtitles->Get(item));
128 else if (iControl == CONTROL_SERVICELIST)
130 CGUIMessage msg(GUI_MSG_ITEM_SELECTED, GetID(), CONTROL_SERVICELIST);
133 int item = msg.GetParam1();
134 if (item >= 0 && item < m_serviceItems->Size() &&
135 SetService(m_serviceItems->Get(item)->GetProperty("Addon.ID").asString()))
141 else if (message.GetMessage() == GUI_MSG_WINDOW_DEINIT)
143 // Resume the video if the user has requested it
144 if (g_application.m_pPlayer->IsPaused() && m_pausedOnRun)
145 g_application.m_pPlayer->Pause();
147 CGUIDialog::OnMessage(message);
153 return CGUIDialog::OnMessage(message);
156 void CGUIDialogSubtitles::OnInitWindow()
158 // Pause the video if the user has requested it
159 m_pausedOnRun = false;
160 if (CSettings::Get().GetBool("subtitles.pauseonsearch") && !g_application.m_pPlayer->IsPaused())
162 g_application.m_pPlayer->Pause();
163 m_pausedOnRun = true;
167 CGUIWindow::OnInitWindow();
171 void CGUIDialogSubtitles::Process(unsigned int currentTime, CDirtyRegionList &dirtyregions)
175 // take copies of our variables to ensure we don't hold the lock for long.
179 CSingleLock lock(m_section);
181 subs.Assign(*m_subtitles);
183 SET_CONTROL_LABEL(CONTROL_SUBSTATUS, status);
185 if (m_updateSubsList)
187 CGUIMessage message(GUI_MSG_LABEL_BIND, GetID(), CONTROL_SUBLIST, 0, 0, &subs);
189 m_updateSubsList = false;
192 if (!m_subtitles->IsEmpty() && !GetFocusedControl())
193 { // set focus to the list
194 CGUIMessage msg(GUI_MSG_SETFOCUS, GetID(), CONTROL_SUBLIST);
198 CGUIDialog::Process(currentTime, dirtyregions);
201 void CGUIDialogSubtitles::FillServices()
206 ADDON::CAddonMgr::Get().GetAddons(ADDON_SUBTITLE_MODULE, addons, true);
210 UpdateStatus(NO_SERVICES);
214 for (VECADDONS::const_iterator addonIt = addons.begin(); addonIt != addons.end(); addonIt++)
216 CFileItemPtr item(CAddonsDirectory::FileItemFromAddon(*addonIt, "plugin://", false));
217 m_serviceItems->Add(item);
220 // Bind our services to the UI
221 CGUIMessage msg(GUI_MSG_LABEL_BIND, GetID(), CONTROL_SERVICELIST, 0, 0, m_serviceItems);
224 // TODO: Default service support will need to check through the items to find the CFileItem in the loop above.
225 SetService(m_serviceItems->Get(0)->GetProperty("Addon.ID").asString());
228 bool CGUIDialogSubtitles::SetService(const std::string &service)
230 if (service != m_currentService)
232 m_currentService = service;
233 CLog::Log(LOGDEBUG, "New Service [%s] ", m_currentService.c_str());
235 CFileItemPtr currentService = GetService();
236 // highlight this item in the skin
237 for (int i = 0; i < m_serviceItems->Size(); i++)
239 CFileItemPtr pItem = m_serviceItems->Get(i);
240 pItem->Select(pItem == currentService);
243 SET_CONTROL_LABEL(CONTROL_NAMELABEL, currentService->GetLabel());
245 CGUIImage* image = (CGUIImage*)GetControl(CONTROL_NAMELOGO);
247 image->SetFileName(currentService->GetArt("thumb"));
249 if (g_application.m_pPlayer->GetSubtitleCount() == 0)
250 SET_CONTROL_HIDDEN(CONTROL_SUBSEXIST);
252 SET_CONTROL_VISIBLE(CONTROL_SUBSEXIST);
259 const CFileItemPtr CGUIDialogSubtitles::GetService() const
261 for (int i = 0; i < m_serviceItems->Size(); i++)
263 if (m_serviceItems->Get(i)->GetProperty("Addon.ID") == m_currentService)
264 return m_serviceItems->Get(i);
266 return CFileItemPtr();
269 void CGUIDialogSubtitles::Search()
271 if (m_currentService.empty())
272 return; // no services available
274 UpdateStatus(SEARCHING);
277 CURL url("plugin://" + m_currentService + "/");
278 url.SetOption("action", "search");
280 const CSetting *setting = CSettings::Get().GetSetting("subtitles.languages");
282 url.SetOption("languages", setting->ToString());
284 AddJob(new CSubtitlesJob(url, ""));
287 void CGUIDialogSubtitles::OnJobComplete(unsigned int jobID, bool success, CJob *job)
289 const CURL &url = ((CSubtitlesJob *)job)->GetURL();
290 const CFileItemList *items = ((CSubtitlesJob *)job)->GetItems();
291 const std::string &language = ((CSubtitlesJob *)job)->GetLanguage();
292 if (url.GetOption("action") == "search")
293 OnSearchComplete(items);
295 OnDownloadComplete(items, language);
296 CJobQueue::OnJobComplete(jobID, success, job);
299 void CGUIDialogSubtitles::OnSearchComplete(const CFileItemList *items)
301 CSingleLock lock(m_section);
302 m_subtitles->Assign(*items);
303 UpdateStatus(SEARCH_COMPLETE);
304 m_updateSubsList = true;
308 void CGUIDialogSubtitles::UpdateStatus(STATUS status)
310 CSingleLock lock(m_section);
315 label = g_localizeStrings.Get(24114);
318 label = g_localizeStrings.Get(24107);
320 case SEARCH_COMPLETE:
321 if (!m_subtitles->IsEmpty())
322 label = StringUtils::Format(g_localizeStrings.Get(24108).c_str(), m_subtitles->Size());
324 label = g_localizeStrings.Get(24109);
327 label = g_localizeStrings.Get(24110);
332 if (label != m_status)
339 void CGUIDialogSubtitles::Download(const CFileItem &subtitle)
341 UpdateStatus(DOWNLOADING);
343 // subtitle URL should be of the form plugin://<addonid>/?param=foo¶m=bar
344 // we just append (if not already present) the action=download parameter.
345 CURL url(subtitle.GetAsUrl());
346 if (url.GetOption("action").empty())
347 url.SetOption("action", "download");
349 AddJob(new CSubtitlesJob(url, subtitle.GetLabel()));
352 void CGUIDialogSubtitles::OnDownloadComplete(const CFileItemList *items, const std::string &language)
354 if (items->IsEmpty())
356 CFileItemPtr service = GetService();
358 CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, service->GetLabel(), g_localizeStrings.Get(24113));
359 UpdateStatus(SEARCH_COMPLETE);
363 CStdString strFileName;
364 CStdString strDestPath;
365 if (g_application.CurrentFileItem().IsStack())
367 for (int i = 0; i < items->Size(); i++)
369 // check for all stack items and match to given subs, item [0] == CD1, item [1] == CD2
370 // CLog::Log(LOGDEBUG, "Stack Subs [%s} Found", vecItems[i]->GetLabel().c_str());
373 else if (StringUtils::StartsWith(g_application.CurrentFile(), "http://"))
375 strFileName = "TemporarySubs";
376 strDestPath = "special://temp/";
380 strFileName = URIUtils::GetFileName(g_application.CurrentFile());
381 if (CSettings::Get().GetBool("subtitles.savetomoviefolder"))
383 strDestPath = URIUtils::GetDirectory(g_application.CurrentFile());
384 if (!CUtil::SupportsWriteFileOperations(strDestPath))
387 if (strDestPath.empty())
389 if (CSpecialProtocol::TranslatePath("special://subtitles").empty())
390 strDestPath = "special://temp";
392 strDestPath = "special://subtitles";
395 // Extract the language and appropriate extension
396 CStdString strSubLang;
397 g_LangCodeExpander.ConvertToTwoCharCode(strSubLang, language);
398 CStdString strUrl = items->Get(0)->GetPath();
399 CStdString strSubExt = URIUtils::GetExtension(strUrl);
401 // construct subtitle path
402 URIUtils::RemoveExtension(strFileName);
403 CStdString strSubName;
404 strSubName.Format("%s.%s%s", strFileName.c_str(), strSubLang.c_str(), strSubExt.c_str());
405 CStdString strSubPath = URIUtils::AddFileToFolder(strDestPath, strSubName);
407 // and copy the file across
408 CFile::Cache(strUrl, strSubPath);
409 SetSubtitles(strSubPath);
414 void CGUIDialogSubtitles::ClearSubtitles()
416 CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_SUBLIST);
418 CSingleLock lock(m_section);
419 m_subtitles->Clear();
422 void CGUIDialogSubtitles::ClearServices()
424 CGUIMessage msg(GUI_MSG_LABEL_RESET, GetID(), CONTROL_SERVICELIST);
426 m_serviceItems->Clear();
427 m_currentService.clear();
430 void CGUIDialogSubtitles::SetSubtitles(const std::string &subtitle)
432 if (g_application.m_pPlayer)
434 int nStream = g_application.m_pPlayer->AddSubtitle(subtitle);
437 g_application.m_pPlayer->SetSubtitle(nStream);
438 g_application.m_pPlayer->SetSubtitleVisible(true);
439 CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay = 0.0f;
440 g_application.m_pPlayer->SetSubTitleDelay(0);