| 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 *)upgradeV1CollectionDirectory:(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 | |
|---|
| 52 | return ret; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | + (NSString *)resolveSymLinksInCollectionPath:(NSString *)path inContext:(NSManagedObjectContext *)moc |
|---|
| 56 | { |
|---|
| 57 | NSArray *components = [path pathComponents]; |
|---|
| 58 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 59 | |
|---|
| 60 | NSEnumerator *pathCompEnum = [components objectEnumerator]; |
|---|
| 61 | NSString *pathComp; |
|---|
| 62 | NSString *subPath = [pathCompEnum nextObject]; //Skip "/" |
|---|
| 63 | while((pathComp = [pathCompEnum nextObject]) != nil) |
|---|
| 64 | { |
|---|
| 65 | subPath = [subPath stringByAppendingPathComponent:pathComp]; |
|---|
| 66 | if([fm isDirectory:subPath]) |
|---|
| 67 | { |
|---|
| 68 | NSDictionary *attributes = [fm fileAttributesAtPath:subPath traverseLink:NO]; |
|---|
| 69 | if([[attributes fileType] isEqualToString:NSFileTypeSymbolicLink]) |
|---|
| 70 | { |
|---|
| 71 | SapphireDirectoryMetaData *dir = [SapphireDirectoryMetaData directoryWithPath:subPath inContext:moc]; |
|---|
| 72 | if(dir != nil) |
|---|
| 73 | { |
|---|
| 74 | NSString *resolvedPath = [subPath stringByResolvingSymlinksInPath]; |
|---|
| 75 | SapphireDirectoryMetaData *redundant = [SapphireDirectoryMetaData directoryWithPath:resolvedPath inContext:moc]; |
|---|
| 76 | if(redundant != nil) |
|---|
| 77 | [moc deleteObject:redundant]; |
|---|
| 78 | [dir setPath:resolvedPath]; |
|---|
| 79 | [SapphireDirectorySymLink createDirectoryLinkWithPath:subPath toPath:resolvedPath inContext:moc]; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | return [path stringByResolvingSymlinksInPath]; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | + (NSArray *)availableCollectionDirectoriesInContext:(NSManagedObjectContext *)moc includeHiddenOverSkipped:(BOOL)hidden |
|---|
| 88 | { |
|---|
| 89 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 90 | NSMutableSet *colSet = [NSMutableSet set]; |
|---|
| 91 | struct statfs *mntbufp; |
|---|
| 92 | |
|---|
| 93 | NSMutableSet *dvds = [NSMutableSet set]; |
|---|
| 94 | NSEnumerator *dvdEnum = [[NSClassFromString(@"BRDiskArbHandler") mountedDVDs] objectEnumerator]; |
|---|
| 95 | BRDiskInfo *dvdInfo; |
|---|
| 96 | while((dvdInfo = [dvdEnum nextObject]) != nil) |
|---|
| 97 | [dvds addObject:[dvdInfo mountpoint]]; |
|---|
| 98 | |
|---|
| 99 | int i, mountCount = getmntinfo(&mntbufp, MNT_NOWAIT); |
|---|
| 100 | for(i=0; i<mountCount; i++) |
|---|
| 101 | { |
|---|
| 102 | if(!strcmp(mntbufp[i].f_fstypename, "autofs")) |
|---|
| 103 | continue; |
|---|
| 104 | if(!strcmp(mntbufp[i].f_fstypename, "volfs")) |
|---|
| 105 | continue; |
|---|
| 106 | if(!strcmp(mntbufp[i].f_mntonname, "/dev")) |
|---|
| 107 | continue; |
|---|
| 108 | [colSet addObject:[fm stringWithFileSystemRepresentation:mntbufp[i].f_mntonname length:strlen(mntbufp[i].f_mntonname)]]; |
|---|
| 109 | } |
|---|
| 110 | [colSet removeObject:@"/mnt"]; |
|---|
| 111 | [colSet removeObject:@"/CIFS"]; |
|---|
| 112 | [colSet removeObject:NSHomeDirectory()]; |
|---|
| 113 | NSString *homeMoviesPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Movies"]; |
|---|
| 114 | if([[NSFileManager defaultManager] fileExistsAtPath:homeMoviesPath]) |
|---|
| 115 | [colSet addObject:homeMoviesPath]; |
|---|
| 116 | NSEnumerator *mountEnum = [[NSSet setWithSet:colSet] objectEnumerator]; |
|---|
| 117 | NSString *mountPoint; |
|---|
| 118 | while((mountPoint = [mountEnum nextObject]) != nil) |
|---|
| 119 | { |
|---|
| 120 | if([dvds containsObject:mountPoint]) |
|---|
| 121 | //Don't show DVDs as collections |
|---|
| 122 | continue; |
|---|
| 123 | NSString *newPath = [SapphireCollectionDirectory resolveSymLinksInCollectionPath:mountPoint inContext:moc]; |
|---|
| 124 | [SapphireCollectionDirectory collectionAtPath:newPath mount:YES skip:NO hidden:NO manual:NO inContext:moc]; |
|---|
| 125 | if(![newPath isEqualToString:mountPoint]) |
|---|
| 126 | { |
|---|
| 127 | [colSet removeObject:mountPoint]; |
|---|
| 128 | [colSet addObject:newPath]; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | NSPredicate *predicate; |
|---|
| 133 | if(hidden) |
|---|
| 134 | predicate = [NSPredicate predicateWithFormat:@"(skip == NO) AND ((manualCollection == YES) OR (directory.path IN %@))", colSet]; |
|---|
| 135 | else |
|---|
| 136 | predicate = [NSPredicate predicateWithFormat:@"(hidden == NO) AND ((manualCollection == YES) OR (directory.path IN %@))", colSet]; |
|---|
| 137 | NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"directory.path" ascending:YES]; |
|---|
| 138 | NSArray *ret = doSortedFetchRequest(SapphireCollectionDirectoryName, moc, predicate, sort); |
|---|
| 139 | |
|---|
| 140 | [sort release]; |
|---|
| 141 | return ret; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | + (NSArray *)skippedCollectionDirectoriesInContext:(NSManagedObjectContext *)moc |
|---|
| 145 | { |
|---|
| 146 | NSPredicate *predicate = [NSPredicate predicateWithFormat:@"skip == YES"]; |
|---|
| 147 | NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"directory.path" ascending:YES]; |
|---|
| 148 | NSArray *ret = doSortedFetchRequest(SapphireCollectionDirectoryName, moc, predicate, sort); |
|---|
| 149 | |
|---|
| 150 | [sort release]; |
|---|
| 151 | return ret; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | + (NSArray *)allCollectionsInContext:(NSManagedObjectContext *)moc |
|---|
| 155 | { |
|---|
| 156 | NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(manualCollection == YES) OR (isMount == YES)"]; |
|---|
| 157 | NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"directory.path" ascending:YES]; |
|---|
| 158 | NSArray *ret = doSortedFetchRequest(SapphireCollectionDirectoryName, moc, predicate, sort); |
|---|
| 159 | |
|---|
| 160 | [sort release]; |
|---|
| 161 | return ret; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | - (BOOL)deleteValue |
|---|
| 165 | { |
|---|
| 166 | return toDelete; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | - (void)setDeleteValue:(BOOL)del |
|---|
| 170 | { |
|---|
| 171 | toDelete = del; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | - (NSString *)rename:(NSString *)name |
|---|
| 175 | { |
|---|
| 176 | self.name = name; |
|---|
| 177 | [SapphireMetaDataSupport save:[self managedObjectContext]]; |
|---|
| 178 | return nil; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | - (NSString *)name |
|---|
| 182 | { |
|---|
| 183 | NSString *name = super.name; |
|---|
| 184 | if(name == nil) |
|---|
| 185 | return [self.directory path]; |
|---|
| 186 | return name; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | - (NSDictionary *)mountInformation |
|---|
| 190 | { |
|---|
| 191 | [self willAccessValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 192 | NSDictionary *ret = [self primitiveValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 193 | [self didAccessValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 194 | if(ret == nil) |
|---|
| 195 | { |
|---|
| 196 | NSData *propData = [self valueForKey:MOUNT_INFORMATION_DATA]; |
|---|
| 197 | if(propData != nil) |
|---|
| 198 | { |
|---|
| 199 | ret = [NSKeyedUnarchiver unarchiveObjectWithData:propData]; |
|---|
| 200 | [self setPrimitiveValue:ret forKey:MOUNT_INFORMATION_KEY]; |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | return ret; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | - (void)setMountInformation:(NSDictionary *)info |
|---|
| 207 | { |
|---|
| 208 | [self willChangeValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 209 | [self setPrimitiveValue:info forKey:MOUNT_INFORMATION_KEY]; |
|---|
| 210 | [self didChangeValueForKey:MOUNT_INFORMATION_KEY]; |
|---|
| 211 | [self setValue:[NSKeyedArchiver archivedDataWithRootObject:info] forKey:MOUNT_INFORMATION_DATA]; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | @end |
|---|