Merge pull request #4775 from jmarshallnz/empty_episode_playcount
[vuplus_xbmc] / xbmc / dialogs / GUIDialogProgress.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "GUIDialogProgress.h"
22 #include "guilib/GUIProgressControl.h"
23 #include "Application.h"
24 #include "GUIInfoManager.h"
25 #include "guilib/GUIWindowManager.h"
26 #include "guilib/LocalizeStrings.h"
27 #include "threads/SingleLock.h"
28 #include "utils/log.h"
29
30 using namespace std;
31
32 #define CONTROL_CANCEL_BUTTON 10
33 #define CONTROL_PROGRESS_BAR 20
34
35 CGUIDialogProgress::CGUIDialogProgress(void)
36     : CGUIDialogBoxBase(WINDOW_DIALOG_PROGRESS, "DialogProgress.xml")
37 {
38   m_bCanceled = false;
39   m_iCurrent=0;
40   m_iMax=0;
41   m_percentage = 0;
42   m_bCanCancel = true;
43 }
44
45 CGUIDialogProgress::~CGUIDialogProgress(void)
46 {
47
48 }
49
50 void CGUIDialogProgress::SetCanCancel(bool bCanCancel)
51 {
52   m_bCanCancel = bCanCancel;
53   CGUIMessage msg(bCanCancel ? GUI_MSG_VISIBLE : GUI_MSG_HIDDEN, GetID(), CONTROL_CANCEL_BUTTON);
54   CSingleTryLock tryLock(g_graphicsContext);
55   if(tryLock.IsOwner())
56     OnMessage(msg);
57   else
58     g_windowManager.SendThreadMessage(msg, GetID());
59 }
60
61 void CGUIDialogProgress::StartModal()
62 {
63   CSingleLock lock(g_graphicsContext);
64
65   CLog::Log(LOGDEBUG, "DialogProgress::StartModal called %s", m_active ? "(already running)!" : "");
66   m_bCanceled = false;
67
68   // set running before it's routed, else the auto-show code
69   // could show it as well if we are in a different thread from
70   // the main rendering thread (this should really be handled via
71   // a thread message though IMO)
72   m_active = true;
73   m_bModal = true;
74   m_closing = false;
75   g_windowManager.RouteToWindow(this);
76
77   // active this window...
78   CGUIMessage msg(GUI_MSG_WINDOW_INIT, 0, 0);
79   OnMessage(msg);
80   ShowProgressBar(false);
81
82   lock.Leave();
83
84   while (m_active && IsAnimating(ANIM_TYPE_WINDOW_OPEN))
85   {
86     Progress();
87     // we should have rendered at least once by now - if we haven't, then
88     // we must be running from fullscreen video or similar where the
89     // calling thread handles rendering (ie not main app thread) but
90     // is waiting on this routine before rendering begins
91     if (!HasProcessed())
92       break;
93   }
94 }
95
96 void CGUIDialogProgress::Progress()
97 {
98   if (m_active)
99   {
100     g_windowManager.ProcessRenderLoop();
101   }
102 }
103
104 void CGUIDialogProgress::ProgressKeys()
105 {
106   if (m_active)
107   {
108     g_application.FrameMove(true);
109   }
110 }
111
112 bool CGUIDialogProgress::OnMessage(CGUIMessage& message)
113 {
114   switch ( message.GetMessage() )
115   {
116
117   case GUI_MSG_WINDOW_DEINIT:
118     SetCanCancel(true);
119     break;
120
121   case GUI_MSG_CLICKED:
122     {
123       int iControl = message.GetSenderId();
124       if (iControl == CONTROL_CANCEL_BUTTON && m_bCanCancel && !m_bCanceled)
125       {
126         string strHeading = m_strHeading;
127         strHeading.append(" : ");
128         strHeading.append(g_localizeStrings.Get(16024));
129         CGUIDialogBoxBase::SetHeading(strHeading);
130         m_bCanceled = true;
131         return true;
132       }
133     }
134     break;
135   }
136   return CGUIDialog::OnMessage(message);
137 }
138
139 bool CGUIDialogProgress::OnBack(int actionID)
140 {
141   if (m_bCanCancel)
142   {
143     m_bCanceled = true;
144     return true;
145   }
146   return false;
147 }
148
149 void CGUIDialogProgress::OnWindowLoaded()
150 {
151   CGUIDialog::OnWindowLoaded();
152   const CGUIControl *control = GetControl(CONTROL_PROGRESS_BAR);
153   if (control && control->GetControlType() == CGUIControl::GUICONTROL_PROGRESS)
154   {
155     // make sure we have the appropriate info set
156     CGUIProgressControl *progress = (CGUIProgressControl *)control;
157     if (!progress->GetInfo())
158       progress->SetInfo(SYSTEM_PROGRESS_BAR);
159   }
160 }
161
162 void CGUIDialogProgress::SetPercentage(int iPercentage)
163 {
164   if (iPercentage < 0) iPercentage = 0;
165   if (iPercentage > 100) iPercentage = 100;
166
167   m_percentage = iPercentage;
168 }
169
170 void CGUIDialogProgress::SetProgressMax(int iMax)
171 {
172   m_iMax=iMax;
173   m_iCurrent=0;
174 }
175
176 void CGUIDialogProgress::SetProgressAdvance(int nSteps/*=1*/)
177 {
178   m_iCurrent+=nSteps;
179
180   if (m_iCurrent>m_iMax)
181     m_iCurrent=0;
182
183   SetPercentage((m_iCurrent*100)/m_iMax);
184 }
185
186 bool CGUIDialogProgress::Abort()
187 {
188   return m_active ? m_bCanceled : false;
189 }
190
191 void CGUIDialogProgress::ShowProgressBar(bool bOnOff)
192 {
193   CGUIMessage msg(bOnOff ? GUI_MSG_VISIBLE : GUI_MSG_HIDDEN, GetID(), CONTROL_PROGRESS_BAR);
194   CSingleTryLock tryLock(g_graphicsContext);
195   if(tryLock.IsOwner())
196     OnMessage(msg);
197   else
198     g_windowManager.SendThreadMessage(msg, GetID());
199 }
200
201 int CGUIDialogProgress::GetDefaultLabelID(int controlId) const
202 {
203   if (controlId == CONTROL_CANCEL_BUTTON)
204     return 222;
205   return CGUIDialogBoxBase::GetDefaultLabelID(controlId);
206 }