forked from karelia/iMedia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IMBObjectArrayController.m
executable file
·348 lines (244 loc) · 11 KB
/
IMBObjectArrayController.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
iMedia Browser Framework <http://karelia.com/imedia/>
Copyright (c) 2005-2012 by Karelia Software et al.
iMedia Browser is based on code originally developed by Jason Terhorst,
further developed for Sandvox by Greg Hulands, Dan Wood, and Terrence Talbot.
The new architecture for version 2.0 was developed by Peter Baumgartner.
Contributions have also been made by Matt Gough, Martin Wennerberg and others
as indicated in source files.
The iMedia Browser Framework is licensed under the following terms:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in all or substantial portions of the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following
conditions:
Redistributions of source code must retain the original terms stated here,
including this list of conditions, the disclaimer noted below, and the
following copyright notice: Copyright (c) 2005-2012 by Karelia Software et al.
Redistributions in binary form must include, in an end-user-visible manner,
e.g., About window, Acknowledgments window, or similar, either a) the original
terms stated here, including this list of conditions, the disclaimer noted
below, and the aforementioned copyright notice, or b) the aforementioned
copyright notice and a link to karelia.com/imedia.
Neither the name of Karelia Software, nor Sandvox, nor the names of
contributors to iMedia Browser may be used to endorse or promote products
derived from the Software without prior and express written permission from
Karelia Software or individual contributors, as appropriate.
Disclaimer: THE SOFTWARE IS PROVIDED BY THE COPYRIGHT OWNER AND CONTRIBUTORS
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH, THE
SOFTWARE OR THE USE OF, OR OTHER DEALINGS IN, THE SOFTWARE.
*/
// Author: Peter Baumgartner
//----------------------------------------------------------------------------------------------------------------------
#pragma mark HEADERS
#import "IMBObjectArrayController.h"
#import "IMBObject.h"
#import "IMBParser.h"
#import "IMBCommon.h"
//----------------------------------------------------------------------------------------------------------------------
#pragma mark CONSTANTS
const NSString* kSearchStringContext = @"searchString";
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
@implementation IMBObjectArrayController
@synthesize delegate = _delegate;
@synthesize searchableProperties = _searchableProperties;
@synthesize searchString = _searchString;
//----------------------------------------------------------------------------------------------------------------------
- (id) init
{
if (self = [super init])
{
}
return self;
}
- (void) awakeFromNib
{
[self addObserver:self forKeyPath:@"searchString" options:0 context:(void*)kSearchStringContext];
}
- (void) dealloc
{
[self removeObserver:self forKeyPath:@"searchString"];
IMBRelease(_searchableProperties);
IMBRelease(_searchString);
[super dealloc];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
- (IBAction) search:(id)inSender
{
[self setSearchString:[inSender stringValue]];
}
- (IBAction) resetSearch:(id)inSender
{
if ([_searchString length])
{
[ibSearchField setStringValue:@""];
[self search:ibSearchField];
}
}
- (void) observeValueForKeyPath:(NSString*)inKeyPath ofObject:(id)inObject change:(NSDictionary*)inChange context:(void*)inContext
{
if (inContext == (void*)kSearchStringContext)
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(rearrangeObjects) object:nil];
[self performSelector:@selector(rearrangeObjects) withObject:nil afterDelay:0.0 inModes:[NSArray arrayWithObject:NSRunLoopCommonModes]];
}
else
{
[super observeValueForKeyPath:inKeyPath ofObject:inObject change:inChange context:inContext];
}
}
//----------------------------------------------------------------------------------------------------------------------
// Set default values, and keep reference to new object -- see arrangeObjects:
- (id) newObject
{
_newObject = [super newObject];
// for (NSString* key in _searchableProperties)
// {
// [_newObject setValue:key forKey:key];
// }
return _newObject;
}
- (NSArray*) arrangeObjects:(NSArray*)inObjects
{
BOOL hasProxyForObject = _delegate && [_delegate respondsToSelector:@selector(proxyForObject:)];
// If we have a filterPredicate, then the array is already filtered at this point. All we need
// to do is replace the objects with proxies...
if ([self filterPredicate])
{
NSArray* arrangedObjects = [super arrangeObjects:inObjects];
if (!hasProxyForObject) return arrangedObjects;
NSMutableArray* proxyArray = [NSMutableArray array];
for (id object in arrangedObjects)
{
[proxyArray addObject:[_delegate proxyForObject:object]];
}
return proxyArray;
}
// Without the predicate, we need to filter the array manually:
else
{
BOOL searching = _searchString != nil &&
_searchableProperties != nil &&
![_searchString isEqualToString:@""] &&
[_searchableProperties count] > 0;
// Create array of objects that match search string.
// Also add any newly-created object unconditionally:
// (a) You'll get an error if a newly-added object isn't added to arrangedObjects.
// (b) The user will see newly-added objects even if they don't match the search term.
// (c) The search is not case-sensitive.
NSMutableArray* matchedObjects = [NSMutableArray arrayWithCapacity:[inObjects count]];
/*
// Let's try to use Spotlight to help us if at all possible. It will be a lot faster on large data sets.
// The trick is to figure out how to combine what the person searches for and the other constraints that we
// want the parser object to impose, e.g. restricting to a certain folder, a certain UTI, or matching to some kind of
// database query. Anyhow, I'll bang out some ideas here and then we can talk about getting it merged together.
// Also, we need to specify what properties we are matching against.
// I think that maybe what we can do is to match the hits it returns against the items we are displaying, so that
// you never can find *more* than what is in the list.
// from PhotoSearch sample code
NSMetaDataQuery *query = [[NSMetadataQuery alloc] init];
// We want the items in the query to automatically be sorted by the file system name; this way, we don't have to do any special sorting
[query setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemFSName ascending:YES] autorelease]]];
[query setPredicate:searchPredicate];
// Use KVO to watch the results of the query
[query addObserver:self forKeyPath:@"results" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
[query setDelegate:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryNote:) name:nil object:query];
[query startQuery];
//For images, put in the predicate: kMDItemContentTypeTree = 'public.image'
//We would be matching on kMDItemKeywords
//we would want to use -[NSMetaDataQuery setSearchScopes:] with the path of the folder we are searching for
*/
for (IMBObject* object in inObjects)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// First check whether object passes our delegate's filter
// (e.g. used to display badged objects only)
if (_delegate && ![_delegate objectArrayController:self filterObject:object])
{
continue;
}
// For searching to work properly we should load object metadata if it isn't available yet,
// because the search might require keypaths like @"metadata.*"...
if (searching && object.metadata == nil)
{
[object.parser loadMetadataForObject:object];
}
// Give the delegate a chance to enhance an object or totally replace it with a proxy object.
// This way an object can be customized or made more rich...
id proxy = hasProxyForObject ? [_delegate proxyForObject:object] : object;
// If the object has just been created, add it unconditionally...
if (object == _newObject)
{
[matchedObjects addObject:proxy];
_newObject = nil;
}
// Search all properties in the array. Please note that we need to check for the existance of a property
// (value!=nil) BEFORE checking rangeOfString: or a nil value will provide us with a positive match.
// This would yield way to many false results...
else if (searching)
{
NSString* value;
BOOL foundMatch = NO;
for (NSString* key in _searchableProperties)
{
value = [object valueForKeyPath:key];
if (value != nil)
{
// We don't test for implementation of the search filtering method, because
// runtime querying for every object could be expensive. The previous contract was
// that metadata values had to be NSString. The new contract is that metadata
// values have to either be NSString or else implement this filter message.
//
// NOTE also that if the client has a custom metadata type. we won't even make assumptions
// about whether they want the lowercase string or not, we'll just let them dictate
// the entire matching policy based on the user's input string.
foundMatch = [value imb_matchesSearchFilterString:_searchString];
}
if (foundMatch)
{
[matchedObjects addObject:proxy];
break;
}
}
}
else
{
[matchedObjects addObject:proxy];
}
[pool drain];
}
return [super arrangeObjects:matchedObjects];
}
}
//----------------------------------------------------------------------------------------------------------------------
@end
@implementation NSString ( IMBOBjectArrayControllerFiltering )
- (BOOL) imb_matchesSearchFilterString:(NSString*)searchString;
{
BOOL foundMatch = [self rangeOfString:searchString options:NSCaseInsensitiveSearch].location != NSNotFound;
return foundMatch;
}
@end
@implementation NSArray ( IMBOBjectArrayControllerFiltering )
- (BOOL) imb_matchesSearchFilterString:(NSString*)searchString;
{
// This might be too slow .... if we needed to cheat I suppose we could look for an exact string match?
for (id object in self)
{
if ([object imb_matchesSearchFilterString:searchString])
{
return YES;
}
}
return NO;
}
@end