2 * Copyright (C) 2005-2008 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, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "threads/SystemClock.h"
24 #include "GUIWindowPictures.h"
28 #include "Application.h"
29 #include "GUIPassword.h"
30 #include "dialogs/GUIDialogMediaSource.h"
31 #include "GUIDialogPictureInfo.h"
32 #include "dialogs/GUIDialogProgress.h"
33 #include "playlists/PlayListFactory.h"
34 #include "PictureInfoLoader.h"
35 #include "guilib/GUIWindowManager.h"
36 #include "dialogs/GUIDialogOK.h"
37 #include "dialogs/GUIDialogYesNo.h"
38 #include "playlists/PlayList.h"
39 #include "settings/Settings.h"
40 #include "settings/GUISettings.h"
41 #include "utils/TimeUtils.h"
42 #include "utils/log.h"
43 #include "utils/URIUtils.h"
46 #define CONTROL_BTNVIEWASICONS 2
47 #define CONTROL_BTNSORTBY 3
48 #define CONTROL_BTNSORTASC 4
49 #define CONTROL_LABELFILES 12
52 using namespace XFILE;
53 using namespace PLAYLIST;
55 #define CONTROL_BTNSLIDESHOW 6
56 #define CONTROL_BTNSLIDESHOW_RECURSIVE 7
57 #define CONTROL_SHUFFLE 9
59 CGUIWindowPictures::CGUIWindowPictures(void)
60 : CGUIMediaWindow(WINDOW_PICTURES, "MyPics.xml")
62 m_thumbLoader.SetObserver(this);
63 m_slideShowStarted = false;
66 void CGUIWindowPictures::OnInitWindow()
68 CGUIMediaWindow::OnInitWindow();
69 if (m_slideShowStarted)
71 CGUIWindowSlideShow* wndw = (CGUIWindowSlideShow*)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
73 if (wndw && wndw->GetCurrentSlide())
74 URIUtils::GetDirectory(wndw->GetCurrentSlide()->GetPath(),path);
75 if (path.Equals(m_vecItems->GetPath()))
77 if (wndw && wndw->GetCurrentSlide())
78 m_viewControl.SetSelectedItem(wndw->GetCurrentSlide()->GetPath());
79 m_iSelectedItem = m_viewControl.GetSelectedItem();
81 m_slideShowStarted = false;
85 CGUIWindowPictures::~CGUIWindowPictures(void)
89 bool CGUIWindowPictures::OnMessage(CGUIMessage& message)
91 switch ( message.GetMessage() )
93 case GUI_MSG_WINDOW_DEINIT:
95 if (m_thumbLoader.IsLoading())
96 m_thumbLoader.StopThread();
98 if (message.GetParam1() != WINDOW_SLIDESHOW)
105 case GUI_MSG_WINDOW_INIT:
107 // is this the first time accessing this window?
108 if (m_vecItems->GetPath() == "?" && message.GetStringParam().IsEmpty())
109 message.SetStringParam(g_settings.m_defaultPictureSource);
111 m_dlgProgress = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
113 if (message.GetParam1() != WINDOW_SLIDESHOW)
118 if (!CGUIMediaWindow::OnMessage(message))
125 case GUI_MSG_CLICKED:
127 int iControl = message.GetSenderId();
128 if (iControl == CONTROL_BTNSLIDESHOW) // Slide Show
132 else if (iControl == CONTROL_BTNSLIDESHOW_RECURSIVE) // Recursive Slide Show
134 OnSlideShowRecursive();
136 else if (iControl == CONTROL_SHUFFLE)
138 g_guiSettings.ToggleBool("slideshow.shuffle");
141 else if (m_viewControl.HasControl(iControl)) // list/thumb control
143 int iItem = m_viewControl.GetSelectedItem();
144 int iAction = message.GetParam1();
146 // iItem is checked for validity inside these routines
147 if (iAction == ACTION_DELETE_ITEM)
149 // is delete allowed?
150 if (g_guiSettings.GetBool("filelists.allowfiledeletion"))
155 else if (iAction == ACTION_PLAYER_PLAY)
157 ShowPicture(iItem, true);
160 else if (iAction == ACTION_SHOW_INFO)
169 return CGUIMediaWindow::OnMessage(message);
172 void CGUIWindowPictures::UpdateButtons()
174 CGUIMediaWindow::UpdateButtons();
176 // Update the shuffle button
177 if (g_guiSettings.GetBool("slideshow.shuffle"))
179 CGUIMessage msg2(GUI_MSG_SELECTED, GetID(), CONTROL_SHUFFLE);
180 g_windowManager.SendMessage(msg2);
184 CGUIMessage msg2(GUI_MSG_DESELECTED, GetID(), CONTROL_SHUFFLE);
185 g_windowManager.SendMessage(msg2);
188 // check we can slideshow or recursive slideshow
189 int nFolders = m_vecItems->GetFolderCount();
190 if (nFolders == m_vecItems->Size())
192 CONTROL_DISABLE(CONTROL_BTNSLIDESHOW);
196 CONTROL_ENABLE(CONTROL_BTNSLIDESHOW);
198 if (m_guiState.get() && !m_guiState->HideParentDirItems())
200 if (m_vecItems->Size() == 0 || nFolders == 0)
202 CONTROL_DISABLE(CONTROL_BTNSLIDESHOW_RECURSIVE);
206 CONTROL_ENABLE(CONTROL_BTNSLIDESHOW_RECURSIVE);
210 void CGUIWindowPictures::OnPrepareFileItems(CFileItemList& items)
212 for (int i=0;i<items.Size();++i )
213 if (items[i]->GetLabel().Equals("folder.jpg"))
216 if (items.GetFolderCount()==items.Size() || !g_guiSettings.GetBool("pictures.usetags"))
219 // Start the music info loader thread
220 CPictureInfoLoader loader;
221 loader.SetProgressCallback(m_dlgProgress);
224 bool bShowProgress=!g_windowManager.HasModalDialog();
225 bool bProgressVisible=false;
227 unsigned int tick=XbmcThreads::SystemClockMillis();
229 while (loader.IsLoading() && m_dlgProgress && !m_dlgProgress->IsCanceled())
232 { // Do we have to init a progress dialog?
233 unsigned int elapsed=XbmcThreads::SystemClockMillis()-tick;
235 if (!bProgressVisible && elapsed>1500 && m_dlgProgress)
236 { // tag loading takes more then 1.5 secs, show a progress dialog
237 CURL url(items.GetPath());
239 m_dlgProgress->SetHeading(189);
240 m_dlgProgress->SetLine(0, 505);
241 m_dlgProgress->SetLine(1, "");
242 m_dlgProgress->SetLine(2, url.GetWithoutUserDetails());
243 m_dlgProgress->StartModal();
244 m_dlgProgress->ShowProgressBar(true);
245 bProgressVisible = true;
248 if (bProgressVisible && m_dlgProgress)
250 m_dlgProgress->Progress();
252 } // if (bShowProgress)
254 } // while (loader.IsLoading())
256 if (bProgressVisible && m_dlgProgress)
257 m_dlgProgress->Close();
260 bool CGUIWindowPictures::Update(const CStdString &strDirectory)
262 if (m_thumbLoader.IsLoading())
263 m_thumbLoader.StopThread();
265 if (!CGUIMediaWindow::Update(strDirectory))
268 m_vecItems->SetThumbnailImage("");
269 if (g_guiSettings.GetBool("pictures.generatethumbs"))
270 m_thumbLoader.Load(*m_vecItems);
271 m_vecItems->SetThumbnailImage(CPictureThumbLoader::GetCachedImage(*m_vecItems, "thumb"));
276 bool CGUIWindowPictures::OnClick(int iItem)
278 if ( iItem < 0 || iItem >= (int)m_vecItems->Size() ) return true;
279 CFileItemPtr pItem = m_vecItems->Get(iItem);
281 if (pItem->IsCBZ() || pItem->IsCBR())
283 CStdString strComicPath;
285 URIUtils::CreateArchivePath(strComicPath, "zip", pItem->GetPath(), "");
287 URIUtils::CreateArchivePath(strComicPath, "rar", pItem->GetPath(), "");
289 OnShowPictureRecursive(strComicPath);
292 else if (CGUIMediaWindow::OnClick(iItem))
298 bool CGUIWindowPictures::GetDirectory(const CStdString &strDirectory, CFileItemList& items)
300 if (!CGUIMediaWindow::GetDirectory(strDirectory, items))
304 if (items.GetLabel().IsEmpty() && m_rootDir.IsSource(items.GetPath(), g_settings.GetSourcesFromType("pictures"), &label))
305 items.SetLabel(label);
310 bool CGUIWindowPictures::OnPlayMedia(int iItem)
312 if (m_vecItems->Get(iItem)->IsVideo())
313 return CGUIMediaWindow::OnPlayMedia(iItem);
315 return ShowPicture(iItem, false);
318 bool CGUIWindowPictures::ShowPicture(int iItem, bool startSlideShow)
320 if ( iItem < 0 || iItem >= (int)m_vecItems->Size() ) return false;
321 CFileItemPtr pItem = m_vecItems->Get(iItem);
322 CStdString strPicture = pItem->GetPath();
326 return MEDIA_DETECT::CAutorun::PlayDiscAskResume(m_vecItems->Get(iItem)->GetPath());
329 if (pItem->m_bIsShareOrDrive)
332 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
335 if (g_application.IsPlayingVideo())
336 g_application.StopPlaying();
339 for (int i = 0; i < (int)m_vecItems->Size();++i)
341 CFileItemPtr pItem = m_vecItems->Get(i);
342 if (!pItem->m_bIsFolder && !(URIUtils::IsRAR(pItem->GetPath()) ||
343 URIUtils::IsZIP(pItem->GetPath())) && (pItem->IsPicture() || (
344 g_guiSettings.GetBool("pictures.showvideos") &&
347 pSlideShow->Add(pItem.get());
351 if (pSlideShow->NumSlides() == 0)
354 pSlideShow->Select(strPicture);
357 pSlideShow->StartSlideShow(false);
359 m_slideShowStarted = true;
360 g_windowManager.ActivateWindow(WINDOW_SLIDESHOW);
365 void CGUIWindowPictures::OnShowPictureRecursive(const CStdString& strPath)
367 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
371 if (g_application.IsPlayingVideo())
372 g_application.StopPlaying();
373 pSlideShow->AddFromPath(strPath, true,
374 m_guiState->GetSortMethod(),
375 m_guiState->GetSortOrder());
376 if (pSlideShow->NumSlides())
378 m_slideShowStarted = true;
379 g_windowManager.ActivateWindow(WINDOW_SLIDESHOW);
384 void CGUIWindowPictures::OnSlideShowRecursive(const CStdString &strPicture)
386 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
389 CStdString strExtensions;
391 CGUIViewState* viewState=CGUIViewState::GetViewState(GetID(), items);
394 strExtensions = viewState->GetExtensions();
397 m_slideShowStarted = true;
398 pSlideShow->RunSlideShow(strPicture, true,
399 g_guiSettings.GetBool("slideshow.shuffle"),false,
400 m_guiState->GetSortMethod(),
401 m_guiState->GetSortOrder(),
406 void CGUIWindowPictures::OnSlideShowRecursive()
408 CStdString strEmpty = "";
409 OnSlideShowRecursive(m_vecItems->GetPath());
412 void CGUIWindowPictures::OnSlideShow()
414 OnSlideShow(m_vecItems->GetPath());
417 void CGUIWindowPictures::OnSlideShow(const CStdString &strPicture)
419 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
422 CStdString strExtensions;
424 CGUIViewState* viewState=CGUIViewState::GetViewState(GetID(), items);
427 strExtensions = viewState->GetExtensions();
430 m_slideShowStarted = true;
431 pSlideShow->RunSlideShow(strPicture, false ,false, false,
432 m_guiState->GetSortMethod(),
433 m_guiState->GetSortOrder(),
438 void CGUIWindowPictures::OnRegenerateThumbs()
440 if (m_thumbLoader.IsLoading()) return;
441 m_thumbLoader.SetRegenerateThumbs(true);
442 m_thumbLoader.Load(*m_vecItems);
445 void CGUIWindowPictures::GetContextButtons(int itemNumber, CContextButtons &buttons)
448 if (itemNumber >= 0 && itemNumber < m_vecItems->Size())
449 item = m_vecItems->Get(itemNumber);
451 if (item && !item->GetProperty("pluginreplacecontextitems").asBoolean())
453 if ( m_vecItems->IsVirtualDirectoryRoot() && item)
455 CGUIDialogContextMenu::GetContextButtons("pictures", item, buttons);
461 if (!(item->m_bIsFolder || item->IsZIP() || item->IsRAR() || item->IsCBZ() || item->IsCBR()))
462 buttons.Add(CONTEXT_BUTTON_INFO, 13406); // picture info
463 buttons.Add(CONTEXT_BUTTON_VIEW_SLIDESHOW, item->m_bIsFolder ? 13317 : 13422); // View Slideshow
464 if (item->m_bIsFolder)
465 buttons.Add(CONTEXT_BUTTON_RECURSIVE_SLIDESHOW, 13318); // Recursive Slideshow
467 if (!m_thumbLoader.IsLoading())
468 buttons.Add(CONTEXT_BUTTON_REFRESH_THUMBS, 13315); // Create Thumbnails
469 if (g_guiSettings.GetBool("filelists.allowfiledeletion") && !item->IsReadOnly())
471 buttons.Add(CONTEXT_BUTTON_DELETE, 117);
472 buttons.Add(CONTEXT_BUTTON_RENAME, 118);
476 if (item->IsPlugin() || item->IsScript() || m_vecItems->IsPlugin())
477 buttons.Add(CONTEXT_BUTTON_PLUGIN_SETTINGS, 1045);
479 buttons.Add(CONTEXT_BUTTON_GOTO_ROOT, 20128);
480 buttons.Add(CONTEXT_BUTTON_SWITCH_MEDIA, 523);
483 CGUIMediaWindow::GetContextButtons(itemNumber, buttons);
484 if (item && !item->GetProperty("pluginreplacecontextitems").asBoolean())
485 buttons.Add(CONTEXT_BUTTON_SETTINGS, 5); // Settings
488 bool CGUIWindowPictures::OnContextButton(int itemNumber, CONTEXT_BUTTON button)
490 CFileItemPtr item = (itemNumber >= 0 && itemNumber < m_vecItems->Size()) ? m_vecItems->Get(itemNumber) : CFileItemPtr();
491 if (m_vecItems->IsVirtualDirectoryRoot() && item)
493 if (CGUIDialogContextMenu::OnContextButton("pictures", item, button))
501 case CONTEXT_BUTTON_VIEW_SLIDESHOW:
502 if (item && item->m_bIsFolder)
503 OnSlideShow(item->GetPath());
505 ShowPicture(itemNumber, true);
507 case CONTEXT_BUTTON_RECURSIVE_SLIDESHOW:
509 OnSlideShowRecursive(item->GetPath());
511 case CONTEXT_BUTTON_INFO:
514 case CONTEXT_BUTTON_REFRESH_THUMBS:
515 OnRegenerateThumbs();
517 case CONTEXT_BUTTON_DELETE:
518 OnDeleteItem(itemNumber);
520 case CONTEXT_BUTTON_RENAME:
521 OnRenameItem(itemNumber);
523 case CONTEXT_BUTTON_SETTINGS:
524 g_windowManager.ActivateWindow(WINDOW_SETTINGS_MYPICTURES);
526 case CONTEXT_BUTTON_GOTO_ROOT:
529 case CONTEXT_BUTTON_SWITCH_MEDIA:
530 CGUIDialogContextMenu::SwitchMedia("pictures", m_vecItems->GetPath());
535 return CGUIMediaWindow::OnContextButton(itemNumber, button);
538 void CGUIWindowPictures::OnItemLoaded(CFileItem *pItem)
540 CPictureThumbLoader::ProcessFoldersAndArchives(pItem);
543 void CGUIWindowPictures::LoadPlayList(const CStdString& strPlayList)
545 CLog::Log(LOGDEBUG,"CGUIWindowPictures::LoadPlayList()... converting playlist into slideshow: %s", strPlayList.c_str());
546 auto_ptr<CPlayList> pPlayList (CPlayListFactory::Create(strPlayList));
547 if ( NULL != pPlayList.get())
549 if (!pPlayList->Load(strPlayList))
551 CGUIDialogOK::ShowAndGetInput(6, 0, 477, 0);
552 return ; //hmmm unable to load playlist?
556 CPlayList playlist = *pPlayList;
557 if (playlist.size() > 0)
560 CGUIWindowSlideShow *pSlideShow = (CGUIWindowSlideShow *)g_windowManager.GetWindow(WINDOW_SLIDESHOW);
563 if (g_application.IsPlayingVideo())
564 g_application.StopPlaying();
566 // convert playlist items into slideshow items
568 for (int i = 0; i < (int)playlist.size(); ++i)
570 CFileItemPtr pItem = playlist[i];
571 //CLog::Log(LOGDEBUG,"-- playlist item: %s", pItem->GetPath().c_str());
572 if (pItem->IsPicture() && !(pItem->IsZIP() || pItem->IsRAR() || pItem->IsCBZ() || pItem->IsCBR()))
573 pSlideShow->Add(pItem.get());
576 // start slideshow if there are items
577 pSlideShow->StartSlideShow();
578 if (pSlideShow->NumSlides())
579 g_windowManager.ActivateWindow(WINDOW_SLIDESHOW);
583 void CGUIWindowPictures::OnInfo(int itemNumber)
585 CFileItemPtr item = (itemNumber >= 0 && itemNumber < m_vecItems->Size()) ? m_vecItems->Get(itemNumber) : CFileItemPtr();
586 if (!item || item->m_bIsFolder || item->IsZIP() || item->IsRAR() || item->IsCBZ() || item->IsCBR() || !item->IsPicture())
588 CGUIDialogPictureInfo *pictureInfo = (CGUIDialogPictureInfo *)g_windowManager.GetWindow(WINDOW_DIALOG_PICTURE_INFO);
591 pictureInfo->SetPicture(item.get());
592 pictureInfo->DoModal();
596 CStdString CGUIWindowPictures::GetStartFolder(const CStdString &dir)
598 if (dir.Equals("Plugins") || dir.Equals("Addons"))
599 return "addons://sources/image/";
603 m_rootDir.GetSources(shares);
604 bool bIsSourceName = false;
605 int iIndex = CUtil::GetMatchingSource(dir, shares, bIsSourceName);
608 if (iIndex < (int)shares.size() && shares[iIndex].m_iHasLock == 2)
610 CFileItem item(shares[iIndex]);
611 if (!g_passwordManager.IsItemUnlocked(&item,"pictures"))
615 return shares[iIndex].strPath;
618 return CGUIMediaWindow::GetStartFolder(dir);