2 Copyright (C) 2004 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 "UIxMailPartViewer.h"
25 UIxMailPartAlternativeViewer
27 Display multipart/alternative parts. Most common application is for messages
28 which contain text/html and text/plain, but it is also used in other
29 contexts, eg in OGo appointment mails.
31 TODO: We might want to give the user the possibility to access all parts
32 of the alternative set.
35 @interface UIxMailPartAlternativeViewer : UIxMailPartViewer
38 unsigned int childIndex;
43 #include "UIxMailRenderingContext.h"
44 #include <UI/MailerUI/WOContext+UIxMailer.h>
47 @implementation UIxMailPartAlternativeViewer
50 [self->childInfo release];
56 - (void)resetBodyInfoCaches {
57 [self->childInfo release]; self->childInfo = nil;
59 [super resetBodyInfoCaches];
64 - (NSArray *)childPartTypes {
65 NSMutableArray *types;
69 childParts = [[self bodyInfo] valueForKey:@"parts"];
70 count = [childParts count];
71 types = [NSMutableArray arrayWithCapacity:count];
73 for (i = 0; i < count; i++) {
76 mt = [[[childParts objectAtIndex:i] valueForKey:@"type"] lowercaseString];
77 st = [[[childParts objectAtIndex:i] valueForKey:@"subtype"]
79 mt = [[mt stringByAppendingString:@"/"] stringByAppendingString:st];
80 [types addObject:mt ? mt : (id)[NSNull null]];
85 - (int)selectPartIndexFromTypes:(NSArray *)_types {
86 /* returns the index of the selected part or NSNotFound */
89 if ((count = [_types count]) == 0)
92 /* we always choose text/plain if available */
93 if ((i = [_types indexOfObject:@"text/plain"]) != NSNotFound)
96 /* then we scan for other text types and choose the first one found */
97 for (i = 0; i < count; i++) {
98 if ([(NSString *)[_types objectAtIndex:i] hasPrefix:@"text/"])
102 /* as a fallback, we select the first available part */
106 - (void)selectChildInfo {
109 [self->childInfo release]; self->childInfo = nil;
110 self->childIndex = 0;
112 idx = [self selectPartIndexFromTypes:[self childPartTypes]];
113 if (idx == NSNotFound) {
114 [self errorWithFormat:@"could not select a part of types: %@",
115 [self childPartTypes]];
119 self->childIndex = idx + 1;
121 [[[[self bodyInfo] valueForKey:@"parts"] objectAtIndex:idx] retain];
127 if (self->childInfo == nil)
128 [self selectChildInfo];
130 return self->childInfo;
133 - (unsigned int)childIndex {
134 if (self->childIndex == 0)
135 [self selectChildInfo];
137 return self->childIndex - 1;
140 - (NSString *)childPartName {
142 sprintf(buf, "%d", [self childIndex] + 1);
143 return [NSString stringWithCString:buf];
146 - (id)childPartPath {
149 pp = [self partPath];
150 return [pp count] > 0
151 ? [pp arrayByAddingObject:[self childPartName]]
152 : [NSArray arrayWithObject:[self childPartName]];
157 - (id)contentViewerComponent {
160 info = [self childInfo];
161 return [[[self context] mailRenderingContext] viewerForBodyInfo:info];
164 @end /* UIxMailPartAlternativeViewer */