]> err.no Git - scalable-opengroupware.org/blob - UI/MailPartViewers/UIxMailPartAlternativeViewer.m
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1275 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / MailPartViewers / UIxMailPartAlternativeViewer.m
1 /*
2   Copyright (C) 2004 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 <Foundation/NSNull.h>
23
24 #import <NGExtensions/NSObject+Logs.h>
25
26 #import <UI/MailerUI/WOContext+UIxMailer.h>
27
28 #import "UIxMailPartViewer.h"
29 #import "UIxMailRenderingContext.h"
30
31 /*
32   UIxMailPartAlternativeViewer
33   
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.
37   
38   TODO: We might want to give the user the possibility to access all parts
39         of the alternative set.
40 */
41
42 @interface UIxMailPartAlternativeViewer : UIxMailPartViewer
43 {
44   id           childInfo;
45   unsigned int childIndex;
46 }
47
48 @end
49
50 @implementation UIxMailPartAlternativeViewer
51
52 - (void)dealloc {
53   [childInfo release];
54   [super dealloc];
55 }
56
57 /* caches */
58
59 - (void)resetBodyInfoCaches {
60   [childInfo release]; childInfo = nil;
61   childIndex = 0;
62   [super resetBodyInfoCaches];
63 }
64
65 /* part selection */
66
67 - (NSArray *)childPartTypes {
68   NSMutableArray *types;
69   unsigned i, count;
70   NSArray  *childParts;
71
72   childParts = [[self bodyInfo] valueForKey:@"parts"];
73   count      = [childParts count];
74   types      = [NSMutableArray arrayWithCapacity:count];
75   
76   for (i = 0; i < count; i++) {
77     NSString *mt, *st;
78
79     mt = [[[childParts objectAtIndex:i] valueForKey:@"type"] lowercaseString];
80     st = [[[childParts objectAtIndex:i] valueForKey:@"subtype"]
81                        lowercaseString];
82     mt = [[mt stringByAppendingString:@"/"] stringByAppendingString:st];
83     [types addObject:mt ? mt : (id)[NSNull null]];
84   }
85   return types;
86 }
87
88 - (int)selectPartIndexFromTypes:(NSArray *)_types {
89   /* returns the index of the selected part or NSNotFound */
90   unsigned i, count;
91
92   if ((count = [_types count]) == 0)
93     return NSNotFound;
94   
95   if ((i = [_types indexOfObject:@"text/html"]) != NSNotFound)
96     return i;
97   if ((i = [_types indexOfObject:@"text/plain"]) != NSNotFound)
98     return i;
99
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/"])
103       return i;
104   }
105   
106   /* as a fallback, we select the first available part */
107   return 0;
108 }
109
110 - (void)selectChildInfo {
111   unsigned idx;
112   
113   [childInfo release]; childInfo = nil;
114   childIndex = 0;
115   
116   idx = [self selectPartIndexFromTypes:[self childPartTypes]];
117   if (idx == NSNotFound) {
118     [self errorWithFormat:@"could not select a part of types: %@",
119             [self childPartTypes]];
120     return;
121   }
122   
123   childIndex = idx + 1;
124   childInfo  = 
125     [[[[self bodyInfo] valueForKey:@"parts"] objectAtIndex:idx] retain];
126 }
127
128 /* accessors */
129
130 - (id)childInfo {
131   if (childInfo == nil)
132     [self selectChildInfo];
133   
134   return childInfo;
135 }
136
137 - (unsigned int) childIndex {
138   if (childIndex == 0)
139     [self selectChildInfo];
140   
141   return childIndex - 1;
142 }
143
144 - (NSString *)childPartName {
145   char buf[8];
146   sprintf(buf, "%d", [self childIndex] + 1);
147   return [NSString stringWithCString:buf];
148 }
149
150 - (id)childPartPath {
151   NSArray *pp;
152
153   pp = [self partPath];
154   return [pp count] > 0
155     ? [pp arrayByAddingObject:[self childPartName]]
156     : [NSArray arrayWithObject:[self childPartName]];
157 }
158
159 /* nested viewers */
160
161 - (id)contentViewerComponent {
162   id info;
163   
164   info = [self childInfo];
165   return [[[self context] mailRenderingContext] viewerForBodyInfo:info];
166 }
167
168 @end /* UIxMailPartAlternativeViewer */