forked from veryweblog/ichm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CHMFile.m
82 lines (74 loc) · 2.17 KB
/
CHMFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// CHMFile.m
// ichm
//
// Created by Robin Lu on 8/10/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "CHMFile.h"
#import "CHMBookmark.h"
@implementation CHMFile
@dynamic path;
@dynamic bookmarks;
@dynamic title;
@dynamic isValid;
+ (CHMFile *)fileByPath:(NSString*)path withContext:(NSManagedObjectContext*)context
{
if (!path)
return nil;
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *fileEntity = [NSEntityDescription
entityForName:@"File" inManagedObjectContext:context];
[request setEntity:fileEntity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"path == %@", path];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
if (array == nil)
{
NSLog(@"Can not fetch file info: %d",error );
return nil;
}
if ([array count] == 0)
{
NSLog(@"Can not fetch file with path: %%",path );
return nil;
}
return [array objectAtIndex:0];
}
+ (NSArray*)allFileswithContext:(NSManagedObjectContext*)context
{
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *tagEntity = [NSEntityDescription
entityForName:@"File" inManagedObjectContext:context];
[request setEntity:tagEntity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"title" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
if (array == nil)
{
NSLog(@"Can not fetch file info: %d",error );
}
return array;
}
+ (void)purgeWithContext:(NSManagedObjectContext*)context
{
NSArray * files = [CHMFile allFileswithContext:context];
for ( CHMFile * file in files )
{
if ([[file bookmarks] count] == 0)
[context deleteObject:file];
else
{
BOOL isDirectory;
BOOL isValid = [[NSFileManager defaultManager] fileExistsAtPath:[file path] isDirectory:&isDirectory] && !isDirectory ;
NSNumber *number = [[NSNumber alloc] initWithBool:isValid];
[file setIsValid:number];
}
}
}
@end