Make me default RSS reader (sandbox compatible)

This commit is contained in:
relikd
2019-08-19 23:30:56 +02:00
parent a777b5672f
commit b961a3a56c
9 changed files with 62 additions and 104 deletions

View File

@@ -24,5 +24,5 @@
@interface SettingsGeneral : NSViewController
- (void)changeHttpApplication:(NSPopUpButton *)sender;
- (void)changeDefaultRSSReader:(NSPopUpButton *)sender;
- (void)clickHowToDefaults:(NSButton *)sender;
@end

View File

@@ -36,102 +36,51 @@
- (void)loadView {
self.view = [[SettingsGeneralView alloc] initWithController:self];
// Default http application for opening the feed urls
[self generateMenuForPopup:self.view.popupHttpApplication withScheme:@"https"];
[self.view.popupHttpApplication insertItemWithTitle:NSLocalizedString(@"System Default", @"Default web browser application") atIndex:0];
[self selectBundleID:[UserPrefs getHttpApplication] inPopup:self.view.popupHttpApplication];
NSPopUpButton *pop = self.view.popupHttpApplication;
[pop removeAllItems];
[pop addItemWithTitle:NSLocalizedString(@"System Default", @"Default web browser application")];
NSArray<NSString*> *browsers = CFBridgingRelease(LSCopyAllHandlersForURLScheme(CFSTR("https")));
for (NSString *bundleID in browsers) {
[pop addItemWithTitle: [self applicationNameForBundleId:bundleID]];
pop.lastItem.representedObject = bundleID;
}
[pop selectItemAtIndex:[pop indexOfItemWithRepresentedObject:[UserPrefs getHttpApplication]]];
// Default RSS Reader application
[self generateMenuForPopup:self.view.popupDefaultRSSReader withScheme:@"feed"];
[self selectBundleID:[self defaultBundleIdForScheme:@"feed"] inPopup:self.view.popupDefaultRSSReader];
NSString *feedBundleId = CFBridgingRelease(LSCopyDefaultHandlerForURLScheme(CFSTR("feed")));
self.view.defaultReader.objectValue = [self applicationNameForBundleId:feedBundleId];
}
#pragma mark - UI interaction with IBAction
/// Get human readable application name such as 'Safari' or 'baRSS'
- (nonnull NSString*)applicationNameForBundleId:(nonnull NSString*)bundleID {
NSString *name;
NSArray<NSURL*> *urls = CFBridgingRelease(LSCopyApplicationURLsForBundleIdentifier((__bridge CFStringRef)bundleID, NULL));
if (urls.count > 0) {
NSDictionary *info = CFBridgingRelease(CFBundleCopyInfoDictionaryForURL((CFURLRef)urls.firstObject));
name = info[(NSString*)kCFBundleExecutableKey];
}
return name ? name : bundleID;
}
#pragma mark - User interaction
// Callback method fired when user selects a different item from popup list
- (void)changeHttpApplication:(NSPopUpButton *)sender {
[UserPrefs setHttpApplication:sender.selectedItem.representedObject];
}
- (void)changeDefaultRSSReader:(NSPopUpButton *)sender {
if ([self setDefaultRSSApplication:sender.selectedItem.representedObject] == NO) {
// in case anything went wrong, restore previous selection
[self selectBundleID:[self defaultBundleIdForScheme:@"feed"] inPopup:sender];
}
// Callback method from round help button right of default feed reader text
- (void)clickHowToDefaults:(NSButton *)sender {
NSAlert *alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleInformational;
alert.messageText = NSLocalizedString(@"How to change default feed reader", nil);
alert.informativeText = NSLocalizedString(@"Unfortunately sandboxed applications are not allowed to change the default application. However, there is an auxiliary application.\n\nFollow the instructions to change the 'feed:' scheme.", nil);
[alert addButtonWithTitle:NSLocalizedString(@"Close", nil)];
[alert addButtonWithTitle:NSLocalizedString(@"Go to download page", nil)].toolTip = auxiliaryAppURL;
[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSModalResponse returnCode) {
if (returnCode == NSAlertSecondButtonReturn) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:auxiliaryAppURL]];
}
}];
}
#pragma mark - Helper methods
/**
Populate @c NSPopUpButton menu with all available application for that scheme.
@param scheme URL scheme like @c 'feed' or @c 'https'
*/
- (void)generateMenuForPopup:(NSPopUpButton*)popup withScheme:(NSString*)scheme {
[popup removeAllItems];
NSArray<NSString*> *apps = [self listOfBundleIdsForScheme:scheme];
for (NSString *bundleID in apps) {
NSString *appName = [self applicationNameForBundleId:bundleID];
if (!appName)
appName = bundleID;
[popup addItemWithTitle:appName];
popup.lastItem.representedObject = bundleID;
}
}
/**
For a given @c NSPopUpButton select the item which represents the @c bundleID.
*/
- (void)selectBundleID:(NSString*)bundleID inPopup:(NSPopUpButton*)popup {
[popup selectItemAtIndex:[popup indexOfItemWithRepresentedObject:bundleID]];
}
/**
Get human readable, application name from @c bundleID.
@param bundleID as defined in @c Info.plist
@return Application name such as 'Safari' or 'baRSS'
*/
- (NSString*)applicationNameForBundleId:(NSString*)bundleID {
NSArray<NSURL*> *urls = CFBridgingRelease(LSCopyApplicationURLsForBundleIdentifier((__bridge CFStringRef)bundleID, NULL));
if (urls.count > 0) {
NSDictionary *info = CFBridgingRelease(CFBundleCopyInfoDictionaryForURL((CFURLRef)urls.firstObject));
return info[(NSString*)kCFBundleExecutableKey];
}
return nil;
}
/**
Get a list of all installed applications supporting that URL scheme.
@param scheme URL scheme like @c 'feed' or @c 'https'
@return Array of @c bundleIDs of installed applications supporting that url scheme.
*/
- (NSArray<NSString*>*)listOfBundleIdsForScheme:(NSString*)scheme {
return CFBridgingRelease(LSCopyAllHandlersForURLScheme((__bridge CFStringRef _Nonnull)(scheme)));
}
/**
Get current default application for provided URL scheme. (e.g., )
@param scheme URL scheme like @c 'feed' or @c 'https'
@return @c bundleID of default application
*/
- (NSString*)defaultBundleIdForScheme:(NSString*)scheme {
return CFBridgingRelease(LSCopyDefaultHandlerForURLScheme((__bridge CFStringRef _Nonnull)(scheme)));
}
/**
Sets the default application for @c feed:// urls. (system wide)
@param bundleID as defined in @c Info.plist
@return Return @c YES if operation was successfull. @c NO otherwise.
*/
- (BOOL)setDefaultRSSApplication:(NSString*)bundleID {
// TODO: Does not work with sandboxing.
OSStatus s = LSSetDefaultHandlerForURLScheme(CFSTR("feed"), (__bridge CFStringRef _Nonnull)(bundleID));
return s == 0;
}
// Rebuild Launch Services cache
// https://eclecticlight.co/2017/08/11/launch-services-database-problems-correcting-and-rebuilding/
// /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -v -apps u
@end

View File

@@ -25,7 +25,7 @@
@interface SettingsGeneralView : NSView
@property (weak) IBOutlet NSPopUpButton* popupHttpApplication;
@property (weak) IBOutlet NSPopUpButton* popupDefaultRSSReader;
@property (weak) IBOutlet NSTextField *defaultReader;
- (instancetype)initWithController:(SettingsGeneral*)controller NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithFrame:(NSRect)frameRect NS_UNAVAILABLE;

View File

@@ -28,20 +28,16 @@
- (instancetype)initWithController:(SettingsGeneral*)controller {
self = [super initWithFrame:NSZeroRect];
NSArray *lbls = @[NSLocalizedString(@"Open URLs with:", nil),
NSLocalizedString(@"Default RSS Reader:", nil)];
NSView *labels = [[NSView labelColumn:lbls rowHeight:HEIGHT_POPUP padding:PAD_M] placeIn:self x:PAD_WIN yTop:PAD_WIN];
CGFloat x = NSMaxX(labels.frame) + PAD_S;
self.popupHttpApplication = [[self createPopup:x top: PAD_WIN + 1] action:@selector(changeHttpApplication:) target:controller];
self.popupDefaultRSSReader = [[self createPopup:x top: YFromTop(self.popupHttpApplication) + PAD_M] action:@selector(changeDefaultRSSReader:) target:controller];
// Change default feed reader application
NSTextField *l1 = [[NSView label:NSLocalizedString(@"Default feed reader:", nil)] placeIn:self x:PAD_WIN yTop:PAD_WIN + 3];
NSButton *help = [[[NSView helpButton] action:@selector(clickHowToDefaults:) target:controller] placeIn:self xRight:PAD_WIN yTop:PAD_WIN];
self.defaultReader = [[[[NSView label:@""] bold] placeIn:self x:NSMaxX(l1.frame) + PAD_S yTop:PAD_WIN + 3] sizeToRight:NSWidth(help.frame) + PAD_WIN];
// Popup button 'Open URLs with:'
CGFloat y = YFromTop(help) + PAD_M;
NSTextField *l2 = [[NSView label:NSLocalizedString(@"Open URLs with:", nil)] placeIn:self x:PAD_WIN yTop:y + 1];
self.popupHttpApplication = [[[[NSView popupButton:0] placeIn:self x:NSMaxX(l2.frame) + PAD_S yTop:y] sizeToRight:PAD_WIN]
action:@selector(changeHttpApplication:) target:controller];
return self;
}
/// Helper method to create sizable popup button
- (NSPopUpButton*)createPopup:(CGFloat)x top:(CGFloat)y {
return [[[NSView popupButton:0] placeIn:self x:x yTop:y] sizeToRight:PAD_WIN];
}
@end