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

@@ -19,6 +19,7 @@ and this project does adhere to [Semantic Versioning](https://semver.org/spec/v2
- *Settings, Feeds:* Drag & Drop feeds from / to OPML file - *Settings, Feeds:* Drag & Drop feeds from / to OPML file
- *Settings, Feeds:* Drag & Drop feed titles and urls as text - *Settings, Feeds:* Drag & Drop feed titles and urls as text
- *Settings, Feeds:* OPML export with selected items only - *Settings, Feeds:* OPML export with selected items only
- *Settings, General*: [Auxiliary application](https://github.com/relikd/URL-Scheme-Defaults) for changing default feed reader
- *UI:* Accessibility hints for most UI elements - *UI:* Accessibility hints for most UI elements
- *UI*: Show welcome message upon first usage (empty db) - *UI*: Show welcome message upon first usage (empty db)
- Welcome message also adds Github releases feed - Welcome message also adds Github releases feed
@@ -46,6 +47,7 @@ and this project does adhere to [Semantic Versioning](https://semver.org/spec/v2
- *Settings, Feeds:* Single add button for feeds, groups, and separators - *Settings, Feeds:* Single add button for feeds, groups, and separators
- *Settings, Feeds:* Always append new items at the end - *Settings, Feeds:* Always append new items at the end
- *Settings, General*: Moved `Fix cache` button to `About` text section - *Settings, General*: Moved `Fix cache` button to `About` text section
- *Settings, General*: Changing default feed reader is prohibited within sandbox
- *Status Bar Menu*: Show `(no title)` instead of `(error)` - *Status Bar Menu*: Show `(no title)` instead of `(error)`
- *Status Bar Menu*: `Update all feeds` will show error alerts for broken URLs - *Status Bar Menu*: `Update all feeds` will show error alerts for broken URLs
- *UI:* Interface builder files replaced with code equivalent - *UI:* Interface builder files replaced with code equivalent

View File

@@ -34,6 +34,8 @@
static NSPasteboardType const UTI_OPML = @"org.opml"; static NSPasteboardType const UTI_OPML = @"org.opml";
/// URL with newest baRSS releases. Automatically added when user starts baRSS for the first time. /// URL with newest baRSS releases. Automatically added when user starts baRSS for the first time.
static NSString* const versionUpdateURL = @"https://github.com/relikd/baRSS/releases.atom"; static NSString* const versionUpdateURL = @"https://github.com/relikd/baRSS/releases.atom";
/// URL to help page of auxiliary application "URL Scheme Defaults"
static NSString* const auxiliaryAppURL = @"https://github.com/relikd/URL-Scheme-Defaults#url-scheme-defaults";
#pragma mark - NSImageName constants #pragma mark - NSImageName constants

View File

@@ -61,6 +61,7 @@ NS_INLINE CGFloat NSMaxWidth(NSView *a, NSView *b) { return Max(NSWidth(a.frame)
+ (NSButton*)button:(NSString*)text; + (NSButton*)button:(NSString*)text;
+ (NSButton*)buttonImageSquare:(nonnull NSImageName)name; + (NSButton*)buttonImageSquare:(nonnull NSImageName)name;
+ (NSButton*)buttonIcon:(nonnull NSImageName)name size:(CGFloat)size; + (NSButton*)buttonIcon:(nonnull NSImageName)name size:(CGFloat)size;
+ (NSButton*)helpButton;
+ (NSButton*)inlineButton:(NSString*)text; + (NSButton*)inlineButton:(NSString*)text;
+ (NSPopUpButton*)popupButton:(CGFloat)w; + (NSPopUpButton*)popupButton:(CGFloat)w;
// UI: Others // UI: Others

View File

@@ -94,6 +94,14 @@
return btn; return btn;
} }
/// Create round button with question mark. @c 21x21px
+ (NSButton*)helpButton {
NSButton *btn = [[NSButton alloc] initWithFrame: NSMakeRect(0, 0, 21, 21)];
btn.bezelStyle = NSBezelStyleHelpButton;
btn.title = @"";
return btn;
}
/// Create gray inline button with rounded corners. @c 16px height. /// Create gray inline button with rounded corners. @c 16px height.
+ (NSButton*)inlineButton:(NSString*)text { + (NSButton*)inlineButton:(NSString*)text {
NSButton *btn = [[NSButton alloc] initWithFrame: NSMakeRect(0, 0, 0, HEIGHT_INLINEBUTTON)]; NSButton *btn = [[NSButton alloc] initWithFrame: NSMakeRect(0, 0, 0, HEIGHT_INLINEBUTTON)];

View File

@@ -70,7 +70,7 @@
</dict> </dict>
</array> </array>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>11159</string> <string>11387</string>
<key>LSApplicationCategoryType</key> <key>LSApplicationCategoryType</key>
<string>public.app-category.news</string> <string>public.app-category.news</string>
<key>LSMinimumSystemVersion</key> <key>LSMinimumSystemVersion</key>

View File

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

View File

@@ -36,102 +36,51 @@
- (void)loadView { - (void)loadView {
self.view = [[SettingsGeneralView alloc] initWithController:self]; self.view = [[SettingsGeneralView alloc] initWithController:self];
// Default http application for opening the feed urls // Default http application for opening the feed urls
[self generateMenuForPopup:self.view.popupHttpApplication withScheme:@"https"]; NSPopUpButton *pop = self.view.popupHttpApplication;
[self.view.popupHttpApplication insertItemWithTitle:NSLocalizedString(@"System Default", @"Default web browser application") atIndex:0]; [pop removeAllItems];
[self selectBundleID:[UserPrefs getHttpApplication] inPopup:self.view.popupHttpApplication]; [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 // Default RSS Reader application
[self generateMenuForPopup:self.view.popupDefaultRSSReader withScheme:@"feed"]; NSString *feedBundleId = CFBridgingRelease(LSCopyDefaultHandlerForURLScheme(CFSTR("feed")));
[self selectBundleID:[self defaultBundleIdForScheme:@"feed"] inPopup:self.view.popupDefaultRSSReader]; 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 { - (void)changeHttpApplication:(NSPopUpButton *)sender {
[UserPrefs setHttpApplication:sender.selectedItem.representedObject]; [UserPrefs setHttpApplication:sender.selectedItem.representedObject];
} }
- (void)changeDefaultRSSReader:(NSPopUpButton *)sender { // Callback method from round help button right of default feed reader text
if ([self setDefaultRSSApplication:sender.selectedItem.representedObject] == NO) { - (void)clickHowToDefaults:(NSButton *)sender {
// in case anything went wrong, restore previous selection NSAlert *alert = [[NSAlert alloc] init];
[self selectBundleID:[self defaultBundleIdForScheme:@"feed"] inPopup:sender]; 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 @end

View File

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

View File

@@ -28,20 +28,16 @@
- (instancetype)initWithController:(SettingsGeneral*)controller { - (instancetype)initWithController:(SettingsGeneral*)controller {
self = [super initWithFrame:NSZeroRect]; self = [super initWithFrame:NSZeroRect];
// Change default feed reader application
NSArray *lbls = @[NSLocalizedString(@"Open URLs with:", nil), NSTextField *l1 = [[NSView label:NSLocalizedString(@"Default feed reader:", nil)] placeIn:self x:PAD_WIN yTop:PAD_WIN + 3];
NSLocalizedString(@"Default RSS Reader:", nil)]; NSButton *help = [[[NSView helpButton] action:@selector(clickHowToDefaults:) target:controller] placeIn:self xRight:PAD_WIN yTop:PAD_WIN];
NSView *labels = [[NSView labelColumn:lbls rowHeight:HEIGHT_POPUP padding:PAD_M] placeIn:self x: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];
CGFloat x = NSMaxX(labels.frame) + PAD_S; // Popup button 'Open URLs with:'
CGFloat y = YFromTop(help) + PAD_M;
self.popupHttpApplication = [[self createPopup:x top: PAD_WIN + 1] action:@selector(changeHttpApplication:) target:controller]; NSTextField *l2 = [[NSView label:NSLocalizedString(@"Open URLs with:", nil)] placeIn:self x:PAD_WIN yTop:y + 1];
self.popupDefaultRSSReader = [[self createPopup:x top: YFromTop(self.popupHttpApplication) + PAD_M] action:@selector(changeDefaultRSSReader:) target:controller]; 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; 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 @end