feat: allow Linux/Windows users to set notification timeout (#20153)

* feat: allow Linux users to set notification timeout

* implement on windows
This commit is contained in:
Shelley Vohr 2019-10-09 17:22:21 +02:00 committed by John Kleinschmidt
parent 5e11be6898
commit f80a17c5be
7 changed files with 92 additions and 3 deletions

View file

@ -114,11 +114,14 @@ void LibnotifyNotification::Show(const NotificationOptions& options) {
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(options.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);
}
// Set the timeout duration for the notification
bool neverTimeout = options.timeout_type == base::ASCIIToUTF16("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);

View file

@ -32,6 +32,7 @@ struct NotificationOptions {
GURL icon_url;
SkBitmap icon;
bool has_reply;
base::string16 timeout_type;
base::string16 reply_placeholder;
base::string16 sound;
base::string16 urgency; // Linux

View file

@ -94,7 +94,8 @@ void WindowsToastNotification::Show(const NotificationOptions& options) {
ComPtr<IXmlDocument> toast_xml;
if (FAILED(GetToastXml(toast_manager_.Get(), options.title, options.msg,
icon_path, options.silent, &toast_xml))) {
icon_path, options.timeout_type, options.silent,
&toast_xml))) {
NotificationFailed();
return;
}
@ -149,6 +150,7 @@ bool WindowsToastNotification::GetToastXml(
const std::wstring& title,
const std::wstring& msg,
const std::wstring& icon_path,
const std::wstring& timeout_type,
bool silent,
IXmlDocument** toast_xml) {
ABI::Windows::UI::Notifications::ToastTemplateType template_type;
@ -183,6 +185,15 @@ bool WindowsToastNotification::GetToastXml(
}
}
// Configure the toast's timeout settings
if (timeout_type == base::ASCIIToUTF16("never")) {
if (FAILED(SetXmlScenarioReminder(*toast_xml))) {
if (IsDebuggingNotifications())
LOG(INFO) << "Setting \"scenario\" option on notification failed";
return false;
}
}
// Configure the toast's notification sound
if (silent) {
if (FAILED(SetXmlAudioSilent(*toast_xml))) {
@ -201,6 +212,56 @@ bool WindowsToastNotification::GetToastXml(
return true;
}
bool WindowsToastNotification::SetXmlScenarioReminder(IXmlDocument* doc) {
ScopedHString tag(L"toast");
if (!tag.success())
return false;
ComPtr<IXmlNodeList> node_list;
if (FAILED(doc->GetElementsByTagName(tag, &node_list)))
return false;
// Check that root "toast" node exists
ComPtr<IXmlNode> root;
if (FAILED(node_list->Item(0, &root)))
return false;
// get attributes of root "toast" node
ComPtr<IXmlNamedNodeMap> attributes;
if (FAILED(root->get_Attributes(&attributes)))
return false;
ComPtr<IXmlAttribute> scenario_attribute;
ScopedHString scenario_str(L"scenario");
if (FAILED(doc->CreateAttribute(scenario_str, &scenario_attribute)))
return false;
ComPtr<IXmlNode> scenario_attribute_node;
if (FAILED(scenario_attribute.As(&scenario_attribute_node)))
return false;
ScopedHString scenario_value(L"reminder");
if (!scenario_value.success())
return false;
ComPtr<IXmlText> scenario_text;
if (FAILED(doc->CreateTextNode(scenario_value, &scenario_text)))
return false;
ComPtr<IXmlNode> scenario_node;
if (FAILED(scenario_text.As(&scenario_node)))
return false;
ComPtr<IXmlNode> child_node;
if (FAILED(scenario_attribute_node->AppendChild(scenario_node.Get(),
&child_node)))
return false;
ComPtr<IXmlNode> scenario_attribute_pnode;
return SUCCEEDED(attributes.Get()->SetNamedItem(scenario_attribute_node.Get(),
&scenario_attribute_pnode));
}
bool WindowsToastNotification::SetXmlAudioSilent(IXmlDocument* doc) {
ScopedHString tag(L"toast");
if (!tag.success())

View file

@ -63,9 +63,11 @@ class WindowsToastNotification : public Notification {
const std::wstring& title,
const std::wstring& msg,
const std::wstring& icon_path,
const std::wstring& timeout_type,
const bool silent,
ABI::Windows::Data::Xml::Dom::IXmlDocument** toastXml);
bool SetXmlAudioSilent(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc);
bool SetXmlScenarioReminder(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc);
bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,
const std::wstring& text);
bool SetXmlText(ABI::Windows::Data::Xml::Dom::IXmlDocument* doc,