-
Notifications
You must be signed in to change notification settings - Fork 56
/
SearchProfile.m
202 lines (171 loc) · 5.37 KB
/
SearchProfile.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
//
// SearchProfile.m
// MetaZ
//
// Created by Brian Olsen on 15/10/09.
// Copyright 2009 Maven-Group. All rights reserved.
//
#import "SearchProfile.h"
@interface ProfileState : NSObject
{
NSString* tag;
BOOL state;
}
+ (id)stateWithTag:(NSString *)tag;
- (id)initWithTag:(NSString *)tag;
@property (readonly) NSString* tag;
@property (assign) BOOL state;
@end
@implementation SearchProfile
+ (SearchProfile*)unknownTypeProfile
{
return [self profileWithIdentifier:@"unknown" mainTag:MZTitleTagIdent
tag:[NSArray arrayWithObjects:
MZTitleTagIdent, MZChaptersTagIdent, nil]];
}
+ (SearchProfile*)tvShowProfile
{
return [self profileWithIdentifier:@"tvShow" mainTag:MZTitleTagIdent
tag:[NSArray arrayWithObjects:
MZTitleTagIdent, MZVideoTypeTagIdent, MZTVShowTagIdent,
MZTVSeasonTagIdent, MZTVEpisodeTagIdent, MZChaptersTagIdent, nil]];
}
+ (SearchProfile*)movieProfile
{
return [self profileWithIdentifier:@"movie" mainTag:MZTitleTagIdent
tag:[NSArray arrayWithObjects:
MZTitleTagIdent, MZVideoTypeTagIdent, MZChaptersTagIdent, nil]];
}
+ (id)profileWithIdentifier:(NSString *)ident mainTag:(NSString *)main tag:(NSArray *)tags;
{
return [[[self alloc] initWithIdentifier:ident mainTag:main tag:tags] autorelease];
}
- (id)initWithIdentifier:(NSString *)ident mainTag:(NSString *)main tag:(NSArray *)theTags;
{
self = [super init];
if(self)
{
identifier = [ident retain];
mainTag = [main retain];
NSMutableArray* arr = [NSMutableArray array];
NSDictionary* states = [[NSUserDefaults standardUserDefaults] dictionaryForKey:[@"profiles." stringByAppendingString:ident]];
for(NSString* tag in theTags)
{
ProfileState* state = [ProfileState stateWithTag:tag];
if(states)
{
NSNumber* num = [states objectForKey:tag];
if(num)
state.state = [num boolValue];
}
[arr addObject:state];
}
tags = [[NSArray alloc] initWithArray:arr];
}
return self;
}
- (void)dealloc
{
[identifier release];
[mainTag release];
[tags release];
[checkObj release];
[checkPrefix release];
[super dealloc];
}
@synthesize identifier;
@synthesize mainTag;
- (NSArray *)tags
{
return [tags arrayByPerformingSelector:@selector(tag)];
}
- (void)setCheckObject:(id)obj withPrefix:(NSString *)prefix
{
[checkObj release];
[checkPrefix release];
checkObj = [obj retain];
checkPrefix = [prefix retain];
if(!checkPrefix)
checkPrefix = @"";
}
- (void)switchItem:(NSMenuItem *)sender
{
NSInteger tag = [sender tag];
ProfileState* state = [tags objectAtIndex:tag];
state.state = !state.state;
[sender setState:(state.state ? NSOnState : NSOffState)];
[self willChangeValueForKey:@"searchTerms"];
NSString* defaultKey = [@"profiles." stringByAppendingString:identifier];
NSMutableDictionary* states = [NSMutableDictionary dictionaryWithDictionary:
[[NSUserDefaults standardUserDefaults] dictionaryForKey:
defaultKey]];
for(ProfileState* myState in tags)
[states setObject:[NSNumber numberWithBool:myState.state] forKey:myState.tag];
[[NSUserDefaults standardUserDefaults] setObject:states forKey:defaultKey];
[self didChangeValueForKey:@"searchTerms"];
}
- (void)alterState:(NSMenuItem *)sender
{
NSInteger tag = [sender tag];
ProfileState* state = [tags objectAtIndex:tag];
[sender setState:(state.state ? NSOnState : NSOffState)];
}
- (NSMutableDictionary *)searchTerms:(NSString *)mainTerm
{
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
for(ProfileState* state in tags)
{
if(state.state)
{
id value;
if([state.tag isEqual:mainTag])
value = mainTerm;
else
value = [self valueForUndefinedKey:state.tag];
if(value != nil && value != [NSNull null] && value != NSNoSelectionMarker &&
value != NSMultipleValuesMarker && value != NSNotApplicableMarker)
{
if([value isKindOfClass:[NSString class]])
{
value = [value stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
}
[dict setObject:value forKey:state.tag];
}
}
}
return dict;
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
NSInteger tag = [menuItem tag];
ProfileState* state = [tags objectAtIndex:tag];
id value = [self valueForUndefinedKey:state.tag];
BOOL ret = value != nil && value != [NSNull null] && value != NSNoSelectionMarker &&
value != NSMultipleValuesMarker && value != NSNotApplicableMarker;
[menuItem setState:(state.state && ret ? NSOnState : NSOffState)];
return ret;
}
- (id)valueForUndefinedKey:(NSString *)key
{
return [checkObj protectedValueForKeyPath:[checkPrefix stringByAppendingString:key]];
}
@end
@implementation ProfileState
+ (id)stateWithTag:(NSString *)tag
{
return [[[self alloc] initWithTag:tag] autorelease];
}
- (id)initWithTag:(NSString *)theTag
{
self = [super init];
if(self)
{
tag = [theTag retain];
state = YES;
}
return self;
}
@synthesize tag;
@synthesize state;
@end