2019-05-24 11:29:27 -07:00
//
// A p p D e l e g a t e . s w i f t
// A l t S e r v e r
//
// C r e a t e d b y R i l e y T e s t u t o n 5 / 2 4 / 1 9 .
// C o p y r i g h t © 2 0 1 9 R i l e y T e s t u t . A l l r i g h t s r e s e r v e d .
//
import Cocoa
2019-07-01 15:19:22 -07:00
import UserNotifications
import AltSign
2019-05-24 11:29:27 -07:00
2019-09-04 11:58:28 -07:00
import LaunchAtLogin
2019-11-18 14:42:38 -08:00
2020-11-11 17:25:16 -08:00
#if STAGING
private let altstoreAppURL = URL ( string : " https://f000.backblazeb2.com/file/altstore-staging/altstore.ipa " ) !
2021-02-01 13:49:27 -06:00
#elseif BETA
private let altstoreAppURL = URL ( string : " https://f000.backblazeb2.com/file/altstore/altstore-beta.ipa " ) !
2020-11-11 17:25:16 -08:00
#else
private let altstoreAppURL = URL ( string : " https://f000.backblazeb2.com/file/altstore/altstore.ipa " ) !
#endif
2019-05-24 11:29:27 -07:00
@NSApplicationMain
class AppDelegate : NSObject , NSApplicationDelegate {
2020-10-06 18:11:03 -07:00
private let pluginManager = PluginManager ( )
2019-07-01 15:19:22 -07:00
private var statusItem : NSStatusItem ?
private var connectedDevices = [ ALTDevice ] ( )
private weak var authenticationAlert : NSAlert ?
@IBOutlet private var appMenu : NSMenu !
@IBOutlet private var connectedDevicesMenu : NSMenu !
2020-11-11 17:25:16 -08:00
@IBOutlet private var sideloadIPAConnectedDevicesMenu : NSMenu !
2019-09-04 11:58:28 -07:00
@IBOutlet private var launchAtLoginMenuItem : NSMenuItem !
2019-11-18 14:42:38 -08:00
@IBOutlet private var installMailPluginMenuItem : NSMenuItem !
2019-07-01 15:19:22 -07:00
private weak var authenticationAppleIDTextField : NSTextField ?
private weak var authenticationPasswordTextField : NSSecureTextField ?
2019-11-18 14:42:38 -08:00
2019-07-01 15:19:22 -07:00
func applicationDidFinishLaunching ( _ aNotification : Notification )
{
2019-08-01 10:45:54 -07:00
UserDefaults . standard . registerDefaults ( )
2019-07-01 15:19:22 -07:00
UNUserNotificationCenter . current ( ) . delegate = self
2020-01-13 10:17:30 -08:00
2020-08-31 13:58:44 -07:00
ServerConnectionManager . shared . start ( )
2020-01-13 10:17:30 -08:00
ALTDeviceManager . shared . start ( )
2019-07-01 15:19:22 -07:00
let item = NSStatusBar . system . statusItem ( withLength : - 1 )
2020-09-04 13:22:26 -07:00
item . menu = self . appMenu
item . button ? . image = NSImage ( named : " MenuBarIcon " )
2019-07-01 15:19:22 -07:00
self . statusItem = item
2020-09-04 13:22:26 -07:00
self . appMenu . delegate = self
2019-07-01 15:19:22 -07:00
self . connectedDevicesMenu . delegate = self
2020-11-11 17:25:16 -08:00
self . sideloadIPAConnectedDevicesMenu . delegate = self
2019-09-25 01:23:23 -07:00
2019-11-04 15:07:51 -08:00
UNUserNotificationCenter . current ( ) . requestAuthorization ( options : [ . alert ] ) { ( success , error ) in
guard success else { return }
2019-09-25 01:23:23 -07:00
2019-11-04 15:07:51 -08:00
if ! UserDefaults . standard . didPresentInitialNotification
{
let content = UNMutableNotificationContent ( )
content . title = NSLocalizedString ( " AltServer Running " , comment : " " )
content . body = NSLocalizedString ( " AltServer runs in the background as a menu bar app listening for AltStore. " , comment : " " )
let request = UNNotificationRequest ( identifier : UUID ( ) . uuidString , content : content , trigger : nil )
UNUserNotificationCenter . current ( ) . add ( request )
UserDefaults . standard . didPresentInitialNotification = true
}
2019-09-25 01:23:23 -07:00
}
2020-10-06 18:11:03 -07:00
if self . pluginManager . isUpdateAvailable
{
self . installMailPlugin ( )
}
2019-05-24 11:29:27 -07:00
}
2019-07-01 15:19:22 -07:00
func applicationWillTerminate ( _ aNotification : Notification )
{
2019-05-24 11:29:27 -07:00
// I n s e r t c o d e h e r e t o t e a r d o w n y o u r a p p l i c a t i o n
}
2019-07-01 15:19:22 -07:00
}
private extension AppDelegate
{
@objc func installAltStore ( _ item : NSMenuItem )
{
2020-11-11 17:25:16 -08:00
guard let index = item . menu ? . index ( of : item ) , index != - 1 else { return }
2019-07-01 15:19:22 -07:00
2020-11-11 17:25:16 -08:00
let device = self . connectedDevices [ index ]
self . installApplication ( at : altstoreAppURL , to : device )
}
@objc func sideloadIPA ( _ item : NSMenuItem )
{
guard let index = item . menu ? . index ( of : item ) , index != - 1 else { return }
let device = self . connectedDevices [ index ]
let openPanel = NSOpenPanel ( )
openPanel . canChooseDirectories = false
openPanel . allowsMultipleSelection = false
openPanel . allowedFileTypes = [ " ipa " ]
openPanel . begin { ( response ) in
guard let fileURL = openPanel . url , response = = . OK else { return }
self . installApplication ( at : fileURL , to : device )
}
}
func installApplication ( at url : URL , to device : ALTDevice )
{
2019-07-01 15:19:22 -07:00
let alert = NSAlert ( )
alert . messageText = NSLocalizedString ( " Please enter your Apple ID and password. " , comment : " " )
2019-11-18 14:17:57 -08:00
alert . informativeText = NSLocalizedString ( " Your Apple ID and password are not saved and are only sent to Apple for authentication. " , comment : " " )
2019-07-01 15:19:22 -07:00
let textFieldSize = NSSize ( width : 300 , height : 22 )
let appleIDTextField = NSTextField ( frame : NSRect ( x : 0 , y : 0 , width : textFieldSize . width , height : textFieldSize . height ) )
appleIDTextField . delegate = self
appleIDTextField . translatesAutoresizingMaskIntoConstraints = false
appleIDTextField . placeholderString = NSLocalizedString ( " Apple ID " , comment : " " )
alert . window . initialFirstResponder = appleIDTextField
self . authenticationAppleIDTextField = appleIDTextField
let passwordTextField = NSSecureTextField ( frame : NSRect ( x : 0 , y : 0 , width : textFieldSize . width , height : textFieldSize . height ) )
passwordTextField . delegate = self
passwordTextField . translatesAutoresizingMaskIntoConstraints = false
passwordTextField . placeholderString = NSLocalizedString ( " Password " , comment : " " )
self . authenticationPasswordTextField = passwordTextField
appleIDTextField . nextKeyView = passwordTextField
let stackView = NSStackView ( frame : NSRect ( x : 0 , y : 0 , width : textFieldSize . width , height : textFieldSize . height * 2 ) )
stackView . orientation = . vertical
stackView . distribution = . equalSpacing
stackView . spacing = 0
stackView . addArrangedSubview ( appleIDTextField )
stackView . addArrangedSubview ( passwordTextField )
alert . accessoryView = stackView
alert . addButton ( withTitle : NSLocalizedString ( " Install " , comment : " " ) )
alert . addButton ( withTitle : NSLocalizedString ( " Cancel " , comment : " " ) )
self . authenticationAlert = alert
self . validate ( )
NSRunningApplication . current . activate ( options : . activateIgnoringOtherApps )
let response = alert . runModal ( )
guard response = = . alertFirstButtonReturn else { return }
let username = appleIDTextField . stringValue
let password = passwordTextField . stringValue
2020-11-11 17:25:16 -08:00
2020-02-13 21:41:31 -08:00
func install ( )
2019-11-18 14:42:38 -08:00
{
2020-11-11 17:25:16 -08:00
ALTDeviceManager . shared . installApplication ( at : url , to : device , appleID : username , password : password ) { ( result ) in
2020-02-13 21:41:31 -08:00
switch result
2019-09-13 14:25:26 -07:00
{
2020-11-11 17:25:16 -08:00
case . success ( let application ) :
2020-02-13 21:41:31 -08:00
let content = UNMutableNotificationContent ( )
content . title = NSLocalizedString ( " Installation Succeeded " , comment : " " )
2020-11-11 17:25:16 -08:00
content . body = String ( format : NSLocalizedString ( " %@ was successfully installed on %@. " , comment : " " ) , application . name , device . name )
2020-02-13 21:41:31 -08:00
let request = UNNotificationRequest ( identifier : UUID ( ) . uuidString , content : content , trigger : nil )
UNUserNotificationCenter . current ( ) . add ( request )
case . failure ( InstallError . cancelled ) , . failure ( ALTAppleAPIError . requiresTwoFactorAuthentication ) :
// I g n o r e
break
case . failure ( let error as NSError ) :
let alert = NSAlert ( )
alert . alertStyle = . critical
alert . messageText = NSLocalizedString ( " Installation Failed " , comment : " " )
if let underlyingError = error . userInfo [ NSUnderlyingErrorKey ] as ? Error
{
alert . informativeText = underlyingError . localizedDescription
}
2020-05-21 21:00:05 -07:00
else if let recoverySuggestion = error . localizedRecoverySuggestion
{
alert . informativeText = error . localizedDescription + " \n \n " + recoverySuggestion
}
2020-02-13 21:41:31 -08:00
else
{
alert . informativeText = error . localizedDescription
}
NSRunningApplication . current . activate ( options : . activateIgnoringOtherApps )
alert . runModal ( )
2019-09-13 14:25:26 -07:00
}
2020-02-13 21:41:31 -08:00
}
}
2020-10-06 18:11:03 -07:00
if ! self . pluginManager . isMailPluginInstalled || self . pluginManager . isUpdateAvailable
2020-02-13 21:41:31 -08:00
{
2020-12-03 12:36:52 -06:00
AnisetteDataManager . shared . isXPCAvailable { ( isAvailable ) in
if isAvailable
{
// X P C s e r v i c e i s a v a i l a b l e , s o w e d o n ' t n e e d t o i n s t a l l / u p d a t e M a i l p l u g - i n .
// U s e r s c a n s t i l l m a n u a l l y d o s o f r o m t h e A l t S e r v e r m e n u .
install ( )
}
else
2020-10-06 18:11:03 -07:00
{
2020-12-03 12:36:52 -06:00
DispatchQueue . main . async {
self . installMailPlugin { ( result ) in
switch result
{
case . failure : break
case . success : install ( )
}
}
}
2019-09-13 14:25:26 -07:00
}
2019-09-04 12:08:27 -07:00
}
2019-07-01 15:19:22 -07:00
}
2020-02-13 21:41:31 -08:00
else
{
install ( )
}
2019-07-01 15:19:22 -07:00
}
2019-09-04 11:58:28 -07:00
@objc func toggleLaunchAtLogin ( _ item : NSMenuItem )
{
LaunchAtLogin . isEnabled . toggle ( )
}
2019-11-18 14:42:38 -08:00
@objc func handleInstallMailPluginMenuItem ( _ item : NSMenuItem )
{
2020-10-06 18:11:03 -07:00
if ! self . pluginManager . isMailPluginInstalled || self . pluginManager . isUpdateAvailable
2020-02-13 21:41:31 -08:00
{
2020-10-06 18:11:03 -07:00
self . installMailPlugin ( )
2020-02-13 21:41:31 -08:00
}
else
{
2020-10-06 18:11:03 -07:00
self . uninstallMailPlugin ( )
2020-02-13 21:41:31 -08:00
}
2019-11-18 14:42:38 -08:00
}
2020-10-06 18:11:03 -07:00
private func installMailPlugin ( completion : ( ( Result < Void , Error > ) -> Void ) ? = nil )
2019-11-18 14:42:38 -08:00
{
2020-10-06 18:11:03 -07:00
self . pluginManager . installMailPlugin { ( result ) in
DispatchQueue . main . async {
switch result
2020-02-13 21:41:31 -08:00
{
2020-10-06 18:11:03 -07:00
case . failure ( PluginError . cancelled ) : break
case . failure ( let error ) :
let alert = NSAlert ( )
alert . messageText = NSLocalizedString ( " Failed to Install Mail Plug-in " , comment : " " )
alert . informativeText = error . localizedDescription
alert . runModal ( )
2020-02-13 21:41:31 -08:00
2020-10-06 18:11:03 -07:00
case . success :
let alert = NSAlert ( )
alert . messageText = NSLocalizedString ( " Mail Plug-in Installed " , comment : " " )
alert . informativeText = NSLocalizedString ( " Please restart Mail and enable AltPlugin in Mail's Preferences. Mail must be running when installing or refreshing apps with AltServer. " , comment : " " )
alert . runModal ( )
2020-02-13 21:41:31 -08:00
}
2020-10-06 18:11:03 -07:00
completion ? ( result )
2020-02-13 21:41:31 -08:00
}
}
}
2020-10-06 18:11:03 -07:00
private func uninstallMailPlugin ( )
2020-02-13 21:41:31 -08:00
{
2020-10-06 18:11:03 -07:00
self . pluginManager . uninstallMailPlugin { ( result ) in
DispatchQueue . main . async {
switch result
2020-02-13 21:41:31 -08:00
{
2020-10-06 18:11:03 -07:00
case . failure ( PluginError . cancelled ) : break
case . failure ( let error ) :
let alert = NSAlert ( )
alert . messageText = NSLocalizedString ( " Failed to Uninstall Mail Plug-in " , comment : " " )
alert . informativeText = error . localizedDescription
alert . runModal ( )
case . success :
let alert = NSAlert ( )
alert . messageText = NSLocalizedString ( " Mail Plug-in Uninstalled " , comment : " " )
alert . informativeText = NSLocalizedString ( " Please restart Mail for changes to take effect. You will not be able to use AltServer until the plug-in is reinstalled. " , comment : " " )
alert . runModal ( )
2020-02-13 21:41:31 -08:00
}
}
}
}
}
2019-07-01 15:19:22 -07:00
extension AppDelegate : NSMenuDelegate
{
2020-09-04 13:22:26 -07:00
func menuWillOpen ( _ menu : NSMenu )
{
guard menu = = self . appMenu else { return }
2020-11-11 16:38:45 -08:00
self . connectedDevices = ALTDeviceManager . shared . availableDevices
2020-09-04 13:22:26 -07:00
self . launchAtLoginMenuItem . target = self
self . launchAtLoginMenuItem . action = #selector ( AppDelegate . toggleLaunchAtLogin ( _ : ) )
self . launchAtLoginMenuItem . state = LaunchAtLogin . isEnabled ? . on : . off
2020-10-06 18:11:03 -07:00
if self . pluginManager . isUpdateAvailable
{
self . installMailPluginMenuItem . title = NSLocalizedString ( " Update Mail Plug-in " , comment : " " )
}
else if self . pluginManager . isMailPluginInstalled
2020-09-04 13:22:26 -07:00
{
self . installMailPluginMenuItem . title = NSLocalizedString ( " Uninstall Mail Plug-in " , comment : " " )
}
else
{
self . installMailPluginMenuItem . title = NSLocalizedString ( " Install Mail Plug-in " , comment : " " )
}
self . installMailPluginMenuItem . target = self
self . installMailPluginMenuItem . action = #selector ( AppDelegate . handleInstallMailPluginMenuItem ( _ : ) )
}
2019-07-01 15:19:22 -07:00
func numberOfItems ( in menu : NSMenu ) -> Int
{
2020-11-11 17:25:16 -08:00
guard menu = = self . connectedDevicesMenu || menu = = self . sideloadIPAConnectedDevicesMenu else { return - 1 }
2020-09-04 13:22:26 -07:00
2019-07-01 15:19:22 -07:00
return self . connectedDevices . isEmpty ? 1 : self . connectedDevices . count
}
func menu ( _ menu : NSMenu , update item : NSMenuItem , at index : Int , shouldCancel : Bool ) -> Bool
{
2020-11-11 17:25:16 -08:00
guard menu = = self . connectedDevicesMenu || menu = = self . sideloadIPAConnectedDevicesMenu else { return false }
2020-09-04 13:22:26 -07:00
2019-07-01 15:19:22 -07:00
if self . connectedDevices . isEmpty
{
item . title = NSLocalizedString ( " No Connected Devices " , comment : " " )
item . isEnabled = false
item . target = nil
item . action = nil
}
else
{
let device = self . connectedDevices [ index ]
item . title = device . name
item . isEnabled = true
item . target = self
2020-11-11 17:25:16 -08:00
item . action = ( menu = = self . connectedDevicesMenu ) ? #selector ( AppDelegate . installAltStore ( _ : ) ) : #selector ( AppDelegate . sideloadIPA ( _ : ) )
2019-07-01 15:19:22 -07:00
item . tag = index
}
return true
}
}
2019-05-24 11:29:27 -07:00
2019-07-01 15:19:22 -07:00
extension AppDelegate : NSTextFieldDelegate
{
func controlTextDidChange ( _ obj : Notification )
{
self . validate ( )
}
func controlTextDidEndEditing ( _ obj : Notification )
{
self . validate ( )
}
private func validate ( )
{
guard
let appleID = self . authenticationAppleIDTextField ? . stringValue . trimmingCharacters ( in : . whitespacesAndNewlines ) ,
let password = self . authenticationPasswordTextField ? . stringValue . trimmingCharacters ( in : . whitespacesAndNewlines )
else { return }
if appleID . isEmpty || password . isEmpty
{
self . authenticationAlert ? . buttons . first ? . isEnabled = false
}
else
{
self . authenticationAlert ? . buttons . first ? . isEnabled = true
}
self . authenticationAlert ? . layout ( )
}
2019-05-24 11:29:27 -07:00
}
2019-07-01 15:19:22 -07:00
extension AppDelegate : UNUserNotificationCenterDelegate
{
func userNotificationCenter ( _ center : UNUserNotificationCenter , willPresent notification : UNNotification , withCompletionHandler completionHandler : @ escaping ( UNNotificationPresentationOptions ) -> Void )
{
completionHandler ( [ . alert , . sound , . badge ] )
}
}