Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:scrod/nv
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Schneirov committed Jul 22, 2010
2 parents 774824c + 0880a5b commit e73eadc
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
2 changes: 1 addition & 1 deletion AppController.h
Expand Up @@ -47,7 +47,7 @@

LinearDividerShader *dividerShader;

NSMutableArray *notesToOpenOnLaunch;
NSMutableArray *pathsToOpenOnLaunch;

NSUndoManager *windowUndoManager;
PrefsWindowController *prefsWindowController;
Expand Down
29 changes: 10 additions & 19 deletions AppController.m
Expand Up @@ -240,10 +240,10 @@ - (void)applicationDidFinishLaunching:(NSNotification*)aNote {
//import old database(s) here if necessary
[AlienNoteImporter importBlorOrHelpFilesIfNecessaryIntoNotation:newNotation];

if (notesToOpenOnLaunch) {
[notationController addNotes:notesToOpenOnLaunch];
[notesToOpenOnLaunch release];
notesToOpenOnLaunch = nil;
if (pathsToOpenOnLaunch) {
[notationController openFiles:pathsToOpenOnLaunch];
[pathsToOpenOnLaunch release];
pathsToOpenOnLaunch = nil;
}

//tell us..
Expand Down Expand Up @@ -408,11 +408,7 @@ - (BOOL)addNotesFromPasteboard:(NSPasteboard*)pasteboard {
if ([types containsObject:NSFilenamesPboardType]) {
NSArray *files = [pasteboard propertyListForType:NSFilenamesPboardType];
if ([files isKindOfClass:[NSArray class]]) {
NSArray *notes = [[[[AlienNoteImporter alloc] initWithStoragePaths:files] autorelease] importedNotes];
if ([notes count] > 0) {
[notationController addNotes:notes];
return YES;
}
if ([notationController openFiles:files]) return YES;
}
}

Expand Down Expand Up @@ -679,17 +675,12 @@ - (IBAction)showHelpDocument:(id)sender {

- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames {

//should check filenames here to see whether notationcontroller already owns these
NSArray *notes = [[[[AlienNoteImporter alloc] initWithStoragePaths:filenames] autorelease] importedNotes];

if (notes) {
if (notationController)
[notationController addNotes:notes];
else
notesToOpenOnLaunch = [notes mutableCopyWithZone:nil];
}
if (notationController)
[notationController openFiles:filenames];
else
pathsToOpenOnLaunch = [filenames mutableCopyWithZone:nil];

[NSApp replyToOpenOrPrint:notes ? NSApplicationDelegateReplySuccess : NSApplicationDelegateReplyFailure];
[NSApp replyToOpenOrPrint:[filenames count] ? NSApplicationDelegateReplySuccess : NSApplicationDelegateReplyFailure];
}

- (void)applicationDidBecomeActive:(NSNotification *)aNotification {
Expand Down
2 changes: 2 additions & 0 deletions NotationController.h
Expand Up @@ -137,6 +137,8 @@ typedef struct _NoteCatalogEntry {
- (NoteObject*)addNote:(NSAttributedString*)attributedContents withTitle:(NSString*)title;
- (NoteObject*)addNoteFromCatalogEntry:(NoteCatalogEntry*)catEntry;

- (BOOL)openFiles:(NSArray*)filenames;

- (void)note:(NoteObject*)note didAddLabelSet:(NSSet*)labelSet;
- (void)note:(NoteObject*)note didRemoveLabelSet:(NSSet*)labelSet;

Expand Down
36 changes: 36 additions & 0 deletions NotationController.m
Expand Up @@ -26,6 +26,7 @@
#import "NotationPrefs.h"
#import "NoteAttributeColumn.h"
#import "FrozenNotation.h"
#import "AlienNoteImporter.h"
#import "NotationFileManager.h"
#import "NotationSyncServiceManager.h"
#import "NotationDirectoryManager.h"
Expand Down Expand Up @@ -845,6 +846,40 @@ - (void)note:(NoteObject*)note attributeChanged:(NSString*)attribute {
[delegate titleUpdatedForNote:note];
}

- (BOOL)openFiles:(NSArray*)filenames {
//reveal notes that already exist with any of these filenames
//for paths left over that weren't in the notes-folder/database, import those files as new notes

if (![filenames count]) return NO;

NSArray *unknownPaths = filenames; //(this is not a requirement for -notesWithFilenames:unknownFiles:)

if ([self currentNoteStorageFormat] != SingleDatabaseFormat) {
//notes are stored as separate files, so if these paths are in the notes folder then NV can claim ownership over them

//probably should sync directory here to make sure notesWithFilenames has the freshest data
[self synchronizeNotesFromDirectory];

NSSet *existingNotes = [self notesWithFilenames:filenames unknownFiles:&unknownPaths];
if ([existingNotes count] > 1) {
[delegate notation:self revealNotes:[existingNotes allObjects]];
return YES;
} else if ([existingNotes count] == 1) {
[delegate notation:self revealNote:[existingNotes anyObject] options:NVEditNoteToReveal];
return YES;
}
}
//NSLog(@"paths not found in DB: %@", unknownPaths);
NSArray *createdNotes = [[[[AlienNoteImporter alloc] initWithStoragePaths:unknownPaths] autorelease] importedNotes];
if (!createdNotes) return NO;

[self addNotes:createdNotes];

return YES;
}



- (void)scheduleUpdateListForAttribute:(NSString*)attribute {

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(scheduleUpdateListForAttribute:) object:attribute];
Expand Down Expand Up @@ -1057,6 +1092,7 @@ - (NoteObject*)noteForUUIDBytes:(CFUUIDBytes*)bytes {
return nil;
}


//re-searching for all notes each time a label is added or removed is unnecessary, I think
- (void)note:(NoteObject*)note didAddLabelSet:(NSSet*)labelSet {
[labelsListController addLabelSet:labelSet toNote:note];
Expand Down
2 changes: 2 additions & 0 deletions NotationDirectoryManager.h
Expand Up @@ -27,6 +27,8 @@ NSInteger compareCatalogValueNodeID(id *a, id *b);
NSInteger compareCatalogValueFileSize(id *a, id *b);
void NotesDirFNSubscriptionProc(FNMessage message, OptionBits flags, void * refcon, FNSubscriptionRef subscription);

- (NSSet*)notesWithFilenames:(NSArray*)filenames unknownFiles:(NSArray**)unknownFiles;

- (BOOL)_readFilesInDirectory;
- (BOOL)modifyNoteIfNecessary:(NoteObject*)aNoteObject usingCatalogEntry:(NoteCatalogEntry*)catEntry;
- (void)makeNotesMatchCatalogEntries:(NoteCatalogEntry**)catEntriesPtrs ofSize:(size_t)catCount;
Expand Down
31 changes: 31 additions & 0 deletions NotationDirectoryManager.m
Expand Up @@ -48,6 +48,37 @@ NSInteger compareCatalogValueFileSize(id *a, id *b) {
}


//used to find notes corresponding to a group of existing files in the notes dir, with the understanding
//that the files' contents are up-to-date and the filename property of the note objs is also up-to-date
//e.g. caller should know that if notes are stored as a single DB, then the file could still be out-of-date
- (NSSet*)notesWithFilenames:(NSArray*)filenames unknownFiles:(NSArray**)unknownFiles {
//intersects a list of filenames with the current set of available notes

NSUInteger i = 0;

NSMutableDictionary *lcNamesDict = [NSMutableDictionary dictionaryWithCapacity:[filenames count]];
for (i=0; i<[filenames count]; i++) {
NSString *path = [filenames objectAtIndex:i];
//assume that paths are of NSFileManager origin, not Carbon File Manager
//(note filenames are derived with the expectation of matching against Carbon File Manager)
[lcNamesDict setObject:path forKey:[[[path lastPathComponent] lowercaseString] stringByReplacingOccurrencesOfString:@":" withString:@"/"]];
}

NSMutableSet *foundNotes = [NSMutableSet setWithCapacity:[filenames count]];

for (i=0; i<[allNotes count]; i++) {
NoteObject *aNote = [allNotes objectAtIndex:i];
NSString *existingRequestedFilename = [filenameOfNote(aNote) lowercaseString];
if (existingRequestedFilename && [lcNamesDict objectForKey:existingRequestedFilename]) {
[foundNotes addObject:aNote];
//remove paths from the dict as they are matched to existing notes; those left over will be new ("unknown") files
[lcNamesDict removeObjectForKey:existingRequestedFilename];
}
}
if (unknownFiles) *unknownFiles = [lcNamesDict allValues];
return foundNotes;
}


- (void)stopFileNotifications {
OSStatus err = noErr;
Expand Down

0 comments on commit e73eadc

Please sign in to comment.