Skip to content

Commit

Permalink
Merge pull request #2 from esiqveland/feature/dbus_signals
Browse files Browse the repository at this point in the history
Feature/dbus signals
  • Loading branch information
esiqveland committed Oct 25, 2015
2 parents 6697e1d + f6e56fa commit 38c5b2a
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 107 deletions.
55 changes: 10 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,66 +6,31 @@ Notify is a go library for interacting with the dbus notification service define
https://developer.gnome.org/notification-spec/

It can deliver notifications to desktop using dbus communication, ala how libnotify does it.
It has so far only been testing with gnome and gnome-shell 3.16 in Arch Linux.
It has so far only been testing with gnome and gnome-shell 3.16/3.18 in Arch Linux.

Please note ```notify``` is still in a very early change and no APIs are locked until a 1.0 is released.

More testers are very welcome =)

Depends on github.com/godbus/dbus.
Depends on:
- [godbus](https://github.com/godbus/dbus).

## Quick intro
See example: [main.go](https://github.com/esiqveland/notify/blob/master/example/main.go).

Clone repo and go to examples folder:

``` go run main.go ```

```go
package main

import (
"github.com/esiqveland/notify"
"github.com/godbus/dbus"
"log"
)

func main() {
conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}

notifier := notify.New(conn)

n := notify.Notification{
AppName: "Test GO App",
ReplacesID: uint32(0),
AppIcon: "mail-unread",
Summary: "Test",
Body: "This is a test of the DBus bindings for go.",
Actions: []string{},
Hints: map[string]dbus.Variant{},
ExpireTimeout: int32(5000),
}

id, err := notifier.SendNotification(n)
if err != nil {
panic(err)
}
log.Printf("sent notification id: %v", id)

// And there is a helper for just sending notifications directly:
notify.SendNotification(conn, n)
}

```

You should now have gotten this notification delivered to your desktop.

## TODO

- [ ] Add callback support aka dbus signals?
- [x] Add callback support aka dbus signals.
- [ ] Tests. I am very interested in any ideas for writing some (useful) tests for this.

## See also

`main.go` in `examples/` directory and https://developer.gnome.org/notification-spec/
The Gnome notification spec https://developer.gnome.org/notification-spec/.

## License

Expand Down
13 changes: 9 additions & 4 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ The notify package is a wrapper around godbus for dbus notification interface
See: https://developer.gnome.org/notification-spec/ and
https://github.com/godbus/dbus
Each notification displayed is allocated a unique ID by the server. (see Notify)
This ID unique within the dbus session. While the notification server is running,
the ID will not be recycled unless the capacity of a uint32 is exceeded.
The package provides exported methods for simple usage, e.g. just show a notification.
It also provides the interface Notifier that includes event delivery for notifications.
Note that if you use New() to create a notifier, it is the caller responsibility to also drain the
channels for ActionInvoked() and NotificationClosed().
This can be used to hide the notification before the expiration timeout is reached. (see CloseNotification)
Each notification created is allocated a unique ID by the server (see Notification).
This ID is unique within the dbus session, and is an increasing number.
While the notification server is running, the ID will not be recycled unless the capacity of a uint32 is exceeded.
The ID can be used to hide the notification before the expiration timeout is reached (see CloseNotification).
The ID can also be used to atomically replace the notification with another (Notification.ReplaceID).
This allows you to (for instance) modify the contents of a notification while it's on-screen.
Expand Down
48 changes: 37 additions & 11 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,44 @@ import (
)

func main() {
iconName := "mail-unread"

conn, err := dbus.SessionBus()
if err != nil {
panic(err)
}

notificator := notify.New(conn)

// Basic usage
// Create a Notification to send
iconName := "mail-unread"
n := notify.Notification{
AppName: "Test GO App",
ReplacesID: uint32(0),
AppIcon: iconName,
Summary: "Test",
Body: "This is a test of the DBus bindings for go.",
Actions: []string{},
Actions: []string{"cancel", "Cancel", "open", "Open"}, // tuples of (action_key, label)
Hints: map[string]dbus.Variant{},
ExpireTimeout: int32(5000),
}

id, err := notificator.SendNotification(n)
// Ship it!
createdID, err := notify.SendNotification(conn, n)
if err != nil {
panic(err)
log.Printf("error sending notification: %v", err.Error())
}
log.Printf("sent notification id: %v", id)
log.Printf("created notification with id: %v", createdID)

caps, err := notificator.GetCapabilities()

// List server features!
caps, err := notify.GetCapabilities(conn)
if err != nil {
log.Printf("error fetching capabilities: %v", err)
}
for x := range caps {
fmt.Printf("Registered capability: %v\n", caps[x])
}

info, err := notificator.GetServerInformation()
info, err := notify.GetServerInformation(conn)
if err != nil {
log.Printf("error getting server information: %v", err)
}
Expand All @@ -52,7 +55,30 @@ func main() {
fmt.Printf("Version: %v\n", info.Version)
fmt.Printf("Spec: %v\n", info.SpecVersion)

// And there is a helper for just sending notifications directly:
notify.SendNotification(conn, n)


// Notifyer interface with event delivery
notifier, err := notify.New(conn)
if err != nil {
log.Fatalln(err.Error())
}
defer notifier.Close()

id, err := notifier.SendNotification(n)
if err != nil {
log.Printf("error sending notification: %v", err)
}
log.Printf("sent notification id: %v", id)

// Listen for actions invoked!
actions := notifier.ActionInvoked()
go func() {
action := <-actions
log.Printf("ActionInvoked: %v Key: %v", action.Id, action.ActionKey)
}()

closer := <-notifier.NotificationClosed()
log.Printf("NotificationClosed: %v Reason: %v", closer.Id, closer.Reason)


}
Loading

0 comments on commit 38c5b2a

Please sign in to comment.