Skip to content

Commit

Permalink
Merge pull request #224 from iosdeveloper/custom-url-scheme
Browse files Browse the repository at this point in the history
added custom url scheme
  • Loading branch information
matryer committed Feb 16, 2016
2 parents d3242c5 + 7d61deb commit 7b5bf88
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 1 deletion.
105 changes: 104 additions & 1 deletion App/BitBar/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
#import "NSUserDefaults+Settings.h"
#import "LaunchAtLoginController.h"
#import "PluginManager.h"
#import <sys/stat.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
@interface AppDelegate : NSObject <NSApplicationDelegate, NSURLDownloadDelegate>

@property (assign) IBOutlet NSWindow *window;

@property PluginManager *pluginManager;

// plugin download
@property NSURLDownload *download;
@property NSString *destinationPath;
@property NSString *suggestedDestinationPath;

@end

@implementation AppDelegate
Expand Down Expand Up @@ -54,13 +60,110 @@ - (void) applicationDidFinishLaunching:(NSNotification*)n {
// make a plugin manager
[_pluginManager = [PluginManager.alloc initWithPluginPath:DEFS.pluginsDirectory]
setupAllPlugins];

// register custom url scheme handler
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
andSelector:@selector(handleGetURLEvent:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
}

- (void) receiveWakeNote: (NSNotification*) note
{
[[self pluginManager] reset];
}

- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
// check if plugins directory is set
if (!DEFS.pluginsDirectory)
return;

// extract the url from the event and handle it

NSString *URLString = [event paramDescriptorForKeyword:keyDirectObject].stringValue;
NSString *prefix = @"bitbar://openPlugin?src=";

// skip urls that don't begin with our prefix
if (![URLString hasPrefix:prefix])
return;

URLString = [URLString substringFromIndex:prefix.length];

NSString *plugin = nil;

// if the plugin is at our repository, only display the filename
if ([URLString hasPrefix:@"https://github.com/matryer/bitbar-plugins/raw/master/"])
plugin = URLString.lastPathComponent;

NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert addButtonWithTitle:@"Cancel"];
alert.messageText = [NSString stringWithFormat:@"Download and install the plugin %@?", plugin ?: [NSString stringWithFormat:@"at %@", URLString]];
alert.informativeText = @"Only install plugins from trusted sources.";

if ([alert runModal] != NSAlertFirstButtonReturn) {
// cancel clicked
return;
}

[self.download cancel];
self.destinationPath = nil;
self.suggestedDestinationPath = nil;

// NSURLSession is not available below 10.9 :(
self.download = [[NSURLDownload alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URLString]] delegate:self];
}

#pragma mark - NSURLDownload delegate

- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename {
self.suggestedDestinationPath = [DEFS.pluginsDirectory stringByAppendingPathComponent:filename];
[download setDestination:self.suggestedDestinationPath allowOverwrite:NO];
}

- (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path {
self.destinationPath = path;
}

- (void)downloadDidFinish:(NSURLDownload *)download {
if (self.destinationPath) {
if (self.suggestedDestinationPath && ![self.suggestedDestinationPath isEqualToString:self.destinationPath]) {
// overwrite file at suggested destination path

[[NSFileManager defaultManager] removeItemAtPath:self.suggestedDestinationPath error:nil];

if ([[NSFileManager defaultManager] moveItemAtPath:self.destinationPath toPath:self.suggestedDestinationPath error:nil])
self.destinationPath = self.suggestedDestinationPath;
}

// ensure plugin is executable

// `chmod +x plugin.sh`
struct stat st;
stat(self.destinationPath.UTF8String, &st);
chmod(self.destinationPath.UTF8String, (st.st_mode & ALLPERMS) | S_IXUSR | S_IXGRP | S_IXOTH);
}

// refresh
[self.pluginManager reset];

self.download = nil;
self.destinationPath = nil;
self.suggestedDestinationPath = nil;
}

- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error {
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
alert.messageText = @"Download failed";
alert.informativeText = error.localizedDescription;
[alert runModal];

self.download = nil;
self.destinationPath = nil;
self.suggestedDestinationPath = nil;
}

@end

int main(int argc, const char * argv[]) { return NSApplicationMain(argc, argv); }
11 changes: 11 additions & 0 deletions App/BitBar/BitBar-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
<string>1.5-beta-2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>CFBundleURLSchemes</key>
<array>
<string>bitbar</string>
</array>
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.5.0</string>
<key>LSApplicationCategoryType</key>
Expand Down

0 comments on commit 7b5bf88

Please sign in to comment.