pax_global_header00006660000000000000000000000064137615742020014521gustar00rootroot0000000000000052 comment=dffea856b8169ebdd9cb26b59ccd068de2be4ac2 go-notify-0.2.0/000077500000000000000000000000001376157420200134335ustar00rootroot00000000000000go-notify-0.2.0/LICENSE000066400000000000000000000024151376157420200144420ustar00rootroot00000000000000Copyright (c) 2015, Jason Ayre All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. go-notify-0.2.0/README.md000066400000000000000000000022601376157420200147120ustar00rootroot00000000000000# go-notify [![PkgGoDev](https://pkg.go.dev/badge/github.com/TheCreeper/go-notify)](https://pkg.go.dev/github.com/TheCreeper/go-notify) Package notify provides an implementation of the Gnome DBus [Notifications Specification](https://developer.gnome.org/notification-spec). ## Examples Display a simple notification. ```Go ntf := notify.NewNotification("Test Notification", "Just a test") if _, err := ntf.Show(); err != nil { return } ``` Display a notification with an icon. Consult the [Icon Naming Specification](http://standards.freedesktop.org/icon-naming-spec). ```Go ntf := notify.NewNotification("Test Notification", "Just a test") ntf.AppIcon = "network-wireless" if _, err := ntf.Show(); err != nil { return } ``` Display a notification that never expires. ```Go ntf := notify.NewNotification("Test Notification", "Just a test") ntf.Timeout = notify.ExpiresNever if _, err := ntf.Show(); err != nil { return } ``` Play a sound with the notification. ```Go ntf := notify.NewNotification("Test Notification", "Just a test") ntf.Hints = make(map[string]interface{}) ntf.Hints[notify.HintSoundFile] = "/home/my-username/sound.oga" if _, err := ntf.Show(); err != nil { return } ``` go-notify-0.2.0/go.mod000066400000000000000000000001321376157420200145350ustar00rootroot00000000000000module github.com/TheCreeper/go-notify go 1.12 require github.com/godbus/dbus/v5 v5.0.3 go-notify-0.2.0/go.sum000066400000000000000000000002511376157420200145640ustar00rootroot00000000000000github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME= github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= go-notify-0.2.0/notify.go000066400000000000000000000164321376157420200153000ustar00rootroot00000000000000// Package notify provides an implementation of the Gnome DBus notifications // specification. package notify import "github.com/godbus/dbus/v5" // Notification object paths and interfaces. const ( DbusObjectPath = "/org/freedesktop/Notifications" DbusInterfacePath = "org.freedesktop.Notifications" SignalNotificationClosed = "org.freedesktop.Notifications.NotificationClosed" SignalActionInvoked = "org.freedesktop.Notifications.ActionInvoked" CallGetCapabilities = "org.freedesktop.Notifications.GetCapabilities" CallCloseNotification = "org.freedesktop.Notifications.CloseNotification" CallNotify = "org.freedesktop.Notifications.Notify" CallGetServerInformation = "org.freedesktop.Notifications.GetServerInformation" DbusMemberActionInvoked = "ActionInvoked" DbusMemberNotificationClosed = "NotificationClosed" ) // Notification expire timeout. const ( ExpiresDefault = -1 ExpiresNever = 0 ) // Notification Categories const ( ClassDevice = "device" ClassDeviceAdded = "device.added" ClassDeviceError = "device.error" ClassDeviceRemoved = "device.removed" ClassEmail = "email" ClassEmailArrived = "email.arrived" ClassEmailBounced = "email.bounced" ClassIm = "im" ClassImError = "im.error" ClassImReceived = "im.received" ClassNetwork = "network" ClassNetworkConnected = "network.connected" ClassNetworkDisconnected = "network.disconnected" ClassNetworkError = "network.error" ClassPresence = "presence" ClassPresenceOffline = "presence.offline" ClassPresenceOnline = "presence.online" ClassTransfer = "transfer" ClassTransferComplete = "transfer.complete" ClassTransferError = "transfer.error" ) // Urgency Levels const ( UrgencyLow = byte(0) UrgencyNormal = byte(1) UrgencyCritical = byte(2) ) // Hints const ( HintActionIcons = "action-icons" HintCategory = "category" HintDesktopEntry = "desktop-entry" HintImageData = "image-data" HintImagePath = "image-path" HintResident = "resident" HintSoundFile = "sound-file" HintSoundName = "sound-name" HintSuppressSound = "suppress-sound" HintTransient = "transient" HintX = "x" HintY = "y" HintUrgency = "urgency" ) // Capabilities is a struct containing the capabilities of the notification // server. type Capabilities struct { // Supports using icons instead of text for displaying actions. ActionIcons bool // The server will provide any specified actions to the user. Actions bool // Supports body text. Some implementations may only show the summary. Body bool // The server supports hyperlinks in the notifications. BodyHyperlinks bool // The server supports images in the notifications. BodyImages bool // Supports markup in the body text. BodyMarkup bool // The server will render an animation of all the frames in a given // image array. IconMulti bool // Supports display of exactly 1 frame of any given image array. IconStatic bool // The server supports persistence of notifications. Notifications will // be retained until they are acknowledged or removed by the user or // recalled by the sender. Persistence bool // The server supports sounds on notifications. Sound bool } // GetCapabilities returns the capabilities of the notification server. func GetCapabilities() (c Capabilities, err error) { conn, err := dbus.SessionBus() if err != nil { return } obj := conn.Object(DbusInterfacePath, DbusObjectPath) call := obj.Call(CallGetCapabilities, 0) if err = call.Err; err != nil { return } s := []string{} if err = call.Store(&s); err != nil { return } for _, v := range s { switch v { case "action-icons": c.ActionIcons = true break case "actions": c.Actions = true break case "body": c.Body = true break case "body-hyperlinks": c.BodyHyperlinks = true break case "body-images": c.BodyImages = true break case "body-markup": c.BodyMarkup = true break case "icon-multi": c.IconMulti = true break case "icon-static": c.IconStatic = true break case "persistence": c.Persistence = true break case "sound": c.Sound = true break } } return } // ServerInformation is a struct containing information about the server such // as its name and version. type ServerInformation struct { // The name of the notification server daemon Name string // The vendor of the notification server Vendor string // Version of the notification server Version string // Spec version the notification server conforms to SpecVersion string } // GetServerInformation returns information about the notification server such // as its name and version. func GetServerInformation() (i ServerInformation, err error) { conn, err := dbus.SessionBus() if err != nil { return } obj := conn.Object(DbusInterfacePath, DbusObjectPath) call := obj.Call(CallGetServerInformation, 0) if err = call.Err; err != nil { return } err = call.Store(&i.Name, &i.Vendor, &i.Version, &i.SpecVersion) return } // Notification is a struct which describes the notification to be displayed // by the notification server. type Notification struct { // The optional name of the application sending the notification. // Can be blank. AppName string // The optional notification ID that this notification replaces. ReplacesID uint32 // The optional program icon of the calling application. AppIcon string // The summary text briefly describing the notification. Summary string // The optional detailed body text. Body string // The actions send a request message back to the notification client // when invoked. Actions []string // Hints are a way to provide extra data to a notification server. Hints map[string]interface{} // The timeout time in milliseconds since the display of the // notification at which the notification should automatically close. Timeout int32 } // NewNotification creates a new notification object with some basic // information. func NewNotification(summary, body string) Notification { return Notification{ Summary: summary, Body: body, Timeout: ExpiresDefault, } } // Show sends the information in the notification object to the server to be // displayed. func (n Notification) Show() (id uint32, err error) { conn, err := dbus.SessionBus() if err != nil { return } // We need to convert the interface type of the map to dbus.Variant as // people dont want to have to import the dbus package just to make use // of the notification hints. hints := map[string]dbus.Variant{} for k, v := range n.Hints { hints[k] = dbus.MakeVariant(v) } obj := conn.Object(DbusInterfacePath, DbusObjectPath) call := obj.Call( CallNotify, 0, n.AppName, n.ReplacesID, n.AppIcon, n.Summary, n.Body, n.Actions, hints, n.Timeout) if err = call.Err; err != nil { return } err = call.Store(&id) return } // CloseNotification closes the notification if it exists using its id. func CloseNotification(id uint32) (err error) { conn, err := dbus.SessionBus() if err != nil { return } obj := conn.Object(DbusInterfacePath, DbusObjectPath) call := obj.Call(CallCloseNotification, 0, id) err = call.Err return } go-notify-0.2.0/notify_test.go000066400000000000000000000035001376157420200163270ustar00rootroot00000000000000package notify import "testing" func TestGetCapabilities(t *testing.T) { c, err := GetCapabilities() if err != nil { t.Fatal(err) } t.Logf("Support Action Icons: %v\n", c.ActionIcons) t.Logf("Support Actions: %v\n", c.Actions) t.Logf("Support Body: %v\n", c.Body) t.Logf("Support Body Hyperlinks: %v\n", c.BodyHyperlinks) t.Logf("Support Body Images: %v\n", c.BodyImages) t.Logf("Support Body Markup: %v\n", c.BodyMarkup) t.Logf("Support Icon Multi: %v\n", c.IconMulti) t.Logf("Support Icon Static: %v\n", c.IconStatic) t.Logf("Support Persistence: %v\n", c.Persistence) t.Logf("Support Sound: %v\n", c.Sound) } func TestGetServerInformation(t *testing.T) { info, err := GetServerInformation() if err != nil { t.Fatal(err) } t.Logf("Server Name: %s\n", info.Name) t.Logf("Server Spec Version: %s\n", info.SpecVersion) t.Logf("Server Vendor: %s\n", info.Vendor) t.Logf("Sserver Version: %s\n", info.Version) } func TestNewNotification(t *testing.T) { ntf := NewNotification("Notification Test", "Just a test") if _, err := ntf.Show(); err != nil { t.Fatal(err) } } func TestCloseNotification(t *testing.T) { ntf := NewNotification("Notification Test", "Just a test") id, err := ntf.Show() if err != nil { t.Fatal(err) } if err = CloseNotification(id); err != nil { t.Fatal(err) } } func TestUrgencyNotification(t *testing.T) { ntfLow := NewNotification("Urgency Test", "Testing notification urgency low") ntfLow.Hints = make(map[string]interface{}) ntfLow.Hints[HintUrgency] = UrgencyLow _, err := ntfLow.Show() if err != nil { t.Fatal(err) } ntfCritical := NewNotification("Urgency Test", "Testing notification urgency critical") ntfCritical.Hints = make(map[string]interface{}) ntfCritical.Hints[HintUrgency] = UrgencyCritical _, err = ntfCritical.Show() if err != nil { t.Fatal(err) } }