Sources/Orgstar/GrantAccess.swift
73 lines · 4086 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 FolderGrants.shared.add(url)
34 return workspace.foldersToGrant(for: [folder.path], directories: true).isEmpty ? .granted : .canceled
35 }
36
37 /// An open panel at `location`, for a folder, or a file too with `files`. Hidden files show
38 /// with `hidden`, for dot folders such as `~/.config`.
39 static func choose(at location: URL, message: String, prompt: String, files: Bool = false, hidden: Bool = false) -> URL? {
40 let panel = NSOpenPanel()
41 panel.canChooseDirectories = true
42 panel.canChooseFiles = files
43 panel.canCreateDirectories = true
44 panel.showsHiddenFiles = hidden
45 panel.directoryURL = location
46 panel.prompt = prompt
47 panel.message = message
48 return panel.runModal() == .OK ? panel.url : nil
49 }
50
51 /// Asks again for `root`, a sidebar folder that doesn't resolve, with an open panel at where
52 /// it was, and puts the folder chosen in its place.
53 static func regrant(_ root: WorkspaceRoot, workspace: WorkspaceModel) {
54 let name = OrgPaths.abbreviatingWithTilde(root.path)
55 guard let url = choose(at: URL(fileURLWithPath: root.path),
56 message: String(localized: "Choose \(name) to give orgstar access to it again.", comment: "Open panel for a sidebar folder orgstar can't open"),
57 prompt: String(localized: "Grant Access", comment: "Open panel button that gives orgstar access to a folder")) else { return }
58 Task {
59 do { try await workspace.regrant(root, with: url) } catch { workspace.lastError = error.localizedDescription }
60 }
61 }
62
63 /// Asks for each folder `paths` need, in turn. Whether all of them can now be reached.
64 static func reach(_ paths: [String], directories: Bool = false, workspace: WorkspaceModel) -> Bool {
65 for folder in workspace.foldersToGrant(for: paths, directories: directories) {
66 // An earlier grant may cover this one.
67 guard !workspace.foldersToGrant(for: [folder.path], directories: true).isEmpty else { continue }
68 guard ask(folder, workspace: workspace) == .granted else { return false }
69 }
70 return true
71 }
72}
73#endif