32#include "model/base_platform.h"
33#include "model/navutil_base.h"
36#include "wx/filename.h"
37#include "model/datetime.h"
38#include "model/comm_appmsg_bus.h"
41extern std::shared_ptr<ObservableListener> ack_listener;
48NotificationManager::NotificationManager() {
49 m_timeout_timer.Bind(wxEVT_TIMER, &NotificationManager::OnTimer,
this,
50 m_timeout_timer.GetId());
51 m_timeout_timer.Start(1000, wxTIMER_CONTINUOUS);
54void NotificationManager::OnTimer(wxTimerEvent& event) {
55 for (
auto note : active_notifications) {
56 if (note->GetTimeoutLeft() > 0) {
57 note->DecrementTimoutCount();
60 for (
auto note : active_notifications) {
61 if (note->GetTimeoutLeft() == 0) {
63 note->DecrementTimoutCount();
69void NotificationManager::ScrubNotificationDirectory(
int days_to_retain) {
71 wxFileName::GetPathSeparator() +
"notifications" +
72 wxFileName::GetPathSeparator();
73 if (!wxDirExists(note_directory))
return;
75 wxDateTime now = wxDateTime::Now();
76 wxArrayString file_list;
77 wxDir::GetAllFiles(note_directory, &file_list);
78 for (
size_t i = 0; i < file_list.GetCount(); i++) {
79 wxFileName fn(file_list[i]);
80 wxTimeSpan age = now.Subtract(fn.GetModificationTime());
81 if (age.IsLongerThan(wxTimeSpan(days_to_retain * 24))) {
82 wxRemoveFile(file_list[i]);
87void NotificationManager::PersistNotificationAsFile(
88 const std::shared_ptr<Notification> _notification) {
90 wxFileName::GetPathSeparator() +
"notifications" +
91 wxFileName::GetPathSeparator();
92 if (!wxDirExists(note_directory)) wxMkdir(note_directory);
93 wxString severity_prefix =
"Info_";
94 NotificationSeverity severity = _notification->GetSeverity();
95 if (severity == NotificationSeverity::kWarning)
96 severity_prefix =
"Warning_";
97 else if (severity == NotificationSeverity::kCritical)
98 severity_prefix =
"Critical_";
99 wxString file_name = wxString(_notification.get()->GetGuid().c_str());
100 file_name.Prepend(severity_prefix);
101 file_name.Prepend(note_directory);
104 wxDateTime act_time = wxDateTime(_notification->GetActivateTime());
105 wxString stime = wxString::Format(
106 "%s", ocpn::toUsrDateTimeFormat(
108 "$short_date $24_hour_minutes_seconds")));
110 std::stringstream ss;
111 ss << stime.ToStdString() << std::endl;
112 ss << _notification->GetMessage() << std::endl;
114 std::ofstream outputFile(file_name.ToStdString().c_str(), std::ios::out);
115 if (outputFile.is_open()) {
116 outputFile << ss.str();
122 for (
auto note : active_notifications) {
123 int severity =
static_cast<int>(note->GetSeverity());
124 if (severity > rv) rv = severity;
126 return static_cast<NotificationSeverity
>(rv);
129std::string NotificationManager::AddNotification(
130 std::shared_ptr<Notification> _notification) {
131 active_notifications.push_back(_notification);
132 PersistNotificationAsFile(_notification);
136 auto msg = std::make_shared<NotificationMsg>(
"POST", _notification);
137 AppMsgBus::GetInstance().
Notify(std::move(msg));
139 return _notification->GetGuid();
142std::string NotificationManager::AddNotification(NotificationSeverity _severity,
143 const std::string& _message,
146 std::make_shared<Notification>(_severity, _message, _timeout_secs);
147 return AddNotification(notification);
151 if (!GUID.length())
return false;
153 size_t target_message_hash = 0;
154 for (
auto it = active_notifications.begin();
155 it != active_notifications.end();) {
156 if ((*it)->GetGuid() == GUID) {
157 target_message_hash = (*it)->GetStringHash();
162 if (!target_message_hash)
return false;
167 while (!done && active_notifications.size()) {
168 for (
auto it = active_notifications.begin();
169 it != active_notifications.end();) {
170 if ((*it)->GetStringHash() == target_message_hash) {
172 auto msg = std::make_shared<NotificationMsg>(
"ACK", *it);
173 AppMsgBus::GetInstance().
Notify(std::move(msg));
176 active_notifications.erase(it);
182 if (it == active_notifications.end()) done =
true;
193bool NotificationManager::AcknowledgeAllNotifications() {
196 while (active_notifications.size()) {
void Notify(const std::shared_ptr< const AppMsg > &msg)
Send message to everyone listening to given message type.
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 NotificationManager.