2 Copyright (C) 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
22 #include "SOGoMailFolderDataSource.h"
23 #include "SOGoMailManager.h"
24 #include <NGObjWeb/EOFetchSpecification+SoDAV.h>
25 #include <NGImap4/NGImap4Envelope.h>
28 @implementation SOGoMailFolderDataSource
30 static BOOL debugOn = NO;
32 - (id)initWithImap4URL:(NSURL *)_imap4URL imap4Password:(NSString *)_pwd {
33 if (_imap4URL == nil) {
38 if ((self = [super init])) {
39 self->imap4URL = [_imap4URL copy];
40 self->imap4Password = [_pwd copy];
45 return [self initWithImap4URL:nil imap4Password:nil];
49 [self->imap4Password release];
50 [self->imap4URL release];
51 [self->fetchSpecification release];
57 - (void)setFetchSpecification:(EOFetchSpecification *)_fetchSpec {
58 if ([_fetchSpec isEqual:self->fetchSpecification]) return;
60 ASSIGN(self->fetchSpecification, _fetchSpec);
61 [self postDataSourceChangedNotification];
63 - (EOFetchSpecification *)fetchSpecification {
64 return self->fetchSpecification;
68 return self->imap4URL;
71 - (NGImap4ConnectionManager *)mailManager {
72 static NGImap4ConnectionManager *mm = nil;
74 mm = [[NGImap4ConnectionManager defaultConnectionManager] retain];
80 - (NSArray *)partsForWebDAVPropertyNames:(NSArray *)_names {
82 static NSArray *parts = nil;
84 // [self logWithFormat:@"props: %@", _names];
87 parts = [[NSArray alloc] initWithObjects:
88 @"FLAGS", @"ENVELOPE", @"RFC822.SIZE", nil];
93 - (void)addRecordsForFolderNames:(NSArray *)_n toArray:(NSMutableArray *)_r {
96 for (i = 0, count = [_n count]; i < count; i++) {
98 NSString *keys[2], *values[2];
100 keys[0] = @"{DAV:}href"; values[0] = [_n objectAtIndex:i];
101 keys[1] = @"davResourceType"; values[1] = @"collection";
102 rec = [[NSDictionary alloc] initWithObjects:values forKeys:keys count:2];
108 - (void)addRecordsForUIDs:(NSArray *)_uids toArray:(NSMutableArray *)_r {
109 NSAutoreleasePool *pool;
110 NSArray *partNames, *results;
113 if ([_uids count] == 0)
116 pool = [[NSAutoreleasePool alloc] init];
118 partNames = [self partsForWebDAVPropertyNames:
119 [[self fetchSpecification] selectedWebDAVPropertyNames]];
121 results = [[self mailManager] fetchUIDs:_uids inURL:self->imap4URL
122 parts:partNames password:self->imap4Password];
123 results = [results valueForKey:@"fetch"];
125 for (i = 0, count = [results count]; i < count; i++) {
126 NGImap4Envelope *envelope;
127 NSDictionary *result;
132 result = [results objectAtIndex:i];
133 envelope = [result valueForKey:@"envelope"];
134 // NSLog(@"RES: %@", result);
136 keys[0] = @"{DAV:}href";
137 values[0] = [[[result objectForKey:@"uid"] stringValue]
138 stringByAppendingString:@".mail"];
139 keys[1] = @"davResourceType";
141 keys[2] = @"davContentLength";
142 values[2] = [result objectForKey:@"size"];
143 keys[3] = @"davDisplayName";
144 values[3] = [envelope subject];
145 keys[4] = @"davLastModified";
146 values[4] = [envelope date];
148 rec = [[NSDictionary alloc] initWithObjects:values forKeys:keys count:5];
158 - (NSArray *)fetchObjects {
159 NSMutableArray *results;
160 EOQualifier *qualifier;
161 NSArray *sortOrderings;
162 NSArray *uids, *folderNames;
165 // TODO: support [fs davBulkTargetKeys]
166 if ([[self fetchSpecification] davBulkTargetKeys] != nil) {
167 [self logWithFormat:@"unsupported fetch specification"];
171 /* fetch message uids */
173 // TODO: translate WebDAV qualifier and sort-ordering into IMAP4 one
174 uids = [[self mailManager] fetchUIDsInURL:self->imap4URL
175 qualifier:nil sortOrdering:@"DATE"
176 password:self->imap4Password];
180 folderNames = [[self mailManager] subfoldersForURL:self->imap4URL
181 password:self->imap4Password];
185 if ((total = ([uids count] + [folderNames count])) == 0)
186 return [NSArray array];
188 results = [NSMutableArray arrayWithCapacity:total];
190 [self addRecordsForFolderNames:folderNames toArray:results];
191 [self addRecordsForUIDs:uids toArray:results];
193 /* filter and sort results */
195 if ((qualifier = [[self fetchSpecification] qualifier]) != nil)
196 results = (id)[results filteredArrayUsingQualifier:qualifier];
198 if ((sortOrderings = [[self fetchSpecification] sortOrderings]) != nil) {
199 if (qualifier != nil)
200 results = (id)[results sortedArrayUsingKeyOrderArray:sortOrderings];
202 [results sortUsingKeyOrderArray:sortOrderings];
210 - (NSString *)loggingPrefix {
211 return @"[mailfolder-ds]";
213 - (BOOL)isDebuggingEnabled {
217 @end /* SOGoMailFolderDataSource */