| 1 | #import "SapphireCollectionDirectory.h" |
|---|
| 2 | #import "SapphireDirectoryMetaData.h" |
|---|
| 3 | #import "CoreDataSupportFunctions.h" |
|---|
| 4 | #import "SapphireMetaDataSupport.h" |
|---|
| 5 | #import "SapphireDirectorySymLink.h" |
|---|
| 6 | #import "NSFileManager-Extensions.h" |
|---|
| 7 | |
|---|
| 8 | #include <sys/types.h> |
|---|
| 9 | #include <sys/stat.h> |
|---|
| 10 | #include <sys/mount.h> |
|---|
| 11 | |
|---|
| 12 | #define MOUNT_INFORMATION_KEY @"mountInformation" |
|---|
| 13 | #define MOUNT_INFORMATION_DATA @"mountInformationData" |
|---|
| 14 | |
|---|
| 15 | @implementation SapphireCollectionDirectory |
|---|
| 16 | |
|---|
| 17 | + (SapphireCollectionDirectory *)collectionAtPath:(NSString *)path mount:(BOOL)isMount skip:(BOOL)skip hidden:(BOOL)hidden manual:(BOOL)manual inContext:(NSManagedObjectContext *)moc |
|---|
| 18 | { |
|---|
| 19 | SapphireDirectoryMetaData *dir = [SapphireDirectoryMetaData createDirectoryWithPath:path inContext:moc]; |
|---|
| 20 | SapphireCollectionDirectory *ret = dir.collectionDirectory; |
|---|
| 21 | if(ret != nil) |
|---|
| 22 | { |
|---|
| 23 | if(isMount) |
|---|
| 24 | ret.isMountValue = isMount; |
|---|
| 25 | return ret; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireCollectionDirectoryName inManagedObjectContext:moc]; |
|---|
| 29 | |
|---|
| 30 | ret.manualCollectionValue = manual; |
|---|
| 31 | ret.isMountValue = isMount; |
|---|
| 32 | ret.skipValue = skip; |
|---|
| 33 | ret.hiddenValue = hidden; |
|---|
| 34 | ret.directory = dir; |
|---|
| 35 | |
|---|
| 36 | return ret; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | + (SapphireCollectionDirectory *)collectionAtPath:(NSString *)path inContext:(NSManagedObjectContext *)moc |
|---|
| 40 | { |
|---|
| 41 | return [SapphireCollectionDirectory collectionAtPath:path mount:YES skip:NO hidden:NO manual:NO inContext:moc]; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | + (SapphireCollectionDirectory *)upgradeCollectionDirectoryVersion:(int)version from:(NSManagedObject *)oldCol toContext:(NSManagedObjectContext *)newMoc |
|---|
| 45 | { |
|---|
| 46 | SapphireCollectionDirectory *ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireCollectionDirectoryName inManagedObjectContext:newMoc]; |
|---|
| 47 | ret.hidden = [oldCol valueForKey:@"hidden"]; |
|---|
| 48 | ret.isMount = [oldCol valueForKey:@"isMount"]; |
|---|
| 49 | ret.manualCollection = [oldCol valueForKey:@"manualCollection"]; |
|---|
| 50 | ret.skip = [oldCol valueForKey:@"skip"]; |
|---|
| 51 | if(version >= 2) |
|---|
| 52 | ret.name = [oldCol valueForKey:@"name"]; |
|---|
| 53 | |
|---|
| 54 | return ret; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | + (NSString *)resolveSymLinksInCollectionPath:(NSString *)path inContext:(NSManagedObjectContext *)moc |
|---|
| 58 | { |
|---|
| 59 | NSArray *components = [path pathComponents]; |
|---|
| 60 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 61 | |
|---|
| 62 | NSEnumerator *pathCompEnum = [components objectEnumerator]; |
|---|
| 63 | NSString *pathComp; |
|---|
| 64 | NSString *subPath = [pathCompEnum nextObject]; //Skip "/" |
|---|
| 65 | while((pathComp = [pathCompEnum nextObject]) != nil) |
|---|
| 66 | { |
|---|
| 67 | subPath = [subPath stringByAppendingPathComponent:pathComp]; |
|---|
| 68 | if([fm isDirectory:subPath]) |
|---|
| 69 | { |
|---|
| 70 | NSDictionary *attributes = [fm fileAttributesAtPath:subPath traverseLink:NO]; |
|---|
| 71 | if([[attributes fileType] isEqualToString:NSFileTypeSymbolicLink]) |
|---|
| 72 | { |
|---|
| 73 | SapphireDirectoryMetaData *dir = [SapphireDirectoryMetaData directoryWithPath:subPath inContext:moc]; |
|---|
| 74 | if(dir != nil) |
|---|
| 75 | { |
|---|
| 76 | NSString *resolvedPath = [subPath stringByResolvingSymlinksInPath]; |
|---|
| 77 | SapphireDirectoryMetaData *redundant = [SapphireDirectoryMetaData directoryWithPath:resolvedPath inContext:moc]; |
|---|
| 78 | if(redundant != nil) |
|---|
| 79 | [moc deleteObject:redundant]; |
|---|
| 80 | [dir setPath:resolvedPath]; |
|---|
| 81 | [SapphireDirectorySymLink createDirectoryLinkWithPath:subPath toPath:resolvedPath inContext:moc]; |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | return [path stringByResolvingSymlinksInPath]; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | + (NSArray *)availableCollectionDirectoriesInContext:(NSManagedObjectContext *)moc includeHiddenOverSkipped:(BOOL)hidden |
|---|
| 90 | { |
|---|
| 91 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 92 | NSMutableSet *colSet = [NSMutableSet set]; |
|---|
| 93 | struct statfs *mntbufp; |
|---|
| 94 | |
|---|
| 95 | NSMutableSet *dvds = [NSMutableSet set]; |
|---|
| 96 | NSEnumerator *dvdEnum = [[NSClassFromString(@"BRDiskArbHandler") mountedDVDs] objectEnumerator]; |
|---|
| 97 | BRDiskInfo *dvdInfo; |
|---|
| 98 | while((dvdInfo = [dvdEnum nextObject]) != nil) |
|---|
| 99 | [dvds addObject:[dvdInfo mountpoint]]; |
|---|
| 100 | |
|---|
| 101 | int i, mountCount = getmntinfo(&mntbufp, MNT_NOWAIT); |
|---|
| 102 | for(i=0; i<mountCount; i++) |
|---|
| 103 | { |
|---|
| 104 | if(!strcmp(mntbufp[i].f_fstypename, "autofs")) |
|---|
| 105 | continue; |
|---|
| 106 | if(!strcmp(mntbufp[i].f_fstypename, "volfs")) |
|---|
| 107 | continue; |
|---|
| 108 | if(!strcmp(mntbufp[i].f_mntonname, "/dev")) |
|---|
| 109 | continue; |
|---|
| 110 | [colSet addObject:[fm stringWithFileSystemRepresentation:mntbufp[i].f_mntonname length:strlen(mntbufp[i].f_mntonname)]]; |
|---|
| 111 | } |
|---|
| 112 | [colSet removeObject:@"/mnt"]; |
|---|
| 113 | [colSet removeObject:@"/CIFS"]; |
|---|
| 114 | [colSet removeObject:@"/Volumes/BOOTCAMP"]; |
|---|
| 115 | [colSet removeObject:NSHomeDirectory()]; |
|---|
| 116 | NSString *homeMoviesPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Movies"]; |
|---|
| 117 | if([[NSFileManager defaultManager] fileExistsAtPath:homeMoviesPath]) |
|---|
| 118 | [colSet addObject:homeMoviesPath]; |
|---|
| 119 | NSEnumerator *mountEnum = [[NSSet setWithSet:colSet] objectEnumerator]; |
|---|
| 120 | NSString *mountPoint; |
|---|
| 121 | while((mountPoint = [mountEnum nextObject]) != nil) |
|---|
| 122 | { |
|---|
| 123 | if([dvds containsObject:mountPoint]) |
|---|
| 124 | //Don't show DVDs as collections |
|---|
| 125 | continue; |
|---|
| 126 | NSString *newPath = [SapphireCollectionDirectory resolveSymLinksInCollectionPath:mountPoint inContext:moc]; |
|---|
| 127 | [SapphireCollectionDirectory collectionAtPath:newPath mount:YES skip:NO hidden:NO manual:NO inContext:moc]; |
|---|
| 128 | if(![newPath isEqualToString:mountPoint]) |
|---|
| 129 | { |
|---|
| 130 | [colSet removeObject:mountPoint]; |
|---|
| 131 | [colSet addObject:newPath]; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | NSPredicate *predicate; |
|---|
| 136 | if(hidden) |
|---|
| 137 | predicate = [NSPredicate predicateWithFormat:@"(skip == NO) AND ((manualCollection == YES) OR (directory.path IN %@))", colSet]; |
|---|
| 138 | else |
|---|
| 139 | predicate = [NSPredicate predicateWithFormat:@"(hidden == NO) AND ((manualCollection == YES) OR (directory.path IN %@))", colSet]; |
|---|
| 140 | NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"directory.path" ascending:YES]; |
|---|
| 141 | NSArray *ret = doSortedFetchRequest(SapphireCollectionDirectoryName, moc, predicate, sort); |
|---|
| 142 | |
|---|
| 143 | [sort release]; |
|---|
| 144 | return ret; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | + (NSArray *)skippedCollectionDirectoriesInContext:(NSManagedObjectContext *)moc |
|---|
| 148 | { |
|---|
| 149 | NSPredicate *predicate = [NSPredicate predicateWithFormat:@"skip == YES"]; |
|---|
| 150 | NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"directory.path" ascending:YES]; |
|---|
| 151 | NSArray *ret = doSortedFetchRequest(SapphireCollectionDirectoryName, moc, predicate, sort); |
|---|
| 152 | |
|---|
| 153 | [sort release]; |
|---|
| 154 | return ret; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | + (NSArray *)allCollectionsInContext:(NSManagedObjectContext *)moc |
|---|
| 158 | { |
|---|
| 159 | NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(manualCollection == YES) OR (isMount == YES)"]; |
|---|
| 160 | NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"directory.path" ascending:YES]; |
|---|
| 161 | NSArray *ret = doSortedFetchRequest(SapphireCollectionDirectoryName, moc, predicate, sort); |
|---|
| 162 | |
|---|
| 163 | [sort release]; |
|---|
| 164 | return ret; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | - (BOOL)deleteValue |
|---|
| 168 | { |
|---|
| 169 | return toDelete; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | - (void)setDeleteValue:(BOOL)del |
|---|
| 173 | { |
|---|
| 174 | toDelete = del; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | - (NSString *)rename:(NSString *)name |
|---|
| 178 | { |
|---|
| 179 | self.name = name; |
|---|
| 180 | [SapphireMetaDataSupport save:[self managedObjectContext]]; |
|---|
| 181 | return nil; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | - (NSString *)name |
|---|
| 185 | { |
|---|
| 186 | NSString *name = super.name; |
|---|
| 187 | if(name == nil) |
|---|
| 188 | return [self.directory path]; |
|---|
| 189 | return name; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | - (NSDictionary *)mountInformation |
|---|
| 193 | { |
|---|
| 194 | [self willAccessValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 195 | NSDictionary *ret = [self primitiveValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 196 | [self didAccessValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 197 | if(ret == nil) |
|---|
| 198 | { |
|---|
| 199 | NSData *propData = [self valueForKey:MOUNT_INFORMATION_DATA]; |
|---|
| 200 | if(propData != nil) |
|---|
| 201 | { |
|---|
| 202 | ret = [NSKeyedUnarchiver unarchiveObjectWithData:propData]; |
|---|
| 203 | [self setPrimitiveValue:ret forKey:MOUNT_INFORMATION_KEY]; |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | return ret; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | - (void)setMountInformation:(NSDictionary *)info |
|---|
| 210 | { |
|---|
| 211 | [self willChangeValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 212 | [self setPrimitiveValue:info forKey:MOUNT_INFORMATION_KEY]; |
|---|
| 213 | [self didChangeValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 214 | [self setValue:[NSKeyedArchiver archivedDataWithRootObject:info] forKey:MOUNT_INFORMATION_DATA]; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | @end |
|---|