pEp macOS Desktop adapter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. //
  2. // main.m
  3. // pEpMacOSAdapter
  4. //
  5. // Created by Volker Birk on 20.04.20.
  6. // Copyleft © 2020 pp foundation.
  7. // This file is under GNU General Public License 3.0
  8. //
  9. #include <signal.h>
  10. #import <Foundation/Foundation.h>
  11. #import <AppKit/NSWorkspace.h>
  12. #import "pEpMacOSAdapter.h"
  13. #import "pEpUpdater.h"
  14. pEpUpdater* updater = nil;
  15. @interface ServiceDelegate : NSObject <NSXPCListenerDelegate>
  16. @end
  17. @implementation ServiceDelegate
  18. - (id)init{
  19. self = [super init];
  20. return self;
  21. }
  22. - (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
  23. // This method is where the NSXPCListener configures, accepts, and resumes a new incoming NSXPCConnection.
  24. NSLog(@"incoming connection");
  25. // Configure the connection.
  26. // First, set the interface that the exported object implements.
  27. newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(pEpMacOSAdapterProtocol)];
  28. // Next, set the object that the connection exports. All messages sent on
  29. // the connection to this service will be sent to the exported object to
  30. // handle. The connection retains the exported object.
  31. pEpMacOSAdapter *exportedObject = [pEpMacOSAdapter new];
  32. newConnection.exportedObject = exportedObject;
  33. if (!newConnection.exportedInterface || !newConnection.exportedObject) {
  34. NSLog(@"failed to allocate object and interface");
  35. return NO;
  36. }
  37. // Resuming the connection allows the system to deliver more incoming messages.
  38. [newConnection resume];
  39. // Returning YES from this method tells the system that you have accepted this connection. If you want to reject the connection for some reason, call -invalidate on the connection and return NO.
  40. NSLog(@"connection accepted");
  41. return YES;
  42. }
  43. @end
  44. BOOL start_helper(void)
  45. {
  46. NSURL *url = [NSURL
  47. fileURLWithPath:@"/Library/Application Support/pEp/pEp.app/Contents/Library/LoginItems/pp updates.app"];
  48. return [[NSWorkspace sharedWorkspace] openURL:url];
  49. }
  50. void signal_TERM(int signal)
  51. {
  52. NSLog(@"stopping agent");
  53. exit(0);
  54. }
  55. int main(int argc, const char *argv[])
  56. {
  57. signal(SIGTERM, signal_TERM);
  58. updater = [pEpUpdater new];
  59. // Create the delegate for the service.
  60. ServiceDelegate *delegate = [ServiceDelegate new];
  61. // Set up the one NSXPCListener for this service. It will handle all incoming connections.
  62. NSXPCListener *listener = [[NSXPCListener alloc] initWithMachServiceName:@"foundation.pEp.adapter.macOS"];
  63. NSLog(@"starting agent");
  64. listener.delegate = delegate;
  65. [listener resume];
  66. // start the GUI helper app
  67. BOOL opened = start_helper();
  68. assert(opened);
  69. NSLog(@"going into main runLoop");
  70. NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
  71. [runLoop run];
  72. return EXIT_FAILURE;
  73. }