]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/Mailer/UIxMailFilterPanel.m
implemented flag filtering (OGo bug #1210)
[scalable-opengroupware.org] / SOGo / UI / Mailer / UIxMailFilterPanel.m
1 /*
2  Copyright (C) 2000-2005 SKYRIX Software AG
3  
4  This file is part of OpenGroupware.org.
5  
6  OGo is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License as published by the
8  Free Software Foundation; either version 2, or (at your option) any
9  later version.
10  
11  OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12  WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  License for more details.
15  
16  You should have received a copy of the GNU Lesser General Public
17  License along with OGo; see the file COPYING.  If not, write to the
18  Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20  */
21
22 #include <NGObjWeb/WOComponent.h>
23
24 @interface UIxMailFilterPanel : WOComponent
25 {
26   NSString *searchText;
27 }
28
29 @end
30
31 #include <SOGoUI/UIxComponent.h>
32 #include "common.h"
33
34 @implementation UIxMailFilterPanel
35
36 static NSArray      *filters           = nil;
37 static NSDictionary *filterToQualifier = nil;
38
39 + (void)initialize {
40   // TODO: also available: answered, draft [custom: NotJunk and Junk]
41   // Note: we currently cannot use: "flags != 'deleted'"
42   static NSString *quals[] = {
43     @"all",     nil,
44     @"read",    @"flags = 'seen'   AND NOT (flags = 'deleted')",
45     @"unread",  @"flags = 'unseen' AND NOT (flags = 'deleted')",
46     @"deleted", @"flags = 'deleted'",
47     @"flagged", @"flags = 'flagged'",
48     nil, nil
49   };
50   NSMutableDictionary *md;
51   NSMutableArray *ma;
52   unsigned i;
53   
54   md = [[NSMutableDictionary alloc] initWithCapacity:8];
55   ma = [[NSMutableArray alloc] initWithCapacity:4];
56
57   for (i = 0; quals[i] != nil; i += 2) {
58     [ma addObject:quals[i]];
59     if (quals[i + 1] != nil) {
60       [md setObject:[EOQualifier qualifierWithQualifierFormat:quals[i + 1]]
61           forKey:quals[i]];
62     }
63   }
64   
65   filterToQualifier = [md copy];
66   filters           = [ma copy];
67   [md release]; md = nil;
68   [ma release]; ma = nil;
69 }
70
71 - (void)dealloc {
72   [self->searchText release];
73   [super dealloc];
74 }
75
76 /* accessors */
77
78 - (void)setSearchText:(NSString *)_txt {
79   ASSIGNCOPY(self->searchText, _txt);
80 }
81 - (NSString *)searchText {
82   if (self->searchText == nil) {
83     // TODO: kinda hack
84     self->searchText = 
85       [[[[self context] request] formValueForKey:@"searchtext"] copy];
86   }
87   return self->searchText;
88 }
89
90 /* filters */
91
92 - (NSArray *)filters {
93   return filters;
94 }
95
96 /* qualifiers */
97
98 - (EOQualifier *)searchTextQualifier {
99   EOQualifier *q;
100   NSString *s;
101   
102   s = [self searchText];
103   if ([s length] == 0)
104     return nil;
105   
106   q = [EOQualifier qualifierWithQualifierFormat:
107                      @"(subject doesContain: %@) OR "
108                      @"(from doesContain: %@)",
109                      s, s];
110   return q;
111 }
112
113 - (NSString *)filterLabel {
114 #if 1
115   return [[[self context] page] labelForKey:[self valueForKey:@"filter"]];
116 #else
117   return [self valueForKey:@"filter"];
118 #endif
119 }
120
121 - (NSString *)selectedFilter {
122   return  [[[self context] request] formValueForKey:@"filterpopup"];
123 }
124
125 - (EOQualifier *)filterQualifier {
126   NSString *selectedFilter;
127   
128   selectedFilter = [self selectedFilter];
129   
130   return [selectedFilter length] > 0
131     ? [filterToQualifier objectForKey:selectedFilter] : nil;
132 }
133
134 - (EOQualifier *)qualifier {
135   EOQualifier *sq, *fq;
136   NSArray *qa;
137   
138   sq = [self searchTextQualifier];
139   fq = [self filterQualifier];
140   
141   if (fq == nil) return sq;
142   if (sq == nil) return fq;
143   
144   qa = [NSArray arrayWithObjects:fq, sq, nil];
145   return [[[EOAndQualifier alloc] initWithQualifierArray:qa] autorelease];
146 }
147
148 @end /* UIxMailFilterPanel */