Automatisches Öffnen des letzten Dokuments default tip
authorMarkus Bröker<broeker.markus@googlemail.com>
Thu, 04 May 2017 18:18:57 +0200
changeset 8 c9cf6f811b86
parent 7 c350b40cbd5e
Automatisches Öffnen des letzten Dokuments
PDFViewer/appdelegate/AppDelegate.h
PDFViewer/appdelegate/AppDelegate.m
PDFViewer/pdfviewer/PDFViewer.m
--- a/PDFViewer/appdelegate/AppDelegate.h
+++ b/PDFViewer/appdelegate/AppDelegate.h
@@ -9,9 +9,7 @@
 #import <Cocoa/Cocoa.h>
 #import "ViewController.h"
 
-@interface AppDelegate : NSObject <NSApplicationDelegate> {
-    NSWindow *window;
-    ViewController *controller;
-}
-
+@interface AppDelegate : NSObject <NSApplicationDelegate>
+@property (weak) NSWindow* window;
+@property (weak) ViewController* controller;
 @end
--- a/PDFViewer/appdelegate/AppDelegate.m
+++ b/PDFViewer/appdelegate/AppDelegate.m
@@ -9,8 +9,25 @@
 #import "AppDelegate.h"
 
 @implementation AppDelegate
+@synthesize controller = _controller;
 
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newDocumentLoaded:) name:@"newDocumentLoaded" object:nil];
+}
+
+- (void)newDocumentLoaded:(NSNotification*) notification {
+    NSString *lastUrl = [notification object];
+    
+    NSLog(@"Setze die letzte URL auf %@", lastUrl);
+    
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+    [defaults setObject:lastUrl forKey:@"lastURL"];
+    [defaults synchronize];
+}
+
+- (void)applicationWillFinishLaunching:(NSNotification *)notification {
+    _window = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
+    _controller = (ViewController*) [_window contentViewController];
 }
 
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
@@ -22,33 +39,21 @@
 }
 
 - (BOOL) application:(NSApplication *)sender openFile:(NSString *)filename {
-    window = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
-    controller = (ViewController*) [window contentViewController];
-    
     NSURL *url = [[NSURL alloc] initFileURLWithPath:filename];
-    BOOL status = [[controller pdfViewer] loadUrl: url];
+    BOOL status = [[_controller pdfViewer] loadUrl: url];
     
     if (!status) {
-        [[controller pdfViewer] speak:(NSMutableString*)@"Das Dokument konnte nicht geladen werden"];
+        [[_controller pdfViewer] speak:(NSMutableString*)@"Das Dokument konnte nicht geladen werden"];
     } else {
-        [[controller pdfViewer] startSpeaking:nil];
+        [[_controller pdfViewer] startSpeaking:nil];
     }
     
     return status;
 }
 
 - (void) application:(NSApplication *)sender openFiles:(NSArray<NSString *> *)filenames {
-    window = [[[NSApplication sharedApplication] windows] objectAtIndex:0];
-    controller = (ViewController*) [window contentViewController];
-    
-    NSString *filename = [filenames objectAtIndex:0];
-    NSURL *url = [[NSURL alloc] initFileURLWithPath:filename];
-    BOOL status = [[controller pdfViewer] loadUrl: url];
-    
-    if (!status) {
-        [[controller pdfViewer] speak:(NSMutableString*)@"Das Dokument konnte nicht geladen werden"];
-    } else {
-        [[controller pdfViewer] startSpeaking:nil];
+    for (id filename in filenames) {
+        [self application:sender openFile:filename];
     }
 }
 
--- a/PDFViewer/pdfviewer/PDFViewer.m
+++ b/PDFViewer/pdfviewer/PDFViewer.m
@@ -22,20 +22,36 @@
     
     NSString *salutation = [self getSalutation];
     
+    if (nOps == 0) {
+        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+        NSURL *url = [NSURL URLWithString:[defaults objectForKey:@"lastURL"]];
+    
+        [self loadUrl:url];
+        nOps = [[self document] pageCount];
+    }
+    
     if (nOps > 0) {
         [strings appendString:[NSString stringWithFormat:@"%@, das Dokument hat %lu Seiten", salutation, nOps]];
-    } else {
-        [strings appendString:[NSString stringWithFormat:@"%@, laden Sie ein Dokument mit MAC-O", salutation]];
-    }
     
-    for (int pages = 0; pages < nOps; pages++) {
-        PDFPage *page = [[self document] pageAtIndex:pages];
-        [strings appendString:[self replaceString:[page string]]];
+        for (int pages = 0; pages < nOps; pages++) {
+            PDFPage *page = [[self document] pageAtIndex:pages];
+            [strings appendString:[self replaceString:[page string]]];
+        }
     }
     
     return strings;
 }
 
+- (IBAction)print:(id)sender {
+    [super print:sender];
+}
+
+- (IBAction)runPageLayout:(id)sender {
+    NSPageLayout *pageLayout = [NSPageLayout pageLayout];
+    
+    [pageLayout runModal];
+}
+
 /* Datei-Menü: Öffnen-Dialog */
 - (IBAction)openDocument:(id)sender {
     NSArray *fileTypes = [NSPDFImageRep imageTypes];
@@ -132,6 +148,10 @@
     [self silence];
     [self setDocument:doc];
     
+    if (doc != NULL) {
+        [[NSNotificationCenter defaultCenter] postNotificationName:@"newDocumentLoaded" object:url.absoluteString];
+    }
+    
     return (doc != NULL);
 }