2021-01-01 10:13:41 +00:00
|
|
|
//
|
|
|
|
// AppDelegate.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/6/18.
|
|
|
|
// 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 Convenience
|
|
|
|
import TunnelKit
|
|
|
|
import ServiceManagement
|
|
|
|
|
|
|
|
// comment on release
|
|
|
|
//import AppCenter
|
|
|
|
//import AppCenterAnalytics
|
|
|
|
//import AppCenterCrashes
|
|
|
|
|
|
|
|
extension Notification.Name {
|
|
|
|
static let killLauncher = Notification.Name("killLauncher")
|
|
|
|
}
|
|
|
|
|
|
|
|
@NSApplicationMain
|
|
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
private let appCenterSecret = GroupConstants.App.config?["appcenter_secret"] as? String
|
|
|
|
|
|
|
|
private var importer: HostImporter?
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
AppConstants.Log.configure()
|
|
|
|
InfrastructureFactory.shared.preload()
|
|
|
|
VPN.shared = OpenVPNProvider(bundleIdentifier: AppConstants.App.tunnelBundleId)
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
|
|
Reviewer.shared.eventCountBeforeRating = AppConstants.Rating.eventCount
|
|
|
|
ProductManager.shared.listProducts(completionHandler: nil)
|
|
|
|
|
|
|
|
NSApp.mainMenu = loadMainMenu()
|
|
|
|
StatusMenu.shared.install()
|
2021-02-02 16:57:42 +00:00
|
|
|
ProductManager.shared.reviewPurchases()
|
2021-01-01 10:13:41 +00:00
|
|
|
|
|
|
|
// if let appCenterSecret = appCenterSecret, !appCenterSecret.isEmpty {
|
|
|
|
// AppCenter.start(withAppSecret: appCenterSecret, services: [Analytics.self, Crashes.self])
|
|
|
|
// }
|
|
|
|
|
|
|
|
// launcher configuration
|
|
|
|
|
|
|
|
let launcherAppId = AppConstants.App.appLauncherId
|
|
|
|
let runningApps = NSWorkspace.shared.runningApplications
|
|
|
|
let isRunning = !runningApps.filter { $0.bundleIdentifier == launcherAppId }.isEmpty
|
|
|
|
|
|
|
|
if isRunning {
|
|
|
|
DistributedNotificationCenter.default().post(name: .killLauncher, object: Bundle.main.bundleIdentifier!)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !TransientStore.didHandleSubreddit {
|
|
|
|
let alert = Macros.warning(L10n.Core.Reddit.title, L10n.Core.Reddit.message)
|
|
|
|
alert.present(in: nil, withOK: L10n.Core.Reddit.Buttons.subscribe, cancel: L10n.Core.Reddit.Buttons.never, dummy: L10n.Core.Reddit.Buttons.remind, handler: {
|
|
|
|
TransientStore.didHandleSubreddit = true
|
|
|
|
self.subscribeSubreddit()
|
|
|
|
}, cancelHandler: {
|
|
|
|
TransientStore.didHandleSubreddit = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
|
2021-01-26 19:39:37 +00:00
|
|
|
// TransientStore.shared.service.preferences.confirmsQuit = true
|
|
|
|
guard TransientStore.shared.service.preferences.confirmsQuit ?? true else {
|
|
|
|
return .terminateNow
|
|
|
|
}
|
2021-01-01 10:13:41 +00:00
|
|
|
let alert = Macros.warning(
|
|
|
|
L10n.App.Menu.Quit.title(GroupConstants.App.name),
|
|
|
|
L10n.App.Menu.Quit.Messages.confirm
|
|
|
|
)
|
2021-01-26 19:39:37 +00:00
|
|
|
switch alert.presentModallyEx(withOK: L10n.Core.Global.ok, other1: L10n.Core.Global.cancel, other2: L10n.Core.Reddit.Buttons.never) {
|
|
|
|
case .alertSecondButtonReturn:
|
2021-01-01 10:13:41 +00:00
|
|
|
return .terminateCancel
|
2021-01-26 19:39:37 +00:00
|
|
|
|
|
|
|
case .alertThirdButtonReturn:
|
|
|
|
TransientStore.shared.service.preferences.confirmsQuit = false
|
|
|
|
break
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
2021-01-01 10:13:41 +00:00
|
|
|
}
|
|
|
|
return .terminateNow
|
|
|
|
}
|
|
|
|
|
|
|
|
func applicationWillTerminate(_ aNotification: Notification) {
|
|
|
|
TransientStore.shared.serialize(withProfiles: true) // exit
|
|
|
|
}
|
|
|
|
|
|
|
|
func application(_ sender: NSApplication, openFile filename: String) -> Bool {
|
|
|
|
let url = URL(fileURLWithPath: filename)
|
|
|
|
importer = HostImporter(withConfigurationURL: url)
|
|
|
|
importer?.importHost(withPassphrase: nil)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Helpers
|
|
|
|
|
|
|
|
private func loadMainMenu() -> NSMenu? {
|
|
|
|
let nibName = "MainMenu"
|
|
|
|
guard let nib = NSNib(nibNamed: nibName, bundle: nil) else {
|
|
|
|
fatalError(nibName)
|
|
|
|
}
|
|
|
|
var objects: NSArray?
|
|
|
|
guard nib.instantiate(withOwner: nil, topLevelObjects: &objects) else {
|
|
|
|
fatalError(nibName)
|
|
|
|
}
|
|
|
|
guard let nonOptionalObjects = objects else {
|
|
|
|
fatalError(nibName)
|
|
|
|
}
|
|
|
|
for o in nonOptionalObjects {
|
|
|
|
if let menu = o as? NSMenu {
|
|
|
|
return menu
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
private func subscribeSubreddit() {
|
|
|
|
NSWorkspace.shared.open(AppConstants.URLs.subreddit)
|
|
|
|
}
|
|
|
|
}
|