electron/brightray/browser/linux/libnotify_notification.cc

166 lines
4.7 KiB
C++
Raw Normal View History

2015-12-25 03:52:19 +00:00
// 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 "brightray/browser/linux/libnotify_notification.h"
2015-12-25 03:52:19 +00:00
#include <vector>
2015-12-25 03:52:19 +00:00
#include "base/files/file_enumerator.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "brightray/browser/notification_delegate.h"
#include "brightray/common/application_info.h"
2017-01-26 10:54:24 +00:00
#include "chrome/browser/ui/libgtkui/skia_utils_gtk.h"
2015-12-25 03:52:19 +00:00
#include "third_party/skia/include/core/SkBitmap.h"
namespace brightray {
namespace {
2016-05-17 11:30:53 +00:00
LibNotifyLoader libnotify_loader_;
2015-12-25 03:52:19 +00:00
bool HasCapability(const std::string& capability) {
bool result = false;
GList* capabilities = libnotify_loader_.notify_get_server_caps();
if (g_list_find_custom(capabilities, capability.c_str(),
(GCompareFunc)g_strcmp0) != NULL)
result = true;
g_list_free_full(capabilities, g_free);
return result;
}
bool NotifierSupportsActions() {
2015-12-25 03:52:19 +00:00
if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
return false;
static bool notify_has_result = false;
static bool notify_result = false;
if (notify_has_result)
return notify_result;
2015-12-25 03:52:19 +00:00
notify_result = HasCapability("actions");
return notify_result;
2015-12-25 03:52:19 +00:00
}
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() {
2016-05-17 11:30:53 +00:00
if (!libnotify_loader_.Load("libnotify.so.4") && // most common one
!libnotify_loader_.Load("libnotify.so.5") &&
2015-12-25 03:52:19 +00:00
!libnotify_loader_.Load("libnotify.so.1") &&
!libnotify_loader_.Load("libnotify.so")) {
return false;
}
if (!libnotify_loader_.notify_is_initted() &&
!libnotify_loader_.notify_init(GetApplicationName().c_str())) {
return false;
}
return true;
}
LibnotifyNotification::LibnotifyNotification(NotificationDelegate* delegate,
NotificationPresenter* presenter)
: Notification(delegate, presenter),
notification_(nullptr) {
}
LibnotifyNotification::~LibnotifyNotification() {
if (notification_) {
g_signal_handlers_disconnect_by_data(notification_, this);
g_object_unref(notification_);
}
2015-12-25 03:52:19 +00:00
}
2017-06-24 11:03:27 +00:00
void LibnotifyNotification::Show(const NotificationOptions& options) {
2015-12-25 03:52:19 +00:00
notification_ = libnotify_loader_.notify_notification_new(
2017-06-24 11:03:27 +00:00
base::UTF16ToUTF8(options.title).c_str(),
base::UTF16ToUTF8(options.msg).c_str(),
2015-12-25 03:52:19 +00:00
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()) {
2015-12-25 03:52:19 +00:00
libnotify_loader_.notify_notification_add_action(
notification_, "default", "View", OnNotificationViewThunk, this,
nullptr);
}
2017-06-24 11:03:27 +00:00
if (!options.icon.drawsNothing()) {
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(options.icon);
2015-12-25 03:52:19 +00:00
libnotify_loader_.notify_notification_set_image_from_pixbuf(
notification_, pixbuf);
libnotify_loader_.notify_notification_set_timeout(
notification_, NOTIFY_EXPIRES_DEFAULT);
g_object_unref(pixbuf);
}
2017-06-24 11:03:27 +00:00
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");
}
2015-12-25 03:52:19 +00:00
GError* error = nullptr;
libnotify_loader_.notify_notification_show(notification_, &error);
if (error) {
log_and_clear_error(error, "notify_notification_show");
NotificationFailed();
return;
}
2017-05-30 09:06:51 +00:00
if (delegate())
delegate()->NotificationDisplayed();
2015-12-25 03:52:19 +00:00
}
void LibnotifyNotification::Dismiss() {
if (!notification_) {
Destroy();
return;
}
2015-12-25 03:52:19 +00:00
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();
2015-12-25 03:52:19 +00:00
}
void LibnotifyNotification::OnNotificationView(
NotifyNotification* notification, char* action) {
NotificationClicked();
2015-12-25 03:52:19 +00:00
}
} // namespace brightray