2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "GUIDialogCache.h"
11 #include "ServiceBroker.h"
12 #include "dialogs/GUIDialogProgress.h"
13 #include "guilib/GUIComponent.h"
14 #include "guilib/GUIWindowManager.h"
15 #include "guilib/LocalizeStrings.h"
16 #include "messaging/ApplicationMessenger.h"
17 #include "threads/SystemClock.h"
18 #include "utils/Variant.h"
19 #include "utils/log.h"
23 using namespace std::chrono_literals
;
25 CGUIDialogCache::CGUIDialogCache(std::chrono::milliseconds delay
,
26 const std::string
& strHeader
,
27 const std::string
& strMsg
)
28 : CThread("GUIDialogCache"), m_strHeader(strHeader
), m_strLinePrev(strMsg
)
32 m_pDlg
= CServiceBroker::GetGUI()->GetWindowManager().GetWindow
<CGUIDialogProgress
>(WINDOW_DIALOG_PROGRESS
);
37 /* if progress dialog is already running, take it over */
38 if( m_pDlg
->IsDialogRunning() )
49 void CGUIDialogCache::Close(bool bForceClose
)
53 // we cannot wait for the app thread to process the close message
54 // as this might happen during player startup which leads to a deadlock
55 if (m_pDlg
&& m_pDlg
->IsDialogRunning())
56 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_GUI_WINDOW_CLOSE
, -1, bForceClose
? 1 : 0,
57 static_cast<void*>(m_pDlg
));
59 //Set stop, this will kill this object, when thread stops
60 CThread::m_bStop
= true;
63 CGUIDialogCache::~CGUIDialogCache()
65 if(m_pDlg
&& m_pDlg
->IsDialogRunning())
69 void CGUIDialogCache::OpenDialog()
73 if (m_strHeader
.empty())
74 m_pDlg
->SetHeading(CVariant
{438});
76 m_pDlg
->SetHeading(CVariant
{m_strHeader
});
78 m_pDlg
->SetLine(2, CVariant
{m_strLinePrev
});
84 void CGUIDialogCache::SetHeader(const std::string
& strHeader
)
86 m_strHeader
= strHeader
;
89 void CGUIDialogCache::SetHeader(int nHeader
)
91 SetHeader(g_localizeStrings
.Get(nHeader
));
94 void CGUIDialogCache::SetMessage(const std::string
& strMessage
)
98 m_pDlg
->SetLine(0, CVariant
{m_strLinePrev2
});
99 m_pDlg
->SetLine(1, CVariant
{m_strLinePrev
});
100 m_pDlg
->SetLine(2, CVariant
{strMessage
});
102 m_strLinePrev2
= m_strLinePrev
;
103 m_strLinePrev
= strMessage
;
106 bool CGUIDialogCache::OnFileCallback(void* pContext
, int ipercent
, float avgSpeed
)
110 m_pDlg
->ShowProgressBar(true);
111 m_pDlg
->SetPercentage(ipercent
);
120 void CGUIDialogCache::Process()
128 { //Section to make the lock go out of scope before sleep
130 if( CThread::m_bStop
) break;
134 std::unique_lock
<CCriticalSection
> lock(CServiceBroker::GetWinSystem()->GetGfxContext());
138 CThread::Sleep(10ms
);
142 if(m_pDlg
->IsCanceled())
146 else if( !m_pDlg
->IsDialogRunning() && m_endtime
.IsTimePast()
147 && !CServiceBroker::GetGUI()->GetWindowManager().IsWindowActive(WINDOW_DIALOG_YES_NO
) )
152 CLog::Log(LOGERROR
, "Exception in CGUIDialogCache::Process()");
156 CThread::Sleep(10ms
);
160 void CGUIDialogCache::ShowProgressBar(bool bOnOff
)
163 m_pDlg
->ShowProgressBar(bOnOff
);
166 void CGUIDialogCache::SetPercentage(int iPercentage
)
169 m_pDlg
->SetPercentage(iPercentage
);
172 bool CGUIDialogCache::IsCanceled() const
174 if (m_pDlg
&& m_pDlg
->IsDialogRunning())
175 return m_pDlg
->IsCanceled();