Keep track of the file modification date on each open/save, compare it to the current file to detect external modifications.

This commit is contained in:
Riccardo Mottola 2021-02-19 10:45:30 +01:00
parent aa2ce8fd73
commit 059e105652
3 changed files with 42 additions and 3 deletions

View file

@ -1,3 +1,10 @@
2021-02-19 Riccardo Mottola <rm@gnu.org>
* 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 <rm@gnu.org>
* Framework/PCFileManager.m

View file

@ -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;

View file

@ -30,6 +30,8 @@
#import <Protocols/Preferences.h>
#import "Modules/Preferences/EditorFSC/PCEditorFSCPrefs.h"
#import <ProjectCenter/PCProjectManager.h>
#import <ProjectCenter/PCLogController.h>
@implementation PCEditor (UInterface)
@ -261,6 +263,8 @@
RELEASE(undoManager);
RELEASE(_lastSaveDate);
[super dealloc];
}
@ -283,6 +287,7 @@
NSMutableDictionary *attributes = [NSMutableDictionary new];
NSFont *font;
id <PCPreferences> 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