-
Notifications
You must be signed in to change notification settings - Fork 27
/
ShareMounterMenu.swift
157 lines (125 loc) · 5.23 KB
/
ShareMounterMenu.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// ShareMounterMenu.swift
// NoMAD
//
// Created by Joel Rennich on 8/12/17.
// Copyright © 2017 Orchard & Grove Inc. All rights reserved.
//
import Foundation
let shareMounterMenu = ShareMounterMenu()
let shareMounterQueue = DispatchQueue(label: "menu.nomad.NoMAD.shareMounting", attributes: [])
// class to build the share mount menu and accept clicks
@objc class ShareMounterMenu: NSObject {
let defaults = UserDefaults.standard
var shareMounter:ShareMounter?
@objc var worksWhenModal = true
@objc let myShareMenu = NSMenu()
var sharePrefs = UserDefaults.standard
@objc func updateShares(connected: Bool=false, tickets: Bool=false) {
guard let kerbUser = PasswordUtils().kerberosPrincipalFromCurrentLoggedInUser() else {
return
}
shareMounterQueue.sync(execute: {
self.shareMounter?.connectedState = connected
self.shareMounter?.tickets = tickets
self.shareMounter?.userPrincipal = kerbUser
self.shareMounter?.getMountedShares()
self.shareMounter?.getMounts()
self.shareMounter?.mountShares()
})
}
@objc func buildMenu(connected: Bool=false) -> NSMenu {
guard let shareMounter = shareMounter else {
return NSMenu()
}
// if sharePrefs.integer(forKey: "Version") != 1 {
// return NSMenu.init()
// }
// check for an actual ticket
klistUtil.klist()
// if !klistUtil.state {
// TCSLogWithMark("No valid ticket, not attempting to mount shares.")
//
//// NotificationQueue.default.enqueue(updateNotification, postingStyle: .now)
//
// return NSMenu.init()
// }
// guard let user = try? PasswordUtils.getLocalRecord(getConsoleUser()) else {
//
// return NSMenu()
// }
// if defaults.object(forKey: PrefKeys.userShortName.rawValue) == nil {
// TCSLogWithMark("No user name, not attempting to mount shares.")
// return NSMenu.init()
// }
if shareMounter.all_shares.count > 0 {
// Menu Items and Menu
myShareMenu.removeAllItems()
if CommandLine.arguments.contains("-shares") {
print("***Building Share Menu***")
print(shareMounter.all_shares)
}
for share in shareMounter.all_shares {
let myItem = NSMenuItem()
myItem.title = share.name
myItem.target = self
if share.connectedOnly == true && connected == false {
myItem.target = nil
}
myItem.action = #selector(openShareFromMenu(_:))
myItem.toolTip = String(describing: share.url)
if share.mountStatus == .mounted {
myItem.isEnabled = true
myItem.state = NSControl.StateValue(rawValue: 1)
} else if share.mountStatus == .mounting {
myItem.isEnabled = false
myItem.state = NSControl.StateValue(rawValue: 0)
} else if share.mountStatus == .unmounted {
myItem.isEnabled = true
myItem.state = NSControl.StateValue(rawValue: 0)
} else if share.mountStatus == .errorOnMount {
myItem.isEnabled = false
myItem.state = NSControl.StateValue(rawValue: 0)
}
myShareMenu.addItem(myItem)
}
}
if CommandLine.arguments.contains("-shares") {
print("***Share Menu***")
print(myShareMenu)
}
return myShareMenu
}
@IBAction func openShareFromMenu(_ sender: AnyObject) {
guard let shareMounter = shareMounter else {
return
}
for share in shareMounter.all_shares {
if share.name == sender.title {
if share.mountStatus != .mounted {
TCSLogWithMark("Mounting share: " + String(describing: share.url))
//myShareMounter.asyncMountShare(share.url, options: share.options, open: true)
//_ = cliTask("open " + DFSResolver.checkAndReplace(url: share.url))
_ = cliTask("open " + share.url.absoluteString.safeURLPath()!)
} else if share.mountStatus == .mounted {
// open up the local shares
// cliTask(“open ” + DFSResolver.checkAndReplace(url: share.url))
if share.localMountPoints != nil {
NSWorkspace.shared.open(URL(fileURLWithPath: share.localMountPoints!, isDirectory: true))
} else {
_ = cliTask("open " + share.url.absoluteString.safeURLPath()!)
}
}
}
}
updateShares()
}
// utility functions
@objc func sharesAvilable() -> Bool {
if myShareMenu.items.count == 0 {
return false
} else {
return true
}
}
}