diff --git a/baRSS/AppHook.m b/baRSS/AppHook.m index 154367b..96b0cad 100644 --- a/baRSS/AppHook.m +++ b/baRSS/AppHook.m @@ -25,6 +25,7 @@ #import "FeedDownload.h" #import "Preferences.h" #import "DrawImage.h" +#import "SettingsFeeds+DragDrop.h" @interface AppHook() @property (strong) NSWindowController *prefWindow; @@ -71,6 +72,19 @@ // NSURLComponents *comp = [NSURLComponents componentsWithString:url]; } +/// Handle opml file imports +- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames { + NSMutableArray *urls = [NSMutableArray arrayWithCapacity:filenames.count]; + for (NSString *file in filenames) { + NSURL *u = [NSURL fileURLWithPath:file]; + if (u) [urls addObject:u]; + } + [self openPreferences]; + SettingsFeeds *sf = [(Preferences*)(self.prefWindow.window) selectFeedsTab]; + [sf importOpmlFiles:urls]; + [sender replyToOpenOrPrint:NSApplicationDelegateReplySuccess]; +} + #pragma mark - App Preferences diff --git a/baRSS/Info.plist b/baRSS/Info.plist index e7c77ec..d0b168c 100644 --- a/baRSS/Info.plist +++ b/baRSS/Info.plist @@ -32,7 +32,7 @@ CFBundleVersion - 9522 + 9628 LSMinimumSystemVersion $(MACOSX_DEPLOYMENT_TARGET) LSUIElement diff --git a/baRSS/Preferences/Feeds Tab/SettingsFeeds+DragDrop.h b/baRSS/Preferences/Feeds Tab/SettingsFeeds+DragDrop.h index 8f434f3..052630f 100644 --- a/baRSS/Preferences/Feeds Tab/SettingsFeeds+DragDrop.h +++ b/baRSS/Preferences/Feeds Tab/SettingsFeeds+DragDrop.h @@ -25,4 +25,5 @@ @interface SettingsFeeds (DragDrop) - (void)prepareOutlineViewForDragDrop:(NSOutlineView*)outline; +- (void)importOpmlFiles:(NSArray*)files; @end diff --git a/baRSS/Preferences/Feeds Tab/SettingsFeeds+DragDrop.m b/baRSS/Preferences/Feeds Tab/SettingsFeeds+DragDrop.m index 243d01c..fa4bc72 100644 --- a/baRSS/Preferences/Feeds Tab/SettingsFeeds+DragDrop.m +++ b/baRSS/Preferences/Feeds Tab/SettingsFeeds+DragDrop.m @@ -99,7 +99,7 @@ const NSPasteboardType dragReorder = @"de.relikd.baRSS.drag-reorder"; } else { // File import NSArray *files = [info.draggingPasteboard readObjectsForClasses:@[NSURL.class] options:@{ NSPasteboardURLReadingContentsConformToTypesKey: @[UTI_OPML] }]; - [[OpmlFileImport withDelegate:self] importFiles:files]; + [self importOpmlFiles:files]; } return YES; } @@ -108,6 +108,11 @@ const NSPasteboardType dragReorder = @"de.relikd.baRSS.drag-reorder"; #pragma mark - OPML File Import +/// Helper method is also called from Application Delegate +- (void)importOpmlFiles:(NSArray*)files { + [[OpmlFileImport withDelegate:self] importFiles:files]; +} + /// Filter out file urls that are not opml files - (void)outlineView:(NSOutlineView *)outlineView updateDraggingItemsForDrag:(id )info { if ([info.draggingPasteboard canReadItemWithDataConformingToTypes:@[(NSPasteboardType)kUTTypeFileURL]]) { @@ -134,7 +139,7 @@ const NSPasteboardType dragReorder = @"de.relikd.baRSS.drag-reorder"; /// OPML import (did end). Save changes, select newly inserted, and perform web request. - (void)opmlFileImportDidEnd:(NSManagedObjectContext*)moc { - if (!moc.hasChanges) { // exit early, dont need to create empty arrays + if (moc.undoManager.groupingLevel == 1 && !moc.hasChanges) { // exit early, dont need to create empty arrays [self endCoreDataChangeUndoEmpty:YES forceUndo:YES]; return; } @@ -153,7 +158,8 @@ const NSPasteboardType dragReorder = @"de.relikd.baRSS.drag-reorder"; } // Persist state, because on crash we have at least inserted items (without articles & icons) [StoreCoordinator saveContext:moc andParent:YES]; - [self.dataStore setSelectionIndexPaths:selection]; + if (selection.count > 0) + [self.dataStore setSelectionIndexPaths:selection]; [FeedDownload batchDownloadFeeds:feedsList favicons:YES showErrorAlert:YES finally:^{ [self endCoreDataChangeUndoEmpty:NO forceUndo:NO]; [self someDatesChangedScheduleUpdateTimer]; diff --git a/baRSS/Preferences/Preferences.h b/baRSS/Preferences/Preferences.h index a79f3e4..297769d 100644 --- a/baRSS/Preferences/Preferences.h +++ b/baRSS/Preferences/Preferences.h @@ -22,6 +22,9 @@ #import +@class SettingsFeeds; + @interface Preferences : NSWindow + (instancetype)window; +- (SettingsFeeds*)selectFeedsTab; @end diff --git a/baRSS/Preferences/Preferences.m b/baRSS/Preferences/Preferences.m index 35fb46f..264d5f7 100644 --- a/baRSS/Preferences/Preferences.m +++ b/baRSS/Preferences/Preferences.m @@ -49,9 +49,7 @@ TabItem(NSImageNameInfo, NSLocalizedString(@"About", nil), [SettingsAbout class]), ]; - NSInteger index = [[NSUserDefaults standardUserDefaults] integerForKey:@"preferencesTab"]; - if (index > 0 || (NSUInteger)index < self.tabViewItems.count) - self.selectedTabViewItemIndex = index; + [self switchToTab:[[NSUserDefaults standardUserDefaults] integerForKey:@"preferencesTab"]]; } return self; } @@ -64,6 +62,12 @@ NS_INLINE NSTabViewItem* TabItem(NSImageName imageName, NSString *text, Class cl return item; } +/// Safely set selected index without out of bounds exception +- (void)switchToTab:(NSInteger)index { + if (index > 0 || (NSUInteger)index < self.tabViewItems.count) + self.selectedTabViewItemIndex = index; +} + /// Delegate method, store last selected tab to user preferences - (void)tabView:(NSTabView*)tabView didSelectTabViewItem:(nullable NSTabViewItem*)tabViewItem { [super tabView:tabView didSelectTabViewItem:tabViewItem]; @@ -96,6 +100,12 @@ NS_INLINE NSTabViewItem* TabItem(NSImageName imageName, NSString *text, Class cl return w; } +- (SettingsFeeds*)selectFeedsTab { + PrefTabs *pref = (PrefTabs*)self.contentViewController; + [pref switchToTab:1]; + return (SettingsFeeds*)[pref.tabViewItems[1] viewController]; +} + - (void)windowWillClose:(NSNotification *)notification { [[NSUserDefaults standardUserDefaults] setObject:self.stringWithSavedFrame forKey:@"prefWindow"]; }