Code review and cleanup.
This commit is contained in:
1 parent
bbe931335f
commit
92d6ad3cc4
23 files changed
+1497
-886
No files matched your search
@@ -10,10 +10,7 @@ package preferences
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
@@ -165,171 +162,6 @@ func (page *tentaclePage) populate(snapshot *tentacle.Snapshot) {
|
||||
page.render()
|
||||
}
|
||||
|
||||
func (page *tentaclePage) action(action int) (*tentacle.ManagementRequest, error) {
|
||||
if action == 16 {
|
||||
if selected := page.selected(); selected != nil {
|
||||
page.selectedKey = selected.key
|
||||
}
|
||||
page.controls()
|
||||
return nil, nil
|
||||
}
|
||||
if action == 17 {
|
||||
page.render()
|
||||
return nil, nil
|
||||
}
|
||||
if action == 15 {
|
||||
return &tentacle.ManagementRequest{Action: "list"}, nil
|
||||
}
|
||||
if page.snapshot == nil || page.snapshot.Suppressed || page.snapshot.Message != "" {
|
||||
message := "Tentacle management is unavailable for this run."
|
||||
if page.snapshot != nil && page.snapshot.Message != "" {
|
||||
message = page.snapshot.Message
|
||||
}
|
||||
return nil, errors.New(message)
|
||||
}
|
||||
if action == 9 {
|
||||
id := C.CString("tentacleRepositoryURL")
|
||||
origin, err := tentacle.RepositoryURL(C.GoString(C.prefs_entry_get(id)))
|
||||
C.free(unsafe.Pointer(id))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tentacle.ManagementRequest{Action: "repository-add", URL: origin}, nil
|
||||
}
|
||||
if action == 12 || action == 18 {
|
||||
origin := page.origin()
|
||||
if origin == "" {
|
||||
return nil, errors.New("Select a repository tab first.")
|
||||
}
|
||||
operation := "repository-remove"
|
||||
if action == 18 {
|
||||
operation = "repository-refresh"
|
||||
}
|
||||
return &tentacle.ManagementRequest{Action: operation, URL: origin}, nil
|
||||
}
|
||||
selected := page.selected()
|
||||
if selected == nil {
|
||||
return nil, errors.New("Select a tentacle first.")
|
||||
}
|
||||
page.selectedKey = selected.key
|
||||
if action == 21 {
|
||||
if !selected.installed {
|
||||
return nil, errors.New("Install this tentacle first.")
|
||||
}
|
||||
entry := selected.status.Entry
|
||||
for _, update := range page.snapshot.Updates {
|
||||
if update.ID != entry.ID {
|
||||
continue
|
||||
}
|
||||
permissions := ""
|
||||
foundManifest := false
|
||||
for _, repository := range page.snapshot.Repositories {
|
||||
if repository.URL != update.RepositoryURL || repository.ID != update.RepositoryID {
|
||||
continue
|
||||
}
|
||||
for _, manifest := range repository.Tentacles {
|
||||
if manifest.ID != update.LocalID {
|
||||
continue
|
||||
}
|
||||
foundManifest = true
|
||||
var previous []string
|
||||
if entry.Presentation {
|
||||
previous = append(previous, "presentation")
|
||||
}
|
||||
if entry.Interaction {
|
||||
previous = append(previous, "interaction")
|
||||
}
|
||||
if entry.ReaderSettings {
|
||||
previous = append(previous, "reader-settings")
|
||||
}
|
||||
if len(previous) == 0 {
|
||||
previous = append(previous, "none")
|
||||
}
|
||||
declared := manifest.Capabilities
|
||||
if len(declared) == 0 {
|
||||
declared = []string{"none"}
|
||||
}
|
||||
permissions = "Current capabilities: " + strings.Join(previous, ", ") +
|
||||
". Repository declares: " + strings.Join(declared, ", ") + "."
|
||||
}
|
||||
}
|
||||
if !foundManifest {
|
||||
return nil, errors.New("Updated package manifest is unavailable; refresh the repository before upgrading.")
|
||||
}
|
||||
message := fmt.Sprintf("Update %s from version %s to version %s?\n\n%s\n\nOnly update packages from publishers you trust. Tentacles run with your account's access.\n\nChanges take effect immediately.", update.Name, update.InstalledVersion, update.AvailableVersion, permissions)
|
||||
text, button := C.CString(message), C.CString("Update")
|
||||
accepted := C.prefs_tentacle_confirm(text, button)
|
||||
C.free(unsafe.Pointer(text))
|
||||
C.free(unsafe.Pointer(button))
|
||||
if accepted == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return &tentacle.ManagementRequest{Action: "upgrade", ID: update.LocalID, URL: update.RepositoryURL}, nil
|
||||
}
|
||||
return nil, errors.New("No newer version is available for the selected tentacle. Refresh its repository to check again.")
|
||||
}
|
||||
if !selected.installed {
|
||||
if action != 11 && action != 19 {
|
||||
return nil, errors.New("Install this tentacle first.")
|
||||
}
|
||||
entry := selected.status.Entry
|
||||
var manifest *tentacle.Manifest
|
||||
for _, repository := range page.snapshot.Repositories {
|
||||
if repository.URL == entry.Repository && repository.ID == entry.RepositoryID {
|
||||
for index := range repository.Tentacles {
|
||||
if repository.Tentacles[index].ID == entry.LocalID {
|
||||
manifest = &repository.Tentacles[index]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if manifest == nil || len(manifest.Releases) == 0 {
|
||||
return nil, errors.New("No package has been published for this tentacle. Refresh the repository after a release is published.")
|
||||
}
|
||||
permissions := "This tentacle can register reader commands."
|
||||
if slices.Contains(manifest.Capabilities, "presentation") {
|
||||
permissions = "This tentacle can register reader commands and send speech and braille messages."
|
||||
}
|
||||
if slices.Contains(manifest.Capabilities, "interaction") {
|
||||
permissions = "This tentacle can register reader commands, send speech and braille messages, open internal forms, read the focused item and clipboard when invoked, and write clipboard text."
|
||||
}
|
||||
if slices.Contains(manifest.Capabilities, "reader-settings") {
|
||||
permissions += " It can also add preferences pages and quick settings, inspect speech voices, and temporarily change the reader voice configuration."
|
||||
}
|
||||
operation, label := "install", "Install"
|
||||
if action == 11 {
|
||||
operation, label = "install-enable", "Install and enable"
|
||||
}
|
||||
message := fmt.Sprintf("%s %s version %s from %s?\n\n%s\n\nOnly install packages from publishers you trust. Tentacles run with your account's access; resource limits do not protect your files or network.\n\nTentacle management changes take effect immediately.", label, entry.Name, entry.Version, entry.Repository, permissions)
|
||||
text, button := C.CString(message), C.CString(label)
|
||||
accepted := C.prefs_tentacle_confirm(text, button)
|
||||
C.free(unsafe.Pointer(text))
|
||||
C.free(unsafe.Pointer(button))
|
||||
if accepted == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return &tentacle.ManagementRequest{Action: operation, ID: entry.LocalID, URL: entry.Repository}, nil
|
||||
}
|
||||
operation := ""
|
||||
switch action {
|
||||
case 11:
|
||||
operation = "enable"
|
||||
if selected.status.Entry.Enabled {
|
||||
operation = "disable"
|
||||
}
|
||||
case 13:
|
||||
operation = "retry"
|
||||
case 14:
|
||||
operation = "remove"
|
||||
case 20:
|
||||
operation = "settings"
|
||||
default:
|
||||
return nil, errors.New("Unknown tentacle action.")
|
||||
}
|
||||
return &tentacle.ManagementRequest{Action: operation, ID: selected.status.Entry.ID}, nil
|
||||
}
|
||||
|
||||
func (page *tentaclePage) accepted(request *tentacle.ManagementRequest) {
|
||||
if request.Action == "repository-add" {
|
||||
id, empty := C.CString("tentacleRepositoryURL"), C.CString("")
|
||||
|
||||
Reference in new issue
Block a user