| 1 | /* |
|---|
| 2 | * SapphireBasicDirectoryFunctions.h |
|---|
| 3 | * Sapphire |
|---|
| 4 | * |
|---|
| 5 | * Created by Graham Booker on Apr. 9, 2008. |
|---|
| 6 | * Copyright 2008 Sapphire Development Team and/or www.nanopi.net |
|---|
| 7 | * All rights reserved. |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or modify it under the terms of the GNU |
|---|
| 10 | * General Public License as published by the Free Software Foundation; either version 3 of the License, |
|---|
| 11 | * or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
|---|
| 14 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|---|
| 15 | * Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License along with this program; if not, |
|---|
| 18 | * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | /* This file contains functions which are commonly used for all virtual directories. |
|---|
| 22 | It assumes the definition of a few instance variables, which can be set using defines in SapphireBasicDirectoryFunctionsDefines. */ |
|---|
| 23 | |
|---|
| 24 | #ifndef RECURSIVE_FUNCTIONS_ALREADY_DEFINED |
|---|
| 25 | /*Function to invoke a command on all files in a subtree*/ |
|---|
| 26 | - (void)invokeRecursivelyOnFiles:(NSInvocation *)fileInv |
|---|
| 27 | { |
|---|
| 28 | NSArray *files = [self metaFiles]; |
|---|
| 29 | if([files count]) |
|---|
| 30 | { |
|---|
| 31 | SapphireFileMetaData *file; |
|---|
| 32 | NSEnumerator *fileEnum = [files objectEnumerator]; |
|---|
| 33 | while((file = [fileEnum nextObject]) != nil) |
|---|
| 34 | { |
|---|
| 35 | [fileInv invokeWithTarget:file]; |
|---|
| 36 | } |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | - (BOOL)checkPredicate:(NSComparisonPredicate *)pred |
|---|
| 41 | { |
|---|
| 42 | NSPredicate *fetchPred = [self metaFileFetchPredicate]; |
|---|
| 43 | if(fetchPred != nil) |
|---|
| 44 | { |
|---|
| 45 | NSPredicate *final = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:pred, fetchPred, nil]]; |
|---|
| 46 | return entityExists(SapphireFileMetaDataName, [self managedObjectContext], final); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | NSArray *files = [self metaFiles]; |
|---|
| 50 | int i, count = [files count]; |
|---|
| 51 | for(i=0; i<count; i++) |
|---|
| 52 | if([pred evaluateWithObject:[files objectAtIndex:i]]) |
|---|
| 53 | return YES; |
|---|
| 54 | |
|---|
| 55 | NSArray *dirs = [self directories]; |
|---|
| 56 | count = [dirs count]; |
|---|
| 57 | for(i=0; i<count; i++) |
|---|
| 58 | { |
|---|
| 59 | NSString *dirName = [dirs objectAtIndex:i]; |
|---|
| 60 | id <SapphireDirectory> dir = [self metaDataForDirectory:dirName]; |
|---|
| 61 | if([pred evaluateWithObject:dir]) |
|---|
| 62 | return YES; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | return NO; |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | - (void)getSubFileMetasWithDelegate:(id <SapphireMetaDataScannerDelegate>)subDelegate skipDirectories:(NSMutableSet *)skip |
|---|
| 69 | { |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | - (void)scanForNewFilesWithDelegate:(id <SapphireMetaDataScannerDelegate>)subDelegate skipDirectories:(NSMutableSet *)skip |
|---|
| 73 | { |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | - (void)cancelImport |
|---|
| 77 | { |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | - (void)resumeImport |
|---|
| 81 | { |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | #endif |
|---|
| 85 | |
|---|
| 86 | - (BOOL)internalWatchedValue |
|---|
| 87 | { |
|---|
| 88 | NSComparisonPredicate *predicate = (NSComparisonPredicate *)[NSPredicate predicateWithFormat:@"watched == NO"]; |
|---|
| 89 | BOOL ret = [self checkPredicate:predicate]; |
|---|
| 90 | return !ret; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | - (BOOL)watchedValue |
|---|
| 94 | { |
|---|
| 95 | if(watchedCache != nil) |
|---|
| 96 | return [watchedCache boolValue]; |
|---|
| 97 | BOOL ret = [self internalWatchedValue]; |
|---|
| 98 | watchedCache = [[NSNumber alloc] initWithBool:ret]; |
|---|
| 99 | return ret; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | - (NSNumber *)watched |
|---|
| 103 | { |
|---|
| 104 | if(watchedCache != nil) |
|---|
| 105 | return watchedCache; |
|---|
| 106 | NSNumber *ret = [NSNumber numberWithBool:[self internalWatchedValue]]; |
|---|
| 107 | watchedCache = [ret retain]; |
|---|
| 108 | return ret; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | - (void)setWatchedValue:(BOOL)watched |
|---|
| 112 | { |
|---|
| 113 | [self setWatched:[NSNumber numberWithBool:watched]]; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | - (void)setWatched:(NSNumber *)watched |
|---|
| 117 | { |
|---|
| 118 | SEL select = @selector(setWatched:); |
|---|
| 119 | NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[SapphireFileMetaData class] instanceMethodSignatureForSelector:select]]; |
|---|
| 120 | [inv setSelector:select]; |
|---|
| 121 | [inv setArgument:&watched atIndex:2]; |
|---|
| 122 | [self invokeRecursivelyOnFiles:inv]; |
|---|
| 123 | [SapphireMetaDataSupport save:[self managedObjectContext]]; |
|---|
| 124 | [watchedCache release]; |
|---|
| 125 | watchedCache = [watched retain]; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | - (BOOL)internalFavoriteValue |
|---|
| 129 | { |
|---|
| 130 | NSComparisonPredicate *predicate = (NSComparisonPredicate *)[NSPredicate predicateWithFormat:@"favorite == YES"]; |
|---|
| 131 | BOOL ret = [self checkPredicate:predicate]; |
|---|
| 132 | return ret; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | - (BOOL)favoriteValue |
|---|
| 136 | { |
|---|
| 137 | if(favoriteCache != nil) |
|---|
| 138 | return [favoriteCache boolValue]; |
|---|
| 139 | BOOL ret = [self internalFavoriteValue]; |
|---|
| 140 | favoriteCache = [[NSNumber alloc] initWithBool:ret]; |
|---|
| 141 | return ret; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | - (NSNumber *)favorite |
|---|
| 145 | { |
|---|
| 146 | if(favoriteCache != nil) |
|---|
| 147 | return favoriteCache; |
|---|
| 148 | NSNumber *ret = [NSNumber numberWithBool:[self internalFavoriteValue]]; |
|---|
| 149 | favoriteCache = [ret retain]; |
|---|
| 150 | return ret; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | - (void)setFavoriteValue:(BOOL)favorite |
|---|
| 154 | { |
|---|
| 155 | [self setFavorite:[NSNumber numberWithBool:favorite]]; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | - (void)setFavorite:(NSNumber *)favorite |
|---|
| 159 | { |
|---|
| 160 | SEL select = @selector(setFavorite:); |
|---|
| 161 | NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[SapphireFileMetaData class] instanceMethodSignatureForSelector:select]]; |
|---|
| 162 | [inv setSelector:select]; |
|---|
| 163 | [inv setArgument:&favorite atIndex:2]; |
|---|
| 164 | [self invokeRecursivelyOnFiles:inv]; |
|---|
| 165 | [SapphireMetaDataSupport save:[self managedObjectContext]]; |
|---|
| 166 | [favoriteCache release]; |
|---|
| 167 | favoriteCache = [favorite retain]; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | - (void)clearPredicateCache |
|---|
| 171 | { |
|---|
| 172 | BOOL report = NO; |
|---|
| 173 | id <SapphireDirectory> myParentDir = [self parentDirectory]; |
|---|
| 174 | if(watchedCache != nil) |
|---|
| 175 | { |
|---|
| 176 | BOOL oldWatched = [watchedCache boolValue]; |
|---|
| 177 | [watchedCache release]; |
|---|
| 178 | watchedCache = nil; |
|---|
| 179 | BOOL doCheck = [myParentDir watched] != nil; |
|---|
| 180 | if(doCheck && [self watchedValue] != oldWatched) |
|---|
| 181 | report = YES; |
|---|
| 182 | } |
|---|
| 183 | if(favoriteCache != nil) |
|---|
| 184 | { |
|---|
| 185 | BOOL oldFavorite = [favoriteCache boolValue]; |
|---|
| 186 | [favoriteCache release]; |
|---|
| 187 | favoriteCache = nil; |
|---|
| 188 | BOOL doCheck = !report && [myParentDir watched] != nil; |
|---|
| 189 | if(doCheck && [self favoriteValue] != oldFavorite) |
|---|
| 190 | report = YES; |
|---|
| 191 | } |
|---|
| 192 | if(report) |
|---|
| 193 | [myParentDir clearPredicateCache]; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | - (void)setFilterPredicate:(NSPredicate *)predicate |
|---|
| 197 | { |
|---|
| 198 | [filterPredicate release]; |
|---|
| 199 | filterPredicate = [predicate retain]; |
|---|
| 200 | [self clearPredicateCache]; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | - (NSPredicate *)filterPredicate |
|---|
| 204 | { |
|---|
| 205 | return filterPredicate; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | - (id <SapphireMetaDataDelegate>)delegate |
|---|
| 209 | { |
|---|
| 210 | return delegate; |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | - (void)setDelegate:(id <SapphireMetaDataDelegate>)newDelegate |
|---|
| 214 | { |
|---|
| 215 | [newDelegate retain]; |
|---|
| 216 | [delegate release]; |
|---|
| 217 | delegate = newDelegate; |
|---|
| 218 | if(delegate == nil) |
|---|
| 219 | [self refreshAllObjects]; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | - (NSMutableDictionary *)getDisplayedMetaDataInOrder:(NSArray * *)order; |
|---|
| 223 | { |
|---|
| 224 | if(order != nil) |
|---|
| 225 | *order = nil; |
|---|
| 226 | return [NSMutableDictionary dictionaryWithObjectsAndKeys: |
|---|
| 227 | [[self path] lastPathComponent], META_TITLE_KEY, |
|---|
| 228 | nil]; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | - (void)setToReimportFromMask:(int)mask |
|---|
| 232 | { |
|---|
| 233 | SEL select = @selector(setToReimportFromMask:); |
|---|
| 234 | NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[SapphireFileMetaData class] instanceMethodSignatureForSelector:select]]; |
|---|
| 235 | [inv setSelector:select]; |
|---|
| 236 | NSNumber *num = [NSNumber numberWithInt:mask]; |
|---|
| 237 | [inv setArgument:&num atIndex:2]; |
|---|
| 238 | [self invokeRecursivelyOnFiles:inv]; |
|---|
| 239 | [SapphireMetaDataSupport save:[self managedObjectContext]]; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | - (void)clearMetaData |
|---|
| 243 | { |
|---|
| 244 | SEL select = @selector(clearMetaData); |
|---|
| 245 | NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[[SapphireFileMetaData class] instanceMethodSignatureForSelector:select]]; |
|---|
| 246 | [inv setSelector:select]; |
|---|
| 247 | [self invokeRecursivelyOnFiles:inv]; |
|---|
| 248 | [SapphireMetaDataSupport save:[self managedObjectContext]]; |
|---|
| 249 | } |
|---|