2 * Copyright (C) 2005-2012 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/>.
21 #include "threads/SystemClock.h"
23 #include "GUIWindowPictures.h"
26 #include "Application.h"
27 #include "GUIPassword.h"
28 #include "dialogs/GUIDialogMediaSource.h"
29 #include "GUIDialogPictureInfo.h"
30 #include "addons/GUIDialogAddonInfo.h"
31 #include "dialogs/GUIDialogProgress.h"
32 #include "playlists/PlayListFactory.h"
33 #include "PictureInfoLoader.h"
34 #include "guilib/GUIWindowManager.h"
35 #include "dialogs/GUIDialogOK.h"
36 #include "dialogs/GUIDialogYesNo.h"
37 #include "playlists/PlayList.h"
38 #include "settings/Settings.h"
39 #include "settings/GUISettings.h"
40 #include "utils/TimeUtils.h"
41 #include "utils/log.h"
42 #include "utils/URIUtils.h"
45 #define CONTROL_BTNVIEWASICONS 2
46 #define CONTROL_BTNSORTBY 3
47 #define CONTROL_BTNSORTASC 4
48 #define CONTROL_LABELFILES 12
51 using namespace XFILE;
52 using namespace PLAYLIST;
54 #define CONTROL_BTNSLIDESHOW 6
55 #define CONTROL_BTNSLIDESHOW_RECURSIVE 7
56 #define CONTROL_SHUFFLE 9
58 CGUIWindowPictures::CGUIWindowPictures(void)
59 : CGUIMediaWindow(WINDOW_PICTURES, "MyPics.xml")
61 m_thumbLoader.SetObserver(this);
62 m_slideShowStarted = false;
65 void CGUIWindowPictures::OnInitWindow()
67 CGUIMediaWindow::OnInitWindow();
68 if (m_slideShowStarted)
70 CGUIWindowSlideShow* wndw = (CGUIWindowSlideShow*)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
72 if (wndw && wndw->GetCurrentSlide())
73 URIUtils::GetDirectory(wndw->GetCurrentSlide()->GetPath(),path);
74 if (path.Equals(m_vecItems->GetPath()))
76 if (wndw && wndw->GetCurrentSlide())
77 m_viewControl.SetSelectedItem(wndw->GetCurrentSlide()->GetPath());
78 m_iSelectedItem = m_viewControl.GetSelectedItem();
80 m_slideShowStarted = false;
84 CGUIWindowPictures::~CGUIWindowPictures(void)
88 bool CGUIWindowPictures::OnMessage(CGUIMessage& message)
90 switch ( message.GetMessage() )
92 case GUI_MSG_WINDOW_DEINIT:
94 if (m_thumbLoader.IsLoading())
95 m_thumbLoader.StopThread();
97 if (message.GetParam1() != WINDOW_SLIDESHOW)
104 case GUI_MSG_WINDOW_INIT:
106 // is this the first time accessing this window?
107 if (m_vecItems->GetPath() == "?" && message.GetStringParam().IsEmpty())
108 message.SetStringParam(g_settings.m_defaultPictureSource);
110 m_dlgProgress = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
112 if (message.GetParam1() != WINDOW_SLIDESHOW)
117 if (!CGUIMediaWindow::OnMessage(message))
124 case GUI_MSG_CLICKED:
126 int iControl = message.GetSenderId();
127 if (iControl == CONTROL_BTNSLIDESHOW) // Slide Show
131 else if (iControl == CONTROL_BTNSLIDESHOW_RECURSIVE) // Recursive Slide Show
133 OnSlideShowRecursive();
135 else if (iControl == CONTROL_SHUFFLE)
137 g_guiSettings.ToggleBool("slideshow.shuffle");
140 else if (m_viewControl.HasControl(iControl)) // list/thumb control
142 int iItem = m_viewControl.GetSelectedItem();
143 int iAction = message.GetParam1();
145 // iItem is checked for validity inside these routines
146 if (iAction == ACTION_DELETE_ITEM)
148 // is delete allowed?
149 if (g_guiSettings.GetBool("filelists.allowfiledeletion"))
154 else if (iAction == ACTION_PLAYER_PLAY)
156 ShowPicture(iItem, true);
159 else if (iAction == ACTION_SHOW_INFO)
168 return CGUIMediaWindow::OnMessage(message);
171 void CGUIWindowPictures::UpdateButtons()
173 CGUIMediaWindow::UpdateButtons();
175 // Update the shuffle button
176 if (g_guiSettings.GetBool("slideshow.shuffle"))
178 CGUIMessage msg2(GUI_MSG_SELECTED, GetID(), CONTROL_SHUFFLE);
179 g_windowManager.SendMessage(msg2);
183 CGUIMessage msg2(GUI_MSG_DESELECTED, GetID(), CONTROL_SHUFFLE);
184 g_windowManager.SendMessage(msg2);
187 // check we can slideshow or recursive slideshow
188 int nFolders = m_vecItems->GetFolderCount();
189 if (nFolders == m_vecItems->Size())
191 CONTROL_DISABLE(CONTROL_BTNSLIDESHOW);
195 CONTROL_ENABLE(CONTROL_BTNSLIDESHOW);
197 if (m_guiState.get() && !m_guiState->HideParentDirItems())
199 if (m_vecItems->Size() == 0 || nFolders == 0)
201 CONTROL_DISABLE(CONTROL_BTNSLIDESHOW_RECURSIVE);
205 CONTROL_ENABLE(CONTROL_BTNSLIDESHOW_RECURSIVE);
209 void CGUIWindowPictures::OnPrepareFileItems(CFileItemList& items)
211 for (int i=0;i<items.Size();++i )
212 if (items[i]->GetLabel().Equals("folder.jpg"))
215 if (items.GetFolderCount()==items.Size() || !g_guiSettings.GetBool("pictures.usetags"))
218 // Start the music info loader thread
219 CPictureInfoLoader loader;
220 loader.SetProgressCallback(m_dlgProgress);
223 bool bShowProgress=!g_windowManager.HasModalDialog();
224 bool bProgressVisible=false;
226 unsigned int tick=XbmcThreads::SystemClockMillis();
228 while (loader.IsLoading() && m_dlgProgress && !m_dlgProgress->IsCanceled())
231 { // Do we have to init a progress dialog?
232 unsigned int elapsed=XbmcThreads::SystemClockMillis()-tick;
234 if (!bProgressVisible && elapsed>1500 && m_dlgProgress)
235 { // tag loading takes more then 1.5 secs, show a progress dialog
236 CURL url(items.GetPath());
238 m_dlgProgress->SetHeading(189);
239 m_dlgProgress->SetLine(0, 505);
240 m_dlgProgress->SetLine(1, "");
241 m_dlgProgress->SetLine(2, url.GetWithoutUserDetails());
242 m_dlgProgress->StartModal();
243 m_dlgProgress->ShowProgressBar(true);
244 bProgressVisible = true;
247 if (bProgressVisible && m_dlgProgress)
249 m_dlgProgress->Progress();
251 } // if (bShowProgress)
253 } // while (loader.IsLoading())
255 if (bProgressVisible && m_dlgProgress)
256 m_dlgProgress->Close();
259 bool CGUIWindowPictures::Update(const CStdString &strDirectory)
261 if (m_thumbLoader.IsLoading())
262 m_thumbLoader.StopThread();
264 if (!CGUIMediaWindow::Update(strDirectory))
267 m_vecItems->SetArt("thumb", "");
268 if (g_guiSettings.GetBool("pictures.generatethumbs"))
269 m_thumbLoader.Load(*m_vecItems);
270 m_vecItems->SetArt("thumb", CPictureThumbLoader::GetCachedImage(*m_vecItems, "thumb"));
275 bool CGUIWindowPictures::OnClick(int iItem)
277 if ( iItem < 0 || iItem >= (int)m_vecItems->Size() ) return true;
278 CFileItemPtr pItem = m_vecItems->Get(iItem);
280 if (pItem->IsCBZ() || pItem->IsCBR())
282 CStdString strComicPath;
284 URIUtils::CreateArchivePath(strComicPath, "zip", pItem->GetPath(), "");
286 URIUtils::CreateArchivePath(strComicPath, "rar", pItem->GetPath(), "");
288 OnShowPictureRecursive(strComicPath);
291 else if (CGUIMediaWindow::OnClick(iItem))
297 bool CGUIWindowPictures::GetDirectory(const CStdString &strDirectory, CFileItemList& items)
299 if (!CGUIMediaWindow::GetDirectory(strDirectory, items))
303 if (items.GetLabel().IsEmpty() && m_rootDir.IsSource(items.GetPath(), g_settings.GetSourcesFromType("pictures"), &label))
304 items.SetLabel(label);
309 bool CGUIWindowPictures::OnPlayMedia(int iItem)
311 if (m_vecItems->Get(iItem)->IsVideo())
312 return CGUIMediaWindow::OnPlayMedia(iItem);
314 return ShowPicture(iItem, false);
317 bool CGUIWindowPictures::ShowPicture(int iItem, bool startSlideShow)
319 if ( iItem < 0 || iItem >= (int)m_vecItems->Size() ) return false;
320 CFileItemPtr pItem = m_vecItems->Get(iItem);
321 CStdString strPicture = pItem->GetPath();
325 return MEDIA_DETECT::CAutorun::PlayDiscAskResume(m_vecItems->Get(iItem)->GetPath());
328 if (pItem->m_bIsShareOrDrive)
331 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
334 if (g_application.IsPlayingVideo())
335 g_application.StopPlaying();
338 for (int i = 0; i < (int)m_vecItems->Size();++i)
340 CFileItemPtr pItem = m_vecItems->Get(i);
341 if (!pItem->m_bIsFolder && !(URIUtils::IsRAR(pItem->GetPath()) ||
342 URIUtils::IsZIP(pItem->GetPath())) && (pItem->IsPicture() || (
343 g_guiSettings.GetBool("pictures.showvideos") &&
346 pSlideShow->Add(pItem.get());
350 if (pSlideShow->NumSlides() == 0)
353 pSlideShow->Select(strPicture);
356 pSlideShow->StartSlideShow(false);
358 m_slideShowStarted = true;
359 g_windowManager.ActivateWindow(WINDOW_SLIDESHOW);
364 void CGUIWindowPictures::OnShowPictureRecursive(const CStdString& strPath)
366 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
370 if (g_application.IsPlayingVideo())
371 g_application.StopPlaying();
372 pSlideShow->AddFromPath(strPath, true,
373 m_guiState->GetSortMethod(),
374 m_guiState->GetSortOrder());
375 if (pSlideShow->NumSlides())
377 m_slideShowStarted = true;
378 g_windowManager.ActivateWindow(WINDOW_SLIDESHOW);
383 void CGUIWindowPictures::OnSlideShowRecursive(const CStdString &strPicture)
385 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
388 CStdString strExtensions;
390 CGUIViewState* viewState=CGUIViewState::GetViewState(GetID(), items);
393 strExtensions = viewState->GetExtensions();
396 m_slideShowStarted = true;
397 pSlideShow->RunSlideShow(strPicture, true,
398 g_guiSettings.GetBool("slideshow.shuffle"),false,
399 m_guiState->GetSortMethod(),
400 m_guiState->GetSortOrder(),
405 void CGUIWindowPictures::OnSlideShowRecursive()
407 CStdString strEmpty = "";
408 OnSlideShowRecursive(m_vecItems->GetPath());
411 void CGUIWindowPictures::OnSlideShow()
413 OnSlideShow(m_vecItems->GetPath());
416 void CGUIWindowPictures::OnSlideShow(const CStdString &strPicture)
418 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
421 CStdString strExtensions;
423 CGUIViewState* viewState=CGUIViewState::GetViewState(GetID(), items);
426 strExtensions = viewState->GetExtensions();
429 m_slideShowStarted = true;
430 pSlideShow->RunSlideShow(strPicture, false ,false, false,
431 m_guiState->GetSortMethod(),
432 m_guiState->GetSortOrder(),
437 void CGUIWindowPictures::OnRegenerateThumbs()
439 if (m_thumbLoader.IsLoading()) return;
440 m_thumbLoader.SetRegenerateThumbs(true);
441 m_thumbLoader.Load(*m_vecItems);
444 void CGUIWindowPictures::GetContextButtons(int itemNumber, CContextButtons &buttons)
447 if (itemNumber >= 0 && itemNumber < m_vecItems->Size())
448 item = m_vecItems->Get(itemNumber);
450 if (item && !item->GetProperty("pluginreplacecontextitems").asBoolean())
452 if ( m_vecItems->IsVirtualDirectoryRoot() && item)
454 CGUIDialogContextMenu::GetContextButtons("pictures", item, buttons);
458 if (item && !item->GetPath().Left(14).Equals("addons://more/"))
460 if (!m_vecItems->IsPlugin() && (item->IsPlugin() || item->IsScript()))
461 buttons.Add(CONTEXT_BUTTON_INFO, 24003); // Add-on info
462 if (!(item->m_bIsFolder || item->IsZIP() || item->IsRAR() || item->IsCBZ() || item->IsCBR() || item->IsScript()))
464 buttons.Add(CONTEXT_BUTTON_INFO, 13406); // picture info
465 buttons.Add(CONTEXT_BUTTON_VIEW_SLIDESHOW, item->m_bIsFolder ? 13317 : 13422); // View Slideshow
467 if (item->m_bIsFolder)
468 buttons.Add(CONTEXT_BUTTON_RECURSIVE_SLIDESHOW, 13318); // Recursive Slideshow
470 if (!m_thumbLoader.IsLoading())
471 buttons.Add(CONTEXT_BUTTON_REFRESH_THUMBS, 13315); // Create Thumbnails
472 if (g_guiSettings.GetBool("filelists.allowfiledeletion") && !item->IsReadOnly())
474 buttons.Add(CONTEXT_BUTTON_DELETE, 117);
475 buttons.Add(CONTEXT_BUTTON_RENAME, 118);
479 if (item->IsPlugin() || item->IsScript() || m_vecItems->IsPlugin())
480 buttons.Add(CONTEXT_BUTTON_PLUGIN_SETTINGS, 1045);
483 buttons.Add(CONTEXT_BUTTON_GOTO_ROOT, 20128);
484 buttons.Add(CONTEXT_BUTTON_SWITCH_MEDIA, 523);
488 CGUIMediaWindow::GetContextButtons(itemNumber, buttons);
489 if (item && !item->GetProperty("pluginreplacecontextitems").asBoolean())
490 buttons.Add(CONTEXT_BUTTON_SETTINGS, 5); // Settings
493 bool CGUIWindowPictures::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
495 CFileItemPtr item = (itemNumber >= 0 && itemNumber < m_vecItems->Size()) ? m_vecItems->Get(itemNumber) : CFileItemPtr();
496 if (m_vecItems->IsVirtualDirectoryRoot() && item)
498 if (CGUIDialogContextMenu::OnContextButton("pictures", item, button))
506 case CONTEXT_BUTTON_VIEW_SLIDESHOW:
507 if (item && item->m_bIsFolder)
508 OnSlideShow(item->GetPath());
510 ShowPicture(itemNumber, true);
512 case CONTEXT_BUTTON_RECURSIVE_SLIDESHOW:
514 OnSlideShowRecursive(item->GetPath());
516 case CONTEXT_BUTTON_INFO:
519 case CONTEXT_BUTTON_REFRESH_THUMBS:
520 OnRegenerateThumbs();
522 case CONTEXT_BUTTON_DELETE:
523 OnDeleteItem(itemNumber);
525 case CONTEXT_BUTTON_RENAME:
526 OnRenameItem(itemNumber);
528 case CONTEXT_BUTTON_SETTINGS:
529 g_windowManager.ActivateWindow(WINDOW_SETTINGS_MYPICTURES);
531 case CONTEXT_BUTTON_GOTO_ROOT:
534 case CONTEXT_BUTTON_SWITCH_MEDIA:
535 CGUIDialogContextMenu::SwitchMedia("pictures", m_vecItems->GetPath());
540 return CGUIMediaWindow::OnContextButton(itemNumber, button);
543 void CGUIWindowPictures::OnItemLoaded(CFileItem *pItem)
545 CPictureThumbLoader::ProcessFoldersAndArchives(pItem);
548 void CGUIWindowPictures::LoadPlayList(const CStdString& strPlayList)
550 CLog::Log(LOGDEBUG,"CGUIWindowPictures::LoadPlayList()... converting playlist into slideshow: %s", strPlayList.c_str());
551 auto_ptr<CPlayList> pPlayList (CPlayListFactory::Create(strPlayList));
552 if ( NULL != pPlayList.get())
554 if (!pPlayList->Load(strPlayList))
556 CGUIDialogOK::ShowAndGetInput(6, 0, 477, 0);
557 return ; //hmmm unable to load playlist?
561 CPlayList playlist = *pPlayList;
562 if (playlist.size() > 0)
565 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
568 if (g_application.IsPlayingVideo())
569 g_application.StopPlaying();
571 // convert playlist items into slideshow items
573 for (int i = 0; i < (int)playlist.size(); ++i)
575 CFileItemPtr pItem = playlist[i];
576 //CLog::Log(LOGDEBUG,"-- playlist item: %s", pItem->GetPath().c_str());
577 if (pItem->IsPicture() && !(pItem->IsZIP() || pItem->IsRAR() || pItem->IsCBZ() || pItem->IsCBR()))
578 pSlideShow->Add(pItem.get());
581 // start slideshow if there are items
582 pSlideShow->StartSlideShow();
583 if (pSlideShow->NumSlides())
584 g_windowManager.ActivateWindow(WINDOW_SLIDESHOW);
588 void CGUIWindowPictures::OnInfo(int itemNumber)
590 CFileItemPtr item = (itemNumber >= 0 && itemNumber < m_vecItems->Size()) ? m_vecItems->Get(itemNumber) : CFileItemPtr();
593 if (!m_vecItems->IsPlugin() && (item->IsPlugin() || item->IsScript()))
595 CGUIDialogAddonInfo::ShowForItem(item);
598 if (item->m_bIsFolder || item->IsZIP() || item->IsRAR() || item->IsCBZ() || item->IsCBR() || !item->IsPicture())
600 CGUIDialogPictureInfo *pictureInfo = (CGUIDialogPictureInfo *)g_windowManager.GetWindow(WINDOW_DIALOG_PICTURE_INFO);
603 pictureInfo->SetPicture(item.get());
604 pictureInfo->DoModal();
608 CStdString CGUIWindowPictures::GetStartFolder(const CStdString &dir)
610 if (dir.Equals("Plugins") || dir.Equals("Addons"))
611 return "addons://sources/image/";
615 m_rootDir.GetSources(shares);
616 bool bIsSourceName = false;
617 int iIndex = CUtil::GetMatchingSource(dir, shares, bIsSourceName);
620 if (iIndex < (int)shares.size() && shares[iIndex].m_iHasLock == 2)
622 CFileItem item(shares[iIndex]);
623 if (!g_passwordManager.IsItemUnlocked(&item,"pictures"))
627 return shares[iIndex].strPath;
630 return CGUIMediaWindow::GetStartFolder(dir);