代码拉取完成,页面将自动刷新
同步操作将从 tupelo-shen/mysnapd 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2021 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package notificationtest
import (
"fmt"
"sort"
"sync"
"github.com/godbus/dbus"
"gitee.com/mysnapcore/mysnapd/dbusutil"
)
const (
fdoBusName = "org.freedesktop.Notifications"
fdoObjectPath = "/org/freedesktop/Notifications"
fdoInterface = "org.freedesktop.Notifications"
)
type FdoNotification struct {
ID uint32
AppName string
Icon string
Summary string
Body string
Actions []string
Hints map[string]dbus.Variant
Expires int32
}
type FdoServer struct {
conn *dbus.Conn
err *dbus.Error
mu sync.Mutex
lastID uint32
notifications map[uint32]*FdoNotification
}
func NewFdoServer() (*FdoServer, error) {
conn, err := dbusutil.SessionBusPrivate()
if err != nil {
return nil, err
}
server := &FdoServer{
conn: conn,
notifications: make(map[uint32]*FdoNotification),
}
conn.Export(fdoApi{server}, fdoObjectPath, fdoInterface)
reply, err := conn.RequestName(fdoBusName, dbus.NameFlagDoNotQueue)
if err != nil {
conn.Close()
return nil, err
}
if reply != dbus.RequestNameReplyPrimaryOwner {
conn.Close()
return nil, fmt.Errorf("cannot obtain bus name %q", fdoBusName)
}
return server, nil
}
func (server *FdoServer) Stop() error {
if _, err := server.conn.ReleaseName(fdoBusName); err != nil {
return err
}
return server.conn.Close()
}
// SetError sets an error to be returned by the D-Bus interface.
//
// If not nil, all the fdoApi methods will return the provided error
// in place of performing their usual task.
func (server *FdoServer) SetError(err *dbus.Error) {
server.err = err
}
func (server *FdoServer) Get(id uint32) *FdoNotification {
server.mu.Lock()
defer server.mu.Unlock()
return server.notifications[id]
}
func (server *FdoServer) GetAll() []*FdoNotification {
server.mu.Lock()
defer server.mu.Unlock()
notifications := make([]*FdoNotification, 0, len(server.notifications))
for _, n := range server.notifications {
notifications = append(notifications, n)
}
sort.Slice(notifications, func(i, j int) bool {
return notifications[i].ID < notifications[j].ID
})
return notifications
}
func (server *FdoServer) Close(id, reason uint32) error {
server.mu.Lock()
defer server.mu.Unlock()
if _, ok := server.notifications[id]; !ok {
return fmt.Errorf("No such notification: %d", id)
}
delete(server.notifications, id)
return server.conn.Emit(fdoObjectPath, fdoInterface+".NotificationClosed", id, reason)
}
func (server *FdoServer) InvokeAction(id uint32, actionKey string) error {
return server.conn.Emit(fdoObjectPath, fdoInterface+".ActionInvoked", id, actionKey)
}
type fdoApi struct {
server *FdoServer
}
func (a fdoApi) GetCapabilities() ([]string, *dbus.Error) {
if a.server.err != nil {
return nil, a.server.err
}
return []string{"cap-foo", "cap-bar"}, nil
}
func (a fdoApi) Notify(appName string, replacesID uint32, icon, summary, body string, actions []string, hints map[string]dbus.Variant, expires int32) (uint32, *dbus.Error) {
if a.server.err != nil {
return 0, a.server.err
}
a.server.mu.Lock()
defer a.server.mu.Unlock()
a.server.lastID += 1
notification := &FdoNotification{
ID: a.server.lastID,
AppName: appName,
Icon: icon,
Summary: summary,
Body: body,
Actions: actions,
Hints: hints,
Expires: expires,
}
if replacesID != 0 {
delete(a.server.notifications, replacesID)
}
a.server.notifications[notification.ID] = notification
return notification.ID, nil
}
func (a fdoApi) CloseNotification(id uint32) *dbus.Error {
if a.server.err != nil {
return a.server.err
}
// close reason 3 is "closed by a call to CloseNotification"
// https://specifications.freedesktop.org/notification-spec/latest/ar01s09.html#signal-notification-closed
if err := a.server.Close(id, 3); err != nil {
return &dbus.Error{
Name: "org.freedesktop.DBus.Error.Failed",
Body: []interface{}{err.Error()},
}
}
return nil
}
func (a fdoApi) GetServerInformation() (name, vendor, version, specVersion string, err *dbus.Error) {
if a.server.err != nil {
return "", "", "", "", a.server.err
}
return "name", "vendor", "version", "specVersion", nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。