PageRenderTime 36ms CodeModel.GetById 17ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/dialogs/GUIDialogBusy.cpp

http://github.com/xbmc/xbmc
C++ | 157 lines | 121 code | 26 blank | 10 comment | 14 complexity | d1453579d10a1b93c88db9383dad6fd4 MD5 | raw file
  1/*
  2 *  Copyright (C) 2005-2018 Team Kodi
  3 *  This file is part of Kodi - https://kodi.tv
  4 *
  5 *  SPDX-License-Identifier: GPL-2.0-or-later
  6 *  See LICENSES/README.md for more information.
  7 */
  8
  9#include "GUIDialogBusy.h"
 10
 11#include "ServiceBroker.h"
 12#include "guilib/GUIComponent.h"
 13#include "guilib/GUIProgressControl.h"
 14#include "guilib/GUIWindowManager.h"
 15#include "threads/IRunnable.h"
 16#include "threads/Thread.h"
 17
 18#define PROGRESS_CONTROL 10
 19
 20class CBusyWaiter : public CThread
 21{
 22  std::shared_ptr<CEvent> m_done;
 23  IRunnable *m_runnable;
 24public:
 25  explicit CBusyWaiter(IRunnable *runnable) :
 26  CThread(runnable, "waiting"), m_done(new CEvent()),  m_runnable(runnable) { }
 27
 28  ~CBusyWaiter() override { StopThread(); }
 29
 30  bool Wait(unsigned int displaytime, bool allowCancel)
 31  {
 32    std::shared_ptr<CEvent> e_done(m_done);
 33
 34    Create();
 35    unsigned int start = XbmcThreads::SystemClockMillis();
 36    if (!CGUIDialogBusy::WaitOnEvent(*e_done, displaytime, allowCancel))
 37    {
 38      m_runnable->Cancel();
 39      unsigned int elapsed = XbmcThreads::SystemClockMillis() - start;
 40      unsigned int remaining = (elapsed >= displaytime) ? 0 : displaytime - elapsed;
 41      CGUIDialogBusy::WaitOnEvent(*e_done, remaining, false);
 42      return false;
 43    }
 44    return true;
 45  }
 46
 47  // 'this' is actually deleted from the thread where it's on the stack
 48  void Process() override
 49  {
 50    std::shared_ptr<CEvent> e_done(m_done);
 51
 52    CThread::Process();
 53    (*e_done).Set();
 54  }
 55
 56};
 57
 58bool CGUIDialogBusy::Wait(IRunnable *runnable, unsigned int displaytime, bool allowCancel)
 59{
 60  if (!runnable)
 61    return false;
 62  CBusyWaiter waiter(runnable);
 63  if (!waiter.Wait(displaytime, allowCancel))
 64  {
 65    return false;
 66  }
 67  return true;
 68}
 69
 70bool CGUIDialogBusy::WaitOnEvent(CEvent &event, unsigned int displaytime /* = 100 */, bool allowCancel /* = true */)
 71{
 72  bool cancelled = false;
 73  if (!event.WaitMSec(displaytime))
 74  {
 75    // throw up the progress
 76    CGUIDialogBusy* dialog = CServiceBroker::GetGUI()->GetWindowManager().GetWindow<CGUIDialogBusy>(WINDOW_DIALOG_BUSY);
 77    if (dialog)
 78    {
 79      if (dialog->IsDialogRunning())
 80      {
 81        throw std::logic_error("busy dialog already running");
 82      }
 83
 84      dialog->Open();
 85
 86      while(!event.WaitMSec(1))
 87      {
 88        dialog->ProcessRenderLoop(false);
 89        if (allowCancel && dialog->IsCanceled())
 90        {
 91          cancelled = true;
 92          break;
 93        }
 94      }
 95
 96      dialog->Close(true);
 97    }
 98  }
 99  return !cancelled;
100}
101
102CGUIDialogBusy::CGUIDialogBusy(void)
103  : CGUIDialog(WINDOW_DIALOG_BUSY, "DialogBusy.xml", DialogModalityType::MODAL)
104{
105  m_loadType = LOAD_ON_GUI_INIT;
106  m_bCanceled = false;
107  m_progress = -1;
108}
109
110CGUIDialogBusy::~CGUIDialogBusy(void) = default;
111
112void CGUIDialogBusy::Open_Internal(const std::string &param /* = "" */)
113{
114  m_bCanceled = false;
115  m_bLastVisible = true;
116  m_progress = -1;
117
118  CGUIDialog::Open_Internal(false, param);
119}
120
121
122void CGUIDialogBusy::DoProcess(unsigned int currentTime, CDirtyRegionList &dirtyregions)
123{
124  bool visible = CServiceBroker::GetGUI()->GetWindowManager().IsModalDialogTopmost(WINDOW_DIALOG_BUSY);
125  if(!visible && m_bLastVisible)
126    dirtyregions.push_back(CDirtyRegion(m_renderRegion));
127  m_bLastVisible = visible;
128
129  // update the progress control if available
130  CGUIControl *control = GetControl(PROGRESS_CONTROL);
131  if (control && control->GetControlType() == CGUIControl::GUICONTROL_PROGRESS)
132  {
133    CGUIProgressControl *progress = static_cast<CGUIProgressControl*>(control);
134    progress->SetPercentage(m_progress);
135    progress->SetVisible(m_progress > -1);
136  }
137
138  CGUIDialog::DoProcess(currentTime, dirtyregions);
139}
140
141void CGUIDialogBusy::Render()
142{
143  if(!m_bLastVisible)
144    return;
145  CGUIDialog::Render();
146}
147
148bool CGUIDialogBusy::OnBack(int actionID)
149{
150  m_bCanceled = true;
151  return true;
152}
153
154void CGUIDialogBusy::SetProgress(float percent)
155{
156  m_progress = percent;
157}