/
GUIDialogCache.cpp
181 lines (153 loc) · 4.07 KB
/
GUIDialogCache.cpp
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
179
180
181
/*
* Copyright (C) 2005-2013 Team XBMC
* http://xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "threads/SystemClock.h"
#include "GUIDialogCache.h"
#include "ApplicationMessenger.h"
#include "guilib/GUIWindowManager.h"
#include "dialogs/GUIDialogProgress.h"
#include "guilib/LocalizeStrings.h"
#include "utils/log.h"
#include "threads/SingleLock.h"
CGUIDialogCache::CGUIDialogCache(DWORD dwDelay, const std::string& strHeader, const std::string& strMsg) : CThread("GUIDialogCache"),
m_strHeader(strHeader),
m_strLinePrev(strMsg)
{
bSentCancel = false;
m_pDlg = (CGUIDialogProgress*)g_windowManager.GetWindow(WINDOW_DIALOG_PROGRESS);
if (!m_pDlg)
return;
/* if progress dialog is already running, take it over */
if( m_pDlg->IsDialogRunning() )
dwDelay = 0;
if(dwDelay == 0)
OpenDialog();
else
m_endtime.Set((unsigned int)dwDelay);
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())
CApplicationMessenger::Get().Close(m_pDlg,bForceClose,false);
//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(438);
else
m_pDlg->SetHeading(m_strHeader);
m_pDlg->SetLine(2, m_strLinePrev);
m_pDlg->StartModal();
}
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, m_strLinePrev2);
m_pDlg->SetLine(1, m_strLinePrev);
m_pDlg->SetLine(2, 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
{
CSingleLock lock(g_graphicsContext);
m_pDlg->Progress();
if( bSentCancel )
{
Sleep(10);
continue;
}
if(m_pDlg->IsCanceled())
{
bSentCancel = true;
}
else if( !m_pDlg->IsDialogRunning() && m_endtime.IsTimePast()
&& !g_windowManager.IsWindowActive(WINDOW_DIALOG_YES_NO) )
OpenDialog();
}
catch(...)
{
CLog::Log(LOGERROR, "Exception in CGUIDialogCache::Process()");
}
}
Sleep(10);
}
}
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;
}