2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "GUIDialogKaiToast.h"
11 #include "ServiceBroker.h"
12 #include "guilib/GUIFadeLabelControl.h"
13 #include "guilib/GUIMessage.h"
14 #include "peripherals/Peripherals.h"
15 #include "utils/TimeUtils.h"
19 #define POPUP_ICON 400
20 #define POPUP_CAPTION_TEXT 401
21 #define POPUP_NOTIFICATION_BUTTON 402
23 CGUIDialogKaiToast::TOASTQUEUE
CGUIDialogKaiToast::m_notifications
;
24 CCriticalSection
CGUIDialogKaiToast::m_critical
;
26 CGUIDialogKaiToast::CGUIDialogKaiToast(void)
27 : CGUIDialog(WINDOW_DIALOG_KAI_TOAST
, "DialogNotification.xml", DialogModalityType::MODELESS
)
29 m_loadType
= LOAD_ON_GUI_INIT
;
31 m_toastDisplayTime
= 0;
32 m_toastMessageTime
= 0;
35 CGUIDialogKaiToast::~CGUIDialogKaiToast(void) = default;
37 bool CGUIDialogKaiToast::OnMessage(CGUIMessage
& message
)
39 switch ( message
.GetMessage() )
41 case GUI_MSG_WINDOW_INIT
:
43 CGUIDialog::OnMessage(message
);
49 case GUI_MSG_WINDOW_DEINIT
:
54 return CGUIDialog::OnMessage(message
);
57 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*/)
59 AddToQueue("", eType
, aCaption
, aDescription
, displayTime
, withSound
, messageTime
);
62 void CGUIDialogKaiToast::QueueNotification(const std::string
& aCaption
, const std::string
& aDescription
)
64 QueueNotification("", aCaption
, aDescription
);
67 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*/)
69 AddToQueue(aImageFile
, Default
, aCaption
, aDescription
, displayTime
, withSound
, messageTime
);
72 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*/)
74 std::unique_lock
<CCriticalSection
> lock(m_critical
);
76 if (!m_notifications
.empty())
78 const auto& last
= m_notifications
.back();
79 if (last
.eType
== eType
&& last
.imagefile
== aImageFile
&& last
.caption
== aCaption
&&
80 last
.description
== aDescription
)
81 return; // avoid duplicates in a row.
86 toast
.imagefile
= aImageFile
;
87 toast
.caption
= aCaption
;
88 toast
.description
= aDescription
;
89 toast
.displayTime
= displayTime
> TOAST_MESSAGE_TIME
+ 500 ? displayTime
: TOAST_MESSAGE_TIME
+ 500;
90 toast
.messageTime
= messageTime
;
91 toast
.withSound
= withSound
;
93 m_notifications
.push(toast
);
96 bool CGUIDialogKaiToast::DoWork()
98 std::unique_lock
<CCriticalSection
> lock(m_critical
);
100 if (!m_notifications
.empty() &&
101 CTimeUtils::GetFrameTime() - m_timer
> m_toastMessageTime
)
103 // if we have a fade label control for the text to display, ensure the whole text was shown
104 // (scrolled to the end) before we move on to the next message
105 const CGUIFadeLabelControl
* notificationText
=
106 dynamic_cast<const CGUIFadeLabelControl
*>(GetControl(POPUP_NOTIFICATION_BUTTON
));
107 if (notificationText
&& !notificationText
->AllLabelsShown())
110 Notification toast
= m_notifications
.front();
111 m_notifications
.pop();
114 m_toastDisplayTime
= toast
.displayTime
;
115 m_toastMessageTime
= toast
.messageTime
;
117 std::unique_lock
<CCriticalSection
> lock2(CServiceBroker::GetWinSystem()->GetGfxContext());
122 SET_CONTROL_LABEL(POPUP_CAPTION_TEXT
, toast
.caption
);
124 SET_CONTROL_LABEL(POPUP_NOTIFICATION_BUTTON
, toast
.description
);
126 // set the appropriate icon
128 std::string icon
= toast
.imagefile
;
131 if (toast
.eType
== Warning
)
132 icon
= "DefaultIconWarning.png";
133 else if (toast
.eType
== Error
)
134 icon
= "DefaultIconError.png";
136 icon
= "DefaultIconInfo.png";
138 SET_CONTROL_FILENAME(POPUP_ICON
, icon
);
141 // Play the window specific init sound for each notification queued
142 SetSound(toast
.withSound
);
144 // Activate haptics for this notification
145 CServiceBroker::GetPeripherals().OnUserNotification();
155 void CGUIDialogKaiToast::ResetTimer()
157 m_timer
= CTimeUtils::GetFrameTime();
160 void CGUIDialogKaiToast::FrameMove()
162 // Fading does not count as display time
163 if (IsAnimating(ANIM_TYPE_WINDOW_OPEN
))
166 // now check if we should exit
167 if (CTimeUtils::GetFrameTime() - m_timer
> m_toastDisplayTime
)
171 // if we have a fade label control for the text to display, ensure the whole text was shown
172 // (scrolled to the end) before we're closing the toast dialog
173 const CGUIFadeLabelControl
* notificationText
=
174 dynamic_cast<const CGUIFadeLabelControl
*>(GetControl(POPUP_NOTIFICATION_BUTTON
));
175 if (notificationText
)
177 std::unique_lock
<CCriticalSection
> lock(m_critical
);
178 bClose
= notificationText
->AllLabelsShown() && m_notifications
.empty();
185 CGUIDialog::FrameMove();