2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// ProviderServiceView.swift
|
2021-01-01 16:53:28 +00:00
|
|
|
// Passepartout
|
2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 6/13/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
|
|
|
|
|
2021-06-26 07:49:48 +00:00
|
|
|
protocol ProviderServiceViewDelegate: AnyObject {
|
2021-01-01 10:13:41 +00:00
|
|
|
func providerView(_ providerView: ProviderServiceView, didSelectPool pool: Pool)
|
|
|
|
|
|
|
|
func providerViewDidRequestInfrastructureRefresh(_ providerView: ProviderServiceView)
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProviderServiceView: NSView {
|
|
|
|
@IBOutlet private weak var labelCategoryCaption: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var popupCategory: NSPopUpButton!
|
|
|
|
|
|
|
|
@IBOutlet private weak var labelLocationCaption: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var popupLocation: NSPopUpButton!
|
|
|
|
|
|
|
|
@IBOutlet private weak var popupArea: NSPopUpButton!
|
|
|
|
|
2021-07-29 14:55:20 +00:00
|
|
|
@IBOutlet private weak var checkOnlyShowsFavorites: NSButton!
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
@IBOutlet private weak var labelLastInfrastructureUpdate: NSTextField!
|
|
|
|
|
|
|
|
@IBOutlet private weak var buttonRefreshInfrastructure: NSButton!
|
|
|
|
|
2021-07-25 08:38:09 +00:00
|
|
|
@IBOutlet private weak var buttonFavorite: NSButton!
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
var isEnabled: Bool = true {
|
|
|
|
didSet {
|
|
|
|
popupCategory.isEnabled = isEnabled
|
|
|
|
popupLocation.isEnabled = isEnabled
|
|
|
|
popupArea.isEnabled = isEnabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var profile: ProviderConnectionProfile? {
|
|
|
|
didSet {
|
|
|
|
guard let profile = profile else {
|
|
|
|
popupCategory.removeAllItems()
|
|
|
|
popupLocation.removeAllItems()
|
|
|
|
popupArea.removeAllItems()
|
|
|
|
return
|
|
|
|
}
|
2021-01-14 22:50:52 +00:00
|
|
|
reloadHierarchy(withProfile: profile)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var isRefreshingInfrastructure: Bool = false {
|
|
|
|
didSet {
|
|
|
|
buttonRefreshInfrastructure.isEnabled = !isRefreshingInfrastructure
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 22:50:52 +00:00
|
|
|
|
2021-07-29 14:55:20 +00:00
|
|
|
private var onlyShowsFavorites: Bool = false {
|
|
|
|
didSet {
|
|
|
|
guard let profile = profile else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reloadHierarchy(withProfile: profile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:50:52 +00:00
|
|
|
private var categories: [PoolCategory] = []
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-07-29 14:55:20 +00:00
|
|
|
private var filteredGroupsByCategory: [String: [PoolGroup]] = [:]
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
weak var delegate: ProviderServiceViewDelegate?
|
|
|
|
|
|
|
|
override func viewWillMove(toSuperview newSuperview: NSView?) {
|
|
|
|
super.viewWillMove(toSuperview: newSuperview)
|
|
|
|
|
|
|
|
labelCategoryCaption.stringValue = L10n.App.Service.Cells.Category.caption.asCaption
|
|
|
|
labelLocationCaption.stringValue = L10n.Core.Service.Cells.Provider.Pool.caption.asCaption
|
2021-07-29 14:55:20 +00:00
|
|
|
checkOnlyShowsFavorites.title = L10n.App.Service.Cells.OnlyShowsFavorites.caption
|
|
|
|
checkOnlyShowsFavorites.state = .off
|
2021-01-01 10:13:41 +00:00
|
|
|
buttonRefreshInfrastructure.image = NSImage(named: NSImage.refreshTemplateName)
|
2021-07-25 08:38:09 +00:00
|
|
|
buttonFavorite.image = NSImage(named: NSImage.bookmarksTemplateName)
|
|
|
|
|
|
|
|
updateFavoriteState()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Actions
|
|
|
|
|
|
|
|
@IBAction private func selectCategory(_ sender: Any?) {
|
2021-01-14 22:50:52 +00:00
|
|
|
loadLocations()
|
|
|
|
loadAreas()
|
2021-07-29 14:55:20 +00:00
|
|
|
delegateSelectedPool()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func selectLocation(_ sender: Any?) {
|
2021-01-14 22:50:52 +00:00
|
|
|
loadAreas()
|
2021-07-29 14:55:20 +00:00
|
|
|
delegateSelectedPool()
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func selectArea(_ sender: Any?) {
|
2021-01-14 22:50:52 +00:00
|
|
|
guard let pool = popupArea.selectedItem?.representedObject as? Pool else {
|
|
|
|
return
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
delegate?.providerView(self, didSelectPool: pool)
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func refreshInfrastructure(_ sender: Any?) {
|
|
|
|
delegate?.providerViewDidRequestInfrastructureRefresh(self)
|
|
|
|
}
|
|
|
|
|
2021-07-25 08:38:09 +00:00
|
|
|
@IBAction private func toggleFavorite(_ sender: Any?) {
|
|
|
|
guard let category = selectedCategory(), let group = selectedGroup() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let groupId = group.uniqueId(in: category)
|
2021-07-29 14:55:20 +00:00
|
|
|
let isFavorite = (buttonFavorite.state == .on)
|
2021-07-25 08:38:09 +00:00
|
|
|
if isFavorite {
|
|
|
|
profile?.favoriteGroupIds = profile?.favoriteGroupIds ?? []
|
|
|
|
profile?.favoriteGroupIds?.append(groupId)
|
|
|
|
} else {
|
|
|
|
profile?.favoriteGroupIds?.removeAll { $0 == groupId }
|
|
|
|
}
|
2021-07-29 14:55:20 +00:00
|
|
|
|
|
|
|
// disable favorite while filtering favorites
|
|
|
|
//
|
|
|
|
// 1. reload list to select first
|
|
|
|
// 2. if last, disable filter
|
|
|
|
if onlyShowsFavorites, let profile = profile, buttonFavorite.state == .off {
|
|
|
|
if popupLocation.numberOfItems == 1 {
|
|
|
|
onlyShowsFavorites = false
|
|
|
|
checkOnlyShowsFavorites.state = .off
|
|
|
|
}
|
|
|
|
reloadHierarchy(withProfile: profile)
|
|
|
|
delegateSelectedPool()
|
|
|
|
}
|
|
|
|
if profile?.favoriteGroupIds?.isEmpty ?? true {
|
|
|
|
checkOnlyShowsFavorites.state = .off
|
|
|
|
checkOnlyShowsFavorites.isEnabled = false
|
|
|
|
} else {
|
|
|
|
checkOnlyShowsFavorites.isEnabled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction private func toggleOnlyShowsFavorites(_ sender: Any?) {
|
|
|
|
onlyShowsFavorites = (checkOnlyShowsFavorites.state == .on)
|
|
|
|
delegateSelectedPool()
|
2021-07-25 08:38:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
// MARK: Helpers
|
|
|
|
|
|
|
|
func reloadData() {
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:50:52 +00:00
|
|
|
private func reloadHierarchy(withProfile profile: ProviderConnectionProfile) {
|
2021-01-01 10:13:41 +00:00
|
|
|
categories = profile.infrastructure.categories.sorted { $0.name.lowercased() < $1.name.lowercased() }
|
|
|
|
popupCategory.removeAllItems()
|
2021-07-29 14:55:20 +00:00
|
|
|
filteredGroupsByCategory.removeAll()
|
2021-01-14 22:50:52 +00:00
|
|
|
|
|
|
|
let menu = NSMenu()
|
2021-07-29 14:55:20 +00:00
|
|
|
categories.forEach { category in
|
2021-01-14 22:50:52 +00:00
|
|
|
let item = NSMenuItem()
|
2021-07-29 14:55:20 +00:00
|
|
|
item.title = !category.name.isEmpty ? category.name.capitalized : L10n.Core.Global.Values.default
|
|
|
|
item.representedObject = category
|
2021-01-14 22:50:52 +00:00
|
|
|
menu.addItem(item)
|
2021-07-29 14:55:20 +00:00
|
|
|
|
|
|
|
setFilteredGroups(category.groups, forCategory: category)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-01-14 22:50:52 +00:00
|
|
|
popupCategory.menu = menu
|
2021-01-01 10:13:41 +00:00
|
|
|
|
2021-01-03 14:30:23 +00:00
|
|
|
let (a, b, c) = selectPopupsFromCurrentProfile()
|
|
|
|
if popupCategory.numberOfItems > 0 {
|
2021-01-01 10:13:41 +00:00
|
|
|
popupCategory.selectItem(at: a)
|
2021-01-14 22:50:52 +00:00
|
|
|
loadLocations()
|
2021-01-03 14:30:23 +00:00
|
|
|
if popupLocation.numberOfItems > 0 {
|
|
|
|
popupLocation.selectItem(at: b)
|
2021-01-14 22:50:52 +00:00
|
|
|
loadAreas()
|
2021-01-03 14:30:23 +00:00
|
|
|
if popupArea.numberOfItems > 0 {
|
|
|
|
popupArea.selectItem(at: c)
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-01-03 14:30:23 +00:00
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
if let lastInfrastructureUpdate = InfrastructureFactory.shared.modificationDate(forName: profile.name) {
|
|
|
|
labelLastInfrastructureUpdate.stringValue = L10n.Core.Service.Sections.ProviderInfrastructure.footer(lastInfrastructureUpdate.timestamp)
|
|
|
|
}
|
2021-07-29 14:55:20 +00:00
|
|
|
|
|
|
|
checkOnlyShowsFavorites.isEnabled = !(profile.favoriteGroupIds?.isEmpty ?? true)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 22:50:52 +00:00
|
|
|
// FIXME: inefficient, cache sorted pools
|
|
|
|
private func selectPopupsFromCurrentProfile() -> (Int, Int, Int) {
|
|
|
|
var a = 0, b = 0, c = 0
|
|
|
|
for category in categories {
|
|
|
|
b = 0
|
2021-07-29 14:55:20 +00:00
|
|
|
for group in filteredGroups(forCategory: category) {
|
2021-01-14 22:50:52 +00:00
|
|
|
c = 0
|
|
|
|
for pool in group.pools.sortedPools() {
|
2021-01-01 10:13:41 +00:00
|
|
|
if pool.id == profile?.poolId {
|
|
|
|
return (a, b, c)
|
|
|
|
}
|
2021-01-14 22:50:52 +00:00
|
|
|
c += 1
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-01-14 22:50:52 +00:00
|
|
|
b += 1
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-01-14 22:50:52 +00:00
|
|
|
a += 1
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
2021-01-03 14:30:23 +00:00
|
|
|
return (0, 0, 0)
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 22:50:52 +00:00
|
|
|
private func loadLocations() {
|
|
|
|
guard let category = popupCategory.selectedItem?.representedObject as? PoolCategory else {
|
|
|
|
return
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
popupLocation.removeAllItems()
|
2021-01-14 22:50:52 +00:00
|
|
|
|
|
|
|
let menu = NSMenu()
|
2021-07-29 14:55:20 +00:00
|
|
|
filteredGroups(forCategory: category).forEach {
|
2021-01-03 14:09:24 +00:00
|
|
|
let item = NSMenuItem(title: $0.localizedCountry, action: nil, keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
item.image = $0.logo
|
2021-01-14 22:50:52 +00:00
|
|
|
item.representedObject = $0 // group
|
2021-01-01 10:13:41 +00:00
|
|
|
menu.addItem(item)
|
|
|
|
}
|
|
|
|
popupLocation.menu = menu
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:50:52 +00:00
|
|
|
private func loadAreas() {
|
|
|
|
guard let group = popupLocation.selectedItem?.representedObject as? PoolGroup else {
|
|
|
|
return
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
popupArea.removeAllItems()
|
2021-01-14 22:50:52 +00:00
|
|
|
|
2021-01-01 10:13:41 +00:00
|
|
|
// FIXME: inefficient, cache sorted pools
|
2021-01-14 22:50:52 +00:00
|
|
|
let menu = NSMenu()
|
|
|
|
let pools = group.pools.sortedPools()
|
|
|
|
pools.forEach {
|
|
|
|
guard !$0.secondaryId.isEmpty || pools.count > 1 else {
|
2021-01-01 10:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
2021-01-03 16:37:36 +00:00
|
|
|
let title = !$0.secondaryId.isEmpty ? $0.secondaryId : L10n.Core.Global.Values.default
|
2021-01-03 14:09:24 +00:00
|
|
|
let item = NSMenuItem(title: title, action: nil, keyEquivalent: "")
|
2021-01-01 10:13:41 +00:00
|
|
|
if let extraCountry = $0.extraCountries?.first {
|
|
|
|
item.image = extraCountry.image
|
|
|
|
}
|
2021-01-14 22:50:52 +00:00
|
|
|
item.representedObject = $0 // pool
|
2021-01-01 10:13:41 +00:00
|
|
|
menu.addItem(item)
|
|
|
|
}
|
|
|
|
popupArea.menu = menu
|
|
|
|
popupArea.isHidden = menu.items.isEmpty
|
|
|
|
}
|
2021-01-14 22:50:52 +00:00
|
|
|
|
2021-07-25 08:38:09 +00:00
|
|
|
private func selectedCategory() -> PoolCategory? {
|
|
|
|
return popupCategory.selectedItem?.representedObject as? PoolCategory
|
|
|
|
}
|
|
|
|
|
|
|
|
private func selectedGroup() -> PoolGroup? {
|
|
|
|
return popupLocation.selectedItem?.representedObject as? PoolGroup
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:50:52 +00:00
|
|
|
private func selectedPool() -> Pool? {
|
|
|
|
guard popupArea.numberOfItems > 0 else {
|
|
|
|
guard let group = popupLocation.selectedItem?.representedObject as? PoolGroup else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return group.pools.first
|
|
|
|
}
|
|
|
|
return popupArea.itemArray.first?.representedObject as? Pool
|
|
|
|
}
|
2021-07-25 08:38:09 +00:00
|
|
|
|
|
|
|
private func updateFavoriteState() {
|
|
|
|
guard let category = selectedCategory(), let group = selectedGroup() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let groupId = group.uniqueId(in: category)
|
|
|
|
let isFavorite = profile?.favoriteGroupIds?.contains(groupId) ?? false
|
|
|
|
buttonFavorite.state = isFavorite ? .on : .off
|
|
|
|
}
|
2021-07-29 14:55:20 +00:00
|
|
|
|
|
|
|
private func delegateSelectedPool() {
|
|
|
|
if let pool = selectedPool() {
|
|
|
|
updateFavoriteState()
|
|
|
|
delegate?.providerView(self, didSelectPool: pool)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func filteredGroups(forCategory category: PoolCategory) -> [PoolGroup] {
|
|
|
|
return filteredGroupsByCategory[category.name] ?? []
|
|
|
|
}
|
|
|
|
|
|
|
|
private func setFilteredGroups(_ groups: [PoolGroup], forCategory category: PoolCategory) {
|
|
|
|
filteredGroupsByCategory[category.name] = category.groups.filter {
|
|
|
|
guard !onlyShowsFavorites else {
|
|
|
|
return profile?.favoriteGroupIds?.contains($0.uniqueId(in: category)) ?? false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}.sorted()
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|