libs-base/Source/NSFileVersion.m

253 lines
5.7 KiB
Mathematica
Raw Permalink Normal View History

2019-09-23 06:51:41 +00:00
/* Definition of class NSFileVersion
Copyright (C) 2019 Free Software Foundation, Inc.
Implemented by: Gregory Casamento <greg.casamento@gmail.com>
Date: Sep 2019
Original File by: Daniel Ferreira
This file is part of the GNUstep Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
2019-09-23 06:51:41 +00:00
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
2019-09-23 06:51:41 +00:00
*/
#import "Foundation/NSFileVersion.h"
#import "Foundation/NSArray.h"
#import "Foundation/NSDictionary.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSError.h"
#import "Foundation/NSString.h"
#import "Foundation/NSURL.h"
#import "Foundation/NSPersonNameComponents.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSData.h"
2019-09-23 06:51:41 +00:00
2019-09-24 06:00:21 +00:00
@interface NSFileVersion (Private)
- (void) _setURL: (NSURL *)u;
2019-09-24 06:04:53 +00:00
- (void) _setContentsURL: (NSURL *)u;
- (void) _setConflict: (BOOL)f;
2019-09-24 06:44:07 +00:00
- (void) _setLocalizedName: (NSString *)name;
2019-09-25 05:28:20 +00:00
- (void) _initWithURL: (NSURL *)url;
2019-09-24 06:00:21 +00:00
@end
@implementation NSFileVersion (Private)
- (void) _setURL: (NSURL *)u
{
ASSIGNCOPY(_fileURL, u);
}
- (void) _setContentsURL: (NSURL *)u
{
ASSIGNCOPY(_contentsURL, u);
}
2019-09-24 06:04:53 +00:00
- (void) _setConflict: (BOOL)f
{
_conflict = f;
}
2019-09-24 06:44:07 +00:00
- (void) _setLocalizedName: (NSString *)name
{
ASSIGNCOPY(_localizedName, name);
}
2019-09-25 05:28:20 +00:00
- (void) _initWithURL: (NSURL *)url
{
[self _setURL: url];
[self _setContentsURL: url];
[self _setConflict: NO];
[self _setLocalizedName: [url path]];
[self setDiscardable: NO];
[self setResolved: YES];
}
2019-09-24 06:00:21 +00:00
@end
@implementation NSFileVersion
2019-09-15 05:13:21 +00:00
2019-09-23 06:51:41 +00:00
// Initializers
+ (NSFileVersion *)currentVersionOfItemAtURL: (NSURL *)url
{
2019-09-24 06:00:21 +00:00
NSFileVersion *fileVersion = AUTORELEASE([[NSFileVersion alloc] init]);
if (fileVersion != nil)
{
2019-09-25 05:28:20 +00:00
[fileVersion _initWithURL: url];
2019-09-24 06:00:21 +00:00
}
return fileVersion;
2019-09-23 06:51:41 +00:00
}
+ (NSArray *)otherVersionsOfItemAtURL: (NSURL *)url
{
2019-09-25 05:28:20 +00:00
NSArray *array = AUTORELEASE([[NSArray alloc] init]);
return array;
2019-09-23 06:51:41 +00:00
}
+ (NSFileVersion *)versionOfItemAtURL: (NSURL *)url
forPersistentIdentifier: (id)persistentIdentifier
{
2019-09-24 06:44:07 +00:00
return [NSFileVersion currentVersionOfItemAtURL: url];
2019-09-23 06:51:41 +00:00
}
+ (NSURL *)temporaryDirectoryURLForNewVersionOfItemAtURL: (NSURL *)url
{
return nil;
}
+ (NSFileVersion *)addVersionOfItemAtURL: (NSURL *)url
withContentsOfURL: (NSURL *)contentsURL
options: (NSFileVersionAddingOptions)options
error: (NSError **)outError
{
2019-09-24 07:12:35 +00:00
NSFileVersion *fileVersion = AUTORELEASE([[NSFileVersion alloc] init]);
if (fileVersion != nil)
{
NSData *data = [NSData dataWithContentsOfURL: contentsURL];
NSFileManager *mgr = [NSFileManager defaultManager];
2019-09-25 05:28:20 +00:00
[fileVersion _initWithURL: url];
2019-09-24 07:12:35 +00:00
// Create new file...
[mgr createFileAtPath: [url path]
contents: data
attributes: nil];
}
return fileVersion;
2019-09-23 06:51:41 +00:00
}
+ (NSArray *)unresolvedConflictVersionsOfItemAtURL: (NSURL *)url
{
return nil;
}
+ (BOOL)removeOtherVersionsOfItemAtURL: (NSURL *)url
error: (NSError **)outError
{
2019-09-24 06:44:07 +00:00
NSFileManager *mgr = [NSFileManager defaultManager];
return [mgr removeItemAtPath: [url path] error: outError];
2019-09-23 06:51:41 +00:00
}
// Instance methods...
2019-09-24 04:09:42 +00:00
- (instancetype) init
{
self = [super init];
if(self != nil)
{
_isDiscardable = NO;
_isResolved = NO;
_modificationDate = [[NSDate alloc] init];
_fileURL = nil;
_contentsURL = nil;
_persistentIdentifier = nil;
_nonLocalVersion = nil;
_hasThumbnail = NO;
_hasLocalContents = YES;
_conflict = NO;
_localizedName = nil;
_localizedNameOfSavingComputer = nil;
}
return self;
}
2019-09-15 05:13:21 +00:00
- (BOOL) isDiscardable
{
return _isDiscardable;
}
2019-09-25 05:28:20 +00:00
2019-09-15 05:13:21 +00:00
- (void) setDiscardable: (BOOL)flag
{
_isDiscardable = flag;
}
- (BOOL) isResolved
{
return _isResolved;
}
- (void) setResolved: (BOOL)flag
{
_isResolved = flag;
}
- (NSDate *) modificationDate
{
return _modificationDate;
}
- (NSPersonNameComponents *) originatorNameComponents
{
return nil;
}
- (NSString *) localizedName
{
return _localizedName;
}
- (NSString *) localizedNameOfSavingComputer
{
return _localizedNameOfSavingComputer;
}
- (BOOL) hasLocalContents
{
return _hasLocalContents;
}
- (BOOL) hasThumbnail
{
return _hasThumbnail;
}
- (NSURL *) URL
{
return _fileURL;
}
- (BOOL) conflict
{
return _conflict;
}
- (id<NSCoding>) persistentIdentifier
{
2019-09-23 06:51:41 +00:00
return _persistentIdentifier;
2019-09-15 05:13:21 +00:00
}
- (BOOL) removeAndReturnError: (NSError **)outError
{
2019-09-24 06:58:12 +00:00
NSURL *url = [self URL];
NSFileManager *mgr = [NSFileManager defaultManager];
return [mgr removeItemAtPath: [url path] error: outError];
2019-09-15 05:13:21 +00:00
}
2019-09-15 15:17:26 +00:00
- (NSURL *) replaceItemAtURL: (NSURL *)url
options: (NSFileVersionReplacingOptions)options
error: (NSError **)error
2019-09-15 05:13:21 +00:00
{
2019-09-25 05:28:20 +00:00
NSFileManager *mgr = [NSFileManager defaultManager];
if([mgr removeItemAtPath: [url path] error: error])
{
NSData *data = [NSData dataWithContentsOfURL: _contentsURL];
NSFileManager *mgr = [NSFileManager defaultManager];
// Create new file...
[mgr createFileAtPath: [url path]
contents: data
attributes: nil];
}
return url;
2019-09-15 05:13:21 +00:00
}
@end