|
10 | 10 |
|
11 | 11 | #import <asl.h>
|
12 | 12 |
|
| 13 | +@interface HITSimplePlugin () |
| 14 | +@property NSString *script; |
| 15 | +@property BOOL scriptChecked; |
| 16 | +- (void)updateWithComputedTitle:(NSMenuItem *)menuItem; |
| 17 | +@end |
| 18 | + |
| 19 | +#define kHITPCustomScriptsPath @"/Library/Application Support/com.github.ygini.hello-it/CustomScripts" |
| 20 | +#define kHITPDenyUserWritableScript @"denyUserWritableScript" |
| 21 | + |
13 | 22 | @implementation HITSimplePlugin
|
14 | 23 |
|
15 |
| --(NSMenuItem *)prepareNewMenuItem { |
16 |
| - NSString *title = [self localizedString:[self.settings objectForKey:kHITSimplePluginTitleKey]]; |
17 |
| - if (!title) { |
18 |
| - title = @""; |
| 24 | + |
| 25 | +- (instancetype)initWithSettings:(NSDictionary*)settings |
| 26 | +{ |
| 27 | + self = [super initWithSettings:settings]; |
| 28 | + if (self) { |
| 29 | + |
| 30 | + if ([[settings objectForKey:kHITSimplePluginComputedTitleKey] length] > 0) { |
| 31 | + _script = [[NSString stringWithFormat:kHITPCustomScriptsPath] stringByAppendingPathComponent:[settings objectForKey:kHITSimplePluginComputedTitleKey]]; |
| 32 | + |
| 33 | + asl_log(NULL, NULL, ASL_LEVEL_INFO, "Loading script based plugin with script at path %s", [_script cStringUsingEncoding:NSUTF8StringEncoding]); |
| 34 | + |
| 35 | + if ([[NSFileManager defaultManager] fileExistsAtPath:_script]) { |
| 36 | + if ([[NSFileManager defaultManager] isWritableFileAtPath:_script] && [[NSUserDefaults standardUserDefaults] boolForKey:kHITPDenyUserWritableScript]) { |
| 37 | +#ifdef DEBUG |
| 38 | + _scriptChecked = YES; |
| 39 | +#else |
| 40 | + _scriptChecked = NO; |
| 41 | +#endif |
| 42 | + asl_log(NULL, NULL, ASL_LEVEL_ERR, "Target script is writable, security restriction deny such a scenario %s", [_script cStringUsingEncoding:NSUTF8StringEncoding]); |
| 43 | + } else { |
| 44 | + _scriptChecked = YES; |
| 45 | + } |
| 46 | + } else { |
| 47 | + _scriptChecked = NO; |
| 48 | + asl_log(NULL, NULL, ASL_LEVEL_ERR, "Target script not accessible %s", [_script cStringUsingEncoding:NSUTF8StringEncoding]); |
| 49 | + } |
| 50 | + } |
| 51 | + |
19 | 52 | }
|
20 |
| - |
21 |
| - NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:title |
22 |
| - action:@selector(mainAction:) |
23 |
| - keyEquivalent:@""]; |
24 |
| - menuItem.target = self; |
25 |
| - |
| 53 | + return self; |
| 54 | +} |
| 55 | + |
| 56 | +-(NSImage *)imageForMenuItem { |
26 | 57 | NSString *imagePath = [self.settings objectForKey:kHITSimplePluginImagePathKey];
|
27 | 58 | NSString *imageBaseName = [self.settings objectForKey:kHITSimplePluginImageBaseNameKey];
|
28 | 59 |
|
@@ -53,18 +84,81 @@ -(NSMenuItem *)prepareNewMenuItem {
|
53 | 84 | }
|
54 | 85 |
|
55 | 86 | if (imagePath) {
|
56 |
| - asl_log(NULL, NULL, ASL_LEVEL_INFO, "Menu item with title %s set with image at path %s.", [title cStringUsingEncoding:NSUTF8StringEncoding], [imagePath cStringUsingEncoding:NSUTF8StringEncoding]); |
| 87 | + asl_log(NULL, NULL, ASL_LEVEL_INFO, "Menu item set with image at path %s.", [imagePath cStringUsingEncoding:NSUTF8StringEncoding]); |
57 | 88 | NSImage *accessoryImage = [[NSImage alloc] initWithContentsOfFile:imagePath];
|
58 | 89 |
|
59 |
| - if (accessoryImage) { |
60 |
| - menuItem.image = accessoryImage; |
61 |
| - } else { |
| 90 | + if (!accessoryImage) { |
62 | 91 | asl_log(NULL, NULL, ASL_LEVEL_ERR, "Impossible to load image at path %s.", [imagePath cStringUsingEncoding:NSUTF8StringEncoding]);
|
63 | 92 | }
|
| 93 | + |
| 94 | + return accessoryImage; |
64 | 95 | }
|
| 96 | + |
| 97 | + return nil; |
| 98 | +} |
| 99 | + |
| 100 | +-(NSMenuItem *)prepareNewMenuItem { |
| 101 | + NSString *title = [self localizedString:[self.settings objectForKey:kHITSimplePluginTitleKey]]; |
| 102 | + if (!title) { |
| 103 | + title = @""; |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:title |
| 108 | + action:@selector(mainAction:) |
| 109 | + keyEquivalent:@""]; |
65 | 110 |
|
| 111 | + [self updateWithComputedTitle:menuItem]; |
| 112 | + |
| 113 | + menuItem.target = self; |
| 114 | + |
| 115 | + menuItem.image = [self imageForMenuItem]; |
66 | 116 |
|
67 | 117 | return menuItem;
|
68 | 118 | }
|
69 | 119 |
|
| 120 | +- (void)updateWithComputedTitle:(NSMenuItem *)menuItem { |
| 121 | + if (self.scriptChecked && self.allowedToRun) { |
| 122 | + asl_log(NULL, NULL, ASL_LEVEL_INFO, "Get computed title with script %s", [self.script cStringUsingEncoding:NSUTF8StringEncoding]); |
| 123 | + |
| 124 | + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ |
| 125 | + NSTask *task = [[NSTask alloc] init]; |
| 126 | + [task setLaunchPath:self.script]; |
| 127 | + |
| 128 | + [task setStandardOutput:[NSPipe pipe]]; |
| 129 | + NSFileHandle *fileToRead = [[task standardOutput] fileHandleForReading]; |
| 130 | + |
| 131 | + dispatch_io_t stdoutChannel = dispatch_io_create(DISPATCH_IO_STREAM, [fileToRead fileDescriptor], dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(int error) { |
| 132 | + |
| 133 | + }); |
| 134 | + |
| 135 | + __block BOOL firstLine = YES; |
| 136 | + dispatch_io_read(stdoutChannel, 0, SIZE_MAX, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(bool done, dispatch_data_t data, int error) { |
| 137 | + if (firstLine) { |
| 138 | + firstLine = NO; |
| 139 | + } else { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + NSData *stdoutData = (NSData *)data; |
| 144 | + |
| 145 | + NSString *stdoutString = [[NSString alloc] initWithData:stdoutData encoding:NSUTF8StringEncoding]; |
| 146 | + |
| 147 | + NSArray *stdoutLines = [stdoutString componentsSeparatedByString:@"\n"]; |
| 148 | + menuItem.title = [stdoutLines firstObject]; |
| 149 | + }); |
| 150 | + |
| 151 | + @try { |
| 152 | + [task launch]; |
| 153 | + |
| 154 | + [task waitUntilExit]; |
| 155 | + |
| 156 | + asl_log(NULL, NULL, ASL_LEVEL_INFO, "Script exited with code %i", [task terminationStatus]); |
| 157 | + } @catch (NSException *exception) { |
| 158 | + asl_log(NULL, NULL, ASL_LEVEL_ERR, "Script failed to run: %s", [[exception reason] UTF8String]); |
| 159 | + } |
| 160 | + }]; |
| 161 | + } |
| 162 | +} |
| 163 | + |
70 | 164 | @end
|
0 commit comments