From af740904e22d00a37d85f0c62c563c566508f469 Mon Sep 17 00:00:00 2001 From: Wolfgang Sourdeau Date: Tue, 30 Oct 2007 17:43:34 +0000 Subject: [PATCH] Monotone-Parent: 0f7a7062f9e521fc44b55467166775907a3e3668 Monotone-Revision: 3afd5e21d96b09fb0b8e7c1fd319dcce3cd06d3b Monotone-Author: wsourdeau@inverse.ca Monotone-Date: 2007-10-30T17:43:34 Monotone-Branch: ca.inverse.sogo --- .../UIxMailPartAlternativeViewer.m | 154 +++++++++++------- 1 file changed, 91 insertions(+), 63 deletions(-) diff --git a/UI/MailPartViewers/UIxMailPartAlternativeViewer.m b/UI/MailPartViewers/UIxMailPartAlternativeViewer.m index 5dc10f575..a2cff584b 100644 --- a/UI/MailPartViewers/UIxMailPartAlternativeViewer.m +++ b/UI/MailPartViewers/UIxMailPartAlternativeViewer.m @@ -41,7 +41,7 @@ @interface UIxMailPartAlternativeViewer : UIxMailPartViewer { - id childInfo; + id childInfo; unsigned int childIndex; } @@ -49,120 +49,148 @@ @implementation UIxMailPartAlternativeViewer -- (void)dealloc { +- (void) dealloc +{ [childInfo release]; [super dealloc]; } /* caches */ -- (void)resetBodyInfoCaches { - [childInfo release]; childInfo = nil; +- (void) resetBodyInfoCaches +{ + [childInfo release]; + childInfo = nil; childIndex = 0; + [super resetBodyInfoCaches]; } /* part selection */ -- (NSArray *)childPartTypes { +- (NSArray *) childPartTypes +{ NSMutableArray *types; - unsigned i, count; - NSArray *childParts; + NSArray *childParts; + NSEnumerator *allParts; + NSString *mt, *st; + NSDictionary *currentPart; - childParts = [[self bodyInfo] valueForKey:@"parts"]; - count = [childParts count]; - types = [NSMutableArray arrayWithCapacity:count]; - - for (i = 0; i < count; i++) { - NSString *mt, *st; + types = [NSMutableArray array]; + + childParts = [[self bodyInfo] valueForKey: @"parts"]; + allParts = [childParts objectEnumerator]; + + while ((currentPart = [allParts nextObject])) + { + mt = [[currentPart valueForKey:@"type"] lowercaseString]; + st = [[currentPart valueForKey:@"subtype"] lowercaseString]; + [types addObject: [NSString stringWithFormat: @"%@/%@", mt, st]]; + } - mt = [[[childParts objectAtIndex:i] valueForKey:@"type"] lowercaseString]; - st = [[[childParts objectAtIndex:i] valueForKey:@"subtype"] - lowercaseString]; - mt = [[mt stringByAppendingString:@"/"] stringByAppendingString:st]; - [types addObject:mt ? mt : (id)[NSNull null]]; - } return types; } -- (int)selectPartIndexFromTypes:(NSArray *)_types { +- (int) selectPartIndexFromTypes: (NSArray *) types +{ /* returns the index of the selected part or NSNotFound */ - unsigned i, count; + unsigned int count, max; + int index; - if ((count = [_types count]) == 0) - return NSNotFound; - - if ((i = [_types indexOfObject:@"text/html"]) != NSNotFound) - return i; - if ((i = [_types indexOfObject:@"text/plain"]) != NSNotFound) - return i; + index = -1; - /* then we scan for other text types and choose the first one found */ - for (i = 0; i < count; i++) { - if ([(NSString *)[_types objectAtIndex:i] hasPrefix:@"text/"]) - return i; - } - - /* as a fallback, we select the first available part */ - return 0; + max = [types count]; + if (max > 0) + { + index = [types indexOfObject: @"text/html"]; + if (index == NSNotFound) + { + index = [types indexOfObject: @"text/plain"]; + if (index == NSNotFound) + { + count = 0; + while (index == -1 + && count < max) + if ([[types objectAtIndex: count] hasPrefix: @"text/"]) + index = count; + else + count++; + if (index == -1) + index = 0; + } + } + else + index = count; + } + else + index = NSNotFound; + + return index; } -- (void)selectChildInfo { - unsigned idx; +- (void) selectChildInfo +{ + int idx; - [childInfo release]; childInfo = nil; + [childInfo release]; + childInfo = nil; childIndex = 0; - idx = [self selectPartIndexFromTypes:[self childPartTypes]]; - if (idx == NSNotFound) { - [self errorWithFormat:@"could not select a part of types: %@", - [self childPartTypes]]; - return; - } - - childIndex = idx + 1; - childInfo = - [[[[self bodyInfo] valueForKey:@"parts"] objectAtIndex:idx] retain]; + idx = [self selectPartIndexFromTypes: [self childPartTypes]]; + if (idx == NSNotFound) + [self errorWithFormat: @"could not select a part of types: %@", + [self childPartTypes]]; + else + { + childIndex = idx + 1; + childInfo = [[[self bodyInfo] valueForKey:@"parts"] objectAtIndex: idx]; + [childInfos retain]; + } } /* accessors */ -- (id)childInfo { - if (childInfo == nil) +- (id) childInfo +{ + if (!childInfo) [self selectChildInfo]; return childInfo; } -- (unsigned int)childIndex { - if (childIndex == 0) +- (unsigned int) childIndex +{ + if (!childIndex) [self selectChildInfo]; - + return childIndex - 1; } -- (NSString *)childPartName { - char buf[8]; - sprintf(buf, "%d", [self childIndex] + 1); - return [NSString stringWithCString:buf]; +- (NSString *) childPartName +{ + return [NSString stringWithFormat: @"%d", ([self childIndex] + 1)]; } -- (id)childPartPath { +- (id) childPartPath +{ NSArray *pp; pp = [self partPath]; + return [pp count] > 0 - ? [pp arrayByAddingObject:[self childPartName]] - : [NSArray arrayWithObject:[self childPartName]]; + ? [pp arrayByAddingObject: [self childPartName]] + : [NSArray arrayWithObject: [self childPartName]]; } /* nested viewers */ -- (id)contentViewerComponent { +- (id) contentViewerComponent +{ id info; info = [self childInfo]; - return [[[self context] mailRenderingContext] viewerForBodyInfo:info]; + + return [[context mailRenderingContext] viewerForBodyInfo:info]; } @end /* UIxMailPartAlternativeViewer */