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