Sources/Orgstar/GrantAccess.swift
88 lines · 5027 bytes
1#if APPSTORE
2import AppKit
3import OrgApp
4import OrgCore
5import OrgWorkspace
6
7/// Access to folders outside the sidebar's folders, which the sandbox keeps orgstar out of
8/// until they're chosen in an open panel.
9@MainActor
10enum GrantAccess {
11 enum Answer {
12 case granted, alternative, canceled
13 }
14
15 /// Says orgstar can't reach `folder` and offers an open panel at it, plus `alternative`
16 /// when given. A folder chosen in the panel is kept as a grant; `granted` once it covers
17 /// `folder`.
18 static func ask(_ folder: URL, workspace: WorkspaceModel, alternative: String? = nil) -> Answer {
19 let alert = NSAlert()
20 let name = OrgPaths.abbreviatingWithTilde(folder.path)
21 alert.messageText = String(localized: "orgstar can't reach \(name).", comment: "Alert for a folder outside the sidebar's folders")
22 alert.informativeText = String(localized: "It isn't in one of your folders. Grant access to it to continue.", comment: "Alert for a folder outside the sidebar's folders")
23 alert.addButton(withTitle: String(localized: "Grant Access…", comment: "Button that opens a panel to give orgstar access to a folder"))
24 if let alternative { alert.addButton(withTitle: alternative) }
25 alert.addButton(withTitle: String(localized: "Cancel"))
26 switch alert.runModal() {
27 case .alertFirstButtonReturn: break
28 case .alertSecondButtonReturn where alternative != nil: return .alternative
29 default: return .canceled
30 }
31 guard let url = choose(at: folder, message: String(localized: "Choose \(name) or a folder that contains it.", comment: "Open panel for giving orgstar access to a folder"),
32 prompt: String(localized: "Grant Access", comment: "Open panel button that gives orgstar access to a folder")) else { return .canceled }
33 guard FolderGrants.covers(url, folder) else {
34 tell(String(localized: "\(OrgPaths.abbreviatingWithTilde(url.path)) doesn't contain \(name).", comment: "Alert when the folder chosen to grant access doesn't contain the one needed"),
35 String(localized: "Choose \(name) or a folder that contains it.", comment: "Open panel for giving orgstar access to a folder"))
36 return .canceled
37 }
38 if !FolderGrants.shared.add(url) {
39 tell(String(localized: "orgstar can't keep access to \(name).", comment: "Alert when a folder grant can't be saved"),
40 String(localized: "Access lasts until orgstar quits.", comment: "Alert when a folder grant or the configuration folder can't be saved"))
41 }
42 return workspace.foldersToGrant(for: [folder.path], directories: true).isEmpty ? .granted : .canceled
43 }
44
45 static func tell(_ message: String, _ information: String) {
46 let alert = NSAlert()
47 alert.messageText = message
48 alert.informativeText = information
49 alert.runModal()
50 }
51
52 /// An open panel at `location`, for a folder, or a file too with `files`. Hidden files show
53 /// with `hidden`, for dot folders such as `~/.config`.
54 static func choose(at location: URL, message: String, prompt: String, files: Bool = false, hidden: Bool = false) -> URL? {
55 let panel = NSOpenPanel()
56 panel.canChooseDirectories = true
57 panel.canChooseFiles = files
58 panel.canCreateDirectories = true
59 panel.showsHiddenFiles = hidden
60 panel.directoryURL = location
61 panel.prompt = prompt
62 panel.message = message
63 return panel.runModal() == .OK ? panel.url : nil
64 }
65
66 /// Asks again for `root`, a sidebar folder that doesn't resolve, with an open panel at where
67 /// it was, and puts the folder chosen in its place.
68 static func regrant(_ root: WorkspaceRoot, workspace: WorkspaceModel) {
69 let name = OrgPaths.abbreviatingWithTilde(root.path)
70 guard let url = choose(at: URL(fileURLWithPath: root.path),
71 message: String(localized: "Choose \(name) to give orgstar access to it again.", comment: "Open panel for a sidebar folder orgstar can't open"),
72 prompt: String(localized: "Grant Access", comment: "Open panel button that gives orgstar access to a folder")) else { return }
73 Task {
74 do { try await workspace.regrant(root, with: url) } catch { workspace.lastError = error.localizedDescription }
75 }
76 }
77
78 /// Asks for each folder `paths` need, in turn. Whether all of them can now be reached.
79 static func reach(_ paths: [String], directories: Bool = false, workspace: WorkspaceModel) -> Bool {
80 for folder in workspace.foldersToGrant(for: paths, directories: directories) {
81 // An earlier grant may cover this one.
82 guard !workspace.foldersToGrant(for: [folder.path], directories: true).isEmpty else { continue }
83 guard ask(folder, workspace: workspace) == .granted else { return false }
84 }
85 return true
86 }
87}
88#endif