Skip to content

Commit

Permalink
daemon: look for snap icon in icons directory as fallback
Browse files Browse the repository at this point in the history
If a snap doesn't ship an icon in `meta/gui/icon.*`, then look for an
icon for the snap in question in the snap icons install directory.

Signed-off-by: Oliver Calder <[email protected]>
  • Loading branch information
olivercalder committed Feb 5, 2025
1 parent 0cd5115 commit 8d41659
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion daemon/api_icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func iconGet(st *state.State, name string) Response {
return NotFound("snap has no current revision")
}

icon := snapIcon(snap.MinimalPlaceInfo(name, sideInfo.Revision))
icon := snapIcon(snap.MinimalPlaceInfo(name, sideInfo.Revision), sideInfo.SnapID)

if icon == "" {
return NotFound("local snap has no icon")
Expand Down
2 changes: 1 addition & 1 deletion daemon/api_icons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ func (s *iconsSuite) TestAppIconGetNoApp(c *check.C) {

func (s *iconsSuite) TestNotInstalledSnapIcon(c *check.C) {
info := &snap.Info{SuggestedName: "notInstalledSnap", Media: []snap.MediaInfo{{Type: "icon", URL: "icon.svg"}}}
iconfile := daemon.SnapIcon(info)
iconfile := daemon.SnapIcon(info, "notInstalledSnapID")
c.Check(iconfile, check.Equals, "")
}
22 changes: 16 additions & 6 deletions daemon/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/client/clientutil"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/overlord/assertstate"
"github.com/snapcore/snapd/overlord/healthstate"
"github.com/snapcore/snapd/overlord/snapstate"
Expand Down Expand Up @@ -241,7 +242,7 @@ func mapLocal(about aboutSnap, sd clientutil.StatusDecorator) *client.Snap {
}
result.InstalledSize = localSnap.Size

if icon := snapIcon(localSnap); icon != "" {
if icon := snapIcon(localSnap, localSnap.SnapID); icon != "" {
result.Icon = icon
}

Expand Down Expand Up @@ -339,12 +340,21 @@ func fillComponentInfo(about aboutSnap) []client.Component {
return comps
}

// snapIcon tries to find the icon inside the snap
func snapIcon(info snap.PlaceInfo) string {
// snapIcon tries to find the icon inside the snap at meta/gui/icon.*, and if
// the snap does not ship an icon there, then tries to find the fallback icon
// in the icons install directory.
func snapIcon(info snap.PlaceInfo, snapID string) string {
// Look in the snap itself
found, _ := filepath.Glob(filepath.Join(info.MountDir(), "meta", "gui", "icon.*"))
if len(found) == 0 {
return ""
if len(found) > 0 {
return found[0]
}

return found[0]
// Look in the snap icons directory as a fallback
if fallback := snapstate.IconInstallFilename(snapID); fallback != "" && osutil.FileExists(fallback) {
return fallback
}

Check warning on line 356 in daemon/snap.go

View check run for this annotation

Codecov / codecov/patch

daemon/snap.go#L355-L356

Added lines #L355 - L356 were not covered by tests

// Didn't find an icon
return ""
}

0 comments on commit 8d41659

Please sign in to comment.