URLScheme for handling app URLs

This commit is contained in:
relikd
2019-09-25 13:06:04 +02:00
parent 2c028e79e0
commit aa87d1be6a
8 changed files with 139 additions and 92 deletions

View File

@@ -27,6 +27,7 @@
5478DF04225A7AE200D30C64 /* SettingsFeedsView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5478DF03225A7AE200D30C64 /* SettingsFeedsView.m */; }; 5478DF04225A7AE200D30C64 /* SettingsFeedsView.m in Sources */ = {isa = PBXBuildFile; fileRef = 5478DF03225A7AE200D30C64 /* SettingsFeedsView.m */; };
548C6D0A230C33DE003A1AAF /* NSURL+Ext.m in Sources */ = {isa = PBXBuildFile; fileRef = 548C6D09230C33DE003A1AAF /* NSURL+Ext.m */; }; 548C6D0A230C33DE003A1AAF /* NSURL+Ext.m in Sources */ = {isa = PBXBuildFile; fileRef = 548C6D09230C33DE003A1AAF /* NSURL+Ext.m */; };
5491005D2331435E00858AE2 /* Download3rdParty.m in Sources */ = {isa = PBXBuildFile; fileRef = 5491005C2331435E00858AE2 /* Download3rdParty.m */; }; 5491005D2331435E00858AE2 /* Download3rdParty.m in Sources */ = {isa = PBXBuildFile; fileRef = 5491005C2331435E00858AE2 /* Download3rdParty.m */; };
54910067233A4D4000858AE2 /* URLScheme.m in Sources */ = {isa = PBXBuildFile; fileRef = 54910066233A4D4000858AE2 /* URLScheme.m */; };
5496B511214D6275003ED4ED /* UserPrefs.m in Sources */ = {isa = PBXBuildFile; fileRef = 5496B510214D6275003ED4ED /* UserPrefs.m */; }; 5496B511214D6275003ED4ED /* UserPrefs.m in Sources */ = {isa = PBXBuildFile; fileRef = 5496B510214D6275003ED4ED /* UserPrefs.m */; };
54A07A7F220E04CF00082C51 /* NSFetchRequest+Ext.m in Sources */ = {isa = PBXBuildFile; fileRef = 54A07A7E220E04CF00082C51 /* NSFetchRequest+Ext.m */; }; 54A07A7F220E04CF00082C51 /* NSFetchRequest+Ext.m in Sources */ = {isa = PBXBuildFile; fileRef = 54A07A7E220E04CF00082C51 /* NSFetchRequest+Ext.m */; };
54A07A82220E723D00082C51 /* MapUnreadTotal.m in Sources */ = {isa = PBXBuildFile; fileRef = 54A07A81220E723D00082C51 /* MapUnreadTotal.m */; }; 54A07A82220E723D00082C51 /* MapUnreadTotal.m in Sources */ = {isa = PBXBuildFile; fileRef = 54A07A81220E723D00082C51 /* MapUnreadTotal.m */; };
@@ -138,6 +139,8 @@
548C6D09230C33DE003A1AAF /* NSURL+Ext.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSURL+Ext.m"; sourceTree = "<group>"; }; 548C6D09230C33DE003A1AAF /* NSURL+Ext.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSURL+Ext.m"; sourceTree = "<group>"; };
5491005B2331435E00858AE2 /* Download3rdParty.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Download3rdParty.h; sourceTree = "<group>"; }; 5491005B2331435E00858AE2 /* Download3rdParty.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Download3rdParty.h; sourceTree = "<group>"; };
5491005C2331435E00858AE2 /* Download3rdParty.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Download3rdParty.m; sourceTree = "<group>"; }; 5491005C2331435E00858AE2 /* Download3rdParty.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Download3rdParty.m; sourceTree = "<group>"; };
54910065233A4D4000858AE2 /* URLScheme.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = URLScheme.h; sourceTree = "<group>"; };
54910066233A4D4000858AE2 /* URLScheme.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = URLScheme.m; sourceTree = "<group>"; };
5496B50F214D6275003ED4ED /* UserPrefs.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UserPrefs.h; sourceTree = "<group>"; }; 5496B50F214D6275003ED4ED /* UserPrefs.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UserPrefs.h; sourceTree = "<group>"; };
5496B510214D6275003ED4ED /* UserPrefs.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UserPrefs.m; sourceTree = "<group>"; }; 5496B510214D6275003ED4ED /* UserPrefs.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UserPrefs.m; sourceTree = "<group>"; };
54A07A7D220E04CF00082C51 /* NSFetchRequest+Ext.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSFetchRequest+Ext.h"; sourceTree = "<group>"; }; 54A07A7D220E04CF00082C51 /* NSFetchRequest+Ext.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSFetchRequest+Ext.h"; sourceTree = "<group>"; };
@@ -408,6 +411,8 @@
54209E932117325100F3B5EF /* DrawImage.m */, 54209E932117325100F3B5EF /* DrawImage.m */,
5496B50F214D6275003ED4ED /* UserPrefs.h */, 5496B50F214D6275003ED4ED /* UserPrefs.h */,
5496B510214D6275003ED4ED /* UserPrefs.m */, 5496B510214D6275003ED4ED /* UserPrefs.m */,
54910065233A4D4000858AE2 /* URLScheme.h */,
54910066233A4D4000858AE2 /* URLScheme.m */,
); );
path = Helper; path = Helper;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -586,6 +591,7 @@
54A07A82220E723D00082C51 /* MapUnreadTotal.m in Sources */, 54A07A82220E723D00082C51 /* MapUnreadTotal.m in Sources */,
54ACC29821061FBA0020715F /* Preferences.m in Sources */, 54ACC29821061FBA0020715F /* Preferences.m in Sources */,
54195886218E1BDB00581B79 /* NSMenu+Ext.m in Sources */, 54195886218E1BDB00581B79 /* NSMenu+Ext.m in Sources */,
54910067233A4D4000858AE2 /* URLScheme.m in Sources */,
54F6025D21C1D4170006D338 /* OpmlFile.m in Sources */, 54F6025D21C1D4170006D338 /* OpmlFile.m in Sources */,
5496B511214D6275003ED4ED /* UserPrefs.m in Sources */, 5496B511214D6275003ED4ED /* UserPrefs.m in Sources */,
546A6A2F22C585580034E806 /* SettingsAboutView.m in Sources */, 546A6A2F22C585580034E806 /* SettingsAboutView.m in Sources */,

View File

@@ -21,20 +21,18 @@
// SOFTWARE. // SOFTWARE.
#import "AppHook.h" #import "AppHook.h"
#import "Constants.h"
#import "DrawImage.h" #import "DrawImage.h"
#import "UserPrefs.h" #import "UserPrefs.h"
#import "Preferences.h" #import "Preferences.h"
#import "BarStatusItem.h" #import "BarStatusItem.h"
#import "UpdateScheduler.h" #import "UpdateScheduler.h"
#import "StoreCoordinator.h" #import "StoreCoordinator.h"
#import "OpmlFile.h"
#import "SettingsFeeds+DragDrop.h" #import "SettingsFeeds+DragDrop.h"
#import "URLScheme.h"
#import "NSURL+Ext.h" #import "NSURL+Ext.h"
#import "NSDate+Ext.h"
#import "NSError+Ext.h" #import "NSError+Ext.h"
@interface AppHook() <OpmlFileExportDelegate> @interface AppHook()
@property (strong) NSWindowController *prefWindow; @property (strong) NSWindowController *prefWindow;
@end @end
@@ -101,15 +99,6 @@
[UpdateScheduler scheduleNextFeed]; [UpdateScheduler scheduleNextFeed];
} }
/// Close previous preferences window and re-open at the same position (will drop undo manager stack!)
- (void)reopenPreferencesIfOpen {
if (self.prefWindow) {
CGPoint screenPoint = self.prefWindow.window.frame.origin;
[self.prefWindow close];
[[self openPreferences] setFrameOrigin:screenPoint];
}
}
#pragma mark - Core Data stack #pragma mark - Core Data stack
@@ -182,58 +171,7 @@
/// Callback method fired when opened with an URL (@c feed: and @c barss: scheme) /// Callback method fired when opened with an URL (@c feed: and @c barss: scheme)
- (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent { - (void)handleAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; [URLScheme withURL:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];
NSString *scheme = [[[NSURL URLWithString:url] scheme] lowercaseString];
url = [url substringFromIndex:scheme.length + 1]; // + ':'
if (url.length >= 2 && [[url substringToIndex:2] isEqualToString:@"//"]) {
url = [url substringFromIndex:2];
}
if ([scheme isEqualToString:kURLSchemeFeed]) {
[UpdateScheduler autoDownloadAndParseURL:url];
} else if ([scheme isEqualToString:kURLSchemeBarss]) {
NSMutableArray<NSString*> *comp = [[url pathComponents] mutableCopy];
NSString *action = comp.firstObject;
if (action) {
[comp removeObjectAtIndex:0];
[self handleConfigURLScheme:action parameters:comp];
}
}
}
/**
Helper method for handling the @c barss: scheme (see below).
@textblock
barss:open/preferences[/0-4]
barss:config/fixcache[/silent]
barss:backup[/show]
@/textblock
*/
- (void)handleConfigURLScheme:(const NSString*)action parameters:(NSArray<NSString*>*)params {
if ([action isEqualToString:kURLActionOpen]) {
if ([params.firstObject isEqualToString:kURLParamPreferences]) {
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:params.lastObject];
[[self openPreferences] selectTab:num.unsignedIntegerValue];
}
} else if ([action isEqualToString:kURLActionConfig]) {
if ([params.firstObject isEqualToString:kURLParamFixCache]) {
[StoreCoordinator cleanupAndShowAlert:![params.lastObject isEqualToString:kURLParamSilent]];
}
} else if ([action isEqualToString:kURLActionBackup]) {
NSURL *baseURL = [NSURL backupPathURL];
[baseURL mkdir]; // non destructive make dir
NSURL *dest = [baseURL file:[@"feeds_" stringByAppendingString:[NSDate dayStringISO8601]] ext:@"opml"];
NSURL *sym = [baseURL file:@"feeds_latest" ext:@"opml"];
[sym remove]; // remove old sym link, otherwise won't be updated
[[NSFileManager defaultManager] createSymbolicLinkAtURL:sym withDestinationURL:[NSURL URLWithString:dest.lastPathComponent] error:nil];
[[OpmlFileExport withDelegate:self] writeOPMLFile:dest withOptions:OpmlFileExportOptionFullBackup];
if ([params.firstObject isEqualToString:kURLParamShow])
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[dest]];
}
}
/// Callback method for OPML backup export
- (NSArray<FeedGroup*>*)opmlFileExportListOfFeedGroups:(OpmlFileExportOptions)options {
return [StoreCoordinator sortedFeedGroupsWithParent:nil inContext:nil];
} }

View File

@@ -108,30 +108,6 @@ static NSNotificationName const kNotificationTotalUnreadCountChanged = @"baRSS-n
static NSNotificationName const kNotificationTotalUnreadCountReset = @"baRSS-notification-total-unread-count-reset"; static NSNotificationName const kNotificationTotalUnreadCountReset = @"baRSS-notification-total-unread-count-reset";
#pragma mark - URL Scheme constants
/// @c feed: URL scheme. Used for feed subscriptions.
/// @note E.g., @c feed://https://feeds.feedburner.com/simpledesktops
static NSString* const kURLSchemeFeed = @"feed";
/// @c barss: URL scheme. Used for configuring the app.
/// @note E.g., @c barss://open/preferences
static NSString* const kURLSchemeBarss = @"barss";
/// Use @c barss:open to display information
static NSString* const kURLActionOpen = @"open";
/// Use @c barss:config to perform configuration steps
static NSString* const kURLActionConfig = @"config";
/// Use @c barss:backup to backup opml file into container
static NSString* const kURLActionBackup = @"backup";
/// Open preferences window with optional tab index. E.g., @c barss:open/preferences/1
static NSString* const kURLParamPreferences = @"preferences";
/// Run core data cleanup with optional silent parameter. E.g., @c barss:config/fixcache/silent
static NSString* const kURLParamFixCache = @"fixcache";
/// Disables error alerts and other interactive UI
static NSString* const kURLParamSilent = @"silent";
/// Show backup directory in Finder. E.g., @c barss:backup/show
static NSString* const kURLParamShow = @"show";
#pragma mark - Internal #pragma mark - Internal

View File

@@ -57,7 +57,7 @@ typedef NS_OPTIONS(NSUInteger, OpmlFileExportOptions) {
@interface OpmlFileExport : NSObject @interface OpmlFileExport : NSObject
@property (weak) id<OpmlFileExportDelegate> delegate; @property (weak) id<OpmlFileExportDelegate> delegate;
+ (instancetype)withDelegate:(id<OpmlFileExportDelegate>)delegate; + (instancetype)withDelegate:(nullable id<OpmlFileExportDelegate>)delegate;
- (void)showExportDialog:(NSWindow*)window; - (void)showExportDialog:(NSWindow*)window;
- (nullable NSError*)writeOPMLFile:(NSURL*)url withOptions:(OpmlFileExportOptions)opt; - (nullable NSError*)writeOPMLFile:(NSURL*)url withOptions:(OpmlFileExportOptions)opt;
@end @end

View File

@@ -197,7 +197,7 @@ static NSInteger RadioGroupSelection(NSView *view) {
@implementation OpmlFileExport @implementation OpmlFileExport
+ (instancetype)withDelegate:(id<OpmlFileExportDelegate>)delegate { + (instancetype)withDelegate:(nullable id<OpmlFileExportDelegate>)delegate {
OpmlFileExport *opml = [[super alloc] init]; OpmlFileExport *opml = [[super alloc] init];
opml.delegate = delegate; opml.delegate = delegate;
return opml; return opml;
@@ -241,6 +241,7 @@ static NSInteger RadioGroupSelection(NSView *view) {
*/ */
- (nullable NSError*)writeOPMLFile:(NSURL*)url withOptions:(OpmlFileExportOptions)opt { - (nullable NSError*)writeOPMLFile:(NSURL*)url withOptions:(OpmlFileExportOptions)opt {
NSArray<FeedGroup*> *list = [self.delegate opmlFileExportListOfFeedGroups:opt]; NSArray<FeedGroup*> *list = [self.delegate opmlFileExportListOfFeedGroups:opt];
if (!list) list = [StoreCoordinator sortedFeedGroupsWithParent:nil inContext:nil]; // fetch all if delegate == nil
NSError *error; NSError *error;
// TODO: set error if nil or empty // TODO: set error if nil or empty
if (list.count > 0) { if (list.count > 0) {

27
baRSS/Helper/URLScheme.h Normal file
View File

@@ -0,0 +1,27 @@
//
// The MIT License (MIT)
// Copyright (c) 2019 Oleg Geier
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
@import Cocoa;
@interface URLScheme : NSObject
+ (void)withURL:(NSString*)url;
@end

99
baRSS/Helper/URLScheme.m Normal file
View File

@@ -0,0 +1,99 @@
//
// The MIT License (MIT)
// Copyright (c) 2019 Oleg Geier
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#import "URLScheme.h"
#import "AppHook.h" // barss:open/preferences
#import "Preferences.h" // barss:open/preferences
#import "UpdateScheduler.h" // feed:http://URL
#import "StoreCoordinator.h" // barss:config/fixcache
#import "OpmlFile.h" // barss:backup
#import "NSURL+Ext.h" // barss:backup
#import "NSDate+Ext.h" // barss:backup
@implementation URLScheme
/// Handles open URL requests. Scheme may start with @c feed: or @c barss:
+ (void)withURL:(NSString*)url {
NSString *scheme = [[[NSURL URLWithString:url] scheme] lowercaseString];
url = [url substringFromIndex:scheme.length + 1]; // + ':'
url = [url stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
if ([scheme isEqualToString:@"feed"]) [[URLScheme new] handleSchemeFeed:url];
else if ([scheme isEqualToString:@"barss"]) [[URLScheme new] handleSchemeConfig:url];
}
/**
@c feed: URL scheme. Used for feed subscriptions.
@note E.g., @c feed://https://feeds.feedburner.com/simpledesktops
*/
- (void)handleSchemeFeed:(NSString*)url {
[UpdateScheduler autoDownloadAndParseURL:url];
}
/**
@c barss: URL scheme. Used for configuring the app.
@textblock
barss:open/preferences[/0-4]
barss:config/fixcache[/silent]
barss:backup[/show]
@/textblock
*/
- (void)handleSchemeConfig:(NSString*)url {
NSArray<NSString*> *comp = url.pathComponents;
NSString *action = comp.firstObject;
if (!action) return;
NSArray<NSString*> *params = [comp subarrayWithRange:NSMakeRange(1, comp.count - 1)];
if ([action isEqualToString:@"open"]) [self handleActionOpen:params];
else if ([action isEqualToString:@"config"]) [self handleActionConfig:params];
else if ([action isEqualToString:@"backup"]) [self handleActionBackup:params];
}
/// @c barss:open/preferences[/0-4]
- (void)handleActionOpen:(NSArray<NSString*>*)params {
if ([params.firstObject isEqualToString:@"preferences"]) {
NSDecimalNumber *num = [NSDecimalNumber decimalNumberWithString:params.lastObject];
[[(AppHook*)NSApp openPreferences] selectTab:num.unsignedIntegerValue];
}
}
/// @c barss:config/fixcache[/silent]
- (void)handleActionConfig:(NSArray<NSString*>*)params {
if ([params.firstObject isEqualToString:@"fixcache"]) {
[StoreCoordinator cleanupAndShowAlert:![params.lastObject isEqualToString:@"silent"]];
}
}
/// @c barss:backup[/show]
- (void)handleActionBackup:(NSArray<NSString*>*)params {
NSURL *baseURL = [NSURL backupPathURL];
[baseURL mkdir]; // non destructive make dir
NSURL *dest = [baseURL file:[@"feeds_" stringByAppendingString:[NSDate dayStringISO8601]] ext:@"opml"];
NSURL *sym = [baseURL file:@"feeds_latest" ext:@"opml"];
[sym remove]; // remove old sym link, otherwise won't be updated
[[NSFileManager defaultManager] createSymbolicLinkAtURL:sym withDestinationURL:[NSURL URLWithString:dest.lastPathComponent] error:nil];
[[OpmlFileExport withDelegate:nil] writeOPMLFile:dest withOptions:OpmlFileExportOptionFullBackup];
if ([params.firstObject isEqualToString:@"show"]) {
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[dest]];
}
}
@end

View File

@@ -70,7 +70,7 @@
</dict> </dict>
</array> </array>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>13607</string> <string>13702</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>