forked from ViennaRSS/vienna-rss
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArticleFilter.m
169 lines (152 loc) · 4.49 KB
/
ArticleFilter.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
//
// ArticleFilter.m
// Vienna
//
// Created by Steve on 3/24/06.
// Copyright (c) 2004-2006 Steve Palmer. All rights reserved.
//
// 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 "ArticleFilter.h"
#import "CalendarExtensions.h"
#import "Preferences.h"
#import "Constants.h"
#import "Message.h"
@interface ArticleFilter (Private)
+(void)createFilter:(NSString *)name tag:(NSInteger)tag comparator:(SEL)comparator;
@end
// There's just one global filter list.
static NSMutableArray * _filterList = nil;
@implementation ArticleFilter
/* unreadArticleFilterComparator
* Returns TRUE if the specified article is unread.
*/
+(BOOL)unreadArticleFilterComparator:(Article *)theArticle
{
return ![theArticle isRead];
}
/* flaggedArticleFilterComparator
* Returns TRUE if the specified article is flagged.
*/
+(BOOL)flaggedArticleFilterComparator:(Article *)theArticle
{
return [theArticle isFlagged];
}
/* todayFilterComparator
* Returns TRUE if the specified article was posted today.
*/
+(BOOL)todayFilterComparator:(Article *)theArticle
{
return ([[theArticle date] compare:[NSCalendarDate today]] != NSOrderedAscending);
}
/* lastRefreshFilterComparator
* Returns TRUE if the specified article is the same as or newer than the last refresh date.
*/
+(BOOL)lastRefreshFilterComparator:(Article *)theArticle
{
return ([[theArticle createdDate] compare:[[Preferences standardPreferences] objectForKey:MAPref_LastRefreshDate]] != NSOrderedAscending);
}
/* initalize
* Create all the default filters. To add a new filter:
*
* 1. Add an unique tag ID for the filter in constants.h (MA_Filter_xxx).
* 2. Add a comparator in this file that returns TRUE if the article should be filtered IN.
* 3. Add a new line below with the filter name, tag and comparator.
* 4. Localise the filter name in the Localizable.strings for each language.
*
* That's it really. The filtering and menu is handled automatically.
*/
+(void)initialize
{
[ArticleFilter createFilter:@"All Articles" tag:MA_Filter_All comparator:nil];
[ArticleFilter createFilter:@"Unread Articles" tag:MA_Filter_Unread comparator:@selector(unreadArticleFilterComparator:)];
[ArticleFilter createFilter:@"Last Refresh" tag:MA_Filter_LastRefresh comparator:@selector(lastRefreshFilterComparator:)];
[ArticleFilter createFilter:@"Today" tag:MA_Filter_Today comparator:@selector(todayFilterComparator:)];
[ArticleFilter createFilter:@"Flagged" tag:MA_Filter_Flagged comparator:@selector(flaggedArticleFilterComparator:)];
}
/* arrayOfFilters
* Return the array of filters.
*/
+(NSArray *)arrayOfFilters
{
return _filterList;
}
/* filterByTag
* Returns the filter identified by the specified tag or nil if there's no filter
* with the given tag.
*/
+(ArticleFilter *)filterByTag:(NSInteger)theTag
{
NSInteger index;
for (index = 0; index < [_filterList count]; ++index)
{
ArticleFilter * filter = [_filterList objectAtIndex:index];
if ([filter tag] == theTag)
return filter;
}
return nil;
}
/* initWithName
* This is the designated initialiser for a new ArticleFilter object.
*/
-(id)initWithName:(NSString *)theName tag:(NSInteger)theTag comparator:(SEL)theComparator
{
if ((self = [super init]) != nil)
{
name = [theName retain];
tag = theTag;
comparator = theComparator;
}
return self;
}
/* name
* Return the filter name.
*/
-(NSString *)name
{
return name;
}
/* comparator
* Return the filter comparator function.
*/
-(SEL)comparator
{
return comparator;
}
/* tag
* Return the filter's unique ID (tag)
*/
-(NSInteger)tag
{
return tag;
}
/* createFilter
* Create a new article filter with the specified name and comparator.
*/
+(void)createFilter:(NSString *)name tag:(NSInteger)tag comparator:(SEL)comparator
{
ArticleFilter * newFilter = [[ArticleFilter alloc] initWithName:name tag:tag comparator:comparator];
if (_filterList == nil)
_filterList = [[NSMutableArray alloc] init];
[_filterList addObject:newFilter];
[newFilter release];
}
/* dealloc
* Clean up behind us.
*/
-(void)dealloc
{
[name release];
[super dealloc];
}
@end