From d90d68c7ba923fea3d805a9ebf872f84b6947e57 Mon Sep 17 00:00:00 2001 From: "Hoa V. DINH" Date: Thu, 25 Jul 2013 11:07:10 -0700 Subject: Moved iOS MCOMessageView to src/ui/ios --- .../iOS UI Test.xcodeproj/project.pbxproj | 42 ++- .../macExample.xcodeproj/project.pbxproj | 20 +- src/ui/common/MCOCIDURLProtocol.h | 31 ++ src/ui/common/MCOCIDURLProtocol.mm | 142 +++++++ src/ui/ios/MCOMessageView.h | 50 +++ src/ui/ios/MCOMessageView.mm | 416 +++++++++++++++++++++ src/ui/mac/MCOCIDURLProtocol.h | 31 -- src/ui/mac/MCOCIDURLProtocol.mm | 148 -------- 8 files changed, 684 insertions(+), 196 deletions(-) create mode 100644 src/ui/common/MCOCIDURLProtocol.h create mode 100644 src/ui/common/MCOCIDURLProtocol.mm create mode 100755 src/ui/ios/MCOMessageView.h create mode 100755 src/ui/ios/MCOMessageView.mm delete mode 100644 src/ui/mac/MCOCIDURLProtocol.h delete mode 100644 src/ui/mac/MCOCIDURLProtocol.mm diff --git a/example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj b/example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj index eec98dc0..da41f157 100644 --- a/example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj +++ b/example/ios/iOS UI Test/iOS UI Test.xcodeproj/project.pbxproj @@ -24,9 +24,7 @@ AB9EAE3617036FD700D750C7 /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = AB9EAE3417036FD600D750C7 /* SettingsViewController.m */; }; AB9EAE3717036FD700D750C7 /* SettingsViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = AB9EAE3517036FD700D750C7 /* SettingsViewController.xib */; }; AB9EAE40170374D900D750C7 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB9EAE3F170374D900D750C7 /* Security.framework */; }; - ABE40269173F0282007F1FB3 /* MCOMessageViewScript.js in Resources */ = {isa = PBXBuildFile; fileRef = ABE40267173F0151007F1FB3 /* MCOMessageViewScript.js */; }; ABE4026B173F3FCE007F1FB3 /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ABE4026A173F3FCE007F1FB3 /* ImageIO.framework */; }; - B12AAA3417322654003551C7 /* MCOMessageView.mm in Sources */ = {isa = PBXBuildFile; fileRef = B12AAA3017322654003551C7 /* MCOMessageView.mm */; }; B12AAA3517322654003551C7 /* MCTMsgViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = B12AAA3217322654003551C7 /* MCTMsgViewController.mm */; }; C6D7194D178BB8B4008ED15F /* GTMHTTPFetcher.m in Sources */ = {isa = PBXBuildFile; fileRef = C6D71939178BA812008ED15F /* GTMHTTPFetcher.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; C6D7194E178BB8B4008ED15F /* GTMHTTPFetchHistory.m in Sources */ = {isa = PBXBuildFile; fileRef = C6D7193B178BA812008ED15F /* GTMHTTPFetchHistory.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; @@ -35,6 +33,8 @@ C6D71951178BB8B4008ED15F /* GTMOAuth2ViewControllerTouch.m in Sources */ = {isa = PBXBuildFile; fileRef = C6D71947178BA812008ED15F /* GTMOAuth2ViewControllerTouch.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; C6D71952178BB8B4008ED15F /* GTMOAuth2ViewTouch.xib in Resources */ = {isa = PBXBuildFile; fileRef = C6D71948178BA812008ED15F /* GTMOAuth2ViewTouch.xib */; }; C6D71954178BB91E008ED15F /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C6D71953178BB91E008ED15F /* SystemConfiguration.framework */; }; + C6ED6D2917A19F2700A4A14C /* MCOCIDURLProtocol.mm in Sources */ = {isa = PBXBuildFile; fileRef = C6ED6D2817A19F1500A4A14C /* MCOCIDURLProtocol.mm */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; + C6ED6D2A17A19F2B00A4A14C /* MCOMessageView.mm in Sources */ = {isa = PBXBuildFile; fileRef = C6ED6D2517A19EF800A4A14C /* MCOMessageView.mm */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; }; DA9AD477179331A600743873 /* MCTTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = DA9AD476179331A600743873 /* MCTTableViewCell.m */; }; /* End PBXBuildFile section */ @@ -109,10 +109,7 @@ AB9EAE3417036FD600D750C7 /* SettingsViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SettingsViewController.m; sourceTree = ""; }; AB9EAE3517036FD700D750C7 /* SettingsViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SettingsViewController.xib; sourceTree = ""; }; AB9EAE3F170374D900D750C7 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; - ABE40267173F0151007F1FB3 /* MCOMessageViewScript.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = MCOMessageViewScript.js; sourceTree = ""; }; ABE4026A173F3FCE007F1FB3 /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = System/Library/Frameworks/ImageIO.framework; sourceTree = SDKROOT; }; - B12AAA2F17322654003551C7 /* MCOMessageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCOMessageView.h; sourceTree = ""; }; - B12AAA3017322654003551C7 /* MCOMessageView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOMessageView.mm; sourceTree = ""; }; B12AAA3117322654003551C7 /* MCTMsgViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCTMsgViewController.h; sourceTree = ""; }; B12AAA3217322654003551C7 /* MCTMsgViewController.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MCTMsgViewController.mm; sourceTree = ""; }; C6D71938178BA812008ED15F /* GTMHTTPFetcher.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GTMHTTPFetcher.h; sourceTree = ""; }; @@ -127,6 +124,10 @@ C6D71947178BA812008ED15F /* GTMOAuth2ViewControllerTouch.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GTMOAuth2ViewControllerTouch.m; sourceTree = ""; }; C6D71948178BA812008ED15F /* GTMOAuth2ViewTouch.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = GTMOAuth2ViewTouch.xib; sourceTree = ""; }; C6D71953178BB91E008ED15F /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; }; + C6ED6D2417A19EF800A4A14C /* MCOMessageView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MCOMessageView.h; sourceTree = ""; }; + C6ED6D2517A19EF800A4A14C /* MCOMessageView.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOMessageView.mm; sourceTree = ""; }; + C6ED6D2717A19F1500A4A14C /* MCOCIDURLProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MCOCIDURLProtocol.h; sourceTree = ""; }; + C6ED6D2817A19F1500A4A14C /* MCOCIDURLProtocol.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOCIDURLProtocol.mm; sourceTree = ""; }; DA9AD475179331A600743873 /* MCTTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCTTableViewCell.h; sourceTree = ""; }; DA9AD476179331A600743873 /* MCTTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MCTTableViewCell.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -207,7 +208,6 @@ AB9EAE0B170368F000D750C7 /* iOS UI Test */ = { isa = PBXGroup; children = ( - C6D71936178BA812008ED15F /* gtm-oauth2 */, AB7BA4321713898B00104953 /* mailcore2.xcodeproj */, AB9EAE11170368F000D750C7 /* main.mm */, AB9EAE14170368F000D750C7 /* AppDelegate.h */, @@ -215,14 +215,14 @@ AB9EAE1D170368F000D750C7 /* MainStoryboard.storyboard */, AB9EAE20170368F000D750C7 /* MasterViewController.h */, AB9EAE21170368F000D750C7 /* MasterViewController.m */, - B12AAA2F17322654003551C7 /* MCOMessageView.h */, - B12AAA3017322654003551C7 /* MCOMessageView.mm */, - ABE40267173F0151007F1FB3 /* MCOMessageViewScript.js */, B12AAA3117322654003551C7 /* MCTMsgViewController.h */, B12AAA3217322654003551C7 /* MCTMsgViewController.mm */, AB9EAE3317036FD600D750C7 /* SettingsViewController.h */, AB9EAE3417036FD600D750C7 /* SettingsViewController.m */, AB9EAE3517036FD700D750C7 /* SettingsViewController.xib */, + C6D71936178BA812008ED15F /* gtm-oauth2 */, + C6ED6D2617A19F1500A4A14C /* common */, + C6ED6D2317A19EF800A4A14C /* ios */, AB9EAE0C170368F000D750C7 /* Supporting Files */, AB665BC917134336007F2151 /* common */, DA9AD475179331A600743873 /* MCTTableViewCell.h */, @@ -287,6 +287,26 @@ path = Touch; sourceTree = ""; }; + C6ED6D2317A19EF800A4A14C /* ios */ = { + isa = PBXGroup; + children = ( + C6ED6D2417A19EF800A4A14C /* MCOMessageView.h */, + C6ED6D2517A19EF800A4A14C /* MCOMessageView.mm */, + ); + name = ios; + path = ../../../../src/ui/ios; + sourceTree = ""; + }; + C6ED6D2617A19F1500A4A14C /* common */ = { + isa = PBXGroup; + children = ( + C6ED6D2717A19F1500A4A14C /* MCOCIDURLProtocol.h */, + C6ED6D2817A19F1500A4A14C /* MCOCIDURLProtocol.mm */, + ); + name = common; + path = ../../../../src/ui/common; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -384,7 +404,6 @@ buildActionMask = 2147483647; files = ( C6D71952178BB8B4008ED15F /* GTMOAuth2ViewTouch.xib in Resources */, - ABE40269173F0282007F1FB3 /* MCOMessageViewScript.js in Resources */, AB9EAE10170368F000D750C7 /* InfoPlist.strings in Resources */, AB9EAE18170368F000D750C7 /* Default.png in Resources */, AB9EAE1A170368F000D750C7 /* Default@2x.png in Resources */, @@ -407,11 +426,12 @@ AB9EAE22170368F000D750C7 /* MasterViewController.m in Sources */, AB9EAE3617036FD700D750C7 /* SettingsViewController.m in Sources */, C6D7194D178BB8B4008ED15F /* GTMHTTPFetcher.m in Sources */, + C6ED6D2917A19F2700A4A14C /* MCOCIDURLProtocol.mm in Sources */, C6D71951178BB8B4008ED15F /* GTMOAuth2ViewControllerTouch.m in Sources */, + C6ED6D2A17A19F2B00A4A14C /* MCOMessageView.mm in Sources */, AB665BCD17134336007F2151 /* FXKeychain.m in Sources */, C6D71950178BB8B4008ED15F /* GTMOAuth2SignIn.m in Sources */, C6D7194F178BB8B4008ED15F /* GTMOAuth2Authentication.m in Sources */, - B12AAA3417322654003551C7 /* MCOMessageView.mm in Sources */, B12AAA3517322654003551C7 /* MCTMsgViewController.mm in Sources */, DA9AD477179331A600743873 /* MCTTableViewCell.m in Sources */, ); diff --git a/example/mac/macExample/macExample.xcodeproj/project.pbxproj b/example/mac/macExample/macExample.xcodeproj/project.pbxproj index c03d6be7..89dcf3c0 100644 --- a/example/mac/macExample/macExample.xcodeproj/project.pbxproj +++ b/example/mac/macExample/macExample.xcodeproj/project.pbxproj @@ -10,7 +10,6 @@ AB666C5917484E6200545290 /* FXKeychain.m in Sources */ = {isa = PBXBuildFile; fileRef = AB666C5817484E6200545290 /* FXKeychain.m */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; }; AB666C5B1748558000545290 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB666C5A1748558000545290 /* Security.framework */; }; C64FF38416AF97F400F8C162 /* main.mm in Sources */ = {isa = PBXBuildFile; fileRef = C64FF38316AF97F400F8C162 /* main.mm */; }; - C6BD2873170BC5C500A91AC1 /* MCOCIDURLProtocol.mm in Sources */ = {isa = PBXBuildFile; fileRef = C6BD2870170BC5C500A91AC1 /* MCOCIDURLProtocol.mm */; }; C6BD2874170BC5C500A91AC1 /* MCOMessageView.mm in Sources */ = {isa = PBXBuildFile; fileRef = C6BD2872170BC5C500A91AC1 /* MCOMessageView.mm */; }; C6BD28B4170BDFE500A91AC1 /* MailCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C6BD28B3170BDFE500A91AC1 /* MailCore.framework */; }; C6D42BB616ABB39A002BB4F9 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C6D42BB516ABB39A002BB4F9 /* Cocoa.framework */; }; @@ -27,6 +26,7 @@ C6D71932178B7D6E008ED15F /* GTMHTTPFetchHistory.m in Sources */ = {isa = PBXBuildFile; fileRef = C6D71921178B7D55008ED15F /* GTMHTTPFetchHistory.m */; }; C6D71933178B7D72008ED15F /* GTMOAuth2Window.xib in Resources */ = {isa = PBXBuildFile; fileRef = C6D71928178B7D55008ED15F /* GTMOAuth2Window.xib */; }; C6D71934178B7D72008ED15F /* GTMOAuth2WindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = C6D7192A178B7D55008ED15F /* GTMOAuth2WindowController.m */; }; + C6ED6CE817A0D06200A4A14C /* MCOCIDURLProtocol.mm in Sources */ = {isa = PBXBuildFile; fileRef = C6ED6CE717A0D05300A4A14C /* MCOCIDURLProtocol.mm */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -92,8 +92,6 @@ AB666C5817484E6200545290 /* FXKeychain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = FXKeychain.m; path = ../../../common/FXKeychain.m; sourceTree = ""; }; AB666C5A1748558000545290 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; C64FF38316AF97F400F8C162 /* main.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = main.mm; sourceTree = ""; }; - C6BD286F170BC5C500A91AC1 /* MCOCIDURLProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCOCIDURLProtocol.h; sourceTree = ""; }; - C6BD2870170BC5C500A91AC1 /* MCOCIDURLProtocol.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOCIDURLProtocol.mm; sourceTree = ""; }; C6BD2871170BC5C500A91AC1 /* MCOMessageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MCOMessageView.h; sourceTree = ""; }; C6BD2872170BC5C500A91AC1 /* MCOMessageView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOMessageView.mm; sourceTree = ""; }; C6BD2877170BC8ED00A91AC1 /* mailcore2.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = mailcore2.xcodeproj; path = "../../../build-mac/mailcore2.xcodeproj"; sourceTree = ""; }; @@ -123,6 +121,8 @@ C6D71928178B7D55008ED15F /* GTMOAuth2Window.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = GTMOAuth2Window.xib; sourceTree = ""; }; C6D71929178B7D55008ED15F /* GTMOAuth2WindowController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GTMOAuth2WindowController.h; sourceTree = ""; }; C6D7192A178B7D55008ED15F /* GTMOAuth2WindowController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GTMOAuth2WindowController.m; sourceTree = ""; }; + C6ED6CE617A0D05300A4A14C /* MCOCIDURLProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MCOCIDURLProtocol.h; sourceTree = ""; }; + C6ED6CE717A0D05300A4A14C /* MCOCIDURLProtocol.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = MCOCIDURLProtocol.mm; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -153,6 +153,7 @@ C6BD286D170BC5C500A91AC1 /* ui */ = { isa = PBXGroup; children = ( + C6ED6CE517A0D05300A4A14C /* common */, C6BD286E170BC5C500A91AC1 /* mac */, ); name = ui; @@ -162,8 +163,6 @@ C6BD286E170BC5C500A91AC1 /* mac */ = { isa = PBXGroup; children = ( - C6BD286F170BC5C500A91AC1 /* MCOCIDURLProtocol.h */, - C6BD2870170BC5C500A91AC1 /* MCOCIDURLProtocol.mm */, C6BD2871170BC5C500A91AC1 /* MCOMessageView.h */, C6BD2872170BC5C500A91AC1 /* MCOMessageView.mm */, ); @@ -308,6 +307,15 @@ path = Mac; sourceTree = ""; }; + C6ED6CE517A0D05300A4A14C /* common */ = { + isa = PBXGroup; + children = ( + C6ED6CE617A0D05300A4A14C /* MCOCIDURLProtocol.h */, + C6ED6CE717A0D05300A4A14C /* MCOCIDURLProtocol.mm */, + ); + path = common; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -417,6 +425,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + C6ED6CE817A0D06200A4A14C /* MCOCIDURLProtocol.mm in Sources */, C6D42BC916ABB39A002BB4F9 /* AppDelegate.m in Sources */, C6D71930178B7D67008ED15F /* GTMOAuth2SignIn.m in Sources */, C6D42BE916ACF711002BB4F9 /* MCTMsgListViewController.m in Sources */, @@ -424,7 +433,6 @@ C6D71931178B7D6C008ED15F /* GTMHTTPFetcher.m in Sources */, C6D71934178B7D72008ED15F /* GTMOAuth2WindowController.m in Sources */, C64FF38416AF97F400F8C162 /* main.mm in Sources */, - C6BD2873170BC5C500A91AC1 /* MCOCIDURLProtocol.mm in Sources */, C6BD2874170BC5C500A91AC1 /* MCOMessageView.mm in Sources */, AB666C5917484E6200545290 /* FXKeychain.m in Sources */, C6D7192F178B7D65008ED15F /* GTMOAuth2Authentication.m in Sources */, diff --git a/src/ui/common/MCOCIDURLProtocol.h b/src/ui/common/MCOCIDURLProtocol.h new file mode 100644 index 00000000..ac4b0f2b --- /dev/null +++ b/src/ui/common/MCOCIDURLProtocol.h @@ -0,0 +1,31 @@ +// +// MCTCIDURLProtocol.h +// testUI +// +// Created by DINH Viêt Hoà on 1/22/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#import + +#include + +@interface MCOCIDURLProtocol : NSURLProtocol + ++ (void) registerProtocol; + ++ (BOOL) isCID:(NSURL *)url; ++ (BOOL) isXMailcoreImage:(NSURL *)url; + +#ifdef __cplusplus ++ (void) startLoadingWithMessage:(MCOAbstractMessage *)message + partUniqueID:(NSString *)partUniqueID + data:(NSData *)data + request:(NSMutableURLRequest *)request; + ++ (void) partDownloadedMessage:(MCOAbstractMessage *)message + partUniqueID:(NSString *)partUniqueID + data:(NSData *)data; +#endif + +@end diff --git a/src/ui/common/MCOCIDURLProtocol.mm b/src/ui/common/MCOCIDURLProtocol.mm new file mode 100644 index 00000000..ad529f54 --- /dev/null +++ b/src/ui/common/MCOCIDURLProtocol.mm @@ -0,0 +1,142 @@ +// +// MCTCIDURLProtocol.m +// testUI +// +// Created by DINH Viêt Hoà on 1/22/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#import "MCOCIDURLProtocol.h" + +#define MCOCIDURLProtocolDownloadedNotification @"MCOCIDURLProtocolDownloadedNotification" + +@implementation MCOCIDURLProtocol + ++ (void) registerProtocol +{ + static dispatch_once_t onceToken = 0; + dispatch_once(&onceToken, ^{ + [NSURLProtocol registerClass:[MCOCIDURLProtocol class]]; + }); +} + ++ (BOOL) canInitWithRequest:(NSURLRequest *)theRequest +{ + if ([self isCID:[theRequest URL]]) + return YES; + if ([self isXMailcoreImage:[theRequest URL]]) + return YES; + return NO; +} + ++ (BOOL) isCID:(NSURL *)url +{ + NSString *theScheme = [url scheme]; + if ([theScheme caseInsensitiveCompare:@"cid"] == NSOrderedSame) + return YES; + return NO; +} + ++ (BOOL) isXMailcoreImage:(NSURL *)url +{ + NSString *theScheme = [url scheme]; + if ([theScheme caseInsensitiveCompare:@"x-mailcore-image"] == NSOrderedSame) + return YES; + return NO; +} + ++ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request +{ + return request; +} + +- (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id < NSURLProtocolClient >)client +{ + self = [super initWithRequest:request cachedResponse:cachedResponse client:client]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_downloaded:) name:MCOCIDURLProtocolDownloadedNotification object:nil]; + return self; +} + +- (void) dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [super dealloc]; +} + +- (NSString *) _partUniqueID +{ + return [NSURLProtocol propertyForKey:@"PartUniqueID" inRequest:[self request]]; +} + +- (NSData *) _data +{ + return [NSURLProtocol propertyForKey:@"Data" inRequest:[self request]]; +} + +- (MCOAbstractMessage *) _message +{ + return (MCOAbstractMessage *) [NSURLProtocol propertyForKey:@"Message" inRequest:[self request]]; +} + +- (void) startLoading +{ + //NSLog(@"waiting for %p %@", self, [self _partUniqueID]); + if ([self _data] != NULL) { + [[self class] partDownloadedMessage:[self _message] partUniqueID:[self _partUniqueID] data:[self _data]]; + } +} + +- (void) _downloaded:(NSNotification *)notification +{ + NSDictionary * userInfo = [notification userInfo]; + + NSString * notifPartID = [userInfo objectForKey:@"PartUniqueID"]; + MCOAbstractMessage * notifMessage = [userInfo objectForKey:@"Message"]; + if (notifMessage != [self _message]) { + return; + } + if (![[self _partUniqueID] isEqualToString:notifPartID]) { + return; + } + + NSData * data = [userInfo objectForKey:@"Data"]; + NSURLResponse * response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"application/data" + expectedContentLength:[data length] textEncodingName:nil]; + [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; + [[self client] URLProtocol:self didLoadData:data]; + [[self client] URLProtocolDidFinishLoading:self]; + [response release]; +} + +- (void) stopLoading +{ +} + ++ (void) startLoadingWithMessage:(MCOAbstractMessage *)message + partUniqueID:(NSString *)partUniqueID + data:(NSData *)data + request:(NSMutableURLRequest *)request +{ + [NSURLProtocol setProperty:message + forKey:@"Message" inRequest:request]; + if (data != NULL) { + [NSURLProtocol setProperty:data forKey:@"Data" inRequest:request]; + } + [NSURLProtocol setProperty:partUniqueID forKey:@"PartUniqueID" inRequest:request]; +} + ++ (void) partDownloadedMessage:(MCOAbstractMessage *)message + partUniqueID:(NSString *)partUniqueID + data:(NSData *)data +{ + NSMutableDictionary * userInfo = [[NSMutableDictionary alloc] init]; + [userInfo setObject:message forKey:@"Message"]; + [userInfo setObject:partUniqueID forKey:@"PartUniqueID"]; + if (data != NULL) { + [userInfo setObject:data forKey:@"Data"]; + } + [[NSNotificationCenter defaultCenter] postNotificationName:MCOCIDURLProtocolDownloadedNotification object:nil userInfo:userInfo]; + [userInfo release]; +} + +@end diff --git a/src/ui/ios/MCOMessageView.h b/src/ui/ios/MCOMessageView.h new file mode 100755 index 00000000..3c4ce50d --- /dev/null +++ b/src/ui/ios/MCOMessageView.h @@ -0,0 +1,50 @@ +// +// MCOMessageView.h +// testUI +// +// Created by DINH Viêt Hoà on 1/19/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#include + +#import + +@protocol MCOMessageViewDelegate; + +@interface MCOMessageView : UIView + +@property (nonatomic, copy) NSString * folder; +@property (nonatomic, strong) MCOAbstractMessage * message; + +@property (nonatomic, assign) id delegate; + +@property (nonatomic, assign) BOOL prefetchIMAPImagesEnabled; +@property (nonatomic, assign) BOOL prefetchIMAPAttachmentsEnabled; + +@end + +@protocol MCOMessageViewDelegate + +@optional +- (NSData *) MCOMessageView:(MCOMessageView *)view dataForPartWithUniqueID:(NSString *)partUniqueID; +- (void) MCOMessageView:(MCOMessageView *)view fetchDataForPartWithUniqueID:(NSString *)partUniqueID + downloadedFinished:(void (^)(NSError * error))downloadFinished; + +- (NSString *) MCOMessageView_templateForMainHeader:(MCOMessageView *)view; +- (NSString *) MCOMessageView_templateForImage:(MCOMessageView *)view; +- (NSString *) MCOMessageView_templateForAttachment:(MCOMessageView *)view; +- (NSString *) MCOMessageView_templateForMessage:(MCOMessageView *)view; +- (NSString *) MCOMessageView_templateForEmbeddedMessage:(MCOMessageView *)view; +- (NSString *) MCOMessageView_templateForEmbeddedMessageHeader:(MCOMessageView *)view; +- (NSString *) MCOMessageView_templateForAttachmentSeparator:(MCOMessageView *)view; + +- (NSDictionary *) MCOMessageView:(MCOMessageView *)view templateValuesForPartWithUniqueID:(NSString *)uniqueID; +- (NSDictionary *) MCOMessageView:(MCOMessageView *)view templateValuesForHeader:(MCOMessageHeader *)header; +- (BOOL) MCOMessageView:(MCOMessageView *)view canPreviewPart:(MCOAbstractPart *)part; + +- (NSString *) MCOMessageView:(MCOMessageView *)view filteredHTMLForPart:(NSString *)html; +- (NSString *) MCOMessageView:(MCOMessageView *)view filteredHTMLForMessage:(NSString *)html; +- (NSData *) MCOMessageView:(MCOMessageView *)view previewForData:(NSData *)data isHTMLInlineImage:(BOOL)isHTMLInlineImage; + +@end diff --git a/src/ui/ios/MCOMessageView.mm b/src/ui/ios/MCOMessageView.mm new file mode 100755 index 00000000..e3786bb5 --- /dev/null +++ b/src/ui/ios/MCOMessageView.mm @@ -0,0 +1,416 @@ +// +// MCOMessageView.m +// testUI +// +// Created by DINH Viêt Hoà on 1/19/13. +// Copyright (c) 2013 MailCore. All rights reserved. +// + +#import "MCOMessageView.h" +#import "MCOCIDURLProtocol.h" + +static NSString * mainJavascript = @"\ +var imageElements = function() {\ + var imageNodes = document.getElementsByTagName('img');\ + return [].slice.call(imageNodes);\ +};\ +\ +var findCIDImageURL = function() {\ + var images = imageElements();\ + \ + var imgLinks = [];\ + for (var i = 0; i < images.length; i++) {\ + var url = images[i].getAttribute('src');\ + if (url.indexOf('cid:') == 0 || url.indexOf('x-mailcore-image:') == 0)\ + imgLinks.push(url);\ + }\ + return JSON.stringify(imgLinks);\ +};\ +\ +var replaceImageSrc = function(info) {\ + var images = imageElements();\ + \ + for (var i = 0; i < images.length; i++) {\ + var url = images[i].getAttribute('src');\ + if (url.indexOf(info.URLKey) == 0) {\ + images[i].setAttribute('src', info.LocalPathKey);\ + break;\ + }\ + }\ +};\ +"; + +static NSString * mainStyle = @"\ +body {\ + font-family: Helvetica;\ + font-size: 14px;\ + word-wrap: break-word;\ + -webkit-text-size-adjust:none;\ + -webkit-nbsp-mode: space;\ +}\ +\ +pre {\ + white-space: pre-wrap;\ +}\ +"; + +@interface MCOMessageView () + +@end + +@implementation MCOMessageView { + UIWebView * _webView; + NSString * _folder; + MCOAbstractMessage * _message; + id _delegate; + BOOL _prefetchIMAPImagesEnabled; + BOOL _prefetchIMAPAttachmentsEnabled; +} + +@synthesize folder = _folder; +@synthesize delegate = _delegate; +@synthesize prefetchIMAPImagesEnabled = _prefetchIMAPImagesEnabled; +@synthesize prefetchIMAPAttachmentsEnabled = _prefetchIMAPAttachmentsEnabled; + +- (id)initWithFrame:(CGRect)frame +{ + self = [super initWithFrame:frame]; + + _webView = [[UIWebView alloc] initWithFrame:[self bounds]]; + [_webView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth)]; + [_webView setDelegate:self]; + [self addSubview:_webView]; + + return self; +} + +- (void) dealloc +{ + [_message release]; + [_folder release]; + [_webView release]; + + [super dealloc]; +} + +- (void) setMessage:(MCOAbstractMessage *)message +{ + [_message release]; + _message = [message retain]; + + [_webView stopLoading]; + [self _refresh]; +} + +- (MCOAbstractMessage *) message +{ + return _message; +} + +- (void) _refresh +{ + NSString * content; + + if (_message == nil) { + content = nil; + } + else { + if ([_message isKindOfClass:[MCOIMAPMessage class]]) { + content = [(MCOIMAPMessage *) _message htmlRenderingWithFolder:_folder delegate:self]; + } + else if ([_message isKindOfClass:[MCOMessageBuilder class]]) { + content = [(MCOMessageBuilder *) _message htmlRenderingWithDelegate:self]; + } + else if ([_message isKindOfClass:[MCOMessageParser class]]) { + content = [(MCOMessageParser *) _message htmlRenderingWithDelegate:self]; + } + else { + content = nil; + MCAssert(0); + } + } + if (content == nil) { + [_webView loadHTMLString:@"" baseURL:nil]; + return; + } + + NSMutableString * html = [NSMutableString string]; + [html appendFormat:@"" + @"%@", mainJavascript, mainStyle, content]; + [_webView loadHTMLString:html baseURL:nil]; +} + +- (void) _loadImages +{ + NSString * result = [_webView stringByEvaluatingJavaScriptFromString:@"findCIDImageURL()"]; + NSLog(@"----------"); + NSLog(@"%@", result); + NSData * data = [result dataUsingEncoding:NSUTF8StringEncoding]; + NSError *error = nil; + NSArray * imagesURLStrings = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; + + for(NSString * urlString in imagesURLStrings) { + MCOAbstractPart * part = nil; + NSURL * url; + + url = [NSURL URLWithString:urlString]; + if ([MCOCIDURLProtocol isCID:url]) { + part = [self _partForCIDURL:url]; + } + else if ([MCOCIDURLProtocol isXMailcoreImage:url]) { + NSString * specifier = [url resourceSpecifier]; + NSString * partUniqueID = specifier; + part = [self _partForUniqueID:partUniqueID]; + } + + if (part == nil) + continue; + + NSString * partUniqueID = [part uniqueID]; + NSData * data = [[self delegate] MCOMessageView:self dataForPartWithUniqueID:partUniqueID]; + + void (^replaceImages)(NSError *error) = ^(NSError *error) { + NSData * downloadedData = [[self delegate] MCOMessageView:self dataForPartWithUniqueID:partUniqueID]; + NSData * previewData = [[self delegate] MCOMessageView:self previewForData:downloadedData isHTMLInlineImage:[MCOCIDURLProtocol isCID:url]]; + NSString * filename = [NSString stringWithFormat:@"%lu", (unsigned long)urlString.hash]; + NSURL * cacheURL = [self _cacheJPEGImageData:previewData withFilename:filename]; + + NSDictionary * args = @{ @"URLKey": urlString, @"LocalPathKey": cacheURL.absoluteString }; + NSString * jsonString = [self _jsonEscapedStringFromDictionary:args]; + + NSString * replaceScript = [NSString stringWithFormat:@"replaceImageSrc(%@)", jsonString]; + [_webView stringByEvaluatingJavaScriptFromString:replaceScript]; + }; + + if (data == nil) { + [[self delegate] MCOMessageView:self fetchDataForPartWithUniqueID:partUniqueID downloadedFinished:^(NSError * error) { + replaceImages(error); + }]; + } else { + replaceImages(nil); + } + } +} + +- (NSString *) _jsonEscapedStringFromDictionary:(NSDictionary *)dictionary +{ + NSData * json = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil]; + NSString * jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]; + return jsonString; +} + +- (NSURL *) _cacheJPEGImageData:(NSData *)imageData withFilename:(NSString *)filename +{ + NSString * path = [[NSTemporaryDirectory() stringByAppendingPathComponent:filename] stringByAppendingPathExtension:@"jpg"]; + [imageData writeToFile:path atomically:YES]; + return [NSURL fileURLWithPath:path]; +} + +- (MCOAbstractPart *) _partForCIDURL:(NSURL *)url +{ + return [_message partForContentID:[url resourceSpecifier]]; +} + +- (MCOAbstractPart *) _partForUniqueID:(NSString *)partUniqueID +{ + return [_message partForUniqueID:partUniqueID]; +} + +- (NSData *) _dataForIMAPPart:(MCOIMAPPart *)part folder:(NSString *)folder +{ + NSData * data; + NSString * partUniqueID = [part uniqueID]; + data = [[self delegate] MCOMessageView:self dataForPartWithUniqueID:partUniqueID]; + if (data == NULL) { + [[self delegate] MCOMessageView:self fetchDataForPartWithUniqueID:partUniqueID downloadedFinished:^(NSError * error) { + [self _refresh]; + }]; + } + return data; +} + +- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { + + NSURLRequest *responseRequest = [self webView:webView resource:nil willSendRequest:request redirectResponse:nil fromDataSource:nil]; + + if(responseRequest == request) { + return YES; + } else { + [webView loadRequest:responseRequest]; + return NO; + } +} + +- (NSURLRequest *)webView:(UIWebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(id)dataSource +{ + if ([[[request URL] scheme] isEqualToString:@"x-mailcore-msgviewloaded"]) { + [self _loadImages]; + } + + return request; +} + +- (BOOL) MCOAbstractMessage:(MCOAbstractMessage *)msg canPreviewPart:(MCOAbstractPart *)part +{ + static NSMutableSet * supportedImageMimeTypes = NULL; + if (supportedImageMimeTypes == NULL) { + supportedImageMimeTypes = [[NSMutableSet alloc] init]; + [supportedImageMimeTypes addObject:@"image/png"]; + [supportedImageMimeTypes addObject:@"image/gif"]; + [supportedImageMimeTypes addObject:@"image/jpg"]; + [supportedImageMimeTypes addObject:@"image/jpeg"]; + } + static NSMutableSet * supportedImageExtension = NULL; + if (supportedImageExtension == NULL) { + supportedImageExtension = [[NSMutableSet alloc] init]; + [supportedImageExtension addObject:@"png"]; + [supportedImageExtension addObject:@"gif"]; + [supportedImageExtension addObject:@"jpg"]; + [supportedImageExtension addObject:@"jpeg"]; + } + + if ([supportedImageMimeTypes containsObject:[[part mimeType] lowercaseString]]) { + return YES; + } + + NSString * ext = nil; + if ([part filename] != nil) { + if ([[part filename] pathExtension] != nil) { + ext = [[[part filename] pathExtension] lowercaseString]; + } + } + if (ext != nil) { + if ([supportedImageExtension containsObject:ext]) + return YES; + } + + if (![[self delegate] respondsToSelector:@selector(MCOMessageView:canPreviewPart:)]) { + return NO; + } + return [[self delegate] MCOMessageView:self canPreviewPart:part]; +} + +- (NSDictionary *) MCOAbstractMessage:(MCOAbstractMessage *)msg templateValuesForHeader:(MCOMessageHeader *)header +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView:templateValuesForHeader:)]) { + return nil; + } + return [[self delegate] MCOMessageView:self templateValuesForHeader:header]; +} + +- (NSDictionary *) MCOAbstractMessage:(MCOAbstractMessage *)msg templateValuesForPart:(MCOAbstractPart *)part +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView:templateValuesForPartWithUniqueID:)]) { + return nil; + } + return [[self delegate] MCOMessageView:self templateValuesForPartWithUniqueID:[part uniqueID]]; +} + +- (NSString *) MCOAbstractMessage:(MCOAbstractMessage *)msg templateForMainHeader:(MCOMessageHeader *)header +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView_templateForMainHeader:)]) { + return nil; + } + return [[self delegate] MCOMessageView_templateForMainHeader:self]; +} + +- (NSString *) MCOAbstractMessage:(MCOAbstractMessage *)msg templateForImage:(MCOAbstractPart *)header +{ + NSString * templateString; + if ([[self delegate] respondsToSelector:@selector(MCOMessageView_templateForImage:)]) { + templateString = [[self delegate] MCOMessageView_templateForImage:self]; + } + else { + templateString = @""; + } + templateString = [NSString stringWithFormat:@"
%@
", templateString]; + return templateString; +} + +- (NSString *) MCOAbstractMessage:(MCOAbstractMessage *)msg templateForAttachment:(MCOAbstractPart *)part +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView_templateForAttachment:)]) { + return NULL; + } + NSString * templateString = [[self delegate] MCOMessageView_templateForAttachment:self]; + templateString = [NSString stringWithFormat:@"
%@
", templateString]; + return templateString; +} + +- (NSString *) MCOAbstractMessage_templateForMessage:(MCOAbstractMessage *)msg +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView_templateForMessage:)]) { + return NULL; + } + return [[self delegate] MCOMessageView_templateForMessage:self]; +} + +- (NSString *) MCOAbstractMessage:(MCOAbstractMessage *)msg templateForEmbeddedMessage:(MCOAbstractMessagePart *)part +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView_templateForEmbeddedMessage:)]) { + return NULL; + } + return [[self delegate] MCOMessageView_templateForEmbeddedMessage:self]; +} + +- (NSString *) MCOAbstractMessage:(MCOAbstractMessage *)msg templateForEmbeddedMessageHeader:(MCOMessageHeader *)header +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView_templateForEmbeddedMessageHeader:)]) { + return NULL; + } + return [[self delegate] MCOMessageView_templateForEmbeddedMessageHeader:self]; +} + +- (NSString *) MCOAbstractMessage_templateForAttachmentSeparator:(MCOAbstractMessage *)msg +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView_templateForAttachmentSeparator:)]) { + return NULL; + } + return [[self delegate] MCOMessageView_templateForAttachmentSeparator:self]; +} + +- (NSString *) MCOAbstractMessage:(MCOAbstractMessage *)msg filterHTMLForPart:(NSString *)html +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView:filteredHTMLForPart:)]) { + return html; + } + return [[self delegate] MCOMessageView:self filteredHTMLForPart:html]; +} + +- (NSString *) MCOAbstractMessage:(MCOAbstractMessage *)msg filterHTMLForMessage:(NSString *)html +{ + if (![[self delegate] respondsToSelector:@selector(MCOMessageView:filteredHTMLForMessage:)]) { + return html; + } + return [[self delegate] MCOMessageView:self filteredHTMLForMessage:html]; +} + +- (NSData *) MCOAbstractMessage:(MCOAbstractMessage *)msg dataForIMAPPart:(MCOIMAPPart *)part folder:(NSString *)folder +{ + return [self _dataForIMAPPart:part folder:folder]; +} + +- (void) MCOAbstractMessage:(MCOAbstractMessage *)msg prefetchAttachmentIMAPPart:(MCOIMAPPart *)part folder:(NSString *)folder +{ + if (!_prefetchIMAPAttachmentsEnabled) + return; + + NSString * partUniqueID = [part uniqueID]; + [[self delegate] MCOMessageView:self fetchDataForPartWithUniqueID:partUniqueID downloadedFinished:^(NSError * error) { + // do nothing + }]; +} + +- (void) MCOAbstractMessage:(MCOAbstractMessage *)msg prefetchImageIMAPPart:(MCOIMAPPart *)part folder:(NSString *)folder +{ + if (!_prefetchIMAPImagesEnabled) + return; + + NSString * partUniqueID = [part uniqueID]; + [[self delegate] MCOMessageView:self fetchDataForPartWithUniqueID:partUniqueID downloadedFinished:^(NSError * error) { + // do nothing + }]; +} + +@end diff --git a/src/ui/mac/MCOCIDURLProtocol.h b/src/ui/mac/MCOCIDURLProtocol.h deleted file mode 100644 index ac4b0f2b..00000000 --- a/src/ui/mac/MCOCIDURLProtocol.h +++ /dev/null @@ -1,31 +0,0 @@ -// -// MCTCIDURLProtocol.h -// testUI -// -// Created by DINH Viêt Hoà on 1/22/13. -// Copyright (c) 2013 MailCore. All rights reserved. -// - -#import - -#include - -@interface MCOCIDURLProtocol : NSURLProtocol - -+ (void) registerProtocol; - -+ (BOOL) isCID:(NSURL *)url; -+ (BOOL) isXMailcoreImage:(NSURL *)url; - -#ifdef __cplusplus -+ (void) startLoadingWithMessage:(MCOAbstractMessage *)message - partUniqueID:(NSString *)partUniqueID - data:(NSData *)data - request:(NSMutableURLRequest *)request; - -+ (void) partDownloadedMessage:(MCOAbstractMessage *)message - partUniqueID:(NSString *)partUniqueID - data:(NSData *)data; -#endif - -@end diff --git a/src/ui/mac/MCOCIDURLProtocol.mm b/src/ui/mac/MCOCIDURLProtocol.mm deleted file mode 100644 index 83730c9d..00000000 --- a/src/ui/mac/MCOCIDURLProtocol.mm +++ /dev/null @@ -1,148 +0,0 @@ -// -// MCTCIDURLProtocol.m -// testUI -// -// Created by DINH Viêt Hoà on 1/22/13. -// Copyright (c) 2013 MailCore. All rights reserved. -// - -#import "MCOCIDURLProtocol.h" - -#import "MCOMessageView.h" - -#define MCOCIDURLProtocolDownloadedNotification @"MCOCIDURLProtocolDownloadedNotification" - -@implementation MCOCIDURLProtocol - -+ (void) registerProtocol -{ - static dispatch_once_t onceToken = 0; - dispatch_once(&onceToken, ^{ - [NSURLProtocol registerClass:[MCOCIDURLProtocol class]]; - }); -} - -+ (BOOL) canInitWithRequest:(NSURLRequest *)theRequest -{ - if ([self isCID:[theRequest URL]]) - return YES; - if ([self isXMailcoreImage:[theRequest URL]]) - return YES; - return NO; -} - -+ (BOOL) isCID:(NSURL *)url -{ - NSString *theScheme = [url scheme]; - if ([theScheme caseInsensitiveCompare:@"cid"] == NSOrderedSame) - return YES; - return NO; -} - -+ (BOOL) isXMailcoreImage:(NSURL *)url -{ - NSString *theScheme = [url scheme]; - if ([theScheme caseInsensitiveCompare:@"x-mailcore-image"] == NSOrderedSame) - return YES; - return NO; -} - -+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request -{ - return request; -} - -- (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id < NSURLProtocolClient >)client -{ - self = [super initWithRequest:request cachedResponse:cachedResponse client:client]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_downloaded:) name:MCOCIDURLProtocolDownloadedNotification object:nil]; - return self; -} - -- (void) dealloc -{ - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [super dealloc]; -} - -- (NSString *) _partUniqueID -{ - return [NSURLProtocol propertyForKey:@"PartUniqueID" inRequest:[self request]]; -} - -- (NSData *) _data -{ - return [NSURLProtocol propertyForKey:@"Data" inRequest:[self request]]; -} - -- (MCOAbstractMessage *) _message -{ - return (MCOAbstractMessage *) [NSURLProtocol propertyForKey:@"Message" inRequest:[self request]]; -} - -- (void) startLoading -{ - //NSLog(@"waiting for %p %@", self, [self _partUniqueID]); - if ([self _data] != NULL) { - [[self class] partDownloadedMessage:[self _message] partUniqueID:[self _partUniqueID] data:[self _data]]; - } -} - -- (void) _downloaded:(NSNotification *)notification -{ - NSDictionary * userInfo = [notification userInfo]; - - //NSLog(@"downloaded?"); - NSString * notifPartID = [userInfo objectForKey:@"PartUniqueID"]; - MCOAbstractMessage * notifMessage = [userInfo objectForKey:@"Message"]; - if (notifMessage != [self _message]) { - return; - } - if (![[self _partUniqueID] isEqualToString:notifPartID]) { - return; - } - - //NSLog(@"downloaded2 %p %@", self, notifPartID); - - NSData * data = [userInfo objectForKey:@"Data"]; - NSURLResponse * response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"application/data" - expectedContentLength:[data length] textEncodingName:nil]; - [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; - [[self client] URLProtocol:self didLoadData:data]; - [[self client] URLProtocolDidFinishLoading:self]; - [response release]; -} - -- (void) stopLoading -{ -} - -+ (void) startLoadingWithMessage:(MCOAbstractMessage *)message - partUniqueID:(NSString *)partUniqueID - data:(NSData *)data - request:(NSMutableURLRequest *)request -{ - [NSURLProtocol setProperty:message - forKey:@"Message" inRequest:request]; - if (data != NULL) { - [NSURLProtocol setProperty:data forKey:@"Data" inRequest:request]; - } - [NSURLProtocol setProperty:partUniqueID forKey:@"PartUniqueID" inRequest:request]; -} - -+ (void) partDownloadedMessage:(MCOAbstractMessage *)message - partUniqueID:(NSString *)partUniqueID - data:(NSData *)data -{ - NSMutableDictionary * userInfo = [[NSMutableDictionary alloc] init]; - [userInfo setObject:message forKey:@"Message"]; - [userInfo setObject:partUniqueID forKey:@"PartUniqueID"]; - if (data != NULL) { - [userInfo setObject:data forKey:@"Data"]; - } - [[NSNotificationCenter defaultCenter] postNotificationName:MCOCIDURLProtocolDownloadedNotification object:nil userInfo:userInfo]; - [userInfo release]; - //NSLog(@"downloaded %p %@", self, partUniqueID); -} - -@end -- cgit v1.2.3