1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* Copyright (C) 2005-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "GUIDialogCache.h"
#include "ServiceBroker.h"
#include "dialogs/GUIDialogProgress.h"
#include "guilib/GUIComponent.h"
#include "guilib/GUIWindowManager.h"
#include "guilib/LocalizeStrings.h"
#include "messaging/ApplicationMessenger.h"
#include "threads/SystemClock.h"
#include "utils/Variant.h"
#include "utils/log.h"
#include <mutex>
using namespace std::chrono_literals;
CGUIDialogCache::CGUIDialogCache(std::chrono::milliseconds delay,
const std::string& strHeader,
const std::string& strMsg)
: CThread("GUIDialogCache"), m_strHeader(strHeader), m_strLinePrev(strMsg)
{
bSentCancel = false;
m_pDlg = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogProgress>(WINDOW_DIALOG_PROGRESS);
if (!m_pDlg)
return;
/* if progress dialog is already running, take it over */
if( m_pDlg->IsDialogRunning() )
delay = 0ms;
if (delay == 0ms)
OpenDialog();
else
m_endtime.Set(delay);
Create(true);
}
void CGUIDialogCache::Close(bool bForceClose)
{
bSentCancel = true;
// we cannot wait for the app thread to process the close message
// as this might happen during player startup which leads to a deadlock
if (m_pDlg && m_pDlg->IsDialogRunning())
CServiceBroker::GetAppMessenger()->PostMsg(TMSG_GUI_WINDOW_CLOSE, -1, bForceClose ? 1 : 0,
static_cast<void*>(m_pDlg));
//Set stop, this will kill this object, when thread stops
CThread::m_bStop = true;
}
CGUIDialogCache::~CGUIDialogCache()
{
if(m_pDlg && m_pDlg->IsDialogRunning())
m_pDlg->Close();
}
void CGUIDialogCache::OpenDialog()
{
if (m_pDlg)
{
if (m_strHeader.empty())
m_pDlg->SetHeading(CVariant{438});
else
m_pDlg->SetHeading(CVariant{m_strHeader});
m_pDlg->SetLine(2, CVariant{m_strLinePrev});
m_pDlg->Open();
}
bSentCancel = false;
}
void CGUIDialogCache::SetHeader(const std::string& strHeader)
{
m_strHeader = strHeader;
}
void CGUIDialogCache::SetHeader(int nHeader)
{
SetHeader(g_localizeStrings.Get(nHeader));
}
void CGUIDialogCache::SetMessage(const std::string& strMessage)
{
if (m_pDlg)
{
m_pDlg->SetLine(0, CVariant{m_strLinePrev2});
m_pDlg->SetLine(1, CVariant{m_strLinePrev});
m_pDlg->SetLine(2, CVariant{strMessage});
}
m_strLinePrev2 = m_strLinePrev;
m_strLinePrev = strMessage;
}
bool CGUIDialogCache::OnFileCallback(void* pContext, int ipercent, float avgSpeed)
{
if (m_pDlg)
{
m_pDlg->ShowProgressBar(true);
m_pDlg->SetPercentage(ipercent);
}
if( IsCanceled() )
return false;
else
return true;
}
void CGUIDialogCache::Process()
{
if (!m_pDlg)
return;
while( true )
{
{ //Section to make the lock go out of scope before sleep
if( CThread::m_bStop ) break;
try
{
std::unique_lock<CCriticalSection> lock(CServiceBroker::GetWinSystem()->GetGfxContext());
m_pDlg->Progress();
if( bSentCancel )
{
CThread::Sleep(10ms);
continue;
}
if(m_pDlg->IsCanceled())
{
bSentCancel = true;
}
else if( !m_pDlg->IsDialogRunning() && m_endtime.IsTimePast()
&& !CServiceBroker::GetGUI()->GetWindowManager().IsWindowActive(WINDOW_DIALOG_YES_NO) )
OpenDialog();
}
catch(...)
{
CLog::Log(LOGERROR, "Exception in CGUIDialogCache::Process()");
}
}
CThread::Sleep(10ms);
}
}
void CGUIDialogCache::ShowProgressBar(bool bOnOff)
{
if (m_pDlg)
m_pDlg->ShowProgressBar(bOnOff);
}
void CGUIDialogCache::SetPercentage(int iPercentage)
{
if (m_pDlg)
m_pDlg->SetPercentage(iPercentage);
}
bool CGUIDialogCache::IsCanceled() const
{
if (m_pDlg && m_pDlg->IsDialogRunning())
return m_pDlg->IsCanceled();
else
return false;
}
|