Sfoglia il codice sorgente

【偏好设置】新增AutoSaveManager及KMGeneralPreferences里自动保存补充

tangchao 1 anno fa
parent
commit
112040eef5

+ 26 - 0
PDF Office/PDF Master.xcodeproj/project.pbxproj

@@ -2275,6 +2275,9 @@
 		BB3EAEB0293E3D6000D92407 /* KMConvertBaseWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = BB3EAEAC293E3D6000D92407 /* KMConvertBaseWindowController.xib */; };
 		BB3EAEB1293E3D6000D92407 /* KMConvertBaseWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = BB3EAEAC293E3D6000D92407 /* KMConvertBaseWindowController.xib */; };
 		BB3EAEB2293E3D6000D92407 /* KMConvertBaseWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = BB3EAEAC293E3D6000D92407 /* KMConvertBaseWindowController.xib */; };
+		BB46CF432AFB663700281EDF /* AutoSaveManager.m in Sources */ = {isa = PBXBuildFile; fileRef = BB46CF422AFB663700281EDF /* AutoSaveManager.m */; };
+		BB46CF442AFB663700281EDF /* AutoSaveManager.m in Sources */ = {isa = PBXBuildFile; fileRef = BB46CF422AFB663700281EDF /* AutoSaveManager.m */; };
+		BB46CF452AFB663700281EDF /* AutoSaveManager.m in Sources */ = {isa = PBXBuildFile; fileRef = BB46CF422AFB663700281EDF /* AutoSaveManager.m */; };
 		BB49ECDD293EED6100C82CA2 /* KMConvertWordWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB49ECDB293EED6100C82CA2 /* KMConvertWordWindowController.swift */; };
 		BB49ECDE293EED6100C82CA2 /* KMConvertWordWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB49ECDB293EED6100C82CA2 /* KMConvertWordWindowController.swift */; };
 		BB49ECDF293EED6100C82CA2 /* KMConvertWordWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB49ECDB293EED6100C82CA2 /* KMConvertWordWindowController.swift */; };
@@ -4463,6 +4466,8 @@
 		BB3AD6F829935483004FC1AE /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = "<group>"; };
 		BB3EAEAB293E3D6000D92407 /* KMConvertBaseWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMConvertBaseWindowController.swift; sourceTree = "<group>"; };
 		BB3EAEAC293E3D6000D92407 /* KMConvertBaseWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMConvertBaseWindowController.xib; sourceTree = "<group>"; };
+		BB46CF412AFB663600281EDF /* AutoSaveManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AutoSaveManager.h; sourceTree = "<group>"; };
+		BB46CF422AFB663700281EDF /* AutoSaveManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AutoSaveManager.m; sourceTree = "<group>"; };
 		BB49ECDB293EED6100C82CA2 /* KMConvertWordWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMConvertWordWindowController.swift; sourceTree = "<group>"; };
 		BB49ECDC293EED6100C82CA2 /* KMConvertWordWindowController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KMConvertWordWindowController.xib; sourceTree = "<group>"; };
 		BB49ECE4293EF54800C82CA2 /* KMCustomPDFView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMCustomPDFView.swift; sourceTree = "<group>"; };
@@ -7544,6 +7549,7 @@
 		BB2C6A7128F2652500478A33 /* Class */ = {
 			isa = PBXGroup;
 			children = (
+				BB46CF3F2AFB659800281EDF /* AutoSave */,
 				AD6260762A9DC62C006C6413 /* KMResourceDownload */,
 				BB32BF212A5EA02900D11348 /* Analytics */,
 				ADE8BC3629F9453A00570F89 /* KMAdvertisement */,
@@ -7777,6 +7783,23 @@
 			path = Controller;
 			sourceTree = "<group>";
 		};
+		BB46CF3F2AFB659800281EDF /* AutoSave */ = {
+			isa = PBXGroup;
+			children = (
+				BB46CF402AFB65F700281EDF /* Tools */,
+			);
+			path = AutoSave;
+			sourceTree = "<group>";
+		};
+		BB46CF402AFB65F700281EDF /* Tools */ = {
+			isa = PBXGroup;
+			children = (
+				BB46CF412AFB663600281EDF /* AutoSaveManager.h */,
+				BB46CF422AFB663700281EDF /* AutoSaveManager.m */,
+			);
+			path = Tools;
+			sourceTree = "<group>";
+		};
 		BB49ECE3293EF50800C82CA2 /* View */ = {
 			isa = PBXGroup;
 			children = (
@@ -11026,6 +11049,7 @@
 				BB5F8A0E29BB04F000365ADB /* GBDeviceInfo_OSX.m in Sources */,
 				BBB7B4892A033F6200B58A5A /* KMThumbnailView.swift in Sources */,
 				BB671A012AD2D2A0003D44D5 /* CPDFStampAnnotation+PDFListView.swift in Sources */,
+				BB46CF432AFB663700281EDF /* AutoSaveManager.m in Sources */,
 				9F0CB4F5298655D500007028 /* KMDesignToken+Fill.swift in Sources */,
 				BBB7B48D2A0384E100B58A5A /* NSCollectionViewItem+KMExtension.swift in Sources */,
 				F3732323292DFFFE0013862C /* CPDFListView+Extension.m in Sources */,
@@ -11919,6 +11943,7 @@
 				ADAFDA352AE8EE9300F084BC /* KMAdvertisementImage.swift in Sources */,
 				9F1F82CB292F6CF90092C4B4 /* KMPDFInsertPageWindow.swift in Sources */,
 				BB6CA4CD298BB0D000A13864 /* KMPreferenceWindowController.swift in Sources */,
+				BB46CF442AFB663700281EDF /* AutoSaveManager.m in Sources */,
 				ADB5E51A2A3711CB007110A8 /* KMSubscribeWaterMarkView.swift in Sources */,
 				BB8F456E295AC1220037EA22 /* KMHeaderFooterAdjectiveModel.swift in Sources */,
 				F3599174292B62F5000D25DE /* CStringConstants.m in Sources */,
@@ -12056,6 +12081,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				BB46CF452AFB663700281EDF /* AutoSaveManager.m in Sources */,
 				BBC745F9296178BD0072C2ED /* KMCropTools.swift in Sources */,
 				ADBC2D21299CDA29006280C8 /* NSView+Layer.swift in Sources */,
 				BB65A06D2AF8D8A1003A27A0 /* KMNotesPreferences.swift in Sources */,

+ 44 - 0
PDF Office/PDF Master/Class/AutoSave/Tools/AutoSaveManager.h

@@ -0,0 +1,44 @@
+//
+//  AutoSaveManager.h
+//  PDF Reader Pro Edition
+//
+//  Created by Niehaoyu on 2023/08/10.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+#define KAutoSaveTimeValueChangedNoti @"KAutoSaveTimeValueChangedNoti"
+
+@interface AutoSaveManager : NSObject
+
+@property (nonatomic, copy, readonly) NSString *autoSaveFolder;
+
+@property (nonatomic, strong, readonly) NSMutableArray *originalPaths;//保存的原文件信息
+
+@property (nonatomic, strong, readonly) NSMutableArray *autoSavePaths;//对应保存的信息
+
+@property (nonatomic, strong) NSMutableArray *opendPaths;//所有打开的文件信息汇总
+
+@property (nonatomic, assign) BOOL autoSaveEnabled;//APP是否允许进行自动缓存
+
+@property (nonatomic, assign) BOOL autoSaveAlertShow;//防止重复弹出
+
+@property (nonatomic, assign) BOOL autoSaveDidEndAction;
+
+@property (nonatomic, assign) BOOL isSaving;//当前是否正在保存
+
+@property (nonatomic, assign) CGFloat timeInterval;
+
++ (AutoSaveManager *)manager;
+
+- (void)clearCache;
+
+- (NSString *)autoSaveWithPath:(NSString *)filePath;
+
+- (void)removeAutoSavePath:(NSString *)filePath;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 232 - 0
PDF Office/PDF Master/Class/AutoSave/Tools/AutoSaveManager.m

@@ -0,0 +1,232 @@
+//
+//  AutoSaveManager.m
+//  PDF Reader Pro Edition
+//
+//  Created by Niehaoyu on 2023/08/10.
+//
+
+#import "AutoSaveManager.h"
+
+@interface AutoSaveManager ()
+
+@property (nonatomic, strong) NSMutableDictionary *infoDict;
+
+@property (nonatomic, strong, readwrite) NSMutableArray *originalPaths;//保存的原文件信息
+
+@property (nonatomic, strong, readwrite) NSMutableArray *autoSavePaths;
+
+@property (nonatomic, copy, readwrite) NSString *autoSaveFolder;
+
+@end
+
+@implementation AutoSaveManager
+
++ (AutoSaveManager *)manager {
+    static AutoSaveManager *__manager = nil;
+    if (!__manager) {
+        __manager = [[super allocWithZone:NULL] init];
+    }
+    return __manager;
+}
+
++ (instancetype)allocWithZone:(struct _NSZone *)zone {
+    return AutoSaveManager.manager;
+}
+
+- (id)init {
+    self = [super init];
+    if (self) {
+        
+        self.autoSaveEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"ComAutoSaveKey"];
+        
+        if ([[NSUserDefaults standardUserDefaults] floatForKey:@"AutoSaveTimeIntervalKey"]) {
+            self.timeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@"AutoSaveTimeIntervalKey"];
+        } else {
+            self.timeInterval = 15;
+        }
+        
+        [self loadData];
+        
+        self.autoSaveDidEndAction = YES;
+        
+    }
+    return self;
+}
+
+- (void)loadData {
+    
+    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
+    
+    if ([[NSFileManager defaultManager] fileExistsAtPath:[cachesPath stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]]) {
+        cachesPath = [cachesPath stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier];
+    }
+    cachesPath = [cachesPath stringByAppendingPathComponent:@"autoSaveFolder"];
+    if (![[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {
+        [[NSFileManager defaultManager] createDirectoryAtPath:cachesPath withIntermediateDirectories:YES attributes:nil error:nil];
+    }
+    self.autoSaveFolder = cachesPath;
+    
+#if DEBUG
+    NSLog(@"autoSaveFolder===%@",cachesPath);
+#endif
+    
+    NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",self.autoSaveFolder];
+    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
+    if (dict.allKeys.count == 0) {
+        return;
+    }
+    
+    self.infoDict = dict;
+    
+    if (!self.autoSavePaths) {
+        self.autoSavePaths = [[NSMutableArray alloc] init];
+    }
+    
+    if (!self.originalPaths) {
+        self.originalPaths = [[NSMutableArray alloc] init];
+    }
+    [self.originalPaths addObjectsFromArray:dict.allKeys];
+    
+    self.opendPaths = [[NSMutableArray alloc] init];
+    
+    NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.autoSaveFolder error:nil];
+    for (NSString *fileName in arr) {
+        NSString *savedKey = [self.autoSaveFolder stringByAppendingPathComponent:fileName];
+        if ([dict.allValues containsObject:savedKey]) {
+            [self.autoSavePaths addObject:savedKey];
+        }else {
+            if ([fileName isEqualToString:@"autoSaveInfo.plist"]) {
+                
+            } else {
+                [[NSFileManager defaultManager] removeItemAtPath:savedKey error:nil];
+            }
+        }
+    }
+}
+
+#pragma mark - Setter
+- (void)setAutoSaveEnabled:(BOOL)autoSaveEnabled {
+    _autoSaveEnabled = autoSaveEnabled;
+
+    [[NSUserDefaults standardUserDefaults] setBool:_autoSaveEnabled forKey:@"ComAutoSaveKey"];
+    [[NSUserDefaults standardUserDefaults] synchronize];
+}
+
+- (void)setTimeInterval:(CGFloat)timeInterval {
+    _timeInterval = timeInterval;
+    
+    [[NSUserDefaults standardUserDefaults] setFloat:_timeInterval forKey:@"AutoSaveTimeIntervalKey"];
+    [[NSUserDefaults standardUserDefaults] synchronize];
+}
+
+- (NSString *)URLEncodedString:(NSString *)string {
+    CFStringRef stringRef = CFBridgingRetain(string);
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+    CFStringRef encoded = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
+                                                                  stringRef,
+                                                                  NULL,
+                                                                  CFSTR("!*'\"();:@&=+$,/?%#[]% "),
+                                                                  kCFStringEncodingUTF8);
+#pragma clang diagnostic pop
+    CFRelease(stringRef);
+    return CFBridgingRelease(encoded);
+}
+
+- (NSString *)getValidFilePath:(NSString *)oldPath {
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    NSDictionary  *fileDic     = [fileManager attributesOfItemAtPath:oldPath error:nil];
+    NSString      *fileType    = [fileDic objectForKey:NSFileType];
+    int i =1;
+    NSString *newPath = oldPath;
+    while ([fileManager fileExistsAtPath:newPath]) {
+        if ([fileType isEqualToString:NSFileTypeDirectory]) {
+            newPath = [oldPath stringByAppendingFormat:@"(%d)",i];
+        } else {
+            NSString *fileExtension = [oldPath pathExtension];
+            newPath = [[oldPath stringByDeletingPathExtension] stringByAppendingFormat:@"(%d).%@",i,fileExtension];
+        }
+        i++;
+    }
+    return newPath;
+}
+
+#pragma mark - Public Method
+- (void)clearCache {
+    if ([[NSFileManager defaultManager] fileExistsAtPath:self.autoSaveFolder]) {
+        [[NSFileManager defaultManager] removeItemAtPath:self.autoSaveFolder error:nil];
+    }
+    if (![[NSFileManager defaultManager] fileExistsAtPath:self.autoSaveFolder]) {
+        [[NSFileManager defaultManager] createDirectoryAtPath:self.autoSaveFolder withIntermediateDirectories:YES attributes:nil error:nil];
+    }
+}
+
+- (NSString *)autoSaveWithPath:(NSString *)filePath {
+    
+    NSString *autoSaveFolder = self.autoSaveFolder;
+    
+    NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",autoSaveFolder];
+    NSString *savePath = [NSString stringWithFormat:@"%@/%@_%@.%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",filePath.pathExtension];
+
+    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
+        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
+        if ([dict.allKeys containsObject:filePath]) {
+            savePath = [dict valueForKey:filePath];
+        }
+        if (savePath.length > 0 && filePath.length > 0) {
+            if ([dict.allValues containsObject:savePath]) {
+                //不同路径下同名文件的保存覆盖的情况处理
+                NSArray *values = [[NSArray alloc] initWithArray:dict.allValues];
+                
+                for (NSString *key in dict.allKeys) {
+                    NSString *value = [dict objectForKey:key];
+                    if ([savePath isEqualToString:value] &&
+                        ![key isEqualToString:filePath]) {
+                        int count = 1;
+                        savePath = [NSString stringWithFormat:@"%@/%@_%@(%d).%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",count,filePath.pathExtension];
+                        while ([values containsObject:savePath]) {
+                            count ++;
+                            savePath = [NSString stringWithFormat:@"%@/%@_%@(%d).%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",count,filePath.pathExtension];
+                        }
+                    }
+                }
+            }
+            [dict setValue:savePath forKey:filePath];
+        }
+        [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
+    } else {
+        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
+        if (savePath.length > 0 && filePath.length > 0) {
+            [dict setValue:savePath forKey:filePath];
+        }
+        [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
+        
+    }
+    return savePath;
+}
+
+- (void)removeAutoSavePath:(NSString *)filePath {
+ 
+    NSString *autoSaveFolder = self.autoSaveFolder;
+
+    NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",autoSaveFolder];
+    if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
+        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
+        if ([dict valueForKey:filePath]) {
+            NSString *savePath = [dict valueForKey:filePath];
+            if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) {
+                [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
+            }
+            [dict removeObjectForKey:filePath];
+        }
+        
+        if (dict.allKeys.count > 0) {
+            [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
+        } else {
+            [[NSFileManager defaultManager] removeItemAtPath:plistPath error:nil];
+        }
+    }
+    
+}
+
+@end

+ 17 - 20
PDF Office/PDF Master/Class/Preference/Controller/KMGeneralPreferences.swift

@@ -138,15 +138,15 @@ class KMGeneralPreferences: NSViewController {
     #endif
         
         self.autoSaveInfoBtn.title = KMLocalizedString("Autosave Documents", nil)
-    //    self.autoSaveInfoBtn.state = [AutoSaveManager manager].autoSaveEnabled ? NSControlStateValueOn : NSControlStateValueOff;
-    //    self.autoSaveSlider.enabled = [AutoSaveManager manager].autoSaveEnabled;
+        self.autoSaveInfoBtn.state = AutoSaveManager().autoSaveEnabled ? .on : .off
+        self.autoSaveSlider.isEnabled = AutoSaveManager().autoSaveEnabled
         
-    //    if ([AutoSaveManager manager].timeInterval > 1) {
-    //        self.autoSaveSlider.floatValue = ([AutoSaveManager manager].timeInterval - 1)/19;
-    //    } else {
+        if (AutoSaveManager().timeInterval > 1) {
+            self.autoSaveSlider.floatValue = Float((AutoSaveManager().timeInterval - 1) / 19)
+        } else {
             self.autoSaveSlider.floatValue = 0;
-    //    }
-    //    self.autoSaveLbl.stringValue = [NSString stringWithFormat:@"%.0f %@", [AutoSaveManager manager].timeInterval, NSLocalizedString(@"minute(s)", nil)];
+        }
+        self.autoSaveLbl.stringValue = String(format: "%.0f %@", AutoSaveManager().timeInterval, KMLocalizedString("minute(s)", nil))
     }
     
     override func viewDidLoad() {
@@ -210,27 +210,24 @@ class KMGeneralPreferences: NSViewController {
     }
     
     @IBAction func autoSaveButtonAction(_ sender: AnyObject?) {
-        //    [AutoSaveManager manager].autoSaveEnabled = self.autoSaveInfoBtn.state == NSControlStateValueOn;
-        //    self.autoSaveSlider.enabled = [AutoSaveManager manager].autoSaveEnabled;
+        AutoSaveManager().autoSaveEnabled = self.autoSaveInfoBtn.state == .on
+        self.autoSaveSlider.isEnabled = AutoSaveManager().autoSaveEnabled
     }
     
     @IBAction func autoSaveSliderAction(_ sender: AnyObject?) {
-//        CGFloat value = 1 + 19 * self.autoSaveSlider.floatValue;
+        let value = 1 + 19 * self.autoSaveSlider.floatValue
         
-    //    [AutoSaveManager manager].timeInterval = value;
-    //    self.autoSaveLbl.stringValue = [NSString stringWithFormat:@"%.0f %@", [AutoSaveManager manager].timeInterval, NSLocalizedString(@"minute(s)", nil)];
+        AutoSaveManager().timeInterval = CGFloat(value)
+        self.autoSaveLbl.stringValue = String(format: "%.0f %@", AutoSaveManager().timeInterval, KMLocalizedString("minute(s)", nil))
         
-//        [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoSaveSliderFinish) object:nil];
-//        [self performSelector:@selector(autoSaveSliderFinish) withObject:nil afterDelay:1];
+        Self.cancelPreviousPerformRequests(withTarget: self, selector: #selector(autoSaveSliderFinish), object: nil)
+        self.perform(#selector(autoSaveSliderFinish), with: nil, afterDelay: 1)
     }
     
-    /*
-
+    @objc func autoSaveSliderFinish() {
+        NotificationCenter.default.post(name: NSNotification.Name(rawValue: KAutoSaveTimeValueChangedNoti), object: nil)
+    }
 
-     - (void)autoSaveSliderFinish {
-     //    [[NSNotificationCenter defaultCenter] postNotificationName:KAutoSaveTimeValueChangedNoti object:nil];
-     }
-     */
     /*
      #pragma mark Accessors
 

+ 2 - 0
PDF Office/PDF Master/PDF_Master DMG-Bridging-Header.h

@@ -60,3 +60,5 @@
 
 #import "KMConvertURLToPDF.h"
 #import "NSImage_SKExtensions.h"
+
+#import "AutoSaveManager.h"

+ 2 - 0
PDF Office/PDF Master/PDF_Master Pro-Bridging-Header.h

@@ -43,3 +43,5 @@
 
 #import "KMConvertURLToPDF.h"
 #import "NSImage_SKExtensions.h"
+
+#import "AutoSaveManager.h"

+ 2 - 0
PDF Office/PDF Master/PDF_Master-Bridging-Header.h

@@ -57,3 +57,5 @@
 #import "KMConvertURLToPDF.h"
 
 #import "NSImage_SKExtensions.h"
+
+#import "AutoSaveManager.h"