2 Copyright (C) 2004-2005 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
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
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.
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
25 This component represent a list of mails and is attached to an SOGoMailFolder
29 #import <Foundation/NSCalendarDate.h>
30 #import <Foundation/NSDictionary.h>
31 #import <Foundation/NSValue.h>
32 #import <NGObjWeb/WOResponse.h>
33 #import <NGObjWeb/WORequest.h>
34 #import <NGObjWeb/SoObject+SoDAV.h>
35 #import <NGObjWeb/NSException+HTTP.h>
36 #import <NGExtensions/NSNull+misc.h>
37 #import <NGExtensions/NSString+misc.h>
39 #import <EOControl/EOQualifier.h>
41 #import <SoObjects/Mailer/SOGoMailFolder.h>
42 #import <SoObjects/Mailer/SOGoMailObject.h>
43 #import <SoObjects/SOGo/SOGoDateFormatter.h>
44 #import <SoObjects/SOGo/SOGoUser.h>
46 #import "UIxMailListView.h"
48 #define messagesPerPage 50
49 static int attachmentFlagSize = 8096;
51 @implementation UIxMailListView
57 if ((self = [super init]))
60 user = [context activeUser];
61 ASSIGN (dateFormatter, [user dateFormatterInContext: context]);
62 ASSIGN (userTimeZone, [user timeZone]);
74 [dateFormatter release];
75 [userTimeZone release];
81 - (void) setMessage: (id) _msg
83 ASSIGN(message, _msg);
91 - (NSString *) messageDate
93 NSCalendarDate *messageDate;
95 messageDate = [[message valueForKey: @"envelope"] date];
96 [messageDate setTimeZone: userTimeZone];
98 return [dateFormatter formattedDateAndTime: messageDate];
101 - (NSString *) messageSubject
106 envSubject = [[message valueForKey: @"envelope"] subject];
107 if ([envSubject isKindOfClass: [NSData class]])
109 subject = [[NSString alloc] initWithData: envSubject
110 encoding: NSUTF8StringEncoding];
111 [subject autorelease];
114 subject = envSubject;
119 - (BOOL) showToAddress
123 ftype = [[self clientObject] valueForKey:@"outlookFolderClass"];
124 return [ftype isEqual:@"IPF.Sent"];
129 - (NSString *) objectTitle
131 return [[self clientObject] nameInContainer];
134 - (NSString *) panelTitle
138 s = [self labelForKey:@"View Mail Folder"];
139 s = [s stringByAppendingString:@": "];
140 s = [s stringByAppendingString:[self objectTitle]];
144 /* derived accessors */
146 - (BOOL) isMessageDeleted
150 flags = [[self message] valueForKey:@"flags"];
151 return [flags containsObject:@"deleted"];
154 - (BOOL) isMessageRead
158 flags = [[self message] valueForKey:@"flags"];
159 return [flags containsObject:@"seen"];
162 - (NSString *) messageUidString
164 return [[[self message] valueForKey:@"uid"] stringValue];
167 - (NSString *) messageRowStyleClass
169 return [self isMessageDeleted]
170 ? @"mailer_listcell_deleted"
171 : @"mailer_listcell_regular";
174 - (NSString *) messageSubjectCellStyleClass
179 flags = [[self message] valueForKey:@"flags"];
181 if ([flags containsObject: @"seen"])
183 if ([flags containsObject: @"answered"])
185 if ([flags containsObject: @"$forwarded"])
186 cellClass = @"mailer_forwardedrepliedmailsubject";
188 cellClass = @"mailer_repliedmailsubject";
190 else if ([flags containsObject: @"$forwarded"])
191 cellClass = @"mailer_forwardedmailsubject";
193 cellClass = @"mailer_readmailsubject";
196 cellClass = @"mailer_unreadmailsubject";
199 // return ([self isMessageRead]
200 // ? @"mailer_readmailsubject"
201 // : @"mailer_unreadmailsubject");
204 - (BOOL) hasMessageAttachment
206 /* we detect attachments by size ... */
209 size = [[[self message] valueForKey:@"size"] intValue];
210 return size > attachmentFlagSize;
213 /* fetching messages */
215 - (NSArray *) fetchKeys
217 /* Note: see SOGoMailManager.m for allowed IMAP4 keys */
218 static NSArray *keys = nil;
220 keys = [[NSArray alloc] initWithObjects: @"UID",
221 @"FLAGS", @"ENVELOPE", @"RFC822.SIZE", nil];
226 - (NSString *) defaultSortKey
231 - (NSString *) imap4SortKey
235 sort = [[context request] formValueForKey: @"sort"];
238 sort = [self defaultSortKey];
240 return [sort uppercaseString];
243 - (NSString *) imap4SortOrdering
245 NSString *sort, *ascending;
247 sort = [self imap4SortKey];
249 ascending = [[context request] formValueForKey: @"asc"];
250 if (![ascending boolValue])
251 sort = [@"REVERSE " stringByAppendingString: sort];
256 - (NSRange) fetchRange
258 if (firstMessageNumber == 0)
259 return NSMakeRange(0, messagesPerPage);
260 return NSMakeRange(firstMessageNumber - 1, messagesPerPage);
263 - (NSArray *) sortedUIDs
265 EOQualifier *fetchQualifier, *notDeleted;
268 notDeleted = [EOQualifier qualifierWithQualifierFormat:
269 @"(not (flags = %@))",
273 fetchQualifier = [[EOAndQualifier alloc] initWithQualifiers:
274 notDeleted, qualifier,
276 [fetchQualifier autorelease];
279 fetchQualifier = notDeleted;
282 = [[self clientObject] fetchUIDsMatchingQualifier: fetchQualifier
283 sortOrdering: [self imap4SortOrdering]];
290 - (unsigned int) totalMessageCount
292 return [sortedUIDs count];
295 - (BOOL) showsAllMessages
297 return ([[self sortedUIDs] count] <= [self fetchRange].length) ? YES : NO;
300 - (NSRange) fetchBlock
306 r = [self fetchRange];
307 uids = [self sortedUIDs];
309 /* only need to restrict if we have a lot */
310 if ((len = [uids count]) <= r.length) {
316 if (len < r.location) {
317 // TODO: CHECK CONDITION (< vs <=)
318 /* out of range, recover at first block */
323 if (r.location + r.length > len)
324 r.length = len - r.location;
328 - (unsigned int) firstMessageNumber
330 return [self fetchBlock].location + 1;
333 - (unsigned int) lastMessageNumber
337 r = [self fetchBlock];
338 return r.location + r.length;
343 return [self fetchBlock].location == 0 ? NO : YES;
348 NSRange r = [self fetchBlock];
349 return r.location + r.length >= [[self sortedUIDs] count] ? NO : YES;
352 - (unsigned int) nextFirstMessageNumber
354 return [self firstMessageNumber] + [self fetchRange].length;
357 - (unsigned int) prevFirstMessageNumber
362 idx = [self firstMessageNumber];
363 r = [self fetchRange];
365 return (idx - r.length);
369 - (NSArray *) messages
379 r = [self fetchBlock];
380 uids = [self sortedUIDs];
381 if ((len = [uids count]) > r.length)
382 /* only need to restrict if we have a lot */
383 uids = [uids subarrayWithRange:r];
385 msgs = [[self clientObject] fetchUIDs: uids parts: [self fetchKeys]];
386 messages = [[msgs valueForKey: @"fetch"] retain];
393 - (NSString *) messageViewTarget
395 return [NSString stringWithFormat: @"SOGo_msg_%@",
396 [self messageUidString]];
399 - (NSString *) messageViewURL
401 // TODO: noframe only when view-target is empty
402 // TODO: markread only if the message is unread
405 s = [[self messageUidString] stringByAppendingString:@"/view?noframe=1"];
406 if (![self isMessageRead]) s = [s stringByAppendingString:@"&markread=1"];
409 - (NSString *) markReadURL
411 return [@"markMessageRead?uid=" stringByAppendingString:
412 [self messageUidString]];
414 - (NSString *) markUnreadURL
416 return [@"markMessageUnread?uid=" stringByAppendingString:
417 [self messageUidString]];
422 - (NSString *)msgRowID
424 return [@"row_" stringByAppendingString:[self messageUidString]];
427 - (NSString *)msgDivID
429 return [@"div_" stringByAppendingString:[self messageUidString]];
432 - (NSString *)msgIconReadImgID
434 return [@"readdiv_" stringByAppendingString:[self messageUidString]];
437 - (NSString *)msgIconUnreadImgID
439 return [@"unreaddiv_" stringByAppendingString:[self messageUidString]];
442 /* error redirects */
444 - (id) redirectToViewWithError: (id) _error
446 // TODO: DUP in UIxMailAccountView
447 // TODO: improve, localize
448 // TODO: there is a bug in the treeview which preserves the current URL for
449 // the active object (displaying the error again)
452 if (![_error isNotNull])
453 return [self redirectToLocation:@"view"];
455 if ([_error isKindOfClass:[NSException class]])
456 _error = [_error reason];
457 else if ([_error isKindOfClass:[NSString class]])
458 _error = [_error stringValue];
460 url = [_error stringByEscapingURL];
461 url = [@"view?error=" stringByAppendingString:url];
462 return [self redirectToLocation:url];
467 - (int) firstMessageOfPageFor: (int) messageNbr
469 NSArray *messageNbrs;
473 messageNbrs = [self sortedUIDs];
475 = [messageNbrs indexOfObject: [NSNumber numberWithInt: messageNbr]];
477 firstMessage = ((int) (nbrInArray / messagesPerPage)
478 * messagesPerPage) + 1;
485 - (void) _setQualifierForCriteria: (NSString *) criteria
486 andValue: (NSString *) value
490 if ([criteria isEqualToString: @"subject"])
491 qualifier = [EOQualifier qualifierWithQualifierFormat:
492 @"(subject doesContain: %@)", value];
493 else if ([criteria isEqualToString: @"sender"])
494 qualifier = [EOQualifier qualifierWithQualifierFormat:
495 @"(from doesContain: %@)", value];
496 else if ([criteria isEqualToString: @"subject_or_sender"])
497 qualifier = [EOQualifier qualifierWithQualifierFormat:
498 @"((subject doesContain: %@)"
499 @" OR (from doesContain: %@))",
501 else if ([criteria isEqualToString: @"to_or_cc"])
502 qualifier = [EOQualifier qualifierWithQualifierFormat:
503 @"((to doesContain: %@)"
504 @" OR (cc doesContain: %@))",
506 else if ([criteria isEqualToString: @"entire_message"])
507 qualifier = [EOQualifier qualifierWithQualifierFormat:
508 @"(body doesContain: %@)", value];
518 NSString *specificMessage, *searchCriteria, *searchValue;
520 request = [context request];
522 [[self clientObject] flushMailCaches];
524 specificMessage = [request formValueForKey: @"pageforuid"];
525 searchCriteria = [request formValueForKey: @"search"];
526 searchValue = [request formValueForKey: @"value"];
527 if ([searchValue length])
528 [self _setQualifierForCriteria: searchCriteria
529 andValue: searchValue];
533 ? [self firstMessageOfPageFor: [specificMessage intValue]]
534 : [[request formValueForKey:@"idx"] intValue]);
540 // TODO: we might want to flush the caches?
543 if ((client = [self clientObject]) == nil) {
544 return [NSException exceptionWithHTTPStatus:404 /* Not Found */
545 reason:@"did not find mail folder"];
548 if (![client respondsToSelector:@selector(flushMailCaches) ])
550 return [NSException exceptionWithHTTPStatus: 500 /* Server Error */
552 @"invalid client object (does not support flush)"];
555 [client flushMailCaches];
557 return [self redirectToLocation:@"view"];
560 - (NSString *) msgLabels
562 NSMutableArray *labels;
564 NSString *currentFlag;
566 labels = [NSMutableArray new];
567 [labels autorelease];
569 flags = [[message objectForKey: @"flags"] objectEnumerator];
570 while ((currentFlag = [flags nextObject]))
571 if ([currentFlag hasPrefix: @"$label"])
572 [labels addObject: [currentFlag substringFromIndex: 1]];
574 return [labels componentsJoinedByString: @" "];
579 /* UIxMailListView */