SKDocumentController.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // SKDocumentController.m
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2024/2/23.
  6. //
  7. #import "SKDocumentController.h"
  8. #import "SKAlias.h"
  9. NSString *SKDocumentSetupAliasKey = @"_BDAlias";
  10. NSString *SKDocumentSetupFileNameKey = @"fileName";
  11. @implementation SKDocumentController
  12. - (id)openDocumentWithSetup:(NSDictionary *)setup error:(NSError **)outError {
  13. id document = nil;
  14. NSURL *fileURL = [[SKAlias aliasWithData:[setup objectForKey:SKDocumentSetupAliasKey]] fileURL];
  15. if (fileURL == nil && [setup objectForKey:SKDocumentSetupFileNameKey])
  16. fileURL = [NSURL fileURLWithPath:[setup objectForKey:SKDocumentSetupFileNameKey]];
  17. if (fileURL && [fileURL checkResourceIsReachableAndReturnError:NULL] && NO == [self isTrashedFileURL:fileURL]) {
  18. if ((document = [self openDocumentWithContentsOfURL:fileURL display:NO error:outError])) {
  19. // [document applySetup:setup];
  20. [document showWindows];
  21. }
  22. } else if (outError) {
  23. *outError = [NSError errorWithDomain:@"SKDocumentErrorDomain" code:2 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:NSLocalizedString(@"Unable to load file", @"Error description"), NSLocalizedDescriptionKey, nil]];
  24. }
  25. return document;
  26. }
  27. - (BOOL)isTrashedFileURL:(NSURL *)url {
  28. NSCParameterAssert([url isFileURL]);
  29. FSRef fileRef;
  30. Boolean result = false;
  31. if (CFURLGetFSRef((CFURLRef)url, &fileRef)) {
  32. FSDetermineIfRefIsEnclosedByFolder(0, kTrashFolderType, &fileRef, &result);
  33. if (result == false)
  34. FSDetermineIfRefIsEnclosedByFolder(0, kSystemTrashFolderType, &fileRef, &result);
  35. }
  36. return result;
  37. }
  38. - (Class)documentClassForContentsOfURL:(NSURL *)inAbsoluteURL {
  39. return [self documentClassForType:[self typeForContentsOfURL:inAbsoluteURL error:NULL]];
  40. }
  41. @end