source: trunk/macosx/FileListNode.m

Last change on this file was 14036, checked in by livings124, 9 years ago

make the children in a FileListNode? a property

  • Property svn:keywords set to Date Rev Author Id
File size: 5.6 KB
Line 
1/******************************************************************************
2 * $Id: FileListNode.m 14036 2013-02-19 01:29:25Z mikedld $
3 *
4 * Copyright (c) 2008-2012 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "FileListNode.h"
26
27@interface FileListNode (Private)
28
29- (id) initWithFolder: (BOOL) isFolder name: (NSString *) name path: (NSString *) path torrent: (Torrent *) torrent;
30
31@end
32
33@implementation FileListNode
34
35#warning remove ivars in header when 64-bit only (or it compiles in 32-bit mode)
36@synthesize name = fName;
37@synthesize path = fPath;
38@synthesize torrent = fTorrent;
39@synthesize size = fSize;
40@synthesize icon = fIcon;
41@synthesize isFolder = fIsFolder;
42@synthesize indexes = fIndexes;
43@synthesize children = fChildren;
44
45- (id) initWithFolderName: (NSString *) name path: (NSString *) path torrent: (Torrent *) torrent
46{
47    if ((self = [self initWithFolder: YES name: name path: path torrent: torrent]))
48    {
49        fChildren = [[NSMutableArray alloc] init];
50        fSize = 0;
51    }
52   
53    return self;
54}
55
56- (id) initWithFileName: (NSString *) name path: (NSString *) path size: (uint64_t) size index: (NSUInteger) index torrent: (Torrent *) torrent
57{
58    if ((self = [self initWithFolder: NO name: name path: path torrent: torrent]))
59    {
60        fSize = size;
61        [fIndexes addIndex: index];
62    }
63   
64    return self;
65}
66
67- (void) insertChild: (FileListNode *) child
68{
69    NSAssert(fIsFolder, @"method can only be invoked on folders");
70   
71    [fChildren addObject: child];
72}
73
74- (void) insertIndex: (NSUInteger) index withSize: (uint64_t) size
75{
76    NSAssert(fIsFolder, @"method can only be invoked on folders");
77   
78    [fIndexes addIndex: index];
79    fSize += size;
80}
81
82- (id) copyWithZone: (NSZone *) zone
83{
84    //this object is essentially immutable after initial setup
85    return [self retain];
86}
87
88- (void) dealloc
89{
90    [fName release];
91    [fPath release];
92    [fIndexes release];
93    [fIcon release];
94    [fChildren release];
95    [super dealloc];
96}
97
98- (NSString *) description
99{
100    if (!fIsFolder)
101        return [NSString stringWithFormat: @"%@ (%ld)", fName, [fIndexes firstIndex]];
102    else
103        return [NSString stringWithFormat: @"%@ (folder: %@)", fName, fIndexes];
104}
105
106- (NSImage *) icon
107{
108    if (!fIcon)
109        fIcon = [[[NSWorkspace sharedWorkspace] iconForFileType: fIsFolder ? NSFileTypeForHFSTypeCode(kGenericFolderIcon)
110                                                                            : [fName pathExtension]] retain];
111    return fIcon;
112}
113
114- (NSMutableArray *) children
115{
116    NSAssert(fIsFolder, @"method can only be invoked on folders");
117   
118    return fChildren;
119}
120
121- (BOOL) updateFromOldName: (NSString *) oldName toNewName: (NSString *) newName inPath: (NSString *) path
122{
123    NSParameterAssert(oldName != nil);
124    NSParameterAssert(newName != nil);
125    NSParameterAssert(path != nil);
126   
127    NSArray * lookupPathComponents = [path pathComponents];
128    NSArray * thesePathComponents = [self.path pathComponents];
129   
130    if ([lookupPathComponents isEqualToArray: thesePathComponents]) //this node represents what's being renamed
131    {
132        if ([oldName isEqualToString: self.name])
133        {
134            [fName release];
135            fName = [newName copy];
136            return YES;
137        }
138    }
139    else if ([lookupPathComponents count] < [thesePathComponents count]) //what's being renamed is part of this node's path
140    {
141        lookupPathComponents = [lookupPathComponents arrayByAddingObject: oldName];
142        const BOOL allSame = NSNotFound == [lookupPathComponents indexOfObjectWithOptions: NSEnumerationConcurrent passingTest: ^BOOL(NSString * name, NSUInteger idx, BOOL * stop) {
143            return ![name isEqualToString: [thesePathComponents objectAtIndex: idx]];
144        }];
145       
146        if (allSame)
147        {
148            NSString * oldPathPrefix = [path stringByAppendingPathComponent: oldName];
149            NSString * newPathPrefix = [path stringByAppendingPathComponent: newName];
150           
151            [fPath autorelease];
152            fPath = [[fPath stringByReplacingCharactersInRange: NSMakeRange(0, [oldPathPrefix length]) withString: newPathPrefix] retain];
153            return YES;
154        }
155    }
156   
157    return NO;
158}
159
160@end
161
162@implementation FileListNode (Private)
163
164- (id) initWithFolder: (BOOL) isFolder name: (NSString *) name path: (NSString *) path torrent: (Torrent *) torrent
165{
166    if ((self = [super init]))
167    {
168        fIsFolder = isFolder;
169        fName = [name copy];
170        fPath = [path copy];
171       
172        fIndexes = [[NSMutableIndexSet alloc] init];
173       
174        fTorrent = torrent;
175    }
176   
177    return self;
178}
179
180@end
Note: See TracBrowser for help on using the repository browser.