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