Sources/Orgstar/GrantAccess.swift

6e84c5315a776376ffe643f78b5b48551895d250
orgstar/Sources/Orgstar/GrantAccess.swift

52 lines · 2778 bytes

 1#if APPSTORE
 2import AppKit
 3import OrgApp
 4import OrgCore
 5
 6/// Access to folders outside the sidebar's folders, which the sandbox keeps orgstar out of
 7/// until they're chosen in an open panel.
 8@MainActor
 9enum GrantAccess {
10    enum Answer {
11        case granted, alternative, canceled
12    }
13
14    /// Says orgstar can't reach `folder` and offers an open panel at it, plus `alternative`
15    /// when given. A folder chosen in the panel is kept as a grant; `granted` once it covers
16    /// `folder`.
17    static func ask(_ folder: URL, workspace: WorkspaceModel, alternative: String? = nil) -> Answer {
18        let alert = NSAlert()
19        let name = OrgPaths.abbreviatingWithTilde(folder.path)
20        alert.messageText = String(localized: "orgstar can't reach \(name).", comment: "Alert for a folder outside the sidebar's folders")
21        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")
22        alert.addButton(withTitle: String(localized: "Grant Access…", comment: "Button that opens a panel to give orgstar access to a folder"))
23        if let alternative { alert.addButton(withTitle: alternative) }
24        alert.addButton(withTitle: String(localized: "Cancel"))
25        switch alert.runModal() {
26        case .alertFirstButtonReturn: break
27        case .alertSecondButtonReturn where alternative != nil: return .alternative
28        default: return .canceled
29        }
30        let panel = NSOpenPanel()
31        panel.canChooseDirectories = true
32        panel.canChooseFiles = false
33        panel.canCreateDirectories = true
34        panel.directoryURL = folder
35        panel.prompt = String(localized: "Grant Access", comment: "Open panel button that gives orgstar access to a folder")
36        panel.message = String(localized: "Choose \(name) or a folder that contains it.", comment: "Open panel for giving orgstar access to a folder")
37        guard panel.runModal() == .OK, let url = panel.url else { return .canceled }
38        FolderGrants.shared.add(url)
39        return workspace.foldersToGrant(for: [folder.path], directories: true).isEmpty ? .granted : .canceled
40    }
41
42    /// Asks for each folder `paths` need, in turn. Whether all of them can now be reached.
43    static func reach(_ paths: [String], directories: Bool = false, workspace: WorkspaceModel) -> Bool {
44        for folder in workspace.foldersToGrant(for: paths, directories: directories) {
45            // An earlier grant may cover this one.
46            guard !workspace.foldersToGrant(for: [folder.path], directories: true).isEmpty else { continue }
47            guard ask(folder, workspace: workspace) == .granted else { return false }
48        }
49        return true
50    }
51}
52#endif
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form