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 #import <Foundation/NSNull.h>
24 #import <NGExtensions/NSObject+Logs.h>
26 #import <UI/MailerUI/WOContext+UIxMailer.h>
28 #import "UIxMailPartViewer.h"
29 #import "UIxMailRenderingContext.h"
32 UIxMailPartAlternativeViewer
34 Display multipart/alternative parts. Most common application is for messages
35 which contain text/html and text/plain, but it is also used in other
36 contexts, eg in OGo appointment mails.
38 TODO: We might want to give the user the possibility to access all parts
39 of the alternative set.
42 @interface UIxMailPartAlternativeViewer : UIxMailPartViewer
45 unsigned int childIndex;
50 @implementation UIxMailPartAlternativeViewer
59 - (void)resetBodyInfoCaches {
60 [childInfo release]; childInfo = nil;
62 [super resetBodyInfoCaches];
67 - (NSArray *)childPartTypes {
68 NSMutableArray *types;
72 childParts = [[self bodyInfo] valueForKey:@"parts"];
73 count = [childParts count];
74 types = [NSMutableArray arrayWithCapacity:count];
76 for (i = 0; i < count; i++) {
79 mt = [[[childParts objectAtIndex:i] valueForKey:@"type"] lowercaseString];
80 st = [[[childParts objectAtIndex:i] valueForKey:@"subtype"]
82 mt = [[mt stringByAppendingString:@"/"] stringByAppendingString:st];
83 [types addObject:mt ? mt : (id)[NSNull null]];
88 - (int)selectPartIndexFromTypes:(NSArray *)_types {
89 /* returns the index of the selected part or NSNotFound */
92 if ((count = [_types count]) == 0)
95 if ((i = [_types indexOfObject:@"text/html"]) != NSNotFound)
97 if ((i = [_types indexOfObject:@"text/plain"]) != NSNotFound)
100 /* then we scan for other text types and choose the first one found */
101 for (i = 0; i < count; i++) {
102 if ([(NSString *)[_types objectAtIndex:i] hasPrefix:@"text/"])
106 /* as a fallback, we select the first available part */
110 - (void)selectChildInfo {
113 [childInfo release]; childInfo = nil;
116 idx = [self selectPartIndexFromTypes:[self childPartTypes]];
117 if (idx == NSNotFound) {
118 [self errorWithFormat:@"could not select a part of types: %@",
119 [self childPartTypes]];
123 childIndex = idx + 1;
125 [[[[self bodyInfo] valueForKey:@"parts"] objectAtIndex:idx] retain];
131 if (childInfo == nil)
132 [self selectChildInfo];
137 - (unsigned int) childIndex {
139 [self selectChildInfo];
141 return childIndex - 1;
144 - (NSString *)childPartName {
146 sprintf(buf, "%d", [self childIndex] + 1);
147 return [NSString stringWithCString:buf];
150 - (id)childPartPath {
153 pp = [self partPath];
154 return [pp count] > 0
155 ? [pp arrayByAddingObject:[self childPartName]]
156 : [NSArray arrayWithObject:[self childPartName]];
161 - (id)contentViewerComponent {
164 info = [self childInfo];
165 return [[[self context] mailRenderingContext] viewerForBodyInfo:info];
168 @end /* UIxMailPartAlternativeViewer */