source: launchers/macosx/obj-cpp/AppDelegate.h @ f9fbc76

Last change on this file since f9fbc76 was f9fbc76, checked in by meeh <meeh@…>, 2 years ago

Code changes:

  • Port check to see if i2p is already running.
  • All buttons in the menu should work now.
  • Rewrote some C++ to Objective-C, which fits better with the integration.
  • Misc cleanup.
  • Made the launch of router repeatable.
  • Property mode set to 100644
File size: 4.7 KB
Line 
1#ifndef __APPDELEGATE_H__
2#define __APPDELEGATE_H__
3
4#include <algorithm>
5#include <string>
6#include <memory>
7
8#include <Cocoa/Cocoa.h>
9
10#include "RouterTask.h"
11#include "StatusItemButton.h"
12#include "JavaHelper.h"
13#include "neither/maybe.hpp"
14#include "optional.hpp"
15#include "subprocess.hpp"
16#include <glob.h>
17#include <vector>
18
19
20#define DEF_I2P_VERSION "0.9.35"
21#define APPDOMAIN "net.i2p.launcher"
22#define NSAPPDOMAIN @APPDOMAIN
23#define CFAPPDOMAIN CFSTR(APPDOMAIN)
24#define APP_IDSTR @"I2P Launcher"
25
26
27using namespace neither;
28
29@class ExtractMetaInfo;
30using maybeAnRouterRunner = std::experimental::optional<I2PRouterTask*>;
31
32std::vector<std::string> buildClassPath(std::string basePath);
33
34extern JvmListSharedPtr gRawJvmList;
35
36// DO NOT ACCESS THIS GLOBAL VARIABLE DIRECTLY.
37static std::mutex globalRouterStatusMutex;
38static maybeAnRouterRunner globalRouterStatus = maybeAnRouterRunner{};
39static bool isRuterRunning = false;
40
41maybeAnRouterRunner getGlobalRouterObject();
42void setGlobalRouterObject(I2PRouterTask* newRouter);
43bool getGlobalRouterIsRunning();
44void setGlobalRouterIsRunning(bool running);
45
46@interface ExtractMetaInfo : NSObject
47@property (copy) NSString* i2pBase;
48@property (copy) NSString* javaBinary;
49@property (copy) NSString* zipFile;
50@property (copy) NSString* jarFile;
51@end
52
53@class I2PStatusMenu;
54@interface I2PStatusMenu : NSMenu
55- (BOOL)validateMenuItem:(NSMenuItem *)menuItem;
56@end
57
58inline void sendUserNotification(NSString* title, NSString* informativeText, NSImage* contentImage = NULL, bool makeSound = false) {
59  NSUserNotification *userNotification = [[[NSUserNotification alloc] init] autorelease];
60
61  userNotification.title = title;
62  userNotification.informativeText = informativeText;
63  if (contentImage != NULL) userNotification.contentImage = contentImage;
64  if (makeSound) userNotification.soundName = NSUserNotificationDefaultSoundName;
65
66  [[NSUserNotificationCenter defaultUserNotificationCenter] scheduleNotification:userNotification];
67};
68
69inline std::vector<std::string> globVector(const std::string& pattern){
70    glob_t glob_result;
71    glob(pattern.c_str(),GLOB_TILDE,NULL,&glob_result);
72    std::vector<std::string> files;
73    for(unsigned int i=0;i<glob_result.gl_pathc;++i){
74        files.push_back(std::string(glob_result.gl_pathv[i]));
75    }
76    globfree(&glob_result);
77    return files;
78}
79
80inline std::string getDefaultBaseDir()
81{
82  // Figure out base directory
83  const char* pathFromHome = "/Users/%s/Library/I2P";
84  auto username = getenv("USER");
85  char buffer[strlen(pathFromHome)+strlen(username)];
86  sprintf(buffer, pathFromHome, username);
87  std::string i2pBaseDir(buffer);
88  return i2pBaseDir;
89}
90
91@interface MenuBarCtrl : NSObject <StatusItemButtonDelegate, NSMenuDelegate>
92@property BOOL enableLogging;
93@property BOOL enableVerboseLogging;
94@property (strong) I2PStatusMenu *menu;
95@property (strong) StatusItemButton* statusBarButton;
96@property (strong) NSUserDefaults *userPreferences;
97@property (strong, nonatomic) NSImage * image;
98@property (strong, nonatomic) NSStatusItem *statusItem;
99// Event handlers
100- (void) statusItemButtonLeftClick: (StatusItemButton *) button;
101- (void) statusItemButtonRightClick: (StatusItemButton *) button;
102- (void) statusBarImageBtnClicked;
103- (void) btnPressedAction:(id)sender;
104- (void) menuWillOpen:(I2PStatusMenu *)menu;
105
106- (void) openRouterConsoleBtnHandler: (NSMenuItem *) menuItem;
107- (void) startJavaRouterBtnHandler: (NSMenuItem *) menuItem;
108- (void) restartJavaRouterBtnHandler: (NSMenuItem *) menuItem;
109- (void) stopJavaRouterBtnHandler: (NSMenuItem *) menuItem;
110- (void) quitWrapperBtnHandler: (NSMenuItem *) menuItem;
111// Methods
112- (MenuBarCtrl *) init;
113- (void) dealloc;
114- (I2PStatusMenu *) createStatusBarMenu;
115@end
116
117@protocol MenuBarCtrlDelegate
118- (void) menuBarCtrlStatusChanged: (BOOL) active;
119@end
120
121@interface AppDelegate : NSObject <NSUserNotificationCenterDelegate, NSApplicationDelegate> {
122@public
123  //NSImageView *imageCell;
124}
125@property (strong) MenuBarCtrl *menuBarCtrl;
126@property (strong) NSUserDefaults *userPreferences;
127@property BOOL enableLogging;
128@property BOOL enableVerboseLogging;
129@property ExtractMetaInfo *metaInfo;
130@property (copy) NSImage *contentImage NS_AVAILABLE(10_9, NA);
131- (void)extractI2PBaseDir:(void(^)(BOOL success, NSError *error))completion;
132- (void)startupI2PRouter;
133- (void)applicationDidFinishLaunching:(NSNotification *)aNotification;
134- (void)applicationWillTerminate:(NSNotification *)aNotification;
135- (void)setApplicationDefaultPreferences;
136- (void)userChooseJavaHome;
137- (AppDelegate *)initWithArgc:(int)argc argv:(const char **)argv;
138- (NSString *)userSelectJavaHome:(JvmListPtr)rawJvmList;
139- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center
140                               shouldPresentNotification:(NSUserNotification *)notification;
141@end
142
143
144#endif
Note: See TracBrowser for help on using the repository browser.