More data link implementation details.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@20850 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Gregory John Casamento 2005-03-05 15:50:32 +00:00
parent 288beea76f
commit 45f40695b6
8 changed files with 329 additions and 93 deletions

View file

@ -779,7 +779,7 @@ NSDrawWindowBackground(NSRect aRect)
float
NSLinkFrameThickness(void)
{
return 1.5;
return 1;
}
void

View file

@ -1,7 +1,9 @@
/** <title>NSDataLink</title>
Copyright (C) 1996 Free Software Foundation, Inc.
Copyright (C) 1996, 2005 Free Software Foundation, Inc.
Author: Gregory John Casamento <greg_casamento@yahoo.com>
Date: 2005
Author: Scott Christley <scottc@net-community.com>
Date: 1996
@ -24,29 +26,17 @@
*/
#include "config.h"
#include <Foundation/NSFileManager.h>
#include <Foundation/NSArchiver.h>
#include <Foundation/NSData.h>
#include "AppKit/NSDataLink.h"
#include "AppKit/NSDataLinkManager.h"
#include "AppKit/NSPasteboard.h"
#include "AppKit/NSSavePanel.h"
#include "AppKit/NSSelection.h"
@interface NSDataLink (Private)
- (void) setLastUpdateTime: (NSDate *)date;
- (void) setSourceFilename: (NSString *)src;
- (void) setDestinationFilename: (NSString *)src;
@end
@implementation NSDataLink (Private)
- (void) setLastUpdateTime: (NSDate *)date
{
ASSIGN(lastUpdateTime, date);
}
- (void) setSourceFilename: (NSString *)src
{
ASSIGN(sourceFilename,src);
}
- (void) setDestinationFilename: (NSString *)dst
{
ASSIGN(destinationFilename, dst);
}
@end
// constants
NSString *NSDataLinkFilenameExtension = @"dlink";
@implementation NSDataLink
@ -58,7 +48,7 @@
if (self == [NSDataLink class])
{
// Initial version
[self setVersion: 1];
[self setVersion: 0];
}
}
@ -70,6 +60,12 @@
//
- (id)initLinkedToFile:(NSString *)filename
{
if((self = [self init]) != nil)
{
NSData *data = [NSData dataWithBytes: [filename cString] length: [filename cStringLength]];
NSSelection *selection = [NSSelection selectionWithDescriptionData: data];
ASSIGN(sourceSelection, selection);
}
return nil;
}
@ -80,7 +76,7 @@
if((self = [self init]) != nil)
{
ASSIGN(sourceSelection,selection);
ASSIGN(manager,linkManager);
ASSIGN(sourceManager,linkManager);
ASSIGN(types,newTypes);
}
return self;
@ -88,12 +84,18 @@
- (id)initWithContentsOfFile:(NSString *)filename
{
return nil;
NSData *data = AUTORELEASE([[NSData alloc] initWithContentsOfFile: filename]);
id object = [NSUnarchiver unarchiveObjectWithData: data];
RELEASE(self);
return object;
}
- (id)initWithPasteboard:(NSPasteboard *)pasteboard
{
return nil;
NSData *data = [pasteboard dataForType: NSDataLinkPboardType];
id object = [NSUnarchiver unarchiveObjectWithData: data];
RELEASE(self);
return object;
}
//
@ -101,16 +103,48 @@
//
- (BOOL)saveLinkIn:(NSString *)directoryName
{
NSSavePanel *sp;
int result;
sp = [NSSavePanel savePanel];
[sp setRequiredFileType: NSDataLinkFilenameExtension];
result = [sp runModalForDirectory: directoryName file: @""];
if (result == NSOKButton)
{
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *path = [sp filename];
if ([mgr fileExistsAtPath: path] == YES)
{
/* NSSavePanel has already asked if it's ok to replace */
NSString *bPath = [path stringByAppendingString: @"~"];
[mgr removeFileAtPath: bPath handler: nil];
[mgr movePath: path toPath: bPath handler: nil];
}
// save it.
return [self writeToFile: path];
}
return NO;
}
- (BOOL)writeToFile:(NSString *)filename
{
return NO;
NSString *path = filename;
if([[path pathExtension] isEqual: NSDataLinkFilenameExtension] == NO)
{
path = [filename stringByAppendingPathExtension: NSDataLinkFilenameExtension];
}
return [NSArchiver archiveRootObject: self toFile: path];
}
- (void)writeToPasteboard:(NSPasteboard *)pasteboard
{
NSData *data = [NSArchiver archivedDataWithRootObject: self];
[pasteboard setData: data forType: NSDataLinkPboardType];
}
//
@ -128,7 +162,7 @@
- (NSDataLinkManager *)manager
{
return manager;
return sourceManager;
}
//
@ -187,11 +221,34 @@
//
- (BOOL)break
{
return NO;
id srcDelegate = [sourceManager delegate];
id dstDelegate = [destinationManager delegate];
// The spec is quite vague here. I don't know under what
// circumstances a link cannot be broken, so this method
// always returns YES.
if([srcDelegate respondsToSelector: @selector(dataLinkManager:didBreakLink:)])
{
[srcDelegate dataLinkManager: sourceManager didBreakLink: self];
}
if([dstDelegate respondsToSelector: @selector(dataLinkManager:didBreakLink:)])
{
[dstDelegate dataLinkManager: destinationManager didBreakLink: self];
}
return (_flags.broken = YES);
}
- (void)noteSourceEdited
{
_flags.isDirty = YES;
if(updateMode != NSUpdateNever)
{
[sourceManager noteDocumentEdited];
}
}
- (void)setUpdateMode:(NSDataLinkUpdateMode)mode
@ -214,42 +271,80 @@
//
- (void) encodeWithCoder: (NSCoder*)aCoder
{
BOOL flag = NO;
[aCoder encodeValueOfObjCType: @encode(int) at: &linkNumber];
[aCoder encodeValueOfObjCType: @encode(int) at: &disposition];
[aCoder encodeValueOfObjCType: @encode(int) at: &updateMode];
[aCoder encodeValueOfObjCType: @encode(id) at: &manager];
[aCoder encodeValueOfObjCType: @encode(id) at: &lastUpdateTime];
[aCoder encodeValueOfObjCType: @encode(id) at: &sourceApplicationName];
[aCoder encodeValueOfObjCType: @encode(id) at: &sourceFilename];
[aCoder encodeValueOfObjCType: @encode(id) at: &sourceSelection];
[aCoder encodeValueOfObjCType: @encode(id) at: &types];
[aCoder encodeValueOfObjCType: @encode(id) at: &sourceManager];
[aCoder encodeValueOfObjCType: @encode(id) at: &destinationApplicationName];
[aCoder encodeValueOfObjCType: @encode(id) at: &destinationFilename];
[aCoder encodeValueOfObjCType: @encode(id) at: &destinationSelection];
[aCoder encodeValueOfObjCType: @encode(id) at: &destinationManager];
[aCoder encodeValueOfObjCType: @encode(id) at: &types];
// flags...
flag = _flags.appVerifies;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.canUpdateContinuously;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.isDirty;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.willOpenSource;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.willUpdate;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
}
- (id) initWithCoder: (NSCoder*)aCoder
{
int version = [aCoder versionForClassName: @"NSDataLink"];
if(version == 1)
if(version == 0)
{
BOOL flag = NO;
[aCoder decodeValueOfObjCType: @encode(int) at: &linkNumber];
[aCoder decodeValueOfObjCType: @encode(int) at: &disposition];
[aCoder decodeValueOfObjCType: @encode(int) at: &updateMode];
[aCoder decodeValueOfObjCType: @encode(id) at: &manager];
[aCoder decodeValueOfObjCType: @encode(id) at: &sourceManager];
[aCoder encodeValueOfObjCType: @encode(id) at: &destinationManager];
[aCoder decodeValueOfObjCType: @encode(id) at: &lastUpdateTime];
[aCoder decodeValueOfObjCType: @encode(id) at: &sourceApplicationName];
[aCoder decodeValueOfObjCType: @encode(id) at: &sourceFilename];
[aCoder decodeValueOfObjCType: @encode(id) at: &sourceSelection];
[aCoder decodeValueOfObjCType: @encode(id) at: &types];
[aCoder decodeValueOfObjCType: @encode(id) at: &sourceManager];
[aCoder decodeValueOfObjCType: @encode(id) at: &destinationApplicationName];
[aCoder decodeValueOfObjCType: @encode(id) at: &destinationFilename];
[aCoder decodeValueOfObjCType: @encode(id) at: &destinationSelection];
[aCoder decodeValueOfObjCType: @encode(id) at: &destinationManager];
[aCoder decodeValueOfObjCType: @encode(id) at: &types];
// flags...
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.appVerifies = flag;
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.canUpdateContinuously = flag;
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.isDirty = flag;
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.willOpenSource = flag;
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.willUpdate = flag;
}
else
{
NSLog(@"No decoder for NSDataLink version #%d",version);
return nil;
}
return self;

View file

@ -1,7 +1,9 @@
/** <title>NSDataLinkManager</title>
Copyright (C) 1996 Free Software Foundation, Inc.
Copyright (C) 1996, 2005 Free Software Foundation, Inc.
Author: Gregory John Casamento <greg_casamento@yahoo.com>
Date: 2005
Author: Scott Christley <scottc@net-community.com>
Date: 1996
@ -26,9 +28,58 @@
#include "config.h"
#include <Foundation/NSDictionary.h>
#include <Foundation/NSEnumerator.h>
#include <Foundation/NSArray.h>
#include <AppKit/NSDataLinkManager.h>
#include <AppKit/NSDataLink.h>
@interface NSDataLink (Private)
- (void) setLastUpdateTime: (NSDate *)date;
- (void) setSourceFilename: (NSString *)src;
- (void) setDestinationFilename: (NSString *)dst;
- (void) setSourceManager: (id)src;
- (void) setDestinationManager: (id)dst;
- (void) setSourceSelection: (id)src;
- (void) setDestinationSelection: (id)dst;
@end
@implementation NSDataLink (Private)
- (void) setLastUpdateTime: (NSDate *)date
{
ASSIGN(lastUpdateTime, date);
}
- (void) setSourceFilename: (NSString *)src
{
ASSIGN(sourceFilename,src);
}
- (void) setDestinationFilename: (NSString *)dst
{
ASSIGN(destinationFilename, dst);
}
- (void) setSourceManager: (id)src
{
ASSIGN(sourceManager,src);
}
- (void) setDestinationManager: (id)dst
{
ASSIGN(destinationManager,dst);
}
- (void) setSourceSelection: (id)src
{
ASSIGN(sourceSelection,src);
}
- (void) setDestinationSelection: (id)dst
{
ASSIGN(destinationSelection,dst);
}
@end
@implementation NSDataLinkManager
//
@ -39,7 +90,7 @@
if (self == [NSDataLinkManager class])
{
// Initial version
[self setVersion:1];
[self setVersion: 0];
}
}
@ -53,12 +104,15 @@
{
self = [super init];
ASSIGN(delegate,anObject);
filename = nil;
delegateVerifiesLinks = NO;
interactsWithUser = NO;
isEdited = NO;
areLinkOutlinesVisible = NO;
if(self != nil)
{
ASSIGN(delegate,anObject);
filename = nil;
_flags.delegateVerifiesLinks = NO;
_flags.interactsWithUser = NO;
_flags.isEdited = NO;
_flags.areLinkOutlinesVisible = NO;
}
return self;
}
@ -68,12 +122,15 @@
{
self = [super init];
ASSIGN(delegate,anObject);
ASSIGN(filename,path);
delegateVerifiesLinks = NO;
interactsWithUser = NO;
isEdited = NO;
areLinkOutlinesVisible = NO;
if(self != nil)
{
ASSIGN(delegate,anObject);
ASSIGN(filename,path);
_flags.delegateVerifiesLinks = NO;
_flags.interactsWithUser = NO;
_flags.isEdited = NO;
_flags.areLinkOutlinesVisible = NO;
}
return self;
}
@ -84,7 +141,18 @@
- (BOOL)addLink:(NSDataLink *)link
at:(NSSelection *)selection
{
return NO;
BOOL result = NO;
[link setDestinationSelection: selection];
[link setDestinationManager: self];
if([destinationLinks containsObject: link] == NO)
{
[destinationLinks addObject: link];
result = YES;
}
return result;
}
- (BOOL)addLinkAsMarker:(NSDataLink *)link
@ -102,23 +170,26 @@
- (void)breakAllLinks
{
NSEnumerator *en = [self destinationLinkEnumerator];
NSArray *allLinks = [sourceLinks arrayByAddingObjectsFromArray: destinationLinks];
NSEnumerator *en = [allLinks objectEnumerator];
id obj = nil;
while((obj = [en nextObject]) != nil)
{
[obj break];
}
en = [self sourceLinkEnumerator];
while((obj = [en nextObject]) != nil)
{
[obj break];
}
}
- (void)writeLinksToPasteboard:(NSPasteboard *)pasteboard
{
NSArray *allLinks = [sourceLinks arrayByAddingObjectsFromArray: destinationLinks];
NSEnumerator *en = [allLinks objectEnumerator];
id obj = nil;
while((obj = [en nextObject]) != nil)
{
[obj writeToPasteboard: pasteboard];
}
}
//
@ -158,7 +229,7 @@
- (BOOL)delegateVerifiesLinks
{
return delegateVerifiesLinks;
return _flags.delegateVerifiesLinks;
}
- (NSString *)filename
@ -168,22 +239,22 @@
- (BOOL)interactsWithUser
{
return interactsWithUser;
return _flags.interactsWithUser;
}
- (BOOL)isEdited
{
return isEdited;
return _flags.isEdited;
}
- (void)setDelegateVerifiesLinks:(BOOL)flag
{
delegateVerifiesLinks = flag;
_flags.delegateVerifiesLinks = flag;
}
- (void)setInteractsWithUser:(BOOL)flag
{
interactsWithUser = flag;
_flags.interactsWithUser = flag;
}
//
@ -191,12 +262,12 @@
//
- (BOOL)areLinkOutlinesVisible
{
return areLinkOutlinesVisible;
return _flags.areLinkOutlinesVisible;
}
- (NSEnumerator *)destinationLinkEnumerator
{
return [destinationLinks keyEnumerator];
return [destinationLinks objectEnumerator];
}
- (NSDataLink *)destinationLinkWithSelection:(NSSelection *)destSel
@ -217,12 +288,12 @@
- (void)setLinkOutlinesVisible:(BOOL)flag
{
areLinkOutlinesVisible = flag;
_flags.areLinkOutlinesVisible = flag;
}
- (NSEnumerator *)sourceLinkEnumerator
{
return [sourceLinks keyEnumerator];
return [sourceLinks objectEnumerator];
}
//
@ -230,10 +301,48 @@
//
- (void) encodeWithCoder: (NSCoder*)aCoder
{
BOOL flag = NO;
[aCoder encodeValueOfObjCType: @encode(id) at: &filename];
[aCoder encodeValueOfObjCType: @encode(id) at: &sourceLinks];
[aCoder encodeValueOfObjCType: @encode(id) at: &destinationLinks];
flag = _flags.areLinkOutlinesVisible;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.delegateVerifiesLinks;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.interactsWithUser;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _flags.isEdited;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
}
- (id) initWithCoder: (NSCoder*)aDecoder
- (id) initWithCoder: (NSCoder*)aCoder
{
int version = [aCoder versionForClassName: @"NSDataLinkManager"];
if(version == 0)
{
BOOL flag = NO;
[aCoder decodeValueOfObjCType: @encode(id) at: &filename];
[aCoder decodeValueOfObjCType: @encode(id) at: &sourceLinks];
[aCoder decodeValueOfObjCType: @encode(id) at: &destinationLinks];
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.areLinkOutlinesVisible = flag;
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.delegateVerifiesLinks = flag;
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.interactsWithUser = flag;
[aCoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
_flags.isEdited = flag;
}
else
{
return nil;
}
return self;
}

View file

@ -104,7 +104,7 @@ static NSDataLinkPanel *_sharedDataLinkPanel;
if (self == [NSDataLinkPanel class])
{
// Initial version
[self setVersion:1];
[self setVersion: 0];
}
}

View file

@ -96,7 +96,7 @@ typedef enum
if (self == [NSSelection class])
{
// Initial version
[self setVersion:1];
[self setVersion: 0];
}
}
@ -226,6 +226,8 @@ typedef enum
at: &_isWellKnownSelection];
[aDecoder decodeValueOfObjCType: @encode(int)
at: &_selectionType];
[aDecoder decodeValueOfObjCType: @encode(id)
at: _descriptionData];
// if it's a well known selection then determine which one it is.
if(_isWellKnownSelection)
@ -249,11 +251,6 @@ typedef enum
break;
}
}
else
{
[aDecoder decodeValueOfObjCType: @encode(id)
at: _descriptionData];
}
return self;
}