ShareController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ShareController.h"
  21. #import "ConversationsViewController.h"
  22. #import "AppDelegate.h"
  23. #import "UIDefines.h"
  24. #import "ContactGroupPickerViewController.h"
  25. #import "FeatureMaskChecker.h"
  26. #import "BundleUtil.h"
  27. #import "FileMessageSender.h"
  28. #import "UTIConverter.h"
  29. #import <CocoaLumberjack/CocoaLumberjack.h>
  30. #ifdef DEBUG
  31. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  32. #else
  33. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  34. #endif
  35. @interface ShareController () <ContactGroupPickerDelegate, ModalNavigationControllerDelegate, UploadProgressDelegate>
  36. @property NSMutableDictionary *info;
  37. @end
  38. @implementation ShareController
  39. - (void)startShare {
  40. _info = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  41. [NSNumber numberWithBool:YES], kKeyForceCompose,
  42. nil];
  43. if (_text) {
  44. [_info setObject:_text forKey:kKeyText];
  45. }
  46. if (_image) {
  47. [_info setObject:_image forKey:kKeyImage];
  48. }
  49. /* do we already know the target contact? */
  50. if (_contact != nil) {
  51. [self shareWithContact];
  52. } else {
  53. ModalNavigationController *navigationController = [ContactGroupPickerViewController pickerFromStoryboardWithDelegate:self];
  54. ContactGroupPickerViewController *picker = (ContactGroupPickerViewController *)navigationController.topViewController;
  55. picker.enableMulitSelection = NO;
  56. picker.enableTextInput = NO;
  57. picker.submitOnSelect = YES;
  58. [[AppDelegate sharedAppDelegate].window.rootViewController presentViewController:navigationController animated:YES completion:nil];
  59. }
  60. }
  61. - (void)shareWithContact {
  62. [_info setObject:_contact forKey:kKeyContact];
  63. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowConversation object:nil userInfo:_info];
  64. }
  65. - (void)shareWithConversation:(Conversation *)conversation renderType:(NSNumber *)renderType sendAsFile:(BOOL)sendAsFile {
  66. if (conversation == nil) {
  67. return;
  68. }
  69. if(_url) {
  70. FeatureMaskChecker *featureMaskChecker = [[FeatureMaskChecker alloc] init];
  71. NSSet *conversations = [NSSet setWithObject:conversation];
  72. UIViewController *presentingViewController = [AppDelegate sharedAppDelegate].window.rootViewController;
  73. [featureMaskChecker checkFileTransferFor:conversations presentAlarmOn:presentingViewController onSuccess:^{
  74. URLSenderItem *item;
  75. if (sendAsFile) {
  76. NSString *mimetype = [UTIConverter mimeTypeFromUTI:[UTIConverter utiForFileURL:_url]];
  77. item = [URLSenderItem itemWithUrl:_url type:mimetype renderType:renderType sendAsFile:true];
  78. } else {
  79. item = [URLSenderItemCreator getSenderItemFor:_url];
  80. }
  81. for (Conversation *conv in conversations) {
  82. FileMessageSender *sender = [[FileMessageSender alloc] init];
  83. [sender sendItem:item inConversation:conv];
  84. sender.uploadProgressDelegate = self;
  85. }
  86. } onFailure:^{
  87. ;//nop
  88. }];
  89. } else {
  90. DDLogError(@"No URL provided, can't share anything");
  91. }
  92. [_info setObject:conversation forKey:kKeyConversation];
  93. [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationShowConversation object:nil userInfo:_info];
  94. }
  95. - (void)deleteInboxFile {
  96. if ([_url.path rangeOfString:@"Documents/Inbox"].location != NSNotFound) {
  97. [[NSFileManager defaultManager] removeItemAtURL:_url error:nil];
  98. }
  99. }
  100. #pragma mark - Contact picker delegate
  101. - (void)contactPicker:(ContactGroupPickerViewController*)contactPicker didPickConversations:(NSSet *)conversations renderType:(NSNumber *)renderType sendAsFile:(BOOL)sendAsFile {
  102. // only one expected
  103. Conversation *conversation = conversations.anyObject;
  104. [self shareWithConversation:conversation renderType:renderType sendAsFile:sendAsFile];
  105. [contactPicker dismissViewControllerAnimated:YES completion:nil];
  106. }
  107. - (void)contactPickerDidCancel:(ContactGroupPickerViewController*)contactPicker {
  108. [contactPicker dismissViewControllerAnimated:YES completion:nil];
  109. }
  110. #pragma mark - ModalNavigationControllerDelegate
  111. - (void)willDismissModalNavigationController {
  112. ;//nop
  113. }
  114. #pragma mark - UploadProgressDelegate
  115. - (void)blobMessageSender:(BlobMessageSender *)blobMessageSender uploadFailedForMessage:(BaseMessage *)message error:(UploadError)error {
  116. NSString *errorTitle = [BundleUtil localizedStringForKey:@"error_sending_failed"];
  117. NSString *errorMessage = [FileMessageSender messageForError:error];
  118. [UIAlertTemplate showAlertWithOwner:[[AppDelegate sharedAppDelegate] currentTopViewController] title:errorTitle message:errorMessage actionOk:nil];
  119. [self deleteInboxFile];
  120. }
  121. - (void)blobMessageSender:(BlobMessageSender *)blobMessageSender uploadSucceededForMessage:(BaseMessage *)message {
  122. [self deleteInboxFile];
  123. }
  124. - (BOOL)blobMessageSenderUploadShouldCancel:(BlobMessageSender *)blobMessageSender {
  125. return NO;
  126. }
  127. - (void)blobMessageSender:(BlobMessageSender *)blobMessageSender uploadProgress:(NSNumber *)progress forMessage:(BaseMessage *)message {
  128. ;//nop
  129. }
  130. @end