]> err.no Git - sope/blob - sope-mime/NGImap4/NGImap4ServerRoot.m
new Xcode projects
[sope] / sope-mime / NGImap4 / NGImap4ServerRoot.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OGo
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 // $Id$
22
23 #include "NGImap4ServerRoot.h"
24 #include "NGImap4Context.h"
25 #include "NGImap4Client.h"
26 #include "NGImap4Message.h"
27 #include "NGImap4Functions.h"
28 #include "NGImap4Folder.h"
29 #include "imCommon.h"
30
31 @interface NGImap4ServerRoot(Private)
32 - (void)initializeSubFolders;
33 @end
34
35 @implementation NGImap4ServerRoot
36
37 static int ShowNonExistentFolder                      = -1;
38 static int FetchNewUnseenMessagesInSubFoldersOnDemand = -1;
39
40 + (void)initialize {
41   NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
42
43   // TODO: document the meaning!
44   ShowNonExistentFolder =
45     [ud boolForKey:@"ShowNonExistentFolder"] ? 1 : 0;
46   FetchNewUnseenMessagesInSubFoldersOnDemand =
47     [ud boolForKey:@"FetchNewUnseenMessagesInSubFoldersOnDemand"] ? 1 : 0;
48 }
49
50 + (id)serverRootWithContext:(NGImap4Context *)_context {
51   return [[[self alloc] initServerRootWithContext:_context] autorelease];
52 }
53
54 - (id)init {
55   [self release];
56   [self doesNotRecognizeSelector:_cmd];
57   return nil;
58 }
59
60 - (id)initServerRootWithContext:(NGImap4Context *)_context {
61   if ((self = [super init])) {
62     self->context    = [_context retain];
63     self->name       = [[_context host] copy];
64     self->subFolders = nil;
65
66     self->noinferiors = ([[_context serverKind] isEqualToString:@"courier"])
67       ? YES : NO;
68   }
69   return self;
70 }
71
72 - (void)dealloc {
73   [self->context    resetSpecialFolders];
74   [self->context    release];
75   [self->name       release];
76   [self->subFolders makeObjectsPerformSelector:@selector(clearParentFolder)];
77   [self->subFolders release];
78   [super dealloc];
79 }
80
81 - (NSException *)lastException {
82   return [self->context lastException];
83 }
84 - (void)resetLastException {
85   [self->context resetLastException];
86 }
87
88 - (BOOL)isEqual:(id)_obj {
89   if (self == _obj)
90     return YES;
91   if ([_obj isKindOfClass:[NGImap4ServerRoot class]])
92     return [self isEqualToServerRoot:_obj];
93   return NO;
94 }
95
96 - (BOOL)isEqualToServerRoot:(NGImap4ServerRoot *)_root {
97   if (_root == self)
98     return YES;
99   if ([_root context] == self->context)
100     return YES;
101   return NO;
102 }
103
104 /* accessors */
105
106 - (NGImap4Context *)context {
107   return self->context;
108 }
109
110 - (NSString *)name {
111   return self->name;
112 }
113
114 - (NSString *)absoluteName {
115   return self->name;
116 }
117
118 - (NSArray *)messages {
119   return nil;
120 }
121
122 - (NSArray *)messagesForQualifier:(EOQualifier *)_qualifier {
123   return nil;
124 }
125
126 - (NSArray *)messagesForQualifier:(EOQualifier *)_q maxCount:(int)_cnt {
127   return nil;
128 }
129
130 - (NSArray *)fetchSortedMessages:(NSArray *)_so {
131   return nil;
132 }
133
134 - (void)bulkFetchHeadersFor:(NSArray *)_array inRange:(NSRange)_aRange {
135 }
136
137 - (void)bulkFetchHeadersFor:(NSArray *)_array inRange:(NSRange)_aRange
138   withAllUnread:(BOOL)_unread
139 {
140 }
141
142 - (NSArray *)messageFlags { 
143   return nil;
144 }
145
146 - (NSArray *)subFolders {
147   if (self->subFolders == nil)
148     [self initializeSubFolders];
149   return self->subFolders;
150 }
151
152 - (NGImap4Folder *)subFolderWithName:(NSString *)_name
153   caseInsensitive:(BOOL)_caseIns
154 {
155   return _subFolderWithName(self, _name, _caseIns);
156 }
157
158 - (id<NGImap4Folder>)parentFolder {
159   return nil;
160 }
161
162 - (BOOL)isReadOnly {
163   return YES;
164 }
165
166 - (BOOL)noinferiors {
167   return self->noinferiors;
168 }
169
170 - (BOOL)noselect {
171   return YES;
172 }
173
174 - (BOOL)nonexistent {
175   return NO;
176 }
177 - (BOOL)haschildren {
178   return YES;
179 }
180 - (BOOL)hasnochildren {
181   return NO;
182 }
183 - (BOOL)marked {
184   return NO;
185 }
186 - (BOOL)unmarked {
187   return NO;
188 }
189
190 - (int)exists {
191   return 0;
192 }
193
194 - (int)recent {
195   return 0;
196 }
197
198 - (int)unseen {
199   return 0;
200 }
201
202 - (int)usedSpace {
203   return 0;
204 }
205
206 - (int)maxQuota {
207   return 0;
208 }
209
210 - (BOOL)isOverQuota {
211   return NO;
212 }
213
214 - (BOOL)hasNewMessagesSearchRecursiv:(BOOL)_recursiv fetchOnDemand:(BOOL)_fetch {
215   if (_recursiv)
216     return _hasNewMessagesInSubFolder(self, _fetch);
217   return NO;
218 }
219
220 - (BOOL)hasNewMessagesSearchRecursiv:(BOOL)_recursiv {
221   if (!_recursiv)
222     return NO;
223   return _hasNewMessagesInSubFolder(self,
224              FetchNewUnseenMessagesInSubFoldersOnDemand);
225 }
226
227 - (BOOL)hasUnseenMessagesSearchRecursiv:(BOOL)_recursiv 
228   fetchOnDemand:(BOOL)_fetch 
229 {
230   if (_recursiv)
231     return _hasUnseenMessagesInSubFolder(self, _fetch);
232   return NO;
233 }
234
235 - (BOOL)hasUnseenMessagesSearchRecursiv:(BOOL)_recursiv {
236   if (_recursiv) {
237     return _hasUnseenMessagesInSubFolder(self,
238                FetchNewUnseenMessagesInSubFoldersOnDemand);
239   }
240   return NO;
241 }
242
243 // private methods
244
245 /*"
246 ** Should only happens if folder is rootfolder
247 "*/
248
249 - (void)initializeSubFolders {
250   NSEnumerator *folders;
251   NSDictionary *res;
252   id           *objs, folder;
253   unsigned     cnt;
254   BOOL         gotInbox;
255   
256   if (self->subFolders != nil) {
257     [self resetSubFolders];
258   }
259
260   if ([self->context showOnlySubscribedInRoot]) {
261     res = [[self->context client] lsub:@"" pattern:@"%"];
262   }
263   else {
264     res = [[self->context client] list:@"" pattern:@"%"];
265   }
266
267   if (!_checkResult(self->context, res, __PRETTY_FUNCTION__))
268     return;
269   
270   res = [res objectForKey:@"list"];
271
272   objs = calloc([res count] + 2, sizeof(id));
273   {
274     NSArray *names;
275     
276     names   = [res allKeys];
277     names   = [names sortedArrayUsingSelector:
278                        @selector(caseInsensitiveCompare:)];
279     folders = [names objectEnumerator];
280   }
281   cnt      = 0;
282   gotInbox = NO;
283   
284   while ((folder = [folders nextObject])) {
285     NSArray *f;
286
287     f = [res objectForKey:folder];
288
289     if (!ShowNonExistentFolder) {
290       if ([f containsObject:@"nonexistent"])
291         continue;
292     }
293
294     objs[cnt] = [[[NGImap4Folder alloc] initWithContext:self->context
295                                         name:folder flags:f parentFolder:self] 
296                                  autorelease];
297     cnt++;
298     
299     if ([[folder lowercaseString] isEqualToString:@"/inbox"])
300       gotInbox = YES;
301   }
302   if (!gotInbox && [self->context showOnlySubscribedInRoot]) {
303     /* try unsubscribed */
304     res = [[[self->context client] list:@"" pattern:@"%"] objectForKey:@"list"];
305
306     {
307       NSArray *names;
308     
309       names   = [res allKeys];
310       names   = [names sortedArrayUsingSelector:
311                        @selector(caseInsensitiveCompare:)];
312       folders = [names objectEnumerator];
313     }
314     while ((folder = [folders nextObject])) {
315       if ([[folder lowercaseString] isEqualToString:@"/inbox"]) {
316         objs[cnt] = [[[NGImap4Folder alloc] initWithContext:self->context
317                                             name:folder 
318                                             flags:[res objectForKey:folder]
319                                             parentFolder:self]
320                                      autorelease];
321         cnt++;
322         break;
323       }
324     }
325   }
326   self->subFolders = [[NSArray alloc] initWithObjects:objs count:cnt];
327   if (objs) free(objs); objs = NULL;
328 }
329
330 - (void)select {
331 }
332
333 - (void)status {
334 }
335
336 /* actions */
337
338 - (void)resetFolder {
339 }
340
341 - (void)resetStatus {
342 }
343
344 - (void)resetSubFolders {
345   [self->context resetSpecialFolders];
346   [self->subFolders release]; self->subFolders = nil;
347 }
348
349 - (BOOL)renameTo:(NSString *)_name {
350   return NO;
351 }
352
353 /* folder */
354
355 - (BOOL)deleteSubFolder:(NGImap4Folder *)_folder {
356   return _deleteSubFolder(self, _folder);
357 }
358
359 - (BOOL)copySubFolder:(NGImap4Folder *)_f to:(id<NGImap4Folder>)_folder {
360   return _copySubFolder(self, _f, _folder);
361 }
362
363 - (BOOL)moveSubFolder:(NGImap4Folder *)_f to:(id<NGImap4Folder>)_folder {
364   return _moveSubFolder(self, _f, _folder);
365 }
366
367 - (BOOL)createSubFolderWithName:(NSString *)_name {
368   return _createSubFolderWithName(self, _name, NO);
369 }
370
371 - (void)expunge {
372 }
373
374 - (BOOL)addFlag:(NSString *)_flag toMessages:(NSArray *)_messages {
375   return NO;
376 }
377
378 - (BOOL)removeFlag:(NSString *)_flag fromMessages:(NSArray *)_messages {
379   return NO;
380 }
381
382 - (BOOL)flag:(NSString *)_flag toMessages:(NSArray *)_messages
383         add:(NSNumber *)_add
384 {
385   return NO;
386 }
387
388 - (BOOL)flagToAllMessages:(NSString *)_flag add:(NSNumber *)_add {
389   return NO;
390 }
391
392 - (BOOL)deleteAllMessages {
393   return NO;
394 }
395
396 - (BOOL)deleteMessages:(NSArray *)_messages {
397   return NO;
398 }
399
400 - (BOOL)moveMessages:(NSArray *)_messages toFolder:(NGImap4Folder *)_folder {
401   return NO;
402 }
403
404 - (BOOL)copyMessages:(NSArray *)_messages toFolder:(NGImap4Folder *)_folder {
405   return NO;
406 }
407
408 - (BOOL)appendMessage:(NSData *)_msg {
409   return NO;
410 }
411
412 - (BOOL)isInTrash {
413   return NO;
414 }
415
416 - (NSString *)description {
417   NSMutableString *ms;
418   NSString *tmp;
419
420   ms = [NSMutableString stringWithCapacity:64];
421
422   [ms appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];
423   
424   if ((tmp = [self name]))
425     [ms appendFormat:@" name=%@", tmp];
426   if ((tmp = [self absoluteName]))
427     [ms appendFormat:@" absolute=%@", tmp];
428   
429   [ms appendString:@">"];
430
431   return ms;
432 }
433
434 - (void)resetSync {
435   NSEnumerator *enumerator;
436   id           folder;
437
438   enumerator = [[self subFolders] objectEnumerator];
439   
440   while ((folder = [enumerator nextObject])) {
441     [folder resetSync];
442   }
443 }
444
445 @end /* NGImap4ServerRoot */