-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoverStoryArrayController.m
More file actions
174 lines (157 loc) · 5.84 KB
/
CoverStoryArrayController.m
File metadata and controls
174 lines (157 loc) · 5.84 KB
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
//
// CoverStoryArrayController.m
// CoverStory
//
// Created by dmaclach on 06/03/10.
// Copyright 2006-2010 Google Inc.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//
#import "CoverStoryArrayController.h"
#import "CoverStoryDocument.h"
#import "CoverStoryPreferenceKeys.h"
#import "NSUserDefaultsController+KeyValues.h"
static NSString *const kPrefsToWatch[] = {
kCoverStorySystemSourcesPatternsKey,
kCoverStoryUnittestSourcesPatternsKey,
};
#define kCoverStoryHideSDKSources @"hideSDKSources"
#define kCoverStoryHideUnittestSources @"hideUnittestSources"
#define kCoverStoryRemoveCommonSourcePrefix @"removeCommonSourcePrefix"
#define kCoverStoryFilterString @"filterString"
static NSString *const kDocumentKeyPathsToWatch[] = {
kCoverStoryHideSDKSources,
kCoverStoryHideUnittestSources,
kCoverStoryRemoveCommonSourcePrefix,
kCoverStoryFilterString
};
@implementation CoverStoryArrayController
- (void)dealloc {
for (size_t i = 0;
i < sizeof(kDocumentKeyPathsToWatch) / sizeof(kDocumentKeyPathsToWatch[0]);
++i) {
[owningDocument_ removeObserver:self forKeyPath:kDocumentKeyPathsToWatch[i]];
}
NSUserDefaultsController *defaults = [NSUserDefaultsController sharedUserDefaultsController];
for (size_t i = 0; i < sizeof(kPrefsToWatch) / sizeof(NSString*); ++i) {
[defaults removeObserver:self
forKeyPath:[NSUserDefaultsController cs_valuesKey:kPrefsToWatch[i]]];
}
[super dealloc];
}
- (void)awakeFromNib {
for (size_t i = 0;
i < sizeof(kDocumentKeyPathsToWatch) / sizeof(kDocumentKeyPathsToWatch[0]);
++i) {
[owningDocument_ addObserver:self
forKeyPath:kDocumentKeyPathsToWatch[i]
options:0
context:nil];
}
NSUserDefaultsController *defaults = [NSUserDefaultsController sharedUserDefaultsController];
for (size_t i = 0; i < sizeof(kPrefsToWatch) / sizeof(NSString*); ++i) {
[defaults addObserver:self
forKeyPath:[NSUserDefaultsController cs_valuesKey:kPrefsToWatch[i]]
options:0
context:nil];
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
BOOL handled = NO;
if ([object isEqualTo:[NSUserDefaultsController sharedUserDefaultsController]]) {
if ([keyPath isEqualToString:
[NSUserDefaultsController cs_valuesKey:kCoverStorySystemSourcesPatternsKey]]) {
if ([owningDocument_ hideSDKSources]) {
// if we're hiding them then update because the pattern changed
[self rearrangeObjects];
handled = YES;
}
} else if ([keyPath isEqualToString:
[NSUserDefaultsController cs_valuesKey:kCoverStoryUnittestSourcesPatternsKey]]) {
if ([owningDocument_ hideUnittestSources]) {
// if we're hiding them then update because the pattern changed
[self rearrangeObjects];
handled = YES;
}
}
} else if ([object isEqualTo:owningDocument_]) {
for (size_t i = 0;
i < sizeof(kDocumentKeyPathsToWatch) / sizeof(kDocumentKeyPathsToWatch[0]);
++i) {
if ([keyPath isEqualToString:kDocumentKeyPathsToWatch[i]]) {
// user has changed a setting that requires a rearrange
[self rearrangeObjects];
handled = YES;
}
}
}
if (!handled) {
_GTMDevLog(@"Unexpected observance of %@ of %@ (%@)", keyPath, object, change);
}
}
- (void)updateCommonPathPrefix {
if (!owningDocument_) return;
NSString *newPrefix = nil;
// now figure out a new prefix
NSArray *arranged = [self arrangedObjects];
if ([arranged count] == 0) {
// empty string
newPrefix = @"";
} else {
// process the list to find the common prefix
// start w/ the first path, and now loop throught them all, but give up
// the moment he only common prefix is "/"
NSArray *sourcePaths = [arranged valueForKey:@"sourcePath"];
NSEnumerator *enumerator = [sourcePaths objectEnumerator];
newPrefix = [enumerator nextObject];
NSString *basePath;
while (([newPrefix length] > 1) &&
(basePath = [enumerator nextObject])) {
newPrefix = [newPrefix commonPrefixWithString:basePath
options:NSLiteralSearch];
}
// if you have two files of:
// /Foo/bar/spam.m
// /Foo/baz/wee.m
// we end up here w/ "/Foo/ba" as the common prefix, but we don't want
// to do that, so we make sure we end in a slash
if (![newPrefix hasSuffix:@"/"]) {
NSRange lastSlash = [newPrefix rangeOfString:@"/"
options:NSBackwardsSearch];
if (lastSlash.location == NSNotFound) {
newPrefix = @"";
} else {
newPrefix = [newPrefix substringToIndex:NSMaxRange(lastSlash)];
}
}
// if we just have the leading "/", use no prefix
if ([newPrefix length] <= 1) {
newPrefix = @"";
}
}
// send it back to the document
[owningDocument_ setCommonPathPrefix:newPrefix];
}
- (void)rearrangeObjects {
// this fires when the filtering changes
[super rearrangeObjects];
[self updateCommonPathPrefix];
}
- (void)setContent:(id)content {
// this fires as results are added during a load
[super setContent:content];
[self updateCommonPathPrefix];
}
@end