OpenCPN Partial API docs
Loading...
Searching...
No Matches
notification_manager.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: Notification Manager
5 * Author: David Register
6 *
7 ***************************************************************************
8 * Copyright (C) 2025 by David S. Register *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25#include <cmath>
26#include <memory>
27#include <vector>
28
29#include "model/navutil_base.h"
30#include "model/notification.h"
32
33NotificationManager& NotificationManager::GetInstance() {
34 static NotificationManager instance;
35 return instance;
36}
37
38NotificationManager::NotificationManager() {
39 m_timeout_timer.Bind(wxEVT_TIMER, &NotificationManager::OnTimer, this,
40 m_timeout_timer.GetId());
41 m_timeout_timer.Start(1000, wxTIMER_CONTINUOUS);
42}
43
44void NotificationManager::OnTimer(wxTimerEvent& event) {
45 for (auto note : active_notifications) {
46 if (note->GetTimeoutCount() > 0) {
47 note->DecrementTimoutCount();
48 }
49 }
50 for (auto note : active_notifications) {
51 if (note->GetTimeoutCount() == 0) {
52 AcknowledgeNotification(note->GetGuid());
53 note->DecrementTimoutCount();
54 break;
55 }
56 }
57}
58
60 int rv = 0;
61 for (auto note : active_notifications) {
62 int severity = static_cast<int>(note->GetSeverity());
63 if (severity > rv) rv = severity;
64 }
65 return static_cast<NotificationSeverity>(rv);
66}
67
68std::string NotificationManager::AddNotification(
69 std::shared_ptr<Notification> _notification) {
70 active_notifications.push_back(_notification);
72 return _notification->GetGuid();
73}
74
75std::string NotificationManager::AddNotification(NotificationSeverity _severity,
76 const std::string& _message,
77 int _timeout_secs) {
78 auto notification =
79 std::make_shared<Notification>(_severity, _message, _timeout_secs);
80 active_notifications.push_back(notification);
82 return notification->GetGuid();
83}
84
85bool NotificationManager::AcknowledgeNotification(const std::string& GUID) {
86 if (!GUID.length()) return false;
87
88 size_t target_message_hash = 0;
89 for (auto it = active_notifications.begin();
90 it != active_notifications.end();) {
91 if ((*it)->GetGuid() == GUID) {
92 target_message_hash = (*it)->GetStringHash();
93 break;
94 } else
95 ++it;
96 }
97 if (!target_message_hash) return false;
98
99 // erase multiple notifications with identical message_hash
100 bool rv = false;
101 bool done = false;
102 while (!done && active_notifications.size()) {
103 for (auto it = active_notifications.begin();
104 it != active_notifications.end();) {
105 if ((*it)->GetStringHash() == target_message_hash) {
106 active_notifications.erase(it);
107 rv = true;
108 break;
109 } else
110 ++it;
111 if (it == active_notifications.end()) done = true;
112 }
113 }
114
116
117 return rv;
118}
const void Notify()
Notify all listeners, no data supplied.
The global list of user notifications, a singleton.
bool AcknowledgeNotification(const std::string &guid)
User ack on a notification which eventually will remove it.
EventVar evt_notificationlist_change
Notified without data when a notification is added or removed.
NotificationSeverity GetMaxSeverity()
Return max severity among current active notifications.
Class Notification.
Class NotificationManager.