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 "browser/linux/libnotify_notification.h"
|
|
|
|
|
|
|
|
#include "base/files/file_enumerator.h"
|
|
|
|
#include "base/strings/string_util.h"
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "browser/notification_delegate.h"
|
|
|
|
#include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
|
|
|
|
#include "common/application_info.h"
|
|
|
|
#include "third_party/skia/include/core/SkBitmap.h"
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
|
|
|
namespace {
|
2016-04-13 02:43:22 +00:00
|
|
|
LibNotifyLoader libnotify_loader_;
|
2015-12-25 03:52:19 +00:00
|
|
|
|
2016-04-14 16:24:00 +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;
|
|
|
|
}
|
|
|
|
|
2016-04-13 02:43:22 +00:00
|
|
|
bool NotifierSupportsActions() {
|
2015-12-25 03:52:19 +00:00
|
|
|
if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
|
2016-04-13 02:43:22 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
static bool notify_has_result = false;
|
|
|
|
static bool notify_result = false;
|
|
|
|
GList *capabilities = NULL;
|
|
|
|
|
|
|
|
if (notify_has_result)
|
|
|
|
return notify_result;
|
2015-12-25 03:52:19 +00:00
|
|
|
|
2016-04-14 16:24:00 +00:00
|
|
|
notify_result = HasCapability("actions");
|
2016-04-13 02:43:22 +00:00
|
|
|
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
|
|
|
|
Notification* Notification::Create(NotificationDelegate* delegate,
|
|
|
|
NotificationPresenter* presenter) {
|
|
|
|
return new LibnotifyNotification(delegate, presenter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool LibnotifyNotification::Initialize() {
|
|
|
|
if (!libnotify_loader_.Load("libnotify.so.4") &&
|
|
|
|
!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() {
|
2016-04-13 02:43:22 +00:00
|
|
|
g_signal_handlers_disconnect_by_data(notification_, this);
|
2015-12-25 03:52:19 +00:00
|
|
|
g_object_unref(notification_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibnotifyNotification::Show(const base::string16& title,
|
|
|
|
const base::string16& body,
|
2016-04-13 03:31:56 +00:00
|
|
|
const std::string& tag,
|
2015-12-25 03:52:19 +00:00
|
|
|
const GURL& icon_url,
|
2016-01-21 00:36:41 +00:00
|
|
|
const SkBitmap& icon,
|
|
|
|
const bool silent) {
|
2015-12-25 03:52:19 +00:00
|
|
|
notification_ = libnotify_loader_.notify_notification_new(
|
|
|
|
base::UTF16ToUTF8(title).c_str(),
|
|
|
|
base::UTF16ToUTF8(body).c_str(),
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
g_signal_connect(
|
|
|
|
notification_, "closed", G_CALLBACK(OnNotificationClosedThunk), this);
|
|
|
|
|
2016-04-13 02:43:22 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!icon.drawsNothing()) {
|
|
|
|
GdkPixbuf* pixbuf = libgtk2ui::GdkPixbufFromSkBitmap(icon);
|
|
|
|
libnotify_loader_.notify_notification_set_image_from_pixbuf(
|
|
|
|
notification_, pixbuf);
|
|
|
|
libnotify_loader_.notify_notification_set_timeout(
|
|
|
|
notification_, NOTIFY_EXPIRES_DEFAULT);
|
|
|
|
g_object_unref(pixbuf);
|
|
|
|
}
|
|
|
|
|
2016-04-13 04:08:35 +00:00
|
|
|
if (!tag.empty()) {
|
|
|
|
GQuark id = g_quark_from_string(tag.c_str());
|
|
|
|
g_object_set(G_OBJECT(notification_), "id", id, NULL);
|
|
|
|
}
|
|
|
|
|
2016-04-14 16:31:02 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
delegate()->NotificationDisplayed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibnotifyNotification::Dismiss() {
|
|
|
|
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) {
|
2016-04-15 07:14:13 +00:00
|
|
|
NotificationDismissed();
|
2015-12-25 03:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibnotifyNotification::OnNotificationView(
|
|
|
|
NotifyNotification* notification, char* action) {
|
2016-04-15 07:14:13 +00:00
|
|
|
NotificationClicked();
|
2015-12-25 03:52:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace brightray
|