"Fossies" - the Fresh Open Source Software Archive 
Member "opengroupware-5.5rc3/WebUI/Mailer/OGoWebMail/SkyImapMailList.m" (5 Dec 2015, 15022 Bytes) of package /linux/privat/opengroupware-5.5rc3.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Matlab source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "SkyImapMailList.m" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
5.5rc2_vs_5.5rc3.
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 #import "common.h"
23 #import <Foundation/NSFormatter.h>
24 #import "LSWImapMailMove.h"
25 #import "SkyImapMailListState.h"
26 #import "SkyImapMailDataSource.h"
27 #import "LSWImapMails.h"
28
29 @interface LSWImapMails(Private)
30 - (void)setToDeletedMails:(NSArray *)_mails;
31 @end /* LSWImapMails(Private) */
32
33 @interface SkyImapMailStringShortenizer : NSFormatter
34 {
35 @protected
36 unsigned length;
37 NSString *nilString;
38 }
39 - (void)setLength:(unsigned int)_length;
40 - (void)setNilString:(NSString *)_nilString;
41 @end
42
43 @interface SkyImapMailList : OGoContentPage
44 {
45 @protected
46 EODataSource *dataSource;
47 SkyImapMailListState *state;
48 SkyImapMailStringShortenizer *senderFormatter;
49 SkyImapMailStringShortenizer *subjectFormatter;
50 NSMutableArray *selections;
51 id message;
52 BOOL dataSourceDidChange;
53 int mailCount;
54 int index;
55 }
56 - (void)_showAll;
57 - (void)_showUnread;
58 - (void)_showFlagged;
59 - (id)showAll;
60 - (id)showUnread;
61 - (id)showFlagged;
62
63 @end
64
65 #include "common.h"
66
67 @implementation SkyImapMailList
68
69 - (id)init {
70 if ((self = [super init])) {
71 NSZone *z;
72
73 z = [self zone];
74 self->senderFormatter = [[SkyImapMailStringShortenizer alloc] init];
75 self->subjectFormatter = [[SkyImapMailStringShortenizer alloc] init];
76
77 [self->subjectFormatter
78 setNilString:[[self labels] valueForKey:@"noMailTitle"]];
79 self->selections = [[NSMutableArray allocWithZone:z] init];
80
81 [[NSNotificationCenter defaultCenter]
82 addObserver:self
83 selector:@selector(clearSelections)
84 name:@"LSWImapMailsShouldClearSelections"
85 object:nil];
86
87 self->dataSourceDidChange = YES;
88 }
89 return self;
90 }
91
92 - (void)dealloc {
93 [[NSNotificationCenter defaultCenter] removeObserver:self];
94 [self->state release];
95 [self->senderFormatter release];
96 [self->subjectFormatter release];
97 [self->message release];
98 [self->selections release];
99 [super dealloc];
100 }
101
102
103 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
104 EOSortOrdering *so;
105 SEL sel;
106 NSArray *soArray;
107
108 sel = ([self->state isDescending])
109 ? EOCompareDescending : EOCompareAscending;
110 so = [EOSortOrdering sortOrderingWithKey:[self->state sortedKey]
111 selector:sel];
112 soArray = [NSArray arrayWithObject:so];
113
114 if ([(SkyImapMailDataSource *)self->dataSource
115 useSSSortingForSOArray:soArray]) {
116 if ([self->state sortedKey]) {
117 EOFetchSpecification *fetchSpec;
118
119 if ((fetchSpec = [self->dataSource fetchSpecification]) == nil) {
120 fetchSpec = [EOFetchSpecification fetchSpecificationWithEntityName:nil
121 qualifier:nil
122 sortOrderings:
123 [NSArray arrayWithObject:so]];
124 [self->dataSource setFetchSpecification:fetchSpec];
125 }
126 else {
127 NSArray *sos;
128
129 sos = [NSArray arrayWithObject:so];
130
131 if (![[fetchSpec sortOrderings] isEqual:sos]) {
132 [fetchSpec setSortOrderings:[NSArray arrayWithObject:so]];
133 [self->dataSource setFetchSpecification:fetchSpec];
134 }
135 }
136 }
137 [(SkyImapMailDataSource *)self->dataSource
138 preFetchMessagesInRange:
139 NSMakeRange([self->state blockSize]
140 * ([self->state currentBatch]-1),
141 [self->state blockSize])];
142 }
143 [super appendToResponse:_response inContext:_ctx];
144 }
145
146 - (void)clearSelections {
147 [self->selections removeAllObjects];
148 }
149
150 - (void)dataSourceDidChange {
151 self->dataSourceDidChange = YES;
152 }
153
154
155 - (void)syncAwake {
156 [super syncAwake];
157
158 [self->subjectFormatter setLength:[self->state subjectLength]];
159 [self->senderFormatter setLength:[self->state senderLength]];
160
161 if (self->dataSourceDidChange && [self->state isShowFilterButtons]) {
162 if ([self->state showAllMessages] == -1)
163 [self _showUnread];
164 else if ([self->state showAllMessages] == 1)
165 [self _showAll];
166 else if ([[self->state showMessages] isEqualToString:@"all"])
167 [self showAll];
168 else if ([[self->state showMessages] isEqualToString:@"flagged"])
169 [self showFlagged];
170 else
171 [self showUnread];
172 self->dataSourceDidChange = NO;
173 }
174 }
175
176 - (void)syncSleep {
177 [self->state synchronize]; // write userDefaults
178 [super syncSleep];
179 }
180
181 // -- accsessors
182
183 - (void)setDataSource:(EODataSource *)_dataSource {
184 if (self->dataSource != _dataSource) {
185 NSNotificationCenter *nc;
186
187 nc = [NSNotificationCenter defaultCenter];
188 [nc removeObserver:self
189 name:EODataSourceDidChangeNotification
190 object:self->dataSource];
191
192 ASSIGN(self->dataSource, _dataSource);
193
194 [nc addObserver:self
195 selector:@selector(dataSourceDidChange)
196 name:EODataSourceDidChangeNotification
197 object:self->dataSource];
198 }
199 }
200 - (EODataSource *)dataSource {
201 return self->dataSource;
202 }
203
204 - (void)setSelections:(NSArray *)_selections {
205 ASSIGN(self->selections, selections);
206 }
207 - (NSArray *)selections {
208 return self->selections;
209 }
210
211 - (id)message {
212 return self->message;
213 }
214 - (void)setMessage:(id)_message {
215 ASSIGN(self->message, _message);
216 }
217
218 - (NSFormatter *)senderFormatter {
219 return self->senderFormatter;
220 }
221 - (NSFormatter *)subjectFormatter {
222 return self->subjectFormatter;
223 }
224
225 - (void)setState:(SkyImapMailListState *)_state {
226 ASSIGN(self->state, _state);
227 }
228 - (SkyImapMailListState *)state {
229 return self->state;
230 }
231
232 // ------------------------------------------------------------------------
233
234 - (NSString *)longSubject {
235 return [NSString stringWithFormat:@"\"%@\" - %@",
236 [self->message valueForKey:@"subject"],
237 [self->message valueForKey:@"sender"]];
238 }
239
240 - (NSString *)messageIdentifier {
241 return [NSString stringWithFormat:@"%p_%d",
242 [self->message folder], [self->message uid]];
243 }
244
245 - (NSCalendarDate *)sendDate {
246 NSCalendarDate *result;
247
248 result = [self->message valueForKey:@"sendDate"];
249 if ([result respondsToSelector:@selector(setTimeZone:)])
250 [result setTimeZone:[[self session] timeZone]];
251 return result;
252 }
253
254 // --- conditions ---------------------------------------------
255
256 - (BOOL)isRead {
257 return [self->message isRead];
258 }
259
260 - (BOOL)isAnswered {
261 return [self->message isAnswered];
262 }
263
264 - (BOOL)isNew {
265 return (([[self->message valueForKey:@"isNew"] intValue] == 1));
266 }
267
268 - (BOOL)isUnread {
269 return (![self isRead] && ![self isNew]);
270 }
271
272 - (BOOL)scrollOnClientSide {
273 int treshold;
274
275 if (![self->state doClientSideScroll])
276 return NO;
277
278 treshold = [self->state clientSideScrollTreshold];
279
280 return ([[self valueForKey:@"mailCount"] intValue] < treshold) ? YES : NO;
281 }
282
283 /* posting notifications */
284
285 - (NSNotificationCenter *)notificationCenter {
286 return [NSNotificationCenter defaultCenter];
287 }
288
289 - (void)_postMailWasDeleted {
290 [[self notificationCenter] postNotificationName:@"LSWImapMailWasDeleted"
291 object:nil];
292 }
293 - (void)_postMailFlagsChanged:(NGImap4Message *)_msg {
294 [[self notificationCenter] postNotificationName:@"LSWImapMailFlagsChanged"
295 object:_msg];
296 }
297
298 /* actions */
299
300 - (void)_showAll {
301 EOFetchSpecification *fetchSpec;
302
303 fetchSpec = [self->dataSource fetchSpecification];
304 [fetchSpec setQualifier:nil];
305
306 [self->dataSource setFetchSpecification:fetchSpec];
307 }
308
309 - (void)_showUnread {
310 EOFetchSpecification *fetchSpec;
311 EOQualifier *q;
312
313 q = [EOQualifier qualifierWithQualifierFormat:@"flags = \"unseen\""];
314
315 fetchSpec = [self->dataSource fetchSpecification];
316 [fetchSpec setQualifier:q];
317
318 [self->dataSource setFetchSpecification:fetchSpec];
319 }
320
321 - (void)_showFlagged {
322 EOFetchSpecification *fetchSpec;
323 EOQualifier *q;
324
325 q = [EOQualifier qualifierWithQualifierFormat:@"flags = \"flagged\""];
326 fetchSpec = [self->dataSource fetchSpecification];
327 [fetchSpec setQualifier:q];
328
329 [self->dataSource setFetchSpecification:fetchSpec];
330 }
331
332 - (id)showAll {
333 [self->state setShowAllMessages:0];
334 [self _showAll];
335 [self->state setShowMessages:@"all"];
336
337 return nil;
338 }
339
340 - (id)showUnread {
341 [self->state setShowAllMessages:0];
342 [self _showUnread];
343 [self->state setShowMessages:@"unread"];
344
345 return nil;
346 }
347
348 - (id)showFlagged {
349 [self->state setShowAllMessages:0];
350 [self _showFlagged];
351 [self->state setShowMessages:@"flagged"];
352
353 return nil;
354 }
355
356 - (id)viewMail {
357 NSNotificationCenter *nc;
358 BOOL wasUnread;
359 id page;
360
361 nc = [NSNotificationCenter defaultCenter];
362 wasUnread = ![self->message isRead];
363
364 if (![self->message isRead]) {
365 [self->message markRead];
366 [self _postMailFlagsChanged:self->message];
367 }
368 page = [[[self session] navigation] activateObject:self->message
369 withVerb:@"view"];
370 [page takeValue:[NSNumber numberWithBool:wasUnread]
371 forKey:@"messageWasUnread"];
372 [page takeValue:self->dataSource forKey:@"mailDS"];
373 return page;
374 }
375
376 - (id)markMessageRead {
377 [self->message markRead];
378 return nil;
379 }
380
381 - (id)markMessageUnread {
382 [self->message markUnread];
383 return nil;
384 }
385
386 - (id)markMessageNotAnswered {
387 [self->message markNotAnswered];
388 [self _postMailFlagsChanged:self->message];
389 return nil;
390 }
391
392 - (id)markMessageFlagged {
393 [self->message markFlagged];
394 return nil;
395 }
396
397 - (id)markMessageUnflagged {
398 [self->message markUnFlagged];
399 return nil;
400 }
401
402 - (id)markRead {
403 NSEnumerator *enumerator;
404 id obj;
405
406 enumerator = [self->selections objectEnumerator];
407
408 while ((obj = [enumerator nextObject]))
409 [(NGImap4Message *)obj markRead];
410
411 [self _postMailFlagsChanged:nil];
412 [self->selections removeAllObjects];
413 return nil;
414 }
415
416 - (id)markUnread {
417 NSEnumerator *enumerator;
418 id obj;
419
420 enumerator = [self->selections objectEnumerator];
421
422 while ((obj = [enumerator nextObject]))
423 [(NGImap4Message *)obj markUnread];
424
425 [self _postMailFlagsChanged:nil /* a set changed */];
426 [self->selections removeAllObjects];
427 return nil;
428 }
429
430 - (id)markFlagged {
431 NSEnumerator *enumerator;
432 id obj;
433
434 enumerator = [self->selections objectEnumerator];
435
436 while ((obj = [enumerator nextObject])) {
437 [(NGImap4Message *)obj markFlagged];
438 }
439 [self _postMailFlagsChanged:nil];
440 [self->selections removeAllObjects];
441 return nil;
442 }
443
444 - (id)markUnFlagged {
445 NSEnumerator *enumerator;
446 id obj;
447
448 enumerator = [self->selections objectEnumerator];
449
450 while ((obj = [enumerator nextObject])) {
451 [(NGImap4Message *)obj markUnFlagged];
452 }
453 [self _postMailFlagsChanged:nil];
454 [self->selections removeAllObjects];
455 return nil;
456 }
457
458 - (id)moveMail {
459 if (([self->selections count] > 0)) {
460 LSWImapMailMove *page;
461
462 page = [self pageWithName:@"LSWImapMailMove"];
463 [page setMails:AUTORELEASE([self->selections copy])];
464 [page setCopyMode:NO];
465 [self->selections removeAllObjects];
466
467 return page;
468 }
469 return nil;
470 }
471
472 - (id)copyMail {
473 if (([self->selections count] > 0)) {
474 LSWImapMailMove *page;
475
476 page = [self pageWithName:@"LSWImapMailMove"];
477 [page setMails:AUTORELEASE([self->selections copy])];
478 [page setCopyMode:YES];
479 [self->selections removeAllObjects];
480
481 return page;
482 }
483 return nil;
484 }
485
486 - (id)deleteMail {
487 NGImap4Folder *f;
488
489 if ([self->selections count] == 0)
490 return nil;
491
492 f = [[self->selections lastObject] folder];
493
494 if ([f isInTrash])
495 [f deleteMessages:self->selections];
496 else {
497 if (![f moveMessages:self->selections
498 toFolder:[[f context] trashFolder]]) {
499 NSException *exc;
500 id p;
501
502 p = [[[self session] navigation] activePage];
503
504 exc = [f lastException];
505
506 [p setWarningOkAction:@"reallyDeleteMails"];
507 {
508 NSString *wp, *reason;
509 id l;
510
511 l = [self labels];
512 wp = [l valueForKey:@"MoveMailToTrashFailedWithReason"];
513 if ((reason = [exc reason])) {
514 reason = [l valueForKey:reason];
515 }
516 wp = [NSString stringWithFormat:@"%@: '%@'. %@", wp, reason,
517 [l valueForKey:@"DeleteMailsAnyway"]];
518 [p setWarningPhrase:wp];
519 }
520 [p setIsInWarningMode:YES];
521 [p setToDeletedMails:self->selections];
522
523 return nil;
524 }
525 }
526 [self _postMailWasDeleted];
527 [self->selections removeAllObjects];
528 if ([self scrollOnClientSide])
529 [self->state setCurrentBatch:1];
530
531 return nil;
532 }
533
534 - (int)mailCount {
535 return self->mailCount;
536 }
537 - (void)setMailCount:(int)_cnt {
538 self->mailCount = _cnt;
539 }
540
541 - (int)index {
542 return self->index;
543 }
544 - (void)setIndex:(int)_cnt {
545 self->index = _cnt;
546 }
547
548 @end
549
550 @implementation SkyImapMailStringShortenizer
551
552 - (id)init {
553 if ((self = [super init])) {
554 self->length = 20;
555 self->nilString = nil;
556 }
557 return self;
558 }
559
560 - (void)dealloc {
561 [self->nilString release];
562 [super dealloc];
563 }
564
565 - (void)setLength:(unsigned int)_length {
566 self->length = _length;
567 }
568
569 - (void)setNilString:(NSString *)_nilString {
570 ASSIGN(self->nilString, _nilString);
571 }
572
573 /* object => string */
574
575 - (NSString *)stringForObjectValue:(id)_object {
576 if (![_object isNotEmpty])
577 return (self->nilString != nil) ? self->nilString : (NSString *)@"";
578
579 if (([_object length] <= self->length) || ([_object length] <= 4))
580 return [_object stringValue];
581
582 {
583 NSString *result = [_object substringToIndex:self->length-2];
584 return [result stringByAppendingString:@".."];
585 }
586 }
587
588 - (NSString *)editingStringForObjectValue:(id)_object {
589 return (![_object isNotNull])
590 ? (NSString *)@""
591 : [_object stringValue];
592 }
593
594 // string => object
595
596 - (BOOL)getObjectValue:(id *)_object
597 forString:(NSString *)_string
598 errorDescription:(NSString **)_error
599 {
600 *_object = [_string isNotEmpty] ? _string : (NSString *)[NSNull null];
601
602 return YES;
603 }
604
605 @end /* SkyImapMailStringShortenizer */