Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add upgradeEncodingToUTF8 method; modifyNoteIfNecessary:usingCatalogE…

…ntry: needs to be smarter; rename some methods
  • Loading branch information...
commit 77b74555d99872d79d2fae7845d0954c6ab8c8f3 1 parent dc15aad
Zachary Schneirov authored
View
2  EncodingsManager.m
@@ -283,7 +283,7 @@ - (IBAction)okAction:(id)sender {
//check whether file mod. date of note is older than mod. date on disk
if ([self shouldUpdateNoteFromDisk]) {
- [note setFileEncodingAndUpdate:currentEncoding];
+ [note setFileEncodingAndReinterpret:currentEncoding];
[[NSApp delegate] contentsUpdatedForNote:note];
}
View
82 NotationController.m
@@ -144,20 +144,8 @@ - (id)initWithDirectoryRef:(FSRef*)directoryRef error:(OSStatus*)err {
//upgrade note-text-encodings here if there might exist notes with the wrong encoding (check NotationPrefs values)
if ([notationPrefs epochIteration] < 2 && ![notationPrefs firstTimeUsed]) {
- //this would have to be a database from epoch 1
-
- //for all notes with a system-default text encoding:
- // if storage format is SingleDB,
- // _setFileEncoding to UTF8
- // if content had high-ASCII in it,
- // update the obj's filemod time to fix encoding at the next format switch, in case those notes are not modified themselves first
- // else
- // if storage format is plaintext,
- // if content had high-ASCII in it,
- // _setFileEncoding to UTF8
- // writeUsingCurrentFileFormat
- // else
- // setFileEncodingAndUpdate to UTF8
+ //this would have to be a database from epoch 1, where the default file-encoding was system-default
+ [allNotes makeObjectsPerformSelector:@selector(upgradeToUTF8IfUsingSystemEncoding)];
}
}
@@ -754,42 +742,44 @@ - (BOOL)_readFilesInDirectory {
- (BOOL)modifyNoteIfNecessary:(NoteObject*)aNoteObject usingCatalogEntry:(NoteCatalogEntry*)catEntry {
//check dates
- UTCDateTime lastReadDate = fileModifiedDateOfNote(aNoteObject);
- UTCDateTime fileModDate = catEntry->lastModified;
-
- //should we always update the note's stored inode here regardless?
-
- if (lastReadDate.lowSeconds != fileModDate.lowSeconds ||
- lastReadDate.highSeconds != fileModDate.highSeconds ||
- lastReadDate.fraction != fileModDate.fraction) {
- //assume the file on disk was modified by someone other than us
-
- //figure out whether there is a conflict; is this file on disk older than the one that we have in memory? do we merge?
- //if ((UInt64*)&fileModDate > (UInt64*)&lastReadDate)
- //CFAbsoluteTime timeOnDisk, lastTime;
- //OSStatus err = noErr;
- //if ((err = (UCConvertUTCDateTimeToCFAbsoluteTime(&lastReadDate, &lastTime) == noErr)) &&
-// (err = (UCConvertUTCDateTimeToCFAbsoluteTime(&fileModDate, &timeOnDisk) == noErr))) {
+ UTCDateTime lastReadDate = fileModifiedDateOfNote(aNoteObject);
+ UTCDateTime fileModDate = catEntry->lastModified;
+
+ //should we always update the note's stored inode here regardless?
+
+ if (lastReadDate.lowSeconds != fileModDate.lowSeconds ||
+ lastReadDate.highSeconds != fileModDate.highSeconds ||
+ lastReadDate.fraction != fileModDate.fraction) {
+ //assume the file on disk was modified by someone other than us
+
+ //figure out whether there is a conflict; is this file on disk older than the one that we have in memory? do we merge?
+ //if ((UInt64*)&fileModDate > (UInt64*)&lastReadDate)
+ CFAbsoluteTime timeOnDisk, lastTime;
+ OSStatus err = noErr;
+ if ((err = (UCConvertUTCDateTimeToCFAbsoluteTime(&lastReadDate, &lastTime) == noErr)) &&
+ (err = (UCConvertUTCDateTimeToCFAbsoluteTime(&fileModDate, &timeOnDisk) == noErr))) {
+
+ if (timeOnDisk > lastTime) {
+ [aNoteObject updateFromCatalogEntry:catEntry];
- //if (timeOnDisk > lastTime) {
- [aNoteObject updateFromCatalogEntry:catEntry];
-
- [delegate contentsUpdatedForNote:aNoteObject];
-
- [self performSelector:@selector(scheduleUpdateListForAttribute:) withObject:NoteDateModifiedColumnString afterDelay:0.0];
-
- notesChanged = YES;
-// }
+ [delegate contentsUpdatedForNote:aNoteObject];
- NSLog(@"FILE WAS MODIFIED: %@", catEntry->filename);
+ [self performSelector:@selector(scheduleUpdateListForAttribute:) withObject:NoteDateModifiedColumnString afterDelay:0.0];
- return YES;
- //} else {
-// NSLog(@"modify note: error converting times: %d", err);
-// }
+ notesChanged = YES;
+ NSLog(@"FILE WAS MODIFIED: %@", catEntry->filename);
+ } else {
+ //check if this file's contents are identical to the current contents; if so, make the note's fileModDate older
+ //otherwise, attempt a merge of some sort
+ NSLog(@"File %@ is older than when we last saved it; not updating the note", catEntry->filename);
+ }
+ return YES;
+ } else {
+ NSLog(@"modify note: error converting times: %d", err);
}
-
- return NO;
+ }
+
+ return NO;
}
- (void)makeNotesMatchCatalogEntries:(NoteCatalogEntry**)catEntriesPtrs ofSize:(size_t)catCount {
View
3  NoteObject.h
@@ -120,7 +120,8 @@ NSInteger compareNodeID(id *a, id *b);
- (OSStatus)writeCurrentFileEncodingToFSRef:(FSRef*)fsRef;
- (void)_setFileEncoding:(NSStringEncoding)encoding;
-- (BOOL)setFileEncodingAndUpdate:(NSStringEncoding)encoding;
+- (BOOL)setFileEncodingAndReinterpret:(NSStringEncoding)encoding;
+- (BOOL)upgradeEncodingToUTF8;
- (BOOL)updateFromFile;
- (BOOL)updateFromCatalogEntry:(NoteCatalogEntry*)catEntry;
- (BOOL)updateFromData:(NSMutableData*)data;
View
33 NoteObject.m
@@ -961,18 +961,47 @@ - (OSStatus)writeCurrentFileEncodingToFSRef:(FSRef*)fsRef {
return err;
}
+- (BOOL)upgradeToUTF8IfUsingSystemEncoding {
+ //upgradeEncodingToUTF8
+}
+
+- (BOOL)upgradeEncodingToUTF8 {
+ //"convert" the file to have a UTF-8 encoding
+ BOOL didUpgrade = YES;
+
+ if (NSUTF8StringEncoding != fileEncoding) {
+ [self _setFileEncoding:NSUTF8StringEncoding];
+
+ if (contentsWere7Bit) {
+ if (PlainTextFormat == currentFormatID) {
+ //actual conversion is required
+ didUpgrade = [self writeUsingCurrentFileFormat];
+ } else if (SingleDatabaseFormat == currentFormatID) {
+ //update last-written-filemod time to guarantee proper encoding at next DB storage format switch,
+ //in case of existing files--if this note is never subsequently modified
+ if (UCConvertCFAbsoluteTimeToUTCDateTime(CFAbsoluteTimeGetCurrent(), &fileModifiedDate) != noErr)
+ NSLog(@"%s: can't set file modification date from current date", _cmd);
+ }
+ }
+ //make note dirty to ensure these changes are saved
+ [self makeNoteDirtyUpdateTime:NO updateFile:NO];
+ }
+ return didUpgrade;
+}
+
- (void)_setFileEncoding:(NSStringEncoding)encoding {
fileEncoding = encoding;
}
-- (BOOL)setFileEncodingAndUpdate:(NSStringEncoding)encoding {
+- (BOOL)setFileEncodingAndReinterpret:(NSStringEncoding)encoding {
+ //"reinterpret" the file using this encoding, also setting the actual file's extended attributes to match
BOOL updated = YES;
if (encoding != fileEncoding) {
[self _setFileEncoding:encoding];
//write the file encoding extended attribute before updating from disk. why?
- //a) to ensure -updateFromData: finds the right encoding when re-reading the file
+ //a) to ensure -updateFromData: finds the right encoding when re-reading the file, and
//b) because the file is otherwise not being rewritten, and the extended attribute--if it existed--may have been different
OSStatus err = noErr;
Please sign in to comment.
Something went wrong with that request. Please try again.