send_email_controller.mm 3.68 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

#include <QtCore>

26 27
#include "ios/src/send_email_controller.h"

28 29
@implementation SimpleEmailSendController

30 31
- (void) showCantSendMailAlert
{
32 33 34 35 36 37
	UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Can't send email", @"Can't send email")
		message:NSLocalizedString(@"The device is not configured for sending email", @"The device is not configured for sending email")
		preferredStyle:UIAlertControllerStyleAlert];
	UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
	[alert addAction:defaultAction];
	[self presentViewController:alert animated:YES completion:nil];
38 39
}

40 41
- (NSString *)guessMIMETypeFromFileName:(NSString *)fileName
{
42 43 44 45 46 47 48 49 50
	CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[fileName pathExtension], NULL);
	CFStringRef MIMEType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
	CFRelease(UTI);
	if (!MIMEType) {
		return @"application/octet-stream";
	}
	return (__bridge NSString *)(MIMEType);
}

51 52
- (void)createEmail:(NSString *)body rec:(NSString *)recipient
    sub:(NSString *)subject files:(NSArray *)filePaths
53 54 55 56 57 58 59 60 61
{
	if (![MFMailComposeViewController canSendMail]) {
		[self showCantSendMailAlert];
		return;
	}

	MFMailComposeViewController *controller =
	    [[MFMailComposeViewController alloc] init];
	controller.mailComposeDelegate = self;
62
	[controller setToRecipients:@[recipient]];
63 64 65 66 67 68 69 70 71 72 73
	[controller setSubject:subject];
	[controller setMessageBody:body isHTML:NO];

	// attachments
	for (NSUInteger i = 0; i < [filePaths count]; i++) {
		NSData *filedata = [NSData dataWithContentsOfFile:filePaths[i]];
		NSString *filename = [filePaths[i] lastPathComponent];
		NSString *mimetype = [self guessMIMETypeFromFileName:filename];
		[controller addAttachmentData:filedata mimeType:mimetype fileName:filename];
	}

74
	if (controller) [self presentViewController:controller animated:YES completion:nil];
75 76 77 78 79 80
	[controller release];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
    didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
81 82
	Q_UNUSED(controller);

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	if (error) {
		NSLog(@"Error : %@", error);
	}

	switch (result) {
	case MFMailComposeResultSent:
		NSLog(@"You sent the email.");
		break;
	case MFMailComposeResultSaved:
		NSLog(@"You saved a draft of this email");
		break;
	case MFMailComposeResultCancelled:
		NSLog(@"You cancelled sending this email.");
		break;
	case MFMailComposeResultFailed:
		NSLog(@"Mail failed:  An error occurred when trying to compose this email");
		break;
	default:
		NSLog(@"An error occurred when trying to compose this email");
		break;
	}

105
	[self dismissViewControllerAnimated:YES completion:nil];
106 107 108
}

@end