Merge pull request #26386 from ksooo/guiinfo-fix-listitem-filenamenoextension
[xbmc.git] / xbmc / dialogs / GUIDialogCache.cpp
blob6ec8b2040be0e10192d189f137a0b3e0840a52d5
1 /*
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.
7 */
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"
21 #include <mutex>
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)
30 bSentCancel = false;
32 m_pDlg = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS);
34 if (!m_pDlg)
35 return;
37 /* if progress dialog is already running, take it over */
38 if( m_pDlg->IsDialogRunning() )
39 delay = 0ms;
41 if (delay == 0ms)
42 OpenDialog();
43 else
44 m_endtime.Set(delay);
46 Create(true);
49 void CGUIDialogCache::Close(bool bForceClose)
51 bSentCancel = true;
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())
66 m_pDlg->Close();
69 void CGUIDialogCache::OpenDialog()
71 if (m_pDlg)
73 if (m_strHeader.empty())
74 m_pDlg->SetHeading(CVariant{438});
75 else
76 m_pDlg->SetHeading(CVariant{m_strHeader});
78 m_pDlg->SetLine(2, CVariant{m_strLinePrev});
79 m_pDlg->Open();
81 bSentCancel = false;
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)
96 if (m_pDlg)
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)
108 if (m_pDlg)
110 m_pDlg->ShowProgressBar(true);
111 m_pDlg->SetPercentage(ipercent);
114 if( IsCanceled() )
115 return false;
116 else
117 return true;
120 void CGUIDialogCache::Process()
122 if (!m_pDlg)
123 return;
125 while( true )
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());
135 m_pDlg->Progress();
136 if( bSentCancel )
138 CThread::Sleep(10ms);
139 continue;
142 if(m_pDlg->IsCanceled())
144 bSentCancel = true;
146 else if( !m_pDlg->IsDialogRunning() && m_endtime.IsTimePast()
147 && !CServiceBroker::GetGUI()->GetWindowManager().IsWindowActive(WINDOW_DIALOG_YES_NO) )
148 OpenDialog();
150 catch(...)
152 CLog::Log(LOGERROR, "Exception in CGUIDialogCache::Process()");
156 CThread::Sleep(10ms);
160 void CGUIDialogCache::ShowProgressBar(bool bOnOff)
162 if (m_pDlg)
163 m_pDlg->ShowProgressBar(bOnOff);
166 void CGUIDialogCache::SetPercentage(int iPercentage)
168 if (m_pDlg)
169 m_pDlg->SetPercentage(iPercentage);
172 bool CGUIDialogCache::IsCanceled() const
174 if (m_pDlg && m_pDlg->IsDialogRunning())
175 return m_pDlg->IsCanceled();
176 else
177 return false;