ShareViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // ShareViewController.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 26/01/16.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "ShareViewController.h"
  24. #import "NCBridgeSwift.h"
  25. @import MobileCoreServices;
  26. @interface ShareViewController () <NCSelectDestinationDelegate>
  27. {
  28. NSURL *dirGroup;
  29. NSUInteger totalSize;
  30. NSExtensionItem *inputItem;
  31. UIColor *barTintColor;
  32. UIColor *tintColor;
  33. NSString *fileNameOriginal;
  34. UIBarButtonItem *rightButtonUpload, *leftButtonCancel;
  35. }
  36. @end
  37. @implementation ShareViewController
  38. #pragma --------------------------------------------------------------------------------------------
  39. #pragma mark ===== View =====
  40. #pragma --------------------------------------------------------------------------------------------
  41. -(void)viewDidLoad
  42. {
  43. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  44. if (tableAccount == nil) {
  45. // close now
  46. [self performSelector:@selector(closeShareViewController) withObject:nil afterDelay:0.1];
  47. return;
  48. } else {
  49. NSInteger serverVersionMajor = [[NCManageDatabase sharedInstance] getCapabilitiesServerVersionMajorWithAccount:tableAccount.account];
  50. NSString *webDavRoot = [[NCManageDatabase sharedInstance] getCapabilitiesWebDavRootWithAccount:tableAccount.account];
  51. // Networking
  52. [[NCCommunicationCommon sharedInstance] setupWithUser:tableAccount.user userId:tableAccount.userID password:[CCUtility getPassword:tableAccount.account] url:tableAccount.url userAgent:[CCUtility getUserAgent] capabilitiesGroup:[NCBrandOptions sharedInstance].capabilitiesGroups webDavRoot:webDavRoot davRoot:nil nextcloudVersion:serverVersionMajor delegate:[NCNetworking sharedInstance]];
  53. _activeAccount = tableAccount.account;
  54. if ([_activeAccount isEqualToString:[CCUtility getActiveAccountExt]]) {
  55. // load
  56. _serverUrl = [CCUtility getServerUrlExt];
  57. _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), [CCUtility getTitleServerUrlExt]];
  58. } else {
  59. // Default settings
  60. [CCUtility setActiveAccountExt:self.activeAccount];
  61. _serverUrl = [CCUtility getHomeServerUrlActiveUrl:tableAccount.url];
  62. [CCUtility setServerUrlExt:_serverUrl];
  63. _destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  64. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  65. }
  66. }
  67. self.filesName = [[NSMutableArray alloc] init];
  68. self.hud = [[CCHud alloc] initWithView:self.navigationController.view];
  69. [self.shareTable registerNib:[UINib nibWithNibName:@"CCCellShareExt" bundle:nil] forCellReuseIdentifier:@"ShareExtCell"];
  70. [self navigationBarToolBar];
  71. [self loadDataSwift];
  72. }
  73. - (void)viewWillAppear:(BOOL)animated
  74. {
  75. [super viewWillAppear:animated];
  76. self.view.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  77. self.shareTable.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  78. }
  79. - (void)closeShareViewController
  80. {
  81. [self.extensionContext completeRequestReturningItems:self.extensionContext.inputItems completionHandler:nil];
  82. }
  83. - (void)applicationWillTerminate:(UIApplication *)application
  84. {
  85. NSLog(@"[LOG] bye bye, Nextcloud Share Extension!");
  86. }
  87. #pragma --------------------------------------------------------------------------------------------
  88. #pragma mark == TextField Delegate ==
  89. #pragma --------------------------------------------------------------------------------------------
  90. - (void)textFieldDidBeginEditing:(UITextField *)textField
  91. {
  92. rightButtonUpload.enabled = false;
  93. fileNameOriginal = textField.text;
  94. }
  95. - (void)textFieldDidEndEditing:(UITextField *)textField
  96. {
  97. rightButtonUpload.enabled = true;
  98. NSInteger index = [self.filesName indexOfObject:fileNameOriginal];
  99. if (index != NSNotFound) {
  100. self.filesName[index] = textField.text;
  101. }
  102. }
  103. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  104. {
  105. NSError *error;
  106. if ([string isEqualToString:@"\n"]) {
  107. //NSString *fileName = [textField.text stringByDeletingPathExtension];
  108. NSString *ext = [textField.text pathExtension];
  109. if (![ext isEqualToString:@""]) {
  110. NSString *fileNameAtPath = [NSTemporaryDirectory() stringByAppendingString:fileNameOriginal];
  111. NSString *fileNameToPath = [NSTemporaryDirectory() stringByAppendingString:textField.text];
  112. [[NSFileManager defaultManager] moveItemAtPath:fileNameAtPath toPath:fileNameToPath error:&error];
  113. if (error != nil) {
  114. textField.text = fileNameOriginal;
  115. }
  116. } else {
  117. textField.text = fileNameOriginal;
  118. }
  119. [textField endEditing:true];
  120. return false;
  121. }
  122. return true;
  123. }
  124. #pragma --------------------------------------------------------------------------------------------
  125. #pragma mark == Action ==
  126. #pragma --------------------------------------------------------------------------------------------
  127. - (void)navigationBarToolBar
  128. {
  129. // Theming
  130. if ([NCBrandOptions sharedInstance].use_themingColor) {
  131. tableCapabilities *capabilities = [[NCManageDatabase sharedInstance] getCapabilitesWithAccount:self.activeAccount];
  132. [CCGraphics settingThemingColor:capabilities.themingColor themingColorElement:capabilities.themingColorElement themingColorText:capabilities.themingColorText];
  133. }
  134. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  135. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  136. self.toolBar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  137. self.toolBar.tintColor = [UIColor grayColor];
  138. // Upload
  139. rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
  140. // Cancel
  141. leftButtonCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelPost)];
  142. // Title
  143. [self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
  144. self.navigationItem.title = [NCBrandOptions sharedInstance].brand;
  145. self.navigationItem.leftBarButtonItem = leftButtonCancel;
  146. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:rightButtonUpload, nil];
  147. self.navigationItem.hidesBackButton = YES;
  148. }
  149. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title type:(NSString *)type
  150. {
  151. // DENIED e2e
  152. if ([CCUtility isFolderEncrypted:serverUrlTo e2eEncrypted:false account:self.activeAccount]) {
  153. return;
  154. }
  155. if (serverUrlTo)
  156. _serverUrl = serverUrlTo;
  157. if (title) {
  158. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), title];
  159. [CCUtility setTitleServerUrlExt:title];
  160. } else {
  161. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  162. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  163. }
  164. [CCUtility setActiveAccountExt:self.activeAccount];
  165. [CCUtility setServerUrlExt:_serverUrl];
  166. }
  167. - (IBAction)destinyFolderButtonTapped:(UIBarButtonItem *)sender
  168. {
  169. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
  170. NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
  171. viewController.delegate = self;
  172. viewController.move.title = NSLocalizedString(@"_select_", nil);
  173. viewController.tintColor = tintColor;
  174. viewController.barTintColor = barTintColor;
  175. viewController.tintColorTitle = tintColor;
  176. // E2EE
  177. viewController.includeDirectoryE2EEncryption = NO;
  178. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  179. [self presentViewController:navigationController animated:YES completion:nil];
  180. }
  181. - (void)selectPost
  182. {
  183. if ([self.filesName count] > 0) {
  184. [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:NCBrandColor.sharedInstance.brandElement];
  185. NSString *fileName = [self.filesName objectAtIndex:0];
  186. NSString *fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
  187. // CONVERSION -->
  188. if ([fileName.pathExtension.uppercaseString isEqualToString:@"HEIC"] && [CCUtility getFormatCompatibility]) {
  189. UIImage *image = [UIImage imageWithContentsOfFile:fileNameLocal];
  190. if (image != nil) {
  191. fileName = fileName.stringByDeletingPathExtension;
  192. fileName = [NSString stringWithFormat:@"%@.jpg", fileName];
  193. [self.filesName replaceObjectAtIndex:0 withObject:fileName];
  194. fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
  195. [UIImageJPEGRepresentation(image, 1.0) writeToFile:fileNameLocal atomically:YES];
  196. [self.shareTable reloadData];
  197. }
  198. }
  199. // <--
  200. NSString *fileNameForUpload = [[NCUtility sharedInstance] createFileName:fileName serverUrl:self.serverUrl account:self.activeAccount];
  201. NSString *fileNameServer = [NSString stringWithFormat:@"%@/%@", self.serverUrl, fileNameForUpload];
  202. (void)[[NCCommunication sharedInstance] uploadWithServerUrlFileName:fileNameServer fileNameLocalPath:fileNameLocal dateCreationFile:nil dateModificationFile:nil customUserAgent:nil addCustomHeaders:nil account:self.activeAccount progressHandler:^(NSProgress * progress) {
  203. [self.hud progress:progress.fractionCompleted];
  204. } completionHandler:^(NSString *account, NSString *ocId, NSString *etag, NSDate *date, int64_t size, NSInteger errorCode, NSString *errorDescription) {
  205. [self.hud hideHud];
  206. [self.filesName removeObject:fileName];
  207. if (errorCode == 0) {
  208. [CCUtility copyFileAtPath:fileNameLocal toPath:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileNameForUpload]];
  209. tableMetadata *metadata = [[NCManageDatabase sharedInstance] createMetadataWithAccount:self.activeAccount fileName:fileNameForUpload ocId:ocId serverUrl:self.serverUrl url:@"" contentType:@""];
  210. metadata.date = date;
  211. metadata.etag = etag;
  212. metadata.serverUrl = self.serverUrl;
  213. metadata.size = size;
  214. metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  215. (void)[[NCManageDatabase sharedInstance] addLocalFileWithMetadata:metadata];
  216. [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  217. [self performSelector:@selector(selectPost) withObject:nil];
  218. } else {
  219. UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message: errorDescription preferredStyle:UIAlertControllerStyleAlert];
  220. UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  221. handler:^(UIAlertAction * action) {
  222. [alert dismissViewControllerAnimated:YES completion:nil];
  223. [self closeShareViewController];
  224. }];
  225. [alert addAction:ok];
  226. [self presentViewController:alert animated:YES completion:nil];
  227. }
  228. }];
  229. } else {
  230. [self closeShareViewController];
  231. }
  232. }
  233. - (void)cancelPost
  234. {
  235. // rimuoviamo i file+ico
  236. for (NSString *fileName in self.filesName) {
  237. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  238. }
  239. [self closeShareViewController];
  240. }
  241. #pragma --------------------------------------------------------------------------------------------
  242. #pragma mark ===== Swipe Table DELETE -> menu =====
  243. #pragma--------------------------------------------------------------------------------------------
  244. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  245. {
  246. [self setEditing:NO animated:YES];
  247. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  248. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  249. [self.filesName removeObjectAtIndex:indexPath.row];
  250. if ([self.filesName count] == 0) [self closeShareViewController];
  251. else [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  252. }
  253. #pragma --------------------------------------------------------------------------------------------
  254. #pragma mark == Table ==
  255. #pragma --------------------------------------------------------------------------------------------
  256. - (void)loadDataSwift
  257. {
  258. CCloadItemData *loadItem = [[CCloadItemData alloc] init];
  259. [loadItem loadFiles:NSTemporaryDirectory() extensionContext:self.extensionContext vc:self];
  260. }
  261. - (void)reloadData:(NSArray *)files
  262. {
  263. totalSize = 0;
  264. for (NSString *file in files) {
  265. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:file] error:nil] fileSize];
  266. totalSize += fileSize;
  267. }
  268. if (totalSize > 0) {
  269. self.filesName = [[NSMutableArray alloc] initWithArray:files];
  270. [self.shareTable reloadData];
  271. } else {
  272. [self closeShareViewController];
  273. }
  274. }
  275. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  276. {
  277. return 80;
  278. }
  279. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  280. {
  281. return 1;
  282. }
  283. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  284. {
  285. return [self.filesName count];
  286. }
  287. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  288. {
  289. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  290. UIImage *image = nil;
  291. CCCellShareExt *cell = (CCCellShareExt *)[tableView dequeueReusableCellWithIdentifier:@"ShareExtCell" forIndexPath:indexPath];
  292. CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
  293. CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  294. if (UTTypeConformsTo(fileUTI, kUTTypeZipArchive) && [(__bridge NSString *)fileUTI containsString:@"org.openxmlformats"] == NO) image = [UIImage imageNamed:@"file_compress"];
  295. else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) image = [UIImage imageNamed:@"file_audio"];
  296. else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) image = [UIImage imageNamed:@"file_movie"];
  297. else if (UTTypeConformsTo(fileUTI, kUTTypeImage)) {
  298. image = [UIImage imageWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:fileName]];
  299. if (image) {
  300. image = [NCUtility.sharedInstance resizeImageWithImage:image newWidth:cell.frame.size.width];
  301. } else {
  302. image = [UIImage imageNamed:@"file_photo"];
  303. }
  304. }
  305. else if (UTTypeConformsTo(fileUTI, kUTTypeContent)) {
  306. image = [UIImage imageNamed:@"document"];
  307. NSString *typeFile = (__bridge NSString *)fileUTI;
  308. if ([typeFile isEqualToString:@"com.adobe.pdf"]) image = [UIImage imageNamed:@"file_pdf"];
  309. if ([typeFile isEqualToString:@"org.openxmlformats.spreadsheetml.sheet"]) image = [UIImage imageNamed:@"file_xls"];
  310. if ([typeFile isEqualToString:@"public.plain-text"]) image = [UIImage imageNamed:@"file_txt"];
  311. }
  312. else image = [UIImage imageNamed:@"file"];
  313. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil] fileSize];
  314. cell.fileImageView.image = image;
  315. cell.fileName.text = fileName;
  316. cell.fileName.textColor = NCBrandColor.sharedInstance.textView;
  317. cell.fileName.delegate = self;
  318. cell.info.text = [CCUtility transformedSize:fileSize];
  319. cell.info.textColor = NCBrandColor.sharedInstance.textView;
  320. return cell;
  321. }
  322. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  323. {
  324. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  325. }
  326. @end