From aa2ce8fd73f3e50f1dd20f86c90dfcaaa7dc43c1 Mon Sep 17 00:00:00 2001 From: Riccardo Mottola Date: Thu, 18 Feb 2021 00:32:47 +0100 Subject: [PATCH 1/6] notify Gorm only if the file was really saved, or it will reload the old file --- Modules/Editors/ProjectCenter/PCEditor.m | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Modules/Editors/ProjectCenter/PCEditor.m b/Modules/Editors/ProjectCenter/PCEditor.m index 7c87098..b6da8ef 100644 --- a/Modules/Editors/ProjectCenter/PCEditor.m +++ b/Modules/Editors/ProjectCenter/PCEditor.m @@ -658,18 +658,18 @@ postNotificationName:PCEditorWillSaveNotification object:self]; - // Send the notification to Gorm... - if([[_path pathExtension] isEqual: @"h"]) - { - [[NSDistributedNotificationCenter defaultCenter] - postNotificationName: @"GormParseClassNotification" - object: _path]; - } - saved = [[_storage string] writeToFile:_path atomically:YES]; if (saved == YES) { + // Send the notification to Gorm... + if([[_path pathExtension] isEqual: @"h"]) + { + [[NSDistributedNotificationCenter defaultCenter] + postNotificationName: @"GormParseClassNotification" + object: _path]; + } + [self setIsEdited:NO]; [[NSNotificationCenter defaultCenter] postNotificationName:PCEditorDidSaveNotification From 059e10565209104ce97cf32c9c50a440a6a2ed81 Mon Sep 17 00:00:00 2001 From: Riccardo Mottola Date: Fri, 19 Feb 2021 10:45:30 +0100 Subject: [PATCH 2/6] Keep track of the file modification date on each open/save, compare it to the current file to detect external modifications. --- ChangeLog | 7 +++++ Modules/Editors/ProjectCenter/PCEditor.h | 3 +- Modules/Editors/ProjectCenter/PCEditor.m | 35 ++++++++++++++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index e1556fe..b66ce27 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2021-02-19 Riccardo Mottola + + * Modules/Editors/ProjectCenter/PCEditor.h + * Modules/Editors/ProjectCenter/PCEditor.m + Keep track of the file modification date on each open/save, + compare it to the current file to detect external modifications. + 2021-02-12 Riccardo Mottola * Framework/PCFileManager.m diff --git a/Modules/Editors/ProjectCenter/PCEditor.h b/Modules/Editors/ProjectCenter/PCEditor.h index 64679b4..96f97f4 100644 --- a/Modules/Editors/ProjectCenter/PCEditor.h +++ b/Modules/Editors/ProjectCenter/PCEditor.h @@ -1,7 +1,7 @@ /* GNUstep ProjectCenter - http://www.gnustep.org/experience/ProjectCenter.html - Copyright (C) 2002-2014 Free Software Foundation + Copyright (C) 2002-2021 Free Software Foundation Authors: Philippe C.D. Robert Serg Stoyan @@ -50,6 +50,7 @@ BOOL _isEditable; BOOL _isWindowed; BOOL _isExternal; + NSDate *_lastSaveDate; // Search NSView *goToLineView; diff --git a/Modules/Editors/ProjectCenter/PCEditor.m b/Modules/Editors/ProjectCenter/PCEditor.m index b6da8ef..bb97314 100644 --- a/Modules/Editors/ProjectCenter/PCEditor.m +++ b/Modules/Editors/ProjectCenter/PCEditor.m @@ -30,6 +30,8 @@ #import #import "Modules/Preferences/EditorFSC/PCEditorFSCPrefs.h" #import +#import + @implementation PCEditor (UInterface) @@ -261,6 +263,8 @@ RELEASE(undoManager); + RELEASE(_lastSaveDate); + [super dealloc]; } @@ -283,6 +287,7 @@ NSMutableDictionary *attributes = [NSMutableDictionary new]; NSFont *font; id prefs; + NSFileManager *fm; // Inform about future file opening [[NSNotificationCenter defaultCenter] @@ -320,6 +325,9 @@ [_storage setAttributedString:attributedString]; RELEASE(attributedString); + fm = [NSFileManager defaultManager]; + ASSIGN(_lastSaveDate, [[fm fileAttributesAtPath:_path traverseLink:NO] fileModificationDate]); + // [self _createInternalView]; /* if (categoryPath) // category == nil if we're non project editor { @@ -647,12 +655,23 @@ - (BOOL)saveFile { - BOOL saved = NO; + BOOL saved = NO; + NSFileManager *fm; + NSDate *fileModDate; if (_isEdited == NO) { return YES; } + + fm = [NSFileManager defaultManager]; + + fileModDate = [[fm fileAttributesAtPath:_path traverseLink:NO] fileModificationDate]; + // Check if the file was ever written and its time is the same as the current file modification date + if (!(_lastSaveDate && [fileModDate isEqualToDate:_lastSaveDate])) + { + PCLogInfo(self, @"File modified externally?"); + } [[NSNotificationCenter defaultCenter] postNotificationName:PCEditorWillSaveNotification @@ -687,7 +706,15 @@ - (BOOL)saveFileTo:(NSString *)path { - return [[_storage string] writeToFile:path atomically:YES]; + NSFileManager *fm = [NSFileManager defaultManager]; + + if ([[_storage string] writeToFile:path atomically:YES]) + { + ASSIGN(_lastSaveDate, [[fm fileAttributesAtPath:_path traverseLink:NO] fileModificationDate]); + return YES; + } + + return NO; } - (BOOL)revertFileToSaved @@ -696,6 +723,7 @@ NSAttributedString *as = nil; NSDictionary *at = nil; NSFont *ft = nil; + NSFileManager *fm; if (_isEdited == NO) { @@ -729,6 +757,9 @@ [_intEditorView setNeedsDisplay:YES]; [_extEditorView setNeedsDisplay:YES]; + + fm = [NSFileManager defaultManager]; + ASSIGN(_lastSaveDate, [[fm fileAttributesAtPath:_path traverseLink:NO] fileModificationDate]); [[NSNotificationCenter defaultCenter] postNotificationName:PCEditorDidRevertNotification From 78e4f47c8ff9a928bcede1437008aa052dabda90 Mon Sep 17 00:00:00 2001 From: Riccardo Mottola Date: Mon, 22 Feb 2021 16:00:48 +0100 Subject: [PATCH 3/6] use path passed as parameter --- Modules/Editors/ProjectCenter/PCEditor.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/Editors/ProjectCenter/PCEditor.m b/Modules/Editors/ProjectCenter/PCEditor.m index bb97314..df82fbe 100644 --- a/Modules/Editors/ProjectCenter/PCEditor.m +++ b/Modules/Editors/ProjectCenter/PCEditor.m @@ -710,7 +710,7 @@ if ([[_storage string] writeToFile:path atomically:YES]) { - ASSIGN(_lastSaveDate, [[fm fileAttributesAtPath:_path traverseLink:NO] fileModificationDate]); + ASSIGN(_lastSaveDate, [[fm fileAttributesAtPath:path traverseLink:NO] fileModificationDate]); return YES; } From a6fc84bc9be0505563e34c52db669528e3b8f1f9 Mon Sep 17 00:00:00 2001 From: Riccardo Mottola Date: Mon, 22 Feb 2021 16:55:13 +0100 Subject: [PATCH 4/6] saveFileTo should not update the save time --- Modules/Editors/ProjectCenter/PCEditor.m | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Modules/Editors/ProjectCenter/PCEditor.m b/Modules/Editors/ProjectCenter/PCEditor.m index df82fbe..4844fc1 100644 --- a/Modules/Editors/ProjectCenter/PCEditor.m +++ b/Modules/Editors/ProjectCenter/PCEditor.m @@ -706,15 +706,7 @@ - (BOOL)saveFileTo:(NSString *)path { - NSFileManager *fm = [NSFileManager defaultManager]; - - if ([[_storage string] writeToFile:path atomically:YES]) - { - ASSIGN(_lastSaveDate, [[fm fileAttributesAtPath:path traverseLink:NO] fileModificationDate]); - return YES; - } - - return NO; + return [[_storage string] writeToFile:path atomically:YES]; } - (BOOL)revertFileToSaved From 2ace3c327a64a8431d1f06d9705e3ab781e1baea Mon Sep 17 00:00:00 2001 From: Riccardo Mottola Date: Wed, 31 Mar 2021 23:25:50 +0200 Subject: [PATCH 5/6] re-set modification date on save, show alert panel --- Modules/Editors/ProjectCenter/PCEditor.m | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Modules/Editors/ProjectCenter/PCEditor.m b/Modules/Editors/ProjectCenter/PCEditor.m index 4844fc1..001f6b5 100644 --- a/Modules/Editors/ProjectCenter/PCEditor.m +++ b/Modules/Editors/ProjectCenter/PCEditor.m @@ -670,7 +670,16 @@ // Check if the file was ever written and its time is the same as the current file modification date if (!(_lastSaveDate && [fileModDate isEqualToDate:_lastSaveDate])) { - PCLogInfo(self, @"File modified externally?"); + NSInteger choice; + + PCLogInfo(self, @"File modified externally. %@ - %@", _lastSaveDate, fileModDate); + choice = NSRunAlertPanel(@"Overwrite File?", + @"File %@ was modified externally. Overwrite?", + @"Cancel", nil, @"Proceed", [_path lastPathComponent]); + if (choice == NSAlertDefaultReturn) + { + return NO; + } } [[NSNotificationCenter defaultCenter] @@ -681,6 +690,10 @@ if (saved == YES) { + [self setIsEdited:NO]; + + // re-read date just saved + ASSIGN(_lastSaveDate, [[fm fileAttributesAtPath:_path traverseLink:NO] fileModificationDate]); // Send the notification to Gorm... if([[_path pathExtension] isEqual: @"h"]) { @@ -689,7 +702,6 @@ object: _path]; } - [self setIsEdited:NO]; [[NSNotificationCenter defaultCenter] postNotificationName:PCEditorDidSaveNotification object:self]; From f9ed96a3e46ad17323f1873dd803e2032de733b6 Mon Sep 17 00:00:00 2001 From: Riccardo Mottola Date: Mon, 5 Apr 2021 19:51:42 +0200 Subject: [PATCH 6/6] offer option to reload file --- Modules/Editors/ProjectCenter/PCEditor.m | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Modules/Editors/ProjectCenter/PCEditor.m b/Modules/Editors/ProjectCenter/PCEditor.m index 001f6b5..da39080 100644 --- a/Modules/Editors/ProjectCenter/PCEditor.m +++ b/Modules/Editors/ProjectCenter/PCEditor.m @@ -675,11 +675,19 @@ PCLogInfo(self, @"File modified externally. %@ - %@", _lastSaveDate, fileModDate); choice = NSRunAlertPanel(@"Overwrite File?", @"File %@ was modified externally. Overwrite?", - @"Cancel", nil, @"Proceed", [_path lastPathComponent]); + @"Cancel", @"Reload", @"Proceed", [_path lastPathComponent]); if (choice == NSAlertDefaultReturn) { return NO; } + else if (choice == NSAlertAlternateReturn) + { + NSLog(@"Self"); + if ([self revertFileToSaved] == YES) + return NO; + NSLog(@"reload failed"); + return NO; + } } [[NSNotificationCenter defaultCenter]