2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2008-2022 Transmission authors and contributors.
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-05-22 18:39:49 +00:00
|
|
|
|
2022-06-29 00:15:52 +00:00
|
|
|
@import AppKit;
|
|
|
|
|
2008-05-22 18:39:49 +00:00
|
|
|
#import "FileListNode.h"
|
|
|
|
|
2022-01-24 01:32:45 +00:00
|
|
|
@interface FileListNode ()
|
2008-05-22 18:39:49 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@property(nonatomic, readonly) NSMutableIndexSet* indexesInternal;
|
|
|
|
@property(nonatomic) NSImage* iconInternal;
|
|
|
|
|
2008-05-22 18:39:49 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation FileListNode
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithFolderName:(NSString*)name path:(NSString*)path torrent:(Torrent*)torrent
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((self = [self initWithFolder:YES name:name path:path torrent:torrent]))
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
2021-01-18 02:52:38 +00:00
|
|
|
_children = [[NSMutableArray alloc] init];
|
|
|
|
_size = 0;
|
2008-05-22 18:39:49 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-05-22 18:39:49 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithFileName:(NSString*)name
|
|
|
|
path:(NSString*)path
|
|
|
|
size:(uint64_t)size
|
|
|
|
index:(NSUInteger)index
|
|
|
|
torrent:(Torrent*)torrent
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ((self = [self initWithFolder:NO name:name path:path torrent:torrent]))
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
2021-01-18 02:52:38 +00:00
|
|
|
_size = size;
|
2022-02-22 16:04:20 +00:00
|
|
|
[_indexesInternal addIndex:index];
|
2008-05-22 18:39:49 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-05-22 18:39:49 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)insertChild:(FileListNode*)child
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
2021-01-18 02:52:38 +00:00
|
|
|
NSAssert(_isFolder, @"method can only be invoked on folders");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[_children addObject:child];
|
2008-05-22 18:39:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)insertIndex:(NSUInteger)index withSize:(uint64_t)size
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
2021-01-18 02:52:38 +00:00
|
|
|
NSAssert(_isFolder, @"method can only be invoked on folders");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
[self.indexesInternal addIndex:index];
|
2021-01-18 02:52:38 +00:00
|
|
|
_size += size;
|
2008-05-22 18:39:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (id)copyWithZone:(NSZone*)zone
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
2008-05-22 21:06:56 +00:00
|
|
|
//this object is essentially immutable after initial setup
|
2017-07-29 16:14:22 +00:00
|
|
|
return self;
|
2008-05-22 18:39:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)description
|
2011-05-21 18:29:35 +00:00
|
|
|
{
|
2021-01-18 02:52:38 +00:00
|
|
|
if (!_isFolder)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return [NSString stringWithFormat:@"%@ (%ld)", _name, _indexesInternal.firstIndex];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2011-05-21 18:29:35 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return [NSString stringWithFormat:@"%@ (folder: %@)", _name, _indexesInternal];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2011-05-21 18:29:35 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSImage*)icon
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!_iconInternal)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-03-30 21:52:23 +00:00
|
|
|
_iconInternal = [NSWorkspace.sharedWorkspace
|
|
|
|
iconForFileType:_isFolder ? NSFileTypeForHFSTypeCode(kGenericFolderIcon) : _name.pathExtension];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2022-02-22 16:04:20 +00:00
|
|
|
return _iconInternal;
|
2021-01-18 02:52:38 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSIndexSet*)indexes
|
2021-01-18 02:52:38 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
return _indexesInternal;
|
2008-05-22 18:39:49 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)updateFromOldName:(NSString*)oldName toNewName:(NSString*)newName inPath:(NSString*)path
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
|
|
|
NSParameterAssert(oldName != nil);
|
|
|
|
NSParameterAssert(newName != nil);
|
|
|
|
NSParameterAssert(path != nil);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSArray* lookupPathComponents = path.pathComponents;
|
|
|
|
NSArray* thesePathComponents = self.path.pathComponents;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([lookupPathComponents isEqualToArray:thesePathComponents]) //this node represents what's being renamed
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([oldName isEqualToString:self.name])
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
2021-01-18 02:52:38 +00:00
|
|
|
_name = [newName copy];
|
2022-02-22 16:04:20 +00:00
|
|
|
_iconInternal = nil;
|
2013-01-22 00:09:48 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
2021-08-07 07:27:56 +00:00
|
|
|
else if (lookupPathComponents.count < thesePathComponents.count) //what's being renamed is part of this node's path
|
2013-01-22 00:09:48 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
lookupPathComponents = [lookupPathComponents arrayByAddingObject:oldName];
|
|
|
|
BOOL const allSame = NSNotFound ==
|
|
|
|
[lookupPathComponents indexOfObjectWithOptions:NSEnumerationConcurrent
|
|
|
|
passingTest:^BOOL(NSString* name, NSUInteger idx, BOOL* stop) {
|
|
|
|
return ![name isEqualToString:thesePathComponents[idx]];
|
|
|
|
}];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
if (allSame)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* oldPathPrefix = [path stringByAppendingPathComponent:oldName];
|
|
|
|
NSString* newPathPrefix = [path stringByAppendingPathComponent:newName];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
_path = [_path stringByReplacingCharactersInRange:NSMakeRange(0, oldPathPrefix.length) withString:newPathPrefix];
|
2013-01-22 00:09:48 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2013-01-22 00:09:48 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
#pragma mark - Private
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)initWithFolder:(BOOL)isFolder name:(NSString*)name path:(NSString*)path torrent:(Torrent*)torrent
|
2008-05-22 18:39:49 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init]))
|
|
|
|
{
|
2021-01-18 02:52:38 +00:00
|
|
|
_isFolder = isFolder;
|
|
|
|
_name = [name copy];
|
|
|
|
_path = [path copy];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_indexesInternal = [[NSMutableIndexSet alloc] init];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-01-18 02:52:38 +00:00
|
|
|
_torrent = torrent;
|
2008-05-22 18:39:49 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-05-22 18:39:49 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|