PageRenderTime 32ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/xbmc/dialogs/GUIDialogKaiToast.cpp

http://github.com/xbmc/xbmc
C++ | 177 lines | 127 code | 34 blank | 16 comment | 16 complexity | 6aea45c516820cde6a69e941c56facaf 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 "GUIDialogKaiToast.h"
 10
 11#include "ServiceBroker.h"
 12#include "guilib/GUIFadeLabelControl.h"
 13#include "guilib/GUIMessage.h"
 14#include "peripherals/Peripherals.h"
 15#include "threads/SingleLock.h"
 16#include "utils/TimeUtils.h"
 17
 18#define POPUP_ICON                400
 19#define POPUP_CAPTION_TEXT        401
 20#define POPUP_NOTIFICATION_BUTTON 402
 21
 22CGUIDialogKaiToast::TOASTQUEUE CGUIDialogKaiToast::m_notifications;
 23CCriticalSection CGUIDialogKaiToast::m_critical;
 24
 25CGUIDialogKaiToast::CGUIDialogKaiToast(void)
 26  : CGUIDialog(WINDOW_DIALOG_KAI_TOAST, "DialogNotification.xml", DialogModalityType::MODELESS)
 27{
 28  m_loadType = LOAD_ON_GUI_INIT;
 29  m_timer = 0;
 30  m_toastDisplayTime = 0;
 31  m_toastMessageTime = 0;
 32}
 33
 34CGUIDialogKaiToast::~CGUIDialogKaiToast(void) = default;
 35
 36bool CGUIDialogKaiToast::OnMessage(CGUIMessage& message)
 37{
 38  switch ( message.GetMessage() )
 39  {
 40  case GUI_MSG_WINDOW_INIT:
 41    {
 42      CGUIDialog::OnMessage(message);
 43      ResetTimer();
 44      return true;
 45    }
 46    break;
 47
 48  case GUI_MSG_WINDOW_DEINIT:
 49    {
 50    }
 51    break;
 52  }
 53  return CGUIDialog::OnMessage(message);
 54}
 55
 56void CGUIDialogKaiToast::QueueNotification(eMessageType eType, const std::string& aCaption, const std::string& aDescription, unsigned int displayTime /*= TOAST_DISPLAY_TIME*/, bool withSound /*= true*/, unsigned int messageTime /*= TOAST_MESSAGE_TIME*/)
 57{
 58  AddToQueue("", eType, aCaption, aDescription, displayTime, withSound, messageTime);
 59}
 60
 61void CGUIDialogKaiToast::QueueNotification(const std::string& aCaption, const std::string& aDescription)
 62{
 63  QueueNotification("", aCaption, aDescription);
 64}
 65
 66void CGUIDialogKaiToast::QueueNotification(const std::string& aImageFile, const std::string& aCaption, const std::string& aDescription, unsigned int displayTime /*= TOAST_DISPLAY_TIME*/, bool withSound /*= true*/, unsigned int messageTime /*= TOAST_MESSAGE_TIME*/)
 67{
 68  AddToQueue(aImageFile, Default, aCaption, aDescription, displayTime, withSound, messageTime);
 69}
 70
 71void CGUIDialogKaiToast::AddToQueue(const std::string& aImageFile, const eMessageType eType, const std::string& aCaption, const std::string& aDescription, unsigned int displayTime /*= TOAST_DISPLAY_TIME*/, bool withSound /*= true*/, unsigned int messageTime /*= TOAST_MESSAGE_TIME*/)
 72{
 73  CSingleLock lock(m_critical);
 74
 75  Notification toast;
 76  toast.eType = eType;
 77  toast.imagefile = aImageFile;
 78  toast.caption = aCaption;
 79  toast.description = aDescription;
 80  toast.displayTime = displayTime > TOAST_MESSAGE_TIME + 500 ? displayTime : TOAST_MESSAGE_TIME + 500;
 81  toast.messageTime = messageTime;
 82  toast.withSound = withSound;
 83
 84  m_notifications.push(toast);
 85}
 86
 87bool CGUIDialogKaiToast::DoWork()
 88{
 89  CSingleLock lock(m_critical);
 90
 91  if (!m_notifications.empty() &&
 92      CTimeUtils::GetFrameTime() - m_timer > m_toastMessageTime)
 93  {
 94    // if we have a fade label control for the text to display, ensure the whole text was shown
 95    // (scrolled to the end) before we move on to the next message
 96    const CGUIFadeLabelControl* notificationText =
 97        dynamic_cast<const CGUIFadeLabelControl*>(GetControl(POPUP_NOTIFICATION_BUTTON));
 98    if (notificationText && !notificationText->AllLabelsShown())
 99      return false;
100
101    Notification toast = m_notifications.front();
102    m_notifications.pop();
103    lock.Leave();
104
105    m_toastDisplayTime = toast.displayTime;
106    m_toastMessageTime = toast.messageTime;
107
108    CSingleLock lock2(CServiceBroker::GetWinSystem()->GetGfxContext());
109
110    if(!Initialize())
111      return false;
112
113    SET_CONTROL_LABEL(POPUP_CAPTION_TEXT, toast.caption);
114
115    SET_CONTROL_LABEL(POPUP_NOTIFICATION_BUTTON, toast.description);
116
117    // set the appropriate icon
118    {
119      std::string icon = toast.imagefile;
120      if (icon.empty())
121      {
122        if (toast.eType == Warning)
123          icon = "DefaultIconWarning.png";
124        else if (toast.eType == Error)
125          icon = "DefaultIconError.png";
126        else
127          icon = "DefaultIconInfo.png";
128      }
129      SET_CONTROL_FILENAME(POPUP_ICON, icon);
130    }
131
132    //  Play the window specific init sound for each notification queued
133    SetSound(toast.withSound);
134
135    // Activate haptics for this notification
136    CServiceBroker::GetPeripherals().OnUserNotification();
137
138    ResetTimer();
139    return true;
140  }
141
142  return false;
143}
144
145
146void CGUIDialogKaiToast::ResetTimer()
147{
148  m_timer = CTimeUtils::GetFrameTime();
149}
150
151void CGUIDialogKaiToast::FrameMove()
152{
153  //  Fading does not count as display time
154  if (IsAnimating(ANIM_TYPE_WINDOW_OPEN))
155    ResetTimer();
156
157  // now check if we should exit
158  if (CTimeUtils::GetFrameTime() - m_timer > m_toastDisplayTime)
159  {
160    bool bClose = true;
161
162    // if we have a fade label control for the text to display, ensure the whole text was shown
163    // (scrolled to the end) before we're closing the toast dialog
164    const CGUIFadeLabelControl* notificationText =
165        dynamic_cast<const CGUIFadeLabelControl*>(GetControl(POPUP_NOTIFICATION_BUTTON));
166    if (notificationText)
167    {
168      CSingleLock lock(m_critical);
169      bClose = notificationText->AllLabelsShown() && m_notifications.empty();
170    }
171
172    if (bClose)
173      Close();
174  }
175
176  CGUIDialog::FrameMove();
177}