2018-06-30 13:10:06 +00:00
|
|
|
#ifndef __APPDELEGATE_H__
|
|
|
|
#define __APPDELEGATE_H__
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
|
|
|
|
#include "StatusItemButton.h"
|
|
|
|
#include "JavaHelper.h"
|
2018-07-13 06:30:16 +00:00
|
|
|
#include "RouterTask.h"
|
|
|
|
#include "neither/maybe.hpp"
|
|
|
|
#include "optional.hpp"
|
|
|
|
#include "subprocess.hpp"
|
|
|
|
#include <glob.h>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-07-13 09:11:46 +00:00
|
|
|
|
|
|
|
#define DEF_I2P_VERSION "0.9.35"
|
|
|
|
#define APPDOMAIN "net.i2p.launcher"
|
|
|
|
#define NSAPPDOMAIN @APPDOMAIN
|
|
|
|
#define CFAPPDOMAIN CFSTR(APPDOMAIN)
|
|
|
|
#define APP_IDSTR @"I2P Launcher"
|
|
|
|
|
|
|
|
|
2018-07-13 06:30:16 +00:00
|
|
|
using namespace neither;
|
2018-06-30 13:10:06 +00:00
|
|
|
|
2018-07-13 09:11:46 +00:00
|
|
|
using maybeAnRouterRunner = std::experimental::optional<RouterTask*>;
|
|
|
|
|
|
|
|
|
2018-06-30 13:10:06 +00:00
|
|
|
extern JvmListSharedPtr gRawJvmList;
|
|
|
|
|
2018-07-13 06:30:16 +00:00
|
|
|
// DO NOT ACCESS THIS GLOBAL VARIABLE DIRECTLY.
|
2018-07-13 09:11:46 +00:00
|
|
|
static std::mutex globalRouterStatusMutex;
|
|
|
|
static maybeAnRouterRunner globalRouterStatus = maybeAnRouterRunner{};
|
|
|
|
|
|
|
|
maybeAnRouterRunner getGlobalRouterObject();
|
|
|
|
void setGlobalRouterObject(RouterTask* newRouter);
|
|
|
|
|
|
|
|
@class ExtractMetaInfo;
|
|
|
|
@interface ExtractMetaInfo : NSObject
|
|
|
|
@property (strong) NSString* i2pBase;
|
|
|
|
@property (strong) NSString* javaBinary;
|
|
|
|
@property (strong) NSString* zipFile;
|
|
|
|
@property (strong) NSString* jarFile;
|
|
|
|
@end
|
2018-07-13 06:30:16 +00:00
|
|
|
|
2018-07-13 09:11:46 +00:00
|
|
|
inline void sendUserNotification(NSString* title, NSString* informativeText, NSImage* contentImage = NULL, bool makeSound = false) {
|
|
|
|
NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
|
2018-07-13 06:30:16 +00:00
|
|
|
|
2018-07-13 09:11:46 +00:00
|
|
|
userNotification.title = title;
|
|
|
|
userNotification.informativeText = informativeText;
|
|
|
|
if (contentImage != NULL) userNotification.contentImage = contentImage;
|
|
|
|
if (makeSound) userNotification.soundName = NSUserNotificationDefaultSoundName;
|
2018-07-13 06:30:16 +00:00
|
|
|
|
2018-07-13 09:11:46 +00:00
|
|
|
[[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:userNotification];
|
|
|
|
};
|
2018-07-13 06:30:16 +00:00
|
|
|
|
2018-07-13 09:11:46 +00:00
|
|
|
inline std::vector<std::string> globVector(const std::string& pattern){
|
2018-07-13 06:30:16 +00:00
|
|
|
glob_t glob_result;
|
|
|
|
glob(pattern.c_str(),GLOB_TILDE,NULL,&glob_result);
|
|
|
|
std::vector<std::string> files;
|
|
|
|
for(unsigned int i=0;i<glob_result.gl_pathc;++i){
|
|
|
|
files.push_back(std::string(glob_result.gl_pathv[i]));
|
|
|
|
}
|
|
|
|
globfree(&glob_result);
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2018-06-30 13:10:06 +00:00
|
|
|
@interface MenuBarCtrl : NSObject <StatusItemButtonDelegate, NSMenuDelegate>
|
|
|
|
@property BOOL enableLogging;
|
|
|
|
@property BOOL enableVerboseLogging;
|
|
|
|
@property (strong) NSMenu *menu;
|
|
|
|
@property (strong) StatusItemButton* statusBarButton;
|
|
|
|
@property (strong) NSUserDefaults *userPreferences;
|
|
|
|
@property (strong, nonatomic) NSImage * image;
|
|
|
|
@property (strong, nonatomic) NSStatusItem *statusItem;
|
|
|
|
// Event handlers
|
|
|
|
- (void) statusItemButtonLeftClick: (StatusItemButton *) button;
|
|
|
|
- (void) statusItemButtonRightClick: (StatusItemButton *) button;
|
|
|
|
- (void) statusBarImageBtnClicked;
|
|
|
|
- (void) btnPressedAction:(id)sender;
|
|
|
|
- (void) menuWillOpen:(NSMenu *)menu;
|
|
|
|
|
2018-07-13 09:11:46 +00:00
|
|
|
- (void) openRouterConsoleBtnHandler: (NSMenuItem *) menuItem;
|
2018-06-30 13:10:06 +00:00
|
|
|
- (void) startJavaRouterBtnHandler: (NSMenuItem *) menuItem;
|
|
|
|
- (void) restartJavaRouterBtnHandler: (NSMenuItem *) menuItem;
|
|
|
|
- (void) stopJavaRouterBtnHandler: (NSMenuItem *) menuItem;
|
|
|
|
- (void) quitWrapperBtnHandler: (NSMenuItem *) menuItem;
|
|
|
|
// Methods
|
|
|
|
- (MenuBarCtrl *) init;
|
|
|
|
- (void) dealloc;
|
|
|
|
- (NSMenu *) createStatusBarMenu;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@protocol MenuBarCtrlDelegate
|
|
|
|
- (void) menuBarCtrlStatusChanged: (BOOL) active;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface AppDelegate : NSObject <NSUserNotificationCenterDelegate, NSApplicationDelegate> {
|
|
|
|
@public
|
|
|
|
//NSImageView *imageCell;
|
|
|
|
}
|
|
|
|
@property (strong) MenuBarCtrl *menuBarCtrl;
|
|
|
|
@property (strong) NSUserDefaults *userPreferences;
|
|
|
|
@property BOOL enableLogging;
|
|
|
|
@property BOOL enableVerboseLogging;
|
2018-07-13 09:11:46 +00:00
|
|
|
@property (copy) NSImage *contentImage NS_AVAILABLE(10_9, NA);
|
|
|
|
- (void)extractI2PBaseDir:(ExtractMetaInfo *)metaInfo completion:(void(^)(BOOL success, NSError *error))completion;
|
|
|
|
- (void)startupI2PRouter:(ExtractMetaInfo *)metaInfo;
|
2018-06-30 13:10:06 +00:00
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
|
|
|
|
- (void)applicationWillTerminate:(NSNotification *)aNotification;
|
|
|
|
- (void)setApplicationDefaultPreferences;
|
|
|
|
- (void)userChooseJavaHome;
|
|
|
|
- (AppDelegate *)initWithArgc:(int)argc argv:(const char **)argv;
|
|
|
|
- (NSString *)userSelectJavaHome:(JvmListPtr)rawJvmList;
|
2018-07-13 06:30:16 +00:00
|
|
|
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
|
|
|
|
shouldPresentNotification:(NSUserNotification *)notification;
|
2018-06-30 13:10:06 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
|
|
@implementation CNSStatusBarCtrl
|
|
|
|
-(id)initWithSysTray:(I2PCtrlSysIcon *)sys
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
item = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
|
|
|
|
menu = 0;
|
|
|
|
systray = sys;
|
|
|
|
imageCell = [[NSImageView alloc] initWithParent:self];
|
|
|
|
[item setView: imageCell];
|
|
|
|
[item setHidden: NO];
|
|
|
|
CFShow(CFSTR("CNSStatusBarCtrl::initWithSysTray executed"));
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
-(NSStatusItem*)item {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
-(void)dealloc {
|
|
|
|
[[NSStatusBar systemStatusBar] removeStatusItem:item];
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:imageCell];
|
|
|
|
[imageCell release];
|
|
|
|
[item release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
class CSystemTrayIcon
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CSystemTrayIcon(I2PCtrlSysIcon *sys)
|
|
|
|
{
|
|
|
|
item = [[CNSStatusBarCtrl alloc] initWithSysTray:sys];
|
|
|
|
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:item];
|
|
|
|
const int menuHeight = [[NSStatusBar systemStatusBar] thickness];
|
|
|
|
printf("menuHeight: %d\n", menuHeight);
|
|
|
|
[[[item item] view] setHidden: NO];
|
|
|
|
}
|
|
|
|
~CSystemTrayIcon()
|
|
|
|
{
|
|
|
|
[[[item item] view] setHidden: YES];
|
|
|
|
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:nil];
|
|
|
|
[item release];
|
|
|
|
}
|
|
|
|
CNSStatusBarCtrl *item;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|