-
Notifications
You must be signed in to change notification settings - Fork 0
/
CannedSearch.m
125 lines (123 loc) · 3.7 KB
/
CannedSearch.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// CannedSearch.m
// spires
//
// Created by Yuji on 4/12/09.
// Copyright 2009 Y. Tachikawa. All rights reserved.
//
#import "CannedSearch.h"
#import "AppDelegate.h"
#import "SpiresHelper.h"
#import "DumbOperation.h"
#import "SpiresQueryOperation.h"
#import "ArticleListReloadOperation.h"
#import "MOC.h"
@implementation CannedSearch
+(CannedSearch*)createCannedSearchWithName:(NSString*)s inMOC:(NSManagedObjectContext*)moc
{
NSEntityDescription*entity=[NSEntityDescription entityForName:@"CannedSearch" inManagedObjectContext:moc];
CannedSearch* mo=(CannedSearch*)[[NSManagedObject alloc] initWithEntity:entity
insertIntoManagedObjectContext:moc];
mo.name=s;
return mo;
}
-(void)reloadLocalWithCap:(NSUInteger)cap
{
// NSLog(@"locally reloading canned search %@", self.name);
if(![self searchString] || [[self searchString] isEqualToString:@""] || [[self searchString] length]<5){
return;
}
NSPredicate*predicate=[[SpiresHelper sharedHelper] predicateFromSPIRESsearchString:[self searchString]];
NSEntityDescription*articleEntity=[NSEntityDescription entityForName:@"Article" inManagedObjectContext:[self managedObjectContext]];
NSFetchRequest*req=[[NSFetchRequest alloc]init];
[req setEntity:articleEntity];
[req setPredicate:predicate];
if(cap!=0){
[req setFetchLimit:cap];
}
NSError*error=nil;
NSArray*a=[[self managedObjectContext] executeFetchRequest:req error:&error];
NSSet*set=[NSSet setWithArray:a];
modifying=YES;
[[self managedObjectContext] disableUndo];
self.articles=set;
[[self managedObjectContext] enableUndo];
modifying=NO;
}
/*-(void)reloadLocalFully
{
NSLog(@"fully");
[self reloadLocalWithCap:0];
}
-(void)reloadLocal
{
if(localReloadTimer){
[localReloadTimer invalidate];
}
localReloadTimer=[NSTimer scheduledTimerWithTimeInterval:.5
target:self
selector:@selector(reloadLocalFully)
userInfo:nil
repeats:NO];
[self reloadLocalWithCap:100];
}*/
-(void)reloadLocal
{
[self reloadLocalWithCap:0];
}
-(void)reload
{
if(![[NSApp appDelegate] isOnline])
return;
NSOperation*op=[[SpiresQueryOperation alloc] initWithQuery:[self searchString]
andMOC:[self managedObjectContext]];
[op setCompletionBlock:^{
[self performSelectorOnMainThread:@selector(reloadLocal) withObject:nil waitUntilDone:NO];
}];
[[OperationQueues spiresQueue] addOperation:op];
}
-(void)prepareForDeletion
{
modifying=YES;
/* without this, "articles" will resurrect during the deletion
because deletion itself invokes -articles, which fires -reloadLocal.
This totally confuses CoreData and the next save will fail,
even when the delete rule is correctly specified! Ugh.
Well, this assumes that prepareForDeletion is called
right after -[NSManagedObjectContext deleteObject:] is called,
before -articles is called to perform Delete Propagation.
I think it's guaranteed, see the "Discussion" in the documentation,
which says "
You can implement this method to perform any operations required
before the object is deleted, such as custom propagation
before relationships are torn down,
or reconfiguration of objects using key-value observing."
*/
}
-(NSSet*)articles
{
if(!modifying){
[self reloadLocal];
}
return [self primitiveValueForKey:@"articles"];
}
-(void)setSearchString:(NSString*)s
{
if(!modifying){
[self reloadLocal];
}
[self setPrimitiveValue:s forKey:@"searchString"];
}
-(NSImage*)icon
{
return [NSImage imageNamed:@"canned-search.png"];
}
-(NSString*)placeholderForSearchField
{
return @"Enter SPIRES query and hit return";
}
-(BOOL)searchStringEnabled
{
return NO;
}
@end