-
Notifications
You must be signed in to change notification settings - Fork 94
/
CHMBookmark.m
84 lines (74 loc) · 1.99 KB
/
CHMBookmark.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
83
84
//
// CHMBookmark.m
// ichm
//
// Created by Robin Lu on 8/11/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "CHMBookmark.h"
#import "CHMFile.h"
#import "CHMTag.h"
@implementation CHMBookmark
@dynamic url;
@dynamic title;
@dynamic createdAt;
@dynamic file;
@dynamic tags;
- (NSString*)tagsString
{
NSMutableString *str = [[[NSMutableString alloc] initWithString:@""] autorelease];
for (CHMTag * tag in self.tags) {
[str appendFormat:@"%@, ", tag.tag];
}
if ([str length] > 2)
{
NSRange range = {[str length] - 2, 2};
[str deleteCharactersInRange:range];
}
return str;
}
- (void)setTagsString:(NSString*)tags
{
if (!tags || [tags length] == 0)
return;
NSManagedObjectContext*context = [self managedObjectContext];
NSArray* taglist = [tags componentsSeparatedByString:@","];
NSMutableSet *set = [[NSMutableSet alloc] init];
for (NSString* tag in taglist) {
NSString* trimmed = [tag stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ( [trimmed length] > 0 )
{
CHMTag *tag = [CHMTag getTagByString:trimmed OnCreate:YES withContext:context];
if ( ![self.tags containsObject:tag] )
[set addObject:tag];
}
}
if ([set count] != 0)
[self addTags:set];
[set release];
}
+ (CHMBookmark *)bookmarkByURL:(NSString*)url withContext:(NSManagedObjectContext*)context
{
if (!url)
return nil;
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
NSEntityDescription *bookmarkEntity = [NSEntityDescription
entityForName:@"Bookmark" inManagedObjectContext:context];
[request setEntity:bookmarkEntity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"url == %@", url];
[request setPredicate:predicate];
NSError *error = nil;
NSArray *array = [context executeFetchRequest:request error:&error];
if (array == nil)
{
NSLog(@"Can not fetch bookmark info: %d",error );
return nil;
}
if ([array count] == 0)
{
return nil;
}
return [array objectAtIndex:0];
}
@end