]> err.no Git - scalable-opengroupware.org/blob - SOGo/UI/Mailer/UIxMailPartAlternativeViewer.m
changes to use NGLogging in all places
[scalable-opengroupware.org] / SOGo / UI / Mailer / 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 "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   /* we always choose text/plain if available */
93   if ((i = [_types indexOfObject:@"text/plain"]) != NSNotFound)
94     return i;
95   
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/"])
99       return i;
100   }
101   
102   /* as a fallback, we select the first available part */
103   return 0;
104 }
105
106 - (void)selectChildInfo {
107   unsigned idx;
108   
109   [self->childInfo release]; self->childInfo = nil;
110   self->childIndex = 0;
111   
112   idx = [self selectPartIndexFromTypes:[self childPartTypes]];
113   if (idx == NSNotFound) {
114     [self errorWithFormat:@"could not select a part of types: %@",
115             [self childPartTypes]];
116     return;
117   }
118   
119   self->childIndex = idx + 1;
120   self->childInfo  = 
121     [[[[self bodyInfo] valueForKey:@"parts"] objectAtIndex:idx] retain];
122 }
123
124 /* accessors */
125
126 - (id)childInfo {
127   if (self->childInfo == nil)
128     [self selectChildInfo];
129   
130   return self->childInfo;
131 }
132
133 - (unsigned int)childIndex {
134   if (self->childIndex == 0)
135     [self selectChildInfo];
136   
137   return self->childIndex - 1;
138 }
139
140 - (NSString *)childPartName {
141   unsigned char buf[8];
142   sprintf(buf, "%d", [self childIndex] + 1);
143   return [NSString stringWithCString:buf];
144 }
145
146 - (id)childPartPath {
147   NSArray *pp;
148
149   pp = [self partPath];
150   return [pp count] > 0
151     ? [pp arrayByAddingObject:[self childPartName]]
152     : [NSArray arrayWithObject:[self childPartName]];
153 }
154
155 /* nested viewers */
156
157 - (id)contentViewerComponent {
158   id info;
159   
160   info = [self childInfo];
161   return [[[self context] mailRenderingContext] viewerForBodyInfo:info];
162 }
163
164 @end /* UIxMailPartAlternativeViewer */