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 "threads/SystemClock.h"
24 #include "PluginDirectory.h"
25 #include "utils/URIUtils.h"
26 #include "addons/AddonManager.h"
27 #include "addons/AddonInstaller.h"
28 #include "addons/IAddon.h"
29 #include "interfaces/generic/ScriptInvocationManager.h"
30 #include "threads/SingleLock.h"
31 #include "guilib/GUIWindowManager.h"
32 #include "dialogs/GUIDialogProgress.h"
33 #include "settings/Settings.h"
35 #include "video/VideoInfoTag.h"
36 #include "guilib/LocalizeStrings.h"
37 #include "utils/log.h"
38 #include "utils/TimeUtils.h"
39 #include "utils/StringUtils.h"
40 #include "ApplicationMessenger.h"
41 #include "Application.h"
44 using namespace XFILE;
46 using namespace ADDON;
48 map<int, CPluginDirectory *> CPluginDirectory::globalHandles;
49 int CPluginDirectory::handleCounter = 0;
50 CCriticalSection CPluginDirectory::m_handleLock;
52 CPluginDirectory::CPluginDirectory()
54 m_listItems = new CFileItemList;
55 m_fileResult = new CFileItem;
58 CPluginDirectory::~CPluginDirectory(void)
64 int CPluginDirectory::getNewHandle(CPluginDirectory *cp)
66 CSingleLock lock(m_handleLock);
67 int handle = ++handleCounter;
68 globalHandles[handle] = cp;
72 void CPluginDirectory::removeHandle(int handle)
74 CSingleLock lock(m_handleLock);
75 if (!globalHandles.erase(handle))
76 CLog::Log(LOGWARNING, "Attempt to erase invalid handle %i", handle);
79 CPluginDirectory *CPluginDirectory::dirFromHandle(int handle)
81 CSingleLock lock(m_handleLock);
82 map<int, CPluginDirectory *>::iterator i = globalHandles.find(handle);
83 if (i != globalHandles.end())
85 CLog::Log(LOGWARNING, "Attempt to use invalid handle %i", handle);
89 bool CPluginDirectory::StartScript(const CStdString& strPath, bool retrievingDir)
93 if (!CAddonMgr::Get().GetAddon(url.GetHostName(), m_addon, ADDON_UNKNOWN) &&
94 !CAddonInstaller::Get().PromptForInstall(url.GetHostName(), m_addon))
96 CLog::Log(LOGERROR, "Unable to find plugin %s", url.GetHostName().c_str());
101 CStdString options = url.GetOptions();
102 URIUtils::RemoveSlashAtEnd(options); // This MAY kill some scripts (eg though with a URL ending with a slash), but
103 // is needed for all others, as XBMC adds slashes to "folders"
104 url.SetOptions(""); // do this because we can then use the url to generate the basepath
105 // which is passed to the plugin (and represents the share)
107 CStdString basePath(url.Get());
108 // reset our wait event, and grab a new handle
109 m_fetchComplete.Reset();
110 int handle = getNewHandle(this);
112 // clear out our status variables
113 m_fileResult->Reset();
114 m_listItems->Clear();
115 m_listItems->SetPath(strPath);
116 m_listItems->SetLabel(m_addon->Name());
121 // setup our parameters to send the script
122 CStdString strHandle = StringUtils::Format("%i", handle);
124 argv.push_back(basePath);
125 argv.push_back(strHandle);
126 argv.push_back(options);
129 CLog::Log(LOGDEBUG, "%s - calling plugin %s('%s','%s','%s')", __FUNCTION__, m_addon->Name().c_str(), argv[0].c_str(), argv[1].c_str(), argv[2].c_str());
130 bool success = false;
131 CStdString file = m_addon->LibPath();
132 int id = CScriptInvocationManager::Get().Execute(file, m_addon, argv);
134 { // wait for our script to finish
135 CStdString scriptName = m_addon->Name();
136 success = WaitOnScriptResult(file, id, scriptName, retrievingDir);
139 CLog::Log(LOGERROR, "Unable to run plugin %s", m_addon->Name().c_str());
142 removeHandle(handle);
147 bool CPluginDirectory::GetPluginResult(const CStdString& strPath, CFileItem &resultItem)
150 CPluginDirectory* newDir = new CPluginDirectory();
152 bool success = newDir->StartScript(strPath, false);
155 { // update the play path and metadata, saving the old one as needed
156 if (!resultItem.HasProperty("original_listitem_url"))
157 resultItem.SetProperty("original_listitem_url", resultItem.GetPath());
158 resultItem.SetPath(newDir->m_fileResult->GetPath());
159 resultItem.SetMimeType(newDir->m_fileResult->GetMimeType());
160 resultItem.UpdateInfo(*newDir->m_fileResult);
161 if (newDir->m_fileResult->HasVideoInfoTag() && newDir->m_fileResult->GetVideoInfoTag()->m_resumePoint.IsSet())
162 resultItem.m_lStartOffset = STARTOFFSET_RESUME; // resume point set in the resume item, so force resume
169 bool CPluginDirectory::AddItem(int handle, const CFileItem *item, int totalItems)
171 CSingleLock lock(m_handleLock);
172 CPluginDirectory *dir = dirFromHandle(handle);
176 CFileItemPtr pItem(new CFileItem(*item));
177 dir->m_listItems->Add(pItem);
178 dir->m_totalItems = totalItems;
180 return !dir->m_cancelled;
183 bool CPluginDirectory::AddItems(int handle, const CFileItemList *items, int totalItems)
185 CSingleLock lock(m_handleLock);
186 CPluginDirectory *dir = dirFromHandle(handle);
190 CFileItemList pItemList;
191 pItemList.Copy(*items);
192 dir->m_listItems->Append(pItemList);
193 dir->m_totalItems = totalItems;
195 return !dir->m_cancelled;
198 void CPluginDirectory::EndOfDirectory(int handle, bool success, bool replaceListing, bool cacheToDisc)
200 CSingleLock lock(m_handleLock);
201 CPluginDirectory *dir = dirFromHandle(handle);
206 dir->m_listItems->SetCacheToDisc(cacheToDisc ? CFileItemList::CACHE_IF_SLOW : CFileItemList::CACHE_NEVER);
208 dir->m_success = success;
209 dir->m_listItems->SetReplaceListing(replaceListing);
211 if (!dir->m_listItems->HasSortDetails())
212 dir->m_listItems->AddSortMethod(SortByNone, 552, LABEL_MASKS("%L", "%D"));
214 // set the event to mark that we're done
215 dir->m_fetchComplete.Set();
218 void CPluginDirectory::AddSortMethod(int handle, SORT_METHOD sortMethod, const CStdString &label2Mask)
220 CSingleLock lock(m_handleLock);
221 CPluginDirectory *dir = dirFromHandle(handle);
225 // TODO: Add all sort methods and fix which labels go on the right or left
228 case SORT_METHOD_LABEL:
229 case SORT_METHOD_LABEL_IGNORE_THE:
231 dir->m_listItems->AddSortMethod(SortByLabel, 551, LABEL_MASKS("%T", label2Mask), CSettings::Get().GetBool("filelists.ignorethewhensorting") ? SortAttributeIgnoreArticle : SortAttributeNone);
234 case SORT_METHOD_TITLE:
235 case SORT_METHOD_TITLE_IGNORE_THE:
237 dir->m_listItems->AddSortMethod(SortByTitle, 556, LABEL_MASKS("%T", label2Mask), CSettings::Get().GetBool("filelists.ignorethewhensorting") ? SortAttributeIgnoreArticle : SortAttributeNone);
240 case SORT_METHOD_ARTIST:
241 case SORT_METHOD_ARTIST_IGNORE_THE:
243 dir->m_listItems->AddSortMethod(SortByArtist, 557, LABEL_MASKS("%T", "%A"), CSettings::Get().GetBool("filelists.ignorethewhensorting") ? SortAttributeIgnoreArticle : SortAttributeNone);
246 case SORT_METHOD_ALBUM:
247 case SORT_METHOD_ALBUM_IGNORE_THE:
249 dir->m_listItems->AddSortMethod(SortByAlbum, 558, LABEL_MASKS("%T", "%B"), CSettings::Get().GetBool("filelists.ignorethewhensorting") ? SortAttributeIgnoreArticle : SortAttributeNone);
252 case SORT_METHOD_DATE:
254 dir->m_listItems->AddSortMethod(SortByDate, 552, LABEL_MASKS("%T", "%J"));
257 case SORT_METHOD_BITRATE:
259 dir->m_listItems->AddSortMethod(SortByBitrate, 623, LABEL_MASKS("%T", "%X"));
262 case SORT_METHOD_SIZE:
264 dir->m_listItems->AddSortMethod(SortBySize, 553, LABEL_MASKS("%T", "%I"));
267 case SORT_METHOD_FILE:
269 dir->m_listItems->AddSortMethod(SortByFile, 561, LABEL_MASKS("%T", label2Mask));
272 case SORT_METHOD_TRACKNUM:
274 dir->m_listItems->AddSortMethod(SortByTrackNumber, 554, LABEL_MASKS("[%N. ]%T", label2Mask));
277 case SORT_METHOD_DURATION:
278 case SORT_METHOD_VIDEO_RUNTIME:
280 dir->m_listItems->AddSortMethod(SortByTime, 180, LABEL_MASKS("%T", "%D"));
283 case SORT_METHOD_VIDEO_RATING:
284 case SORT_METHOD_SONG_RATING:
286 dir->m_listItems->AddSortMethod(SortByRating, 563, LABEL_MASKS("%T", "%R"));
289 case SORT_METHOD_YEAR:
291 dir->m_listItems->AddSortMethod(SortByYear, 562, LABEL_MASKS("%T", "%Y"));
294 case SORT_METHOD_GENRE:
296 dir->m_listItems->AddSortMethod(SortByGenre, 515, LABEL_MASKS("%T", "%G"));
299 case SORT_METHOD_COUNTRY:
301 dir->m_listItems->AddSortMethod(SortByCountry, 574, LABEL_MASKS("%T", "%G"));
304 case SORT_METHOD_VIDEO_TITLE:
306 dir->m_listItems->AddSortMethod(SortByTitle, 369, LABEL_MASKS("%T", label2Mask));
309 case SORT_METHOD_VIDEO_SORT_TITLE:
310 case SORT_METHOD_VIDEO_SORT_TITLE_IGNORE_THE:
312 dir->m_listItems->AddSortMethod(SortBySortTitle, 556, LABEL_MASKS("%T", label2Mask), CSettings::Get().GetBool("filelists.ignorethewhensorting") ? SortAttributeIgnoreArticle : SortAttributeNone);
315 case SORT_METHOD_MPAA_RATING:
317 dir->m_listItems->AddSortMethod(SortByMPAA, 20074, LABEL_MASKS("%T", "%O"));
320 case SORT_METHOD_STUDIO:
321 case SORT_METHOD_STUDIO_IGNORE_THE:
323 dir->m_listItems->AddSortMethod(SortByStudio, 572, LABEL_MASKS("%T", "%U"), CSettings::Get().GetBool("filelists.ignorethewhensorting") ? SortAttributeIgnoreArticle : SortAttributeNone);
326 case SORT_METHOD_PROGRAM_COUNT:
328 dir->m_listItems->AddSortMethod(SortByProgramCount, 567, LABEL_MASKS("%T", "%C"));
331 case SORT_METHOD_UNSORTED:
333 dir->m_listItems->AddSortMethod(SortByNone, 571, LABEL_MASKS("%T", label2Mask));
336 case SORT_METHOD_NONE:
338 dir->m_listItems->AddSortMethod(SortByNone, 552, LABEL_MASKS("%T", label2Mask));
341 case SORT_METHOD_DRIVE_TYPE:
343 dir->m_listItems->AddSortMethod(SortByDriveType, 564, LABEL_MASKS()); // Preformatted
346 case SORT_METHOD_PLAYLIST_ORDER:
348 CStdString strTrackLeft=CSettings::Get().GetString("musicfiles.trackformat");
349 CStdString strTrackRight=CSettings::Get().GetString("musicfiles.trackformatright");
351 dir->m_listItems->AddSortMethod(SortByPlaylistOrder, 559, LABEL_MASKS(strTrackLeft, strTrackRight));
354 case SORT_METHOD_EPISODE:
356 dir->m_listItems->AddSortMethod(SortByEpisodeNumber, 20359, LABEL_MASKS("%E. %T","%R"));
359 case SORT_METHOD_PRODUCTIONCODE:
361 //dir->m_listItems.AddSortMethod(SORT_METHOD_PRODUCTIONCODE,20368,LABEL_MASKS("%E. %T","%P", "%E. %T","%P"));
362 dir->m_listItems->AddSortMethod(SortByProductionCode, 20368, LABEL_MASKS("%H. %T","%P", "%H. %T","%P"));
365 case SORT_METHOD_LISTENERS:
367 dir->m_listItems->AddSortMethod(SortByListeners, 20455, LABEL_MASKS("%T","%W"));
370 case SORT_METHOD_DATEADDED:
372 dir->m_listItems->AddSortMethod(SortByDateAdded, 570, LABEL_MASKS("%T", "%a"));
375 case SORT_METHOD_FULLPATH:
377 dir->m_listItems->AddSortMethod(SortByPath, 573, LABEL_MASKS("%T", label2Mask));
380 case SORT_METHOD_LABEL_IGNORE_FOLDERS:
382 dir->m_listItems->AddSortMethod(SortByLabel, SortAttributeIgnoreFolders, 551, LABEL_MASKS("%T", label2Mask));
385 case SORT_METHOD_LASTPLAYED:
387 dir->m_listItems->AddSortMethod(SortByLastPlayed, 568, LABEL_MASKS("%T", "%G"));
390 case SORT_METHOD_PLAYCOUNT:
392 dir->m_listItems->AddSortMethod(SortByPlaycount, 567, LABEL_MASKS("%T", "%V"));
395 case SORT_METHOD_CHANNEL:
397 dir->m_listItems->AddSortMethod(SortByChannel, 19029, LABEL_MASKS("%T", label2Mask));
406 bool CPluginDirectory::GetDirectory(const CStdString& strPath, CFileItemList& items)
410 bool success = StartScript(strPath, true);
412 // append the items to the list
413 items.Assign(*m_listItems, true); // true to keep the current items
414 m_listItems->Clear();
418 bool CPluginDirectory::RunScriptWithParams(const CStdString& strPath)
421 if (url.GetHostName().empty()) // called with no script - should never happen
425 if (!CAddonMgr::Get().GetAddon(url.GetHostName(), addon, ADDON_PLUGIN) && !CAddonInstaller::Get().PromptForInstall(url.GetHostName(), addon))
427 CLog::Log(LOGERROR, "Unable to find plugin %s", url.GetHostName().c_str());
432 CStdString options = url.GetOptions();
433 URIUtils::RemoveSlashAtEnd(options); // This MAY kill some scripts (eg though with a URL ending with a slash), but
434 // is needed for all others, as XBMC adds slashes to "folders"
435 url.SetOptions(""); // do this because we can then use the url to generate the basepath
436 // which is passed to the plugin (and represents the share)
438 CStdString basePath(url.Get());
440 // setup our parameters to send the script
441 CStdString strHandle = StringUtils::Format("%i", -1);
443 argv.push_back(basePath);
444 argv.push_back(strHandle);
445 argv.push_back(options);
448 CLog::Log(LOGDEBUG, "%s - calling plugin %s('%s','%s','%s')", __FUNCTION__, addon->Name().c_str(), argv[0].c_str(), argv[1].c_str(), argv[2].c_str());
449 if (CScriptInvocationManager::Get().Execute(addon->LibPath(), addon, argv) >= 0)
452 CLog::Log(LOGERROR, "Unable to run plugin %s", addon->Name().c_str());
457 bool CPluginDirectory::WaitOnScriptResult(const CStdString &scriptPath, int scriptId, const CStdString &scriptName, bool retrievingDir)
459 const unsigned int timeBeforeProgressBar = 1500;
460 const unsigned int timeToKillScript = 1000;
462 unsigned int startTime = XbmcThreads::SystemClockMillis();
463 CGUIDialogProgress *progressBar = NULL;
464 bool cancelled = false;
465 bool inMainAppThread = g_application.IsCurrentThread();
467 CLog::Log(LOGDEBUG, "%s - waiting on the %s (id=%d) plugin...", __FUNCTION__, scriptName.c_str(), scriptId);
471 CSingleExit ex(g_graphicsContext);
472 // check if the python script is finished
473 if (m_fetchComplete.WaitMSec(20))
474 { // python has returned
475 CLog::Log(LOGDEBUG, "%s- plugin returned %s", __FUNCTION__, m_success ? "successfully" : "failure");
479 // check our script is still running
480 if (!CScriptInvocationManager::Get().IsRunning(scriptId))
481 { // check whether we exited normally
482 if (!m_fetchComplete.WaitMSec(0))
483 { // python didn't return correctly
484 CLog::Log(LOGDEBUG, " %s - plugin exited prematurely - terminating", __FUNCTION__);
490 // check whether we should pop up the progress dialog
491 if (!retrievingDir && !progressBar && XbmcThreads::SystemClockMillis() - startTime > timeBeforeProgressBar)
492 { // loading takes more then 1.5 secs, show a progress dialog
493 progressBar = (CGUIDialogProgress *)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
495 // if script has shown progressbar don't override it
496 if (progressBar && progressBar->IsActive())
498 startTime = XbmcThreads::SystemClockMillis();
504 progressBar->SetHeading(scriptName);
505 progressBar->SetLine(0, retrievingDir ? 1040 : 10214);
506 progressBar->SetLine(1, "");
507 progressBar->SetLine(2, "");
508 progressBar->ShowProgressBar(retrievingDir);
509 progressBar->StartModal();
514 { // update the progress bar and check for user cancel
515 progressBar->Progress();
516 if (progressBar->IsCanceled())
517 { // user has cancelled our process - cancel our process
521 else // if the progressBar exists and we call StartModal or Progress we get the
522 // ProcessRenderLoop call anyway.
524 g_windowManager.ProcessRenderLoop();
526 if (!cancelled && m_cancelled)
529 startTime = XbmcThreads::SystemClockMillis();
531 if ((cancelled && XbmcThreads::SystemClockMillis() - startTime > timeToKillScript) || g_application.m_bStop)
532 { // cancel our script
533 if (scriptId != -1 && CScriptInvocationManager::Get().IsRunning(scriptId))
535 CLog::Log(LOGDEBUG, "%s- cancelling plugin %s (id=%d)", __FUNCTION__, scriptName.c_str(), scriptId);
536 CScriptInvocationManager::Get().Stop(scriptId);
543 CApplicationMessenger::Get().Close(progressBar, false, false);
545 return !cancelled && m_success;
548 void CPluginDirectory::SetResolvedUrl(int handle, bool success, const CFileItem *resultItem)
550 CSingleLock lock(m_handleLock);
551 CPluginDirectory *dir = dirFromHandle(handle);
555 dir->m_success = success;
556 *dir->m_fileResult = *resultItem;
558 // set the event to mark that we're done
559 dir->m_fetchComplete.Set();
562 CStdString CPluginDirectory::GetSetting(int handle, const CStdString &strID)
564 CSingleLock lock(m_handleLock);
565 CPluginDirectory *dir = dirFromHandle(handle);
566 if(dir && dir->m_addon)
567 return dir->m_addon->GetSetting(strID);
572 void CPluginDirectory::SetSetting(int handle, const CStdString &strID, const CStdString &value)
574 CSingleLock lock(m_handleLock);
575 CPluginDirectory *dir = dirFromHandle(handle);
576 if(dir && dir->m_addon)
577 dir->m_addon->UpdateSetting(strID, value);
580 void CPluginDirectory::SetContent(int handle, const CStdString &strContent)
582 CSingleLock lock(m_handleLock);
583 CPluginDirectory *dir = dirFromHandle(handle);
585 dir->m_listItems->SetContent(strContent);
588 void CPluginDirectory::SetProperty(int handle, const CStdString &strProperty, const CStdString &strValue)
590 CSingleLock lock(m_handleLock);
591 CPluginDirectory *dir = dirFromHandle(handle);
594 if (strProperty == "fanart_image")
595 dir->m_listItems->SetArt("fanart", strValue);
597 dir->m_listItems->SetProperty(strProperty, strValue);
600 void CPluginDirectory::CancelDirectory()
605 float CPluginDirectory::GetProgress() const
607 if (m_totalItems > 0)
608 return (m_listItems->Size() * 100.0f) / m_totalItems;