31#include <wx/filename.h>
45NotificationManager::NotificationManager() {
46 m_timeout_timer.Bind(wxEVT_TIMER, &NotificationManager::OnTimer,
this,
47 m_timeout_timer.GetId());
48 m_timeout_timer.Start(1000, wxTIMER_CONTINUOUS);
51void NotificationManager::OnTimer(wxTimerEvent& event) {
52 for (
auto note : active_notifications) {
53 if (note->GetTimeoutLeft() > 0) {
54 note->DecrementTimoutCount();
57 for (
auto note : active_notifications) {
58 if (note->GetTimeoutLeft() == 0) {
60 note->DecrementTimoutCount();
66void NotificationManager::ScrubNotificationDirectory(
int days_to_retain) {
68 wxFileName::GetPathSeparator() +
"notifications" +
69 wxFileName::GetPathSeparator();
70 if (!wxDirExists(note_directory))
return;
72 wxDateTime now = wxDateTime::Now();
73 wxArrayString file_list;
74 wxDir::GetAllFiles(note_directory, &file_list);
75 for (
size_t i = 0; i < file_list.GetCount(); i++) {
76 wxFileName fn(file_list[i]);
77 wxTimeSpan age = now.Subtract(fn.GetModificationTime());
78 if (age.IsLongerThan(wxTimeSpan(days_to_retain * 24))) {
79 wxRemoveFile(file_list[i]);
84void NotificationManager::PersistNotificationAsFile(
85 const std::shared_ptr<Notification> _notification) {
87 wxFileName::GetPathSeparator() +
"notifications" +
88 wxFileName::GetPathSeparator();
89 if (!wxDirExists(note_directory)) wxMkdir(note_directory);
90 wxString severity_prefix =
"Info_";
91 NotificationSeverity severity = _notification->GetSeverity();
92 if (severity == NotificationSeverity::kWarning)
93 severity_prefix =
"Warning_";
94 else if (severity == NotificationSeverity::kCritical)
95 severity_prefix =
"Critical_";
96 wxString file_name = wxString(_notification.get()->GetGuid().c_str());
97 file_name.Prepend(severity_prefix);
98 file_name.Prepend(note_directory);
101 wxDateTime act_time = wxDateTime(_notification->GetActivateTime());
102 wxString stime = wxString::Format(
103 "%s", ocpn::toUsrDateTimeFormat(
105 "$short_date $24_hour_minutes_seconds")));
107 std::stringstream ss;
108 ss << stime.ToStdString() << std::endl;
109 ss << _notification->GetMessage() << std::endl;
111 std::ofstream outputFile(file_name.ToStdString().c_str(), std::ios::out);
112 if (outputFile.is_open()) {
113 outputFile << ss.str();
119 for (
auto note : active_notifications) {
120 int severity =
static_cast<int>(note->GetSeverity());
121 if (severity > rv) rv = severity;
123 return static_cast<NotificationSeverity
>(rv);
126std::string NotificationManager::AddNotification(
127 std::shared_ptr<Notification> _notification) {
128 active_notifications.push_back(_notification);
129 PersistNotificationAsFile(_notification);
133 auto msg = std::make_shared<NotificationMsg>(
"POST", _notification);
134 AppMsgBus::GetInstance().
Notify(std::move(msg));
136 return _notification->GetGuid();
139std::string NotificationManager::AddNotification(NotificationSeverity _severity,
140 const std::string& _message,
143 std::make_shared<Notification>(_severity, _message, _timeout_secs);
144 return AddNotification(notification);
148 if (!GUID.length())
return false;
150 size_t target_message_hash = 0;
151 for (
auto it = active_notifications.begin();
152 it != active_notifications.end();) {
153 if ((*it)->GetGuid() == GUID) {
154 target_message_hash = (*it)->GetStringHash();
159 if (!target_message_hash)
return false;
164 while (!done && active_notifications.size()) {
165 for (
auto it = active_notifications.begin();
166 it != active_notifications.end();) {
167 if ((*it)->GetStringHash() == target_message_hash) {
169 auto msg = std::make_shared<NotificationMsg>(
"ACK", *it);
170 AppMsgBus::GetInstance().
Notify(std::move(msg));
173 active_notifications.erase(it);
179 if (it == active_notifications.end()) done =
true;
190bool NotificationManager::AcknowledgeAllNotifications() {
193 while (active_notifications.size()) {
void Notify(const std::shared_ptr< const AppMsg > &msg)
Send message to everyone listening to given message type.
void Notify() override
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.
Decoded messages send/receive support.
Navigation Utility Functions without GUI dependencies.
User notification container.
User notifications manager.