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 if ((i = [_types indexOfObject:@"text/html"]) != NSNotFound)
94 if ((i = [_types indexOfObject:@"text/plain"]) != NSNotFound)
97 /* then we scan for other text types and choose the first one found */
98 for (i = 0; i < count; i++) {
99 if ([(NSString *)[_types objectAtIndex:i] hasPrefix:@"text/"])
103 /* as a fallback, we select the first available part */
107 - (void)selectChildInfo {
110 [self->childInfo release]; self->childInfo = nil;
111 self->childIndex = 0;
113 idx = [self selectPartIndexFromTypes:[self childPartTypes]];
114 if (idx == NSNotFound) {
115 [self errorWithFormat:@"could not select a part of types: %@",
116 [self childPartTypes]];
120 self->childIndex = idx + 1;
122 [[[[self bodyInfo] valueForKey:@"parts"] objectAtIndex:idx] retain];
128 if (self->childInfo == nil)
129 [self selectChildInfo];
131 return self->childInfo;
134 - (unsigned int)childIndex {
135 if (self->childIndex == 0)
136 [self selectChildInfo];
138 return self->childIndex - 1;
141 - (NSString *)childPartName {
143 sprintf(buf, "%d", [self childIndex] + 1);
144 return [NSString stringWithCString:buf];
147 - (id)childPartPath {
150 pp = [self partPath];
151 return [pp count] > 0
152 ? [pp arrayByAddingObject:[self childPartName]]
153 : [NSArray arrayWithObject:[self childPartName]];
158 - (id)contentViewerComponent {
161 info = [self childInfo];
162 return [[[self context] mailRenderingContext] viewerForBodyInfo:info];
165 @end /* UIxMailPartAlternativeViewer */