KMCloudDownloadOperationQueue.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // KMCloudDownloadOperationQueue.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 万军 on 2020/2/25.
  6. // Copyright © 2020 WanJun. All rights reserved.
  7. //
  8. #import "KMCloudDownloadOperationQueue.h"
  9. #import "KMCloudOperation.h"
  10. //#import "KMSyncHistoricalFileManager.h"
  11. @interface KMCloudDownloadOperationQueue ()
  12. @end
  13. @implementation KMCloudDownloadOperationQueue
  14. - (void)dealloc {
  15. [[NSNotificationCenter defaultCenter] removeObserver:self];
  16. }
  17. + (instancetype)sharedInstance {
  18. static KMCloudDownloadOperationQueue *sharedInstance;
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. sharedInstance = [KMCloudDownloadOperationQueue new];
  22. sharedInstance.downloadingOperations = [NSMutableDictionary dictionary];
  23. });
  24. return sharedInstance;
  25. }
  26. - (void)downloadAlert:(NSString *)alertString filePath:(NSString *)filePath {
  27. NSAlert *alert = [NSAlert alertWithMessageText:alertString
  28. defaultButton:NSLocalizedString(@"OK", nil)
  29. alternateButton:nil
  30. otherButton:nil
  31. informativeTextWithFormat:@"%@ %@", alertString, filePath];
  32. [alert runModal];
  33. }
  34. #pragma mark - download
  35. - (void)addDownloadOperation:(NSOperation *)op
  36. {
  37. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverCloudFileManagerDownloadSuccessfulNotification:) name:KMServerCloudFileManagerDownloadSuccessfulNotification object:nil];
  38. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverCloudFileManagerDownloadFailureNotification:) name:KMServerCloudFileManagerDownloadFailureNotification object:nil];
  39. if ([op isKindOfClass:[KMCloudOperation class]]) {
  40. KMCloudOperation *operation = (KMCloudOperation *)op;
  41. if (operation.serverType == KMDropbox) {
  42. [self addDropboxDownloadOperation:(KMCloudOperation *)op];
  43. } else if (operation.serverType == KMGoogleDrive) {
  44. [self addGoogleDriveDownloadOperation:(KMCloudOperation *)op];
  45. }
  46. // KMSyncHistoricalFile *historicalFile = [[[KMSyncHistoricalFile alloc] initWithOperationQueue:operation] autorelease];
  47. // [[KMSyncHistoricalFileManager defaultManager] addHistoricalFile:historicalFile];
  48. }
  49. }
  50. - (void)addDropboxDownloadOperation:(KMCloudOperation *)op {
  51. if ([self isDownloadOperationExisting:op.filePath]) {
  52. NSLog(@"KMDropboxOperation exists %@", op);
  53. return;
  54. }
  55. if (![self.downloadingOperations objectForKey:op.filePath]) {
  56. @synchronized (self) {
  57. [self.downloadingOperations setObject:op forKey:op.filePath];
  58. }
  59. [self addOperation:op];
  60. }
  61. }
  62. - (void)addGoogleDriveDownloadOperation:(KMCloudOperation *)op {
  63. if ([self isDownloadOperationExisting:op.filePath]) {
  64. NSLog(@"KMGoogleDriveOperation exists %@", op);
  65. return;
  66. }
  67. if (![self.downloadingOperations objectForKey:op.filePath]) {
  68. @synchronized (self) {
  69. [self.downloadingOperations setObject:op forKey:op.filePath];
  70. }
  71. [self addOperation:op];
  72. }
  73. }
  74. - (NSOperation *)isDownloadOperationExisting:(NSString *)urlString {
  75. for (NSString *url in self.downloadingOperations.allKeys) {
  76. if ([url isEqualToString:urlString]) {
  77. return [self.downloadingOperations objectForKey:url];
  78. }
  79. }
  80. return nil;
  81. }
  82. #pragma mark - cancel
  83. - (void)cancel:(NSString *)urlString {
  84. KMCloudOperation *op = [self.downloadingOperations objectForKey:urlString];
  85. if (op) {
  86. @synchronized (self) {
  87. [self.downloadingOperations removeObjectForKey:urlString];
  88. }
  89. }
  90. }
  91. - (void)cancelAll {
  92. @synchronized (self) {
  93. [self.downloadingOperations removeAllObjects];
  94. }
  95. [self cancelAllOperations];
  96. }
  97. #pragma mark - NSNotificationCenter
  98. - (void)serverCloudFileManagerDownloadSuccessfulNotification:(NSNotification *)notification {
  99. if (notification.object) {
  100. KMCloudOperation *operation = (KMCloudOperation *)notification.object;
  101. if ([self.downloadingOperations objectForKey:operation.filePath]) {
  102. @synchronized (self) {
  103. [self.downloadingOperations removeObjectForKey:operation.filePath];
  104. }
  105. }
  106. }
  107. }
  108. - (void)serverCloudFileManagerDownloadFailureNotification:(NSNotification *)notification {
  109. if (notification.object) {
  110. KMCloudOperation *operation = (KMCloudOperation *)notification.object;
  111. if ([self.downloadingOperations objectForKey:operation.filePath]) {
  112. @synchronized (self) {
  113. [self downloadAlert:NSLocalizedString(@"Download Failed", nil) filePath:[operation.toPath lastPathComponent]];
  114. [self.downloadingOperations removeObjectForKey:operation.filePath];
  115. }
  116. }
  117. }
  118. }
  119. @end