Merge pull request #4594 from FernetMenta/paplayer
[xbmc.git] / xbmc / dialogs / GUIDialogCache.cpp
blob1a7d0431685afbce3cd702ed2fc06db658e78727
1 /*
2 * Copyright (C) 2005-2013 Team XBMC
3 * http://xbmc.org
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)
8 * any later version.
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"
22 #include "GUIDialogCache.h"
23 #include "ApplicationMessenger.h"
24 #include "guilib/GUIWindowManager.h"
25 #include "dialogs/GUIDialogProgress.h"
26 #include "guilib/LocalizeStrings.h"
27 #include "utils/log.h"
28 #include "threads/SingleLock.h"
29 #include "utils/TimeUtils.h"
31 CGUIDialogCache::CGUIDialogCache(DWORD dwDelay, const CStdString& strHeader, const CStdString& strMsg) : CThread("GUIDialogCache")
33 m_strHeader = strHeader;
34 m_strLinePrev = strMsg;
35 bSentCancel = false;
37 m_pDlg = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
39 if (!m_pDlg)
40 return;
42 /* if progress dialog is already running, take it over */
43 if( m_pDlg->IsDialogRunning() )
44 dwDelay = 0;
46 if(dwDelay == 0)
47 OpenDialog();
48 else
49 m_endtime.Set((unsigned int)dwDelay);
51 Create(true);
54 void CGUIDialogCache::Close(bool bForceClose)
56 bSentCancel = true;
58 // we cannot wait for the app thread to process the close message
59 // as this might happen during player startup which leads to a deadlock
60 if (m_pDlg && m_pDlg->IsDialogRunning())
61 CApplicationMessenger::Get().Close(m_pDlg,bForceClose,false);
63 //Set stop, this will kill this object, when thread stops
64 CThread::m_bStop = true;
67 CGUIDialogCache::~CGUIDialogCache()
69 if(m_pDlg && m_pDlg->IsDialogRunning())
70 m_pDlg->Close();
73 void CGUIDialogCache::OpenDialog()
75 if (m_pDlg)
77 if (m_strHeader.empty())
78 m_pDlg->SetHeading(438);
79 else
80 m_pDlg->SetHeading(m_strHeader);
82 m_pDlg->SetLine(2, m_strLinePrev);
83 m_pDlg->StartModal();
85 bSentCancel = false;
88 void CGUIDialogCache::SetHeader(const CStdString& strHeader)
90 m_strHeader = strHeader;
93 void CGUIDialogCache::SetHeader(int nHeader)
95 SetHeader(g_localizeStrings.Get(nHeader));
98 void CGUIDialogCache::SetMessage(const CStdString& strMessage)
100 if (m_pDlg)
102 m_pDlg->SetLine(0, m_strLinePrev2);
103 m_pDlg->SetLine(1, m_strLinePrev);
104 m_pDlg->SetLine(2, strMessage);
106 m_strLinePrev2 = m_strLinePrev;
107 m_strLinePrev = strMessage;
110 bool CGUIDialogCache::OnFileCallback(void* pContext, int ipercent, float avgSpeed)
112 if (m_pDlg)
114 m_pDlg->ShowProgressBar(true);
115 m_pDlg->SetPercentage(ipercent);
118 if( IsCanceled() )
119 return false;
120 else
121 return true;
124 void CGUIDialogCache::Process()
126 if (!m_pDlg)
127 return;
129 while( true )
132 { //Section to make the lock go out of scope before sleep
134 if( CThread::m_bStop ) break;
136 try
138 CSingleLock lock(g_graphicsContext);
139 m_pDlg->Progress();
140 if( bSentCancel )
142 Sleep(10);
143 continue;
146 if(m_pDlg->IsCanceled())
148 bSentCancel = true;
150 else if( !m_pDlg->IsDialogRunning() && m_endtime.IsTimePast()
151 && !g_windowManager.IsWindowActive(WINDOW_DIALOG_YES_NO) )
152 OpenDialog();
154 catch(...)
156 CLog::Log(LOGERROR, "Exception in CGUIDialogCache::Process()");
160 Sleep(10);
164 void CGUIDialogCache::ShowProgressBar(bool bOnOff)
166 if (m_pDlg)
167 m_pDlg->ShowProgressBar(bOnOff);
170 void CGUIDialogCache::SetPercentage(int iPercentage)
172 if (m_pDlg)
173 m_pDlg->SetPercentage(iPercentage);
176 bool CGUIDialogCache::IsCanceled() const
178 if (m_pDlg && m_pDlg->IsDialogRunning())
179 return m_pDlg->IsCanceled();
180 else
181 return false;