1/* Definition of class NSFileVersion 2 Copyright (C) 2019 Free Software Foundation, Inc. 3 4 Implemented by: Gregory Casamento <greg.casamento@gmail.com> 5 Date: Sep 2019 6 Original File by: Daniel Ferreira 7 8 This file is part of the GNUstep Library. 9 10 This library is free software; you can redistribute it and/or 11 modify it under the terms of the GNU Lesser General Public 12 License as published by the Free Software Foundation; either 13 version 2 of the License, or (at your option) any later version. 14 15 This library is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 Lesser General Public License for more details. 19 20 You should have received a copy of the GNU Lesser General Public 21 License along with this library; if not, write to the Free 22 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 23 Boston, MA 02110 USA. 24*/ 25 26#include <Foundation/NSFileVersion.h> 27#include <Foundation/NSArray.h> 28#include <Foundation/NSDictionary.h> 29#include <Foundation/NSDate.h> 30#include <Foundation/NSError.h> 31#include <Foundation/NSString.h> 32#include <Foundation/NSURL.h> 33#include <Foundation/NSPersonNameComponents.h> 34#include <Foundation/NSFileManager.h> 35#include <Foundation/NSData.h> 36 37@interface NSFileVersion (Private) 38- (void) _setURL: (NSURL *)u; 39- (void) _setContentsURL: (NSURL *)u; 40- (void) _setConflict: (BOOL)f; 41- (void) _setLocalizedName: (NSString *)name; 42- (void) _initWithURL: (NSURL *)url; 43@end 44 45@implementation NSFileVersion (Private) 46- (void) _setURL: (NSURL *)u 47{ 48 ASSIGNCOPY(_fileURL, u); 49} 50 51- (void) _setContentsURL: (NSURL *)u 52{ 53 ASSIGNCOPY(_contentsURL, u); 54} 55 56- (void) _setConflict: (BOOL)f 57{ 58 _conflict = f; 59} 60 61- (void) _setLocalizedName: (NSString *)name 62{ 63 ASSIGNCOPY(_localizedName, name); 64} 65 66- (void) _initWithURL: (NSURL *)url 67{ 68 [self _setURL: url]; 69 [self _setContentsURL: url]; 70 [self _setConflict: NO]; 71 [self _setLocalizedName: [url path]]; 72 [self setDiscardable: NO]; 73 [self setResolved: YES]; 74} 75@end 76 77@implementation NSFileVersion 78 79// Initializers 80+ (NSFileVersion *)currentVersionOfItemAtURL: (NSURL *)url 81{ 82 NSFileVersion *fileVersion = AUTORELEASE([[NSFileVersion alloc] init]); 83 if (fileVersion != nil) 84 { 85 [fileVersion _initWithURL: url]; 86 } 87 return fileVersion; 88} 89 90+ (NSArray *)otherVersionsOfItemAtURL: (NSURL *)url 91{ 92 NSArray *array = AUTORELEASE([[NSArray alloc] init]); 93 return array; 94} 95 96+ (NSFileVersion *)versionOfItemAtURL: (NSURL *)url 97 forPersistentIdentifier: (id)persistentIdentifier 98{ 99 return [NSFileVersion currentVersionOfItemAtURL: url]; 100} 101 102+ (NSURL *)temporaryDirectoryURLForNewVersionOfItemAtURL: (NSURL *)url 103{ 104 return nil; 105} 106 107+ (NSFileVersion *)addVersionOfItemAtURL: (NSURL *)url 108 withContentsOfURL: (NSURL *)contentsURL 109 options: (NSFileVersionAddingOptions)options 110 error: (NSError **)outError 111{ 112 NSFileVersion *fileVersion = AUTORELEASE([[NSFileVersion alloc] init]); 113 if (fileVersion != nil) 114 { 115 NSData *data = [NSData dataWithContentsOfURL: contentsURL]; 116 NSFileManager *mgr = [NSFileManager defaultManager]; 117 118 [fileVersion _initWithURL: url]; 119 120 // Create new file... 121 [mgr createFileAtPath: [url path] 122 contents: data 123 attributes: nil]; 124 } 125 return fileVersion; 126} 127 128+ (NSArray *)unresolvedConflictVersionsOfItemAtURL: (NSURL *)url 129{ 130 return nil; 131} 132 133+ (BOOL)removeOtherVersionsOfItemAtURL: (NSURL *)url 134 error: (NSError **)outError 135{ 136 NSFileManager *mgr = [NSFileManager defaultManager]; 137 return [mgr removeItemAtPath: [url path] error: outError]; 138} 139 140// Instance methods... 141- (instancetype) init 142{ 143 self = [super init]; 144 if(self != nil) 145 { 146 _isDiscardable = NO; 147 _isResolved = NO; 148 _modificationDate = [[NSDate alloc] init]; 149 _fileURL = nil; 150 _contentsURL = nil; 151 _persistentIdentifier = nil; 152 _nonLocalVersion = nil; 153 _hasThumbnail = NO; 154 _hasLocalContents = YES; 155 _conflict = NO; 156 _localizedName = nil; 157 _localizedNameOfSavingComputer = nil; 158 } 159 return self; 160} 161 162- (BOOL) isDiscardable 163{ 164 return _isDiscardable; 165} 166 167- (void) setDiscardable: (BOOL)flag 168{ 169 _isDiscardable = flag; 170} 171 172- (BOOL) isResolved 173{ 174 return _isResolved; 175} 176 177- (void) setResolved: (BOOL)flag 178{ 179 _isResolved = flag; 180} 181 182- (NSDate *) modificationDate 183{ 184 return _modificationDate; 185} 186 187- (NSPersonNameComponents *) originatorNameComponents 188{ 189 return nil; 190} 191 192- (NSString *) localizedName 193{ 194 return _localizedName; 195} 196 197- (NSString *) localizedNameOfSavingComputer 198{ 199 return _localizedNameOfSavingComputer; 200} 201 202- (BOOL) hasLocalContents 203{ 204 return _hasLocalContents; 205} 206 207- (BOOL) hasThumbnail 208{ 209 return _hasThumbnail; 210} 211 212- (NSURL *) URL 213{ 214 return _fileURL; 215} 216 217- (BOOL) conflict 218{ 219 return _conflict; 220} 221 222- (id<NSCoding>) persistentIdentifier 223{ 224 return _persistentIdentifier; 225} 226 227- (BOOL) removeAndReturnError: (NSError **)outError 228{ 229 NSURL *url = [self URL]; 230 NSFileManager *mgr = [NSFileManager defaultManager]; 231 return [mgr removeItemAtPath: [url path] error: outError]; 232} 233 234- (NSURL *) replaceItemAtURL: (NSURL *)url 235 options: (NSFileVersionReplacingOptions)options 236 error: (NSError **)error 237{ 238 NSFileManager *mgr = [NSFileManager defaultManager]; 239 if([mgr removeItemAtPath: [url path] error: error]) 240 { 241 NSData *data = [NSData dataWithContentsOfURL: _contentsURL]; 242 NSFileManager *mgr = [NSFileManager defaultManager]; 243 244 // Create new file... 245 [mgr createFileAtPath: [url path] 246 contents: data 247 attributes: nil]; 248 249 } 250 return url; 251} 252 253@end 254