-
-
Save w-i-n-s/ba53ef49818ea6db79be to your computer and use it in GitHub Desktop.
MailCore tip2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)updateDownloadedAttachment:(Attachment*)attachment withFilePath:(NSString*)filePath andMessage:(Message*)aMessage{ | |
NSManagedObjectContext *moc = [self getContextForBGTask]; | |
[moc performBlock:^{ | |
Attachment *__attachment = (Attachment*)[moc objectWithID:[attachment objectID]]; | |
if(!__attachment ){ //(error){ | |
NSLog(@"Error get attach %s - %@",__func__,__attachment); | |
return; | |
} | |
[__attachment setIsDownloaded:@(YES)]; | |
[__attachment setFilePath:filePath]; | |
[self saveContextForBGTask:moc]; | |
Message *message = (Message*)[moc objectWithID:[aMessage objectID]]; | |
if (!message || ![message mcUid]) { | |
NSLog(@"Something wrong with attach %@",filePath); | |
return; | |
} | |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isDownloaded == nil || isDownloaded == %@",@(NO)]; | |
NSArray *unloadedAttaches = [[[message attachments] allObjects] filteredArrayUsingPredicate:predicate]; | |
if (![unloadedAttaches count]){ | |
[message setIsDownloadAllAtachments:@(YES)]; | |
[[NSNotificationCenter defaultCenter] postNotificationName:NMNotif_dataSingleton_updateMessageBody | |
object:self | |
userInfo:[NSDictionary dictionaryWithObject:[message mcUid] | |
forKey:@"uid"]]; | |
predicate = [NSPredicate predicateWithFormat:@"type == %@",@(NMAttachmentTypeCalendarInvite)]; | |
NSArray *invitations = [[[message attachments] allObjects] filteredArrayUsingPredicate:predicate]; | |
if ([invitations count]){ | |
Attachment *invite = [invitations firstObject]; | |
if (invite && | |
[invite filePath]) { | |
NSError *error; | |
NSURL *url = [[NSURL alloc] initFileURLWithPath:[invite filePath]]; | |
NSString *icsString = [NSString stringWithContentsOfURL:url | |
encoding:NSUTF8StringEncoding | |
error:&error]; | |
BMIcsToHtmlConverter *converter = [[BMIcsToHtmlConverter alloc] init]; | |
NSString *htmlHeader = [converter htmlHeaderStringFromICSString:icsString]; | |
icsString = nil; | |
icsString = [message bodyHTML]; | |
NSRange range = [icsString rangeOfString:@"reply="]; | |
if (range.location==NSNotFound){ | |
if(!icsString) | |
[message setBodyHTML:htmlHeader]; | |
else{ | |
NSRange range = [icsString rangeOfString:@"<body>"]; | |
if (range.location==NSNotFound) { | |
[message setBodyHTML:htmlHeader]; | |
}else{ | |
icsString = nil; | |
icsString = [NSString stringWithFormat:@"%@%@%@", | |
[[message bodyHTML] substringToIndex:range.location], | |
htmlHeader, | |
[[message bodyHTML] substringFromIndex:range.location]]; | |
if(icsString) | |
[message setBodyHTML:icsString]; | |
[self saveContextForBGTask:moc]; | |
[[NSNotificationCenter defaultCenter] postNotificationName:NMNotif_dataSingleton_updateMessageBody | |
object:self | |
userInfo:[NSDictionary dictionaryWithObject:[message mcUid] | |
forKey:@"uid"]]; | |
} | |
} | |
} | |
} | |
} | |
[self saveContextForBGTask:moc]; | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment