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) {
67 if (g_disableNotifications)
return;
69 wxFileName::GetPathSeparator() +
"notifications" +
70 wxFileName::GetPathSeparator();
71 if (!wxDirExists(note_directory))
return;
73 wxDateTime now = wxDateTime::Now();
74 wxArrayString file_list;
75 wxDir::GetAllFiles(note_directory, &file_list);
76 for (
size_t i = 0; i < file_list.GetCount(); i++) {
77 wxFileName fn(file_list[i]);
78 wxTimeSpan age = now.Subtract(fn.GetModificationTime());
79 if (age.IsLongerThan(wxTimeSpan(days_to_retain * 24))) {
80 wxRemoveFile(file_list[i]);
85void NotificationManager::PersistNotificationAsFile(
86 const std::shared_ptr<Notification> _notification) {
88 wxFileName::GetPathSeparator() +
"notifications" +
89 wxFileName::GetPathSeparator();
90 if (!wxDirExists(note_directory)) wxMkdir(note_directory);
91 wxString severity_prefix =
"Info_";
92 NotificationSeverity severity = _notification->GetSeverity();
93 if (severity == NotificationSeverity::kWarning)
94 severity_prefix =
"Warning_";
95 else if (severity == NotificationSeverity::kCritical)
96 severity_prefix =
"Critical_";
97 wxString file_name = wxString(_notification.get()->GetGuid().c_str());
98 file_name.Prepend(severity_prefix);
99 file_name.Prepend(note_directory);
102 wxDateTime act_time = wxDateTime(_notification->GetActivateTime());
103 wxString stime = wxString::Format(
104 "%s", ocpn::toUsrDateTimeFormat(
106 "$short_date $24_hour_minutes_seconds")));
108 std::stringstream ss;
109 ss << stime.ToStdString() << std::endl;
110 ss << _notification->GetMessage() << std::endl;
112 std::ofstream outputFile(file_name.ToStdString().c_str(), std::ios::out);
113 if (outputFile.is_open()) {
114 outputFile << ss.str();
120 for (
auto note : active_notifications) {
121 int severity =
static_cast<int>(note->GetSeverity());
122 if (severity > rv) rv = severity;
124 return static_cast<NotificationSeverity
>(rv);
127std::string NotificationManager::AddNotification(
128 std::shared_ptr<Notification> _notification) {
129 if (g_disableNotifications)
return "";
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.
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.