]> err.no Git - scalable-opengroupware.org/blob - SOGo/SoObjects/Mailer/SOGoMailFolderDataSource.m
consolidated imap client objects to NGImap4
[scalable-opengroupware.org] / SOGo / SoObjects / Mailer / SOGoMailFolderDataSource.m
1 /*
2   Copyright (C) 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 "SOGoMailFolderDataSource.h"
23 #include "SOGoMailManager.h"
24 #include <NGObjWeb/EOFetchSpecification+SoDAV.h>
25 #include <NGImap4/NGImap4Envelope.h>
26 #include "common.h"
27
28 @implementation SOGoMailFolderDataSource
29
30 static BOOL debugOn = NO;
31
32 - (id)initWithImap4URL:(NSURL *)_imap4URL imap4Password:(NSString *)_pwd {
33   if (_imap4URL == nil) {
34     [self release];
35     return nil;
36   }
37
38   if ((self = [super init])) {
39     self->imap4URL      = [_imap4URL copy];
40     self->imap4Password = [_pwd copy];
41   }
42   return self;
43 }
44 - (id)init {
45   return [self initWithImap4URL:nil imap4Password:nil];
46 }
47
48 - (void)dealloc {
49   [self->imap4Password      release];
50   [self->imap4URL           release];
51   [self->fetchSpecification release];
52   [super dealloc];
53 }
54
55 /* accessors */
56
57 - (void)setFetchSpecification:(EOFetchSpecification *)_fetchSpec {
58   if ([_fetchSpec isEqual:self->fetchSpecification]) return;
59   
60   ASSIGN(self->fetchSpecification, _fetchSpec);
61   [self postDataSourceChangedNotification];
62 }
63 - (EOFetchSpecification *)fetchSpecification {
64   return self->fetchSpecification;
65 }
66
67 - (NSURL *)imap4URL {
68   return self->imap4URL;
69 }
70
71 - (NGImap4ConnectionManager *)mailManager {
72   static NGImap4ConnectionManager *mm = nil;
73   if (mm == nil) 
74     mm = [[NGImap4ConnectionManager defaultConnectionManager] retain];
75   return mm;
76 }
77
78 /* fetches */
79
80 - (NSArray *)partsForWebDAVPropertyNames:(NSArray *)_names {
81   // TODO: implement
82   static NSArray *parts = nil;
83
84   // [self logWithFormat:@"props: %@", _names];
85   
86   if (parts == nil) {
87     parts = [[NSArray alloc] initWithObjects:
88                                @"FLAGS", @"ENVELOPE", @"RFC822.SIZE", nil];
89   }
90   return parts;
91 }
92
93 - (void)addRecordsForFolderNames:(NSArray *)_n toArray:(NSMutableArray *)_r {
94   unsigned i, count;
95   
96   for (i = 0, count = [_n count]; i < count; i++) {
97     NSDictionary *rec;
98     NSString *keys[2], *values[2];
99     
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];
103     [_r addObject:rec];
104     [rec release];
105   }
106 }
107
108 - (void)addRecordsForUIDs:(NSArray *)_uids toArray:(NSMutableArray *)_r {
109   NSAutoreleasePool *pool;
110   NSArray  *partNames, *results;
111   unsigned i, count;
112   
113   if ([_uids count] == 0)
114     return;
115
116   pool = [[NSAutoreleasePool alloc] init];
117   
118   partNames = [self partsForWebDAVPropertyNames:
119                       [[self fetchSpecification] selectedWebDAVPropertyNames]];
120   
121   results = [[self mailManager] fetchUIDs:_uids inURL:self->imap4URL
122                                 parts:partNames password:self->imap4Password];
123   results = [results valueForKey:@"fetch"];
124
125   for (i = 0, count = [results count]; i < count; i++) {
126     NGImap4Envelope *envelope;
127     NSDictionary *result;
128     NSDictionary *rec;
129     NSString *keys[6];
130     id       values[6];
131     
132     result   = [results objectAtIndex:i];
133     envelope = [result valueForKey:@"envelope"];
134     // NSLog(@"RES: %@", result);
135     
136     keys[0]   = @"{DAV:}href";      
137     values[0] = [[[result objectForKey:@"uid"] stringValue]
138                   stringByAppendingString:@".mail"];
139     keys[1]   = @"davResourceType";
140     values[1] = @"";
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];
147     
148     rec = [[NSDictionary alloc] initWithObjects:values forKeys:keys count:5];
149     [_r addObject:rec];
150     [rec release];
151   }
152
153   [pool release];
154 }
155
156 /* operations */
157
158 - (NSArray *)fetchObjects {
159   NSMutableArray *results;
160   EOQualifier    *qualifier;
161   NSArray        *sortOrderings;
162   NSArray  *uids, *folderNames;
163   unsigned total;
164   
165   // TODO: support [fs davBulkTargetKeys]
166   if ([[self fetchSpecification] davBulkTargetKeys] != nil) {
167     [self logWithFormat:@"unsupported fetch specification"];
168     return nil;
169   }
170   
171   /* fetch message uids */
172   
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];
177   
178   /* fetch folders */
179   
180   folderNames = [[self mailManager] subfoldersForURL:self->imap4URL
181                                     password:self->imap4Password];
182   
183   /* builds results */
184   
185   if ((total = ([uids count] + [folderNames count])) == 0)
186     return [NSArray array];
187   
188   results = [NSMutableArray arrayWithCapacity:total];
189   
190   [self addRecordsForFolderNames:folderNames toArray:results];
191   [self addRecordsForUIDs:uids               toArray:results];
192   
193   /* filter and sort results */
194   
195   if ((qualifier = [[self fetchSpecification] qualifier]) != nil)
196     results = (id)[results filteredArrayUsingQualifier:qualifier];
197   
198   if ((sortOrderings = [[self fetchSpecification] sortOrderings]) != nil) {
199     if (qualifier != nil)
200       results = (id)[results sortedArrayUsingKeyOrderArray:sortOrderings];
201     else
202       [results sortUsingKeyOrderArray:sortOrderings];
203   }
204   
205   return results;
206 }
207
208 /* logging */
209
210 - (NSString *)loggingPrefix {
211   return @"[mailfolder-ds]";
212 }
213 - (BOOL)isDebuggingEnabled {
214   return debugOn;
215 }
216
217 @end /* SOGoMailFolderDataSource */