Debian Codegraph @

click/click.go

raw
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 click exposes some utilities related to click packages and
18 // wraps libclick to check if packages are installed.
19 package click
20
21 import (
22 "encoding/json"
23 "errors"
24 "fmt"
25 "path/filepath"
26 "regexp"
27 "strings"
28 "sync"
29
30 "github.com/adrg/xdg"
31
32 "gitlab.com/ubports/development/core/lomiri-push-service/click/cappinfo"
33 "gitlab.com/ubports/development/core/lomiri-push-service/click/cclick"
34 )
35
36 // AppId holds a parsed application id.
37 type AppId struct {
38 Package string
39 Application string
40 Version string
41 Click bool
42 original string
43 }
44
45 // from https://wiki.ubuntu.com/AppStore/Interfaces/ApplicationId
46 // except the version is made optional
47 var rxClick = regexp.MustCompile(`^([a-z0-9][a-z0-9+.-]+)_([a-zA-Z0-9+.-]+)(?:_([0-9][a-zA-Z0-9.+:~-]*))?$`)
48
49 // no / and not starting with .
50 var rxLegacy = regexp.MustCompile(`^[^./][^/]*$`)
51
52 var (
53 ErrInvalidAppId = errors.New("invalid application id")
54 ErrMissingApp = errors.New("application not installed")
55 )
56
57 func ParseAppId(id string) (*AppId, error) {
58 app := new(AppId)
59 err := app.setFromString(id)
60 if err == nil {
61 return app, nil
62 } else {
63 return nil, err
64 }
65 }
66
67 func (app *AppId) setFromString(id string) error {
68 if strings.HasPrefix(id, "_") { // legacy
69 appname := id[1:]
70 if !rxLegacy.MatchString(appname) {
71 return ErrInvalidAppId
72 }
73 app.Package = ""
74 app.Application = appname
75 app.Version = ""
76 app.Click = false
77 app.original = id
78 return nil
79 } else {
80 m := rxClick.FindStringSubmatch(id)
81 if len(m) == 0 {
82 return ErrInvalidAppId
83 }
84 app.Package = m[1]
85 app.Application = m[2]
86 app.Version = m[3]
87 app.Click = true
88 app.original = id
89 return nil
90 }
91 }
92
93 func (app *AppId) InPackage(pkgname string) bool {
94 return app.Package == pkgname
95 }
96
97 func (app *AppId) DispatchPackage() string {
98 if app.Click {
99 return app.Package
100 }
101 return app.Application
102 }
103
104 func (app *AppId) Original() string {
105 return app.original
106 }
107
108 func (app *AppId) String() string {
109 return app.Original()
110 }
111
112 func (app *AppId) Base() string {
113 if app.Click {
114 return app.Package + "_" + app.Application
115 } else {
116 return app.Application
117 }
118 }
119
120 func (app *AppId) Versioned() string {
121 if app.Click {
122 if app.Version == "" {
123 panic(fmt.Errorf("Versioned() on AppId without version/not verified: %#v", app))
124 }
125 return app.Package + "_" + app.Application + "_" + app.Version
126 } else {
127 return app.Application
128 }
129 }
130
131 func (app *AppId) DesktopId() string {
132 return app.Versioned() + ".desktop"
133 }
134
135 func (app *AppId) Icon() string {
136 return cappinfo.AppIconFromDesktopId(app.DesktopId())
137 }
138
139 func _symbolic(icon string) string {
140 if strings.ContainsRune(icon, '/') {
141 return icon
142 }
143 return icon + "-symbolic"
144 }
145
146 var symbolic = _symbolic
147
148 func (app *AppId) SymbolicIcon() string {
149 symbolicIcon := cappinfo.AppSymbolicIconFromDesktopId(app.DesktopId())
150 if symbolicIcon != "" {
151 return symbolicIcon
152 }
153 return symbolic(app.Icon())
154 }
155
156 func (app *AppId) MarshalJSON() ([]byte, error) {
157 return json.Marshal(app.Original())
158 }
159
160 func (app *AppId) UnmarshalJSON(s []byte) error {
161 var v string
162 err := json.Unmarshal(s, &v)
163 if err != nil {
164 return err
165 }
166 return app.setFromString(v)
167 }
168
169 // ClickUser exposes the click package registry for the user.
170 type ClickUser struct {
171 ccu cclick.CClickUser
172 lock sync.Mutex
173 }
174
175 type InstalledChecker interface {
176 Installed(app *AppId, setVersion bool) bool
177 }
178
179 // ParseAndVerifyAppId parses the given app id and checks if the
180 // corresponding app is installed, returning the parsed id or
181 // ErrInvalidAppId, or the parsed id and ErrMissingApp respectively.
182 func ParseAndVerifyAppId(id string, installedChecker InstalledChecker) (*AppId, error) {
183 app, err := ParseAppId(id)
184 if err != nil {
185 return nil, err
186 }
187 if installedChecker != nil && !installedChecker.Installed(app, true) {
188 return app, ErrMissingApp
189 }
190 return app, nil
191 }
192
193 // User makes a new ClickUser object for the current user.
194 func User() (*ClickUser, error) {
195 cu := new(ClickUser)
196 err := cu.ccu.CInit(cu)
197 if err != nil {
198 return nil, err
199 }
200 return cu, nil
201 }
202
203 // Installed checks if the appId is installed for user, optionally setting
204 // the version if it was absent.
205 func (cu *ClickUser) Installed(app *AppId, setVersion bool) bool {
206 cu.lock.Lock()
207 defer cu.lock.Unlock()
208 if app.Click {
209 ver := cu.ccu.CGetVersion(app.Package)
210 if ver == "" {
211 return false
212 }
213 if app.Version != "" {
214 return app.Version == ver
215 } else if setVersion {
216 app.Version = ver
217 }
218 return true
219 } else {
220 _, err := xdg.SearchDataFile(filepath.Join("applications", app.DesktopId()))
221 return err == nil
222 }
223 }