2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// StatusMenu.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 8/14/19.
|
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
import PassepartoutCore
|
|
|
|
import TunnelKit
|
|
|
|
import Convenience
|
|
|
|
|
|
|
|
class StatusMenu: NSObject {
|
|
|
|
static let didAddProfile = Notification.Name("didAddProfile")
|
|
|
|
|
|
|
|
static let didRenameProfile = Notification.Name("didRenameProfile")
|
|
|
|
|
|
|
|
static let didRemoveProfile = Notification.Name("didRemoveProfile")
|
|
|
|
|
|
|
|
static let willDeactivateProfile = Notification.Name("willDeactivateProfile")
|
|
|
|
|
|
|
|
static let didActivateProfile = Notification.Name("didActivateProfile")
|
|
|
|
|
|
|
|
static let didUpdateProfile = Notification.Name("didUpdateProfile")
|
|
|
|
|
|
|
|
static let shared = StatusMenu()
|
|
|
|
|
|
|
|
private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
|
|
|
|
|
|
|
private let service = TransientStore.shared.service
|
|
|
|
|
|
|
|
private var vpn: GracefulVPN {
|
|
|
|
return GracefulVPN(service: service)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Button images
|
|
|
|
|
|
|
|
private let imageStatus = Asset.Assets.statusBarButtonImage.image
|
|
|
|
|
|
|
|
private lazy var imageStatusActive: NSImage = imageStatus.tinted(withColor: Theme.current.palette.colorOn)
|
|
|
|
|
|
|
|
private lazy var imageStatusInactive: NSImage = imageStatus.tinted(withColor: Theme.current.palette.colorPrimaryText)
|
|
|
|
|
|
|
|
private lazy var imageStatusInProgress: NSImage = imageStatus.tinted(withColor: Theme.current.palette.colorIndeterminate)
|
|
|
|
|
|
|
|
// MARK: Menu references
|
|
|
|
|
|
|
|
private let menu = NSMenu()
|
|
|
|
|
|
|
|
private let menuAllProfiles = NSMenu()
|
|
|
|
|
2021-02-02 19:39:09 +00:00
|
|
|
private lazy var itemSwitchProfile = NSMenuItem(title: L10n.App.Menu.SwitchProfile.title, action: nil, keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
private var itemsAllProfiles: [NSMenuItem] = []
|
|
|
|
|
2021-02-02 19:39:09 +00:00
|
|
|
private lazy var itemProfileName = NSMenuItem(title: "", action: nil, keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
private var itemsProfile: [NSMenuItem] = []
|
|
|
|
|
2021-02-02 19:39:09 +00:00
|
|
|
private lazy var itemPool = NSMenuItem(title: "", action: nil, keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-02-02 19:39:09 +00:00
|
|
|
private lazy var itemToggleVPN = NSMenuItem(title: L10n.App.Service.Cells.Vpn.TurnOn.caption, action: nil, keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-02-02 19:39:09 +00:00
|
|
|
private lazy var itemReconnectVPN = NSMenuItem(title: L10n.Core.Service.Cells.Reconnect.caption, action: #selector(reconnectVPN), keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
private override init() {
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
let nc = NotificationCenter.default
|
|
|
|
nc.addObserver(self, selector: #selector(vpnDidUpdate), name: VPN.didChangeStatus, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
func install() {
|
|
|
|
guard let button = statusItem.button else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
button.image = imageStatus
|
|
|
|
|
|
|
|
VPN.shared.prepare {
|
|
|
|
self.rebuild()
|
|
|
|
self.statusItem.menu = self.menu
|
2021-02-02 19:39:09 +00:00
|
|
|
self.service.delegate = self
|
2021-02-02 19:50:54 +00:00
|
|
|
self.reloadVpnStatus()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func rebuild() {
|
|
|
|
menu.removeAllItems()
|
|
|
|
|
2021-01-26 17:33:56 +00:00
|
|
|
// main actions
|
|
|
|
|
|
|
|
let itemShow = NSMenuItem(title: L10n.App.Menu.Show.title, action: #selector(showOrganizer), keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
let itemPreferences = NSMenuItem(title: L10n.App.Menu.Preferences.title.asContinuation, action: #selector(showPreferences), keyEquivalent: ",")
|
2021-01-26 17:33:56 +00:00
|
|
|
itemShow.target = self
|
2021-01-01 10:13:41 +00:00
|
|
|
itemPreferences.target = self
|
2021-01-26 17:33:56 +00:00
|
|
|
menu.addItem(itemShow)
|
2021-01-01 10:13:41 +00:00
|
|
|
menu.addItem(itemPreferences)
|
2021-02-02 19:39:09 +00:00
|
|
|
menu.addItem(itemSwitchProfile)
|
2021-01-01 10:13:41 +00:00
|
|
|
reloadProfiles()
|
|
|
|
menu.addItem(.separator())
|
|
|
|
|
|
|
|
// active profile
|
|
|
|
|
2021-02-02 19:39:09 +00:00
|
|
|
menu.addItem(itemProfileName)
|
2021-01-01 10:13:41 +00:00
|
|
|
setActiveProfile(service.activeProfile)
|
|
|
|
menu.addItem(.separator())
|
2021-01-26 17:33:56 +00:00
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
// support
|
|
|
|
|
|
|
|
let menuSupport = NSMenu()
|
|
|
|
let itemCommunity = NSMenuItem(title: L10n.Core.Organizer.Cells.JoinCommunity.caption.asContinuation, action: #selector(joinCommunity), keyEquivalent: "")
|
|
|
|
// let itemDonate = NSMenuItem(title: L10n.Core.Organizer.Cells.Donate.caption.asContinuation, action: #selector(showDonations), keyEquivalent: "")
|
|
|
|
// let itemGitHubSponsors = NSMenuItem(title: L10n.Core.Organizer.Cells.GithubSponsors.caption.asContinuation, action: #selector(seeGitHubSponsors), keyEquivalent: "")
|
|
|
|
// let itemTranslate = NSMenuItem(title: L10n.Core.Organizer.Cells.Translate.caption.asContinuation, action: #selector(offerToTranslate), keyEquivalent: "")
|
|
|
|
let itemFAQ = NSMenuItem(title: L10n.Core.About.Cells.Faq.caption.asContinuation, action: #selector(visitFAQ), keyEquivalent: "")
|
|
|
|
itemCommunity.target = self
|
|
|
|
// itemDonate.target = self
|
|
|
|
// itemGitHubSponsors.target = self
|
|
|
|
// itemTranslate.target = self
|
|
|
|
itemFAQ.target = self
|
|
|
|
// menuSupport.addItem(itemDonate)
|
|
|
|
menuSupport.addItem(itemCommunity)
|
|
|
|
// menuSupport.addItem(.separator())
|
|
|
|
// menuSupport.addItem(itemGitHubSponsors)
|
|
|
|
// menuSupport.addItem(itemTranslate)
|
2021-02-02 16:13:39 +00:00
|
|
|
if ProductManager.shared.isEligibleForFeedback() {
|
|
|
|
let itemReview = NSMenuItem(title: L10n.Core.Organizer.Cells.WriteReview.caption.asContinuation, action: #selector(writeReview), keyEquivalent: "")
|
|
|
|
itemReview.target = self
|
|
|
|
menuSupport.addItem(itemReview)
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
menuSupport.addItem(.separator())
|
|
|
|
menuSupport.addItem(itemFAQ)
|
2021-02-02 16:13:39 +00:00
|
|
|
if ProductManager.shared.isEligibleForFeedback() {
|
|
|
|
let itemReport = NSMenuItem(title: L10n.Core.Service.Cells.ReportIssue.caption.asContinuation, action: #selector(reportConnectivityIssue), keyEquivalent: "")
|
|
|
|
itemReport.target = self
|
|
|
|
menuSupport.addItem(itemReport)
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
let itemSupport = NSMenuItem(title: L10n.App.Menu.Support.title, action: nil, keyEquivalent: "")
|
|
|
|
menu.setSubmenu(menuSupport, for: itemSupport)
|
|
|
|
menu.addItem(itemSupport)
|
2021-02-04 14:57:29 +00:00
|
|
|
|
|
|
|
// share
|
|
|
|
|
|
|
|
let menuShare = NSMenu()
|
|
|
|
let itemTweet = NSMenuItem(title: L10n.Core.About.Cells.ShareTwitter.caption, action: #selector(shareTwitter), keyEquivalent: "")
|
|
|
|
let itemInvite = NSMenuItem(title: L10n.Core.About.Cells.ShareGeneric.caption.asContinuation, action: #selector(shareGeneric), keyEquivalent: "")
|
|
|
|
let itemAlternativeTo = NSMenuItem(title: "AlternativeTo".asContinuation, action: #selector(visitAlternativeTo), keyEquivalent: "")
|
|
|
|
itemTweet.target = self
|
|
|
|
itemInvite.target = self
|
|
|
|
itemAlternativeTo.target = self
|
|
|
|
menuShare.addItem(itemTweet)
|
|
|
|
menuShare.addItem(itemInvite)
|
|
|
|
menuShare.addItem(itemAlternativeTo)
|
|
|
|
let itemShare = NSMenuItem(title: L10n.Core.About.Sections.Share.header, action: nil, keyEquivalent: "")
|
|
|
|
menu.setSubmenu(menuShare, for: itemShare)
|
|
|
|
menu.addItem(itemShare)
|
2021-01-01 10:13:41 +00:00
|
|
|
menu.addItem(.separator())
|
|
|
|
|
2021-01-26 17:33:56 +00:00
|
|
|
// secondary
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-01-26 17:33:56 +00:00
|
|
|
let itemAbout = NSMenuItem(title: L10n.Core.Organizer.Cells.About.caption(GroupConstants.App.name), action: #selector(showAbout), keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
let itemQuit = NSMenuItem(title: L10n.App.Menu.Quit.title(GroupConstants.App.name), action: #selector(quit), keyEquivalent: "q")
|
2021-01-26 17:33:56 +00:00
|
|
|
itemAbout.target = self
|
2021-01-01 10:13:41 +00:00
|
|
|
itemQuit.target = self
|
2021-01-26 17:33:56 +00:00
|
|
|
menu.addItem(itemAbout)
|
2021-01-01 10:13:41 +00:00
|
|
|
menu.addItem(itemQuit)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func reloadProfiles() {
|
|
|
|
for item in itemsAllProfiles {
|
|
|
|
menuAllProfiles.removeItem(item)
|
|
|
|
}
|
|
|
|
itemsAllProfiles.removeAll()
|
|
|
|
|
|
|
|
let sortedProfileKeys = service.allProfileKeys().sorted {
|
|
|
|
service.screenTitle($0).lowercased() < service.screenTitle($1).lowercased()
|
|
|
|
}
|
|
|
|
|
|
|
|
for profileKey in sortedProfileKeys {
|
|
|
|
let title = service.screenTitle(profileKey)
|
|
|
|
let item = NSMenuItem(title: title, action: #selector(switchActiveProfile(_:)), keyEquivalent: "")
|
|
|
|
item.representedObject = profileKey
|
|
|
|
item.target = self
|
|
|
|
item.state = service.isActiveProfile(profileKey) ? .on : .off
|
|
|
|
menuAllProfiles.addItem(item)
|
|
|
|
itemsAllProfiles.append(item)
|
|
|
|
}
|
2021-02-02 19:39:09 +00:00
|
|
|
menu.setSubmenu(menuAllProfiles, for: itemSwitchProfile)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func refreshWithCurrentProfile() {
|
|
|
|
setActiveProfile(service.activeProfile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setActiveProfile(_ profile: ConnectionProfile?) {
|
2021-02-02 19:39:09 +00:00
|
|
|
let startIndex = menu.index(of: itemProfileName)
|
2021-01-01 10:13:41 +00:00
|
|
|
var i = startIndex + 1
|
|
|
|
|
|
|
|
for item in itemsProfile {
|
|
|
|
menu.removeItem(item)
|
|
|
|
}
|
|
|
|
itemsProfile.removeAll()
|
|
|
|
|
|
|
|
guard let profile = profile else {
|
2021-02-02 19:39:09 +00:00
|
|
|
itemProfileName.title = L10n.App.Menu.ActiveProfile.Title.none
|
|
|
|
// itemProfileName.image = nil
|
2021-01-01 10:13:41 +00:00
|
|
|
statusItem.button?.image = imageStatusInactive
|
2021-01-24 21:19:18 +00:00
|
|
|
statusItem.button?.toolTip = nil
|
2021-01-01 10:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-24 21:19:18 +00:00
|
|
|
let profileTitle = service.screenTitle(ProfileKey(profile))
|
2021-02-02 19:39:09 +00:00
|
|
|
itemProfileName.title = profileTitle
|
|
|
|
// itemProfileName.image = profile.image
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
let needsCredentials = service.needsCredentials(for: profile)
|
|
|
|
if !needsCredentials {
|
2021-02-02 19:39:09 +00:00
|
|
|
itemToggleVPN.indentationLevel = 1
|
|
|
|
itemReconnectVPN.indentationLevel = 1
|
|
|
|
itemToggleVPN.target = self
|
|
|
|
itemReconnectVPN.target = self
|
|
|
|
menu.insertItem(itemToggleVPN, at: i)
|
2021-01-01 10:13:41 +00:00
|
|
|
i += 1
|
2021-02-02 19:39:09 +00:00
|
|
|
menu.insertItem(itemReconnectVPN, at: i)
|
2021-01-01 10:13:41 +00:00
|
|
|
i += 1
|
|
|
|
|
2021-02-02 19:39:09 +00:00
|
|
|
itemsProfile.append(itemToggleVPN)
|
|
|
|
itemsProfile.append(itemReconnectVPN)
|
2021-01-01 10:13:41 +00:00
|
|
|
} else {
|
|
|
|
let itemMissingCredentials = NSMenuItem(title: L10n.App.Menu.ActiveProfile.Messages.missingCredentials, action: nil, keyEquivalent: "")
|
|
|
|
itemMissingCredentials.indentationLevel = 1
|
|
|
|
menu.insertItem(itemMissingCredentials, at: i)
|
|
|
|
i += 1
|
|
|
|
itemsProfile.append(itemMissingCredentials)
|
|
|
|
}
|
|
|
|
|
2021-02-02 19:50:54 +00:00
|
|
|
reloadVpnStatus()
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
if !needsCredentials, let providerProfile = profile as? ProviderConnectionProfile {
|
|
|
|
|
|
|
|
// endpoint (port only)
|
|
|
|
let itemEndpoint = NSMenuItem(title: L10n.Core.Endpoint.title, action: nil, keyEquivalent: "")
|
|
|
|
itemEndpoint.indentationLevel = 1
|
|
|
|
let menuEndpoint = NSMenu()
|
|
|
|
|
|
|
|
// automatic
|
|
|
|
let itemEndpointAutomatic = NSMenuItem(title: L10n.Core.Endpoint.Cells.AnyProtocol.caption, action: #selector(connectToEndpoint(_:)), keyEquivalent: "")
|
|
|
|
itemEndpointAutomatic.target = self
|
|
|
|
if providerProfile.customProtocol == nil {
|
|
|
|
itemEndpointAutomatic.state = .on
|
|
|
|
}
|
|
|
|
menuEndpoint.addItem(itemEndpointAutomatic)
|
|
|
|
|
|
|
|
for proto in profile.protocols {
|
|
|
|
let item = NSMenuItem(title: proto.description, action: #selector(connectToEndpoint(_:)), keyEquivalent: "")
|
|
|
|
item.representedObject = proto
|
|
|
|
item.target = self
|
|
|
|
if providerProfile.customProtocol == proto {
|
|
|
|
item.state = .on
|
|
|
|
}
|
|
|
|
menuEndpoint.addItem(item)
|
|
|
|
}
|
|
|
|
menu.setSubmenu(menuEndpoint, for: itemEndpoint)
|
|
|
|
menu.insertItem(itemEndpoint, at: i)
|
|
|
|
i += 1
|
|
|
|
itemsProfile.append(itemEndpoint)
|
|
|
|
|
|
|
|
// account
|
|
|
|
let itemAccount = NSMenuItem(title: L10n.Core.Account.title.asContinuation, action: #selector(editAccountCredentials(_:)), keyEquivalent: "")
|
|
|
|
menu.insertItem(itemAccount, at: i)
|
|
|
|
i += 1
|
|
|
|
itemAccount.target = self
|
|
|
|
itemAccount.indentationLevel = 1
|
|
|
|
itemsProfile.append(itemAccount)
|
|
|
|
|
|
|
|
// customize
|
|
|
|
let itemCustomize = NSMenuItem(title: L10n.App.Menu.ActiveProfile.Items.Customize.title, action: #selector(customizeProfile(_:)), keyEquivalent: "")
|
|
|
|
menu.insertItem(itemCustomize, at: i)
|
|
|
|
i += 1
|
|
|
|
itemCustomize.target = self
|
|
|
|
itemCustomize.indentationLevel = 1
|
|
|
|
itemsProfile.append(itemCustomize)
|
|
|
|
|
|
|
|
let itemSep1: NSMenuItem = .separator()
|
|
|
|
menu.insertItem(itemSep1, at: i)
|
|
|
|
i += 1
|
|
|
|
itemsProfile.append(itemSep1)
|
|
|
|
|
|
|
|
// guard poolDescription = providerProfile.pool?.localizedId else {
|
|
|
|
// fatalError("No pool selected?")
|
|
|
|
// }
|
2021-02-02 19:39:09 +00:00
|
|
|
itemPool.title = providerProfile.pool?.localizedId ?? ""
|
|
|
|
menu.insertItem(itemPool, at: i)
|
2021-01-01 10:13:41 +00:00
|
|
|
i += 1
|
2021-02-02 19:39:09 +00:00
|
|
|
itemsProfile.append(itemPool)
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
let infrastructure = providerProfile.infrastructure
|
|
|
|
for category in infrastructure.categories {
|
2021-01-03 16:37:36 +00:00
|
|
|
let title = category.name.isEmpty ? L10n.Core.Global.Values.default : category.name.capitalized
|
2021-01-01 10:13:41 +00:00
|
|
|
let submenu = NSMenu()
|
2021-01-26 18:16:52 +00:00
|
|
|
let itemCategory = NSMenuItem(title: title, action: nil, keyEquivalent: "")
|
|
|
|
itemCategory.indentationLevel = 1
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
for group in category.groups.sorted() {
|
2021-01-03 14:01:08 +00:00
|
|
|
let title = group.localizedCountry
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-01-03 14:19:39 +00:00
|
|
|
let itemGroup = NSMenuItem(title: title, action: #selector(connectToGroup(_:)), keyEquivalent: "")
|
2021-01-03 11:49:00 +00:00
|
|
|
itemGroup.image = group.logo
|
2021-01-01 10:13:41 +00:00
|
|
|
itemGroup.target = self
|
|
|
|
itemGroup.representedObject = group
|
|
|
|
|
2021-01-03 14:19:39 +00:00
|
|
|
let submenuGroup = NSMenu()
|
2021-01-01 10:13:41 +00:00
|
|
|
for pool in group.pools {
|
2021-01-03 16:37:36 +00:00
|
|
|
let title = !pool.secondaryId.isEmpty ? pool.secondaryId : L10n.Core.Global.Values.default
|
2021-01-03 14:19:39 +00:00
|
|
|
let item = NSMenuItem(title: title, action: #selector(connectToPool(_:)), keyEquivalent: "")
|
|
|
|
if let extraCountry = pool.extraCountries?.first {
|
|
|
|
item.image = extraCountry.image
|
|
|
|
}
|
|
|
|
item.target = self
|
|
|
|
item.representedObject = pool
|
|
|
|
submenuGroup.addItem(item)
|
2021-01-14 23:42:49 +00:00
|
|
|
|
|
|
|
if pool.id == providerProfile.poolId {
|
2021-01-26 18:16:52 +00:00
|
|
|
itemCategory.state = .on
|
2021-01-14 23:42:49 +00:00
|
|
|
itemGroup.state = .on
|
|
|
|
item.state = .on
|
|
|
|
}
|
2021-01-03 14:19:39 +00:00
|
|
|
}
|
|
|
|
if submenuGroup.numberOfItems > 1 {
|
|
|
|
itemGroup.action = nil
|
|
|
|
itemGroup.submenu = submenuGroup
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-01-03 14:19:39 +00:00
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
submenu.addItem(itemGroup)
|
|
|
|
}
|
2021-01-26 18:16:52 +00:00
|
|
|
menu.setSubmenu(submenu, for: itemCategory)
|
|
|
|
menu.insertItem(itemCategory, at: i)
|
2021-01-01 10:13:41 +00:00
|
|
|
i += 1
|
2021-01-26 18:16:52 +00:00
|
|
|
itemsProfile.append(itemCategory)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// account
|
|
|
|
let itemAccount = NSMenuItem(title: L10n.Core.Account.title.asContinuation, action: #selector(editAccountCredentials(_:)), keyEquivalent: "")
|
|
|
|
menu.insertItem(itemAccount, at: i)
|
|
|
|
i += 1
|
|
|
|
itemAccount.target = self
|
|
|
|
itemAccount.indentationLevel = 1
|
|
|
|
itemsProfile.append(itemAccount)
|
|
|
|
|
|
|
|
// customize
|
|
|
|
let itemCustomize = NSMenuItem(title: L10n.App.Menu.ActiveProfile.Items.Customize.title, action: #selector(customizeProfile(_:)), keyEquivalent: "")
|
|
|
|
menu.insertItem(itemCustomize, at: i)
|
|
|
|
i += 1
|
|
|
|
itemCustomize.target = self
|
|
|
|
itemCustomize.indentationLevel = 1
|
|
|
|
itemsProfile.append(itemCustomize)
|
|
|
|
|
|
|
|
let itemSep1: NSMenuItem = .separator()
|
|
|
|
menu.insertItem(itemSep1, at: i)
|
|
|
|
i += 1
|
|
|
|
itemsProfile.append(itemSep1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@objc private func showAbout() {
|
|
|
|
WindowManager.shared.showAbout()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func showOrganizer() {
|
|
|
|
WindowManager.shared.showOrganizer()
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func showPreferences() {
|
|
|
|
let organizer = WindowManager.shared.showOrganizer()
|
|
|
|
let preferences = StoryboardScene.Preferences.initialScene.instantiate()
|
|
|
|
organizer?.contentViewController?.presentAsModalWindow(preferences)
|
|
|
|
}
|
2021-01-26 17:33:56 +00:00
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
@objc private func switchActiveProfile(_ sender: Any?) {
|
|
|
|
guard let item = sender as? NSMenuItem else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let profileKey = item.representedObject as? ProfileKey, let profile = service.profile(withKey: profileKey) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let wasConnected = (vpn.status == .connected) || (vpn.status == .connecting)
|
|
|
|
|
|
|
|
// XXX: copy/paste from ServiceViewController.activateProfile()
|
|
|
|
service.activateProfile(profile)
|
|
|
|
vpn.disconnect(completionHandler: nil)
|
|
|
|
if wasConnected {
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) { [weak self] in
|
|
|
|
self?.vpn.reconnect(completionHandler: nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func enableVPN() {
|
|
|
|
vpn.reconnect(completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func disableVPN() {
|
|
|
|
vpn.disconnect(completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func reconnectVPN() {
|
|
|
|
vpn.reconnect(completionHandler: nil)
|
|
|
|
}
|
|
|
|
|
2021-01-03 14:19:39 +00:00
|
|
|
@objc private func connectToGroup(_ sender: Any?) {
|
2021-01-01 10:13:41 +00:00
|
|
|
guard let item = sender as? NSMenuItem else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let group = item.representedObject as? PoolGroup else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let profile = service.activeProfile as? ProviderConnectionProfile else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert(!group.pools.isEmpty)
|
2021-01-08 17:46:57 +00:00
|
|
|
profile.poolId = group.pools.randomElement()!.id
|
2021-01-01 10:13:41 +00:00
|
|
|
vpn.reconnect(completionHandler: nil)
|
|
|
|
|
|
|
|
// update menu
|
|
|
|
setActiveProfile(profile)
|
|
|
|
}
|
|
|
|
|
2021-01-03 14:19:39 +00:00
|
|
|
@objc private func connectToPool(_ sender: Any?) {
|
|
|
|
guard let item = sender as? NSMenuItem else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let pool = item.representedObject as? Pool else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let profile = service.activeProfile as? ProviderConnectionProfile else {
|
|
|
|
return
|
|
|
|
}
|
2021-01-08 17:46:57 +00:00
|
|
|
profile.poolId = pool.id
|
2021-01-03 14:19:39 +00:00
|
|
|
vpn.reconnect(completionHandler: nil)
|
|
|
|
|
|
|
|
// update menu
|
|
|
|
setActiveProfile(profile)
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
@objc private func connectToEndpoint(_ sender: Any?) {
|
|
|
|
guard let item = sender as? NSMenuItem else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let profile = service.activeProfile as? ProviderConnectionProfile else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
profile.customProtocol = item.representedObject as? EndpointProtocol
|
|
|
|
vpn.reconnect(completionHandler: nil)
|
|
|
|
|
|
|
|
// update menu
|
|
|
|
setActiveProfile(profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func editAccountCredentials(_ sender: Any?) {
|
|
|
|
let organizer = WindowManager.shared.showOrganizer()
|
|
|
|
let accountController = StoryboardScene.Service.accountViewController.instantiate()
|
|
|
|
accountController.profile = service.activeProfile
|
|
|
|
organizer?.contentViewController?.presentAsSheet(accountController)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func customizeProfile(_ sender: Any?) {
|
|
|
|
let organizer = WindowManager.shared.showOrganizer()
|
|
|
|
let profileCustomization = StoryboardScene.Service.profileCustomizationContainerViewController.instantiate()
|
|
|
|
profileCustomization.profile = service.activeProfile
|
|
|
|
organizer?.contentViewController?.presentAsSheet(profileCustomization)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func joinCommunity() {
|
|
|
|
NSWorkspace.shared.open(AppConstants.URLs.subreddit)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func writeReview() {
|
|
|
|
let url = Reviewer.urlForReview(withAppId: AppConstants.App.appStoreId)
|
|
|
|
NSWorkspace.shared.open(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func showDonations() {
|
|
|
|
NSWorkspace.shared.open(AppConstants.URLs.donate)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func seeGitHubSponsors() {
|
|
|
|
NSWorkspace.shared.open(AppConstants.URLs.githubSponsors)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func offerToTranslate() {
|
|
|
|
let V = AppConstants.Translations.Email.self
|
|
|
|
let recipient = V.recipient
|
|
|
|
let subject = V.subject
|
|
|
|
let body = V.body(V.template)
|
|
|
|
|
|
|
|
guard let url = URL.mailto(to: recipient, subject: subject, body: body) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
NSWorkspace.shared.open(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func visitFAQ() {
|
|
|
|
NSWorkspace.shared.open(AppConstants.URLs.faq)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func reportConnectivityIssue() {
|
|
|
|
let issue = Issue(debugLog: true, profile: TransientStore.shared.service.activeProfile)
|
|
|
|
IssueReporter.shared.present(withIssue: issue)
|
|
|
|
}
|
|
|
|
|
2021-02-04 14:57:29 +00:00
|
|
|
@objc private func shareTwitter() {
|
|
|
|
NSWorkspace.shared.open(AppConstants.URLs.twitterIntent(withMessage: L10n.Core.Share.message))
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func shareGeneric() {
|
|
|
|
guard let source = statusItem.button else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let message = "\(L10n.Core.Share.message) \(AppConstants.URLs.website)"
|
|
|
|
let picker = NSSharingServicePicker(items: [message])
|
|
|
|
picker.show(relativeTo: source.bounds, of: source, preferredEdge: .minY)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func visitAlternativeTo() {
|
|
|
|
NSWorkspace.shared.open(AppConstants.URLs.alternativeTo)
|
|
|
|
}
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
@objc private func quit() {
|
|
|
|
NSApp.terminate(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
@objc private func vpnDidUpdate() {
|
|
|
|
reloadVpnStatus()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helpers
|
|
|
|
|
|
|
|
private func reloadVpnStatus() {
|
|
|
|
if vpn.isEnabled {
|
2021-02-02 19:39:09 +00:00
|
|
|
itemToggleVPN.title = L10n.App.Service.Cells.Vpn.TurnOff.caption
|
|
|
|
itemToggleVPN.action = #selector(disableVPN)
|
2021-01-01 10:13:41 +00:00
|
|
|
} else {
|
2021-02-02 19:39:09 +00:00
|
|
|
itemToggleVPN.title = L10n.App.Service.Cells.Vpn.TurnOn.caption
|
|
|
|
itemToggleVPN.action = #selector(enableVPN)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-01-24 21:19:18 +00:00
|
|
|
if let profile = service.activeProfile {
|
|
|
|
let profileTitle = service.screenTitle(ProfileKey(profile))
|
|
|
|
statusItem.button?.toolTip = "\(GroupConstants.App.name)\n\(profileTitle)\n\((vpn.status ?? .disconnected).uiDescription)"
|
|
|
|
} else {
|
|
|
|
statusItem.button?.toolTip = nil
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
switch vpn.status ?? .disconnected {
|
|
|
|
case .connected:
|
|
|
|
statusItem.button?.image = imageStatusActive
|
2021-02-04 14:18:10 +00:00
|
|
|
statusItem.button?.alphaValue = 1.0
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
Reviewer.shared.reportEvent()
|
|
|
|
|
|
|
|
case .connecting:
|
|
|
|
statusItem.button?.image = imageStatusInProgress
|
2021-02-04 14:18:10 +00:00
|
|
|
statusItem.button?.alphaValue = 1.0
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
case .disconnected:
|
|
|
|
statusItem.button?.image = imageStatusInactive
|
2021-01-24 21:21:26 +00:00
|
|
|
statusItem.button?.alphaValue = 0.5
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
case .disconnecting:
|
|
|
|
statusItem.button?.image = imageStatusInProgress
|
2021-02-04 14:18:10 +00:00
|
|
|
statusItem.button?.alphaValue = 1.0
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension StatusMenu: ConnectionServiceDelegate {
|
|
|
|
func connectionService(didAdd profile: ConnectionProfile) {
|
|
|
|
TransientStore.shared.serialize(withProfiles: false) // add
|
|
|
|
reloadProfiles()
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: StatusMenu.didAddProfile, object: profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectionService(didRename profile: ConnectionProfile, to newTitle: String) {
|
|
|
|
TransientStore.shared.serialize(withProfiles: false) // rename
|
|
|
|
reloadProfiles()
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: StatusMenu.didRenameProfile, object: profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectionService(didRemoveProfileWithKey key: ProfileKey) {
|
|
|
|
TransientStore.shared.serialize(withProfiles: false) // delete
|
|
|
|
reloadProfiles()
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: StatusMenu.didRemoveProfile, object: key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectionService(willDeactivate profile: ConnectionProfile) {
|
|
|
|
TransientStore.shared.serialize(withProfiles: false) // deactivate
|
|
|
|
reloadProfiles()
|
|
|
|
setActiveProfile(nil)
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: StatusMenu.willDeactivateProfile, object: profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectionService(didActivate profile: ConnectionProfile) {
|
|
|
|
TransientStore.shared.serialize(withProfiles: false) // activate
|
|
|
|
reloadProfiles()
|
|
|
|
setActiveProfile(profile)
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: StatusMenu.didActivateProfile, object: profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectionService(didUpdate profile: ConnectionProfile) {
|
|
|
|
guard let providerProfile = profile as? ProviderConnectionProfile else {
|
|
|
|
return
|
|
|
|
}
|
2021-02-02 19:39:09 +00:00
|
|
|
itemPool.title = providerProfile.pool?.localizedId ?? ""
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
NotificationCenter.default.post(name: StatusMenu.didUpdateProfile, object: profile)
|
|
|
|
}
|
|
|
|
}
|