ShareViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. //
  2. // ShareViewController.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 26/01/16.
  6. // Copyright (c) 2016 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] getCapabilitiesServerIntWithAccount:tableAccount.account elements:NCElementsJSON.shared.capabilitiesVersionMajor];
  50. NSString *webDavRoot = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:tableAccount.account elements:NCElementsJSON.shared.capabilitiesWebDavRoot];
  51. // Networking
  52. [[NCCommunicationCommon shared] setupWithAccount:tableAccount.account user: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 shared]];
  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. NSString *themingColor = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.activeAccount elements:NCElementsJSON.shared.capabilitiesThemingColor];
  132. NSString *themingColorElement = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.activeAccount elements:NCElementsJSON.shared.capabilitiesThemingColorElement];
  133. NSString *themingColorText = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:self.activeAccount elements:NCElementsJSON.shared.capabilitiesThemingColorText];
  134. [CCGraphics settingThemingColor:themingColor themingColorElement:themingColorElement themingColorText:themingColorText];
  135. }
  136. self.navigationController.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand;
  137. self.navigationController.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText;
  138. self.toolBar.barTintColor = NCBrandColor.sharedInstance.tabBar;
  139. self.toolBar.tintColor = [UIColor grayColor];
  140. // Upload
  141. rightButtonUpload = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_save_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(selectPost)];
  142. // Cancel
  143. leftButtonCancel = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIBarButtonItemStylePlain target:self action:@selector(cancelPost)];
  144. // Title
  145. [self.navigationController.navigationBar setTitleTextAttributes: @{NSForegroundColorAttributeName:self.navigationController.navigationBar.tintColor}];
  146. self.navigationItem.title = [NCBrandOptions sharedInstance].brand;
  147. self.navigationItem.leftBarButtonItem = leftButtonCancel;
  148. self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects:rightButtonUpload, nil];
  149. self.navigationItem.hidesBackButton = YES;
  150. }
  151. - (void)moveServerUrlTo:(NSString *)serverUrlTo title:(NSString *)title type:(NSString *)type
  152. {
  153. // DENIED e2e
  154. if ([CCUtility isFolderEncrypted:serverUrlTo e2eEncrypted:false account:self.activeAccount]) {
  155. return;
  156. }
  157. if (serverUrlTo)
  158. _serverUrl = serverUrlTo;
  159. if (title) {
  160. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), title];
  161. [CCUtility setTitleServerUrlExt:title];
  162. } else {
  163. self.destinyFolderButton.title = [NSString stringWithFormat:NSLocalizedString(@"_destiny_folder_", nil), NSLocalizedString(@"_home_", nil)];
  164. [CCUtility setTitleServerUrlExt:NSLocalizedString(@"_home_", nil)];
  165. }
  166. [CCUtility setActiveAccountExt:self.activeAccount];
  167. [CCUtility setServerUrlExt:_serverUrl];
  168. }
  169. - (IBAction)destinyFolderButtonTapped:(UIBarButtonItem *)sender
  170. {
  171. UINavigationController* navigationController = [[UIStoryboard storyboardWithName:@"NCSelectDestination" bundle:nil] instantiateInitialViewController];
  172. NCSelectDestination *viewController = (NCSelectDestination *)navigationController.topViewController;
  173. viewController.delegate = self;
  174. viewController.move.title = NSLocalizedString(@"_select_", nil);
  175. viewController.tintColor = tintColor;
  176. viewController.barTintColor = barTintColor;
  177. viewController.tintColorTitle = tintColor;
  178. // E2EE
  179. viewController.includeDirectoryE2EEncryption = NO;
  180. [navigationController setModalPresentationStyle:UIModalPresentationFormSheet];
  181. [self presentViewController:navigationController animated:YES completion:nil];
  182. }
  183. - (void)selectPost
  184. {
  185. if ([self.filesName count] > 0) {
  186. [self.hud visibleHudTitle:NSLocalizedString(@"_uploading_", nil) mode:MBProgressHUDModeDeterminate color:NCBrandColor.sharedInstance.brandElement];
  187. NSString *fileName = [self.filesName objectAtIndex:0];
  188. NSString *fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
  189. // CONVERSION -->
  190. if ([fileName.pathExtension.uppercaseString isEqualToString:@"HEIC"] && [CCUtility getFormatCompatibility]) {
  191. UIImage *image = [UIImage imageWithContentsOfFile:fileNameLocal];
  192. if (image != nil) {
  193. fileName = fileName.stringByDeletingPathExtension;
  194. fileName = [NSString stringWithFormat:@"%@.jpg", fileName];
  195. [self.filesName replaceObjectAtIndex:0 withObject:fileName];
  196. fileNameLocal = [NSTemporaryDirectory() stringByAppendingString:fileName];
  197. [UIImageJPEGRepresentation(image, 1.0) writeToFile:fileNameLocal atomically:YES];
  198. [self.shareTable reloadData];
  199. }
  200. }
  201. // <--
  202. NSString *fileNameForUpload = [[NCUtility sharedInstance] createFileName:fileName serverUrl:self.serverUrl account:self.activeAccount];
  203. NSString *fileNameServer = [NSString stringWithFormat:@"%@/%@", self.serverUrl, fileNameForUpload];
  204. [[NCCommunication shared] uploadWithServerUrlFileName:fileNameServer fileNameLocalPath:fileNameLocal dateCreationFile:nil dateModificationFile:nil customUserAgent:nil addCustomHeaders:nil progressHandler:^(NSProgress * progress) {
  205. [self.hud progress:progress.fractionCompleted];
  206. } completionHandler:^(NSString *account, NSString *ocId, NSString *etag, NSDate *date, int64_t size, NSInteger errorCode, NSString *errorDescription) {
  207. [self.hud hideHud];
  208. [self.filesName removeObject:fileName];
  209. if (errorCode == 0) {
  210. [CCUtility copyFileAtPath:fileNameLocal toPath:[CCUtility getDirectoryProviderStorageOcId:ocId fileNameView:fileNameForUpload]];
  211. tableMetadata *metadata = [[NCManageDatabase sharedInstance] createMetadataWithAccount:self.activeAccount fileName:fileNameForUpload ocId:ocId serverUrl:self.serverUrl url:@"" contentType:@""];
  212. metadata.date = date;
  213. metadata.etag = etag;
  214. metadata.serverUrl = self.serverUrl;
  215. metadata.size = size;
  216. metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  217. (void)[[NCManageDatabase sharedInstance] addLocalFileWithMetadata:metadata];
  218. [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  219. [self performSelector:@selector(selectPost) withObject:nil];
  220. } else {
  221. UIAlertController * alert= [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_error_", nil) message: errorDescription preferredStyle:UIAlertControllerStyleAlert];
  222. UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
  223. handler:^(UIAlertAction * action) {
  224. [alert dismissViewControllerAnimated:YES completion:nil];
  225. [self closeShareViewController];
  226. }];
  227. [alert addAction:ok];
  228. [self presentViewController:alert animated:YES completion:nil];
  229. }
  230. }];
  231. } else {
  232. [self closeShareViewController];
  233. }
  234. }
  235. - (void)cancelPost
  236. {
  237. // rimuoviamo i file+ico
  238. for (NSString *fileName in self.filesName) {
  239. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  240. }
  241. [self closeShareViewController];
  242. }
  243. #pragma --------------------------------------------------------------------------------------------
  244. #pragma mark ===== Swipe Table DELETE -> menu =====
  245. #pragma--------------------------------------------------------------------------------------------
  246. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  247. {
  248. [self setEditing:NO animated:YES];
  249. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  250. [[NSFileManager defaultManager] removeItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil];
  251. [self.filesName removeObjectAtIndex:indexPath.row];
  252. if ([self.filesName count] == 0) [self closeShareViewController];
  253. else [self.shareTable performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  254. }
  255. #pragma --------------------------------------------------------------------------------------------
  256. #pragma mark == Table ==
  257. #pragma --------------------------------------------------------------------------------------------
  258. - (void)loadDataSwift
  259. {
  260. CCloadItemData *loadItem = [[CCloadItemData alloc] init];
  261. [loadItem loadFiles:NSTemporaryDirectory() extensionContext:self.extensionContext vc:self];
  262. }
  263. - (void)reloadData:(NSArray *)files
  264. {
  265. totalSize = 0;
  266. for (NSString *file in files) {
  267. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:file] error:nil] fileSize];
  268. totalSize += fileSize;
  269. }
  270. if (totalSize > 0) {
  271. self.filesName = [[NSMutableArray alloc] initWithArray:files];
  272. [self.shareTable reloadData];
  273. } else {
  274. [self closeShareViewController];
  275. }
  276. }
  277. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  278. {
  279. return 80;
  280. }
  281. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  282. {
  283. return 1;
  284. }
  285. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  286. {
  287. return [self.filesName count];
  288. }
  289. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  290. {
  291. NSString *fileName = [self.filesName objectAtIndex:indexPath.row];
  292. UIImage *image = nil;
  293. CCCellShareExt *cell = (CCCellShareExt *)[tableView dequeueReusableCellWithIdentifier:@"ShareExtCell" forIndexPath:indexPath];
  294. CFStringRef fileExtension = (__bridge CFStringRef)[fileName pathExtension];
  295. CFStringRef fileUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
  296. if (UTTypeConformsTo(fileUTI, kUTTypeZipArchive) && [(__bridge NSString *)fileUTI containsString:@"org.openxmlformats"] == NO) image = [UIImage imageNamed:@"file_compress"];
  297. else if (UTTypeConformsTo(fileUTI, kUTTypeAudio)) image = [UIImage imageNamed:@"file_audio"];
  298. else if (UTTypeConformsTo(fileUTI, kUTTypeMovie)) image = [UIImage imageNamed:@"file_movie"];
  299. else if (UTTypeConformsTo(fileUTI, kUTTypeImage)) {
  300. image = [UIImage imageWithContentsOfFile:[NSTemporaryDirectory() stringByAppendingString:fileName]];
  301. if (image) {
  302. image = [NCUtility.sharedInstance resizeImageWithImage:image newWidth:cell.frame.size.width];
  303. } else {
  304. image = [UIImage imageNamed:@"file_photo"];
  305. }
  306. }
  307. else if (UTTypeConformsTo(fileUTI, kUTTypeContent)) {
  308. image = [UIImage imageNamed:@"document"];
  309. NSString *typeFile = (__bridge NSString *)fileUTI;
  310. if ([typeFile isEqualToString:@"com.adobe.pdf"]) image = [UIImage imageNamed:@"file_pdf"];
  311. if ([typeFile isEqualToString:@"org.openxmlformats.spreadsheetml.sheet"]) image = [UIImage imageNamed:@"file_xls"];
  312. if ([typeFile isEqualToString:@"public.plain-text"]) image = [UIImage imageNamed:@"file_txt"];
  313. }
  314. else image = [UIImage imageNamed:@"file"];
  315. NSUInteger fileSize = (NSInteger)[[[NSFileManager defaultManager] attributesOfItemAtPath:[NSTemporaryDirectory() stringByAppendingString:fileName] error:nil] fileSize];
  316. cell.fileImageView.image = image;
  317. cell.fileName.text = fileName;
  318. cell.fileName.textColor = NCBrandColor.sharedInstance.textView;
  319. cell.fileName.delegate = self;
  320. cell.info.text = [CCUtility transformedSize:fileSize];
  321. cell.info.textColor = NCBrandColor.sharedInstance.textView;
  322. return cell;
  323. }
  324. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  325. {
  326. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  327. }
  328. @end