| 1 |
/* |
| 2 |
Copyright 2014 Canonical Ltd. |
| 3 |
|
| 4 |
This program is free software: you can redistribute it and/or modify it |
| 5 |
under the terms of the GNU General Public License version 3, as published |
| 6 |
by the Free Software Foundation. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but |
| 9 |
WITHOUT ANY WARRANTY; without even the implied warranties of |
| 10 |
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 11 |
PURPOSE. See the GNU General Public License for more details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU General Public License along |
| 14 |
with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
// Package emblemcounter can present notifications as a counter on an |
| 18 |
// emblem on an item in the launcher. |
| 19 |
package emblemcounter |
| 20 |
|
| 21 |
import ( |
| 22 |
"github.com/godbus/dbus/v5" |
| 23 |
|
| 24 |
"gitlab.com/ubports/development/core/lomiri-push-service/bus" |
| 25 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/launch_helper" |
| 27 |
"gitlab.com/ubports/development/core/lomiri-push-service/logger" |
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/nih" |
| 29 |
) |
| 30 |
|
| 31 |
// emblemcounter works by setting properties on a well-known dbus name. |
| 32 |
var BusAddress = bus.Address{ |
| 33 |
Interface: "com.lomiri.Shell.Launcher.Item", |
| 34 |
Path: "/com/lomiri/Shell/Launcher", |
| 35 |
Name: "com.lomiri.Shell.Launcher", |
| 36 |
} |
| 37 |
|
| 38 |
// EmblemCounter is a little tool that fiddles with the Lomiri launcher |
| 39 |
// to put emblems with counters on launcher icons. |
| 40 |
type EmblemCounter struct { |
| 41 |
bus bus.Endpoint |
| 42 |
log logger.Logger |
| 43 |
} |
| 44 |
|
| 45 |
// Build an EmblemCounter using the given bus and log. |
| 46 |
func New(endp bus.Endpoint, log logger.Logger) *EmblemCounter { |
| 47 |
return &EmblemCounter{bus: endp, log: log} |
| 48 |
} |
| 49 |
|
| 50 |
// Look for an EmblemCounter section in a Notification and, if |
| 51 |
// present, presents it to the user. |
| 52 |
func (ctr *EmblemCounter) Present(app *click.AppId, nid string, notification *launch_helper.Notification) bool { |
| 53 |
if notification == nil { |
| 54 |
panic("please check notification is not nil before calling present") |
| 55 |
} |
| 56 |
|
| 57 |
ec := notification.EmblemCounter |
| 58 |
|
| 59 |
if ec == nil { |
| 60 |
ctr.log.Debugf("[%s] notification has no EmblemCounter: %#v", nid, ec) |
| 61 |
return false |
| 62 |
} |
| 63 |
ctr.log.Debugf("[%s] setting emblem counter for %s to %d (visible: %t)", nid, app.Base(), ec.Count, ec.Visible) |
| 64 |
return ctr.SetCounter(app, ec.Count, ec.Visible) |
| 65 |
} |
| 66 |
|
| 67 |
// SetCounter sets an emblem counter on the launcher for app to count (if |
| 68 |
// visible is true), or clears it (if count is 0 or visible is false). |
| 69 |
func (ctr *EmblemCounter) SetCounter(app *click.AppId, count int32, visible bool) bool { |
| 70 |
base := app.Base() |
| 71 |
quoted := string(nih.Quote([]byte(base))) |
| 72 |
|
| 73 |
err := ctr.bus.SetProperty("count", "/"+quoted, dbus.MakeVariant(count)) |
| 74 |
if err != nil { |
| 75 |
ctr.log.Errorf("call to set count failed: %v", err) |
| 76 |
return false |
| 77 |
} |
| 78 |
err = ctr.bus.SetProperty("countVisible", "/"+quoted, dbus.MakeVariant(visible)) |
| 79 |
if err != nil { |
| 80 |
ctr.log.Errorf("call to set countVisible failed: %v", err) |
| 81 |
return false |
| 82 |
} |
| 83 |
|
| 84 |
return true |
| 85 |
} |