-
Notifications
You must be signed in to change notification settings - Fork 15k
/
libnotify_notification.cc
182 lines (150 loc) · 5.8 KB
/
libnotify_notification.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/notifications/linux/libnotify_notification.h"
#include <set>
#include <string>
#include "base/files/file_enumerator.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "shell/browser/notifications/notification_delegate.h"
#include "shell/browser/ui/gtk_util.h"
#include "shell/common/application_info.h"
#include "shell/common/platform_util.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gtk/gtk_util.h" // nogncheck
namespace electron {
namespace {
LibNotifyLoader libnotify_loader_;
const std::set<std::string>& GetServerCapabilities() {
static std::set<std::string> caps;
if (caps.empty()) {
auto* capabilities = libnotify_loader_.notify_get_server_caps();
for (auto* l = capabilities; l != nullptr; l = l->next)
caps.insert(static_cast<const char*>(l->data));
g_list_free_full(capabilities, g_free);
}
return caps;
}
bool HasCapability(const std::string& capability) {
return GetServerCapabilities().count(capability) != 0;
}
bool NotifierSupportsActions() {
if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
return false;
return HasCapability("actions");
}
void log_and_clear_error(GError* error, const char* context) {
LOG(ERROR) << context << ": domain=" << error->domain
<< " code=" << error->code << " message=\"" << error->message
<< '"';
g_error_free(error);
}
} // namespace
// static
bool LibnotifyNotification::Initialize() {
if (!libnotify_loader_.Load("libnotify.so.4") && // most common one
!libnotify_loader_.Load("libnotify.so.5") &&
!libnotify_loader_.Load("libnotify.so.1") &&
!libnotify_loader_.Load("libnotify.so")) {
LOG(WARNING) << "Unable to find libnotify; notifications disabled";
return false;
}
if (!libnotify_loader_.notify_is_initted() &&
!libnotify_loader_.notify_init(GetApplicationName().c_str())) {
LOG(WARNING) << "Unable to initialize libnotify; notifications disabled";
return false;
}
return true;
}
LibnotifyNotification::LibnotifyNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter)
: Notification(delegate, presenter) {}
LibnotifyNotification::~LibnotifyNotification() {
if (notification_) {
g_signal_handlers_disconnect_by_data(notification_, this);
g_object_unref(notification_);
}
}
void LibnotifyNotification::Show(const NotificationOptions& options) {
notification_ = libnotify_loader_.notify_notification_new(
base::UTF16ToUTF8(options.title).c_str(),
base::UTF16ToUTF8(options.msg).c_str(), nullptr);
g_signal_connect(notification_, "closed",
G_CALLBACK(OnNotificationClosedThunk), this);
// NB: On Unity and on any other DE using Notify-OSD, adding a notification
// action will cause the notification to display as a modal dialog box.
if (NotifierSupportsActions()) {
libnotify_loader_.notify_notification_add_action(
notification_, "default", "View", OnNotificationViewThunk, this,
nullptr);
}
NotifyUrgency urgency = NOTIFY_URGENCY_NORMAL;
if (options.urgency == u"critical") {
urgency = NOTIFY_URGENCY_CRITICAL;
} else if (options.urgency == u"low") {
urgency = NOTIFY_URGENCY_LOW;
}
// Set the urgency level of the notification.
libnotify_loader_.notify_notification_set_urgency(notification_, urgency);
if (!options.icon.drawsNothing()) {
GdkPixbuf* pixbuf = gtk_util::GdkPixbufFromSkBitmap(options.icon);
libnotify_loader_.notify_notification_set_image_from_pixbuf(notification_,
pixbuf);
g_object_unref(pixbuf);
}
// Set the timeout duration for the notification
bool neverTimeout = options.timeout_type == u"never";
int timeout = (neverTimeout) ? NOTIFY_EXPIRES_NEVER : NOTIFY_EXPIRES_DEFAULT;
libnotify_loader_.notify_notification_set_timeout(notification_, timeout);
if (!options.tag.empty()) {
GQuark id = g_quark_from_string(options.tag.c_str());
g_object_set(G_OBJECT(notification_), "id", id, NULL);
}
// Always try to append notifications.
// Unique tags can be used to prevent this.
if (HasCapability("append")) {
libnotify_loader_.notify_notification_set_hint_string(notification_,
"append", "true");
} else if (HasCapability("x-canonical-append")) {
libnotify_loader_.notify_notification_set_hint_string(
notification_, "x-canonical-append", "true");
}
// Send the desktop name to identify the application
// The desktop-entry is the part before the .desktop
std::string desktop_id = platform_util::GetXdgAppId();
if (!desktop_id.empty()) {
libnotify_loader_.notify_notification_set_hint_string(
notification_, "desktop-entry", desktop_id.c_str());
}
GError* error = nullptr;
libnotify_loader_.notify_notification_show(notification_, &error);
if (error) {
log_and_clear_error(error, "notify_notification_show");
NotificationFailed();
return;
}
if (delegate())
delegate()->NotificationDisplayed();
}
void LibnotifyNotification::Dismiss() {
if (!notification_) {
Destroy();
return;
}
GError* error = nullptr;
libnotify_loader_.notify_notification_close(notification_, &error);
if (error) {
log_and_clear_error(error, "notify_notification_close");
Destroy();
}
}
void LibnotifyNotification::OnNotificationClosed(
NotifyNotification* notification) {
NotificationDismissed();
}
void LibnotifyNotification::OnNotificationView(NotifyNotification* notification,
char* action) {
NotificationClicked();
}
} // namespace electron