gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/desktop/notification/notify.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2020 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 // Package notification implements bindings to D-Bus notification 21 // specification, version 1.2, as documented at https://developer.gnome.org/notification-spec/ 22 package notification 23 24 import ( 25 "fmt" 26 "time" 27 ) 28 29 // Message describes a single notification message. 30 // 31 // The notification should be related to a specific application, as indicated by 32 // AppName. In practice this should be the name of the desktop file and could be 33 // also accompanied by an appropriate hint indicating which icon to use. 34 // 35 // Message must include a summary and should include a body. The body may use 36 // HTML-like markup language to include bold, italic or underline text, as well 37 // as to include images and hyperlinks. 38 // 39 // A notification can automatically expire after the given number of 40 // milliseconds. This is separate from the notification being visible or 41 // invisible on-screen. Expired notifications are removed from persistent 42 // message roster, if one is supported. Two special values are recognized. When 43 // the expiration timeout is zero a message never expires. When the expiration 44 // timeout is -1 a message expires after a server-defined duration which may 45 // vary for the type of the notification message sent. 46 // 47 // A notification may replace an existing notification by setting the ReplacesID 48 // to a non-zero value. This only works if the notification server was not 49 // re-started and should be used for as long as the sender process is alive, as 50 // sending the identifier across session startup boundary has no chance of 51 // working correctly. 52 // 53 // A notification may optionally carry a number of hints that further customize it 54 // in a specific way. Refer to various hint constructors for details. 55 // 56 // A notification may optionally also carry one of several actions. If 57 // supported, actions can be invoked by the user, broadcasting a notification 58 // response back to the session. This mechanism only works if there is someone 59 // listening for the action being triggered. 60 // 61 // In all cases, the specific notification must take into account the 62 // capabilities of the server. For instance, if a server does not support body 63 // markup, then such markup is not automatically stripped by either the client 64 // or the server. 65 type Message struct { 66 AppName string 67 Icon string 68 Summary string 69 Body string 70 ExpireTimeout time.Duration // Effective resolution in milliseconds with 31-bit range. 71 ReplacesID ID 72 Actions []Action 73 Hints []Hint 74 } 75 76 // ServerSelectedExpireTimeout requests the server to pick an expiration timeout 77 // appropriate for the message type. 78 const ServerSelectedExpireTimeout = time.Millisecond * -1 79 80 // ID is the opaque identifier of a notification assigned by the server. 81 // 82 // Notifications with known identifiers can be closed or updated. The identifier 83 // is valid within one desktop session and should not be used unless the calling 84 // process initially sent the message. 85 type ID uint32 86 87 // Action describes a single notification action. 88 // 89 // ActionKey is returned in an D-Bus signal when an action is activated by the 90 // user. The text must be localized for the appropriate language. 91 type Action struct { 92 ActionKey string 93 LocalizedText string 94 } 95 96 // Hint describes supplementeary information that may be used by the server. 97 // 98 // Various helpers create hint objects of specifc purpose. 99 // 100 // Specification: https://developer.gnome.org/notification-spec/#hints 101 type Hint struct { 102 Name string 103 Value interface{} 104 } 105 106 // ServerCapability describes a single capability of the notification server. 107 type ServerCapability string 108 109 // CloseReason indicates why a notification message was closed. 110 type CloseReason uint32 111 112 const ( 113 // CloseReasonExpired indicates that a notification message has expired. 114 CloseReasonExpired CloseReason = 1 115 // CloseReasonDismissed indicates that a notification message was dismissed by the user. 116 CloseReasonDismissed CloseReason = 2 117 // CloseReasonClosed indicates that a notification message was closed with an API call. 118 CloseReasonClosed CloseReason = 3 119 // CloseReasonUndefined indicates that no other well-known reason applies. 120 CloseReasonUndefined CloseReason = 4 121 ) 122 123 // String implements the Stringer interface. 124 func (r CloseReason) String() string { 125 switch r { 126 case CloseReasonExpired: 127 return "expired" 128 case CloseReasonDismissed: 129 return "dismissed" 130 case CloseReasonClosed: 131 return "closed" 132 case CloseReasonUndefined: 133 return "undefined" 134 default: 135 return fmt.Sprintf("CloseReason(%d)", uint32(r)) 136 } 137 } 138 139 // Observer is an interface for observing interactions with notification messages. 140 // 141 // An observer can be used to either observe a notification being closed or 142 // dismissed or to react to actions being invoked by the user. Practical 143 // implementations must remember the ID of the message they have sent to filter 144 // out other notifications. 145 type Observer interface { 146 // NotificationClosed is called when a notification is either closed or removed 147 // from the persistent roster. 148 NotificationClosed(id ID, reason CloseReason) error 149 // ActionInvoked is caliled when one of the notification message actions is 150 // clicked by the user. 151 ActionInvoked(id ID, actionKey string) error 152 }